+ in the interest of robustness, I added
[oweals/busybox.git] / mount.c
diff --git a/mount.c b/mount.c
index 37f789d3c3b637a9d5651ea2487506bf9d758b4b..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>
@@ -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";
 
@@ -138,7 +139,6 @@ do_mount(char *specialfile, char *dir, char *filesystemtype,
                                fprintf(stderr, "WARNING: loop device is read-only\n");
                                flags &= ~MS_RDONLY;
                        }
-                       use_loop = FALSE;
                }
 #endif
                status =
@@ -221,9 +221,8 @@ mount_one(char *blockDevice, char *directory, char *filesystemType,
 {
        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");
 
@@ -251,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,
@@ -282,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");
 
@@ -292,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);
@@ -310,7 +375,7 @@ extern int mount_main(int argc, char **argv)
                }
                exit(TRUE);
        }
-
+#endif
 
        /* Parse options */
        i = --argc;
@@ -342,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;
@@ -372,10 +438,9 @@ 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 swap or nfs, then mount it