Main Page | File List

SDaemonFramework.h

Go to the documentation of this file.
00001 
00005 //
00006 // Date:      19 May 2003
00007 //            11 August 2003
00008 //            27 November 2005
00009 //            2  December 2005  umask(0) added
00010 //                              sigwait() instead of handlers
00011 //
00012 // Copyright (c) Sergey Satskiy 2003
00013 //               <sergesatsky@yahoo.com>
00014 //
00015 // Permission to copy, use, modify, sell and distribute this software 
00016 // is granted provided this copyright notice appears in all copies. 
00017 // This software is provided "as is" without express or implied
00018 // warranty, and with no claim as to its suitability for any purpose.
00019 //
00020 
00021 
00022 
00023 #ifndef SDAEMONFRAMEWORK_H
00024 #define SDAEMONFRAMEWORK_H
00025 
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <signal.h>
00029 #include <sys/stat.h>
00030 
00031 
00032     // Anonymous namespace gives ability compiled with no problems
00033     // even if the file is included into many .cpp files
00034 namespace
00035 {
00036 
00037     inline void SignalOK( void );
00038     inline void SignalFailed( void );
00039 
00042     inline void SignalOK( void )
00043     {
00044         #if DAEMON == 1
00045         kill( getppid(), SIGUSR1 );
00046         #endif
00047     }
00048 
00051     inline void SignalFailed( void )
00052     {
00053         #if DAEMON == 1
00054         kill( getppid(), SIGUSR2 );
00055         #endif
00056     }
00057 
00058      /*template <typename Fake>*/
00061     inline void Daemonise( void )
00062     {
00063 
00064          #if DAEMON == 1
00065          if ( getppid() != 1 )
00066          {
00067             sigset_t           Feedback;    // Mask to wait for feedback
00068             sigset_t           Storage;     // Old mask - used to restore
00069 
00070             sigemptyset( &Feedback );
00071             sigaddset( &Feedback, SIGUSR1 );
00072             sigaddset( &Feedback, SIGUSR2 );
00073             sigprocmask( SIG_BLOCK, &Feedback, &Storage );
00074 
00075             signal( SIGTTOU, SIG_IGN );
00076             signal( SIGTTIN, SIG_IGN );
00077             signal( SIGTSTP, SIG_IGN );
00078     
00079                 // Become a daemon
00080             if ( fork() != 0 )
00081             {
00082                 int     SignalNumber( -1 );
00083 
00084                 if ( sigwait( &Feedback, &SignalNumber ) != 0 )
00085                 {
00086                         // Error waiting for the signal
00087                     exit( 2 );
00088                 }
00089 
00090                 exit( SignalNumber != SIGUSR1 );
00091             }
00092 
00093                 // Child process - restore the signals mask
00094                 // and set a new leader of the session
00095             if ( (sigprocmask( SIG_SETMASK, &Storage, NULL ) != 0) ||
00096                  ( setsid() == -1 ) )
00097             {
00098                 SignalFailed();
00099                 exit( 1 );
00100             }
00101         }
00102 
00103             // Close file descriptors of stdin, stdout and stderr
00104         if ( ttyname( 0 ) != NULL ) close( 0 );
00105         if ( ttyname( 1 ) != NULL ) close( 1 );
00106         if ( ttyname( 2 ) != NULL ) close( 2 );
00107         
00108 
00109         #ifdef HOME
00110         chdir( HOME );              // To be sure not staying nowhere
00111         setenv( "HOME", HOME, 1 );  
00112         #else
00113         chdir( "/" );
00114         setenv( "HOME", "/", 1 );
00115         #endif                      // #ifdef HOME
00116         
00117         #endif      // #if DAEMON == 1
00118     
00119             // Daemon should not inherit the files mask
00120             // To make the non-daemon process behaviour uniform do it 
00121             //    in any case
00122         umask( 0 );
00123 
00124         return;
00125     }
00126 
00127 
00128 }   // End of anonymous namespace
00129 
00130 
00131 #endif
00132 

Generated on Tue Dec 6 02:14:19 2005 for Daemon framework by  doxygen 1.4.4