1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-28 16:00:25 +08:00

文件打开线程安全

This commit is contained in:
fumiama
2021-06-01 18:15:02 +08:00
parent ea674bd311
commit c86dce46c0
4 changed files with 125 additions and 111 deletions

56
dict.c
View File

@@ -3,25 +3,28 @@
#include <stdlib.h> #include <stdlib.h>
#include <simplemd5.h> #include <simplemd5.h>
#include "dict.h" #include "dict.h"
#include "server.h"
static FILE *fp = NULL; static uint8_t lock = 0;
static int lock = 0;
static char* filepath; static char* filepath;
static uint8_t* dict_md5; static uint8_t* dict_md5;
static FILE *fp = NULL; //fp for EX
static FILE *fp5 = NULL; //fp for md5
static FILE* thread_fp[THREADCNT];
#ifdef CPUBIT64 #ifdef CPUBIT64
#define _dict_md5_2 ((uint64_t*)dict_md5) #define _dict_md5_2 ((uint64_t*)dict_md5)
#else #else
#define _dict_md5_4 ((uint32_t*)dict_md5) #define _dict_md5_4 ((uint32_t*)dict_md5)
#endif #endif
int fill_md5() {
static int fill_md5() {
size_t size = get_dict_size(); size_t size = get_dict_size();
uint8_t* dict_buff = (uint8_t*)malloc(size); uint8_t* dict_buff = (uint8_t*)malloc(size);
if(dict_buff) { if(dict_buff) {
rewind(fp); rewind(fp5);
if(fread(dict_buff, size, 1, fp) == 1) { if(fread(dict_buff, size, 1, fp5) == 1) {
if(dict_md5) free(dict_md5); if(dict_md5) free(dict_md5);
dict_md5 = md5(dict_buff, size); dict_md5 = md5(dict_buff, size);
free(dict_buff); free(dict_buff);
@@ -44,6 +47,7 @@ uint32_t last_nonnull(char* p, uint32_t max_size) {
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");
if(fp) { if(fp) {
lock = LOCK_UN; lock = LOCK_UN;
filepath = file_path; filepath = file_path;
@@ -54,25 +58,39 @@ int init_dict(char* file_path) {
} }
} }
FILE *open_dict(int lock_type) { #define _open_dict(p)\
if(p) {\
lock |= lock_type;\
rewind(p);\
return p;\
} else {\
puts("Open dict error");\
return NULL;\
}
FILE* open_dict(uint8_t lock_type, uint32_t index) {
if(lock & LOCK_EX) return NULL; if(lock & LOCK_EX) return NULL;
else { else if(lock_type & LOCK_EX) {
if(!fp) fp = fopen(filepath, "rb+"); if(!fp) fp = fopen(filepath, "rb+");
if(fp) { _open_dict(fp);
lock = lock_type; } else if(index < THREADCNT) {
fill_md5(); if(!thread_fp[index]) thread_fp[index] = fopen(filepath, "rb");
rewind(fp); _open_dict(thread_fp[index]);
return fp; } else {
} else { puts("Index out of bounds");
puts("Open dict error"); return NULL;
return NULL;
}
} }
} }
void close_dict() { FILE* get_dict_fp(uint32_t index) {
if(lock & LOCK_EX) return fp;
else if(lock & LOCK_SH && index < THREADCNT) return thread_fp[index];
else return NULL;
}
void close_dict(uint8_t lock_type) {
puts("Close dict"); puts("Close dict");
lock = LOCK_UN; lock &= ~lock_type;
} }
off_t get_dict_size() { off_t get_dict_size() {

6
dict.h
View File

@@ -15,10 +15,12 @@ typedef struct DICT DICT;
#define LOCK_SH 0x01 #define LOCK_SH 0x01
#define LOCK_EX 0x02 #define LOCK_EX 0x02
int fill_md5();
uint32_t last_nonnull(char* p, uint32_t max_size); uint32_t last_nonnull(char* p, uint32_t max_size);
int init_dict(char* file_path); int init_dict(char* file_path);
FILE *open_dict(int lock_type); FILE *open_dict(uint8_t lock_type, uint32_t index);
void close_dict(); void close_dict(uint8_t lock_type);
FILE* get_dict_fp(uint32_t index);
off_t get_dict_size(); off_t get_dict_size();
int is_md5_equal(uint8_t* digest); int is_md5_equal(uint8_t* digest);

132
server.c
View File

@@ -12,6 +12,7 @@
#include <signal.h> #include <signal.h>
#include <time.h> #include <time.h>
#include <simple_protobuf.h> #include <simple_protobuf.h>
#include "server.h"
#include "dict.h" #include "dict.h"
#include "config.h" #include "config.h"
@@ -19,8 +20,6 @@
#include <sys/sendfile.h> #include <sys/sendfile.h>
#endif #endif
static int fd;
#ifdef LISTEN_ON_IPV6 #ifdef LISTEN_ON_IPV6
static socklen_t struct_len = sizeof(struct sockaddr_in6); static socklen_t struct_len = sizeof(struct sockaddr_in6);
static struct sockaddr_in6 server_addr; static struct sockaddr_in6 server_addr;
@@ -29,52 +28,16 @@ static int fd;
static struct sockaddr_in server_addr; static struct sockaddr_in server_addr;
#endif #endif
#define THREADCNT 16 static int fd; //server fd
static pthread_t accept_threads[THREADCNT]; static pthread_t accept_threads[THREADCNT];
#define MAXWAITSEC 10
struct THREADTIMER {
pthread_t *thread;
time_t touch;
int accept_fd;
ssize_t numbytes;
char *data;
char status;
char is_open;
FILE *fp;
};
typedef struct THREADTIMER THREADTIMER;
static DICT d; static DICT d;
static uint32_t* items_len; static uint32_t* items_len;
static CONFIG* cfg; static CONFIG* cfg;
static char *setpass, *delpass; static char *setpass, *delpass;
#define showUsage(program) printf("Usage: %s [-d] listen_port try_times dict_file config_file\n\t-d: As daemon\n", program) #define showUsage(program) printf("Usage: %s [-d] listen_port try_times dict_file config_file\n\t-d: As daemon\n", program)
void accept_client(); int bind_server(uint16_t port, int try_times) {
void accept_timer(void *p);
int bind_server(uint16_t port, u_int try_times);
int check_buffer(THREADTIMER *timer);
int close_and_send(THREADTIMER *timer, char *data, size_t numbytes);
int free_after_send(int accept_fd, char *data, size_t length);
void handle_accept(void *accept_fd_p);
void handle_pipe(int signo);
void handle_quit(int signo);
void kill_thread(THREADTIMER* timer);
int listen_socket(u_int try_times);
int send_all(THREADTIMER *timer);
int send_data(int accept_fd, char *data, size_t length);
int sm1_pwd(THREADTIMER *timer);
int s0_init(THREADTIMER *timer);
int s1_get(THREADTIMER *timer);
int s2_set(THREADTIMER *timer);
int s3_set_data(THREADTIMER *timer);
int s4_del(THREADTIMER *timer);
int s5_md5(THREADTIMER *timer);
int bind_server(uint16_t port, u_int try_times) {
int fail_count = 0; int fail_count = 0;
int result = -1; int result = -1;
#ifdef LISTEN_ON_IPV6 #ifdef LISTEN_ON_IPV6
@@ -99,7 +62,7 @@ int bind_server(uint16_t port, u_int try_times) {
} }
} }
int listen_socket(u_int try_times) { int listen_socket(int try_times) {
int fail_count = 0; int fail_count = 0;
int result = -1; int result = -1;
while(!~(result = listen(fd, 10)) && fail_count++ < try_times) sleep(1); while(!~(result = listen(fd, 10)) && fail_count++ < try_times) sleep(1);
@@ -125,16 +88,15 @@ int send_data(int accept_fd, char *data, size_t length) {
int free_after_send(int accept_fd, char *data, size_t length) { int free_after_send(int accept_fd, char *data, size_t length) {
int re = send_data(accept_fd, data, length); int re = send_data(accept_fd, data, length);
free(data);\ free(data);
return re; return re;
} }
int send_all(THREADTIMER *timer) { int send_all(THREADTIMER *timer) {
int re = 1; int re = 1;
FILE *fp = open_dict(LOCK_SH); FILE *fp = open_dict(LOCK_SH, timer->index);
if(fp) { if(fp) {
timer->fp = fp; timer->lock_type = LOCK_SH;
timer->is_open = 1;
off_t len = 0, file_size = get_dict_size(); off_t len = 0, file_size = get_dict_size();
sprintf(timer->data, "%u$", file_size); sprintf(timer->data, "%u$", file_size);
printf("Get file size: %s bytes.\n", timer->data); printf("Get file size: %s bytes.\n", timer->data);
@@ -154,8 +116,7 @@ int send_all(THREADTIMER *timer) {
re = sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0; re = sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0;
#endif #endif
printf("Send %u bytes.\n", len); printf("Send %u bytes.\n", len);
close_dict(); close_dict(LOCK_SH);
timer->is_open = 0;
} }
return re; return re;
} }
@@ -178,37 +139,31 @@ int s0_init(THREADTIMER *timer) {
#define has_next(fp, ch) ((ch=getc(fp)),(feof(fp)?0:(ungetc(ch,fp),1))) #define has_next(fp, ch) ((ch=getc(fp)),(feof(fp)?0:(ungetc(ch,fp),1)))
int s1_get(THREADTIMER *timer) { int s1_get(THREADTIMER *timer) {
FILE *fp = open_dict(LOCK_SH); FILE *fp = open_dict(LOCK_SH, timer->index);
timer->status = 0; timer->status = 0;
if(fp) { if(fp) {
timer->fp = fp;
timer->is_open = 1;
int ch; int ch;
timer->lock_type = LOCK_SH;
while(has_next(fp, ch)) { while(has_next(fp, ch)) {
SIMPLE_PB* spb = get_pb(fp); SIMPLE_PB* spb = get_pb(fp);
DICT* d = (DICT*)spb->target; DICT* d = (DICT*)spb->target;
if(!strcmp(timer->data, d->key)) { if(!strcmp(timer->data, d->key)) {
int r = close_and_send(timer, d->data, last_nonnull(d->data, ITEMSZ)); int r = close_and_send(timer->accept_fd, d->data, last_nonnull(d->data, ITEMSZ), LOCK_SH);
free(spb); free(spb);
return r; return r;
} else free(spb); } else free(spb);
} }
} }
return close_and_send(timer, "null", 4); return close_and_send(timer->accept_fd, "null", 4, LOCK_SH);
}
#define copy_key() {\
strncpy(d.key, timer->data, ITEMSZ-1);\
} }
int s2_set(THREADTIMER *timer) { int s2_set(THREADTIMER *timer) {
FILE *fp = open_dict(LOCK_EX); FILE *fp = open_dict(LOCK_EX, timer->index);
if(fp) { if(fp) {
timer->lock_type = LOCK_EX;
timer->status = 3; timer->status = 3;
timer->fp = fp;
timer->is_open = 1;
memset(&d, 0, sizeof(DICT)); memset(&d, 0, sizeof(DICT));
copy_key(); strncpy(d.key, timer->data, ITEMSZ-1);
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
return send_data(timer->accept_fd, "data", 4); return send_data(timer->accept_fd, "data", 4);
} else { } else {
@@ -223,22 +178,21 @@ int s3_set_data(THREADTIMER *timer) {
printf("Set data size: %u\n", datasize); printf("Set data size: %u\n", datasize);
memcpy(d.data, timer->data, datasize); memcpy(d.data, timer->data, datasize);
puts("Data copy to dict succ"); puts("Data copy to dict succ");
if(!set_pb(timer->fp, items_len, sizeof(DICT), &d)) { if(!set_pb(get_dict_fp(timer->index), items_len, sizeof(DICT), &d)) {
printf("Error set data: dict[%s]=%s\n", d.key, timer->data); printf("Error set data: dict[%s]=%s\n", d.key, timer->data);
return close_and_send(timer, "erro", 4); return close_and_send(timer->accept_fd, "erro", 4, LOCK_EX);
} else { } else {
printf("Set data: dict[%s]=%s\n", d.key, timer->data); printf("Set data: dict[%s]=%s\n", d.key, timer->data);
return close_and_send(timer, "succ", 4); return close_and_send(timer->accept_fd, "succ", 4, LOCK_EX);
} }
} }
int s4_del(THREADTIMER *timer) { int s4_del(THREADTIMER *timer) {
FILE *fp = open_dict(LOCK_EX); FILE *fp = open_dict(LOCK_EX, timer->index);
timer->status = 0; timer->status = 0;
if(fp) { if(fp) {
timer->fp = fp;
timer->is_open = 1;
int ch; int ch;
timer->lock_type = LOCK_EX;
while(has_next(fp, ch)) { while(has_next(fp, ch)) {
SIMPLE_PB* spb = get_pb(fp); SIMPLE_PB* spb = get_pb(fp);
DICT* d = (DICT*)spb->target; DICT* d = (DICT*)spb->target;
@@ -250,10 +204,10 @@ int s4_del(THREADTIMER *timer) {
if(next == end) { if(next == end) {
if(!ftruncate(fileno(fp), end - spb->real_len)) { if(!ftruncate(fileno(fp), end - spb->real_len)) {
free(spb); free(spb);
return close_and_send(timer, "succ", 4); return close_and_send(timer->accept_fd, "succ", 4, LOCK_EX);
} else { } else {
free(spb); free(spb);
return close_and_send(timer, "erro", 4); return close_and_send(timer->accept_fd, "erro", 4, LOCK_EX);
} }
} else { } else {
uint32_t cap = end - next; uint32_t cap = end - next;
@@ -267,23 +221,24 @@ int s4_del(THREADTIMER *timer) {
if(fwrite(data, cap, 1, fp) == 1) { if(fwrite(data, cap, 1, fp) == 1) {
free(data); free(data);
free(spb); free(spb);
return close_and_send(timer, "succ", 4); return close_and_send(timer->accept_fd, "succ", 4, LOCK_EX);
} }
} }
} }
free(data); free(data);
} }
free(spb); free(spb);
return close_and_send(timer, "erro", 4); return close_and_send(timer->accept_fd, "erro", 4, LOCK_EX);
} }
} else free(spb); } else free(spb);
} }
} }
return close_and_send(timer, "null", 4); return close_and_send(timer->accept_fd, "null", 4, LOCK_EX);
} }
int s5_md5(THREADTIMER *timer) { int s5_md5(THREADTIMER *timer) {
timer->status = 0; timer->status = 0;
fill_md5();
if(is_md5_equal(timer->data)) return send_data(timer->accept_fd, "null", 4); if(is_md5_equal(timer->data)) return send_data(timer->accept_fd, "null", 4);
else return send_data(timer->accept_fd, "nequ", 4); else return send_data(timer->accept_fd, "nequ", 4);
} }
@@ -313,7 +268,8 @@ void handle_quit(int signo) {
void accept_timer(void *p) { void accept_timer(void *p) {
pthread_detach(pthread_self()); pthread_detach(pthread_self());
THREADTIMER *timer = timer_pointer_of(p); THREADTIMER *timer = timer_pointer_of(p);
while(*(timer->thread) && !pthread_kill(*(timer->thread), 0)) { uint32_t index = timer->index;
while(accept_threads[index] && !pthread_kill(accept_threads[index], 0)) {
sleep(MAXWAITSEC / 4); sleep(MAXWAITSEC / 4);
puts("Check accept status"); puts("Check accept status");
if(time(NULL) - timer->touch > MAXWAITSEC) break; if(time(NULL) - timer->touch > MAXWAITSEC) break;
@@ -327,9 +283,11 @@ void accept_timer(void *p) {
void kill_thread(THREADTIMER* timer) { void kill_thread(THREADTIMER* timer) {
puts("Start killing."); puts("Start killing.");
if(*(timer->thread)) { uint32_t index = timer->index;
pthread_kill(*(timer->thread), SIGQUIT); pthread_t thread = accept_threads[index];
*(timer->thread) = 0; if(thread) {
pthread_kill(thread, SIGQUIT);
accept_threads[index] = 0;
puts("Kill thread."); puts("Kill thread.");
} }
if(timer->accept_fd) { if(timer->accept_fd) {
@@ -342,11 +300,7 @@ void kill_thread(THREADTIMER* timer) {
timer->data = NULL; timer->data = NULL;
puts("Free data."); puts("Free data.");
} }
if(timer->is_open) { if(timer->lock_type) close_dict(timer->lock_type);
close_dict();
timer->is_open = 0;
puts("Close file.");
}
puts("Finish killing."); puts("Finish killing.");
} }
@@ -381,10 +335,11 @@ void handle_accept(void *p) {
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);
timer_pointer_of(p)->status = -1; timer_pointer_of(p)->status = -1;
uint32_t index = timer_pointer_of(p)->index;
char *buff = calloc(BUFSIZ, sizeof(char)); char *buff = calloc(BUFSIZ, sizeof(char));
if(buff) { if(buff) {
timer_pointer_of(p)->data = buff; timer_pointer_of(p)->data = buff;
while(*(timer_pointer_of(p)->thread) && (timer_pointer_of(p)->numbytes = recv(accept_fd, buff, BUFSIZ, 0)) > 0) { while(accept_threads[index] && (timer_pointer_of(p)->numbytes = recv(accept_fd, buff, BUFSIZ, 0)) > 0) {
touch_timer(p); touch_timer(p);
buff[timer_pointer_of(p)->numbytes] = 0; buff[timer_pointer_of(p)->numbytes] = 0;
printf("Get %u bytes: %s\n", timer_pointer_of(p)->numbytes, buff); printf("Get %u bytes: %s\n", timer_pointer_of(p)->numbytes, buff);
@@ -398,7 +353,7 @@ void handle_accept(void *p) {
} }
printf("Break: recv %u bytes\n", timer_pointer_of(p)->numbytes); printf("Break: recv %u bytes\n", timer_pointer_of(p)->numbytes);
} else puts("Error allocating buffer"); } else puts("Error allocating buffer");
*(timer_pointer_of(p)->thread) = 0; accept_threads[index] = 0;
kill_thread(timer_pointer_of(p)); kill_thread(timer_pointer_of(p));
} else puts("Error accepting client"); } else puts("Error accepting client");
} }
@@ -421,14 +376,12 @@ void accept_client() {
if(timer) { if(timer) {
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len); timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
timer->thread = &accept_threads[p]; timer->index = p;
timer->touch = time(NULL); timer->touch = time(NULL);
timer->data = NULL; timer->data = NULL;
timer->is_open = 0;
timer->fp = NULL;
signal(SIGQUIT, handle_quit); signal(SIGQUIT, handle_quit);
signal(SIGPIPE, handle_pipe); signal(SIGPIPE, handle_pipe);
if (pthread_create(timer->thread, NULL, (void *)&handle_accept, timer)) puts("Error creating thread"); if (pthread_create(accept_threads + p, NULL, (void *)&handle_accept, timer)) puts("Error creating thread");
else puts("Creating thread succeeded"); else puts("Creating thread succeeded");
} else puts("Allocate timer error"); } else puts("Allocate timer error");
} else { } else {
@@ -438,10 +391,9 @@ void accept_client() {
} }
} }
int close_and_send(THREADTIMER *timer, char *data, size_t numbytes) { int close_and_send(int accept_fd, char *data, size_t numbytes, uint32_t lock_type) {
close_dict(); close_dict(lock_type);
timer->is_open = 0; return send_data(accept_fd, data, numbytes);
return send_data(timer->accept_fd, data, numbytes);
} }
#define set_pass(pass, sps, slen, cmd) (pass=malloc(strlen(cmd)+slen+1),((pass)?(strcpy(pass,cmd),strcpy(pass+strlen(cmd),sps),1):0)) #define set_pass(pass, sps, slen, cmd) (pass=malloc(strlen(cmd)+slen+1),((pass)?(strcpy(pass,cmd),strcpy(pass+strlen(cmd),sps),1):0))

42
server.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef _SERVER_H_
#define _SERVER_H_
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#define THREADCNT 16
#define MAXWAITSEC 10
struct THREADTIMER {
uint32_t index;
time_t touch;
int accept_fd;
ssize_t numbytes;
char *data;
char status, lock_type;
};
typedef struct THREADTIMER THREADTIMER;
void accept_client();
void accept_timer(void *p);
int bind_server(uint16_t port, int try_times);
int check_buffer(THREADTIMER *timer);
int close_and_send(int accept_fd, char *data, size_t numbytes, uint32_t lock_type);
int free_after_send(int accept_fd, char *data, size_t length);
void handle_accept(void *accept_fd_p);
void handle_pipe(int signo);
void handle_quit(int signo);
void kill_thread(THREADTIMER* timer);
int listen_socket(int try_times);
int send_all(THREADTIMER *timer);
int send_data(int accept_fd, char *data, size_t length);
int sm1_pwd(THREADTIMER *timer);
int s0_init(THREADTIMER *timer);
int s1_get(THREADTIMER *timer);
int s2_set(THREADTIMER *timer);
int s3_set_data(THREADTIMER *timer);
int s4_del(THREADTIMER *timer);
int s5_md5(THREADTIMER *timer);
#endif