freeramdisk: make NOEXEC
[oweals/busybox.git] / util-linux / mount.c
index 42962b85920c6241ee2b5385da09ddbf9a8069d2..4d5c2243a0e9acbba2974cf89b8f59375d408446 100644 (file)
 //
 
 //config:config MOUNT
-//config:      bool "mount"
+//config:      bool "mount (30 kb)"
 //config:      default y
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        All files and filesystems in Unix are arranged into one big directory
-//config:        tree. The 'mount' utility is used to graft a filesystem onto a
-//config:        particular part of the tree. A filesystem can either live on a block
-//config:        device, or it can be accessible over the network, as is the case with
-//config:        NFS filesystems. Most people using BusyBox will also want to enable
-//config:        the 'mount' utility.
+//config:      All files and filesystems in Unix are arranged into one big directory
+//config:      tree. The 'mount' utility is used to graft a filesystem onto a
+//config:      particular part of the tree. A filesystem can either live on a block
+//config:      device, or it can be accessible over the network, as is the case with
+//config:      NFS filesystems.
 //config:
 //config:config FEATURE_MOUNT_FAKE
-//config:      bool "Support option -f"
+//config:      bool "Support -f (fake mount)"
 //config:      default y
 //config:      depends on MOUNT
 //config:      help
-//config:        Enable support for faking a file system mount.
+//config:      Enable support for faking a file system mount.
 //config:
 //config:config FEATURE_MOUNT_VERBOSE
-//config:      bool "Support option -v"
+//config:      bool "Support -v (verbose)"
 //config:      default y
 //config:      depends on MOUNT
 //config:      help
-//config:        Enable multi-level -v[vv...] verbose messages. Useful if you
-//config:        debug mount problems and want to see what is exactly passed
-//config:        to the kernel.
+//config:      Enable multi-level -v[vv...] verbose messages. Useful if you
+//config:      debug mount problems and want to see what is exactly passed
+//config:      to the kernel.
 //config:
 //config:config FEATURE_MOUNT_HELPERS
 //config:      bool "Support mount helpers"
 //config:      default n
 //config:      depends on MOUNT
 //config:      help
-//config:        Enable mounting of virtual file systems via external helpers.
-//config:        E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call
-//config:        "obexfs -b00.11.22.33.44.55 /mnt"
-//config:        Also "mount -t sometype [-o opts] fs /mnt" will try
-//config:        "sometype [-o opts] fs /mnt" if simple mount syscall fails.
-//config:        The idea is to use such virtual filesystems in /etc/fstab.
+//config:      Enable mounting of virtual file systems via external helpers.
+//config:      E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call
+//config:      "obexfs -b00.11.22.33.44.55 /mnt"
+//config:      Also "mount -t sometype [-o opts] fs /mnt" will try
+//config:      "sometype [-o opts] fs /mnt" if simple mount syscall fails.
+//config:      The idea is to use such virtual filesystems in /etc/fstab.
 //config:
 //config:config FEATURE_MOUNT_LABEL
 //config:      bool "Support specifying devices by label or UUID"
@@ -63,9 +62,8 @@
 //config:      depends on MOUNT
 //config:      select VOLUMEID
 //config:      help
-//config:        This allows for specifying a device by label or uuid, rather than by
-//config:        name. This feature utilizes the same functionality as blkid/findfs.
-//config:        This also enables label or uuid support for swapon.
+//config:      This allows for specifying a device by label or uuid, rather than by
+//config:      name. This feature utilizes the same functionality as blkid/findfs.
 //config:
 //config:config FEATURE_MOUNT_NFS
 //config:      bool "Support mounting NFS file systems on Linux < 2.6.23"
 //config:      select FEATURE_HAVE_RPC
 //config:      select FEATURE_SYSLOG
 //config:      help
-//config:        Enable mounting of NFS file systems on Linux kernels prior
-//config:        to version 2.6.23. Note that in this case mounting of NFS
-//config:        over IPv6 will not be possible.
+//config:      Enable mounting of NFS file systems on Linux kernels prior
+//config:      to version 2.6.23. Note that in this case mounting of NFS
+//config:      over IPv6 will not be possible.
 //config:
-//config:        Note that this option links in RPC support from libc,
-//config:        which is rather large (~10 kbytes on uclibc).
+//config:      Note that this option links in RPC support from libc,
+//config:      which is rather large (~10 kbytes on uclibc).
 //config:
 //config:config FEATURE_MOUNT_CIFS
 //config:      bool "Support mounting CIFS/SMB file systems"
 //config:      default y
 //config:      depends on MOUNT
 //config:      help
-//config:        Enable support for samba mounts.
+//config:      Enable support for samba mounts.
 //config:
 //config:config FEATURE_MOUNT_FLAGS
 //config:      depends on MOUNT
-//config:      bool "Support lots of -o flags in mount"
+//config:      bool "Support lots of -o flags"
 //config:      default y
 //config:      help
-//config:        Without this, mount only supports ro/rw/remount. With this, it
-//config:        supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime,
-//config:        noatime, diratime, nodiratime, loud, bind, move, shared, slave,
-//config:        private, unbindable, rshared, rslave, rprivate, and runbindable.
+//config:      Without this, mount only supports ro/rw/remount. With this, it
+//config:      supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime,
+//config:      noatime, diratime, nodiratime, loud, bind, move, shared, slave,
+//config:      private, unbindable, rshared, rslave, rprivate, and runbindable.
 //config:
 //config:config FEATURE_MOUNT_FSTAB
 //config:      depends on MOUNT
-//config:      bool "Support /etc/fstab and -a"
+//config:      bool "Support /etc/fstab and -a (mount all)"
 //config:      default y
 //config:      help
-//config:        Support mount all and looking for files in /etc/fstab.
+//config:      Support mount all and looking for files in /etc/fstab.
 //config:
 //config:config FEATURE_MOUNT_OTHERTAB
 //config:      depends on FEATURE_MOUNT_FSTAB
 //config:      bool "Support -T <alt_fstab>"
 //config:      default y
 //config:      help
-//config:        Support mount -T (specifying an alternate fstab)
+//config:      Support mount -T (specifying an alternate fstab)
 
 /* On full-blown systems, requires suid for user mounts.
  * But it's not unthinkable to have it available in non-suid flavor on some systems,
 /* This is just a warning of a common mistake.  Possibly this should be a
  * uclibc faq entry rather than in busybox... */
 # if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
-#  error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support"
+#  warning "You probably need to build uClibc with UCLIBC_HAS_RPC for NFS support"
+   /* not #error, since user may be using e.g. libtirpc instead */
 # endif
 # include <rpc/rpc.h>
 # include <rpc/pmap_prot.h>
@@ -1887,6 +1886,7 @@ static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
 // NB: mp->xxx fields may be trashed on exit
 static int singlemount(struct mntent *mp, int ignore_busy)
 {
+       int loopfd = -1;
        int rc = -1;
        unsigned long vfsflags;
        char *loopFile = NULL, *filteropts = NULL;
@@ -2026,7 +2026,20 @@ static int singlemount(struct mntent *mp, int ignore_busy)
                if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
                        loopFile = bb_simplify_path(mp->mnt_fsname);
                        mp->mnt_fsname = NULL; // will receive malloced loop dev name
-                       if (set_loop(&mp->mnt_fsname, loopFile, 0, /*ro:*/ (vfsflags & MS_RDONLY)) < 0) {
+
+                       // mount always creates AUTOCLEARed loopdevs, so that umounting
+                       // drops them without any code in the userspace.
+                       // This happens since circa linux-2.6.25:
+                       // commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
+                       // Date:    Wed Feb 6 01:36:27 2008 -0800
+                       // Subject: Allow auto-destruction of loop devices
+                       loopfd = set_loop(&mp->mnt_fsname,
+                                       loopFile,
+                                       0,
+                                       ((vfsflags & MS_RDONLY) ? BB_LO_FLAGS_READ_ONLY : 0)
+                                               | BB_LO_FLAGS_AUTOCLEAR
+                       );
+                       if (loopfd < 0) {
                                if (errno == EPERM || errno == EACCES)
                                        bb_error_msg(bb_msg_perm_denied_are_you_root);
                                else
@@ -2074,6 +2087,8 @@ static int singlemount(struct mntent *mp, int ignore_busy)
        }
 
        // If mount failed, clean up loop file (if any).
+       // (Newer kernels which support LO_FLAGS_AUTOCLEAR should not need this,
+       // merely "close(loopfd)" should do it?)
        if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
                del_loop(mp->mnt_fsname);
                if (ENABLE_FEATURE_CLEAN_UP) {
@@ -2086,6 +2101,9 @@ static int singlemount(struct mntent *mp, int ignore_busy)
        if (ENABLE_FEATURE_CLEAN_UP)
                free(filteropts);
 
+       if (loopfd >= 0)
+               close(loopfd);
+
        if (errno == EBUSY && ignore_busy)
                return 0;
        if (errno == ENOENT && (vfsflags & MOUNT_NOFAIL))
@@ -2312,7 +2330,7 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
                                bb_error_msg_and_die(bb_msg_you_must_be_root);
 
                        // Does type match? (NULL matches always)
-                       if (!match_fstype(mtcur, fstype))
+                       if (!fstype_matches(mtcur->mnt_type, fstype))
                                continue;
 
                        // Skip noauto and swap anyway