ifupdown: save some 100+ bytes of code in addstr()
[oweals/busybox.git] / util-linux / mount.c
index ea96fb5b06f862d830a3b8ff014e93c59b507ea3..027adc5680ed9fe0759c84c99ce5f22cb45ec63f 100644 (file)
@@ -9,10 +9,6 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-/* todo:
- * bb_getopt_ulflags();
- */
-
 /* Design notes: There is no spec for mount.  Remind me to write one.
 
    mount_main() calls singlemount() which calls mount_it_now().
 
 
 // Not real flags, but we want to be able to check for this.
-#define MOUNT_NOAUTO    (1<<29)
-#define MOUNT_SWAP      (1<<30)
+enum {
+       MOUNT_USERS  = (1<<28)*ENABLE_DESKTOP,
+       MOUNT_NOAUTO = (1<<29),
+       MOUNT_SWAP   = (1<<30),
+};
+// TODO: more "user" flag compatibility.
+// "user" option (from mount manpage):
+// Only the user that mounted a filesystem can unmount it again.
+// If any user should be able to unmount, then use users instead of user
+// in the fstab line.  The owner option is similar to the user option,
+// with the restriction that the user must be the owner of the special file.
+// This may be useful e.g. for /dev/fd if a login script makes
+// the console user owner of this device.
 
 /* Standard mount options (from -o options or --options), with corresponding
  * flags */
@@ -55,8 +62,10 @@ struct {
        USE_FEATURE_MOUNT_FSTAB(
                {"defaults", 0},
                {"quiet", 0},
-               {"noauto",MOUNT_NOAUTO},
-               {"swap",MOUNT_SWAP},
+               {"noauto", MOUNT_NOAUTO},
+               {"swap", MOUNT_SWAP},
+               USE_DESKTOP({"user",  MOUNT_USERS},)
+               USE_DESKTOP({"users", MOUNT_USERS},)
        )
 
        USE_FEATURE_MOUNT_FLAGS(
@@ -96,7 +105,7 @@ struct {
        {"remount", MS_REMOUNT},  // action flag
 };
 
-
+#define VECTOR_SIZE(v) (sizeof(v) / sizeof((v)[0]))
 
 /* Append mount options to string */
 static void append_mount_options(char **oldopts, char *newopts)
@@ -110,7 +119,8 @@ static void append_mount_options(char **oldopts, char *newopts)
                        if (p) len = p - newopts;
                        p = *oldopts;
                        while (1) {
-                               if (!strncmp(p,newopts,len) && (p[len]==',' || p[len]==0))
+                               if (!strncmp(p, newopts, len)
+                                && (p[len]==',' || p[len]==0))
                                        goto skip;
                                p = strchr(p,',');
                                if(!p) break;
@@ -143,7 +153,7 @@ static int parse_mount_options(char *options, char **unrecognized)
                if (comma) *comma = 0;
 
                // Find this option in mount_options
-               for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
+               for (i = 0; i < VECTOR_SIZE(mount_options); i++) {
                        if (!strcasecmp(mount_options[i].name, options)) {
                                long fl = mount_options[i].flags;
                                if (fl < 0) flags &= fl;
@@ -152,9 +162,7 @@ static int parse_mount_options(char *options, char **unrecognized)
                        }
                }
                // If unrecognized not NULL, append unrecognized mount options */
-               if (unrecognized
-                               && i == (sizeof(mount_options) / sizeof(*mount_options)))
-               {
+               if (unrecognized && i == VECTOR_SIZE(mount_options)) {
                        // Add it to strflags, to pass on to kernel
                        i = *unrecognized ? strlen(*unrecognized) : 0;
                        *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
@@ -178,8 +186,12 @@ static int parse_mount_options(char *options, char **unrecognized)
 
 static llist_t *get_block_backed_filesystems(void)
 {
-       char *fs, *buf,
-                *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
+       static const char *const filesystems[] = {
+               "/etc/filesystems",
+               "/proc/filesystems",
+               0
+       };
+       char *fs, *buf;
        llist_t *list = 0;
        int i;
        FILE *f;
@@ -188,16 +200,14 @@ static llist_t *get_block_backed_filesystems(void)
                f = fopen(filesystems[i], "r");
                if (!f) continue;
 
-               for (fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
-                       free(buf))
-               {
-                       if (!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
-
-                       while (isspace(*fs)) fs++;
-                       if (*fs=='#' || *fs=='*') continue;
-                       if (!*fs) continue;
+               while ((buf = xmalloc_getline(f)) != 0) {
+                       if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
+                               continue;
+                       fs = skip_whitespace(buf);
+                       if (*fs=='#' || *fs=='*' || !*fs) continue;
 
-                       llist_add_to_end(&list,xstrdup(fs));
+                       llist_add_to_end(&list, xstrdup(fs));
+                       free(buf);
                }
                if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
        }
@@ -220,19 +230,17 @@ void delete_block_backed_filesystems(void);
 static int useMtab = 1;
 static int fakeIt;
 #else
-enum {
-       useMtab = 0,
-       fakeIt = 0,
-};
+#define useMtab 0
+#define fakeIt 0
 #endif
 
 // Perform actual mount of specific filesystem at specific location.
 // NB: mp->xxx fields may be trashed on exit
 static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
 {
-       int rc;
+       int rc = 0;
 
-       if (fakeIt) return 0;
+       if (fakeIt) goto mtab;
 
        // Mount, with fallback to read-only if necessary.
 
@@ -253,14 +261,16 @@ static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
 
        /* If the mount was successful, and we're maintaining an old-style
         * mtab file by hand, add the new entry to it now. */
-
+ mtab:
        if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
                char *fsname;
                FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
                int i;
 
-               if (!mountTable)
+               if (!mountTable) {
                        bb_error_msg("no %s",bb_path_mtab_file);
+                       goto ret;
+               }
 
                // Add vfs string flags
 
@@ -292,7 +302,7 @@ static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
                        free(fsname);
                }
        }
-
+ ret:
        return rc;
 }
 
@@ -673,7 +683,7 @@ get_mountport(struct sockaddr_in *server_addr,
        p.pm_vers = version;
        p.pm_prot = proto;
        p.pm_port = port;
-       
+
        while (pmap) {
                if (pmap->pml_map.pm_prog != prog)
                        goto next;
@@ -715,7 +725,7 @@ static int daemonize(void)
        dup2(fd, 2);
        if (fd > 2) close(fd);
        setsid();
-       openlog(bb_applet_name, LOG_PID, LOG_DAEMON);
+       openlog(applet_name, LOG_PID, LOG_DAEMON);
        logmode = LOGMODE_SYSLOG;
        return 1;
 }
@@ -732,7 +742,7 @@ static inline int we_saw_this_host_before(const char *hostname)
  * error_msg_rpc(clnt_*error*(" ")) */
 static void error_msg_rpc(const char *msg)
 {
-       size_t len;
+       int len;
        while (msg[0] == ' ' || msg[0] == ':') msg++;
        len = strlen(msg);
        while (len && msg[len-1] == '\n') len--;
@@ -864,99 +874,168 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
        for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
                char *opteq = strchr(opt, '=');
                if (opteq) {
-                       int val = atoi(opteq + 1);
+                       const char *const options[] = {
+                               /* 0 */ "rsize",
+                               /* 1 */ "wsize",
+                               /* 2 */ "timeo",
+                               /* 3 */ "retrans",
+                               /* 4 */ "acregmin",
+                               /* 5 */ "acregmax",
+                               /* 6 */ "acdirmin",
+                               /* 7 */ "acdirmax",
+                               /* 8 */ "actimeo",
+                               /* 9 */ "retry",
+                               /* 10 */ "port",
+                               /* 11 */ "mountport",
+                               /* 12 */ "mounthost",
+                               /* 13 */ "mountprog",
+                               /* 14 */ "mountvers",
+                               /* 15 */ "nfsprog",
+                               /* 16 */ "nfsvers",
+                               /* 17 */ "vers",
+                               /* 18 */ "proto",
+                               /* 19 */ "namlen",
+                               /* 20 */ "addr",
+                               NULL
+                       };
+                       int val = xatoi_u(opteq + 1);
                        *opteq = '\0';
-                       if (!strcmp(opt, "rsize"))
+                       switch (index_in_str_array(options, opt)) {
+                       case 0: // "rsize"
                                data.rsize = val;
-                       else if (!strcmp(opt, "wsize"))
+                               break;
+                       case 1: // "wsize"
                                data.wsize = val;
-                       else if (!strcmp(opt, "timeo"))
+                               break;
+                       case 2: // "timeo"
                                data.timeo = val;
-                       else if (!strcmp(opt, "retrans"))
+                               break;
+                       case 3: // "retrans"
                                data.retrans = val;
-                       else if (!strcmp(opt, "acregmin"))
+                               break;
+                       case 4: // "acregmin"
                                data.acregmin = val;
-                       else if (!strcmp(opt, "acregmax"))
+                               break;
+                       case 5: // "acregmax"
                                data.acregmax = val;
-                       else if (!strcmp(opt, "acdirmin"))
+                               break;
+                       case 6: // "acdirmin"
                                data.acdirmin = val;
-                       else if (!strcmp(opt, "acdirmax"))
+                               break;
+                       case 7: // "acdirmax"
                                data.acdirmax = val;
-                       else if (!strcmp(opt, "actimeo")) {
+                               break;
+                       case 8: // "actimeo"
                                data.acregmin = val;
                                data.acregmax = val;
                                data.acdirmin = val;
                                data.acdirmax = val;
-                       }
-                       else if (!strcmp(opt, "retry"))
+                               break;
+                       case 9: // "retry"
                                retry = val;
-                       else if (!strcmp(opt, "port"))
+                               break;
+                       case 10: // "port"
                                port = val;
-                       else if (!strcmp(opt, "mountport"))
+                               break;
+                       case 11: // "mountport"
                                mountport = val;
-                       else if (!strcmp(opt, "mounthost"))
+                               break;
+                       case 12: // "mounthost"
                                mounthost = xstrndup(opteq+1,
-                                                 strcspn(opteq+1," \t\n\r,"));
-                       else if (!strcmp(opt, "mountprog"))
+                                               strcspn(opteq+1," \t\n\r,"));
+                               break;
+                       case 13: // "mountprog"
                                mountprog = val;
-                       else if (!strcmp(opt, "mountvers"))
+                               break;
+                       case 14: // "mountvers"
                                mountvers = val;
-                       else if (!strcmp(opt, "nfsprog"))
+                               break;
+                       case 15: // "nfsprog"
                                nfsprog = val;
-                       else if (!strcmp(opt, "nfsvers") ||
-                                !strcmp(opt, "vers"))
+                               break;
+                       case 16: // "nfsvers"
+                       case 17: // "vers"
                                nfsvers = val;
-                       else if (!strcmp(opt, "proto")) {
+                               break;
+                       case 18: // "proto"
                                if (!strncmp(opteq+1, "tcp", 3))
                                        tcp = 1;
                                else if (!strncmp(opteq+1, "udp", 3))
                                        tcp = 0;
                                else
                                        bb_error_msg("warning: unrecognized proto= option");
-                       } else if (!strcmp(opt, "namlen")) {
+                               break;
+                       case 19: // "namlen"
                                if (nfs_mount_version >= 2)
                                        data.namlen = val;
                                else
                                        bb_error_msg("warning: option namlen is not supported\n");
-                       } else if (!strcmp(opt, "addr"))
-                               /* ignore */;
-                       else {
+                               break;
+                       case 20: // "addr" - ignore
+                               break;
+                       default:
                                bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
                                goto fail;
                        }
                }
                else {
+                       const char *const options[] = {
+                               "bg",
+                               "fg",
+                               "soft",
+                               "hard",
+                               "intr",
+                               "posix",
+                               "cto",
+                               "ac",
+                               "tcp",
+                               "udp",
+                               "lock",
+                               NULL
+                       };
                        int val = 1;
                        if (!strncmp(opt, "no", 2)) {
                                val = 0;
                                opt += 2;
                        }
-                       if (!strcmp(opt, "bg"))
+                       switch (index_in_str_array(options, opt)) {
+                       case 0: // "bg"
                                bg = val;
-                       else if (!strcmp(opt, "fg"))
+                               break;
+                       case 1: // "fg"
                                bg = !val;
-                       else if (!strcmp(opt, "soft"))
+                               break;
+                       case 2: // "soft"
                                soft = val;
-                       else if (!strcmp(opt, "hard"))
+                               break;
+                       case 3: // "hard"
                                soft = !val;
-                       else if (!strcmp(opt, "intr"))
+                               break;
+                       case 4: // "intr"
                                intr = val;
-                       else if (!strcmp(opt, "posix"))
+                               break;
+                       case 5: // "posix"
                                posix = val;
-                       else if (!strcmp(opt, "cto"))
+                               break;
+                       case 6: // "cto"
                                nocto = !val;
-                       else if (!strcmp(opt, "ac"))
+                               break;
+                       case 7: // "ac"
                                noac = !val;
-                       else if (!strcmp(opt, "tcp"))
+                               break;
+                       case 8: // "tcp"
                                tcp = val;
-                       else if (!strcmp(opt, "udp"))
+                               break;
+                       case 9: // "udp"
                                tcp = !val;
-                       else if (!strcmp(opt, "lock")) {
+                               break;
+                       case 10: // "lock"
                                if (nfs_mount_version >= 3)
                                        nolock = !val;
                                else
                                        bb_error_msg("warning: option nolock is not supported");
-                       } else {
+                               break;
+                       default:
                                bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
                                goto fail;
                        }
@@ -1315,7 +1394,7 @@ static int singlemount(struct mntent *mp, int ignore_busy)
                s = strrchr(mp->mnt_fsname, '\\');
                if (s == mp->mnt_fsname+1) goto report_error;
                *s = 0;
-               he = gethostbyname(mp->mnt_fsname+2);
+               he = gethostbyname(mp->mnt_fsname+2);
                *s = '\\';
                if (!he) goto report_error;
 
@@ -1427,43 +1506,45 @@ report_error:
 // Parse options, if necessary parse fstab/mtab, and call singlemount for
 // each directory to be mounted.
 
+const char must_be_root[] = "you must be root";
+
 int mount_main(int argc, char **argv)
 {
-       enum { OPT_ALL = 0x8 };
+       enum { OPT_ALL = 0x10 };
 
        char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
-       char *opt_o, *fstabname;
+       char *opt_o;
+       const char *fstabname;
        FILE *fstab;
        int i, j, rc = 0;
-       unsigned long opt;
+       unsigned opt;
        struct mntent mtpair[2], *mtcur = mtpair;
+       SKIP_DESKTOP(const int nonroot = 0;)
+       USE_DESKTOP( int nonroot = (getuid() != 0);)
 
        /* parse long options, like --bind and --move.  Note that -o option
         * and --option are synonymous.  Yes, this means --remount,rw works. */
 
        for (i = j = 0; i < argc; i++) {
                if (argv[i][0] == '-' && argv[i][1] == '-') {
-                       append_mount_options(&cmdopts,argv[i]+2);
+                       append_mount_options(&cmdopts, argv[i]+2);
                } else argv[j++] = argv[i];
        }
+       argv[j] = 0;
        argc = j;
 
        // Parse remaining options
 
-       opt = bb_getopt_ulflags(argc, argv, "o:t:rwavnf", &opt_o, &fstype);
-       if (opt & 1) // -o
-               append_mount_options(&cmdopts, opt_o);
-       //if (opt & 1) // -t
-       if (opt & 2) // -r
-               append_mount_options(&cmdopts, "ro");
-       if (opt & 4) // -w
-               append_mount_options(&cmdopts, "rw");
-       //if (opt & 8) // -a
-       if (opt & 0x10) // -n
-               USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE);
-       if (opt & 0x20) // -f
-               USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE);
-       //if (opt & 0x40) // ignore -v
+       opt = getopt32(argc, argv, "o:t:rwanfvs", &opt_o, &fstype);
+       if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
+       //if (opt & 0x2) // -t
+       if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
+       if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
+       //if (opt & 0x10) // -a
+       if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
+       if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
+       //if (opt & 0x80) // -v: verbose (ignore)
+       //if (opt & 0x100) // -s: sloppy (ignore)
        argv += optind;
        argc -= optind;
 
@@ -1477,12 +1558,12 @@ int mount_main(int argc, char **argv)
                if (!(opt & OPT_ALL)) {
                        FILE *mountTable = setmntent(bb_path_mtab_file, "r");
 
-                       if (!mountTable) bb_error_msg_and_die("no %s",bb_path_mtab_file);
+                       if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
 
-                       while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
+                       while (getmntent_r(mountTable, mtpair, bb_common_bufsiz1,
                                                                sizeof(bb_common_bufsiz1)))
                        {
-                               // Don't show rootfs.
+                               // Don't show rootfs. FIXME: why??
                                if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
 
                                if (!fstype || !strcmp(mtpair->mnt_type, fstype))
@@ -1500,6 +1581,8 @@ int mount_main(int argc, char **argv)
        // argument when we get it.
 
        if (argc == 2) {
+               if (nonroot)
+                       bb_error_msg_and_die(must_be_root);
                mtpair->mnt_fsname = argv[0];
                mtpair->mnt_dir = argv[1];
                mtpair->mnt_type = fstype;
@@ -1508,29 +1591,33 @@ int mount_main(int argc, char **argv)
                goto clean_up;
        }
 
+       i = parse_mount_options(cmdopts, 0);
+       if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
+               bb_error_msg_and_die(must_be_root);
+
        // If we have a shared subtree flag, don't worry about fstab or mtab.
-       i = parse_mount_options(cmdopts,0);
+
        if (ENABLE_FEATURE_MOUNT_FLAGS &&
-                       (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
+                       (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)))
        {
                rc = mount("", argv[0], "", i, "");
                if (rc) bb_perror_msg_and_die("%s", argv[0]);
                goto clean_up;
        }
-       
+
        // Open either fstab or mtab
 
-       if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
+       fstabname = "/etc/fstab";
+       if (i & MS_REMOUNT) {
                fstabname = bb_path_mtab_file;
-       else fstabname="/etc/fstab";
-
-       fstab = setmntent(fstabname,"r");
+       }
+       fstab = setmntent(fstabname, "r");
        if (!fstab)
-               bb_perror_msg_and_die("cannot read %s",fstabname);
+               bb_perror_msg_and_die("cannot read %s", fstabname);
 
        // Loop through entries until we find what we're looking for.
 
-       memset(mtpair,0,sizeof(mtpair));
+       memset(mtpair, 0, sizeof(mtpair));
        for (;;) {
                struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
 
@@ -1550,11 +1637,17 @@ int mount_main(int argc, char **argv)
                                        bb_error_msg_and_die("can't find %s in %s",
                                                argv[0], fstabname);
 
+                               mtcur = mtnext;
+                               if (nonroot) {
+                                       // fstab must have "users" or "user"
+                                       if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
+                                               bb_error_msg_and_die(must_be_root);
+                               }
+
                                // Mount the last thing we found.
 
-                               mtcur = mtnext;
                                mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
-                               append_mount_options(&(mtcur->mnt_opts),cmdopts);
+                               append_mount_options(&(mtcur->mnt_opts), cmdopts);
                                rc = singlemount(mtcur, 0);
                                free(mtcur->mnt_opts);
                        }
@@ -1569,10 +1662,10 @@ int mount_main(int argc, char **argv)
 
                        // Is this what we're looking for?
 
-                       if (strcmp(argv[0],mtcur->mnt_fsname) &&
-                          strcmp(storage_path,mtcur->mnt_fsname) &&
-                          strcmp(argv[0],mtcur->mnt_dir) &&
-                          strcmp(storage_path,mtcur->mnt_dir)) continue;
+                       if (strcmp(argv[0], mtcur->mnt_fsname) &&
+                          strcmp(storage_path, mtcur->mnt_fsname) &&
+                          strcmp(argv[0], mtcur->mnt_dir) &&
+                          strcmp(storage_path, mtcur->mnt_dir)) continue;
 
                        // Remember this entry.  Something later may have overmounted
                        // it, and we want the _last_ match.
@@ -1582,15 +1675,22 @@ int mount_main(int argc, char **argv)
                // If we're mounting all.
 
                } else {
-
                        // Do we need to match a filesystem type?
-                       if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
+                       // TODO: support "-t type1,type2"; "-t notype1,type2"
+
+                       if (fstype && strcmp(mtcur->mnt_type, fstype)) continue;
 
                        // Skip noauto and swap anyway.
 
-                       if (parse_mount_options(mtcur->mnt_opts,0)
+                       if (parse_mount_options(mtcur->mnt_opts, 0)
                                & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
 
+                       // No, mount -a won't mount anything,
+                       // even user mounts, for mere humans.
+
+                       if (nonroot)
+                               bb_error_msg_and_die(must_be_root);
+
                        // Mount this thing.
 
                        if (singlemount(mtcur, 1)) {