-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskmountd.c
548 lines (456 loc) · 10.5 KB
/
diskmountd.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
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef WITH_UGID
#include <grp.h>
#include <pwd.h>
#endif
#include <sys/mount.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#ifdef WITH_LIBMOUNT
#include <libmount/libmount.h>
#endif
#include "util.h"
#include "diskconf.h"
#include "disktab.h"
#include "diskev.h"
#include "nlsock.h"
#include "evsock.h"
#define EV_SCHED_TIME 1
struct diskmnt_ctx {
int verbosity;
int daemonize;
int kevent;
int monitor;
int debug;
#ifdef WITH_UGID
int uid;
int gid;
#endif
};
struct diskmnt_ctx ctx;
static int quit;
static void sigterm(int signo)
{
vinfo("Got signal %u", signo);
quit = 1;
}
static int perform_mount(const char *device, const char *point,
const char *type, unsigned long flags, const char *opts)
{
#if defined(WITH_LIBMOUNT)
struct libmnt_context *cxt;
mnt_init_debug(0);
cxt = mnt_new_context();
if (!cxt) {
verror("Failed to allocate MNT context");
return -1;
}
if (mnt_context_enable_verbose(cxt, 1)) {
verror("Failed to set MNT verbose");
}
if (mnt_context_append_options(cxt, opts ? opts : "rw")) {
verror("Failed to set MNT 'rw' options '%s'", opts);
goto fail;
}
if (mnt_context_set_fstype(cxt, type)) {
verror("Failed to set MNT type '%s'", type);
goto fail;
}
if (mnt_context_set_source(cxt, device)) {
verror("Failed to set MNT source '%s'", device);
goto fail;
}
if (mnt_context_set_target(cxt, point)) {
verror("Failed to set MNT target '%s'", point);
goto fail;
}
if (mnt_context_set_mflags(cxt, flags)) {
verror("Failed to set MNT flags '%lu'", flags);
goto fail;
}
if (mnt_context_is_restricted(cxt)) {
vwarn("Mounting is restricted");
}
if (mnt_context_mount(cxt)) {
goto fail;
}
return 0;
fail:
mnt_free_context(cxt);
return 1;
#elif defined(WITH_SHMOUNT)
char cmd[128];
snprintf(cmd, sizeof(cmd), "mount -t '%s' -o '%s' %s %s",
type, opts ? opts : "rw", device, point);
return system(cmd);
#else
return mount(device, point, fs, flags, data);
#endif
}
static int perform_umount(const char *device, const char *point)
{
#if defined(WITH_SHMOUNT)
char cmd[32];
snprintf(cmd, sizeof(cmd), "umount %s", point);
return system(cmd);
#else
return umount(point);
#endif
}
static void process_mount(struct diskev *evt)
{
char *point, *fs, *opts;
char *device = evt->device;
char *action = evt->action;
vdebug("Processing mount event: '%s'", device);
if (!strcmp(action, "add")) {
/* Try to fill up missing event
* properties; required delayed
* sanitize for add event. */
if (ev_sanitize(evt)) {
warn("Skip mount, cannot sanitize mount: '%s'", device);
return;
}
if (ctx.monitor) {
ev_dump(stdout, evt);
return;
}
if (conf_find(evt, &point, &fs, &opts)) {
debug("Skip mount, no confiured mount: '%s'", device);
return;
}
if (!fs)
fs = evt->filesys;
if (!fs) {
error("Skip mount, unknown file system: '%s'", device);
return;
}
info("Mounting '%s' -> '%s' (%s, %s)", device, point, fs, opts);
if (mkdir(point, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
verror("Failed to create create dir '%s'", point);
#ifdef WITH_UGID
if (ctx.uid && ctx.gid && chown(point, ctx.uid, ctx.gid))
verror("Failed to chown created dir '%s'", point);
#endif
if (perform_mount(device, point, fs, MS_NOSUID | MS_NOATIME, opts))
error("Failed to mount '%s' to '%s', type '%s', opts '%s': %u (%s)",
device, point, fs, opts, errno, strerror(errno));
else
tab_add(device, point);
} else if (!strcmp(action, "remove")) {
if (ctx.monitor) {
ev_dump(stdout, evt);
return;
}
point = tab_find(device);
if (!point) {
debug("Skip unmount, not mounted '%s'", device);
return;
}
info("Unmounting '%s' -> '%s'", device, point);
if (perform_umount(device, point))
error("Failed to unmount '%s' from '%s': %u (%s)",
device, point, errno, strerror(errno));
else
tab_del(device);
} else {
warn("Unknown event '%s' mounting '%s' -> '%s' (%s, %s)",
action, device, point, fs, opts);
}
}
static void process_events(void)
{
struct diskev *tmp;
while ((tmp = ev_next())) {
/* Find mount point and do mount */
process_mount(tmp);
ev_free(tmp);
free(tmp);
}
}
static void schedule_event(struct diskev *evt)
{
struct diskev *tmp;
tmp = ev_find(evt);
if (!tmp) {
debug("Scheduling new event");
ev_insert(evt, EV_SCHED_TIME);
return;
}
/* Implies add or remove action. */
if (strcmp(evt->action, tmp->action)) {
debug("Inverse event already in queue, removing");
ev_remove(tmp);
return;
} else {
debug("Similar event already in queue, ignoring");
/* Shall we refresh event time stamp? */
}
ev_free(evt);
}
static void handle_kobj_event(int sock)
{
struct diskev evt;
size_t descr;
size_t len;
size_t size = getpagesize() * 2;
char dbuf[size];
char *buf = dbuf;
memset(buf, 0, size);
len = nlsock_recv(sock, buf, size);
if (len < 0) {
warn("Failed kobject uevent receive");
return;
} else if (len == 0) {
info("Empty kobject uevent message");
return;
}
if (!strchr(buf, '@')) {
warn("Invalid kobject uevent format, size %zu", len);
return;
}
vinfo("Received kobject uevent, size %zu/%zu", size, len);
/* Skip desriptive line. */
descr = strlen(buf) + 1;
len -= descr;
buf += descr;
if (nlev_parse(&evt, buf, len)) {
debug("Invalid kobject uevent message, size %zu", len);
return;
}
schedule_event(&evt);
}
static void handle_udev_event(int sock)
{
struct diskev evt;
struct udev_monitor_netlink_header *umh;
size_t descr;
size_t len;
size_t size = getpagesize() * 2;
char dbuf[size];
char *buf = dbuf;
memset(buf, 0, size);
len = nlsock_recv(sock, buf, size);
if (len < 0) {
warn("Failed udev uevent receive");
return;
} else if (len == 0) {
info("Empty udev uevent message");
return;
}
umh = (struct udev_monitor_netlink_header *)buf;
if (len < sizeof(*umh)) {
warn("Short udev uevent size: %zu", len);
return;
}
if (strcmp(umh->prefix, "libudev") || (ntohl(umh->magic) != UDEV_MONITOR_MAGIC)) {
warn("Invalid udev uevent format, size: %zu", len);
return;
}
vinfo("Received udev uevent, size %zu/%zu", size, len);
/* Skip event header. */
descr = sizeof(*umh);
len -= descr;
buf += descr;
if (nlev_parse(&evt, buf, len)) {
debug("Invalid udev uevent message, size %zu", len);
return;
}
schedule_event(&evt);
}
static void handle_local_event(int sock)
{
struct diskev evt;
struct evtlv *evh;
int magic;
size_t len;
size_t size = getpagesize() * 2;
char dbuf[size];
char *buf = dbuf;
len = sizeof(magic);
if (evsock_read(sock, (char *)&magic, &len)) {
error("Failed event magic receive");
return;
}
if (magic != EVHEAD_MAGIC) {
info("Invalid event magic");
return;
}
len = size;
if (evsock_read(sock, buf, &len)) {
error("Failed event header receive");
return;
}
evh = (struct evtlv *)buf;
if (evh->type != EVTYPE_GROUP || evh->length != (len - sizeof(*evh))) {
error("Invalid event header, type %i, size %u/%zu",
evh->type, evh->length, len);
return;
}
vinfo("Read local event, size %u/%zu", evh->length, len);
if (evev_parse(&evt, evh->value, evh->length)) {
warn("Invalid event message, size %u", evh->length);
return;
}
schedule_event(&evt);
}
#ifdef WITH_UGID
static int user2uid(const char *name)
{
struct passwd *pwd = getpwnam(name);
if (!pwd)
die("User '%s' does not exist", name);
return pwd->pw_uid;
}
static int group2gid(const char *name)
{
struct group *grp = getgrnam(name);
if (!grp)
die("Group '%s' does not exist", name);
return grp->gr_gid;
}
#endif
static void
print_usage(void)
{
fprintf(stderr, "Usage: diskmountd <options>\n"
" -b, --background Run as daemon.\n"
" -m, --monitor Event monitoring.\n"
" -k, --kevent Force kernel uevent.\n"
" -v, --verbose Increase verbosity.\n"
" -d, --debug Debug mode.\n"
#ifdef WITH_UGID
" -u, --user <name> User name.\n"
" -g, --group <name> Group name.\n"
#endif
" -h, --help Show this help.\n"
);
}
static struct option long_options[] =
{
{ "help", no_argument, 0, 'h' },
{ "background", no_argument, 0, 'b' },
{ "monitor", no_argument, 0, 'm' },
{ "kevent", no_argument, 0, 'k' },
{ "verbose", no_argument, 0, 'v' },
{ "debug", no_argument, 0, 'd' },
#ifdef WITH_UGID
{ "user", required_argument, 0, 'u' },
{ "group", required_argument, 0, 'g' },
#endif
{ 0, 0, 0, 0 }
};
static void
parse_options(int argc, char *argv[])
{
int opt, index;
ctx.verbosity = 2;
while ((opt = getopt_long(argc, argv, "bdg:hkmu:v", long_options, &index)) != -1) {
switch(opt) {
case 'b':
ctx.daemonize = 1;
break;
case 'k':
ctx.kevent = 1;
break;
case 'm':
ctx.monitor = 1;
break;
case 'v':
ctx.verbosity++;
break;
case 'd':
ctx.debug = 1;
break;
#ifdef WITH_UGID
case 'u':
ctx.uid = user2uid(optarg);
break;
case 'g':
ctx.gid = group2gid(optarg);
break;
#endif
case 'h':
print_usage();
exit(EXIT_SUCCESS);
default:
print_usage();
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char *argv[])
{
int kern_feed;
int evsock;
int nlsock;
parse_options(argc, argv);
log_debug(ctx.debug);
log_level(ctx.verbosity);
/* Load mount config */
conf_load();
/* Load configured mounts */
tab_load();
if (ctx.monitor) {
conf_dump(stdout);
tab_dump(stdout);
}
if (!ctx.monitor && ctx.daemonize) {
if (daemon(0, 0) == -1)
die("daemon() failed\n");
syslog_open();
}
signal(SIGTERM, sigterm);
signal(SIGQUIT, sigterm);
if (!ctx.kevent && !access("/run/udev/control", F_OK)) {
debug("Subscribed to udev events");
kern_feed = 0;
} else {
debug("Subscribed to kobject events");
kern_feed = 1;
}
if (kern_feed)
nlsock = nlsock_open(UEVENT_KERNEL);
else
nlsock = nlsock_open(UEVENT_UDEV);
evsock = evsock_open();
while (!quit) {
struct timeval timeout = { 0, 500*1000 };
int maxfd = -1, n;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(nlsock, &rfds);
maxfd = MAX(maxfd, nlsock);
FD_SET(evsock, &rfds);
maxfd = MAX(maxfd, evsock);
n = select(maxfd + 1, &rfds, NULL, NULL, &timeout);
if (n < 0) {
if (errno == EINTR)
continue;
die("select() failed");
break;
}
if (FD_ISSET(nlsock, &rfds)) {
if (kern_feed)
handle_kobj_event(nlsock);
else
handle_udev_event(nlsock);
}
if (FD_ISSET(evsock, &rfds)) {
handle_local_event(evsock);
}
process_events();
}
nlsock_close(nlsock);
evsock_close(evsock);
syslog_close();
return 0;
}