File:  [local] / rpl / src / daemon.c
Revision 1.57: download - view: text, annotated - select for diffs - revision graph
Sat May 17 14:06:47 2014 UTC (10 years ago) by bertrand
Branches: MAIN
CVS tags: HEAD
Ajout de #pragma pour éviter de faux messages d'avertissement.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.18
    4:   Copyright (C) 1989-2014 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:     pid = fork();
   63: 
   64:     if (pid < 0)
   65:     {
   66:         (*s_etat_processus).erreur_systeme = d_es_processus;
   67:         return;
   68:     }
   69: 
   70:     if (pid > 0)
   71:     {
   72:         // Fin du processus père.
   73: 
   74:         // À noter : dans le cas où l'on utilise l'émulation des
   75:         // sémaphores anonymes POSIX ou la sémantique SysV, il faut
   76:         // détruire les sémaphores et les recréer dans le processus fils.
   77: 
   78: #       ifndef SEMAPHORES_NOMMES
   79:         sem_post(&((*s_etat_processus).semaphore_fork));
   80:         sem_destroy(&((*s_etat_processus).semaphore_fork));
   81: 
   82:         sem_post(&semaphore_gestionnaires_signaux);
   83:         sem_destroy(&semaphore_gestionnaires_signaux);
   84: #       else
   85:         sem_post((*s_etat_processus).semaphore_fork);
   86:         sem_destroy3((*s_etat_processus).semaphore_fork, getpid(),
   87:                 pthread_self(), SEM_FORK);
   88: 
   89:         sem_post(semaphore_gestionnaires_signaux);
   90:         sem_destroy2(semaphore_gestionnaires_signaux, getpid(), SEM_SIGNAUX);
   91: #       endif
   92: 
   93:         destruction_queue_signaux(s_etat_processus);
   94:         _exit(EXIT_SUCCESS);
   95:     }
   96: 
   97:     sid = setsid();
   98: 
   99:     if (sid < 0)
  100:     {
  101:         (*s_etat_processus).erreur_systeme = d_es_processus;
  102:         return;
  103:     }
  104: 
  105:     if ((chdir((*s_etat_processus).chemin_fichiers_temporaires)) < 0)
  106:     {
  107:         (*s_etat_processus).erreur_systeme = d_es_processus;
  108:         return;
  109:     }
  110: 
  111: #   pragma GCC diagnostic push
  112: #   pragma GCC diagnostic ignored "-Wunused-result"
  113:     freopen("/dev/null", "r", stdin);
  114:     freopen("/dev/null", "w", stdout);
  115:     freopen("/dev/null", "w", stderr);
  116: #   pragma GCC diagnostic pop
  117: 
  118:     /*
  119:      * Second fork pour ne plus être un session leader.
  120:      */
  121: 
  122:     fflush(NULL);
  123:     pid = fork();
  124: 
  125:     if (pid < 0)
  126:     {
  127:         (*s_etat_processus).erreur_systeme = d_es_processus;
  128:         return;
  129:     }
  130: 
  131:     if (pid > 0)
  132:     {
  133:         // Fin du processus père.
  134:         _exit(EXIT_SUCCESS);
  135:     }
  136: 
  137:     (*s_etat_processus).pid_processus_pere = getpid();
  138:     creation_queue_signaux(s_etat_processus);
  139:     modification_pid_thread_pere(s_etat_processus);
  140: 
  141: #   ifndef SEMAPHORES_NOMMES
  142:     sem_init(&semaphore_gestionnaires_signaux, 0, 0);
  143:     sem_init(&((*s_etat_processus).semaphore_fork), 0, 0);
  144: #   else
  145:     if ((semaphore_gestionnaires_signaux = sem_init2(0, getpid(), SEM_SIGNAUX))
  146:             == SEM_FAILED)
  147:     {
  148:         (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
  149:         return;
  150:     }
  151: 
  152:     if ((sem_init((*s_etat_processus).semaphore_fork, 0, 0)) != 0)
  153:     {
  154:         (*s_etat_processus).erreur_systeme = d_es_allocation_memoire;
  155:         return;
  156:     }
  157: #   endif
  158: 
  159:     return;
  160: }
  161: 
  162: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>