diff --git a/configure.ac b/configure.ac index 1b99ab6f..aad46874 100644 --- a/configure.ac +++ b/configure.ac @@ -105,6 +105,9 @@ AC_ARG_WITH(daemonize, [ no_daemonize=yes ]) +if test "x$no_daemonize" = "xyes" ; then +CFLAGS="$CFLAGS -DLWS_NO_DAEMONIZE" +fi AM_CONDITIONAL(NO_DAEMONIZE, test x$no_daemonize = xyes) # diff --git a/lib/daemonize.c b/lib/daemonize.c index 5216121b..4add03dd 100644 --- a/lib/daemonize.c +++ b/lib/daemonize.c @@ -44,7 +44,7 @@ child_handler(int signum) fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640); if (fd < 0) { fprintf(stderr, "unable to create lock" - " file %s, code=%d (%s)", + " file %s, code=%d (%s)\n", lock_path, errno, strerror(errno)); exit(1); } @@ -52,7 +52,7 @@ child_handler(int signum) sent = write(fd, sz, len); if (sent != len) fprintf(stderr, "unable write pid to lock" - " file %s, code=%d (%s)", + " file %s, code=%d (%s)\n", lock_path, errno, strerror(errno)); close(fd); diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 58256773..1b62f927 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -1923,7 +1923,7 @@ libwebsockets_fork_service_loop(struct libwebsocket_context *context) if (n < 0) return n; - if (!n) { + if (n) { /* main process context */ @@ -1959,6 +1959,7 @@ libwebsockets_fork_service_loop(struct libwebsocket_context *context) #ifdef HAVE_SYS_PRCTL_H /* we want a SIGHUP when our parent goes down */ + signal(SIGHUP, SIG_DFL); prctl(PR_SET_PDEATHSIG, SIGHUP); #endif diff --git a/test-server/test-server.c b/test-server/test-server.c index 87cc351e..7ac0a9b1 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -43,6 +43,8 @@ int count_pollfds; #endif /* EXTERNAL_POLL */ +//#define LWS_NO_FORK + /* * This demo server shows how to use libwebsockets for one or more * websocket protocols in the same server @@ -488,6 +490,9 @@ static struct option options[] = { { "killmask", no_argument, NULL, 'k' }, { "interface", required_argument, NULL, 'i' }, { "closetest", no_argument, NULL, 'c' }, +#ifndef NO_DAEMONIZE + { "daemonize", no_argument, NULL, 'D' }, +#endif { NULL, 0, 0, 0 } }; @@ -506,16 +511,26 @@ int main(int argc, char **argv) int opts = 0; char interface_name[128] = ""; const char *interface = NULL; + int syslog_options = LOG_PID | LOG_PERROR; #ifdef LWS_NO_FORK unsigned int oldus = 0; #endif int debug_level = 7; +#ifndef NO_DAEMONIZE + int daemonize = 0; +#endif while (n >= 0) { - n = getopt_long(argc, argv, "ci:khsp:d:", options, NULL); + n = getopt_long(argc, argv, "ci:khsp:d:D", options, NULL); if (n < 0) continue; switch (n) { +#ifndef NO_DAEMONIZE + case 'D': + daemonize = 1; + syslog_options &= ~LOG_PERROR; + break; +#endif case 'd': debug_level = atoi(optarg); break; @@ -547,16 +562,26 @@ int main(int argc, char **argv) } } - setlogmask(LOG_UPTO (LOG_NOTICE)); - openlog("lwsts", LOG_PID | LOG_PERROR, LOG_DAEMON); + /* + * normally lock path would be /var/lock/lwsts or similar, to + * simplify getting started without having to take care about + * permissions or running as root, set to /tmp/.lwsts-lock + */ + if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { + fprintf(stderr, "Failed to daemonize\n"); + return 1; + } + + /* we will only try to log things according to our debug_level */ + setlogmask(LOG_UPTO (LOG_DEBUG)); + openlog("lwsts", syslog_options, LOG_DAEMON); /* tell the library what debug level to emit and to send it to syslog */ lws_set_log_level(debug_level, lwsl_emit_syslog); - lwsl_notice("libwebsockets test server\n" - "(C) Copyright 2010-2013 Andy Green " + lwsl_notice("libwebsockets test server - " + "(C) Copyright 2010-2013 Andy Green - " "licensed under LGPL2.1\n"); - if (!use_ssl) cert_path = key_path = NULL;