-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunvmed.c
548 lines (424 loc) · 10.4 KB
/
unvmed.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <ccan/str/str.h>
#include <ccan/list/list.h>
#include <vfn/nvme.h>
#include <execinfo.h>
#include "libunvmed.h"
#include "unvme.h"
#include "unvmed.h"
struct unvme_job {
int client_pid;
pthread_t thread;
struct list_node list;
};
__thread FILE *__stdout = NULL;
__thread FILE *__stderr = NULL;
static char *__libfio;
static pthread_mutex_t __app_mutex;
static LIST_HEAD(__jobs);
static pthread_mutex_t __job_mutex;
__thread struct unvme_msg *__msg;
static void unvme_callstack_dump(void)
{
void *callstack[128];
int i, nr_frames;
char **strs;
nr_frames = backtrace(callstack, sizeof(callstack) / sizeof(void *));
strs = backtrace_symbols(callstack, nr_frames);
for (i = 0; i < nr_frames; i++)
unvmed_log_err("%s", strs[i]);
free(strs);
}
static inline struct unvme_job *unvme_add_job(int client_pid)
{
struct unvme_job *job = calloc(1, sizeof(*job));
job->client_pid = client_pid;
assert(job != NULL);
pthread_mutex_lock(&__job_mutex);
list_add_tail(&__jobs, &job->list);
pthread_mutex_unlock(&__job_mutex);
return job;
}
static inline struct unvme_job *unvme_get_job(int client_pid)
{
struct unvme_job *job;
pthread_mutex_lock(&__job_mutex);
list_for_each(&__jobs, job, list) {
if (job->client_pid == client_pid) {
pthread_mutex_unlock(&__job_mutex);
return job;
}
}
pthread_mutex_unlock(&__job_mutex);
return NULL;
}
static inline void unvme_del_job(int client_pid)
{
struct unvme_job *job = unvme_get_job(client_pid);
if (!job)
return;
pthread_mutex_lock(&__job_mutex);
list_del(&job->list);
pthread_mutex_unlock(&__job_mutex);
}
static int unvme_set_pid(void)
{
pid_t pid = getpid();
char spid[8] = {};
int fd;
fd = open(UNVME_DAEMON_PID, O_CREAT | O_WRONLY, S_IRUSR);
if (fd < 0)
return -EINVAL;
if (sprintf(spid, "%d", pid) < 0) {
close(fd);
return -EINVAL;
}
if (write(fd, spid, sizeof(spid)) < 0)
return -1;
close(fd);
return 0;
}
static int unvme_set_ver(void)
{
char ver[UNVME_VER_STR];
int fd;
fd = open(UNVME_DAEMON_VER, O_CREAT | O_WRONLY, S_IRUSR);
if (fd < 0)
return -1;
if (sprintf(ver, "%s", UNVME_VERSION) < 0) {
close(fd);
return -1;
}
if (write(fd, ver, strlen(ver) + 1) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
static __thread char __argv[UNVME_MAX_OPT * UNVME_MAX_STR];
static int unvme_msg_init(struct unvme_msg *msg, int argc, char **argv)
{
char *oneline = __argv;
FILE *file;
char buf[128];
int i = 0;
file = fopen(msg->msg.argv_file, "r");
if (!file) {
unvme_pr_err("ERROR: failed to open %s\n", msg->msg.argv_file);
return -1;
}
while (fgets(buf, sizeof(buf), file) != NULL) {
if (!strlen(buf))
break;
argv[i] = calloc(1, strlen(buf));
if (!argv[i]) {
unvme_pr_err("ERROR: failed to malloc\n");
goto err;
}
/* remove \n newline character */
buf[strcspn(buf, "\n")] = '\0';
strcpy(argv[i], buf);
oneline += sprintf(oneline, "%s%s", (i == 0) ? "":" ", argv[i]);
i++;
}
argv[i] = NULL;
fclose(file);
remove(msg->msg.argv_file);
unvmed_log_info("msg (pid=%d): start: '%s'", unvme_msg_pid(msg), __argv);
return 0;
err:
for (i = 0; i < argc; i++) {
if (argv[i])
free(argv[i]);
}
if (file)
fclose(file);
remove(msg->msg.argv_file);
return -1;
}
static int __unvme_handler(struct unvme_msg *msg)
{
struct command *cmd;
char **argv;
int argc = msg->msg.argc;
int ret;
argv = (char **)calloc(argc + 1, sizeof(char *));
if (!argv)
unvme_pr_return(-ENOMEM, "ERROR: failed to allocate memory\n");
if (unvme_msg_init(msg, argc, argv))
unvme_pr_return(-errno, "ERROR: failed to parse msg\n");
/*
* If the message represents termination, simple return here.
*/
if (streq(argv[1], "stop")) {
ret = -ECANCELED;
goto out;
}
ret = -EINVAL;
cmd = unvme_get_cmd(argv[1]);
if (cmd && cmd->ctype & UNVME_DAEMON_CMD) {
/*
* We should truncate the stdout and stderr file which will be
* written by the application thread per every running.
*/
if (cmd->ctype & UNVME_APP_CMD)
pthread_mutex_lock(&__app_mutex);
ret = cmd->func(argc, argv, msg);
if (cmd->ctype & UNVME_APP_CMD)
pthread_mutex_unlock(&__app_mutex);
}
out:
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
unvmed_log_info("msg (pid=%d): compl: '%s' (ret=%d, type='%s')",
unvme_msg_pid(msg), __argv, ret, strerror(abs(ret)));
return ret;
}
static int unvme_msgq_delete(const char *keyfile)
{
key_t key;
int msg_id;
key = ftok(keyfile, 0x0);
if (key < 0)
return -1;
msg_id = msgget(key, 0666);
if (msg_id < 0)
return -1;
if (msgctl(msg_id, IPC_RMID, NULL) < 0)
return -1;
if (remove(keyfile))
return -1;
return 0;
}
static int unvme_msgq_create(const char *keyfile)
{
int msg_id;
key_t key;
int fd;
retry:
fd = open(keyfile, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0) {
if (errno == EEXIST) {
if (!remove(keyfile))
goto retry;
}
unvme_pr_return(-1, "failed to create msgq keyfile %s\n",
keyfile);
}
close(fd);
key = ftok(keyfile, 0x0);
if (key < 0)
unvme_pr_return(-1, "ERROR: failed to get a key\n");
/*
* If the message queue already exists, remove it and re-create.
*/
msg_id = msgget(key, 0666);
if (msg_id >= 0) {
if (msgctl(msg_id, IPC_RMID, NULL) < 0)
unvme_pr_return(-1, "ERROR: failed to remove a msgq\n");
}
msg_id = msgget(key, IPC_CREAT | 0666);
if (msg_id < 0)
unvme_pr_return(-1, "ERROR: failed to get a msgq\n");
return msg_id;
}
static void unvme_release(int signum)
{
if (__libfio) {
free(__libfio);
__libfio = NULL;
}
unvme_msgq_delete(UNVME_MSGQ);
unvmed_free_ctrl_all();
remove(UNVME_DAEMON_VER);
remove(UNVME_DAEMON_PID);
unvmed_log_info("unvmed(pid=%d) terminated (signum=%d, sigtype='%s')",
getpid(), signum, strsignal(signum));
unvme_callstack_dump();
if (signum == SIGTERM)
exit(EXIT_SUCCESS);
exit(EXIT_FAILURE);
}
static void unvme_error(int signum)
{
unvmed_log_err("signal trapped (signum=%d, type='%s')",
signum, strsignal(signum));
unvme_release(signum);
}
static inline int unvme_cmdline_strlen(void)
{
char buf[16];
ssize_t len;
int fd;
fd = open("/proc/self/cmdline", O_RDONLY);
assert(fd > 0);
len = read(fd, buf, sizeof(buf));
close(fd);
return len;
}
static int unvme_recv_msg(struct unvme_msg *msg)
{
int msgq = unvme_msgq_get(UNVME_MSGQ);
return unvme_msgq_recv(msgq, msg, getpid());
}
static int unvme_send_msg(struct unvme_msg *msg)
{
int msgq = unvme_msgq_get(UNVME_MSGQ);
return unvme_msgq_send(msgq, msg);
}
static void __unvme_get_stdio(pid_t pid, char *filefmt, FILE **stdio)
{
char *filename = NULL;
FILE *file;
int ret;
ret = asprintf(&filename, filefmt, pid);
if (ret < 0) {
unvmed_log_err("failed to init stdio (ret=%d)", ret);
goto err;
}
file = fopen(filename, "w");
if (!file) {
unvmed_log_err("failed to open %s", filename);
goto err;
}
*stdio = file;
setbuf(*stdio, NULL);
free(filename);
return;
err:
if (filename)
free(filename);
unvme_release(0);
}
static void unvme_get_stdio(pid_t pid)
{
__unvme_get_stdio(pid, UNVME_STDOUT, &__stdout);
__unvme_get_stdio(pid, UNVME_STDERR, &__stderr);
}
void unvme_exit_job(int ret)
{
if (!__msg)
return;
unvme_msg_to_client(__msg, unvme_msg_pid(__msg), ret);
unvme_send_msg(__msg);
fclose(__stdout);
fclose(__stderr);
unvme_del_job(unvme_msg_pid(__msg));
pthread_mutex_unlock(&__app_mutex);
free(__msg);
__msg = NULL;
}
static void *unvme_handler(void *opaque)
{
struct unvme_msg *msg = (struct unvme_msg *)opaque;
pid_t pid = unvme_msg_pid(msg);
int ret;
__msg = msg;
unvme_get_stdio(pid);
ret = __unvme_handler(msg);
unvme_exit_job(ret);
pthread_exit(NULL);
}
static inline struct unvme_msg *unvme_alloc_msg(void)
{
struct unvme_msg *msg = calloc(1, sizeof(struct unvme_msg));
if (!msg) {
unvmed_log_err("failed to alloc unvme_msg");
unvme_release(0);
}
return msg;
}
const char *unvmed_get_libfio(void)
{
return __libfio;
}
static void unvme_run_job(struct unvme_msg *msg)
{
struct unvme_job *job = unvme_add_job(unvme_msg_pid(msg));
pthread_create(&job->thread, NULL, unvme_handler, (void *)msg);
}
static void unvme_signal_job(struct unvme_msg *msg)
{
struct unvme_job *job;
job = unvme_get_job(unvme_msg_pid(msg));
/*
* When sig occurs more than twice, the unvmed can't find a job.
* client continues to wait to receive msg. unvmed should send msg
*/
if (!job) {
unvme_msg_to_client(msg, unvme_msg_pid(msg), 0);
unvme_send_msg(msg);
return;
}
if (unvme_msg_signum(msg) == SIGINT || unvme_msg_signum(msg) == SIGTERM)
unvme_del_job(unvme_msg_pid(msg));
pthread_kill(job->thread, unvme_msg_signum(msg));
pthread_join(job->thread, NULL);
}
int unvmed(char *argv[], const char *fio)
{
struct unvme_msg *msg;
/*
* Convert the current process to a daemon process
*/
if (setsid() < 0)
unvme_pr_return(-1, "ERROR: failed to create new session\n");
prctl(PR_SET_NAME, (unsigned long)"unvmed", 0, 0, 0);
memset(argv[0], 0, unvme_cmdline_strlen());
strcpy(argv[0], "unvmed");
argv[0][strlen("unvmed")] = '\0';
umask(0);
if (chdir("/"))
unvme_pr_return(-1, "ERROR: failed to change directory\n");
unvmed_init(UNVME_DAEMON_LOG);
close(STDIN_FILENO);
if (!freopen(UNVME_DAEMON_STDOUT, "w", stdout))
unvmed_log_err("failed to redirect stdout to %s", UNVME_DAEMON_STDOUT);
setvbuf(stdout, NULL, _IOLBF, 0);
if (!freopen(UNVME_DAEMON_STDERR, "w", stderr))
unvmed_log_err("failed to redirect stdout to %s", UNVME_DAEMON_STDERR);
setvbuf(stderr, NULL, _IOLBF, 0);
unvme_msgq_create(UNVME_MSGQ);
unvmed_log_info("unvmed daemon process is sucessfully created "
"(pid=%d, ver='%s')", getpid(), UNVME_VERSION);
signal(SIGTERM, unvme_release);
signal(SIGSEGV, unvme_error);
signal(SIGABRT, unvme_error);
unvme_set_pid();
if (unvme_set_ver())
unvme_pr_return(-1, "ERROR: failed to set unvme-cli version\n");
pthread_mutex_init(&__app_mutex, NULL);
pthread_mutex_init(&__job_mutex, NULL);
unvmed_log_info("ready to receive messages from client ...");
if (fio)
__libfio = strdup(fio);
while (true) {
msg = unvme_alloc_msg();
if (unvme_recv_msg(msg) < 0)
return -1;
if (unvme_msg_is_normal(msg))
unvme_run_job(msg);
else
unvme_signal_job(msg);
}
/*
* Should not reach here.
*/
return 0;
}