daemonize.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2002 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  */
00019 
00020 
00021 #include <config.h>
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <errno.h>
00026 #include <signal.h>
00027 
00028 #ifdef HAVE_UNISTD_H
00029 #include <unistd.h>
00030 #endif
00031 
00032 #ifdef HAVE_SYS_TYPES_H
00033 #include <sys/types.h>
00034 #endif
00035 
00036 #ifdef HAVE_SYS_STAT_H
00037 #include <sys/stat.h>
00038 #endif
00039 
00040 #ifdef HAVE_FCNTL_H
00041 #include <fcntl.h>
00042 #endif
00043 
00044 #ifdef HAVE_STRING_H
00045 #include <string.h>
00046 #endif
00047 
00048 #include "monitor.h"
00049 
00050 
00063 /* ------------------------------------------------------------------ Public */
00064 
00065 
00070 void  daemonize() {
00071 
00072   int i;
00073   pid_t pid;
00074   
00075   
00076   /*
00077    * Clear file creation mask
00078    */
00079   umask(0);
00080 
00081   /*
00082    * Become a session leader to lose our controlling terminal
00083    */
00084   if((pid= fork ()) < 0) {
00085     
00086     log("Cannot fork of a new process\n");  
00087     exit (1);
00088     
00089   }  
00090   else if(pid != 0) {
00091     
00092     _exit(0);
00093     
00094   }
00095   
00096   setsid();
00097 
00098   /*
00099    * Don't let future opens allocate controlling terminals
00100    */
00101   signal(SIGHUP, SIG_IGN);
00102 
00103   if((pid= fork ()) < 0) {
00104     
00105     log("Cannot fork of a new process\n");  
00106     exit (1);
00107     
00108   }  
00109   else if(pid != 0) {
00110     
00111     _exit(0);
00112     
00113   }
00114 
00115   
00116   /*
00117    * Change current directory to the root so that other file systems
00118    * can be unmounted while we're running
00119    */
00120   if(chdir("/") < 0) {
00121     
00122     log("Cannot chdir to / -- %s\n", STRERROR);
00123     exit(1);
00124     
00125   }
00126 
00127   /*
00128    * Attach standard descriptors to /dev/null. Other descriptors
00129    * should be closed in env.c
00130    */
00131   for(i= 0; i < 3; i++) {
00132     
00133     if(close(i) == -1 || open("/dev/null", O_RDWR) != i) {
00134       
00135       log("Cannot reopen standard file descriptor (%d) -- %s\n", i, STRERROR);
00136 
00137     }
00138      
00139   }
00140 
00141 } 
00142 
00143 
00148 int kill_daemon() {
00149   
00150   pid_t pid;
00151 
00152   if ( (pid= exist_daemon()) > 0 ) {
00153     
00154     if ( kill(pid, SIGTERM) < 0 ) {
00155       
00156       error("%s: Cannot kill daemon process -- %s\n",prog, STRERROR);
00157       return FALSE;
00158       
00159     }
00160     
00161   } else {
00162     
00163     error("%s: No daemon process found\n", prog);
00164     return TRUE;
00165     
00166   }
00167   
00168   fprintf(stdout, "%s daemon with pid [%d] killed\n", prog, (int)pid);
00169   fflush(stdout);
00170   
00171   return TRUE;
00172   
00173 }
00174 
00175 
00180 int exist_daemon() {
00181 
00182   pid_t pid;
00183   struct myprocess d;
00184   
00185   d.pidfile= Run.pidfile;
00186   pid= is_process_running(&d);
00187   
00188   return ( (int)pid ) ;
00189 
00190 }
00191