diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index dfd9935..36ce186 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -49,6 +49,7 @@ static const int debug = 1; #include #include #include +#include #include #include #include @@ -247,10 +248,14 @@ static int sockin = -1; /* socket to accept connections from */ static char *wwwroot = NULL; /* a path name */ static char *logfile_name = NULL; /* NULL = no logging */ static FILE *logfile = NULL; +static char *pidfile_name = NULL; /* NULL = no pidfile */ +static struct pidfh *pidfile = NULL; static int want_chroot = 0, want_accf = 0; static uint32_t num_requests = 0; static uint64_t total_in = 0, total_out = 0; +static int running = 1; /* signal handler sets this to false */ + #define INVALID_UID ((uid_t) -1) #define INVALID_GID ((gid_t) -1) @@ -923,13 +928,17 @@ static void usage(void) "\t\tParses specified file for extension-MIME associations.\n" "\n"); printf( - "\t--uid uid, --gid gid\n" + "\t--uid uid/uname, --gid gid/gname (default: don't privdrop)\n" "\t\tDrops privileges to given uid:gid after initialization.\n" "\n"); + printf( + "\t--pidfile filename (default: no pidfile)\n" + "\t\tWrite PID to the specified file.\n" + "\n"); #ifdef __FreeBSD__ printf( - "\t--accf\n" - "\t\tUse acceptfilter.\n" + "\t--accf (default: don't use acceptfilter)\n" + "\t\tUse acceptfilter. Needs the accf_http module loaded.\n" "\n"); #endif } @@ -1043,6 +1052,12 @@ static void parse_commandline(const int argc, char *argv[]) if (g == NULL) errx(1, "no such gid: `%s'", argv[i]); drop_gid = g->gr_gid; } + else if (strcmp(argv[i], "--pidfile") == 0) + { + if (++i >= argc) + errx(1, "missing filename after --pidfile"); + pidfile_name = argv[i]; + } else if (strcmp(argv[i], "--accf") == 0) { want_accf = 1; @@ -2233,7 +2248,12 @@ static void httpd_poll(void) else return; } - if (select_ret == -1) err(1, "select()"); + if (select_ret == -1) { + if (errno == EINTR) + return; /* interrupted by signal */ + else + err(1, "select() failed"); + } /* poll connections that select() says need attention */ if (FD_ISSET(sockin, &recv_set)) accept_connection(); @@ -2262,44 +2282,11 @@ static void httpd_poll(void) /* --------------------------------------------------------------------------- * Close all sockets and FILEs and exit. */ -static void exit_quickly(int sig) +static void +stop_running(int sig) { - struct connection *conn, *next; - struct rusage r; - size_t i; - - printf("\ncaught %s, cleaning up...", strsignal(sig)); fflush(stdout); - /* close and free connections */ - LIST_FOREACH_SAFE(conn, &connlist, entries, next) - { - LIST_REMOVE(conn, entries); - free_connection(conn); - free(conn); - } - xclose(sockin); - if (logfile != NULL) fclose(logfile); - - /* free mime_map */ - for (i=0; i