just include fcntl.h not sys/fcntl.h
[oweals/busybox.git] / util-linux / mount.c
index c5aec74fa59f9960a02000e7c36e2e52fdf922b3..68f48355539e41ecee669e75938e3fb4bf3b67a8 100644 (file)
@@ -16,7 +16,7 @@
 /* Design notes: There is no spec for this.  Remind me to write one.
 
    mount_main() calls singlemount() which calls mount_it_now().
+
    mount_main() can loop through /etc/fstab for mount -a
    singlemount() can loop through /etc/filesystems for fstype detection.
    mount_it_now() does the actual mount.
@@ -62,7 +62,7 @@ struct {
        {"defaults", 0},
        {"quiet", 0},
 
-       // vfs flags 
+       // vfs flags
 
        {"ro", MS_RDONLY},
        {"rw", ~MS_RDONLY},
@@ -79,7 +79,7 @@ struct {
        {"diratime", ~MS_NODIRATIME},
        {"nodiratime", MS_NODIRATIME},
        {"loud", ~MS_SILENT},
-       
+
        // action flags
 
        {"remount", MS_REMOUNT},
@@ -127,23 +127,23 @@ 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)))
-               {       
+               {
                        // Add it to strflags, to pass on to kernel
                        i = *unrecognized ? strlen(*unrecognized) : 0;
                        *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
-                       
+
                        // Comma separated if it's not the first one
                        if (i) (*unrecognized)[i++] = ',';
                        strcpy((*unrecognized)+i, options);
                }
-               
+
                // Advance to next option, or finish
                if(comma) {
                        *comma = ',';
                        options = ++comma;
                } else break;
        }
-       
+
        return flags;
 }
 
@@ -159,16 +159,16 @@ static llist_t *get_block_backed_filesystems(void)
 
        for(i = 0; filesystems[i]; i++) {
                if(!(f = fopen(filesystems[i], "r"))) 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;
-                       
+
                        list=llist_add_to_end(list,bb_xstrdup(fs));
                }
                if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
@@ -179,28 +179,31 @@ static llist_t *get_block_backed_filesystems(void)
 
 llist_t *fslist = 0;
 
-void delete_block_backed_filesystems(void);
 #if ENABLE_FEATURE_CLEAN_UP
 static void delete_block_backed_filesystems(void)
 {
        llist_free(fslist);
 }
+#else
+void delete_block_backed_filesystems(void);
 #endif
 
 #if ENABLE_FEATURE_MTAB_SUPPORT
 static int useMtab;
+static int fakeIt;
 #else
 #define useMtab 0
+#define fakeIt 0
 #endif
 
 // Perform actual mount of specific filesystem at specific location.
 
-static int mount_it_now(struct mntent *mp, int vfsflags)
+static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
 {
        int rc;
-       char *filteropts = 0;
 
-       parse_mount_options(mp->mnt_opts, &filteropts);
+       if (fakeIt) { return 0; }
+
        // Mount, with fallback to read-only if necessary.
 
        for(;;) {
@@ -213,8 +216,6 @@ static int mount_it_now(struct mntent *mp, int vfsflags)
                vfsflags |= MS_RDONLY;
        }
 
-    free(filteropts);
-
        // Abort entirely if permission denied.
 
        if (rc && errno == EPERM)
@@ -222,7 +223,7 @@ static int mount_it_now(struct mntent *mp, int vfsflags)
 
        /* If the mount was successful, and we're maintaining an old-style
         * mtab file by hand, add the new entry to it now. */
-       
+
        if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
                FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
                int i;
@@ -251,7 +252,7 @@ static int mount_it_now(struct mntent *mp, int vfsflags)
                if (ENABLE_FEATURE_CLEAN_UP)
                        if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
        }
-       
+
        return rc;
 }
 
@@ -261,12 +262,16 @@ static int mount_it_now(struct mntent *mp, int vfsflags)
 
 static int singlemount(struct mntent *mp)
 {
-       int rc = 0, vfsflags;
-       char *loopFile = 0;
+       int rc = 1, vfsflags;
+       char *loopFile = 0, *filteropts = 0;
        llist_t *fl = 0;
        struct stat st;
 
-       vfsflags = parse_mount_options(mp->mnt_opts, 0);
+       vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
+
+       // Treat fstype "auto" as unspecified.
+
+       if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
 
        // Might this be an NFS filesystem?
 
@@ -274,27 +279,28 @@ static int singlemount(struct mntent *mp)
                (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
                strchr(mp->mnt_fsname, ':') != NULL)
        {
-               char *options=0;
-               parse_mount_options(mp->mnt_opts, &options);
-               if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &options, 1)) {
+               if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) {
                        bb_perror_msg("nfsmount failed");
                        return 1;
+               } else {
+                       // Strangely enough, nfsmount() doesn't actually mount() anything.
+                       mp->mnt_type = "nfs";
+                       rc = mount_it_now(mp, vfsflags, filteropts);
+                       if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
+                       
+                       return rc;
                }
-               
-               // Strangely enough, nfsmount() doesn't actually mount() anything.
-
-               else return mount_it_now(mp, vfsflags);
        }
 
-       // Look at the file.  (Not found isn't a failure for remount.)
+       // Look at the file.  (Not found isn't a failure for remount, or for
+       // a synthetic filesystem like proc or sysfs.)
 
        if (lstat(mp->mnt_fsname, &st));
-
-       if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {   
+       else if (!(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 = mp->mnt_fsname;
+                       loopFile = bb_simplify_path(mp->mnt_fsname);
                        mp->mnt_fsname = 0;
                        switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
                                case 0:
@@ -306,7 +312,7 @@ static int singlemount(struct mntent *mp)
                                                : "Couldn't setup loop device");
                                        return errno;
                        }
-                       
+
                // Autodetect bind mounts
 
                } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND;
@@ -316,22 +322,41 @@ static int singlemount(struct mntent *mp)
         * to the actual mount. */
 
        if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
-               rc = mount_it_now(mp, vfsflags);
+               rc = mount_it_now(mp, vfsflags, filteropts);
 
        // Loop through filesystem types until mount succeeds or we run out
 
-       else for (fl = fslist; fl; fl = fl->link) {
-               mp->mnt_type = fl->data;
+       else {
 
-               if (!(rc = mount_it_now(mp,vfsflags))) break;
+               /* Initialize list of block backed filesystems.  This has to be
+                * done here so that during "mount -a", mounts after /proc shows up
+                * can autodetect. */
+
+               if (!fslist) {
+                       fslist = get_block_backed_filesystems();
+                       if (ENABLE_FEATURE_CLEAN_UP && fslist)
+                               atexit(delete_block_backed_filesystems);
+               }
 
-               mp->mnt_type = 0;
+               for (fl = fslist; fl; fl = fl->link) {
+                       mp->mnt_type = fl->data;
+
+                       if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
+
+                       mp->mnt_type = 0;
+               }
        }
 
-       // Mount failed.  Clean up
+       if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
+
+       // If mount failed, clean up loop file (if any).
+
        if (rc && loopFile) {
                del_loop(mp->mnt_fsname);
-               if(ENABLE_FEATURE_CLEAN_UP) free(loopFile);
+               if (ENABLE_FEATURE_CLEAN_UP) {
+                       free(loopFile);
+                       free(mp->mnt_fsname);
+               }
        }
        return rc;
 }
@@ -346,13 +371,13 @@ int mount_main(int argc, char **argv)
        FILE *fstab;
        int i, opt, all = FALSE, rc = 1;
        struct mntent mtpair[2], *mtcur = mtpair;
-       
+
        /* parse long options, like --bind and --move.  Note that -o option
         * and --option are synonymous.  Yes, this means --remount,rw works. */
 
        for (i = opt = 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[opt++] = argv[i];
        }
        argc = opt;
@@ -388,15 +413,11 @@ int mount_main(int argc, char **argv)
                                bb_show_usage();
                }
        }
-       
-       // Ignore type "auto".
-
-       if (fstype && !strcmp(fstype, "auto")) fstype = NULL;
 
        // Three or more non-option arguments?  Die with a usage message.
 
        if (optind-argc>2) bb_show_usage();
-       
+
        // If we have no arguments, show currently mounted filesystems
 
        if (optind == argc) {
@@ -421,11 +442,6 @@ int mount_main(int argc, char **argv)
                }
        }
 
-       /* Initialize list of block backed filesystems */
-
-       fslist = get_block_backed_filesystems();
-       if (ENABLE_FEATURE_CLEAN_UP) atexit(delete_block_backed_filesystems);
-       
        // When we have two arguments, the second is the directory and we can
        // skip looking at fstab entirely.  We can always abspath() the directory
        // argument when we get it.
@@ -440,18 +456,18 @@ int mount_main(int argc, char **argv)
        }
 
        // If we have at least one argument, it's the storage location
-               
+
        if (optind < argc) storage_path = bb_simplify_path(argv[optind]);
-       
+
        // Open either fstab or mtab
 
        if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
                fstabname = (char *)bb_path_mtab_file;  // Again with the evil const.
        else fstabname="/etc/fstab";
-       
+
        if (!(fstab=setmntent(fstabname,"r")))
                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));
@@ -460,8 +476,9 @@ int mount_main(int argc, char **argv)
 
                // Get next fstab entry
 
-               if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1,
-                                       sizeof(bb_common_bufsiz1)))
+               if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
+                                       + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
+                               sizeof(bb_common_bufsiz1)/2))
                {
                        // Were we looking for something specific?
 
@@ -472,7 +489,7 @@ int mount_main(int argc, char **argv)
                                if (!mtnext->mnt_fsname)
                                        bb_error_msg_and_die("Can't find %s in %s",
                                                argv[optind], fstabname);
-                               
+
                                // Mount the last thing we found.
 
                                mtcur = mtnext;
@@ -483,7 +500,7 @@ int mount_main(int argc, char **argv)
                        }
                        break;
                }
-               
+
                /* If we're trying to mount something specific and this isn't it,
                 * skip it.  Note we must match both the exact text in fstab (ala
                 * "proc") or a full path from root */
@@ -501,14 +518,14 @@ int mount_main(int argc, char **argv)
                        // it, and we want the _last_ match.
 
                        mtcur = mtnext;
-               
+
                // If we're mounting all.
 
                } else {
-                       
+
                        // Do we need to match a filesystem type?
                        if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
-                       
+
                        // Skip noauto and swap anyway.
 
                        if (parse_mount_options(mtcur->mnt_opts,0)
@@ -531,9 +548,8 @@ clean_up:
        if (ENABLE_FEATURE_CLEAN_UP) {
                free(storage_path);
                free(cmdopts);
-               free(fstype);
        }
-       
+
        if(rc)
                bb_perror_msg("Mounting %s on %s failed",
                                mtcur->mnt_fsname, mtcur->mnt_dir);