70 |
void daemonize() { |
void daemonize() { |
71 |
|
|
72 |
pid_t pid; |
pid_t pid; |
|
void *hup_handler= NULL; |
|
73 |
|
|
74 |
/* |
/* |
75 |
* Clear file creation mask |
* Clear file creation mask |
93 |
|
|
94 |
setsid(); |
setsid(); |
95 |
|
|
|
/* |
|
|
* Don't let future opens allocate controlling terminals |
|
|
*/ |
|
|
hup_handler= signal(SIGHUP, SIG_IGN); |
|
|
|
|
96 |
if((pid= fork ()) < 0) { |
if((pid= fork ()) < 0) { |
97 |
|
|
98 |
log("Cannot fork of a new process\n"); |
log("Cannot fork of a new process\n"); |
123 |
*/ |
*/ |
124 |
redirect_stdfd(); |
redirect_stdfd(); |
125 |
|
|
|
/* |
|
|
* Reset the SIGHUP handler |
|
|
*/ |
|
|
if(hup_handler) |
|
|
signal(SIGHUP, hup_handler); |
|
|
|
|
126 |
} |
} |
127 |
|
|
128 |
|
|
129 |
/** |
/** |
130 |
* Kill a daemon process |
* Send signal to a daemon process |
131 |
* @return TRUE if the daemon was killed, otherwise FALSE |
* @param sig Signal to send daemon to |
132 |
|
* @return TRUE if signal was send, otherwise FALSE |
133 |
*/ |
*/ |
134 |
int kill_daemon() { |
int kill_daemon(int sig) { |
135 |
|
|
136 |
pid_t pid; |
pid_t pid; |
137 |
|
|
138 |
if ( (pid= exist_daemon()) > 0 ) { |
if ( (pid= exist_daemon()) > 0 ) { |
139 |
|
|
140 |
if ( kill(pid, SIGTERM) < 0 ) { |
if ( kill(pid, sig) < 0 ) { |
141 |
|
|
142 |
error("%s: Cannot kill daemon process -- %s\n",prog, STRERROR); |
error("%s: Cannot send signal to daemon process -- %s\n",prog, STRERROR); |
143 |
return FALSE; |
return FALSE; |
144 |
|
|
145 |
} |
} |
151 |
|
|
152 |
} |
} |
153 |
|
|
154 |
fprintf(stdout, "%s daemon with pid [%d] killed\n", prog, (int)pid); |
if(sig == SIGTERM) { |
155 |
fflush(stdout); |
|
156 |
|
fprintf(stdout, "%s daemon with pid [%d] killed\n", prog, (int)pid); |
157 |
|
fflush(stdout); |
158 |
|
|
159 |
|
} |
160 |
|
|
161 |
return TRUE; |
return TRUE; |
162 |
|
|