Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

dsDaemon.h

Go to the documentation of this file.
00001 /* $Id: dsDaemon.h,v 1.9 2004/06/10 10:58:17 dsamersoff Exp $ 
00002  * 
00003  */
00004 
00005 #ifndef dsDaemon_h
00006 #define dsDaemon_h
00007 
00008 #ifdef HAVE_CONFIG_H
00009 # include <config.h>
00010 #endif
00011 
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include <stdarg.h>
00017 
00018 #ifdef HAVE_UNISTD_H
00019 # include <unistd.h>
00020 #endif
00021 
00022 #include <signal.h>
00023 #include <string.h>
00024 #include <dirent.h>
00025 
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028 #include <sys/wait.h>
00029 #include <utime.h>
00030 #include <pwd.h>
00031 #include <time.h>
00032 #include <errno.h>
00033 
00034 #ifdef FreeBSD
00035 #include <libutil.h>
00036 #define HAVE_SETPROCTITLE
00037 #endif
00038 
00039 #include <dsSmartException.h>
00040 
00041 #define DAEMON_NOPARENTSIG 0x01
00042 #define DAEMON_NOCHILDSIG  0x02
00043 #define DAEMON_NODETACH    0x04
00044 #define DAEMON_NOCHDIR     0x08
00045 #define DAEMON_KILLPREV    0x10
00046 
00047 
00048 typedef void    Sigfunc(int);   /* for signal handlers */
00049 
00050 /* Richard Stevens's portable signal function */
00051 Sigfunc * Signal(int signo, Sigfunc *func);
00052 
00053 DECLARE_EXCEPTION(dsDaemon);
00054 
00055 inline int DaemonFlags(char *str)
00056 {
00057      int rc = 0;
00058      if ( strstr(str, "DAEMON_NOPARENTSIG") != 0 ) rc |= DAEMON_NOPARENTSIG;
00059      if ( strstr(str, "DAEMON_NOCHILDSIG")  != 0 ) rc |= DAEMON_NOCHILDSIG;
00060      if ( strstr(str, "DAEMON_NODETACH")    != 0 ) rc |= DAEMON_NODETACH;
00061      if ( strstr(str, "DAEMON_NOCHDIR")     != 0 ) rc |= DAEMON_NOCHDIR;
00062      if ( strstr(str, "DAEMON_KILLPREV")    != 0 ) rc |= DAEMON_KILLPREV;
00063 
00064      return rc;
00065 }
00066 
00078 class dsDaemon
00079 {
00080     int _max_children;
00081     char  *_progname;
00082     char  *_pid_path;
00083     pid_t *_pchild; 
00084     int    _flags;
00085 
00086 protected:
00087 
00088     void processPidFile();
00089 
00090     void addChild(pid_t npid); 
00091     void removeChild(pid_t npid);
00092   
00097     int waitForFreeSlot(pid_t pid);
00098 
00099 
00100 
00105      dsDaemon();
00106 
00107 public:
00108 
00114   dsDaemon(char *progname, int max_children = 50, int flags = 0);
00115   dsDaemon(char *progname, char *pid_path, int max_children = 50, int flags = 0);
00116 
00117   virtual ~dsDaemon();
00118 
00123   virtual int   init_globals();
00124 
00131   virtual pid_t fork_child();
00132 
00136    virtual int   child_work();
00137 
00142   virtual void  shutdown_child();
00143 
00147   virtual int   reinit();
00148 
00152   void kill_running(); 
00153 
00160   friend void daemonize(dsDaemon * sd, int options = 0);
00161 
00167  friend void dsSafeSleep(int secs, int nsecs = 0);
00168 
00172   friend  int DaemonFlags(char *str);
00173 
00175   friend void SIGCHLD_hdl(int sig);
00176   friend void SIGANY_hdl(int sig);
00177   friend void SIGINT_hdl(int sig);
00179 };
00180 
00181 
00182 
00183 #endif

Generated on Mon May 16 18:26:57 2005 for libdms4 by doxygen1.3-rc2