add pivot support, anon swap, device name support, hotplug
[project/ubox.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 #include <stdio.h>
16 #include <unistd.h>
17 #include <syslog.h>
18 #include <libgen.h>
19 #include <glob.h>
20
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/swap.h>
24 #include <sys/mount.h>
25
26 #include <uci.h>
27 #include <uci_blob.h>
28
29 #include <libubox/list.h>
30 #include <libubox/vlist.h>
31 #include <libubox/blobmsg_json.h>
32 #include <libubox/avl-cmp.h>
33
34 #include "libblkid-tiny/libblkid-tiny.h"
35
36 enum {
37         TYPE_MOUNT,
38         TYPE_SWAP,
39 };
40
41 struct mount {
42         struct vlist_node node;
43         int type;
44
45         char *target;
46         char *path;
47         char *options;
48         char *uuid;
49         char *label;
50         char *device;
51         int extroot;
52         int overlay;
53         int disabled_fsck;
54         unsigned int prio;
55 };
56
57 static struct vlist_tree mounts;
58 static struct blob_buf b;
59 static LIST_HEAD(devices);
60 static int anon_mount, anon_swap;
61
62 enum {
63         CFG_ANON_MOUNT,
64         CFG_ANON_SWAP,
65         __CFG_MAX
66 };
67
68 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
69         [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
70         [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
71 };
72
73 enum {
74         MOUNT_UUID,
75         MOUNT_LABEL,
76         MOUNT_ENABLE,
77         MOUNT_TARGET,
78         MOUNT_DEVICE,
79         MOUNT_OPTIONS,
80         __MOUNT_MAX
81 };
82
83 static const struct uci_blob_param_list config_attr_list = {
84         .n_params = __CFG_MAX,
85         .params = config_policy,
86 };
87
88 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
89         [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
90         [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
91         [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
92         [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
93         [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
94         [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
95 };
96
97 static const struct uci_blob_param_list mount_attr_list = {
98         .n_params = __MOUNT_MAX,
99         .params = mount_policy,
100 };
101
102 enum {
103         SWAP_ENABLE,
104         SWAP_UUID,
105         SWAP_DEVICE,
106         SWAP_PRIO,
107         __SWAP_MAX
108 };
109
110 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
111         [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
112         [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
113         [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
114         [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
115 };
116
117 static const struct uci_blob_param_list swap_attr_list = {
118         .n_params = __SWAP_MAX,
119         .params = swap_policy,
120 };
121
122 static char *blobmsg_get_strdup(struct blob_attr *attr)
123 {
124         if (!attr)
125                 return NULL;
126
127         return strdup(blobmsg_get_string(attr));
128 }
129
130 static int mount_add(struct uci_section *s)
131 {
132         struct blob_attr *tb[__MOUNT_MAX] = { 0 };
133         struct mount *m;
134
135         blob_buf_init(&b, 0);
136         uci_to_blob(&b, s, &mount_attr_list);
137         blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
138
139         if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
140                 return -1;
141
142         if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
143                 return -1;
144
145         m = malloc(sizeof(struct mount));
146         m->type = TYPE_MOUNT;
147         m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
148         m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
149         m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
150         m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
151         m->device = blobmsg_get_strdup(tb[MOUNT_DEVICE]);
152         m->overlay = m->extroot = 0;
153         if (m->target && !strcmp(m->target, "/"))
154                 m->extroot = 1;
155         if (m->target && !strcmp(m->target, "/overlay"))
156                 m->extroot = m->overlay = 1;
157
158         if (m->uuid)
159                 vlist_add(&mounts, &m->node, m->uuid);
160         else if (m->label)
161                 vlist_add(&mounts, &m->node, m->label);
162         else if (m->device)
163                 vlist_add(&mounts, &m->node, m->device);
164
165         return 0;
166 }
167
168 static int swap_add(struct uci_section *s)
169 {
170         struct blob_attr *tb[__SWAP_MAX] = { 0 };
171         struct mount *m;
172
173         blob_buf_init(&b, 0);
174         uci_to_blob(&b, s, &swap_attr_list);
175         blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
176
177         if (!tb[SWAP_UUID] && !tb[SWAP_DEVICE])
178                 return -1;
179
180         m = malloc(sizeof(struct mount));
181         memset(m, 0, sizeof(struct mount));
182         m->type = TYPE_SWAP;
183         m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
184         m->device = blobmsg_get_strdup(tb[SWAP_DEVICE]);
185         if (tb[SWAP_PRIO])
186                 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
187         if (m->prio)
188                 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
189
190         if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE]))
191                 vlist_add(&mounts, &m->node, (m->uuid) ? (m->uuid) : (m->device));
192
193         return 0;
194 }
195
196 static int global_add(struct uci_section *s)
197 {
198         struct blob_attr *tb[__CFG_MAX] = { 0 };
199
200         blob_buf_init(&b, 0);
201         uci_to_blob(&b, s, &config_attr_list);
202         blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
203
204         if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
205                 anon_mount = 1;
206         if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
207                 anon_swap = 1;
208
209         return 0;
210 }
211
212 static struct mount* find_swap(const char *uuid, const char *device)
213 {
214         struct mount *m;
215
216         vlist_for_each_element(&mounts, m, node) {
217                 if (m->type != TYPE_SWAP)
218                         continue;
219                 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
220                         return m;
221                 if (device && m->device && !strcmp(m->device, device))
222                         return m;
223         }
224
225         return NULL;
226 }
227
228 static struct mount* find_block(const char *uuid, const char *label, const char *device,
229                                 const char *target)
230 {
231         struct mount *m;
232
233         vlist_for_each_element(&mounts, m, node) {
234                 if (m->type != TYPE_MOUNT)
235                         continue;
236                 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
237                         return m;
238                 if (m->label && label && !strcmp(m->label, label))
239                         return m;
240                 if (m->target && target && !strcmp(m->target, target))
241                         return m;
242                 if (m->device && device && !strcmp(m->device, device))
243                         return m;
244         }
245
246         return NULL;
247 }
248
249 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
250                           struct vlist_node *node_old)
251 {
252 }
253
254 static int config_load(char *cfg)
255 {
256         struct uci_context *ctx;
257         struct uci_package *pkg;
258         struct uci_element *e;
259
260         vlist_init(&mounts, avl_strcmp, mounts_update);
261
262         ctx = uci_alloc_context();
263         if (cfg) {
264                 char path[32];
265                 snprintf(path, 32, "%s/etc/config", cfg);
266                 uci_set_confdir(ctx, path);
267         }
268
269         if (uci_load(ctx, "fstab", &pkg))
270                 return -1;
271
272         vlist_update(&mounts);
273         uci_foreach_element(&pkg->sections, e) {
274                 struct uci_section *s = uci_to_section(e);
275
276                 if (!strcmp(s->type, "mount"))
277                         mount_add(s);
278                 if (!strcmp(s->type, "swap"))
279                         swap_add(s);
280                 if (!strcmp(s->type, "global"))
281                         global_add(s);
282         }
283         vlist_flush(&mounts);
284
285         return 0;
286 }
287
288 static int _cache_load(const char *path)
289 {
290         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
291         int j;
292         glob_t gl;
293
294         if (glob(path, gl_flags, NULL, &gl) < 0)
295                 return -1;
296
297         for (j = 0; j < gl.gl_pathc; j++) {
298                 struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
299                 memset(pr, 0, sizeof(struct blkid_struct_probe));
300                 probe_block(gl.gl_pathv[j], pr);
301                 if (pr->err)
302                         free(pr);
303                 else
304                         list_add_tail(&pr->list, &devices);
305         }
306
307         globfree(&gl);
308
309         return 0;
310 }
311
312 static void cache_load(int mtd)
313 {
314         if (mtd)
315                 _cache_load("/dev/mtdblock*");
316         _cache_load("/dev/mmcblk*");
317         _cache_load("/dev/sd*");
318 }
319
320 static int print_block_info(struct blkid_struct_probe *pr)
321 {
322         printf("%s:", pr->dev);
323         if (pr->uuid[0])
324                 printf(" UUID=\"%s\"", pr->uuid);
325
326         if (pr->label[0])
327                 printf(" LABEL=\"%s\"", pr->label);
328
329         if (pr->name[0])
330                 printf(" NAME=\"%s\"", pr->name);
331
332         if (pr->version[0])
333                 printf(" VERSION=\"%s\"", pr->version);
334
335         printf(" TYPE=\"%s\"\n", pr->id->name);
336
337         return 0;
338 }
339
340 static int print_block_uci(struct blkid_struct_probe *pr)
341 {
342         if (!strcmp(pr->id->name, "swap")) {
343                 printf("config 'swap'\n");
344         } else {
345                 printf("config 'mount'\n");
346                 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
347         }
348         if (pr->uuid[0])
349                 printf("\toption\tuuid\t'%s'\n", pr->uuid);
350         else
351                 printf("\toption\tdevice\t'%s'\n", basename(pr->dev));
352         printf("\toption\tenabled\t'0'\n\n");
353
354         return 0;
355 }
356
357 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
358 {
359         struct blkid_struct_probe *pr = NULL;
360
361         if (uuid)
362                 list_for_each_entry(pr, &devices, list)
363                         if (!strcmp(pr->uuid, uuid))
364                                 return pr;
365
366         if (label)
367                 list_for_each_entry(pr, &devices, list)
368                         if (strcmp(pr->label, label))
369                                 return pr;
370
371         if (path)
372                 list_for_each_entry(pr, &devices, list)
373                         if (!strcmp(pr->dev, path))
374                                 return pr;
375
376         return NULL;
377 }
378
379 static char* find_mount_point(char *block)
380 {
381         FILE *fp = fopen("/proc/mounts", "r");
382         static char line[256];
383         int len = strlen(block);
384         char *point = NULL;
385
386         if(!fp)
387                 return NULL;
388
389         while (fgets(line, sizeof(line), fp)) {
390                 if (!strncmp(line, block, len)) {
391                         char *p = &line[len + 1];
392                         char *t = strstr(p, " ");
393
394                         if (!t)
395                                 return NULL;
396                         *t = '\0';
397                         point = p;
398                         break;
399                 }
400         }
401
402         fclose(fp);
403
404         return point;
405 }
406
407 static void mkdir_p(char *dir)
408 {
409         char *l = strrchr(dir, '/');
410
411         if (l) {
412                 *l = '\0';
413                 mkdir_p(dir);
414                 *l = '/';
415                 mkdir(dir, 0755);
416         }
417 }
418
419 static int main_hotplug(int argc, char **argv)
420 {
421         struct mount *m;
422         char path[256];
423         char *action, *device, *mount_point;
424         struct blkid_struct_probe *pr;
425
426         action = getenv("ACTION");
427         device = getenv("DEVNAME");
428
429         if (!action || !device)
430                 return -1;
431         snprintf(path, sizeof(path), "/dev/%s", device);
432
433         if (!strcmp(action, "remove")) {
434                 int err = 0;
435                 mount_point = find_mount_point(path);
436                 if (mount_point)
437                         err = umount2(mount_point, MNT_DETACH);
438
439                 if (err)
440                         fprintf(stderr, "unmount of %s failed (%d) - %s\n",
441                                         mount_point, err, strerror(err));
442
443                 return 0;
444         } else if (strcmp(action, "add")) {
445                 fprintf(stderr, "Unkown action %s\n", action);
446
447                 return -1;
448         }
449
450         if (config_load(NULL))
451                 return -1;
452         cache_load(0);
453
454         pr = find_block_info(NULL, NULL, path);
455         if (!pr) {
456                 fprintf(stderr, "failed to read blockinfo for %s\n", path);
457                 return -1;
458         }
459
460         if (!strcmp(pr->id->name, "swap")) {
461                 m = find_swap(pr->uuid, device);
462                 if (m || anon_swap) {
463                         if (!strcmp(action, "add"))
464                                 swapon(path, (m) ? (m->prio) : (0));
465                         else
466                                 swapoff(path);
467                 }
468                 return 0;
469         }
470
471         m = find_block(pr->uuid, pr->label, device, NULL);
472         if (m && !m->extroot) {
473                 char *target = m->target;
474                 char _target[] = "/mnt/mmcblk123";
475                 int err = 0;
476
477                 if (!target) {
478                         snprintf(_target, sizeof(_target), "/mnt/%s", device);
479                         target = _target;
480                 }
481                 mkdir_p(target);
482                 err = mount(path, target, pr->id->name, 0, (m->options) ? (m->options) : (""));
483                 if (err)
484                         fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
485                                         path, pr->id->name, target, err, strerror(err));
486                 return err;
487         }
488
489         if (anon_mount) {
490                 char target[] = "/mnt/mmcblk123";
491                 int err = 0;
492
493                 snprintf(target, sizeof(target), "/mnt/%s", device);
494                 mkdir_p(target);
495                 err = mount(path, target, pr->id->name, 0, "");
496                 if (err)
497                         fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
498                                         path, pr->id->name, target, err, strerror(err));
499                 return err;
500         }
501
502         return 0;
503 }
504
505 static int find_block_mtd(char *name, char *part, int plen)
506 {
507         FILE *fp = fopen("/proc/mtd", "r");
508         static char line[256];
509         char *index = NULL;
510
511         if(!fp)
512                 return -1;
513
514         while (!index && fgets(line, sizeof(line), fp)) {
515                 if (strstr(line, name)) {
516                         char *eol = strstr(line, ":");
517
518                         if (!eol)
519                                 continue;
520
521                         *eol = '\0';
522                         index = &line[3];
523                 }
524         }
525
526         fclose(fp);
527
528         if (!index)
529                 return -1;
530
531         snprintf(part, plen, "/dev/mtdblock%s", index);
532
533         return 0;
534 }
535
536 static int check_extroot(char *path)
537 {
538         struct blkid_struct_probe *pr = NULL;
539         char fs[32];
540
541         if (find_block_mtd("rootfs", fs, sizeof(fs)))
542                 return -1;
543
544         list_for_each_entry(pr, &devices, list) {
545                 if (!strcmp(pr->dev, fs)) {
546                         struct stat s;
547                         FILE *fp = NULL;
548                         char tag[32];
549                         char uuid[32] = { 0 };
550
551                         snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
552                         if (stat(tag, &s)) {
553                                 fp = fopen(tag, "w+");
554                                 if (!fp) {
555                                         fprintf(stderr, "extroot: failed to write uuid tag file\n");
556                                         /* return 0 to continue boot regardless of error */
557                                         return 0;
558                                 }
559                                 fputs(pr->uuid, fp);
560                                 fclose(fp);
561                                 return 0;
562                         }
563
564                         fp = fopen(tag, "r");
565                         if (!fp) {
566                                 fprintf(stderr, "extroot: failed to open uuid tag file\n");
567                                 return -1;
568                         }
569
570                         fgets(uuid, sizeof(uuid), fp);
571                         fclose(fp);
572                         if (!strcmp(uuid, pr->uuid))
573                                 return 0;
574                         fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
575                 }
576         }
577         return -1;
578 }
579
580 static int mount_extroot(char *cfg)
581 {
582         char overlay[] = "/tmp/overlay";
583         char mnt[] = "/tmp/mnt";
584         char *path = mnt;
585         struct blkid_struct_probe *pr;
586         struct mount *m;
587         int err = -1;
588
589         if (config_load(cfg))
590                 return -2;
591
592         m = find_block(NULL, NULL, NULL, "/");
593         if (!m)
594                 m = find_block(NULL, NULL, NULL, "/overlay");
595
596         if (!m || !m->extroot)
597                 return -1;
598
599         pr = find_block_info(m->uuid, m->label, NULL);
600         if (pr) {
601                 if (strncmp(pr->id->name, "ext", 3)) {
602                         fprintf(stderr, "extroot: %s is not supported, try ext4\n", pr->id->name);
603                         return -1;
604                 }
605                 if (m->overlay)
606                         path = overlay;
607                 mkdir_p(path);
608
609                 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
610
611                 if (err) {
612                         fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
613                                         pr->dev, pr->id->name, path, err, strerror(err));
614                 } else if (m->overlay) {
615                         err = check_extroot(path);
616                         if (err)
617                                 umount(path);
618                 }
619         }
620
621         return err;
622 }
623
624 static int main_extroot(int argc, char **argv)
625 {
626         struct blkid_struct_probe *pr;
627         char fs[32] = { 0 };
628         char fs_data[32] = { 0 };
629         int err = -1;
630
631         if (!getenv("PREINIT"))
632                 return -1;
633
634         if (argc != 2) {
635                 fprintf(stderr, "Usage: block extroot mountpoint\n");
636                 return -1;
637         }
638
639         mkblkdev();
640         cache_load(1);
641
642         find_block_mtd("rootfs", fs, sizeof(fs));
643         if (!fs[0])
644                 return -2;
645
646         pr = find_block_info(NULL, NULL, fs);
647         if (!pr)
648                 return -3;
649
650         find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
651         if (fs_data[0]) {
652                 pr = find_block_info(NULL, NULL, fs_data);
653                 if (pr && !strcmp(pr->id->name, "jffs2")) {
654                         char cfg[] = "/tmp/jffs_cfg";
655
656                         mkdir_p(cfg);
657                         if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
658                                 err = mount_extroot(cfg);
659                                 umount2(cfg, MNT_DETACH);
660                         }
661                         if (err < 0)
662                                 rmdir("/tmp/overlay");
663                         rmdir(cfg);
664                         return err;
665                 }
666         }
667
668         return mount_extroot(NULL);
669 }
670
671 static int main_detect(int argc, char **argv)
672 {
673         struct blkid_struct_probe *pr;
674
675         cache_load(0);
676         list_for_each_entry(pr, &devices, list)
677                 print_block_uci(pr);
678
679         return 0;
680 }
681
682 static int main_info(int argc, char **argv)
683 {
684         int i;
685         struct blkid_struct_probe *pr;
686
687         cache_load(1);
688         if (argc == 2) {
689                 list_for_each_entry(pr, &devices, list)
690                         print_block_info(pr);
691
692                 return 0;
693         };
694
695         for (i = 2; i < argc; i++) {
696                 struct stat s;
697
698                 if (stat(argv[i], &s)) {
699                         fprintf(stderr, "failed to stat %s\n", argv[i]);
700                         continue;
701                 }
702                 if (!S_ISBLK(s.st_mode)) {
703                         fprintf(stderr, "%s is not a block device\n", argv[i]);
704                         continue;
705                 }
706                 pr = find_block_info(NULL, NULL, argv[i]);
707                 if (pr)
708                         print_block_info(pr);
709         }
710
711         return 0;
712 }
713
714 static int main_swapon(int argc, char **argv)
715 {
716         if (argc != 2) {
717                 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a      Stop swapping on all swap devices\n");
718                 return -1;
719         }
720
721         if (!strcmp(argv[1], "-a")) {
722                 struct blkid_struct_probe *pr;
723
724                 cache_load(0);
725                 list_for_each_entry(pr, &devices, list) {
726                         if (strcmp(pr->id->name, "swap"))
727                                 continue;
728                         if (swapon(pr->dev, 0))
729                                 fprintf(stderr, "failed to swapon %s\n", pr->dev);
730                 }
731         } else {
732                 struct stat s;
733                 int err;
734
735                 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
736                         fprintf(stderr, "%s is not a block device\n", argv[1]);
737                         return -1;
738                 }
739                 err = swapon(argv[1], 0);
740                 if (err) {
741                         fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
742                         return err;
743                 }
744         }
745
746         return 0;
747 }
748
749 static int main_swapoff(int argc, char **argv)
750 {
751         if (argc != 2) {
752                 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a      Stop swapping on all swap devices\n");
753                 return -1;
754         }
755
756         if (!strcmp(argv[1], "-a")) {
757                 FILE *fp = fopen("/proc/swaps", "r");
758                 char line[256];
759
760                 if (!fp) {
761                         fprintf(stderr, "failed to open /proc/swaps\n");
762                         return -1;
763                 }
764                 fgets(line, sizeof(line), fp);
765                 while (fgets(line, sizeof(line), fp)) {
766                         char *end = strchr(line, ' ');
767                         int err;
768
769                         if (!end)
770                                 continue;
771                         *end = '\0';
772                         err = swapoff(line);
773                         if (err)
774                                 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
775                 }
776                 fclose(fp);
777         } else {
778                 struct stat s;
779                 int err;
780
781                 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
782                         fprintf(stderr, "%s is not a block device\n", argv[1]);
783                         return -1;
784                 }
785                 err = swapoff(argv[1]);
786                 if (err) {
787                         fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
788                         return err;
789                 }
790         }
791
792         return 0;
793 }
794
795 int main(int argc, char **argv)
796 {
797         char *base = basename(*argv);
798
799         umask(0);
800
801         if (!strcmp(base, "swapon"))
802                 return main_swapon(argc, argv);
803
804         if (!strcmp(base, "swapoff"))
805                 return main_swapoff(argc, argv);
806
807         if ((argc > 1) && !strcmp(base, "block")) {
808                 if (!strcmp(argv[1], "info"))
809                         return main_info(argc, argv);
810
811                 if (!strcmp(argv[1], "detect"))
812                         return main_detect(argc, argv);
813
814                 if (!strcmp(argv[1], "hotplug"))
815                         return main_hotplug(argc, argv);
816
817                 if (!strcmp(argv[1], "extroot"))
818                         return main_extroot(argc, argv);
819         }
820
821         fprintf(stderr, "Usage: block <info|detect>\n");
822
823         return -1;
824 }