File:  [local] / rpl / src / daemon.c
Revision 1.29: download - view: text, annotated - select for diffs - revision graph
Mon Jul 25 07:44:53 2011 UTC (12 years, 9 months ago) by bertrand
Branches: MAIN
CVS tags: rpl-4_1_2, HEAD
En route pour la 4.1.2.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.2
    4:   Copyright (C) 1989-2011 Dr. BERTRAND Joël
    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: 
   23: #include "rpl-conv.h"
   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: 
   49:     if (getppid() == (pid_t) 1)
   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);
   61: 
   62: #   ifdef _BROKEN_SIGINFO
   63:     destruction_fifos_signaux(s_etat_processus);
   64: #   endif
   65: 
   66:     pid = fork();
   67: 
   68: #   ifdef OS2
   69:     if (pid == 0)
   70:     {
   71:         sem_init(&semaphore_liste_threads, 0, 1);
   72:         sem_init(&semaphore_gestionnaires_signaux, 0, 0);
   73:         sem_init(&semaphore_gestionnaires_signaux_atomique, 0, 1);
   74:         sem_init(&((*s_etat_processus).semaphore_fork), 0, 0);
   75:     }
   76: #   endif
   77: 
   78:     if (pid < 0)
   79:     {
   80:         (*s_etat_processus).erreur_systeme = d_es_processus;
   81:         return;
   82:     }
   83: 
   84:     if (pid > 0)
   85:     {
   86:         // Fin du processus père.
   87:         _exit(EXIT_SUCCESS);
   88:     }
   89: 
   90:     sid = setsid();
   91: 
   92:     if (sid < 0)
   93:     {
   94:         (*s_etat_processus).erreur_systeme = d_es_processus;
   95:         return;
   96:     }
   97: 
   98:     if ((chdir((*s_etat_processus).chemin_fichiers_temporaires)) < 0)
   99:     {
  100:         (*s_etat_processus).erreur_systeme = d_es_processus;
  101:         return;
  102:     }
  103: 
  104:     freopen("/dev/null", "r", stdin);
  105:     freopen("/dev/null", "w", stdout);
  106:     freopen("/dev/null", "w", stderr);
  107: 
  108:     /*
  109:      * Second fork pour ne plus être un session leader.
  110:      */
  111: 
  112:     fflush(NULL);
  113:     pid = fork();
  114: 
  115: #   ifdef _BROKEN_SIGINFO
  116:     creation_fifos_signaux(s_etat_processus);
  117: #   endif
  118: 
  119: #   ifdef OS2
  120:     if (pid == 0)
  121:     {
  122:         sem_init(&semaphore_liste_threads, 0, 1);
  123:         sem_init(&semaphore_gestionnaires_signaux, 0, 0);
  124:         sem_init(&semaphore_gestionnaires_signaux_atomique, 0, 1);
  125:         sem_init(&((*s_etat_processus).semaphore_fork), 0, 0);
  126:     }
  127: #   endif
  128: 
  129:     if (pid < 0)
  130:     {
  131:         (*s_etat_processus).erreur_systeme = d_es_processus;
  132:         return;
  133:     }
  134: 
  135:     if (pid > 0)
  136:     {
  137:         // Fin du processus père.
  138:         _exit(EXIT_SUCCESS);
  139:     }
  140: 
  141:     (*s_etat_processus).pid_processus_pere = getpid();
  142:     modification_pid_thread_pere(s_etat_processus);
  143: 
  144:     return;
  145: }
  146: 
  147: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>