From 1c81c87fbb47b9692ecb8cae183c33239da45299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Str=C3=B6m?= Date: Fri, 15 Aug 2014 09:05:33 +0200 Subject: Clean up unix socket on exit so we can start properly. If unix socket is not cleaned up, we will fail to bind on it the next startup round (Address already in use). --- fcgiwrap.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'fcgiwrap.c') diff --git a/fcgiwrap.c b/fcgiwrap.c index d4f7901..9f12396 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -605,14 +605,29 @@ err_pipein: FCGI_puts("System error"); } +static volatile sig_atomic_t sigint_received ; +static void sigint_handler(int __attribute__((__unused__))dummy) +{ + sigint_received = 1; + FCGX_ShutdownPending(); // Or we could send SIGUSR1 +} + static void fcgiwrap_main(void) { + struct sigaction a; signal(SIGCHLD, SIG_IGN); signal(SIGPIPE, SIG_IGN); + // Use sigaction for SIGINT so we can avoid SA_RESTART and actually react + a.sa_handler = sigint_handler; + a.sa_flags = 0; + sigemptyset( &a.sa_mask ); + sigaction( SIGINT, &a, NULL ); + sigaction( SIGQUIT, &a, NULL ); + inherited_environ = environ; - while (FCGI_Accept() >= 0) { + while (FCGI_Accept() >= 0 && !sigint_received) { handle_fcgi_request(); } } @@ -689,7 +704,7 @@ static int listen_on_fd(int fd) { return 0; } -static int setup_socket(char *url) { +static int setup_socket(char *url, int *fd_out) { char *p = url; char *q; int fd; @@ -769,6 +784,7 @@ invalid_url: return -1; } + *fd_out = fd; return listen_on_fd(fd); } @@ -776,6 +792,7 @@ int main(int argc, char **argv) { int nchildren = 1; char *socket_url = NULL; + int fd = 0; int c; while ((c = getopt(argc, argv, "c:hfs:p:")) != -1) { @@ -833,7 +850,7 @@ int main(int argc, char **argv) } else #endif if (socket_url) { - if (setup_socket(socket_url) < 0) { + if (setup_socket(socket_url, &fd) < 0) { return 1; } free(socket_url); @@ -841,5 +858,15 @@ int main(int argc, char **argv) prefork(nchildren); fcgiwrap_main(); + + if(fd) { + const char *p = socket_url; + close(fd); + + if (!strncmp(p, "unix:", sizeof("unix:") - 1)) { + p += sizeof("unix:") - 1; + unlink(p); + } + } return 0; } -- cgit v1.2.3 From 99aedae9f611beffd11a448033bd8f72c069a3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Str=C3=B6m?= Date: Fri, 15 Aug 2014 09:31:16 +0200 Subject: SIGTERM, not SIGQUIT.. --- fcgiwrap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'fcgiwrap.c') diff --git a/fcgiwrap.c b/fcgiwrap.c index 9f12396..a2c88af 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -623,7 +623,7 @@ static void fcgiwrap_main(void) a.sa_flags = 0; sigemptyset( &a.sa_mask ); sigaction( SIGINT, &a, NULL ); - sigaction( SIGQUIT, &a, NULL ); + sigaction( SIGTERM, &a, NULL ); inherited_environ = environ; -- cgit v1.2.3 From 5ad97b1c7eb853a921e0167e6bfcb6b1815b4fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Str=C3=B6m?= Date: Fri, 15 Aug 2014 09:41:37 +0200 Subject: Use-after-free fix --- fcgiwrap.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'fcgiwrap.c') diff --git a/fcgiwrap.c b/fcgiwrap.c index a2c88af..3c36cf7 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -853,7 +853,6 @@ int main(int argc, char **argv) if (setup_socket(socket_url, &fd) < 0) { return 1; } - free(socket_url); } prefork(nchildren); @@ -863,9 +862,12 @@ int main(int argc, char **argv) const char *p = socket_url; close(fd); - if (!strncmp(p, "unix:", sizeof("unix:") - 1)) { - p += sizeof("unix:") - 1; - unlink(p); + if(socket_url) { + if (!strncmp(p, "unix:", sizeof("unix:") - 1)) { + p += sizeof("unix:") - 1; + unlink(p); + } + free(socket_url); } } return 0; -- cgit v1.2.3