mirror of
https://github.com/fumiama/simple-http-server.git
synced 2026-06-12 06:00:41 +08:00
fix: possible concurrent errors
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 2.6)
|
cmake_minimum_required(VERSION 2.6...4.1.1)
|
||||||
project(simple-http-server C)
|
project(simple-http-server C)
|
||||||
SET(CMAKE_BUILD_TYPE "Release")
|
SET(CMAKE_BUILD_TYPE "Release")
|
||||||
|
|
||||||
|
|||||||
47
server.c
47
server.c
@@ -98,7 +98,8 @@ static void wait_pid_push(void*);
|
|||||||
#define getmethod(method_type) ((method_type == GET)?"GET":"POST")
|
#define getmethod(method_type) ((method_type == GET)?"GET":"POST")
|
||||||
static void accept_action(tcpool_thread_timer_t *p) {
|
static void accept_action(tcpool_thread_timer_t *p) {
|
||||||
int client = p->accept_fd;
|
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
|
int numchars, cgi = 0, j; // cgi becomes true if server decides this is a CGI program
|
||||||
struct stat st;
|
struct stat st;
|
||||||
method_type_enum_t method_type;
|
method_type_enum_t method_type;
|
||||||
@@ -166,7 +167,7 @@ static void accept_action(tcpool_thread_timer_t *p) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int content_length = 0;
|
int content_length = -1;
|
||||||
int host_chk_passed = !(uintptr_t)hostnameport;
|
int host_chk_passed = !(uintptr_t)hostnameport;
|
||||||
cgi |= ((st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH));
|
cgi |= ((st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH));
|
||||||
if(cgi) printf("(CGI) ");
|
if(cgi) printf("(CGI) ");
|
||||||
@@ -176,7 +177,7 @@ static void accept_action(tcpool_thread_timer_t *p) {
|
|||||||
numchars = 0;
|
numchars = 0;
|
||||||
break;
|
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);
|
content_length = atoi(p->data + 16);
|
||||||
}
|
}
|
||||||
else if(!host_chk_passed && !strncasecmp(p->data, "Host: ", 6)) {
|
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);
|
execl(request->path, request->path, request->method, request->query_string, NULL);
|
||||||
exit(EXIT_FAILURE); // a success execl will never return
|
exit(EXIT_FAILURE); // a success execl will never return
|
||||||
}
|
}
|
||||||
pthread_cleanup_push((void (*)(void*))&close, cgi_output[0]);
|
pthread_cleanup_push((void (*)(void*))&close, (void*)(uintptr_t)cgi_output[0]);
|
||||||
pthread_cleanup_push((void (*)(void*))&close, cgi_input[1]);
|
pthread_cleanup_push((void (*)(void*))&close, (void*)(uintptr_t)cgi_input[1]);
|
||||||
pthread_cleanup_push((void (*)(void*))&wait_pid_push, pid);
|
pthread_cleanup_push((void (*)(void*))&wait_pid_push, (void*)(uintptr_t)pid);
|
||||||
/* parent */
|
/* parent */
|
||||||
is_in_cgi[timer->index] = 1;
|
is_in_cgi[timer->index] = 1;
|
||||||
close(cgi_output[1]);
|
close(cgi_output[1]);
|
||||||
@@ -510,8 +511,40 @@ static void unimplemented(int client) {
|
|||||||
puts("501 Method Not Implemented.");
|
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) {
|
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))
|
#define argequ(arg) (*(uint16_t*)argv[i] == *(uint16_t*)(arg))
|
||||||
|
|||||||
34
tcpool.h
34
tcpool.h
@@ -259,8 +259,9 @@ static void accept_timer(void *p) {
|
|||||||
uint32_t index = timer->index;
|
uint32_t index = timer->index;
|
||||||
pthread_t thread = timer->thread;
|
pthread_t thread = timer->thread;
|
||||||
uint8_t isbusy;
|
uint8_t isbusy;
|
||||||
|
const time_t check_interval = TCPOOL_MAXWAITSEC / 4;
|
||||||
|
|
||||||
sleep(TCPOOL_MAXWAITSEC / 4);
|
sleep(check_interval);
|
||||||
while(thread && !pthread_kill(thread, 0)) {
|
while(thread && !pthread_kill(thread, 0)) {
|
||||||
pthread_rwlock_rdlock(&timer->mb);
|
pthread_rwlock_rdlock(&timer->mb);
|
||||||
isbusy = timer->isbusy;
|
isbusy = timer->isbusy;
|
||||||
@@ -274,7 +275,7 @@ static void accept_timer(void *p) {
|
|||||||
timer->hastimerslept = 0;
|
timer->hastimerslept = 0;
|
||||||
pthread_mutex_unlock(&timer->tmc);
|
pthread_mutex_unlock(&timer->tmc);
|
||||||
printf("Timer@%d wake up\n", timer->index);
|
printf("Timer@%d wake up\n", timer->index);
|
||||||
sleep(TCPOOL_MAXWAITSEC / 4);
|
sleep(check_interval);
|
||||||
thread = timer->thread;
|
thread = timer->thread;
|
||||||
}
|
}
|
||||||
if(TCPOOL_TOUCH_TIMER_CONDITION) tcpool_touch_timer(p);
|
if(TCPOOL_TOUCH_TIMER_CONDITION) tcpool_touch_timer(p);
|
||||||
@@ -289,7 +290,7 @@ static void accept_timer(void *p) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sleep(TCPOOL_MAXWAITSEC / 4);
|
sleep(check_interval);
|
||||||
thread = timer->thread;
|
thread = timer->thread;
|
||||||
}
|
}
|
||||||
goto TIMER_SLEEP;
|
goto TIMER_SLEEP;
|
||||||
@@ -353,7 +354,9 @@ static void handle_accept(void *p) {
|
|||||||
pthread_rwlock_unlock(&tcpool_timer_pointer_of(p)->mb);
|
pthread_rwlock_unlock(&tcpool_timer_pointer_of(p)->mb);
|
||||||
|
|
||||||
puts("Set thread status to idle");
|
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);
|
pthread_mutex_unlock(&tcpool_timer_pointer_of(p)->mc);
|
||||||
puts("Thread wakeup");
|
puts("Thread wakeup");
|
||||||
@@ -379,20 +382,28 @@ static void accept_client(int fd) {
|
|||||||
pthread_key_create(&__tcpool_pthread_key_index, NULL);
|
pthread_key_create(&__tcpool_pthread_key_index, NULL);
|
||||||
while(1) {
|
while(1) {
|
||||||
int p = 0;
|
int p = 0;
|
||||||
|
tcpool_thread_timer_t* timer = NULL;
|
||||||
|
|
||||||
while(p < TCPOOL_THREADCNT) {
|
while(p < TCPOOL_THREADCNT) {
|
||||||
pthread_rwlock_rdlock(&tcpool_timers[p].mb);
|
timer = &tcpool_timers[p];
|
||||||
if(!tcpool_timers[p].isbusy) break;
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
pthread_rwlock_unlock(&tcpool_timers[p].mb);
|
if(!timer->isbusy) {
|
||||||
|
timer->isbusy = 1;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
|
timer = NULL;
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
if(p >= TCPOOL_THREADCNT) {
|
|
||||||
|
if(!timer) {
|
||||||
puts("Max thread cnt exceeded");
|
puts("Max thread cnt exceeded");
|
||||||
sleep(1);
|
sleep(1);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Ready for accept on slot No.%d\n", p);
|
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
|
#ifdef LISTEN_ON_IPV6
|
||||||
struct sockaddr_in6 client_addr;
|
struct sockaddr_in6 client_addr;
|
||||||
#else
|
#else
|
||||||
@@ -401,6 +412,9 @@ static void accept_client(int fd) {
|
|||||||
int accept_fd;
|
int accept_fd;
|
||||||
if((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &tcpool_struct_len))<=0) {
|
if((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &tcpool_struct_len))<=0) {
|
||||||
perror("accept");
|
perror("accept");
|
||||||
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
|
timer->isbusy = 0;
|
||||||
|
pthread_rwlock_unlock(&timer->mb);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pthread_rwlock_wrlock(&timer->mb);
|
pthread_rwlock_wrlock(&timer->mb);
|
||||||
|
|||||||
Reference in New Issue
Block a user