diff --git a/CMakeLists.txt b/CMakeLists.txt index acfd718..f395c99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.6...4.1.1) project(simple-http-server C) SET(CMAKE_BUILD_TYPE "Release") diff --git a/server.c b/server.c index e6c96e4..0e5a65a 100644 --- a/server.c +++ b/server.c @@ -98,7 +98,8 @@ static void wait_pid_push(void*); #define getmethod(method_type) ((method_type == GET)?"GET":"POST") static void accept_action(tcpool_thread_timer_t *p) { int client = p->accept_fd; - char *path, *query_string; + char *path; + char *query_string = ""; int numchars, cgi = 0, j; // cgi becomes true if server decides this is a CGI program struct stat st; method_type_enum_t method_type; @@ -166,7 +167,7 @@ static void accept_action(tcpool_thread_timer_t *p) { break; } } - int content_length = 0; + int content_length = -1; int host_chk_passed = !(uintptr_t)hostnameport; cgi |= ((st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH)); if(cgi) printf("(CGI) "); @@ -176,7 +177,7 @@ static void accept_action(tcpool_thread_timer_t *p) { numchars = 0; break; } - if(!content_length && !strncasecmp(p->data, "Content-Length: ", 16)) { + if(!~content_length && !strncasecmp(p->data, "Content-Length: ", 16)) { content_length = atoi(p->data + 16); } else if(!host_chk_passed && !strncasecmp(p->data, "Host: ", 6)) { @@ -279,9 +280,9 @@ static void execute_cgi(tcpool_thread_timer_t *timer, int content_length, const execl(request->path, request->path, request->method, request->query_string, NULL); exit(EXIT_FAILURE); // a success execl will never return } - pthread_cleanup_push((void (*)(void*))&close, cgi_output[0]); - pthread_cleanup_push((void (*)(void*))&close, cgi_input[1]); - pthread_cleanup_push((void (*)(void*))&wait_pid_push, pid); + pthread_cleanup_push((void (*)(void*))&close, (void*)(uintptr_t)cgi_output[0]); + pthread_cleanup_push((void (*)(void*))&close, (void*)(uintptr_t)cgi_input[1]); + pthread_cleanup_push((void (*)(void*))&wait_pid_push, (void*)(uintptr_t)pid); /* parent */ is_in_cgi[timer->index] = 1; close(cgi_output[1]); @@ -510,8 +511,40 @@ static void unimplemented(int client) { puts("501 Method Not Implemented."); } +#if __STDC_VERSION__ >= 201112L + static _Thread_local pid_t timeout_pid = 0; +#else + static __thread pid_t timeout_pid = 0; +#endif + +static void alarm_handler(int sig) { + pid_t p = timeout_pid; + timeout_pid = 0; + if(p > 0) { + printf("Wait pid %d > 5s, kill it.\n", p); + kill(p, SIGKILL); + timeout_pid = 0; + } +} + static void wait_pid_push(void* pid) { - waitpid((pid_t)((uintptr_t)pid), NULL, 0); + pid_t p = (pid_t)((uintptr_t)pid); + timeout_pid = p; + + struct sigaction sa, old_sa; + sa.sa_handler = alarm_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sigaction(SIGALRM, &sa, &old_sa); + + printf("Wait pid %d start.\n", p); + alarm(5); + waitpid(p, NULL, 0); + alarm(0); + printf("Wait pid %d finish.\n", p); + + timeout_pid = 0; + sigaction(SIGALRM, &old_sa, NULL); } #define argequ(arg) (*(uint16_t*)argv[i] == *(uint16_t*)(arg)) diff --git a/tcpool.h b/tcpool.h index cf188d0..eeaec98 100644 --- a/tcpool.h +++ b/tcpool.h @@ -259,8 +259,9 @@ static void accept_timer(void *p) { uint32_t index = timer->index; pthread_t thread = timer->thread; uint8_t isbusy; + const time_t check_interval = TCPOOL_MAXWAITSEC / 4; - sleep(TCPOOL_MAXWAITSEC / 4); + sleep(check_interval); while(thread && !pthread_kill(thread, 0)) { pthread_rwlock_rdlock(&timer->mb); isbusy = timer->isbusy; @@ -274,7 +275,7 @@ static void accept_timer(void *p) { timer->hastimerslept = 0; pthread_mutex_unlock(&timer->tmc); printf("Timer@%d wake up\n", timer->index); - sleep(TCPOOL_MAXWAITSEC / 4); + sleep(check_interval); thread = timer->thread; } if(TCPOOL_TOUCH_TIMER_CONDITION) tcpool_touch_timer(p); @@ -289,7 +290,7 @@ static void accept_timer(void *p) { } break; } - sleep(TCPOOL_MAXWAITSEC / 4); + sleep(check_interval); thread = timer->thread; } goto TIMER_SLEEP; @@ -353,8 +354,10 @@ static void handle_accept(void *p) { pthread_rwlock_unlock(&tcpool_timer_pointer_of(p)->mb); puts("Set thread status to idle"); - pthread_cond_wait(&tcpool_timer_pointer_of(p)->c, &tcpool_timer_pointer_of(p)->mc); - + while(tcpool_timer_pointer_of(p)->isbusy == 0) { // prevent fake wakeup + pthread_cond_wait(&tcpool_timer_pointer_of(p)->c, &tcpool_timer_pointer_of(p)->mc); + } + pthread_mutex_unlock(&tcpool_timer_pointer_of(p)->mc); puts("Thread wakeup"); } @@ -379,20 +382,28 @@ static void accept_client(int fd) { pthread_key_create(&__tcpool_pthread_key_index, NULL); while(1) { int p = 0; + tcpool_thread_timer_t* timer = NULL; + while(p < TCPOOL_THREADCNT) { - pthread_rwlock_rdlock(&tcpool_timers[p].mb); - if(!tcpool_timers[p].isbusy) break; - pthread_rwlock_unlock(&tcpool_timers[p].mb); + timer = &tcpool_timers[p]; + pthread_rwlock_wrlock(&timer->mb); + if(!timer->isbusy) { + timer->isbusy = 1; + pthread_rwlock_unlock(&timer->mb); + break; + } + pthread_rwlock_unlock(&timer->mb); + timer = NULL; p++; } - if(p >= TCPOOL_THREADCNT) { + + if(!timer) { puts("Max thread cnt exceeded"); sleep(1); continue; } + printf("Ready for accept on slot No.%d\n", p); - tcpool_thread_timer_t* timer = &tcpool_timers[p]; - pthread_rwlock_unlock(&timer->mb); #ifdef LISTEN_ON_IPV6 struct sockaddr_in6 client_addr; #else @@ -401,6 +412,9 @@ static void accept_client(int fd) { int accept_fd; if((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &tcpool_struct_len))<=0) { perror("accept"); + pthread_rwlock_wrlock(&timer->mb); + timer->isbusy = 0; + pthread_rwlock_unlock(&timer->mb); continue; } pthread_rwlock_wrlock(&timer->mb);