. Ignoring SIGPIPE

. httpd_poll()'s select() will timeout after IDLETIME seconds if
  there are any connections currently in a send/recv state
This commit is contained in:
Emil Mikulic 2003-03-01 10:10:27 +00:00
parent 4456c044e5
commit 09d69063bd

View File

@ -9,7 +9,7 @@
/* /*
* TODO: * TODO:
* . Ignore SIGPIPE. * x Ignore SIGPIPE.
* x Actually serve files. * x Actually serve files.
* . Generate directory entries. * . Generate directory entries.
* x Log to file. * x Log to file.
@ -30,6 +30,7 @@
#include <ctype.h> #include <ctype.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -766,6 +767,8 @@ 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; struct connection *conn;
struct timeval timeout = { IDLETIME, 0 };
int bother_with_timeout = 0;
FD_ZERO(&recv_set); FD_ZERO(&recv_set);
FD_ZERO(&send_set); FD_ZERO(&send_set);
@ -784,11 +787,13 @@ static void httpd_poll(void)
{ {
case RECV_REQUEST: case RECV_REQUEST:
MAX_FD_SET(conn->socket, &recv_set); MAX_FD_SET(conn->socket, &recv_set);
bother_with_timeout = 1;
break; break;
case SEND_HEADER: case SEND_HEADER:
case SEND_REPLY: case SEND_REPLY:
MAX_FD_SET(conn->socket, &send_set); MAX_FD_SET(conn->socket, &send_set);
bother_with_timeout = 1;
break; break;
case DONE: case DONE:
@ -805,8 +810,15 @@ static void httpd_poll(void)
#undef MAX_FD_SET #undef MAX_FD_SET
debugf("select("), fflush(stdout); debugf("select("), fflush(stdout);
select_ret = select(max_fd + 1, &recv_set, &send_set, NULL, NULL); select_ret = select(max_fd + 1, &recv_set, &send_set, NULL,
if (select_ret == 0) errx(1, "select() timed out"); (bother_with_timeout) ? &timeout : NULL);
if (select_ret == 0)
{
if (!bother_with_timeout)
errx(1, "select() timed out");
else
return;
}
if (select_ret == -1) err(1, "select()"); if (select_ret == -1) err(1, "select()");
debugf(")\n"); debugf(")\n");
@ -837,6 +849,16 @@ static void httpd_poll(void)
/* ---------------------------------------------------------------------------
* Ignore SIGPIPE
*/
static void ignore_signal(int signum)
{
if (signum == signum) { /* do nothing */ }
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
printf("%s, %s.\n", pkgname, copyright); printf("%s, %s.\n", pkgname, copyright);
@ -850,6 +872,8 @@ int main(int argc, char *argv[])
if (logfile == NULL) err(1, "fopen(\"%s\")", logfile_name); if (logfile == NULL) err(1, "fopen(\"%s\")", logfile_name);
} }
signal(SIGPIPE, ignore_signal);
for (;;) httpd_poll(); for (;;) httpd_poll();
(void) close(sockin); /* unreachable =/ fix later */ (void) close(sockin); /* unreachable =/ fix later */