-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
446 lines (397 loc) · 8.67 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*============================================================================
# Author: Wade Leng
# E-mail: wade.hit@gmail.com
# Last modified: 2012-06-19 19:29
# Filename: server.c
# Description: 实现FUSE接口,及主线程
============================================================================*/
#define FUSE_USE_VERSION 26
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <sys/wait.h>
#include <ctype.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include <pthread.h>
#include <iostream>
#include <err.h>
#include <event.h>
#include <evhttp.h>
#include <fuse.h>
#include "leveldb/iterator.h"
#include "leveldb/db.h"
#include "leveldb/comparator.h"
#include "leveldb/write_batch.h"
#include "leveldb/cache.h"
#include "database.h"
#include "utils.h"
#include "handler.h"
#include "server.h"
using namespace std;
//存放pid的文件
char* pidfile = (char*) "/tmp/full_fs.pid";
//leveldb缓存,默认100MB
int server_settings_cache = 100;
//leveldb路径
char server_settings_dataname[1024];
leveldb::DB* db;
leveldb::Options full_options;
/*
* 查看path是路径还是文件,并赋权限。
* 文件都是只读的。
* 如果不存在返回-ENOENT
* 成功返回0
*/
static int full_fs_getattr(const char* path, struct stat* stbuf)
{
int res = 0, value_size;
char* key = strdup(path), *value = NULL;
leveldb::Status s;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
} else
{
stbuf->st_mode = S_IFREG | 0444;
s = get(key + 1, &value, &value_size);
if (!s.ok())
res = -ENOENT;
else
stbuf->st_size = value_size;
free(value);
}
free(key);
return res;
}
/*
* 列出根目录下所有文件名
* 如果不在根目录下,返回 -ENOENT
* 成功返回0
*/
static int full_fs_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
char *key;
int key_size = 0;
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
for (it->SeekToFirst(); it->Valid(); it->Next())
{
key_size = it->key().ToString().length();
key = (char*) malloc(key_size * sizeof(char) + 1);
strcpy(key, it->key().ToString().c_str());
filler(buf, key, NULL, 0);
free(key);
}
delete it;
return 0;
}
/*
* 只读文件系统,不能创建目录
* 直接返回 -EPERM
*/
static int full_fs_mkdir(const char* path, mode_t mode)
{
return -EPERM; //no permission
}
/*
* 只读文件系统,不能删除目录
* 直接返回 -EPERM
*/
static int full_fs_rmdir(const char* path)
{
return -EPERM;
}
/*
* 只读文件系统,不能创建文件
* 直接返回 -EPERM
*/
static int full_fs_mknod(const char* path, mode_t mode, dev_t rdev)
{
return -EPERM; // full_fs RDONLY
}
/*
* 根据path从leveldb里读数据,到buf里。从offset处开始
* 如果存在文件,返回size
* 否则返回 -ENOENT
*/
static int full_fs_read(const char* path, char* buf, size_t size, off_t offset,
struct fuse_file_info* fi)
{
int value_size;
size_t len;
char* key = strdup(path);
char* value;
leveldb::Status s;
s = get(key + 1, &value, &value_size);
if (!s.ok())
{
free(value);
return -ENOENT;
}
len = (size_t) value_size;
if (offset < len)
{
if (offset + size > len)
size = len - offset;
memcpy(buf, value + offset, size);
} else
size = 0;
free(value);
free(key);
return size;
}
/*
* 只读文件系统,不可写
* 直接返回 -EPERM
* 只有libevent端可以写数据
*/
static int full_fs_write(const char* path, const char* buf, size_t size,
off_t offset, struct fuse_file_info* fi)
{
return -EPERM;
}
/*
* 没有权限
* 直接返回 -EPERM
*/
static int full_fs_unlink(const char* path)
{
return -EPERM;
}
/*
* 根据path看文件是否存在
* 如果不存在返回 -ENOENT
* 成功返回0
*/
static int full_fs_open(const char* path, struct fuse_file_info* fi)
{
int res = 0, value_size;
char* key = strdup(path), *value;
leveldb::Status s;
if (strcmp(path, "/") == 0)
res = 0;
else
{
s = get(key + 1, &value, &value_size);
if (!s.ok())
res = -ENOENT;
else
res = 0;
free(value);
}
free(key);
return res;
}
/*
* This function should not be modified
*/
static int full_fs_flush(const char* path, struct fuse_file_info* fi)
{
return 0;
}
/*
* This function should not be modified
*/
static int full_fs_truncate(const char* path, off_t size)
{
return 0;
}
static struct full_fs_operations : fuse_operations
{
full_fs_operations()
{
getattr = full_fs_getattr;
readdir = full_fs_readdir;
mkdir = full_fs_mkdir;
rmdir = full_fs_rmdir;
mknod = full_fs_mknod;
read = full_fs_read;
write = full_fs_write;
unlink = full_fs_unlink;
open = full_fs_open;
flush = full_fs_flush;
truncate = full_fs_truncate;
}
} full_fs_oper;
/* FUSE线程 */
void* fuse_init(void* f_argv)
{
int f_argc = 3;
fuse_main(f_argc, (char**)f_argv, &full_fs_oper, NULL);
pthread_exit(NULL);
}
/* 子进程信号处理 */
static void kill_signal_worker(const int sig)
{
delete db;
exit(0);
}
/* 父进程信号处理 */
static void kill_signal_master(const int sig)
{
remove(pidfile);
/* 给进程组所有进程发送终止信号 */
kill(0, SIGTERM);
exit(0);
}
typedef void(*ptr_event)(const int sig);
/* 处理kill信号 */
void kill_signal_register(ptr_event p)
{
signal(SIGINT, p);
signal(SIGKILL, p);
signal(SIGQUIT, p);
signal(SIGTERM, p);
signal(SIGHUP, p);
}
int main(int argc, char *argv[])
{
/* 绑定所有IP */
char* listen = (char*) "0.0.0.0";
/* fuse启动参数 */
char* f_argv[4] = {NULL};
/* 端口号 */
int port = 12345;
/* 后台运行 */
int daemon = false;
/* http请求超时时间 */
int timeout = 60;
/* leveldb路径 */
char *datapath;
int c;
while ((c = getopt(argc, argv, "l:p:x:t:c:m:i:a:r:dh")) != -1)
{
switch (c)
{
case 'l':
listen = strdup(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 'x':
datapath = strdup(optarg);
/* 测试是否可写 */
if (access(datapath, W_OK) != 0)
{
/* 测试是否可读 */
if (access(datapath, R_OK) == 0)
/*others可写*/
chmod(datapath, S_IWOTH);
else
create_multilayer_dir(datapath);
if (access(datapath, W_OK) != 0)
fprintf(stderr, "database directory not writeable!\n");
}
break;
case 't':
timeout = atoi(optarg);
break;
case 'c':
server_settings_cache = atoi(optarg) > 0 ? atoi(optarg) : 100;
break;
case 'i':
pidfile = strdup(optarg);
break;
case 'r':
f_argv[0] = argv[0];
f_argv[1] = strdup(optarg);
/* 非后台运行,否则无法启动libevnet进程就关闭了 */
f_argv[2] = (char*)"-f";
break;
case 'd':
daemon = true;
break;
case 'h':
default:
show_help();
return -1;
}
}
/* 必填参数 数据库路径 */
if (datapath == NULL)
{
show_help();
fprintf(stderr, "Attention: Please use the indispensable argument: -x <path>\n\n");
return -1;
}
/* 必填参数, 文件系统根目录路径 */
if (f_argv[1] == NULL)
{
show_help();
fprintf(stderr, "Attention: Please use the indispensable argument: -r <root_dir>\n\n");
return -1;
}
sprintf(server_settings_dataname, "%s/full_server.db", datapath);
if (!opendb().ok())
{
show_help();
fprintf(stderr, "Attention: database open failed.\n\n");
return -1;
}
/* 后台运行,fork一个进程,终止父进程 */
if (daemon)
{
pid_t pid = fork();
if (pid < 0)
return -1;
if (pid > 0)
return 0;
}
/* 写进程ID到pidfile */
/* 派生工作进程 */
pid_t worker_process_pid = fork();
if (worker_process_pid < 0) //error
{
fprintf(stderr, "Error: %s:%d\n", __FILE__, __LINE__);
return -1;
}
/* 父进程 */
if (worker_process_pid > 0)
{
/* 处理kill信号 */
kill_signal_register(&kill_signal_master);
/* 如果子进程终止,重新派生子进程 */
while (1)
{
if (wait(NULL) < 0)
continue;
usleep(100000);
worker_process_pid = fork();
if (worker_process_pid == 0)
break;
}
}
/* 处理kill信号 */
kill_signal_register(&kill_signal_worker);
FILE *fp_pidfile = fopen(pidfile, "w");
fprintf(fp_pidfile, "%d\n", getpid());
fclose(fp_pidfile);
/* FUSE线程启动 */
pthread_t pid;
pthread_create(&pid, NULL, fuse_init, f_argv);
/* libevent启动 */
httpserver_init(listen, port, timeout);
pthread_join(pid, NULL);
return 0;
}