mirror of
https://github.com/fumiama/CMoe-Counter.git
synced 2026-06-12 22:40:42 +08:00
fix flock
This commit is contained in:
31
cmoe.c
31
cmoe.c
@@ -13,8 +13,6 @@ static counter_t counter;
|
|||||||
static char* datfile = "dat.sp";
|
static char* datfile = "dat.sp";
|
||||||
static char* token = "fumiama";
|
static char* token = "fumiama";
|
||||||
|
|
||||||
static FILE* fp;
|
|
||||||
|
|
||||||
#define ADD_HEADER_PARAM(buf, offset, h, p) sprintf(buf + offset, (h), (p))
|
#define ADD_HEADER_PARAM(buf, offset, h, p) sprintf(buf + offset, (h), (p))
|
||||||
#define HEADER(content_type) HTTP200 SERVER_STRING CACHE_CTRL CONTENT_TYPE(content_type)
|
#define HEADER(content_type) HTTP200 SERVER_STRING CACHE_CTRL CONTENT_TYPE(content_type)
|
||||||
#define headers(content_len, content_type) (_headers(content_len, HEADER(content_type), sizeof(HEADER(content_type))-1))
|
#define headers(content_len, content_type) (_headers(content_len, HEADER(content_type), sizeof(HEADER(content_type))-1))
|
||||||
@@ -167,13 +165,7 @@ static int name_exist(FILE* fp, char* name) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define create_or_open(fp, filename) ((fp = fopen(filename, "rb+"))?(fp):(fp = fopen(filename, "wb+")))
|
#define create_or_open(filename) (open(filename, O_CREAT|O_RDWR, 0600))
|
||||||
|
|
||||||
#define flease(fp) { \
|
|
||||||
fflush(fp); \
|
|
||||||
flock(fileno(fp), LOCK_UN); \
|
|
||||||
fclose(fp); fp = NULL; \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define QS (argv[2])
|
#define QS (argv[2])
|
||||||
// Usage: cmoe method query_string
|
// Usage: cmoe method query_string
|
||||||
@@ -206,17 +198,19 @@ int main(int argc, char **argv) {
|
|||||||
theme = get_arg(theme + 6);
|
theme = get_arg(theme + 6);
|
||||||
}
|
}
|
||||||
char* reg = strstr(QS, "reg=");
|
char* reg = strstr(QS, "reg=");
|
||||||
|
int fd; FILE* fp;
|
||||||
if (!reg) {
|
if (!reg) {
|
||||||
if(!create_or_open(fp, datfile)) {
|
if((fd=create_or_open(datfile)) < 0) {
|
||||||
http_error(HTTP500, "Open File Error.");
|
http_error(HTTP500, "Open File Error.");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
if(flock(fileno(fp), LOCK_EX)) {
|
if(flock(fd, LOCK_EX)) {
|
||||||
http_error(HTTP500, "Lock File Error.");
|
http_error(HTTP500, "Lock File Error.");
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
fp = fdopen(fd, "rb+");
|
||||||
return_count(fp, name, theme);
|
return_count(fp, name, theme);
|
||||||
flease(fp);
|
fclose(fp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
reg = get_arg(reg + 4);
|
reg = get_arg(reg + 4);
|
||||||
@@ -228,22 +222,23 @@ int main(int argc, char **argv) {
|
|||||||
http_error(HTTP400, "Token Error.");
|
http_error(HTTP400, "Token Error.");
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
if(!create_or_open(fp, datfile)) {
|
if((fd=create_or_open(datfile)) < 0) {
|
||||||
http_error(HTTP500, "Open File Error.");
|
http_error(HTTP500, "Open File Error.");
|
||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
if(flock(fileno(fp), LOCK_EX)) {
|
if(flock(fd, LOCK_EX)) {
|
||||||
http_error(HTTP500, "Lock File Error.");
|
http_error(HTTP500, "Lock File Error.");
|
||||||
return -5;
|
return -5;
|
||||||
}
|
}
|
||||||
|
fp = fdopen(fd, "rb+");
|
||||||
if(name_exist(fp, name)) {
|
if(name_exist(fp, name)) {
|
||||||
flease(fp);
|
fclose(fp);
|
||||||
http_error(HTTP400, "Name Exist.");
|
http_error(HTTP400, "Name Exist.");
|
||||||
return 7;
|
return 7;
|
||||||
}
|
}
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
add_user(name, 0, fp);
|
add_user(name, 0, fp);
|
||||||
flease(fp);
|
fclose(fp);
|
||||||
char* msg = "<P>Success.\r\n";
|
char* msg = "<P>Success.\r\n";
|
||||||
if(headers(strlen(msg), "text/html")) {
|
if(headers(strlen(msg), "text/html")) {
|
||||||
write(1, "\0\0\0\0", 4);
|
write(1, "\0\0\0\0", 4);
|
||||||
@@ -251,7 +246,3 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
return write(1, msg, strlen(msg)) <= 0;
|
return write(1, msg, strlen(msg)) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __attribute__((destructor)) defer_close_fp() {
|
|
||||||
if(fp) flease(fp);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user