tar: support -T - and -X -
[oweals/busybox.git] / e2fsprogs / fsck.c
index 178792f6fc885182fa2ab86ecce03d47fe595ce5..a86a9d96f6577ad13df4e6a9b691da368284e7e8 100644 (file)
  * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
  *      2001, 2002, 2003, 2004, 2005 by  Theodore Ts'o.
  *
- * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
- * %End-Header%
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* All filesystem specific hooks have been removed.
  * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
 #define DO_PROGRESS_INDICATOR 0
 
+/* fsck 1.41.4 (27-Jan-2009) manpage says:
+ * 0   - No errors
+ * 1   - File system errors corrected
+ * 2   - System should be rebooted
+ * 4   - File system errors left uncorrected
+ * 8   - Operational error
+ * 16  - Usage or syntax error
+ * 32  - Fsck canceled by user request
+ * 128 - Shared library error
+ */
 #define EXIT_OK          0
 #define EXIT_NONDESTRUCT 1
 #define EXIT_DESTRUCT    2
@@ -55,7 +62,6 @@
 /*
  * Internal structure for mount table entries.
  */
-
 struct fs_info {
        struct fs_info *next;
        char    *device;
@@ -106,9 +112,7 @@ static const char really_wanted[] ALIGN1 =
 
 #define BASE_MD "/dev/md"
 
-static char **devices;
 static char **args;
-static int num_devices;
 static int num_args;
 static int verbose;
 
@@ -119,13 +123,10 @@ static char **fs_type_list;
 static uint8_t *fs_type_flag;
 static smallint fs_type_negated;
 
-static volatile smallint cancel_requested;
-static smallint doall;
 static smallint noexecute;
 static smallint serialize;
 static smallint skip_root;
 /* static smallint like_mount; */
-static smallint notitle;
 static smallint parallel_root;
 static smallint force_all_parallel;
 
@@ -168,12 +169,12 @@ static char *base_device(const char *device)
        const char *disk;
        int len;
 #endif
-       cp = str = xstrdup(device);
+       str = xstrdup(device);
 
-       /* Skip over /dev/; if it's not present, give up. */
-       if (strncmp(cp, "/dev/", 5) != 0)
+       /* Skip over "/dev/"; if it's not present, give up */
+       cp = skip_dev_pfx(str);
+       if (cp == str)
                goto errout;
-       cp += 5;
 
        /*
         * For md devices, we treat them all as if they were all
@@ -280,9 +281,11 @@ static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
        fs = xzalloc(sizeof(*fs));
        fs->device = xstrdup(device);
        fs->mountpt = xstrdup(mntpnt);
+       if (strchr(type, ','))
+               type = (char *)"auto";
        fs->type = xstrdup(type);
        fs->opts = xstrdup(opts ? opts : "");
-       fs->passno = passno;
+       fs->passno = passno < 0 ? 1 : passno;
        /*fs->flags = 0; */
        /*fs->next = NULL; */
 
@@ -295,130 +298,29 @@ static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
        return fs;
 }
 
-static void strip_line(char *line)
-{
-       char *p = line + strlen(line) - 1;
-
-       while (*line) {
-               if (*p != '\n' && *p != '\r')
-                       break;
-               *p-- = '\0';
-       }
-}
-
-static char *parse_word(char **buf)
-{
-       char *word, *next;
-
-       word = *buf;
-       if (*word == '\0')
-               return NULL;
-
-       word = skip_whitespace(word);
-       next = skip_non_whitespace(word);
-       if (*next)
-               *next++ = '\0';
-       *buf = next;
-       return word;
-}
-
-static void parse_escape(char *word)
-{
-       char *q, c;
-       const char *p;
-
-       if (!word)
-               return;
-
-       for (p = q = word; *p; q++) {
-               c = *p++;
-               if (c != '\\') {
-                       *q = c;
-               } else {
-                       *q = bb_process_escape_sequence(&p);
-               }
-       }
-       *q = '\0';
-}
-
-static int parse_fstab_line(char *line, struct fs_info **ret_fs)
-{
-       char *device, *mntpnt, *type, *opts, *passno, *cp;
-       struct fs_info *fs;
-
-       *ret_fs = NULL;
-       strip_line(line);
-       *strchrnul(line, '#') = '\0'; /* Ignore everything after comment */
-       cp = line;
-
-       device = parse_word(&cp);
-       if (!device) return 0; /* Allow blank lines */
-       mntpnt = parse_word(&cp);
-       type = parse_word(&cp);
-       opts = parse_word(&cp);
-       /*freq =*/ parse_word(&cp);
-       passno = parse_word(&cp);
-
-       if (!mntpnt || !type)
-               return -1;
-
-       parse_escape(device);
-       parse_escape(mntpnt);
-       parse_escape(type);
-       parse_escape(opts);
-       parse_escape(passno);
-
-       if (strchr(type, ','))
-               type = NULL;
-
-       fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
-                       (passno ? atoi(passno) : -1));
-       *ret_fs = fs;
-       return 0;
-}
-
 /* Load the filesystem database from /etc/fstab */
 static void load_fs_info(const char *filename)
 {
-       FILE *f;
-       int lineno = 0;
-       int old_fstab = 1;
+       FILE *fstab;
+       struct mntent mte;
        struct fs_info *fs;
 
-       f = fopen_or_warn(filename, "r");
-       if (f == NULL) {
+       fstab = setmntent(filename, "r");
+       if (!fstab) {
+               bb_perror_msg("can't read '%s'", filename);
                return;
        }
-       while (1) {
-               int r;
-               char *buf = xmalloc_getline(f);
-               if (!buf) break;
-               r = parse_fstab_line(buf, &fs);
-               free(buf);
-               lineno++;
-               if (r < 0) {
-                       bb_error_msg("WARNING: bad format "
-                               "on line %d of %s", lineno, filename);
-                       continue;
-               }
-               if (!fs)
-                       continue;
-               if (fs->passno < 0)
-                       fs->passno = 0;
-               else
-                       old_fstab = 0;
-       }
-       fclose(f);
 
-       if (old_fstab) {
-               fputs("\007"
-"WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
-"I will kludge around things for you, but you should fix\n"
-"your /etc/fstab file as soon as you can.\n\n", stderr);
-               for (fs = filesys_info; fs; fs = fs->next) {
-                       fs->passno = 1;
-               }
+       // Loop through entries
+       while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
+               //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
+               //      mte.mnt_type, mte.mnt_opts,
+               //      mte.mnt_passno);
+               fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
+                       mte.mnt_type, mte.mnt_opts,
+                       mte.mnt_passno);
        }
+       endmntent(fstab);
 }
 
 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
@@ -455,13 +357,13 @@ static int progress_active(void)
 /*
  * Send a signal to all outstanding fsck child processes
  */
-static void kill_all_if_cancel_requested(void)
+static void kill_all_if_got_signal(void)
 {
        static smallint kill_sent;
 
        struct fsck_instance *inst;
 
-       if (!cancel_requested || kill_sent)
+       if (!bb_got_signal || kill_sent)
                return;
 
        for (inst = instance_list; inst; inst = inst->next) {
@@ -490,7 +392,7 @@ static int wait_one(int flags)
 
        while (1) {
                pid = waitpid(-1, &status, flags);
-               kill_all_if_cancel_requested();
+               kill_all_if_got_signal();
                if (pid == 0) /* flags == WNOHANG and no children exited */
                        return -1;
                if (pid < 0) {
@@ -597,60 +499,55 @@ static int wait_many(int flags)
 static void execute(const char *type, const char *device,
                const char *mntpt /*, int interactive */)
 {
-       char *argv[num_args + 4]; /* see count below: */
-       int argc;
        int i;
        struct fsck_instance *inst;
        pid_t pid;
 
-       argv[0] = xasprintf("fsck.%s", type); /* 1 */
-       for (i = 0; i < num_args; i++)
-               argv[i+1] = args[i]; /* num_args */
-       argc = num_args + 1;
+       args[0] = xasprintf("fsck.%s", type);
 
 #if DO_PROGRESS_INDICATOR
        if (progress && !progress_active()) {
                if (strcmp(type, "ext2") == 0
                 || strcmp(type, "ext3") == 0
                ) {
-                       argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
+                       args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
                        inst->flags |= FLAG_PROGRESS;
                }
        }
 #endif
 
-       argv[argc++] = (char*)device; /* 1 */
-       argv[argc] = NULL; /* 1 */
+       args[num_args - 2] = (char*)device;
+       /* args[num_args - 1] = NULL; - already is */
 
        if (verbose || noexecute) {
-               printf("[%s (%d) -- %s]", argv[0], num_running,
+               printf("[%s (%d) -- %s]", args[0], num_running,
                                        mntpt ? mntpt : device);
-               for (i = 0; i < argc; i++)
-                       printf(" %s", argv[i]);
+               for (i = 0; args[i]; i++)
+                       printf(" %s", args[i]);
                bb_putchar('\n');
        }
 
        /* Fork and execute the correct program. */
        pid = -1;
        if (!noexecute) {
-               pid = spawn(argv);
+               pid = spawn(args);
                if (pid < 0)
-                       bb_simple_perror_msg(argv[0]);
+                       bb_simple_perror_msg(args[0]);
        }
 
 #if DO_PROGRESS_INDICATOR
-       free(argv[num_args + 1]);
+       free(args[XXX]);
 #endif
 
        /* No child, so don't record an instance */
        if (pid <= 0) {
-               free(argv[0]);
+               free(args[0]);
                return;
        }
 
        inst = xzalloc(sizeof(*inst));
        inst->pid = pid;
-       inst->prog = argv[0];
+       inst->prog = args[0];
        inst->device = xstrdup(device);
        inst->base_device = base_device(device);
 #if DO_PROGRESS_INDICATOR
@@ -890,7 +787,7 @@ static int check_all(void)
                pass_done = 1;
 
                for (fs = filesys_info; fs; fs = fs->next) {
-                       if (cancel_requested)
+                       if (bb_got_signal)
                                break;
                        if (fs->flags & FLAG_DONE)
                                continue;
@@ -930,7 +827,7 @@ static int check_all(void)
                                break;
                        }
                }
-               if (cancel_requested)
+               if (bb_got_signal)
                        break;
                if (verbose > 1)
                        printf("--waiting-- (pass %d)\n", passno);
@@ -943,7 +840,7 @@ static int check_all(void)
                } else
                        not_done_yet = 1;
        }
-       kill_all_if_cancel_requested();
+       kill_all_if_got_signal();
        status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
        return status;
 }
@@ -1006,36 +903,56 @@ static void compile_fs_type(char *fs_type)
        }
 }
 
-static void parse_args(char **argv)
+static char **new_args(void)
+{
+       args = xrealloc_vector(args, 2, num_args);
+       return &args[num_args++];
+}
+
+int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int fsck_main(int argc UNUSED_PARAM, char **argv)
 {
-       int i, j;
-       char *arg, *tmp;
-       char *options;
-       int optpos;
-       int opts_for_fsck = 0;
+       int i, status;
+       /*int interactive;*/
+       struct fs_info *fs;
+       const char *fstab;
+       char *tmp;
+       char **devices;
+       int num_devices;
+       smallint opts_for_fsck;
+       smallint doall;
+       smallint notitle;
+
+       /* we want wait() to be interruptible */
+       signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
+       signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
+
+       setbuf(stdout, NULL);
 
-       /* in bss, so already zeroed
+       opts_for_fsck = doall = notitle = 0;
+       devices = NULL;
        num_devices = 0;
-       num_args = 0;
-       instance_list = NULL;
-       */
+       new_args(); /* args[0] = NULL, will be replaced by fsck.<type> */
+       /* instance_list = NULL; - in bss, so already zeroed */
 
-       for (i = 1; argv[i]; i++) {
-               arg = argv[i];
+       while (*++argv) {
+               int j;
+               int optpos;
+               char *options;
+               char *arg = *argv;
 
                /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
                if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
 // FIXME: must check that arg is a blkdev, or resolve
 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
-                       devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
-                       devices[num_devices++] = xstrdup(arg);
+                       devices = xrealloc_vector(devices, 2, num_devices);
+                       devices[num_devices++] = arg;
                        continue;
                }
 
                if (arg[0] != '-' || opts_for_fsck) {
-                       args = xrealloc(args, (num_args+1) * sizeof(args[0]));
-                       args[num_args++] = xstrdup(arg);
+                       *new_args() = arg;
                        continue;
                }
 
@@ -1055,12 +972,13 @@ static void parse_args(char **argv)
                        case 'C':
                                progress = 1;
                                if (arg[++j]) { /* -Cn */
-                                       progress_fd = xatoi_u(&arg[j]);
+                                       progress_fd = xatoi_positive(&arg[j]);
                                        goto next_arg;
                                }
                                /* -C n */
-                               if (!argv[++i]) bb_show_usage();
-                               progress_fd = xatoi_u(argv[i]);
+                               if (!*++argv)
+                                       bb_show_usage();
+                               progress_fd = xatoi_positive(*argv);
                                goto next_arg;
 #endif
                        case 'V':
@@ -1089,8 +1007,8 @@ static void parse_args(char **argv)
                                        bb_show_usage();
                                if (arg[++j])
                                        tmp = &arg[j];
-                               else if (argv[++i])
-                                       tmp = argv[i];
+                               else if (*++argv)
+                                       tmp = *argv;
                                else
                                        bb_show_usage();
                                fstype = xstrdup(tmp);
@@ -1111,8 +1029,7 @@ static void parse_args(char **argv)
                if (optpos) {
                        options[0] = '-';
                        options[optpos + 1] = '\0';
-                       args = xrealloc(args, (num_args+1) * sizeof(args[0]));
-                       args[num_args++] = options;
+                       *new_args() = options;
                }
        }
        if (getenv("FSCK_FORCE_ALL_PARALLEL"))
@@ -1120,28 +1037,8 @@ static void parse_args(char **argv)
        tmp = getenv("FSCK_MAX_INST");
        if (tmp)
                max_running = xatoi(tmp);
-}
-
-static void signal_cancel(int sig ATTRIBUTE_UNUSED)
-{
-       cancel_requested = 1;
-}
-
-int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int fsck_main(int argc ATTRIBUTE_UNUSED, char **argv)
-{
-       int i, status;
-       /*int interactive;*/
-       const char *fstab;
-       struct fs_info *fs;
-
-       /* we want wait() to be interruptible */
-       signal_no_SA_RESTART_empty_mask(SIGINT, signal_cancel);
-       signal_no_SA_RESTART_empty_mask(SIGTERM, signal_cancel);
-
-       setbuf(stdout, NULL);
-
-       parse_args(argv);
+       new_args(); /* args[num_args - 2] will be replaced by <device> */
+       new_args(); /* args[num_args - 1] is the last, NULL element */
 
        if (!notitle)
                puts("fsck (busybox "BB_VER", "BB_BT")");
@@ -1162,8 +1059,8 @@ int fsck_main(int argc ATTRIBUTE_UNUSED, char **argv)
 
        status = 0;
        for (i = 0; i < num_devices; i++) {
-               if (cancel_requested) {
-                       kill_all_if_cancel_requested();
+               if (bb_got_signal) {
+                       kill_all_if_got_signal();
                        break;
                }