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