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 //
00011 // Copyright (c) Sergey Satskiy 2003
00012 //               <sergesatsky@yahoo.com>
00013 //
00014 // Permission to copy, use, modify, sell and distribute this software 
00015 // is granted provided this copyright notice appears in all copies. 
00016 // This software is provided "as is" without express or implied
00017 // warranty, and with no claim as to its suitability for any purpose.
00018 //
00019 
00020 
00021 
00022 #ifndef SDAEMONFRAMEWORK_H
00023 #define SDAEMONFRAMEWORK_H
00024 
00025 #include <unistd.h>
00026 #include <stdlib.h>
00027 #include <signal.h>
00028 #include <sys/stat.h>
00029 
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     #if DAEMON == 1
00037 
00039     volatile bool    ParentRun( true );
00040 
00042     volatile bool    DaemonOK( false );
00043 
00044 
00048     void SigOKHandler( int  Unused )
00049     {
00050         signal( Unused, SIG_IGN );
00051         DaemonOK = true; 
00052         ParentRun = false;
00053     }
00054 
00058     void SigFailedHandler( int  Unused )
00059     {
00060         signal( Unused, SIG_IGN );
00061         DaemonOK = false; 
00062         ParentRun = false;
00063     }
00064 
00065         // Signalling functions prototypes
00066     inline void SignalOK( void );
00067     inline void SignalFailed( void );
00068 
00069     #endif
00070 
00071     
00074     inline void Daemonise( void )
00075     {
00076 
00077          #if DAEMON == 1
00078          signal( SIGUSR1, SigOKHandler );
00079          signal( SIGUSR2, SigFailedHandler );
00080 
00081          if ( getppid() != 1 )
00082          {
00083             signal( SIGTTOU, SIG_IGN );
00084             signal( SIGTTIN, SIG_IGN );
00085             signal( SIGTSTP, SIG_IGN );
00086     
00087                 // Become a daemon
00088             if ( fork() != 0 )
00089             {
00090                     // Parent process
00091                 while ( ParentRun ) 
00092                 {
00093                     sleep( 1 );
00094                 }
00095             
00096                 if ( DaemonOK )     
00097                 {
00098                     exit( 0 );
00099                 }
00100 
00101                 exit( 1 );
00102             }
00103 
00104                 // Child process
00105             if ( setsid() == -1 )
00106             {
00107                 SignalFailed();
00108                 exit( 1 );
00109             }
00110         }
00111 
00112             // Close file descriptors of stdin, stdout and stderr
00113         if ( ttyname( 0 ) != NULL ) close( 0 );
00114         if ( ttyname( 1 ) != NULL ) close( 1 );
00115         if ( ttyname( 2 ) != NULL ) close( 2 );
00116         
00117 
00118         #ifdef HOME
00119         chdir( HOME );              // To be sure not staying nowhere
00120         setenv( "HOME", HOME, 1 );  
00121         #else
00122         chdir( "/" );
00123         setenv( "HOME", "/", 1 );
00124         #endif                      // #ifdef HOME
00125         
00126         #endif      // #if DAEMON == 1
00127     
00128             // Daemon should not inherit the files mask
00129             // To make the non-daemon process behaviour uniform do it 
00130             //    in any case
00131         umask( 0 );
00132 
00133         return;
00134     }
00135 
00136 
00139     inline void SignalOK( void )
00140     {
00141         #if DAEMON == 1
00142         kill( getppid(), SIGUSR1 );
00143         #endif
00144     }
00145 
00148     inline void SignalFailed( void )
00149     {
00150         #if DAEMON == 1
00151         kill( getppid(), SIGUSR2 );
00152         #endif
00153     }
00154 
00155 }   // End of anonymous namespace
00156 
00157 
00158 #endif
00159 

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