add missing includes
[project/fstools.git] / block.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #define _GNU_SOURCE
16 #include <getopt.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <libgen.h>
21 #include <glob.h>
22 #include <dirent.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/swap.h>
32 #include <sys/mount.h>
33 #include <sys/wait.h>
34 #include <sys/sysmacros.h>
35
36 #include <linux/fs.h>
37
38 #include <uci.h>
39 #include <uci_blob.h>
40
41 #include <libubox/ulog.h>
42 #include <libubox/list.h>
43 #include <libubox/vlist.h>
44 #include <libubox/blobmsg_json.h>
45 #include <libubox/avl-cmp.h>
46
47 #include "probe.h"
48
49 #ifdef UBIFS_EXTROOT
50 #include "libubi/libubi.h"
51 #endif
52
53 enum {
54 TYPE_MOUNT,
55 TYPE_SWAP,
56 };
57
58 struct mount {
59 struct vlist_node node;
60 int type;
61
62 char *target;
63 char *path;
64 char *options;
65 uint32_t flags;
66 char *uuid;
67 char *label;
68 char *device;
69 int extroot;
70 int overlay;
71 int disabled_fsck;
72 unsigned int prio;
73 };
74
75 static struct vlist_tree mounts;
76 static struct blob_buf b;
77 static LIST_HEAD(devices);
78 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
79 static unsigned int delay_root;
80
81 enum {
82 CFG_ANON_MOUNT,
83 CFG_ANON_SWAP,
84 CFG_AUTO_MOUNT,
85 CFG_AUTO_SWAP,
86 CFG_DELAY_ROOT,
87 CFG_CHECK_FS,
88 __CFG_MAX
89 };
90
91 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
92 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
93 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
94 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
95 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
96 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
97 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
98 };
99
100 enum {
101 MOUNT_UUID,
102 MOUNT_LABEL,
103 MOUNT_ENABLE,
104 MOUNT_TARGET,
105 MOUNT_DEVICE,
106 MOUNT_OPTIONS,
107 __MOUNT_MAX
108 };
109
110 static const struct uci_blob_param_list config_attr_list = {
111 .n_params = __CFG_MAX,
112 .params = config_policy,
113 };
114
115 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
116 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
117 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
118 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
119 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
120 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
121 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
122 };
123
124 static const struct uci_blob_param_list mount_attr_list = {
125 .n_params = __MOUNT_MAX,
126 .params = mount_policy,
127 };
128
129 enum {
130 SWAP_ENABLE,
131 SWAP_UUID,
132 SWAP_LABEL,
133 SWAP_DEVICE,
134 SWAP_PRIO,
135 __SWAP_MAX
136 };
137
138 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
139 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
140 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
141 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
142 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
143 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
144 };
145
146 static const struct uci_blob_param_list swap_attr_list = {
147 .n_params = __SWAP_MAX,
148 .params = swap_policy,
149 };
150
151 struct mount_flag {
152 const char *name;
153 int32_t flag;
154 };
155
156 static const struct mount_flag mount_flags[] = {
157 { "sync", MS_SYNCHRONOUS },
158 { "async", ~MS_SYNCHRONOUS },
159 { "dirsync", MS_DIRSYNC },
160 { "mand", MS_MANDLOCK },
161 { "nomand", ~MS_MANDLOCK },
162 { "atime", ~MS_NOATIME },
163 { "noatime", MS_NOATIME },
164 { "dev", ~MS_NODEV },
165 { "nodev", MS_NODEV },
166 { "diratime", ~MS_NODIRATIME },
167 { "nodiratime", MS_NODIRATIME },
168 { "exec", ~MS_NOEXEC },
169 { "noexec", MS_NOEXEC },
170 { "suid", ~MS_NOSUID },
171 { "nosuid", MS_NOSUID },
172 { "rw", ~MS_RDONLY },
173 { "ro", MS_RDONLY },
174 { "relatime", MS_RELATIME },
175 { "norelatime", ~MS_RELATIME },
176 { "strictatime", MS_STRICTATIME },
177 { "acl", MS_POSIXACL },
178 { "noacl", ~MS_POSIXACL },
179 { "nouser_xattr", MS_NOUSER },
180 { "user_xattr", ~MS_NOUSER },
181 };
182
183 static char *blobmsg_get_strdup(struct blob_attr *attr)
184 {
185 if (!attr)
186 return NULL;
187
188 return strdup(blobmsg_get_string(attr));
189 }
190
191 static char *blobmsg_get_basename(struct blob_attr *attr)
192 {
193 if (!attr)
194 return NULL;
195
196 return strdup(basename(blobmsg_get_string(attr)));
197 }
198
199 static void parse_mount_options(struct mount *m, char *optstr)
200 {
201 int i;
202 bool is_flag;
203 char *p, *opts, *last;
204
205 m->flags = 0;
206 m->options = NULL;
207
208 if (!optstr || !*optstr)
209 return;
210
211 m->options = opts = calloc(1, strlen(optstr) + 1);
212
213 if (!m->options)
214 return;
215
216 p = last = optstr;
217
218 do {
219 p = strchr(p, ',');
220
221 if (p)
222 *p++ = 0;
223
224 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
225 if (!strcmp(last, mount_flags[i].name)) {
226 if (mount_flags[i].flag < 0)
227 m->flags &= (uint32_t)mount_flags[i].flag;
228 else
229 m->flags |= (uint32_t)mount_flags[i].flag;
230 is_flag = true;
231 break;
232 }
233 }
234
235 if (!is_flag)
236 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
237
238 last = p;
239
240 } while (p);
241
242 free(optstr);
243 }
244
245 static int mount_add(struct uci_section *s)
246 {
247 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
248 struct mount *m;
249
250 blob_buf_init(&b, 0);
251 uci_to_blob(&b, s, &mount_attr_list);
252 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
253
254 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
255 return -1;
256
257 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
258 return -1;
259
260 m = malloc(sizeof(struct mount));
261 m->type = TYPE_MOUNT;
262 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
263 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
264 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
265 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
266
267 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
268
269 m->overlay = m->extroot = 0;
270 if (m->target && !strcmp(m->target, "/"))
271 m->extroot = 1;
272 if (m->target && !strcmp(m->target, "/overlay"))
273 m->extroot = m->overlay = 1;
274
275 if (m->target && *m->target != '/') {
276 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
277 s->e.name, m->target);
278 free(m);
279 return -1;
280 }
281
282 if (m->uuid)
283 vlist_add(&mounts, &m->node, m->uuid);
284 else if (m->label)
285 vlist_add(&mounts, &m->node, m->label);
286 else if (m->device)
287 vlist_add(&mounts, &m->node, m->device);
288
289 return 0;
290 }
291
292 static int swap_add(struct uci_section *s)
293 {
294 struct blob_attr *tb[__SWAP_MAX] = { 0 };
295 struct mount *m;
296
297 blob_buf_init(&b, 0);
298 uci_to_blob(&b, s, &swap_attr_list);
299 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
300
301 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
302 return -1;
303
304 m = malloc(sizeof(struct mount));
305 memset(m, 0, sizeof(struct mount));
306 m->type = TYPE_SWAP;
307 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
308 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
309 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
310 if (tb[SWAP_PRIO])
311 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
312 if (m->prio)
313 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
314
315 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
316 /* store complete swap path */
317 if (tb[SWAP_DEVICE])
318 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
319
320 if (m->uuid)
321 vlist_add(&mounts, &m->node, m->uuid);
322 else if (m->label)
323 vlist_add(&mounts, &m->node, m->label);
324 else if (m->device)
325 vlist_add(&mounts, &m->node, m->device);
326 }
327
328 return 0;
329 }
330
331 static int global_add(struct uci_section *s)
332 {
333 struct blob_attr *tb[__CFG_MAX] = { 0 };
334
335 blob_buf_init(&b, 0);
336 uci_to_blob(&b, s, &config_attr_list);
337 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
338
339 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
340 anon_mount = 1;
341 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
342 anon_swap = 1;
343
344 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
345 auto_mount = 1;
346 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
347 auto_swap = 1;
348
349 if (tb[CFG_DELAY_ROOT])
350 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
351
352 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
353 check_fs = 1;
354
355 return 0;
356 }
357
358 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
359 {
360 struct mount *m;
361
362 vlist_for_each_element(&mounts, m, node) {
363 if (m->type != TYPE_SWAP)
364 continue;
365 if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
366 return m;
367 if (label && m->label && !strcmp(m->label, label))
368 return m;
369 if (device && m->device && !strcmp(m->device, device))
370 return m;
371 }
372
373 return NULL;
374 }
375
376 static struct mount* find_block(const char *uuid, const char *label, const char *device,
377 const char *target)
378 {
379 struct mount *m;
380
381 vlist_for_each_element(&mounts, m, node) {
382 if (m->type != TYPE_MOUNT)
383 continue;
384 if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
385 return m;
386 if (m->label && label && !strcmp(m->label, label))
387 return m;
388 if (m->target && target && !strcmp(m->target, target))
389 return m;
390 if (m->device && device && !strcmp(m->device, device))
391 return m;
392 }
393
394 return NULL;
395 }
396
397 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
398 struct vlist_node *node_old)
399 {
400 }
401
402 static struct uci_package * config_try_load(struct uci_context *ctx, char *path)
403 {
404 char *file = basename(path);
405 char *dir = dirname(path);
406 char *err;
407 struct uci_package *pkg;
408
409 uci_set_confdir(ctx, dir);
410 ULOG_INFO("attempting to load %s/%s\n", dir, file);
411
412 if (uci_load(ctx, file, &pkg)) {
413 uci_get_errorstr(ctx, &err, file);
414 ULOG_ERR("unable to load configuration (%s)\n", err);
415
416 free(err);
417 return NULL;
418 }
419
420 return pkg;
421 }
422
423 static int config_load(char *cfg)
424 {
425 struct uci_context *ctx = uci_alloc_context();
426 struct uci_package *pkg = NULL;
427 struct uci_element *e;
428 char path[64];
429
430 vlist_init(&mounts, avl_strcmp, mounts_update);
431
432 if (cfg) {
433 snprintf(path, sizeof(path), "%s/upper/etc/config/fstab", cfg);
434 pkg = config_try_load(ctx, path);
435
436 if (!pkg) {
437 snprintf(path, sizeof(path), "%s/etc/config/fstab", cfg);
438 pkg = config_try_load(ctx, path);
439 }
440 }
441
442 if (!pkg) {
443 snprintf(path, sizeof(path), "/etc/config/fstab");
444 pkg = config_try_load(ctx, path);
445 }
446
447 if (!pkg) {
448 ULOG_ERR("no usable configuration\n");
449 return -1;
450 }
451
452 vlist_update(&mounts);
453 uci_foreach_element(&pkg->sections, e) {
454 struct uci_section *s = uci_to_section(e);
455
456 if (!strcmp(s->type, "mount"))
457 mount_add(s);
458 if (!strcmp(s->type, "swap"))
459 swap_add(s);
460 if (!strcmp(s->type, "global"))
461 global_add(s);
462 }
463 vlist_flush(&mounts);
464
465 return 0;
466 }
467
468 static struct probe_info* _probe_path(char *path)
469 {
470 struct probe_info *pr;
471 char tmppath[64];
472
473 /* skip ubi device if ubiblock device is present */
474 if (path[5] == 'u' && path[6] == 'b' && path[7] == 'i' &&
475 path[8] >= '0' && path[8] <= '9' ) {
476 snprintf(tmppath, sizeof(tmppath), "/dev/ubiblock%s", path + 8);
477 list_for_each_entry(pr, &devices, list)
478 if (!strcasecmp(pr->dev, tmppath))
479 return NULL;
480 }
481
482 return probe_path(path);
483 }
484
485 static int _cache_load(const char *path)
486 {
487 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
488 int j;
489 glob_t gl;
490
491 if (glob(path, gl_flags, NULL, &gl) < 0)
492 return -1;
493
494 for (j = 0; j < gl.gl_pathc; j++) {
495 struct probe_info *pr = _probe_path(gl.gl_pathv[j]);
496 if (pr)
497 list_add_tail(&pr->list, &devices);
498 }
499
500 globfree(&gl);
501
502 return 0;
503 }
504
505 static void cache_load(int mtd)
506 {
507 if (mtd) {
508 _cache_load("/dev/mtdblock*");
509 _cache_load("/dev/ubiblock*");
510 _cache_load("/dev/ubi[0-9]*");
511 }
512 _cache_load("/dev/loop*");
513 _cache_load("/dev/mmcblk*");
514 _cache_load("/dev/sd*");
515 _cache_load("/dev/hd*");
516 _cache_load("/dev/md*");
517 _cache_load("/dev/vd*");
518 _cache_load("/dev/mapper/*");
519 }
520
521
522 static int print_block_uci(struct probe_info *pr)
523 {
524 if (!strcmp(pr->type, "swap")) {
525 printf("config 'swap'\n");
526 } else {
527 printf("config 'mount'\n");
528 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
529 }
530 if (pr->uuid)
531 printf("\toption\tuuid\t'%s'\n", pr->uuid);
532 else
533 printf("\toption\tdevice\t'%s'\n", pr->dev);
534 printf("\toption\tenabled\t'0'\n\n");
535
536 return 0;
537 }
538
539 static struct probe_info* find_block_info(char *uuid, char *label, char *path)
540 {
541 struct probe_info *pr = NULL;
542
543 if (uuid)
544 list_for_each_entry(pr, &devices, list)
545 if (pr->uuid && !strcasecmp(pr->uuid, uuid))
546 return pr;
547
548 if (label)
549 list_for_each_entry(pr, &devices, list)
550 if (pr->label && !strcmp(pr->label, label))
551 return pr;
552
553 if (path)
554 list_for_each_entry(pr, &devices, list)
555 if (pr->dev && !strcmp(basename(pr->dev), basename(path)))
556 return pr;
557
558 return NULL;
559 }
560
561 static char* find_mount_point(char *block)
562 {
563 FILE *fp = fopen("/proc/self/mountinfo", "r");
564 static char line[256];
565 int len = strlen(block);
566 char *point = NULL, *pos, *tmp, *cpoint, *devname;
567 struct stat s;
568 int rstat;
569 unsigned int minor, major;
570
571 if (!fp)
572 return NULL;
573
574 rstat = stat(block, &s);
575
576 while (fgets(line, sizeof(line), fp)) {
577 pos = strchr(line, ' ');
578 if (!pos)
579 continue;
580
581 pos = strchr(pos + 1, ' ');
582 if (!pos)
583 continue;
584
585 tmp = ++pos;
586 pos = strchr(pos, ':');
587 if (!pos)
588 continue;
589
590 *pos = '\0';
591 major = atoi(tmp);
592 tmp = ++pos;
593 pos = strchr(pos, ' ');
594 if (!pos)
595 continue;
596
597 *pos = '\0';
598 minor = atoi(tmp);
599 pos = strchr(pos + 1, ' ');
600 if (!pos)
601 continue;
602 tmp = ++pos;
603
604 pos = strchr(pos, ' ');
605 if (!pos)
606 continue;
607 *pos = '\0';
608 cpoint = tmp;
609
610 pos = strchr(pos + 1, ' ');
611 if (!pos)
612 continue;
613
614 pos = strchr(pos + 1, ' ');
615 if (!pos)
616 continue;
617
618 pos = strchr(pos + 1, ' ');
619 if (!pos)
620 continue;
621
622 tmp = ++pos;
623 pos = strchr(pos, ' ');
624 if (!pos)
625 continue;
626
627 *pos = '\0';
628 devname = tmp;
629 if (!strncmp(block, devname, len)) {
630 point = strdup(cpoint);
631 break;
632 }
633
634 if (rstat)
635 continue;
636
637 if (!S_ISBLK(s.st_mode))
638 continue;
639
640 if (major == major(s.st_rdev) &&
641 minor == minor(s.st_rdev)) {
642 point = strdup(cpoint);
643 break;
644 }
645 }
646
647 fclose(fp);
648
649 return point;
650 }
651
652 static int print_block_info(struct probe_info *pr)
653 {
654 static char *mp;
655
656 mp = find_mount_point(pr->dev);
657 printf("%s:", pr->dev);
658 if (pr->uuid)
659 printf(" UUID=\"%s\"", pr->uuid);
660
661 if (pr->label)
662 printf(" LABEL=\"%s\"", pr->label);
663
664 if (pr->version)
665 printf(" VERSION=\"%s\"", pr->version);
666
667 if (mp) {
668 printf(" MOUNT=\"%s\"", mp);
669 free(mp);
670 }
671
672 printf(" TYPE=\"%s\"\n", pr->type);
673
674 return 0;
675 }
676
677 static void mkdir_p(char *dir)
678 {
679 char *l = strrchr(dir, '/');
680
681 if (l) {
682 *l = '\0';
683 mkdir_p(dir);
684 *l = '/';
685 mkdir(dir, 0755);
686 }
687 }
688
689 static void check_filesystem(struct probe_info *pr)
690 {
691 pid_t pid;
692 struct stat statbuf;
693 const char *e2fsck = "/usr/sbin/e2fsck";
694 const char *f2fsck = "/usr/sbin/fsck.f2fs";
695 const char *dosfsck = "/usr/sbin/dosfsck";
696 const char *ckfs;
697
698 /* UBIFS does not need stuff like fsck */
699 if (!strncmp(pr->type, "ubifs", 5))
700 return;
701
702 if (!strncmp(pr->type, "vfat", 4)) {
703 ckfs = dosfsck;
704 } else if (!strncmp(pr->type, "f2fs", 4)) {
705 ckfs = f2fsck;
706 } else if (!strncmp(pr->type, "ext", 3)) {
707 ckfs = e2fsck;
708 } else {
709 ULOG_ERR("check_filesystem: %s is not supported\n", pr->type);
710 return;
711 }
712
713 if (stat(ckfs, &statbuf) < 0) {
714 ULOG_ERR("check_filesystem: %s not found\n", ckfs);
715 return;
716 }
717
718 pid = fork();
719 if (!pid) {
720 if(!strncmp(pr->type, "f2fs", 4)) {
721 execl(ckfs, ckfs, "-f", pr->dev, NULL);
722 exit(-1);
723 } else {
724 execl(ckfs, ckfs, "-p", pr->dev, NULL);
725 exit(-1);
726 }
727 } else if (pid > 0) {
728 int status;
729
730 waitpid(pid, &status, 0);
731 if (WEXITSTATUS(status))
732 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs, WEXITSTATUS(status));
733 }
734 }
735
736 static void handle_swapfiles(bool on)
737 {
738 struct stat s;
739 struct mount *m;
740 struct probe_info *pr;
741
742 vlist_for_each_element(&mounts, m, node)
743 {
744 if (m->type != TYPE_SWAP || !m->target)
745 continue;
746
747 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
748 continue;
749
750 pr = _probe_path(m->target);
751
752 if (!pr)
753 continue;
754
755 if (!strcmp(pr->type, "swap")) {
756 if (on)
757 swapon(pr->dev, m->prio);
758 else
759 swapoff(pr->dev);
760 }
761
762 free(pr);
763 }
764 }
765
766 static void to_devnull(int fd)
767 {
768 int devnull = open("/dev/null", fd ? O_WRONLY : O_RDONLY);
769
770 if (devnull >= 0)
771 dup2(devnull, fd);
772
773 if (devnull > STDERR_FILENO)
774 close(devnull);
775 }
776
777 static int exec_mount(const char *source, const char *target,
778 const char *fstype, const char *options)
779 {
780 pid_t pid;
781 struct stat s;
782 FILE *mount_fd;
783 int err, status, pfds[2];
784 char errmsg[128], cmd[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
785
786 snprintf(cmd, sizeof(cmd), "/sbin/mount.%s", fstype);
787
788 if (stat(cmd, &s) < 0 || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR)) {
789 ULOG_ERR("No \"mount.%s\" utility available\n", fstype);
790 return -1;
791 }
792
793 if (pipe(pfds) < 0)
794 return -1;
795
796 fcntl(pfds[0], F_SETFD, fcntl(pfds[0], F_GETFD) | FD_CLOEXEC);
797 fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
798
799 pid = vfork();
800
801 switch (pid) {
802 case -1:
803 close(pfds[0]);
804 close(pfds[1]);
805
806 return -1;
807
808 case 0:
809 to_devnull(STDIN_FILENO);
810 to_devnull(STDOUT_FILENO);
811
812 dup2(pfds[1], STDERR_FILENO);
813 close(pfds[0]);
814 close(pfds[1]);
815
816 if (options && *options)
817 execl(cmd, cmd, "-o", options, source, target, NULL);
818 else
819 execl(cmd, cmd, source, target, NULL);
820
821 return -1;
822
823 default:
824 close(pfds[1]);
825
826 mount_fd = fdopen(pfds[0], "r");
827
828 while (fgets(errmsg, sizeof(errmsg), mount_fd))
829 ULOG_ERR("mount.%s: %s", fstype, errmsg);
830
831 fclose(mount_fd);
832
833 err = waitpid(pid, &status, 0);
834
835 if (err != -1) {
836 if (status != 0) {
837 ULOG_ERR("mount.%s: failed with status %d\n", fstype, status);
838 errno = EINVAL;
839 err = -1;
840 } else {
841 errno = 0;
842 err = 0;
843 }
844 }
845
846 break;
847 }
848
849 return err;
850 }
851
852 static int handle_mount(const char *source, const char *target,
853 const char *fstype, struct mount *m)
854 {
855 int i, err;
856 size_t mount_opts_len;
857 char *mount_opts = NULL, *ptr;
858
859 err = mount(source, target, fstype, m ? m->flags : 0,
860 (m && m->options) ? m->options : "");
861
862 /* Requested file system type is not available in kernel,
863 attempt to call mount helper. */
864 if (err == -1 && errno == ENODEV) {
865 if (m) {
866 /* Convert mount flags back into string representation,
867 first calculate needed length of string buffer... */
868 mount_opts_len = 1 + (m->options ? strlen(m->options) : 0);
869
870 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
871 if ((mount_flags[i].flag > 0) &&
872 (mount_flags[i].flag < INT_MAX) &&
873 (m->flags & (uint32_t)mount_flags[i].flag))
874 mount_opts_len += strlen(mount_flags[i].name) + 1;
875
876 /* ... then now allocate and fill it ... */
877 ptr = mount_opts = calloc(1, mount_opts_len);
878
879 if (!ptr) {
880 errno = ENOMEM;
881 return -1;
882 }
883
884 if (m->options)
885 ptr += sprintf(ptr, "%s,", m->options);
886
887 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
888 if ((mount_flags[i].flag > 0) &&
889 (mount_flags[i].flag < INT_MAX) &&
890 (m->flags & (uint32_t)mount_flags[i].flag))
891 ptr += sprintf(ptr, "%s,", mount_flags[i].name);
892
893 mount_opts[mount_opts_len - 1] = 0;
894 }
895
896 /* ... and now finally invoke the external mount program */
897 err = exec_mount(source, target, fstype, mount_opts);
898 }
899
900 return err;
901 }
902
903 static int mount_device(struct probe_info *pr, int hotplug)
904 {
905 struct mount *m;
906 char *device;
907 char *mp;
908
909 if (!pr)
910 return -1;
911
912 device = basename(pr->dev);
913
914 if (!strcmp(pr->type, "swap")) {
915 if (hotplug && !auto_swap)
916 return -1;
917 m = find_swap(pr->uuid, pr->label, device);
918 if (m || anon_swap)
919 swapon(pr->dev, (m) ? (m->prio) : (0));
920
921 return 0;
922 }
923
924 if (hotplug && !auto_mount)
925 return -1;
926
927 mp = find_mount_point(pr->dev);
928 if (mp) {
929 ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
930 free(mp);
931 return -1;
932 }
933
934 m = find_block(pr->uuid, pr->label, device, NULL);
935 if (m && m->extroot)
936 return -1;
937
938 if (m) {
939 char *target = m->target;
940 char _target[32];
941 int err = 0;
942
943 if (!target) {
944 snprintf(_target, sizeof(_target), "/mnt/%s", device);
945 target = _target;
946 }
947 mkdir_p(target);
948
949 if (check_fs)
950 check_filesystem(pr);
951
952 err = handle_mount(pr->dev, target, pr->type, m);
953 if (err)
954 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
955 pr->dev, pr->type, target, errno, strerror(errno));
956 else
957 handle_swapfiles(true);
958 return err;
959 }
960
961 if (anon_mount) {
962 char target[32];
963 int err = 0;
964
965 snprintf(target, sizeof(target), "/mnt/%s", device);
966 mkdir_p(target);
967
968 if (check_fs)
969 check_filesystem(pr);
970
971 err = handle_mount(pr->dev, target, pr->type, NULL);
972 if (err)
973 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
974 pr->dev, pr->type, target, errno, strerror(errno));
975 else
976 handle_swapfiles(true);
977 return err;
978 }
979
980 return 0;
981 }
982
983 static int umount_device(struct probe_info *pr)
984 {
985 struct mount *m;
986 char *device = basename(pr->dev);
987 char *mp;
988 int err;
989
990 if (!pr)
991 return -1;
992
993 if (!strcmp(pr->type, "swap"))
994 return -1;
995
996 mp = find_mount_point(pr->dev);
997 if (!mp)
998 return -1;
999
1000 m = find_block(pr->uuid, pr->label, device, NULL);
1001 if (m && m->extroot)
1002 return -1;
1003
1004 err = umount2(mp, MNT_DETACH);
1005 if (err)
1006 ULOG_ERR("unmounting %s (%s) failed (%d) - %s\n",
1007 pr->dev, mp, errno, strerror(errno));
1008 else
1009 ULOG_INFO("unmounted %s (%s)\n",
1010 pr->dev, mp);
1011
1012 free(mp);
1013 return err;
1014 }
1015
1016 static int main_hotplug(int argc, char **argv)
1017 {
1018 char path[32];
1019 char *action, *device, *mount_point;
1020
1021 action = getenv("ACTION");
1022 device = getenv("DEVNAME");
1023
1024 if (!action || !device)
1025 return -1;
1026 snprintf(path, sizeof(path), "/dev/%s", device);
1027
1028 if (!strcmp(action, "remove")) {
1029 int err = 0;
1030 mount_point = find_mount_point(path);
1031 if (mount_point)
1032 err = umount2(mount_point, MNT_DETACH);
1033
1034 if (err)
1035 ULOG_ERR("umount of %s failed (%d) - %s\n",
1036 mount_point, errno, strerror(errno));
1037
1038 free(mount_point);
1039 return 0;
1040 } else if (strcmp(action, "add")) {
1041 ULOG_ERR("Unkown action %s\n", action);
1042
1043 return -1;
1044 }
1045
1046 if (config_load(NULL))
1047 return -1;
1048 cache_load(0);
1049
1050 return mount_device(find_block_info(NULL, NULL, path), 1);
1051 }
1052
1053 static int find_block_mtd(char *name, char *part, int plen)
1054 {
1055 FILE *fp = fopen("/proc/mtd", "r");
1056 static char line[256];
1057 char *index = NULL;
1058
1059 if(!fp)
1060 return -1;
1061
1062 while (!index && fgets(line, sizeof(line), fp)) {
1063 if (strstr(line, name)) {
1064 char *eol = strstr(line, ":");
1065
1066 if (!eol)
1067 continue;
1068
1069 *eol = '\0';
1070 index = &line[3];
1071 }
1072 }
1073
1074 fclose(fp);
1075
1076 if (!index)
1077 return -1;
1078
1079 snprintf(part, plen, "/dev/mtdblock%s", index);
1080
1081 return 0;
1082 }
1083
1084 #ifdef UBIFS_EXTROOT
1085 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
1086 {
1087 int dev = 0;
1088
1089 while (ubi_dev_present(libubi, dev))
1090 {
1091 struct ubi_dev_info dev_info;
1092 struct ubi_vol_info vol_info;
1093
1094 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
1095 continue;
1096 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
1097 continue;
1098
1099 *dev_num = dev_info.dev_num;
1100 *vol_id = vol_info.vol_id;
1101
1102 return 0;
1103 }
1104
1105 return -1;
1106 }
1107
1108 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
1109 {
1110 int dev_num;
1111 int vol_id;
1112 int err = -1;
1113
1114 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1115 if (!err)
1116 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
1117
1118 return err;
1119 }
1120
1121 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
1122 {
1123 int dev_num;
1124 int vol_id;
1125 int err = -1;
1126
1127 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1128 if (!err)
1129 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
1130
1131 return err;
1132 }
1133
1134 #else
1135
1136 static int find_root_dev(char *buf, int len)
1137 {
1138 DIR *d;
1139 dev_t root;
1140 struct stat s;
1141 struct dirent *e;
1142
1143 if (stat("/", &s))
1144 return -1;
1145
1146 if (!(d = opendir("/dev")))
1147 return -1;
1148
1149 root = s.st_dev;
1150
1151 while ((e = readdir(d)) != NULL) {
1152 snprintf(buf, len, "/dev/%s", e->d_name);
1153
1154 if (stat(buf, &s) || s.st_rdev != root)
1155 continue;
1156
1157 closedir(d);
1158 return 0;
1159 }
1160
1161 closedir(d);
1162 return -1;
1163 }
1164
1165 #endif
1166
1167 static int test_fs_support(const char *name)
1168 {
1169 char line[128], *p;
1170 int rv = -1;
1171 FILE *f;
1172
1173 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
1174 while (fgets(line, sizeof(line), f)) {
1175 p = strtok(line, "\t\n");
1176
1177 if (p && !strcmp(p, "nodev"))
1178 p = strtok(NULL, "\t\n");
1179
1180 if (p && !strcmp(p, name)) {
1181 rv = 0;
1182 break;
1183 }
1184 }
1185 fclose(f);
1186 }
1187
1188 return rv;
1189 }
1190
1191 static int check_extroot(char *path)
1192 {
1193 struct probe_info *pr = NULL;
1194 char devpath[32];
1195
1196 #ifdef UBIFS_EXTROOT
1197 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1198 int err = -1;
1199 libubi_t libubi;
1200
1201 libubi = libubi_open();
1202 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
1203 libubi_close(libubi);
1204 if (err)
1205 return -1;
1206 }
1207 #else
1208 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1209 if (find_root_dev(devpath, sizeof(devpath))) {
1210 ULOG_ERR("extroot: unable to determine root device\n");
1211 return -1;
1212 }
1213 }
1214 #endif
1215
1216 list_for_each_entry(pr, &devices, list) {
1217 if (!strcmp(pr->dev, devpath)) {
1218 struct stat s;
1219 FILE *fp = NULL;
1220 char tag[64];
1221 char uuid[64] = { 0 };
1222
1223 snprintf(tag, sizeof(tag), "%s/etc", path);
1224 if (stat(tag, &s))
1225 mkdir_p(tag);
1226
1227 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1228 if (stat(tag, &s)) {
1229 fp = fopen(tag, "w+");
1230 if (!fp) {
1231 ULOG_ERR("extroot: failed to write UUID to %s: %d (%s)\n",
1232 tag, errno, strerror(errno));
1233 /* return 0 to continue boot regardless of error */
1234 return 0;
1235 }
1236 fputs(pr->uuid, fp);
1237 fclose(fp);
1238 return 0;
1239 }
1240
1241 fp = fopen(tag, "r");
1242 if (!fp) {
1243 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1244 tag, errno, strerror(errno));
1245 return -1;
1246 }
1247
1248 if (!fgets(uuid, sizeof(uuid), fp))
1249 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1250 tag, errno, strerror(errno));
1251 fclose(fp);
1252
1253 if (*uuid && !strcasecmp(uuid, pr->uuid))
1254 return 0;
1255
1256 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1257 pr->uuid, basename(path), uuid);
1258 return -1;
1259 }
1260 }
1261
1262 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath);
1263 return -1;
1264 }
1265
1266 /*
1267 * Read info about extroot from UCI (using prefix) and mount it.
1268 */
1269 static int mount_extroot(char *cfg)
1270 {
1271 char overlay[] = "/tmp/extroot/overlay";
1272 char mnt[] = "/tmp/extroot/mnt";
1273 char *path = mnt;
1274 struct probe_info *pr;
1275 struct mount *m;
1276 int err = -1;
1277
1278 /* Load @cfg/etc/config/fstab */
1279 if (config_load(cfg))
1280 return -2;
1281
1282 /* See if there is extroot-specific mount config */
1283 m = find_block(NULL, NULL, NULL, "/");
1284 if (!m)
1285 m = find_block(NULL, NULL, NULL, "/overlay");
1286
1287 if (!m || !m->extroot)
1288 {
1289 ULOG_INFO("extroot: not configured\n");
1290 return -1;
1291 }
1292
1293 /* Find block device pointed by the mount config */
1294 pr = find_block_info(m->uuid, m->label, m->device);
1295
1296 if (!pr && delay_root){
1297 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1298 sleep(delay_root);
1299 make_devs();
1300 cache_load(0);
1301 pr = find_block_info(m->uuid, m->label, m->device);
1302 }
1303 if (pr) {
1304 if (strncmp(pr->type, "ext", 3) &&
1305 strncmp(pr->type, "f2fs", 4) &&
1306 strncmp(pr->type, "ubifs", 5)) {
1307 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs or ubifs\n", pr->type);
1308 return -1;
1309 }
1310
1311 if (test_fs_support(pr->type)) {
1312 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr->type);
1313 return -1;
1314 }
1315
1316 if (m->overlay)
1317 path = overlay;
1318 mkdir_p(path);
1319
1320 if (check_fs)
1321 check_filesystem(pr);
1322
1323 err = mount(pr->dev, path, pr->type, m->flags,
1324 (m->options) ? (m->options) : (""));
1325
1326 if (err) {
1327 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1328 pr->dev, pr->type, path, errno, strerror(errno));
1329 } else if (m->overlay) {
1330 err = check_extroot(path);
1331 if (err)
1332 umount(path);
1333 }
1334 } else {
1335 ULOG_ERR("extroot: cannot find device %s%s\n",
1336 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1337 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1338 }
1339
1340 return err;
1341 }
1342
1343 static int main_extroot(int argc, char **argv)
1344 {
1345 struct probe_info *pr;
1346 char blkdev_path[32] = { 0 };
1347 int err = -1;
1348 #ifdef UBIFS_EXTROOT
1349 libubi_t libubi;
1350 #endif
1351
1352 if (!getenv("PREINIT"))
1353 return -1;
1354
1355 if (argc != 2) {
1356 ULOG_ERR("Usage: block extroot\n");
1357 return -1;
1358 }
1359
1360 make_devs();
1361 cache_load(1);
1362
1363 /* enable LOG_INFO messages */
1364 ulog_threshold(LOG_INFO);
1365
1366 /*
1367 * Look for "rootfs_data". We will want to mount it and check for
1368 * extroot configuration.
1369 */
1370
1371 /* Start with looking for MTD partition */
1372 find_block_mtd("\"rootfs_data\"", blkdev_path, sizeof(blkdev_path));
1373 if (blkdev_path[0]) {
1374 pr = find_block_info(NULL, NULL, blkdev_path);
1375 if (pr && !strcmp(pr->type, "jffs2")) {
1376 char cfg[] = "/tmp/jffs_cfg";
1377
1378 /*
1379 * Mount MTD part and try extroot (using
1380 * /etc/config/fstab from that partition)
1381 */
1382 mkdir_p(cfg);
1383 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1384 err = mount_extroot(cfg);
1385 umount2(cfg, MNT_DETACH);
1386 }
1387 if (err < 0)
1388 rmdir("/tmp/overlay");
1389 rmdir(cfg);
1390 return err;
1391 }
1392 }
1393
1394 #ifdef UBIFS_EXTROOT
1395 /* ... but it also could be an UBI volume */
1396 memset(blkdev_path, 0, sizeof(blkdev_path));
1397 libubi = libubi_open();
1398 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1399 libubi_close(libubi);
1400 if (blkdev_path[0]) {
1401 char cfg[] = "/tmp/ubifs_cfg";
1402
1403 /* Mount volume and try extroot (using fstab from that vol) */
1404 mkdir_p(cfg);
1405 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1406 err = mount_extroot(cfg);
1407 umount2(cfg, MNT_DETACH);
1408 }
1409 if (err < 0)
1410 rmdir("/tmp/overlay");
1411 rmdir(cfg);
1412 return err;
1413 }
1414 #endif
1415
1416 return mount_extroot(NULL);
1417 }
1418
1419 static int main_mount(int argc, char **argv)
1420 {
1421 struct probe_info *pr;
1422
1423 if (config_load(NULL))
1424 return -1;
1425
1426 cache_load(1);
1427 list_for_each_entry(pr, &devices, list)
1428 mount_device(pr, 0);
1429
1430 handle_swapfiles(true);
1431
1432 return 0;
1433 }
1434
1435 static int main_umount(int argc, char **argv)
1436 {
1437 struct probe_info *pr;
1438
1439 if (config_load(NULL))
1440 return -1;
1441
1442 handle_swapfiles(false);
1443
1444 cache_load(0);
1445 list_for_each_entry(pr, &devices, list)
1446 umount_device(pr);
1447
1448 return 0;
1449 }
1450
1451 static int main_detect(int argc, char **argv)
1452 {
1453 struct probe_info *pr;
1454
1455 cache_load(0);
1456 printf("config 'global'\n");
1457 printf("\toption\tanon_swap\t'0'\n");
1458 printf("\toption\tanon_mount\t'0'\n");
1459 printf("\toption\tauto_swap\t'1'\n");
1460 printf("\toption\tauto_mount\t'1'\n");
1461 printf("\toption\tdelay_root\t'5'\n");
1462 printf("\toption\tcheck_fs\t'0'\n\n");
1463 list_for_each_entry(pr, &devices, list)
1464 print_block_uci(pr);
1465
1466 return 0;
1467 }
1468
1469 static int main_info(int argc, char **argv)
1470 {
1471 int i;
1472 struct probe_info *pr;
1473
1474 cache_load(1);
1475 if (argc == 2) {
1476 list_for_each_entry(pr, &devices, list)
1477 print_block_info(pr);
1478
1479 return 0;
1480 };
1481
1482 for (i = 2; i < argc; i++) {
1483 struct stat s;
1484
1485 if (stat(argv[i], &s)) {
1486 ULOG_ERR("failed to stat %s\n", argv[i]);
1487 continue;
1488 }
1489 if (!S_ISBLK(s.st_mode) && !(S_ISCHR(s.st_mode) && major(s.st_rdev) == 250)) {
1490 ULOG_ERR("%s is not a block device\n", argv[i]);
1491 continue;
1492 }
1493 pr = find_block_info(NULL, NULL, argv[i]);
1494 if (pr)
1495 print_block_info(pr);
1496 }
1497
1498 return 0;
1499 }
1500
1501 static int swapon_usage(void)
1502 {
1503 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1504 "\tStart swapping on [DEVICE]\n"
1505 " -a\tStart swapping on all swap devices\n"
1506 " -p pri\tSet priority of swap device\n"
1507 " -s\tShow summary\n");
1508 return -1;
1509 }
1510
1511 static int main_swapon(int argc, char **argv)
1512 {
1513 int ch;
1514 FILE *fp;
1515 char *lineptr;
1516 size_t s;
1517 struct probe_info *pr;
1518 int flags = 0;
1519 int pri;
1520 struct stat st;
1521 int err;
1522
1523 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1524 switch(ch) {
1525 case 's':
1526 fp = fopen("/proc/swaps", "r");
1527 lineptr = NULL;
1528
1529 if (!fp) {
1530 ULOG_ERR("failed to open /proc/swaps\n");
1531 return -1;
1532 }
1533 while (getline(&lineptr, &s, fp) > 0)
1534 printf("%s", lineptr);
1535 if (lineptr)
1536 free(lineptr);
1537 fclose(fp);
1538 return 0;
1539 case 'a':
1540 cache_load(0);
1541 list_for_each_entry(pr, &devices, list) {
1542 if (strcmp(pr->type, "swap"))
1543 continue;
1544 if (swapon(pr->dev, 0))
1545 ULOG_ERR("failed to swapon %s\n", pr->dev);
1546 }
1547 return 0;
1548 case 'p':
1549 pri = atoi(optarg);
1550 if (pri >= 0)
1551 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1552 break;
1553 default:
1554 return swapon_usage();
1555 }
1556
1557 }
1558
1559 if (optind != (argc - 1))
1560 return swapon_usage();
1561
1562 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1563 ULOG_ERR("%s is not a block device or file\n", argv[optind]);
1564 return -1;
1565 }
1566 err = swapon(argv[optind], flags);
1567 if (err) {
1568 ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
1569 return err;
1570 }
1571
1572 return 0;
1573 }
1574
1575 static int main_swapoff(int argc, char **argv)
1576 {
1577 if (argc != 2) {
1578 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1579 "\tStop swapping on DEVICE\n"
1580 " -a\tStop swapping on all swap devices\n");
1581 return -1;
1582 }
1583
1584 if (!strcmp(argv[1], "-a")) {
1585 FILE *fp = fopen("/proc/swaps", "r");
1586 char line[256];
1587
1588 if (!fp) {
1589 ULOG_ERR("failed to open /proc/swaps\n");
1590 return -1;
1591 }
1592 if (fgets(line, sizeof(line), fp))
1593 while (fgets(line, sizeof(line), fp)) {
1594 char *end = strchr(line, ' ');
1595 int err;
1596
1597 if (!end)
1598 continue;
1599 *end = '\0';
1600 err = swapoff(line);
1601 if (err)
1602 ULOG_ERR("failed to swapoff %s (%d)\n", line, err);
1603 }
1604 fclose(fp);
1605 } else {
1606 struct stat s;
1607 int err;
1608
1609 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1610 ULOG_ERR("%s is not a block device or file\n", argv[1]);
1611 return -1;
1612 }
1613 err = swapoff(argv[1]);
1614 if (err) {
1615 ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
1616 return err;
1617 }
1618 }
1619
1620 return 0;
1621 }
1622
1623 int main(int argc, char **argv)
1624 {
1625 char *base = basename(*argv);
1626
1627 umask(0);
1628
1629 ulog_open(-1, -1, "block");
1630 ulog_threshold(LOG_NOTICE);
1631
1632 if (!strcmp(base, "swapon"))
1633 return main_swapon(argc, argv);
1634
1635 if (!strcmp(base, "swapoff"))
1636 return main_swapoff(argc, argv);
1637
1638 if ((argc > 1) && !strcmp(base, "block")) {
1639 if (!strcmp(argv[1], "info"))
1640 return main_info(argc, argv);
1641
1642 if (!strcmp(argv[1], "detect"))
1643 return main_detect(argc, argv);
1644
1645 if (!strcmp(argv[1], "hotplug"))
1646 return main_hotplug(argc, argv);
1647
1648 if (!strcmp(argv[1], "extroot"))
1649 return main_extroot(argc, argv);
1650
1651 if (!strcmp(argv[1], "mount"))
1652 return main_mount(argc, argv);
1653
1654 if (!strcmp(argv[1], "umount"))
1655 return main_umount(argc, argv);
1656
1657 if (!strcmp(argv[1], "remount")) {
1658 int ret = main_umount(argc, argv);
1659
1660 if (!ret)
1661 ret = main_mount(argc, argv);
1662 return ret;
1663 }
1664 }
1665
1666 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");
1667
1668 return -1;
1669 }