multiplier suffixes are short, store them directly in struct suffix_mult
[oweals/busybox.git] / util-linux / mount.c
index 4069416d9ced7c39a4670a49f9649bd6048a91f4..7ee24ca143241bd2a9b586918d5d5a18718420f0 100644 (file)
@@ -18,7 +18,7 @@
    mount_it_now() does the actual mount.
 */
 
-#include "busybox.h"
+#include "libbb.h"
 #include <mntent.h>
 
 /* Needed for nfs support only... */
 #include <rpc/pmap_clnt.h>
 
 
+#if defined(__dietlibc__)
+/* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
+ * dietlibc-0.30 does not have implementation of getmntent_r() */
+/* OTOH: why we use getmntent_r instead of getmntent? TODO... */
+struct mntent *getmntent_r(FILE* stream, struct mntent* result, char* buffer, int bufsize)
+{
+       /* *** XXX FIXME WARNING: This hack is NOT thread safe. --Sampo */
+       struct mntent* ment = getmntent(stream);
+       memcpy(result, ment, sizeof(struct mntent));
+       return result;
+}
+#endif
+
+
 // Not real flags, but we want to be able to check for this.
 enum {
        MOUNT_USERS  = (1<<28)*ENABLE_DESKTOP,
@@ -50,7 +64,7 @@ enum {
  * flags */
 
 struct {
-       char *name;
+       const char *name;
        long flags;
 } static mount_options[] = {
        // MS_FLAGS set a bit.  ~MS_FLAGS disable that bit.  0 flags are NOPs.
@@ -61,8 +75,9 @@ struct {
 
        USE_FEATURE_MOUNT_FSTAB(
                {"defaults", 0},
-               {"quiet", 0},
+               /* {"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},)
@@ -105,10 +120,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 */
@@ -123,7 +137,7 @@ static void append_mount_options(char **oldopts, char *newopts)
                                 && (p[len]==',' || p[len]==0))
                                        goto skip;
                                p = strchr(p,',');
-                               if(!p) break;
+                               if (!p) break;
                                p++;
                        }
                        p = xasprintf("%s,%.*s", *oldopts, len, newopts);
@@ -153,7 +167,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;
@@ -162,7 +176,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);
@@ -186,17 +200,16 @@ static int parse_mount_options(char *options, char **unrecognized)
 
 static llist_t *get_block_backed_filesystems(void)
 {
-       static const char *const filesystems[] = {
+       static const char filesystems[2][sizeof("/proc/filesystems")] = {
                "/etc/filesystems",
                "/proc/filesystems",
-               0
        };
        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;
 
@@ -261,14 +274,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
 
@@ -287,7 +302,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;
 
@@ -300,7 +315,7 @@ mtab:
                        free(fsname);
                }
        }
-
+ ret:
        return rc;
 }
 
@@ -423,7 +438,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];
 };
@@ -671,6 +686,8 @@ get_mountport(struct sockaddr_in *server_addr,
        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)
@@ -721,7 +738,7 @@ 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(applet_name, LOG_PID, LOG_DAEMON);
        logmode = LOGMODE_SYSLOG;
@@ -868,37 +885,34 @@ 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) {
-                       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
-                       };
+                       static const char options[] =
+                               /* 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';
-                       switch (index_in_str_array(options, opt)) {
+                       switch (index_in_strings(options, opt)) {
                        case 0: // "rsize"
                                data.rsize = val;
                                break;
@@ -977,26 +991,24 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
                        }
                }
                else {
-                       const char *const options[] = {
-                               "bg",
-                               "fg",
-                               "soft",
-                               "hard",
-                               "intr",
-                               "posix",
-                               "cto",
-                               "ac",
-                               "tcp",
-                               "udp",
-                               "lock",
-                               NULL
-                       };
+                       static const char options[] =
+                               "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;
                        }
-                       switch (index_in_str_array(options, opt)) {
+                       switch (index_in_strings(options, opt)) {
                        case 0: // "bg"
                                bg = val;
                                break;
@@ -1326,7 +1338,7 @@ 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;
 
@@ -1370,16 +1382,19 @@ 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 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".
@@ -1390,47 +1405,55 @@ 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, lsa->len);
+               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)) {
@@ -1458,10 +1481,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
@@ -1490,8 +1512,9 @@ 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 (rc < 0)
@@ -1506,6 +1529,7 @@ report_error:
 
 const char must_be_root[] = "you must be root";
 
+int mount_main(int argc, char **argv);
 int mount_main(int argc, char **argv)
 {
        enum { OPT_ALL = 0x10 };
@@ -1562,7 +1586,8 @@ int mount_main(int argc, char **argv)
                                                                sizeof(bb_common_bufsiz1)))
                        {
                                // Don't show rootfs. FIXME: why??
-                               if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
+                               // 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,
@@ -1595,9 +1620,9 @@ int mount_main(int argc, char **argv)
 
        // 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]);
                goto clean_up;
@@ -1674,9 +1699,7 @@ int mount_main(int argc, char **argv)
 
                } else {
                        // Do we need to match a filesystem type?
-                       // TODO: support "-t type1,type2"; "-t notype1,type2"
-
-                       if (fstype && strcmp(mtcur->mnt_type, fstype)) continue;
+                       if (fstype && match_fstype(mtcur, fstype)) continue;
 
                        // Skip noauto and swap anyway.
 
@@ -1691,10 +1714,13 @@ int mount_main(int argc, char **argv)
 
                        // 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);