From 2dcbad79f0c1324c7c2ef68afab22884c083d97b Mon Sep 17 00:00:00 2001 From: snowdrop Date: Fri, 26 Mar 2004 13:37:57 +0000 Subject: patch f creates detached threads (depending on problem under Redhat 9), thanks Menzo Windhouwer gracefully terminate HTTP server, added SIGTERM signal handler, thanks again to Menzo Windhouwer --- nanohttp/nanohttp-server.c | 53 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'nanohttp/nanohttp-server.c') diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index d3a96e3..bbd741f 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.c,v 1.4 2004/02/03 08:59:23 snowdrop Exp $ + * $Id: nanohttp-server.c,v 1.5 2004/03/26 13:37:57 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -27,10 +27,22 @@ #include #endif +#include #include #include #include #include +#include + +/* According to POSIX 1003.1-2001 */ +#include + +/* According to earlier standards */ +#include +#include +#include + +#include typedef struct tag_conndata @@ -45,6 +57,7 @@ static int _httpd_port = 10000; static hsocket_t _httpd_socket; static hservice_t *_httpd_services_head = NULL; static hservice_t *_httpd_services_tail = NULL; +static int _httpd_run = 1; /* ----------------------------------------------------- @@ -310,6 +323,15 @@ static void* httpd_session_main(void *data) } +/* ----------------------------------------------------- + FUNCTION: httpd_term +----------------------------------------------------- */ +void httpd_term(int sig) +{ + if (sig == SIGTERM) + _httpd_run = 0; +} + /* ----------------------------------------------------- FUNCTION: httpd_run ----------------------------------------------------- */ @@ -318,16 +340,22 @@ int httpd_run() { conndata_t *conn; pthread_t tid; + pthread_attr_t attr; hsocket_t sockfd; int err; fd_set fds; - /*struct timeval timeout;*/ + struct timeval timeout; + + pthread_attr_init(&attr); +#ifdef PTHREAD_CREATE_DETACHED + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); +#endif + log_verbose1("starting run routine"); - /* timeout.tv_sec = 1; timeout.tv_usec = 0; - */ + /* listen to port */ err = hsocket_listen(_httpd_socket,15); if (err != HSOCKET_OK) { @@ -335,23 +363,26 @@ int httpd_run() return err; } + log_verbose1("registering sigterm handler"); + signal(SIGTERM,httpd_term); log_verbose2("listening to port '%d'", _httpd_port); - /* fcntl(_httpd_socket, F_SETFL, O_NONBLOCK);*/ + fcntl(_httpd_socket, F_SETFL, O_NONBLOCK); - while (1) { + while (_httpd_run) { - /*FD_ZERO(&fds); + FD_ZERO(&fds); FD_SET(_httpd_socket, &fds); select(1, &fds, NULL, NULL, &timeout); - while ( (FD_ISSET(_httpd_socket, &fds))) + while (_httpd_run && (FD_ISSET(_httpd_socket, &fds))) select(1, &fds, NULL, NULL, &timeout); - */ - + + if (!_httpd_run) + break; if (hsocket_accept(_httpd_socket, &sockfd) != HSOCKET_OK) { continue; @@ -361,7 +392,7 @@ int httpd_run() conn = (conndata_t*)malloc(sizeof(conndata_t)); conn->sock = sockfd; - err = pthread_create(&tid, NULL, httpd_session_main, conn); + err = pthread_create(&tid, &attr, httpd_session_main, conn); if (err) { log_error2("Error creating thread: ('%d')", err); } -- cgit v1.1-32-gdbae