29 #include <libdaemon/daemon.h>
35 #include <sys/types.h>
86 _shutdown_cond.
signal(
true);
116 sighandler.
handle(SIGINT);
117 sighandler.
handle(SIGTERM);
119 sighandler.
handle(SIGHUP);
120 sighandler.
handle(SIGQUIT);
121 sighandler.
handle(SIGUSR1);
122 sighandler.
handle(SIGUSR2);
125 sigemptyset(&blockset);
126 sigaddset(&blockset, SIGPIPE);
127 ::sigprocmask(SIG_BLOCK, &blockset, NULL);
187 #ifdef HAVE_LIBDAEMON
191 daemon_retval_send(0);
192 daemon_log(LOG_INFO,
"Sucessfully started");
207 static char* __daemon_pidfile__ = NULL;
209 static const char* __daemon_pid_file_proc__(
void) {
210 return __daemon_pidfile__;
213 int main(
int argc,
char *argv[])
221 #ifdef HAVE_LIBDAEMON
227 #ifdef HAVE_DAEMON_RESET_SIGS
229 if (daemon_reset_sigs(-1) < 0) {
230 daemon_log(LOG_ERR,
"Failed to reset all signal handlers: %s", strerror(errno));
235 if (daemon_unblock_sigs(-1) < 0) {
236 daemon_log(LOG_ERR,
"Failed to unblock all signals: %s", strerror(errno));
242 daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
247 __daemon_pidfile__ =
new char[p.length() + 1];
248 ::strcpy(__daemon_pidfile__, p.c_str());
249 daemon_pid_file_proc = __daemon_pid_file_proc__;
260 if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0)
261 daemon_log(LOG_WARNING,
"Failed to kill daemon: %s", strerror(errno));
263 return ret < 0 ? 1 : 0;
267 if ((pid = daemon_pid_file_is_running()) >= 0) {
268 daemon_log(LOG_ERR,
"Daemon already running on PID file %u", pid);
273 if (daemon_retval_init() < 0) {
274 daemon_log(LOG_ERR,
"Failed to create pipe.");
279 if ((pid = daemon_fork()) < 0) {
282 daemon_retval_done();
289 if ((ret = daemon_retval_wait(20)) < 0) {
290 daemon_log(LOG_ERR,
"Could not recieve return value from daemon process: %s", strerror(errno));
299 if (daemon_close_all(-1) < 0) {
300 daemon_log(LOG_ERR,
"Failed to close all file descriptors: %s", strerror(errno));
303 daemon_retval_send(1);
308 if (daemon_pid_file_create() < 0) {
309 daemon_log(LOG_ERR,
"Could not create PID file (%s).", strerror(errno));
310 daemon_retval_send(2);
315 if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGUSR1, SIGUSR2, 0) < 0) {
316 daemon_log(LOG_ERR,
"Could not register signal handlers (%s).", strerror(errno));
317 daemon_retval_send(3);
325 daemon_log(LOG_INFO,
"Exiting...");
326 daemon_retval_send(255);
327 daemon_signal_done();
328 daemon_pid_file_remove();
336 #ifdef HAVE_LIBDAEMON