Finished style changes.

This commit is contained in:
Emil Mikulic
2011-01-15 20:12:12 +11:00
parent 7343ecf35f
commit f32ffbabd5

View File

@ -2070,12 +2070,10 @@ static void poll_send_reply(struct connection *conn)
conn->state = DONE; conn->state = DONE;
} }
/* --------------------------------------------------------------------------- /* Main loop of the httpd - a select() and then delegation to accept
* Main loop of the httpd - a select() and then delegation to accept
* connections, handle receiving of requests, and sending of replies. * connections, handle receiving of requests, and sending of replies.
*/ */
static void httpd_poll(void) static void httpd_poll(void) {
{
fd_set recv_set, send_set; fd_set recv_set, send_set;
int max_fd, select_ret; int max_fd, select_ret;
struct connection *conn, *next; struct connection *conn, *next;
@ -2095,11 +2093,9 @@ static void httpd_poll(void)
MAX_FD_SET(sockin, &recv_set); MAX_FD_SET(sockin, &recv_set);
LIST_FOREACH_SAFE(conn, &connlist, entries, next) LIST_FOREACH_SAFE(conn, &connlist, entries, next) {
{
poll_check_timeout(conn); poll_check_timeout(conn);
switch (conn->state) switch (conn->state) {
{
case DONE: case DONE:
/* do nothing */ /* do nothing */
break; break;
@ -2123,8 +2119,7 @@ static void httpd_poll(void)
/* -select- */ /* -select- */
select_ret = select(max_fd + 1, &recv_set, &send_set, NULL, select_ret = select(max_fd + 1, &recv_set, &send_set, NULL,
(bother_with_timeout) ? &timeout : NULL); (bother_with_timeout) ? &timeout : NULL);
if (select_ret == 0) if (select_ret == 0) {
{
if (!bother_with_timeout) if (!bother_with_timeout)
errx(1, "select() timed out"); errx(1, "select() timed out");
else else
@ -2141,12 +2136,11 @@ static void httpd_poll(void)
now = time(NULL); now = time(NULL);
/* poll connections that select() says need attention */ /* poll connections that select() says need attention */
if (FD_ISSET(sockin, &recv_set)) accept_connection(); if (FD_ISSET(sockin, &recv_set))
accept_connection();
LIST_FOREACH_SAFE(conn, &connlist, entries, next) LIST_FOREACH_SAFE(conn, &connlist, entries, next) {
{ switch (conn->state) {
switch (conn->state)
{
case RECV_REQUEST: case RECV_REQUEST:
if (FD_ISSET(conn->socket, &recv_set)) poll_recv_request(conn); if (FD_ISSET(conn->socket, &recv_set)) poll_recv_request(conn);
break; break;
@ -2183,86 +2177,75 @@ static void httpd_poll(void)
} }
} }
/* Daemonize helpers. */
/* ---------------------------------------------------------------------------
* Daemonize helpers.
*/
#define PATH_DEVNULL "/dev/null" #define PATH_DEVNULL "/dev/null"
static int lifeline[2] = { -1, -1 }; static int lifeline[2] = { -1, -1 };
static int fd_null = -1; static int fd_null = -1;
static void static void daemonize_start(void) {
daemonize_start(void) pid_t f, w;
{
pid_t f, w;
if (pipe(lifeline) == -1) if (pipe(lifeline) == -1)
err(1, "pipe(lifeline)"); err(1, "pipe(lifeline)");
fd_null = open(PATH_DEVNULL, O_RDWR, 0); fd_null = open(PATH_DEVNULL, O_RDWR, 0);
if (fd_null == -1) if (fd_null == -1)
err(1, "open(" PATH_DEVNULL ")"); err(1, "open(" PATH_DEVNULL ")");
f = fork(); f = fork();
if (f == -1) if (f == -1)
err(1, "fork"); err(1, "fork");
else if (f != 0) { else if (f != 0) {
/* parent: wait for child */ /* parent: wait for child */
char tmp[1]; char tmp[1];
int status; int status;
if (close(lifeline[1]) == -1) if (close(lifeline[1]) == -1)
warn("close lifeline in parent"); warn("close lifeline in parent");
if (read(lifeline[0], tmp, sizeof(tmp)) == -1) if (read(lifeline[0], tmp, sizeof(tmp)) == -1)
warn("read lifeline in parent"); warn("read lifeline in parent");
w = waitpid(f, &status, WNOHANG); w = waitpid(f, &status, WNOHANG);
if (w == -1) if (w == -1)
err(1, "waitpid"); err(1, "waitpid");
else if (w == 0) else if (w == 0)
/* child is running happily */ /* child is running happily */
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
else else
/* child init failed, pass on its exit status */ /* child init failed, pass on its exit status */
exit(WEXITSTATUS(status)); exit(WEXITSTATUS(status));
} }
/* else we are the child: continue initializing */ /* else we are the child: continue initializing */
} }
static void static void daemonize_finish(void) {
daemonize_finish(void) if (fd_null == -1)
{ return; /* didn't daemonize_start() so we're not daemonizing */
if (fd_null == -1)
return; /* didn't daemonize_start() so we're not daemonizing */
if (setsid() == -1) if (setsid() == -1)
err(1, "setsid"); err(1, "setsid");
if (close(lifeline[0]) == -1) if (close(lifeline[0]) == -1)
warn("close read end of lifeline in child"); warn("close read end of lifeline in child");
if (close(lifeline[1]) == -1) if (close(lifeline[1]) == -1)
warn("couldn't cut the lifeline"); warn("couldn't cut the lifeline");
/* close all our std fds */ /* close all our std fds */
if (dup2(fd_null, STDIN_FILENO) == -1) if (dup2(fd_null, STDIN_FILENO) == -1)
warn("dup2(stdin)"); warn("dup2(stdin)");
if (dup2(fd_null, STDOUT_FILENO) == -1) if (dup2(fd_null, STDOUT_FILENO) == -1)
warn("dup2(stdout)"); warn("dup2(stdout)");
if (dup2(fd_null, STDERR_FILENO) == -1) if (dup2(fd_null, STDERR_FILENO) == -1)
warn("dup2(stderr)"); warn("dup2(stderr)");
if (fd_null > 2) if (fd_null > 2)
close(fd_null); close(fd_null);
} }
/* --------------------------------------------------------------------------- /* [<-] pidfile helpers, based on FreeBSD src/lib/libutil/pidfile.c,v 1.3
* Pidfile helpers, based on FreeBSD src/lib/libutil/pidfile.c,v 1.3
* Original was copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> * Original was copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
*/ */
static int pidfile_fd = -1; static int pidfile_fd = -1;
#define PIDFILE_MODE 0600 #define PIDFILE_MODE 0600
static void static void pidfile_remove(void) {
pidfile_remove(void)
{
if (unlink(pidfile_name) == -1) if (unlink(pidfile_name) == -1)
err(1, "unlink(pidfile) failed"); err(1, "unlink(pidfile) failed");
/* if (flock(pidfile_fd, LOCK_UN) == -1) /* if (flock(pidfile_fd, LOCK_UN) == -1)
@ -2271,9 +2254,7 @@ pidfile_remove(void)
pidfile_fd = -1; pidfile_fd = -1;
} }
static int static int pidfile_read(void) {
pidfile_read(void)
{
char buf[16], *endptr; char buf[16], *endptr;
int fd, i, pid; int fd, i, pid;
@ -2293,9 +2274,7 @@ pidfile_read(void)
return (pid); return (pid);
} }
static void static void pidfile_create(void) {
pidfile_create(void)
{
int error, fd; int error, fd;
char pidstr[16]; char pidstr[16];
@ -2325,24 +2304,16 @@ pidfile_create(void)
err(1, "pwrite() failed"); err(1, "pwrite() failed");
} }
} }
/* [<-] end of pidfile helpers. */
/* end of pidfile helpers. /* Close all sockets and FILEs and exit. */
* --------------------------------------------------------------------------- static void stop_running(int sig) {
* Close all sockets and FILEs and exit.
*/
static void
stop_running(int sig)
{
running = 0; running = 0;
fprintf(stderr, "\ncaught %s, stopping\n", strsignal(sig)); fprintf(stderr, "\ncaught %s, stopping\n", strsignal(sig));
} }
/* --------------------------------------------------------------------------- /* Execution starts here. */
* Execution starts here. int main(int argc, char **argv) {
*/
int
main(int argc, char **argv)
{
printf("%s, %s.\n", pkgname, copyright); printf("%s, %s.\n", pkgname, copyright);
parse_default_extension_map(); parse_default_extension_map();
parse_commandline(argc, argv); parse_commandline(argc, argv);
@ -2354,14 +2325,14 @@ main(int argc, char **argv)
init_sockin(); init_sockin();
/* open logfile */ /* open logfile */
if (logfile_name != NULL) if (logfile_name != NULL) {
{
logfile = fopen(logfile_name, "ab"); logfile = fopen(logfile_name, "ab");
if (logfile == NULL) if (logfile == NULL)
err(1, "opening logfile: fopen(\"%s\")", logfile_name); err(1, "opening logfile: fopen(\"%s\")", logfile_name);
} }
if (want_daemon) daemonize_start(); if (want_daemon)
daemonize_start();
/* signals */ /* signals */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
@ -2374,8 +2345,7 @@ main(int argc, char **argv)
err(1, "signal(SIGTERM)"); err(1, "signal(SIGTERM)");
/* security */ /* security */
if (want_chroot) if (want_chroot) {
{
tzset(); /* read /etc/localtime before we chroot */ tzset(); /* read /etc/localtime before we chroot */
if (chdir(wwwroot) == -1) if (chdir(wwwroot) == -1)
err(1, "chdir(%s)", wwwroot); err(1, "chdir(%s)", wwwroot);
@ -2384,14 +2354,14 @@ main(int argc, char **argv)
printf("chrooted to `%s'\n", wwwroot); printf("chrooted to `%s'\n", wwwroot);
wwwroot[0] = '\0'; /* empty string */ wwwroot[0] = '\0'; /* empty string */
} }
if (drop_gid != INVALID_GID) if (drop_gid != INVALID_GID) {
{ if (setgid(drop_gid) == -1)
if (setgid(drop_gid) == -1) err(1, "setgid(%d)", drop_gid); err(1, "setgid(%d)", drop_gid);
printf("set gid to %d\n", drop_gid); printf("set gid to %d\n", drop_gid);
} }
if (drop_uid != INVALID_UID) if (drop_uid != INVALID_UID) {
{ if (setuid(drop_uid) == -1)
if (setuid(drop_uid) == -1) err(1, "setuid(%d)", drop_uid); err(1, "setuid(%d)", drop_uid);
printf("set uid to %d\n", drop_uid); printf("set uid to %d\n", drop_uid);
} }
@ -2412,8 +2382,7 @@ main(int argc, char **argv)
{ {
struct connection *conn, *next; struct connection *conn, *next;
LIST_FOREACH_SAFE(conn, &connlist, entries, next) LIST_FOREACH_SAFE(conn, &connlist, entries, next) {
{
LIST_REMOVE(conn, entries); LIST_REMOVE(conn, entries);
free_connection(conn); free_connection(conn);
free(conn); free(conn);
@ -2423,8 +2392,8 @@ main(int argc, char **argv)
/* free the mallocs */ /* free the mallocs */
{ {
size_t i; size_t i;
for (i=0; i<mime_map_size; i++)
{ for (i=0; i<mime_map_size; i++) {
free(mime_map[i].extension); free(mime_map[i].extension);
free(mime_map[i].mimetype); free(mime_map[i].mimetype);
} }
@ -2448,7 +2417,7 @@ main(int argc, char **argv)
printf("Bytes: %llu in, %llu out\n", total_in, total_out); printf("Bytes: %llu in, %llu out\n", total_in, total_out);
} }
return (0); return 0;
} }
/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */ /* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */