28 #include <libdaemon/daemon.h>
35 #include <sys/types.h>
80 _shutdown_cond.
signal(
true);
116 sigemptyset(&blockset);
117 sigaddset(&blockset, SIGPIPE);
118 ::sigprocmask(SIG_BLOCK, &blockset, NULL);
174 #ifdef HAVE_LIBDAEMON
178 daemon_retval_send(0);
179 daemon_log(LOG_INFO,
"Sucessfully started");
194 static char* __daemon_pidfile__ = NULL;
196 static const char* __daemon_pid_file_proc__(
void) {
197 return __daemon_pidfile__;
200 int main(
int argc,
char *argv[])
208 #ifdef HAVE_LIBDAEMON
214 #ifdef HAVE_DAEMON_RESET_SIGS
216 if (daemon_reset_sigs(-1) < 0) {
217 daemon_log(LOG_ERR,
"Failed to reset all signal handlers: %s", strerror(errno));
222 if (daemon_unblock_sigs(-1) < 0) {
223 daemon_log(LOG_ERR,
"Failed to unblock all signals: %s", strerror(errno));
229 daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);
234 __daemon_pidfile__ =
new char[p.length() + 1];
235 ::strcpy(__daemon_pidfile__, p.c_str());
236 daemon_pid_file_proc = __daemon_pid_file_proc__;
247 if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0)
248 daemon_log(LOG_WARNING,
"Failed to kill daemon: %s", strerror(errno));
250 return ret < 0 ? 1 : 0;
254 if ((pid = daemon_pid_file_is_running()) >= 0) {
255 daemon_log(LOG_ERR,
"Daemon already running on PID file %u", pid);
260 if (daemon_retval_init() < 0) {
261 daemon_log(LOG_ERR,
"Failed to create pipe.");
266 if ((pid = daemon_fork()) < 0) {
269 daemon_retval_done();
276 if ((ret = daemon_retval_wait(20)) < 0) {
277 daemon_log(LOG_ERR,
"Could not recieve return value from daemon process: %s", strerror(errno));
286 if (daemon_close_all(-1) < 0) {
287 daemon_log(LOG_ERR,
"Failed to close all file descriptors: %s", strerror(errno));
290 daemon_retval_send(1);
295 if (daemon_pid_file_create() < 0) {
296 daemon_log(LOG_ERR,
"Could not create PID file (%s).", strerror(errno));
297 daemon_retval_send(2);
302 if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGUSR1, SIGUSR2, 0) < 0) {
303 daemon_log(LOG_ERR,
"Could not register signal handlers (%s).", strerror(errno));
304 daemon_retval_send(3);
312 daemon_log(LOG_INFO,
"Exiting...");
313 daemon_retval_send(255);
314 daemon_signal_done();
315 daemon_pid_file_remove();
323 #ifdef HAVE_LIBDAEMON