Annotation of rpl/src/daemon.c, revision 1.31

1.1       bertrand    1: /*
                      2: ================================================================================
1.30      bertrand    3:   RPL/2 (R) version 4.1.3
1.18      bertrand    4:   Copyright (C) 1989-2011 Dr. BERTRAND Joël
1.1       bertrand    5: 
                      6:   This file is part of RPL/2.
                      7: 
                      8:   RPL/2 is free software; you can redistribute it and/or modify it
                      9:   under the terms of the CeCILL V2 License as published by the french
                     10:   CEA, CNRS and INRIA.
                     11:  
                     12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
                     13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
                     14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
                     15:   for more details.
                     16:  
                     17:   You should have received a copy of the CeCILL License
                     18:   along with RPL/2. If not, write to info@cecill.info.
                     19: ================================================================================
                     20: */
                     21: 
                     22: 
1.12      bertrand   23: #include "rpl-conv.h"
1.1       bertrand   24: 
                     25: 
                     26: /*
                     27: ================================================================================
                     28:   Fonction de bascule vers un mode de fonctionnement de daemon
                     29: ================================================================================
                     30:   Entrées : pointeur sur une structure
                     31: --------------------------------------------------------------------------------
                     32:   Sorties :
                     33: --------------------------------------------------------------------------------
                     34:   Effets de bord : néant
                     35: ================================================================================
                     36: */
                     37: 
                     38: void
                     39: lancement_daemon(struct_processus *s_etat_processus)
                     40: {
                     41:    pid_t               pid;
                     42:    pid_t               sid;
                     43: 
                     44:    /*
                     45:     * Si le processus en cours est déjà un daemon (dont le père est init),
                     46:     * on ne fait rien.
                     47:     */
                     48: 
1.7       bertrand   49:    if (getppid() == (pid_t) 1)
1.1       bertrand   50:    {
                     51:        return;
                     52:    }
                     53: 
                     54:    /*
                     55:     * Premier fork pour lancer un setsid(). Le fork() n'est pas protégé par
                     56:     * un mutex car on ne peut transformer en daemon que le processus de
                     57:     * lancement du RPL/2.
                     58:     */
                     59: 
                     60:    fflush(NULL);
1.15      bertrand   61: 
1.1       bertrand   62:    pid = fork();
                     63: 
1.14      bertrand   64: #  ifdef OS2
                     65:    if (pid == 0)
                     66:    {
                     67:        sem_init(&semaphore_liste_threads, 0, 1);
                     68:        sem_init(&semaphore_gestionnaires_signaux, 0, 0);
                     69:        sem_init(&semaphore_gestionnaires_signaux_atomique, 0, 1);
                     70:        sem_init(&((*s_etat_processus).semaphore_fork), 0, 0);
                     71:    }
                     72: #  endif
                     73: 
1.1       bertrand   74:    if (pid < 0)
                     75:    {
                     76:        (*s_etat_processus).erreur_systeme = d_es_processus;
                     77:        return;
                     78:    }
                     79: 
                     80:    if (pid > 0)
                     81:    {
                     82:        // Fin du processus père.
1.31    ! bertrand   83:        destruction_queue_signaux(s_etat_processus);
1.1       bertrand   84:        _exit(EXIT_SUCCESS);
                     85:    }
                     86: 
                     87:    sid = setsid();
                     88: 
                     89:    if (sid < 0)
                     90:    {
                     91:        (*s_etat_processus).erreur_systeme = d_es_processus;
                     92:        return;
                     93:    }
                     94: 
                     95:    if ((chdir((*s_etat_processus).chemin_fichiers_temporaires)) < 0)
                     96:    {
                     97:        (*s_etat_processus).erreur_systeme = d_es_processus;
                     98:        return;
                     99:    }
                    100: 
                    101:    freopen("/dev/null", "r", stdin);
                    102:    freopen("/dev/null", "w", stdout);
                    103:    freopen("/dev/null", "w", stderr);
                    104: 
                    105:    /*
                    106:     * Second fork pour ne plus être un session leader.
                    107:     */
                    108: 
                    109:    fflush(NULL);
                    110:    pid = fork();
                    111: 
1.14      bertrand  112: #  ifdef OS2
                    113:    if (pid == 0)
                    114:    {
                    115:        sem_init(&semaphore_liste_threads, 0, 1);
                    116:        sem_init(&semaphore_gestionnaires_signaux, 0, 0);
                    117:        sem_init(&semaphore_gestionnaires_signaux_atomique, 0, 1);
                    118:        sem_init(&((*s_etat_processus).semaphore_fork), 0, 0);
                    119:    }
                    120: #  endif
                    121: 
1.1       bertrand  122:    if (pid < 0)
                    123:    {
                    124:        (*s_etat_processus).erreur_systeme = d_es_processus;
                    125:        return;
                    126:    }
                    127: 
                    128:    if (pid > 0)
                    129:    {
                    130:        // Fin du processus père.
                    131:        _exit(EXIT_SUCCESS);
                    132:    }
                    133: 
                    134:    (*s_etat_processus).pid_processus_pere = getpid();
1.31    ! bertrand  135:    creation_queue_signaux(s_etat_processus);
1.1       bertrand  136:    modification_pid_thread_pere(s_etat_processus);
1.31    ! bertrand  137:    (*s_etat_processus).tid_courant = pthread_self();
1.1       bertrand  138: 
                    139:    return;
                    140: }
                    141: 
                    142: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>