From 915316644cb47ec5a2d2ad3b5a233e5cd2b73db5 Mon Sep 17 00:00:00 2001 From: Artem Baguinski Date: Wed, 14 Dec 2011 22:14:03 +0100 Subject: [PATCH] check for prctl, poll parent PID if not present this allows forking code to be used on non-linux systems --- configure.ac | 2 +- lib/libwebsockets.c | 16 +++++++++++++++- lib/private-libwebsockets.h | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 93dc7f4..3980b2d 100644 --- a/configure.ac +++ b/configure.ac @@ -112,7 +112,7 @@ AM_CONDITIONAL(NOPING, test x$noping = xyes) # Checks for header files. -AC_CHECK_HEADERS([zlib.h fcntl.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h]) +AC_CHECK_HEADERS([zlib.h fcntl.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h sys/prctl.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 06f5c06..7680817 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -3021,14 +3021,28 @@ libwebsockets_fork_service_loop(struct libwebsocket_context *context) return 0; } +#ifdef HAVE_SYS_PRCTL_H /* we want a SIGHUP when our parent goes down */ prctl(PR_SET_PDEATHSIG, SIGHUP); +#endif /* in this forked process, sit and service websocket connections */ - while (1) + while (1) { if (libwebsocket_service(context, 1000)) return -1; +#ifndef HAVE_SYS_PRCTL_H +/* + * on systems without prctl() (i.e. anything but linux) we can notice that our + * parent is dead if getppid() returns 1. FIXME apparently this is not true for + * solaris, could remember ppid right after fork and wait for it to change. + */ + + if (getppid() == 1) + break; +#endif + } + return 0; } diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 2f682db..3c9776f 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -56,8 +56,10 @@ #include #include #ifndef LWS_NO_FORK +#ifdef HAVE_SYS_PRCTL_H #include #endif +#endif #include #include #include -- 2.7.4