+ wrap things in B<> to make pod2man happy
[oweals/busybox.git] / mount.c
diff --git a/mount.c b/mount.c
index c3e3bbd7542be5ae722e13db5654f2b98e4a870a..972e5066cbcb2978abdd719fff7da82cf1e1d79e 100644 (file)
--- a/mount.c
+++ b/mount.c
 #include <mntent.h>
 #include <sys/mount.h>
 #include <ctype.h>
-#include <fstab.h>
+#if defined BB_FEATURE_USE_DEVPS_PATCH
+#include <linux/devmtab.h>
+#endif
+
 
 #if defined BB_FEATURE_MOUNT_LOOP
 #include <fcntl.h>
@@ -53,7 +56,7 @@
 #include <linux/loop.h>
 
 
-static int use_loop = 0;
+static int use_loop = FALSE;
 #endif
 
 extern const char mtab_file[]; /* Defined in utility.c */
@@ -80,8 +83,6 @@ static const char mount_usage[] = "\tmount [flags]\n"
        "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n"
        "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n"
        "\tro / rw: Mount for read-only / read-write.\n"
-       "\t"
-
        "There are EVEN MORE flags that are specific to each filesystem.\n"
        "You'll have to see the written documentation for those.\n";
 
@@ -114,13 +115,14 @@ do_mount(char *specialfile, char *dir, char *filesystemtype,
                 char *mtab_opts)
 {
        int status = 0;
+       char *lofile = NULL;
 
 #if defined BB_MTAB
        if (fakeIt == FALSE)
 #endif
        {
 #if defined BB_FEATURE_MOUNT_LOOP
-               if (use_loop) {
+               if (use_loop==TRUE) {
                        int loro = flags & MS_RDONLY;
                        char *lofile = specialfile;
 
@@ -157,7 +159,7 @@ do_mount(char *specialfile, char *dir, char *filesystemtype,
 
        /* Bummer.  mount failed.  Clean up */
 #if defined BB_FEATURE_MOUNT_LOOP
-       if (specialfile != NULL) {
+       if (lofile != NULL) {
                del_loop(specialfile);
        }
 #endif
@@ -166,20 +168,6 @@ do_mount(char *specialfile, char *dir, char *filesystemtype,
 
 
 
-#if defined BB_MTAB
-#define whine_if_fstab_is_missing() {}
-#else
-extern void whine_if_fstab_is_missing()
-{
-       struct stat statBuf;
-
-       if (stat("/etc/fstab", &statBuf) < 0)
-               fprintf(stderr,
-                               "/etc/fstab file missing -- install one to name /dev/root.\n\n");
-}
-#endif
-
-
 /* Seperate standard mount options from the nonstandard string options */
 static void
 parse_mount_options(char *options, unsigned long *flags, char *strflags)
@@ -204,7 +192,7 @@ parse_mount_options(char *options, unsigned long *flags, char *strflags)
                }
 #if defined BB_FEATURE_MOUNT_LOOP
                if (gotone == FALSE && !strcasecmp("loop", options)) {  /* loop device support */
-                       use_loop = 1;
+                       use_loop = TRUE;
                        gotone = TRUE;
                }
 #endif
@@ -229,13 +217,12 @@ parse_mount_options(char *options, unsigned long *flags, char *strflags)
 int
 mount_one(char *blockDevice, char *directory, char *filesystemType,
                  unsigned long flags, char *string_flags, int useMtab, int fakeIt,
-                 char *mtab_opts)
+                 char *mtab_opts, int whineOnErrors)
 {
        int status = 0;
 
-       char buf[255];
-
 #if defined BB_FEATURE_USE_PROCFS
+       char buf[255];
        if (strcmp(filesystemType, "auto") == 0) {
                FILE *f = fopen("/proc/filesystems", "r");
 
@@ -263,6 +250,43 @@ mount_one(char *blockDevice, char *directory, char *filesystemType,
                }
                fclose(f);
        } else
+#endif
+#if defined BB_FEATURE_USE_DEVPS_PATCH
+       if (strcmp(filesystemType, "auto") == 0) {
+               int fd, i, numfilesystems;
+               char device[] = "/dev/mtab";
+               struct k_fstype *fslist;
+
+               /* open device */ 
+               fd = open(device, O_RDONLY);
+               if (fd < 0)
+                       fatalError("open failed for `%s': %s\n", device, strerror (errno));
+
+               /* How many filesystems?  We need to know to allocate enough space */
+               numfilesystems = ioctl (fd, DEVMTAB_COUNT_FILESYSTEMS);
+               if (numfilesystems<0)
+                       fatalError("\nDEVMTAB_COUNT_FILESYSTEMS: %s\n", strerror (errno));
+               fslist = (struct k_fstype *) calloc ( numfilesystems, sizeof(struct k_fstype));
+
+               /* Grab the list of available filesystems */
+               status = ioctl (fd, DEVMTAB_GET_FILESYSTEMS, fslist);
+               if (status<0)
+                       fatalError("\nDEVMTAB_GET_FILESYSTEMS: %s\n", strerror (errno));
+
+               /* Walk the list trying to mount filesystems 
+                * that do not claim to be nodev filesystems */
+               for( i = 0 ; i < numfilesystems ; i++) {
+                       if (fslist[i].mnt_nodev)
+                               continue;
+                       status = do_mount(blockDevice, directory, fslist[i].mnt_type,
+                                                         flags | MS_MGC_VAL, string_flags,
+                                                         useMtab, fakeIt, mtab_opts);
+                       if (status == TRUE)
+                               break;
+               }
+               free( fslist);
+               close(fd);
+       } else
 #endif
        {
                status = do_mount(blockDevice, directory, filesystemType,
@@ -270,9 +294,11 @@ mount_one(char *blockDevice, char *directory, char *filesystemType,
                                                  fakeIt, mtab_opts);
        }
 
-       if (status == FALSE) {
-               fprintf(stderr, "Mounting %s on %s failed: %s\n",
-                               blockDevice, directory, strerror(errno));
+       if (status == FALSE && whineOnErrors == TRUE) {
+               if (whineOnErrors == TRUE) {
+                       fprintf(stderr, "Mounting %s on %s failed: %s\n",
+                                       blockDevice, directory, strerror(errno));
+               }
                return (FALSE);
        }
        return (TRUE);
@@ -292,9 +318,43 @@ extern int mount_main(int argc, char **argv)
        int useMtab = TRUE;
        int i;
 
-       /* Only compiled in if BB_MTAB is not defined */
-       whine_if_fstab_is_missing();
-
+#if defined BB_FEATURE_USE_DEVPS_PATCH
+       if (argc == 1) {
+               int fd, i, numfilesystems;
+               char device[] = "/dev/mtab";
+               struct k_mntent *mntentlist;
+
+               /* open device */ 
+               fd = open(device, O_RDONLY);
+               if (fd < 0)
+                       fatalError("open failed for `%s': %s\n", device, strerror (errno));
+
+               /* How many mounted filesystems?  We need to know to 
+                * allocate enough space for later... */
+               numfilesystems = ioctl (fd, DEVMTAB_COUNT_MOUNTS);
+               if (numfilesystems<0)
+                       fatalError( "\nDEVMTAB_COUNT_MOUNTS: %s\n", strerror (errno));
+               mntentlist = (struct k_mntent *) calloc ( numfilesystems, sizeof(struct k_mntent));
+               
+               /* Grab the list of mounted filesystems */
+               if (ioctl (fd, DEVMTAB_GET_MOUNTS, mntentlist)<0)
+                       fatalError( "\nDEVMTAB_GET_MOUNTS: %s\n", strerror (errno));
+
+               for( i = 0 ; i < numfilesystems ; i++) {
+                       fprintf( stdout, "%s %s %s %s %d %d\n", mntentlist[i].mnt_fsname,
+                                       mntentlist[i].mnt_dir, mntentlist[i].mnt_type, 
+                                       mntentlist[i].mnt_opts, mntentlist[i].mnt_freq, 
+                                       mntentlist[i].mnt_passno);
+               }
+               /* Don't bother to close files or free memory.  Exit 
+                * does that automagically, so we can save a few bytes */
+#if 0
+               free( mntentlist);
+               close(fd);
+#endif
+               exit(TRUE);
+       }
+#else
        if (argc == 1) {
                FILE *mountTable = setmntent(mtab_file, "r");
 
@@ -302,14 +362,9 @@ extern int mount_main(int argc, char **argv)
                        struct mntent *m;
 
                        while ((m = getmntent(mountTable)) != 0) {
-                               struct fstab *fstabItem;
                                char *blockDevice = m->mnt_fsname;
-
-                               /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
                                if (strcmp(blockDevice, "/dev/root") == 0) {
-                                       fstabItem = getfsfile("/");
-                                       if (fstabItem != NULL)
-                                               blockDevice = fstabItem->fs_spec;
+                                       find_real_root_device_name( blockDevice);
                                }
                                printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
                                           m->mnt_type, m->mnt_opts);
@@ -320,7 +375,7 @@ extern int mount_main(int argc, char **argv)
                }
                exit(TRUE);
        }
-
+#endif
 
        /* Parse options */
        i = --argc;
@@ -352,15 +407,16 @@ extern int mount_main(int argc, char **argv)
                                case 'a':
                                        all = TRUE;
                                        break;
-#ifdef BB_MTAB
                                case 'f':
                                        fakeIt = TRUE;
                                        break;
+#ifdef BB_MTAB
                                case 'n':
                                        useMtab = FALSE;
                                        break;
 #endif
                                case 'v':
+                                       break; /* ignore -v */
                                case 'h':
                                case '-':
                                        goto goodbye;
@@ -382,23 +438,32 @@ extern int mount_main(int argc, char **argv)
                struct mntent *m;
                FILE *f = setmntent("/etc/fstab", "r");
 
-               if (f == NULL) {
-                       perror("/etc/fstab");
-                       exit(FALSE);
-               }
+               if (f == NULL)
+                       fatalError( "\nCannot read /etc/fstab: %s\n", strerror (errno));
+
                while ((m = getmntent(f)) != NULL) {
-                       // If the file system isn't noauto, and isn't mounted on /, 
+                       // If the file system isn't noauto, 
                        // and isn't swap or nfs, then mount it
                        if ((!strstr(m->mnt_opts, "noauto")) &&
-                               (m->mnt_dir[1] != '\0') &&
                                (!strstr(m->mnt_type, "swap")) &&
                                (!strstr(m->mnt_type, "nfs"))) {
                                flags = 0;
                                *string_flags = '\0';
                                parse_mount_options(m->mnt_opts, &flags, string_flags);
-                               mount_one(m->mnt_fsname, m->mnt_dir, m->mnt_type,
+                               /* If the directory is /, try to remount
+                                * with the options specified in fstab */
+                               if (m->mnt_dir[0] == '/' && m->mnt_dir[1] == '\0') {
+                                       flags |= MS_REMOUNT;
+                               }
+                               if (mount_one(m->mnt_fsname, m->mnt_dir, m->mnt_type,
                                                  flags, string_flags, useMtab, fakeIt,
-                                                 extra_opts);
+                                                 extra_opts, FALSE)) 
+                               {
+                                       /* Try again, but this time try a remount */
+                                       mount_one(m->mnt_fsname, m->mnt_dir, m->mnt_type,
+                                                         flags|MS_REMOUNT, string_flags, useMtab, fakeIt,
+                                                         extra_opts, TRUE);
+                               }
                        }
                }
                endmntent(f);
@@ -414,7 +479,7 @@ extern int mount_main(int argc, char **argv)
 #endif
                        exit(mount_one(device, directory, filesystemType,
                                                   flags, string_flags, useMtab, fakeIt,
-                                                  extra_opts));
+                                                  extra_opts, TRUE));
                } else {
                        goto goodbye;
                }