'simple' error message functions by Loic Grenie <loic.grenie@gmail.com>.
[oweals/busybox.git] / util-linux / mount.c
index 565dccd9a16aba7f9bfadc7e14228f9d9fab2a60..10ada5426577bd4214cae7cea6588fca5a1cb812 100644 (file)
@@ -18,8 +18,8 @@
    mount_it_now() does the actual mount.
 */
 
-#include "busybox.h"
 #include <mntent.h>
+#include "libbb.h"
 
 /* Needed for nfs support only... */
 #include <syslog.h>
 #include <rpc/pmap_prot.h>
 #include <rpc/pmap_clnt.h>
 
+#ifndef MS_SILENT
+#define MS_SILENT      (1 << 15)
+#endif
+
+#if defined(__dietlibc__)
+/* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
+ * dietlibc-0.30 does not have implementation of getmntent_r() */
+struct mntent *getmntent_r(FILE* stream, struct mntent* result, char* buffer, int bufsize)
+{
+       struct mntent* ment = getmntent(stream);
+       memcpy(result, ment, sizeof(struct mntent));
+       return result;
+}
+#endif
+
+#define getmntent_buf bb_common_bufsiz1
+
 
 // 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 */
 
-struct {
-       char *name;
+static const struct {
+       const char *name;
        long flags;
-} static mount_options[] = {
+} mount_options[] = {
        // MS_FLAGS set a bit.  ~MS_FLAGS disable that bit.  0 flags are NOPs.
 
        USE_FEATURE_MOUNT_LOOP(
@@ -50,9 +78,12 @@ struct {
 
        USE_FEATURE_MOUNT_FSTAB(
                {"defaults", 0},
-               {"quiet", 0},
-               {"noauto",MOUNT_NOAUTO},
-               {"swap",MOUNT_SWAP},
+               /* {"quiet", 0}, - do not filter out, vfat wants to see it */
+               {"noauto", MOUNT_NOAUTO},
+               {"sw", MOUNT_SWAP},
+               {"swap", MOUNT_SWAP},
+               USE_DESKTOP({"user",  MOUNT_USERS},)
+               USE_DESKTOP({"users", MOUNT_USERS},)
        )
 
        USE_FEATURE_MOUNT_FLAGS(
@@ -92,10 +123,9 @@ 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)
+static void append_mount_options(char **oldopts, const char *newopts)
 {
        if (*oldopts && **oldopts) {
                /* do not insert options which are already there */
@@ -106,10 +136,11 @@ 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;
+                               if (!p) break;
                                p++;
                        }
                        p = xasprintf("%s,%.*s", *oldopts, len, newopts);
@@ -139,7 +170,7 @@ static int parse_mount_options(char *options, char **unrecognized)
                if (comma) *comma = 0;
 
                // Find this option in mount_options
-               for (i = 0; i < VECTOR_SIZE(mount_options); i++) {
+               for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
                        if (!strcasecmp(mount_options[i].name, options)) {
                                long fl = mount_options[i].flags;
                                if (fl < 0) flags &= fl;
@@ -148,7 +179,7 @@ static int parse_mount_options(char *options, char **unrecognized)
                        }
                }
                // If unrecognized not NULL, append unrecognized mount options */
-               if (unrecognized && i == VECTOR_SIZE(mount_options)) {
+               if (unrecognized && i == ARRAY_SIZE(mount_options)) {
                        // Add it to strflags, to pass on to kernel
                        i = *unrecognized ? strlen(*unrecognized) : 0;
                        *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
@@ -158,11 +189,11 @@ static int parse_mount_options(char *options, char **unrecognized)
                        strcpy((*unrecognized)+i, options);
                }
 
-               // Advance to next option, or finish
-               if (comma) {
-                       *comma = ',';
-                       options = ++comma;
-               } else break;
+               if (!comma)
+                       break;
+               // Advance to next option
+               *comma = ',';
+               options = ++comma;
        }
 
        return flags;
@@ -172,26 +203,27 @@ 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 filesystems[2][sizeof("/proc/filesystems")] = {
+               "/etc/filesystems",
+               "/proc/filesystems",
+       };
+       char *fs, *buf;
        llist_t *list = 0;
        int i;
        FILE *f;
 
-       for (i = 0; filesystems[i]; i++) {
+       for (i = 0; i < 2; i++) {
                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 ((buf = xmalloc_getline(f)) != 0) {
+                       if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
+                               continue;
+                       fs = skip_whitespace(buf);
+                       if (*fs=='#' || *fs=='*' || !*fs) continue;
 
-                       while (isspace(*fs)) fs++;
-                       if (*fs=='#' || *fs=='*') continue;
-                       if (!*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);
        }
@@ -199,7 +231,7 @@ static llist_t *get_block_backed_filesystems(void)
        return list;
 }
 
-llist_t *fslist = 0;
+static llist_t *fslist;
 
 #if ENABLE_FEATURE_CLEAN_UP
 static void delete_block_backed_filesystems(void)
@@ -233,8 +265,9 @@ static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
                                vfsflags, filteropts);
                if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
                        break;
-               bb_error_msg("%s is write-protected, mounting read-only",
-                               mp->mnt_fsname);
+               if (!(vfsflags & MS_SILENT))
+                       bb_error_msg("%s is write-protected, mounting read-only",
+                                               mp->mnt_fsname);
                vfsflags |= MS_RDONLY;
        }
 
@@ -245,14 +278,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:
+ 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
 
@@ -271,7 +306,7 @@ mtab:
                fsname = 0;
                if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
                        mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
-                       mp->mnt_type = "bind";
+                       mp->mnt_type = (char*)"bind";
                }
                mp->mnt_freq = mp->mnt_passno = 0;
 
@@ -284,7 +319,7 @@ mtab:
                        free(fsname);
                }
        }
-
+ ret:
        return rc;
 }
 
@@ -407,7 +442,7 @@ struct ppathcnf {
        short pc_name_max;
        short pc_path_max;
        short pc_pipe_buf;
-       u_char pc_vdisable;
+       uint8_t pc_vdisable;
        char pc_xxx;
        short pc_mask[2];
 };
@@ -527,14 +562,12 @@ static const struct {
 static char *nfs_strerror(int status)
 {
        int i;
-       static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3];
 
        for (i = 0; nfs_errtbl[i].stat != -1; i++) {
                if (nfs_errtbl[i].stat == status)
                        return strerror(nfs_errtbl[i].errnum);
        }
-       sprintf(buf, "unknown nfs status return value: %d", status);
-       return buf;
+       return xasprintf("unknown nfs status return value: %d", status);
 }
 
 static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
@@ -607,11 +640,7 @@ static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
 
 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
 
-/*
- * nfs_mount_version according to the sources seen at compile time.
- */
-static int nfs_mount_version;
-static int kernel_version;
+static smalluint nfs_mount_version;
 
 /*
  * Unfortunately, the kernel prints annoying console messages
@@ -627,7 +656,9 @@ static int kernel_version;
 static void
 find_kernel_nfs_mount_version(void)
 {
-       if (kernel_version)
+       int kernel_version;
+
+       if (nfs_mount_version)
                return;
 
        nfs_mount_version = 4; /* default */
@@ -644,54 +675,56 @@ find_kernel_nfs_mount_version(void)
        }
 }
 
-static struct pmap *
-get_mountport(struct sockaddr_in *server_addr,
+static void 
+get_mountport(struct pmap *pm_mnt,
+       struct sockaddr_in *server_addr,
        long unsigned prog,
        long unsigned version,
        long unsigned proto,
        long unsigned port)
 {
        struct pmaplist *pmap;
-       static struct pmap p = {0, 0, 0, 0};
 
        server_addr->sin_port = PMAPPORT;
+/* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
+ * I understand it like "IPv6 for this is not 100% ready" */
        pmap = pmap_getmaps(server_addr);
 
        if (version > MAX_NFSPROT)
                version = MAX_NFSPROT;
        if (!prog)
                prog = MOUNTPROG;
-       p.pm_prog = prog;
-       p.pm_vers = version;
-       p.pm_prot = proto;
-       p.pm_port = port;
+       pm_mnt->pm_prog = prog;
+       pm_mnt->pm_vers = version;
+       pm_mnt->pm_prot = proto;
+       pm_mnt->pm_port = port;
 
        while (pmap) {
                if (pmap->pml_map.pm_prog != prog)
                        goto next;
-               if (!version && p.pm_vers > pmap->pml_map.pm_vers)
+               if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
                        goto next;
                if (version > 2 && pmap->pml_map.pm_vers != version)
                        goto next;
                if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
                        goto next;
                if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
-                   (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
+                   (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto) ||
                    (port && pmap->pml_map.pm_port != port))
                        goto next;
-               memcpy(&p, &pmap->pml_map, sizeof(p));
-next:
+               memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
+ next:
                pmap = pmap->pml_next;
        }
-       if (!p.pm_vers)
-               p.pm_vers = MOUNTVERS;
-       if (!p.pm_port)
-               p.pm_port = MOUNTPORT;
-       if (!p.pm_prot)
-               p.pm_prot = IPPROTO_TCP;
-       return &p;
+       if (!pm_mnt->pm_vers)
+               pm_mnt->pm_vers = MOUNTVERS;
+       if (!pm_mnt->pm_port)
+               pm_mnt->pm_port = MOUNTPORT;
+       if (!pm_mnt->pm_prot)
+               pm_mnt->pm_prot = IPPROTO_TCP;
 }
 
+#if BB_MMU
 static int daemonize(void)
 {
        int fd;
@@ -705,12 +738,15 @@ static int daemonize(void)
        dup2(fd, 0);
        dup2(fd, 1);
        dup2(fd, 2);
-       if (fd > 2) close(fd);
+       while (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;
 }
+#else
+static inline int daemonize(void) { return -ENOSYS; }
+#endif
 
 // TODO
 static inline int we_saw_this_host_before(const char *hostname)
@@ -753,7 +789,11 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
        int port;
        int mountport;
        int proto;
-       int bg;
+#if BB_MMU
+       int bg = 0;
+#else
+       enum { bg = 0 };
+#endif
        int soft;
        int intr;
        int posix;
@@ -834,7 +874,6 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
        data.acdirmax   = 60;
        data.namlen     = NAME_MAX;
 
-       bg = 0;
        soft = 0;
        intr = 0;
        posix = 0;
@@ -852,103 +891,171 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
        nfsvers = 0;
 
        /* parse options */
-
-       for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
+       if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
                char *opteq = strchr(opt, '=');
                if (opteq) {
-                       int val = atoi(opteq + 1);
+                       static const char options[] ALIGN1 =
+                               /* 0 */ "rsize\0"
+                               /* 1 */ "wsize\0"
+                               /* 2 */ "timeo\0"
+                               /* 3 */ "retrans\0"
+                               /* 4 */ "acregmin\0"
+                               /* 5 */ "acregmax\0"
+                               /* 6 */ "acdirmin\0"
+                               /* 7 */ "acdirmax\0"
+                               /* 8 */ "actimeo\0"
+                               /* 9 */ "retry\0"
+                               /* 10 */ "port\0"
+                               /* 11 */ "mountport\0"
+                               /* 12 */ "mounthost\0"
+                               /* 13 */ "mountprog\0"
+                               /* 14 */ "mountvers\0"
+                               /* 15 */ "nfsprog\0"
+                               /* 16 */ "nfsvers\0"
+                               /* 17 */ "vers\0"
+                               /* 18 */ "proto\0"
+                               /* 19 */ "namlen\0"
+                               /* 20 */ "addr\0";
+                       int val = xatoi_u(opteq + 1);
                        *opteq = '\0';
-                       if (!strcmp(opt, "rsize"))
+                       switch (index_in_strings(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 {
+                       static const char options[] ALIGN1 =
+                               "bg\0"
+                               "fg\0"
+                               "soft\0"
+                               "hard\0"
+                               "intr\0"
+                               "posix\0"
+                               "cto\0"
+                               "ac\0"
+                               "tcp\0"
+                               "udp\0"
+                               "lock\0";
                        int val = 1;
                        if (!strncmp(opt, "no", 2)) {
                                val = 0;
                                opt += 2;
                        }
-                       if (!strcmp(opt, "bg"))
+                       switch (index_in_strings(options, opt)) {
+                       case 0: // "bg"
+#if BB_MMU
                                bg = val;
-                       else if (!strcmp(opt, "fg"))
+#endif
+                               break;
+                       case 1: // "fg"
+#if BB_MMU
                                bg = !val;
-                       else if (!strcmp(opt, "soft"))
+#endif
+                               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;
                        }
@@ -990,7 +1097,7 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
         * give up immediately, to avoid the initial timeout.
         */
        if (bg && we_saw_this_host_before(hostname)) {
-               daemonized = daemonize(); /* parent or error */
+               daemonized = daemonize();
                if (daemonized <= 0) { /* parent or error */
                        retval = -daemonized;
                        goto ret;
@@ -1035,7 +1142,7 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
        {
                struct timeval total_timeout;
                struct timeval retry_timeout;
-               struct pmap* pm_mnt;
+               struct pmap pm_mnt;
                time_t t;
                time_t prevt;
                time_t timeout;
@@ -1052,36 +1159,36 @@ retry:
                if (t - prevt < 30)
                        sleep(30);
 
-               pm_mnt = get_mountport(&mount_server_addr,
+               get_mountport(&pm_mnt, &mount_server_addr,
                                mountprog,
                                mountvers,
                                proto,
                                mountport);
-               nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
+               nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
 
                /* contact the mount daemon via TCP */
-               mount_server_addr.sin_port = htons(pm_mnt->pm_port);
+               mount_server_addr.sin_port = htons(pm_mnt.pm_port);
                msock = RPC_ANYSOCK;
 
-               switch (pm_mnt->pm_prot) {
+               switch (pm_mnt.pm_prot) {
                case IPPROTO_UDP:
                        mclient = clntudp_create(&mount_server_addr,
-                                                pm_mnt->pm_prog,
-                                                pm_mnt->pm_vers,
+                                                pm_mnt.pm_prog,
+                                                pm_mnt.pm_vers,
                                                 retry_timeout,
                                                 &msock);
                        if (mclient)
                                break;
-                       mount_server_addr.sin_port = htons(pm_mnt->pm_port);
+                       mount_server_addr.sin_port = htons(pm_mnt.pm_port);
                        msock = RPC_ANYSOCK;
                case IPPROTO_TCP:
                        mclient = clnttcp_create(&mount_server_addr,
-                                                pm_mnt->pm_prog,
-                                                pm_mnt->pm_vers,
+                                                pm_mnt.pm_prog,
+                                                pm_mnt.pm_vers,
                                                 &msock, 0, 0);
                        break;
                default:
-                       mclient = 0;
+                       mclient = NULL;
                }
                if (!mclient) {
                        if (!daemonized && prevt == 0)
@@ -1096,7 +1203,7 @@ retry:
                         */
                        memset(&status, 0, sizeof(status));
 
-                       if (pm_mnt->pm_vers == 3)
+                       if (pm_mnt.pm_vers == 3)
                                clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
                                              (xdrproc_t) xdr_dirpath,
                                              (caddr_t) &pathname,
@@ -1122,8 +1229,9 @@ retry:
                                error_msg_rpc(clnt_sperror(mclient, " "));
                        auth_destroy(mclient->cl_auth);
                        clnt_destroy(mclient);
-                       mclient = 0;
+                       mclient = NULL;
                        close(msock);
+                       msock = -1;
                }
 
                /* Timeout. We are going to retry... maybe */
@@ -1219,6 +1327,7 @@ prepare_kernel_data:
        auth_destroy(mclient->cl_auth);
        clnt_destroy(mclient);
        close(msock);
+       msock = -1;
 
        if (bg) {
                /* We must wait until mount directory is available */
@@ -1228,6 +1337,7 @@ prepare_kernel_data:
                        if (!daemonized) {
                                daemonized = daemonize();
                                if (daemonized <= 0) { /* parent or error */
+       // FIXME: parent doesn't close fsock - ??!
                                        retval = -daemonized;
                                        goto ret;
                                }
@@ -1241,20 +1351,20 @@ prepare_kernel_data:
 
 do_mount: /* perform actual mount */
 
-       mp->mnt_type = "nfs";
+       mp->mnt_type = (char*)"nfs";
        retval = mount_it_now(mp, vfsflags, (char*)&data);
        goto ret;
 
 fail:  /* abort */
 
-       if (msock != -1) {
+       if (msock >= 0) {
                if (mclient) {
                        auth_destroy(mclient->cl_auth);
                        clnt_destroy(mclient);
                }
                close(msock);
        }
-       if (fsock != -1)
+       if (fsock >= 0)
                close(fsock);
 
 ret:
@@ -1285,16 +1395,40 @@ static int singlemount(struct mntent *mp, int ignore_busy)
 
        // Treat fstype "auto" as unspecified.
 
-       if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
+       if (mp->mnt_type && strcmp(mp->mnt_type,"auto") == 0)
+               mp->mnt_type = 0;
+
+       // Might this be a virtual filesystem?
+
+       if (ENABLE_FEATURE_MOUNT_HELPERS
+        && (strchr(mp->mnt_fsname,'#'))
+       ) {
+               char *s, *p, *args[35];
+               int n = 0;
+               for (s = p = mp->mnt_fsname; *s && n < 35-3; ++s) {
+                       if (s[0] == '#' && s[1] != '#') {
+                               *s = '\0';
+                               args[n++] = p;
+                               p = s + 1;
+                       }
+               }
+               args[n++] = p;
+               args[n++] = mp->mnt_dir;
+               args[n] = NULL;
+               rc = wait4pid(xspawn(args));
+               goto report_error;
+       }
 
        // Might this be an CIFS filesystem?
 
-       if (ENABLE_FEATURE_MOUNT_CIFS &&
-               (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) &&
-               (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')))
-       {
-               struct hostent *he;
-               char ip[32], *s;
+       if (ENABLE_FEATURE_MOUNT_CIFS
+        && (!mp->mnt_type || strcmp(mp->mnt_type,"cifs") == 0)
+        && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')
+        && mp->mnt_fsname[0]==mp->mnt_fsname[1]
+       ) {
+               len_and_sockaddr *lsa;
+               char *ip, *dotted;
+               char *s;
 
                rc = 1;
                // Replace '/' with '\' and verify that unc points to "//server/share".
@@ -1305,60 +1439,65 @@ static int singlemount(struct mntent *mp, int ignore_busy)
                // get server IP
 
                s = strrchr(mp->mnt_fsname, '\\');
-               if (s == mp->mnt_fsname+1) goto report_error;
-               *s = 0;
-               he = gethostbyname(mp->mnt_fsname+2);
+               if (s <= mp->mnt_fsname+1) goto report_error;
+               *s = '\0';
+               lsa = host2sockaddr(mp->mnt_fsname+2, 0);
                *s = '\\';
-               if (!he) goto report_error;
+               if (!lsa) goto report_error;
 
-               // Insert ip=... option into string flags.  (NOTE: Add IPv6 support.)
+               // insert ip=... option into string flags.
 
-               sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1],
-                               he->h_addr[2], he->h_addr[3]);
+               dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa);
+               ip = xasprintf("ip=%s", dotted);
                parse_mount_options(ip, &filteropts);
 
                // compose new unc '\\server-ip\share'
+               // (s => slash after hostname)
 
-               mp->mnt_fsname = xasprintf("\\\\%s%s", ip+3,
-                                       strchr(mp->mnt_fsname+2,'\\'));
+               mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
 
                // lock is required
                vfsflags |= MS_MANDLOCK;
 
-               mp->mnt_type = "cifs";
+               mp->mnt_type = (char*)"cifs";
                rc = mount_it_now(mp, vfsflags, filteropts);
-               if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname);
+               if (ENABLE_FEATURE_CLEAN_UP) {
+                       free(mp->mnt_fsname);
+                       free(ip);
+                       free(dotted);
+                       free(lsa);
+               }
                goto report_error;
        }
 
        // Might this be an NFS filesystem?
 
-       if (ENABLE_FEATURE_MOUNT_NFS &&
-               (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
-               strchr(mp->mnt_fsname, ':') != NULL)
-       {
+       if (ENABLE_FEATURE_MOUNT_NFS
+        && (!mp->mnt_type || !strcmp(mp->mnt_type, "nfs"))
+        && strchr(mp->mnt_fsname, ':') != NULL
+       {
                rc = nfsmount(mp, vfsflags, filteropts);
                goto report_error;
        }
 
        // Look at the file.  (Not found isn't a failure for remount, or for
        // a synthetic filesystem like proc or sysfs.)
+       // (We use stat, not lstat, in order to allow
+       // mount symlink_to_file_or_blkdev dir)
 
-       if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
-       {
+       if (!stat(mp->mnt_fsname, &st)
+        && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
+       ) {
                // Do we need to allocate a loopback device for it?
 
                if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
                        loopFile = bb_simplify_path(mp->mnt_fsname);
-                       mp->mnt_fsname = 0;
-                       switch (set_loop(&(mp->mnt_fsname), loopFile, 0)) {
-                       case 0:
-                       case 1:
-                               break;
-                       default:
-                               bb_error_msg( errno == EPERM || errno == EACCES
-                                       ? bb_msg_perm_denied_are_you_root
-                                       : "cannot setup loop device");
+                       mp->mnt_fsname = NULL; /* will receive malloced loop dev name */
+                       if (set_loop(&(mp->mnt_fsname), loopFile, 0) < 0) {
+                               if (errno == EPERM || errno == EACCES)
+                                       bb_error_msg(bb_msg_perm_denied_are_you_root);
+                               else
+                                       bb_perror_msg("cannot setup loop device");
                                return errno;
                        }
 
@@ -1373,10 +1512,9 @@ static int singlemount(struct mntent *mp, int ignore_busy)
 
        if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
                rc = mount_it_now(mp, vfsflags, filteropts);
-
-       // Loop through filesystem types until mount succeeds or we run out
-
        else {
+               // Loop through filesystem types until mount succeeds
+               // or we run out
 
                /* Initialize list of block backed filesystems.  This has to be
                 * done here so that during "mount -a", mounts after /proc shows up
@@ -1405,20 +1543,23 @@ static int singlemount(struct mntent *mp, int ignore_busy)
                }
        }
 
-report_error:
-       if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
+ report_error:
+       if (ENABLE_FEATURE_CLEAN_UP)
+               free(filteropts);
 
-       if (rc && errno == EBUSY && ignore_busy) rc = 0;
+       if (errno == EBUSY && ignore_busy)
+               return 0;
        if (rc < 0)
-               /* perror here sometimes says "mounting ... on ... failed: Success" */
-               bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
-
+               bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
        return rc;
 }
 
 // Parse options, if necessary parse fstab/mtab, and call singlemount for
 // each directory to be mounted.
 
+static const char must_be_root[] ALIGN1 = "you must be root";
+
+int mount_main(int argc, char **argv);
 int mount_main(int argc, char **argv)
 {
        enum { OPT_ALL = 0x10 };
@@ -1428,15 +1569,17 @@ int mount_main(int argc, char **argv)
        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;
@@ -1444,7 +1587,7 @@ int mount_main(int argc, char **argv)
 
        // Parse remaining options
 
-       opt = bb_getopt_ulflags(argc, argv, "o:t:rwanfvs", &opt_o, &fstype);
+       opt = getopt32(argv, "o:t:rwanfvsi", &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
@@ -1454,6 +1597,7 @@ int mount_main(int argc, char **argv)
        if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
        //if (opt & 0x80) // -v: verbose (ignore)
        //if (opt & 0x100) // -s: sloppy (ignore)
+       //if (opt & 0x200) // -i: don't call mount.<fstype> (ignore)
        argv += optind;
        argc -= optind;
 
@@ -1467,13 +1611,14 @@ 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,
-                                                               sizeof(bb_common_bufsiz1)))
+                       while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
+                                                               sizeof(getmntent_buf)))
                        {
-                               // Don't show rootfs.
-                               if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
+                               // Don't show rootfs. FIXME: why??
+                               // util-linux 2.12a happily shows rootfs...
+                               //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
 
                                if (!fstype || !strcmp(mtpair->mnt_type, fstype))
                                        printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
@@ -1490,6 +1635,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;
@@ -1499,23 +1646,26 @@ int mount_main(int argc, char **argv)
        }
 
        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.
 
-       if (ENABLE_FEATURE_MOUNT_FLAGS &&
-                       (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
-       {
+       if (ENABLE_FEATURE_MOUNT_FLAGS
+        && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
+       {
                rc = mount("", argv[0], "", i, "");
-               if (rc) bb_perror_msg_and_die("%s", argv[0]);
+               if (rc) bb_simple_perror_msg_and_die(argv[0]);
                goto clean_up;
        }
 
        // Open either fstab or mtab
 
-       if (i & 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);
 
@@ -1527,9 +1677,9 @@ int mount_main(int argc, char **argv)
 
                // Get next fstab entry
 
-               if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
-                                       + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
-                               sizeof(bb_common_bufsiz1)/2))
+               if (!getmntent_r(fstab, mtcur, getmntent_buf
+                                       + (mtcur==mtpair ? sizeof(getmntent_buf)/2 : 0),
+                               sizeof(getmntent_buf)/2))
                {
                        // Were we looking for something specific?
 
@@ -1541,11 +1691,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);
                        }
@@ -1560,10 +1716,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.
@@ -1573,21 +1729,29 @@ 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;
+                       if (fstype && match_fstype(mtcur, 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.
 
+                       // NFS mounts want this to be xrealloc-able
+                       mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
                        if (singlemount(mtcur, 1)) {
                                /* Count number of failed mounts */
                                rc++;
                        }
+                       free(mtcur->mnt_opts);
                }
        }
        if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);