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