From 0c93fa9ef0e014ff3504f098ed5d0cf6c23641d2 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sun, 19 Aug 2012 15:10:38 -0400 Subject: split listen() logic into separate function --- fcgiwrap.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'fcgiwrap.c') diff --git a/fcgiwrap.c b/fcgiwrap.c index f0bf6d5..9d87034 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -638,13 +638,35 @@ static void prefork(int nchildren) } } +static int listen_on_fd(int fd) { + int one = 1; + + if (listen(fd, 511) < 0) { + perror("Failed to listen"); + return -1; + } + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one) < 0) { + perror("Failed to enable SO_REUSEADDR"); + return -1; + } + if (dup2(fd, 0) < 0) { + perror("Failed to move socket to fd 0"); + return -1; + } + if (close(fd) < 0) { + perror("Failed to close original socket"); + return -1; + } + + return 0; +} + int setup_socket(char *url) { char *p = url; char *q; int fd; int port; size_t sockaddr_size; - int one = 1; union { struct sockaddr sa; @@ -718,24 +740,8 @@ invalid_url: perror("Failed to bind"); return -1; } - if (listen(fd, 511) < 0) { - perror("Failed to listen"); - return -1; - } - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one) < 0) { - perror("Failed to enable SO_REUSEADDR"); - return -1; - } - if (dup2(fd, 0) < 0) { - perror("Failed to move socket to fd 0"); - return -1; - } - if (close(fd) < 0) { - perror("Failed to close original socket"); - return -1; - } - return 0; + return listen_on_fd(fd); } int main(int argc, char **argv) -- cgit v1.2.3 From 9836d6d22ac22fe7be584da2006ae8d9479096cc Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sun, 19 Aug 2012 15:40:11 -0400 Subject: Add support for socket activation via systemd This prevents the need for starting fcgiwrap explicitly, or using a tool such as spawn-fcgi. The type of socket does not matter, we merely accept a single FD passed from pid 1 and listen on it. --- fcgiwrap.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'fcgiwrap.c') diff --git a/fcgiwrap.c b/fcgiwrap.c index 9d87034..2ff33f3 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -47,6 +47,10 @@ #include #include +#ifdef HAVE_SYSTEMD +#include +#endif + /* glibc doesn't seem to export it */ #ifndef UNIX_PATH_MAX #define UNIX_PATH_MAX 108 @@ -788,6 +792,14 @@ int main(int argc, char **argv) } } +#ifdef HAVE_SYSTEMD + if (sd_listen_fds(true) > 0) { + /* systemd woke us up. we should never see more than one FD passed to us. */ + if (listen_on_fd(SD_LISTEN_FDS_START) < 0) { + return 1; + } + } else +#endif if (socket_url) { if (setup_socket(socket_url) < 0) { return 1; -- cgit v1.2.3 From 3468d79391b13f4524cbdafd7085bca99a01d787 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sun, 19 Aug 2012 22:05:25 -0400 Subject: Cleanup -Wmissing-prototypes compiler warnings --- fcgiwrap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'fcgiwrap.c') diff --git a/fcgiwrap.c b/fcgiwrap.c index 2ff33f3..aeccf3b 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -348,7 +348,7 @@ static void fcgi_pass(struct fcgi_context *fc) fcgi_finish(fc, "reading CGI reply (no response received)"); } -int check_file_perms(const char *path) +static int check_file_perms(const char *path) { struct stat ls; struct stat fs; @@ -378,7 +378,7 @@ int check_file_perms(const char *path) } } -char *get_cgi_filename() /* and fixup environment */ +static char *get_cgi_filename(void) /* and fixup environment */ { int buflen = 1, docrootlen; char *buf = NULL; @@ -461,7 +461,7 @@ static int blacklisted_env(const char *var_name, const char *var_name_end) return 0; } -static void inherit_environment() +static void inherit_environment(void) { char * const * p; char *q; @@ -495,7 +495,7 @@ static void error_403(const char *reason, const char *filename) exit(99); } -static void handle_fcgi_request() +static void handle_fcgi_request(void) { int pipe_in[2]; int pipe_out[2]; @@ -665,7 +665,7 @@ static int listen_on_fd(int fd) { return 0; } -int setup_socket(char *url) { +static int setup_socket(char *url) { char *p = url; char *q; int fd; -- cgit v1.2.3