diff --git a/dict.c b/dict.c index 2a3aa84..b5db396 100644 --- a/dict.c +++ b/dict.c @@ -8,12 +8,11 @@ #include "server.h" static pthread_rwlock_t mu; -static int lock; static char* filepath; static uint8_t dict_md5[16]; static FILE* fp = NULL; //fp for EX -static FILE* fp5 = NULL; //fp for md5 +static FILE* fp_read = NULL; //fp for md5 static FILE* thread_fp[THREADCNT]; #ifdef CPUBIT64 @@ -32,8 +31,8 @@ int fill_md5() { uint8_t* dict_buff = (uint8_t*)malloc(size); if(dict_buff) { pthread_rwlock_rdlock(&mu); - rewind(fp5); - if(fread(dict_buff, size, 1, fp5) == 1) { + rewind(fp_read); + if(fread(dict_buff, size, 1, fp_read) == 1) { pthread_rwlock_unlock(&mu); md5(dict_buff, size, dict_md5); free(dict_buff); @@ -52,14 +51,13 @@ int fill_md5() { int init_dict(char* file_path) { fp = fopen(file_path, "rb+"); - fp5 = fopen(file_path, "rb"); + fp_read = fopen(file_path, "rb"); if(fp) { int err = pthread_rwlock_init(&mu, NULL); if(err) { puts("Init lock error"); return 0; } - lock = DICT_LOCK_UN; filepath = file_path; return fill_md5(); } else { @@ -71,7 +69,6 @@ int init_dict(char* file_path) { FILE* open_dict(uint8_t lock_type, uint32_t index) { if(lock_type & DICT_LOCK_EX) { pthread_rwlock_wrlock(&mu); - lock |= DICT_LOCK_EX; if(!fp) fp = fopen(filepath, "rb+"); else rewind(fp); return fp; @@ -81,25 +78,22 @@ FILE* open_dict(uint8_t lock_type, uint32_t index) { return NULL; } pthread_rwlock_rdlock(&mu); - lock |= DICT_LOCK_SH; if(!thread_fp[index]) thread_fp[index] = fopen(filepath, "rb"); else rewind(thread_fp[index]); return thread_fp[index]; } FILE* get_dict_fp_wr() { - if(lock & DICT_LOCK_EX) return fp; - return NULL; + return fp; } FILE* get_dict_fp_rd() { - rewind(fp5); - return fp5; + rewind(fp_read); + return fp_read; } void close_dict(uint8_t lock_type, uint32_t index) { if(lock_type & DICT_LOCK_EX) fflush(fp); - lock &= ~lock_type; pthread_rwlock_unlock(&mu); puts("Close dict"); } diff --git a/server.c b/server.c index 4e9cf62..294cbbf 100644 --- a/server.c +++ b/server.c @@ -108,7 +108,7 @@ static int listen_socket(int try_times) { puts("Listen failed!"); return 0; } else{ - puts("Listening...."); + puts("Listening..."); return 1; } } @@ -394,34 +394,30 @@ static void accept_timer(void *p) { printf("Wait sec: %u, max: %u\n", (unsigned int)waitsec, MAXWAITSEC); if(waitsec > MAXWAITSEC) break; } - puts("Call kill thread"); - kill_thread(timer); - puts("Free timer"); - free(timer); - puts("Finish calling kill thread\n"); -} - -static void kill_thread(THREADTIMER* timer) { - puts("Start killing."); - uint32_t index = timer->index; pthread_t thread = accept_threads[index]; if(thread) { pthread_kill(thread, SIGQUIT); accept_threads[index] = 0; - puts("Kill thread."); + puts("Kill thread"); } +} + +static void kill_thread(THREADTIMER* timer) { + puts("Start killing"); + accept_threads[timer->index] = 0; if(timer->accept_fd) { close(timer->accept_fd); timer->accept_fd = 0; - puts("Close accept."); + puts("Close accept"); } if(timer->ptr) { free(timer->ptr); timer->ptr = NULL; - puts("Free data."); + puts("Free data"); } if(timer->lock_type) close_dict(timer->lock_type, timer->index); - puts("Finish killing."); + free(timer); + puts("Finish killing\n"); } static void handle_pipe(int signo) { @@ -432,8 +428,11 @@ static void handle_pipe(int signo) { static void handle_accept(void *p) { int accept_fd = timer_pointer_of(p)->accept_fd; if(accept_fd > 0) { - puts("\nConnected to the client."); + puts("\nConnected to the client"); pthread_t thread; + pthread_key_t key; + pthread_key_create(&key, (void *)&kill_thread); + pthread_setspecific(key, p); if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread"); else puts("Creating timer thread succeeded"); //send_data(accept_fd, "Welcome to simple dict server.", 31); @@ -495,7 +494,7 @@ static void handle_accept(void *p) { default: goto CONV_END; break; } } else { - puts("Decrypt normal data failed."); + puts("Decrypt normal data failed"); break; } } else if(cp->cmd < 8) { @@ -521,11 +520,11 @@ static void handle_accept(void *p) { default: goto CONV_END; break; } } else { - puts("Decrypt super data failed."); + puts("Decrypt super data failed"); break; } } else { - puts("Invalid command."); + puts("Invalid command"); break; } if(offset > numbytes) { @@ -537,10 +536,9 @@ static void handle_accept(void *p) { printf("Offset after analyzing packet: %zd\n", offset); #endif } - CONV_END: puts("Conversation end\n"); + CONV_END: puts("Conversation end"); } else puts("Error allocating buffer"); - accept_threads[index] = 0; - kill_thread(timer_pointer_of(p)); + puts("Thread exited normally"); } else puts("Error accepting client"); } @@ -558,7 +556,7 @@ static void accept_client() { pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); init_crypto(); init_dict_pool(get_dict_fp_rd()); - if(pid < 0) puts("Error when forking a subprocess."); + if(pid < 0) puts("Error when forking a subprocess"); else while(1) { puts("Ready for accept, waitting..."); int p = 0; @@ -570,7 +568,7 @@ static void accept_client() { timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len); if(timer->accept_fd <= 0) { free(timer); - puts("Accept client error."); + puts("Accept client error"); } else { #ifdef LISTEN_ON_IPV6 uint16_t port = ntohs(client_addr.sin6_port); @@ -626,7 +624,7 @@ int main(int argc, char *argv[]) { if(argv[as_daemon?5:4][0] == '-') { // use env fp = (FILE*)1; cfg = (CONFIG*)malloc(sizeof(CONFIG)); - puts("Read config from env."); + puts("Read config from env"); char* pwd = getenv("SDS_PWD"); if(pwd) { char* sps = getenv("SDS_SPS"); @@ -636,8 +634,8 @@ int main(int argc, char *argv[]) { cfg->pwd[63] = 0; cfg->sps[63] = 0; fp = (FILE*)-1; - } else puts("Env SDS_SPS is null."); - } else puts("Env SDS_PWD is null."); + } else puts("Env SDS_SPS is null"); + } else puts("Env SDS_PWD is null"); } if(!fp) fp = fopen(argv[as_daemon?5:4], "rb"); if(fp && ((int)fp-1)) { @@ -649,7 +647,7 @@ int main(int argc, char *argv[]) { items_len = align_struct(sizeof(DICT), 2, d.key, d.data); if(items_len) { if(bind_server(port, times)) if(listen_socket(times)) accept_client(); - } else puts("Align struct error."); + } else puts("Align struct error"); } else printf("Error opening config file: %s\n", argv[as_daemon?5:4]); } } else printf("Error opening dict file: %s\n", argv[as_daemon?4:3]);