'simple' error message functions by Loic Grenie <loic.grenie@gmail.com>.
[oweals/busybox.git] / util-linux / mount.c
index 0819be4d1c69f0464b952e2b120c1ce23bd3b5be..10ada5426577bd4214cae7cea6588fca5a1cb812 100644 (file)
@@ -18,8 +18,8 @@
    mount_it_now() does the actual mount.
 */
 
-#include "libbb.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() */
-/* 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
 
+#define getmntent_buf bb_common_bufsiz1
+
 
 // Not real flags, but we want to be able to check for this.
 enum {
@@ -63,10 +66,10 @@ enum {
 /* Standard mount options (from -o options or --options), with corresponding
  * flags */
 
-struct {
+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(
@@ -186,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;
@@ -228,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)
@@ -262,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;
        }
 
@@ -558,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)
@@ -638,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
@@ -658,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 */
@@ -675,15 +675,15 @@ 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 *).
@@ -694,37 +694,37 @@ get_mountport(struct sockaddr_in *server_addr,
                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;
@@ -744,6 +744,9 @@ static int daemonize(void)
        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)
@@ -786,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;
@@ -867,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;
@@ -1010,10 +1016,14 @@ static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
                        }
                        switch (index_in_strings(options, opt)) {
                        case 0: // "bg"
+#if BB_MMU
                                bg = val;
+#endif
                                break;
                        case 1: // "fg"
+#if BB_MMU
                                bg = !val;
+#endif
                                break;
                        case 2: // "soft"
                                soft = val;
@@ -1132,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;
@@ -1149,32 +1159,32 @@ 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:
@@ -1193,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,
@@ -1388,6 +1398,27 @@ static int singlemount(struct mntent *mp, int ignore_busy)
        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
@@ -1516,11 +1547,10 @@ static int singlemount(struct mntent *mp, int ignore_busy)
        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)
                bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
-
        return rc;
 }
 
@@ -1557,7 +1587,7 @@ int mount_main(int argc, char **argv)
 
        // Parse remaining options
 
-       opt = getopt32(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
@@ -1567,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;
 
@@ -1582,8 +1613,8 @@ int mount_main(int argc, char **argv)
 
                        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. FIXME: why??
                                // util-linux 2.12a happily shows rootfs...
@@ -1624,7 +1655,7 @@ int mount_main(int argc, char **argv)
         && (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;
        }
 
@@ -1646,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?