Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 3 | * Mini mount implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 10 | */ |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 11 | // Design notes: There is no spec for mount. Remind me to write one. |
| 12 | // |
| 13 | // mount_main() calls singlemount() which calls mount_it_now(). |
| 14 | // |
| 15 | // mount_main() can loop through /etc/fstab for mount -a |
| 16 | // singlemount() can loop through /etc/filesystems for fstype detection. |
| 17 | // mount_it_now() does the actual mount. |
| 18 | // |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 19 | |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 20 | //config:config MOUNT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 21 | //config: bool "mount (23 kb)" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 22 | //config: default y |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 23 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 24 | //config: All files and filesystems in Unix are arranged into one big directory |
| 25 | //config: tree. The 'mount' utility is used to graft a filesystem onto a |
| 26 | //config: particular part of the tree. A filesystem can either live on a block |
| 27 | //config: device, or it can be accessible over the network, as is the case with |
Denys Vlasenko | 68b653b | 2017-07-27 10:53:09 +0200 | [diff] [blame] | 28 | //config: NFS filesystems. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 29 | //config: |
| 30 | //config:config FEATURE_MOUNT_FAKE |
Denys Vlasenko | 5b3cbe3 | 2017-07-27 14:45:25 +0200 | [diff] [blame] | 31 | //config: bool "Support -f (fake mount)" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 32 | //config: default y |
| 33 | //config: depends on MOUNT |
| 34 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 35 | //config: Enable support for faking a file system mount. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 36 | //config: |
| 37 | //config:config FEATURE_MOUNT_VERBOSE |
Denys Vlasenko | 5b3cbe3 | 2017-07-27 14:45:25 +0200 | [diff] [blame] | 38 | //config: bool "Support -v (verbose)" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 39 | //config: default y |
| 40 | //config: depends on MOUNT |
| 41 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 42 | //config: Enable multi-level -v[vv...] verbose messages. Useful if you |
| 43 | //config: debug mount problems and want to see what is exactly passed |
| 44 | //config: to the kernel. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 45 | //config: |
| 46 | //config:config FEATURE_MOUNT_HELPERS |
| 47 | //config: bool "Support mount helpers" |
| 48 | //config: default n |
| 49 | //config: depends on MOUNT |
| 50 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 51 | //config: Enable mounting of virtual file systems via external helpers. |
| 52 | //config: E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call |
| 53 | //config: "obexfs -b00.11.22.33.44.55 /mnt" |
| 54 | //config: Also "mount -t sometype [-o opts] fs /mnt" will try |
| 55 | //config: "sometype [-o opts] fs /mnt" if simple mount syscall fails. |
| 56 | //config: The idea is to use such virtual filesystems in /etc/fstab. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 57 | //config: |
| 58 | //config:config FEATURE_MOUNT_LABEL |
| 59 | //config: bool "Support specifying devices by label or UUID" |
| 60 | //config: default y |
| 61 | //config: depends on MOUNT |
| 62 | //config: select VOLUMEID |
| 63 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 64 | //config: This allows for specifying a device by label or uuid, rather than by |
| 65 | //config: name. This feature utilizes the same functionality as blkid/findfs. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 66 | //config: |
| 67 | //config:config FEATURE_MOUNT_NFS |
| 68 | //config: bool "Support mounting NFS file systems on Linux < 2.6.23" |
| 69 | //config: default n |
| 70 | //config: depends on MOUNT |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 71 | //config: select FEATURE_SYSLOG |
| 72 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 73 | //config: Enable mounting of NFS file systems on Linux kernels prior |
| 74 | //config: to version 2.6.23. Note that in this case mounting of NFS |
| 75 | //config: over IPv6 will not be possible. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 76 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 77 | //config: Note that this option links in RPC support from libc, |
| 78 | //config: which is rather large (~10 kbytes on uclibc). |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 79 | //config: |
| 80 | //config:config FEATURE_MOUNT_CIFS |
| 81 | //config: bool "Support mounting CIFS/SMB file systems" |
| 82 | //config: default y |
| 83 | //config: depends on MOUNT |
| 84 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 85 | //config: Enable support for samba mounts. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 86 | //config: |
| 87 | //config:config FEATURE_MOUNT_FLAGS |
| 88 | //config: depends on MOUNT |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 89 | //config: bool "Support lots of -o flags" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 90 | //config: default y |
| 91 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 92 | //config: Without this, mount only supports ro/rw/remount. With this, it |
| 93 | //config: supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime, |
| 94 | //config: noatime, diratime, nodiratime, loud, bind, move, shared, slave, |
| 95 | //config: private, unbindable, rshared, rslave, rprivate, and runbindable. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 96 | //config: |
| 97 | //config:config FEATURE_MOUNT_FSTAB |
| 98 | //config: depends on MOUNT |
Denys Vlasenko | 5b3cbe3 | 2017-07-27 14:45:25 +0200 | [diff] [blame] | 99 | //config: bool "Support /etc/fstab and -a (mount all)" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 100 | //config: default y |
| 101 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 102 | //config: Support mount all and looking for files in /etc/fstab. |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 103 | //config: |
| 104 | //config:config FEATURE_MOUNT_OTHERTAB |
| 105 | //config: depends on FEATURE_MOUNT_FSTAB |
| 106 | //config: bool "Support -T <alt_fstab>" |
| 107 | //config: default y |
| 108 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 109 | //config: Support mount -T (specifying an alternate fstab) |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 110 | |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 111 | /* On full-blown systems, requires suid for user mounts. |
| 112 | * But it's not unthinkable to have it available in non-suid flavor on some systems, |
| 113 | * for viewing mount table. |
| 114 | * Therefore we use BB_SUID_MAYBE instead of BB_SUID_REQUIRE: */ |
| 115 | //applet:IF_MOUNT(APPLET(mount, BB_DIR_BIN, IF_DESKTOP(BB_SUID_MAYBE) IF_NOT_DESKTOP(BB_SUID_DROP))) |
| 116 | |
| 117 | //kbuild:lib-$(CONFIG_MOUNT) += mount.o |
| 118 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 119 | //usage:#define mount_trivial_usage |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 120 | //usage: "[OPTIONS] [-o OPT] DEVICE NODE" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 121 | //usage:#define mount_full_usage "\n\n" |
| 122 | //usage: "Mount a filesystem. Filesystem autodetection requires /proc.\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 123 | //usage: "\n -a Mount all filesystems in fstab" |
| 124 | //usage: IF_FEATURE_MOUNT_FAKE( |
| 125 | //usage: IF_FEATURE_MTAB_SUPPORT( |
| 126 | //usage: "\n -f Update /etc/mtab, but don't mount" |
| 127 | //usage: ) |
| 128 | //usage: IF_NOT_FEATURE_MTAB_SUPPORT( |
| 129 | //usage: "\n -f Dry run" |
| 130 | //usage: ) |
| 131 | //usage: ) |
| 132 | //usage: IF_FEATURE_MOUNT_HELPERS( |
| 133 | //usage: "\n -i Don't run mount helper" |
| 134 | //usage: ) |
| 135 | //usage: IF_FEATURE_MTAB_SUPPORT( |
| 136 | //usage: "\n -n Don't update /etc/mtab" |
| 137 | //usage: ) |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 138 | //usage: IF_FEATURE_MOUNT_VERBOSE( |
| 139 | //usage: "\n -v Verbose" |
| 140 | //usage: ) |
| 141 | ////usage: "\n -s Sloppy (ignored)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 142 | //usage: "\n -r Read-only mount" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 143 | ////usage: "\n -w Read-write mount (default)" |
Karol Lewandowski | b5ebe5f | 2011-11-03 10:02:31 +0100 | [diff] [blame] | 144 | //usage: "\n -t FSTYPE[,...] Filesystem type(s)" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 145 | //usage: IF_FEATURE_MOUNT_OTHERTAB( |
| 146 | //usage: "\n -T FILE Read FILE instead of /etc/fstab" |
| 147 | //usage: ) |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 148 | //usage: "\n -O OPT Mount only filesystems with option OPT (-a only)" |
| 149 | //usage: "\n-o OPT:" |
| 150 | //usage: IF_FEATURE_MOUNT_LOOP( |
| 151 | //usage: "\n loop Ignored (loop devices are autodetected)" |
| 152 | //usage: ) |
| 153 | //usage: IF_FEATURE_MOUNT_FLAGS( |
| 154 | //usage: "\n [a]sync Writes are [a]synchronous" |
| 155 | //usage: "\n [no]atime Disable/enable updates to inode access times" |
| 156 | //usage: "\n [no]diratime Disable/enable atime updates to directories" |
| 157 | //usage: "\n [no]relatime Disable/enable atime updates relative to modification time" |
| 158 | //usage: "\n [no]dev (Dis)allow use of special device files" |
| 159 | //usage: "\n [no]exec (Dis)allow use of executable files" |
| 160 | //usage: "\n [no]suid (Dis)allow set-user-id-root programs" |
| 161 | //usage: "\n [r]shared Convert [recursively] to a shared subtree" |
| 162 | //usage: "\n [r]slave Convert [recursively] to a slave subtree" |
| 163 | //usage: "\n [r]private Convert [recursively] to a private subtree" |
| 164 | //usage: "\n [un]bindable Make mount point [un]able to be bind mounted" |
| 165 | //usage: "\n [r]bind Bind a file or directory [recursively] to another location" |
| 166 | //usage: "\n move Relocate an existing mount point" |
| 167 | //usage: ) |
| 168 | //usage: "\n remount Remount a mounted filesystem, changing flags" |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 169 | //usage: "\n ro Same as -r" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 170 | //usage: "\n" |
| 171 | //usage: "\nThere are filesystem-specific -o flags." |
| 172 | //usage: |
| 173 | //usage:#define mount_example_usage |
| 174 | //usage: "$ mount\n" |
| 175 | //usage: "/dev/hda3 on / type minix (rw)\n" |
| 176 | //usage: "proc on /proc type proc (rw)\n" |
| 177 | //usage: "devpts on /dev/pts type devpts (rw)\n" |
| 178 | //usage: "$ mount /dev/fd0 /mnt -t msdos -o ro\n" |
| 179 | //usage: "$ mount /tmp/diskimage /opt -t ext2 -o loop\n" |
| 180 | //usage: "$ mount cd_image.iso mydir\n" |
| 181 | //usage:#define mount_notes_usage |
| 182 | //usage: "Returns 0 for success, number of failed mounts for -a, or errno for one mount." |
| 183 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | #include <mntent.h> |
Lauri Kasanen | 6cfec7d | 2020-12-21 18:55:59 +0200 | [diff] [blame] | 185 | #if ENABLE_FEATURE_SYSLOG |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 186 | #include <syslog.h> |
Lauri Kasanen | 6cfec7d | 2020-12-21 18:55:59 +0200 | [diff] [blame] | 187 | #endif |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 188 | #include <sys/mount.h> |
Denys Vlasenko | 102ff76 | 2009-11-21 17:14:08 +0100 | [diff] [blame] | 189 | // Grab more as needed from util-linux's mount/mount_constants.h |
| 190 | #ifndef MS_DIRSYNC |
| 191 | # define MS_DIRSYNC (1 << 7) // Directory modifications are synchronous |
| 192 | #endif |
Denys Vlasenko | 34c5115 | 2020-12-06 21:47:24 +0100 | [diff] [blame] | 193 | #ifndef MS_NOSYMFOLLOW |
| 194 | # define MS_NOSYMFOLLOW (1 << 8) |
Vladimir Dronnikov | be168b1 | 2009-10-05 02:18:01 +0200 | [diff] [blame] | 195 | #endif |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 196 | #ifndef MS_BIND |
| 197 | # define MS_BIND (1 << 12) |
| 198 | #endif |
| 199 | #ifndef MS_MOVE |
| 200 | # define MS_MOVE (1 << 13) |
| 201 | #endif |
| 202 | #ifndef MS_RECURSIVE |
| 203 | # define MS_RECURSIVE (1 << 14) |
| 204 | #endif |
| 205 | #ifndef MS_SILENT |
| 206 | # define MS_SILENT (1 << 15) |
| 207 | #endif |
Denys Vlasenko | 102ff76 | 2009-11-21 17:14:08 +0100 | [diff] [blame] | 208 | // The shared subtree stuff, which went in around 2.6.15 |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 209 | #ifndef MS_UNBINDABLE |
| 210 | # define MS_UNBINDABLE (1 << 17) |
| 211 | #endif |
| 212 | #ifndef MS_PRIVATE |
| 213 | # define MS_PRIVATE (1 << 18) |
| 214 | #endif |
| 215 | #ifndef MS_SLAVE |
| 216 | # define MS_SLAVE (1 << 19) |
| 217 | #endif |
| 218 | #ifndef MS_SHARED |
| 219 | # define MS_SHARED (1 << 20) |
| 220 | #endif |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 221 | |
Denys Vlasenko | da49f58 | 2009-07-08 02:58:38 +0200 | [diff] [blame] | 222 | #ifndef MS_RELATIME |
| 223 | # define MS_RELATIME (1 << 21) |
| 224 | #endif |
Denys Vlasenko | 9ad8979 | 2012-06-26 16:09:00 +0200 | [diff] [blame] | 225 | #ifndef MS_STRICTATIME |
| 226 | # define MS_STRICTATIME (1 << 24) |
| 227 | #endif |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 228 | #ifndef MS_LAZYTIME |
| 229 | # define MS_LAZYTIME (1 << 25) |
| 230 | #endif |
Denys Vlasenko | 9ad8979 | 2012-06-26 16:09:00 +0200 | [diff] [blame] | 231 | |
| 232 | /* Any ~MS_FOO value has this bit set: */ |
| 233 | #define BB_MS_INVERTED_VALUE (1u << 31) |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 234 | |
Denys Vlasenko | 102ff76 | 2009-11-21 17:14:08 +0100 | [diff] [blame] | 235 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 236 | #include "common_bufsiz.h" |
Denis Vlasenko | 6aa7696 | 2008-03-18 01:44:52 +0000 | [diff] [blame] | 237 | #if ENABLE_FEATURE_MOUNT_LABEL |
Natanael Copa | 9aff299 | 2009-09-20 04:28:22 +0200 | [diff] [blame] | 238 | # include "volume_id.h" |
| 239 | #else |
| 240 | # define resolve_mount_spec(fsname) ((void)0) |
Denis Vlasenko | 6aa7696 | 2008-03-18 01:44:52 +0000 | [diff] [blame] | 241 | #endif |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 243 | // Needed for nfs support only |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 244 | #include <sys/utsname.h> |
| 245 | #undef TRUE |
| 246 | #undef FALSE |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 247 | #if ENABLE_FEATURE_MOUNT_NFS |
| 248 | /* This is just a warning of a common mistake. Possibly this should be a |
| 249 | * uclibc faq entry rather than in busybox... */ |
| 250 | # if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__) |
Denys Vlasenko | ce55284 | 2017-07-10 14:43:22 +0200 | [diff] [blame] | 251 | # warning "You probably need to build uClibc with UCLIBC_HAS_RPC for NFS support" |
Denys Vlasenko | 4892f3a | 2018-02-13 18:20:28 +0100 | [diff] [blame] | 252 | /* not #error, since user may be using e.g. libtirpc instead. |
| 253 | * This might work: |
| 254 | * CONFIG_EXTRA_CFLAGS="-I/usr/include/tirpc" |
| 255 | * CONFIG_EXTRA_LDLIBS="tirpc" |
| 256 | */ |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 257 | # endif |
| 258 | # include <rpc/rpc.h> |
| 259 | # include <rpc/pmap_prot.h> |
| 260 | # include <rpc/pmap_clnt.h> |
| 261 | #endif |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 262 | |
| 263 | |
Denis Vlasenko | 908d6b7 | 2006-12-18 23:07:42 +0000 | [diff] [blame] | 264 | #if defined(__dietlibc__) |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 265 | // 16.12.2006, Sampo Kellomaki (sampo@iki.fi) |
| 266 | // dietlibc-0.30 does not have implementation of getmntent_r() |
Denis Vlasenko | a0e17f7 | 2008-05-26 01:19:53 +0000 | [diff] [blame] | 267 | static struct mntent *getmntent_r(FILE* stream, struct mntent* result, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 268 | char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM) |
Denis Vlasenko | 908d6b7 | 2006-12-18 23:07:42 +0000 | [diff] [blame] | 269 | { |
Denis Vlasenko | 908d6b7 | 2006-12-18 23:07:42 +0000 | [diff] [blame] | 270 | struct mntent* ment = getmntent(stream); |
Denis Vlasenko | a0e17f7 | 2008-05-26 01:19:53 +0000 | [diff] [blame] | 271 | return memcpy(result, ment, sizeof(*ment)); |
Denis Vlasenko | 908d6b7 | 2006-12-18 23:07:42 +0000 | [diff] [blame] | 272 | } |
| 273 | #endif |
| 274 | |
| 275 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 276 | // Not real flags, but we want to be able to check for this. |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 277 | enum { |
Denys Vlasenko | 911d265 | 2015-12-30 20:11:34 +0100 | [diff] [blame] | 278 | MOUNT_USERS = (1 << 27) * ENABLE_DESKTOP, |
| 279 | MOUNT_NOFAIL = (1 << 28) * ENABLE_DESKTOP, |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 280 | MOUNT_NOAUTO = (1 << 29), |
| 281 | MOUNT_SWAP = (1 << 30), |
Denys Vlasenko | 911d265 | 2015-12-30 20:11:34 +0100 | [diff] [blame] | 282 | MOUNT_FAKEFLAGS = MOUNT_USERS | MOUNT_NOFAIL | MOUNT_NOAUTO | MOUNT_SWAP |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 283 | }; |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 284 | |
| 285 | |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 286 | #define OPTION_STR "o:*t:rwanfvsiO:" IF_FEATURE_MOUNT_OTHERTAB("T:") |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 287 | enum { |
| 288 | OPT_o = (1 << 0), |
| 289 | OPT_t = (1 << 1), |
| 290 | OPT_r = (1 << 2), |
| 291 | OPT_w = (1 << 3), |
| 292 | OPT_a = (1 << 4), |
| 293 | OPT_n = (1 << 5), |
| 294 | OPT_f = (1 << 6), |
| 295 | OPT_v = (1 << 7), |
| 296 | OPT_s = (1 << 8), |
| 297 | OPT_i = (1 << 9), |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 298 | OPT_O = (1 << 10), |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 299 | OPT_T = (1 << 11), |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | #if ENABLE_FEATURE_MTAB_SUPPORT |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 303 | #define USE_MTAB (!(option_mask32 & OPT_n)) |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 304 | #else |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 305 | #define USE_MTAB 0 |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 306 | #endif |
| 307 | |
| 308 | #if ENABLE_FEATURE_MOUNT_FAKE |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 309 | #define FAKE_IT (option_mask32 & OPT_f) |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 310 | #else |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 311 | #define FAKE_IT 0 |
| 312 | #endif |
| 313 | |
| 314 | #if ENABLE_FEATURE_MOUNT_HELPERS |
| 315 | #define HELPERS_ALLOWED (!(option_mask32 & OPT_i)) |
| 316 | #else |
| 317 | #define HELPERS_ALLOWED 0 |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 318 | #endif |
| 319 | |
| 320 | |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 321 | // TODO: more "user" flag compatibility. |
| 322 | // "user" option (from mount manpage): |
| 323 | // Only the user that mounted a filesystem can unmount it again. |
| 324 | // If any user should be able to unmount, then use users instead of user |
| 325 | // in the fstab line. The owner option is similar to the user option, |
| 326 | // with the restriction that the user must be the owner of the special file. |
| 327 | // This may be useful e.g. for /dev/fd if a login script makes |
| 328 | // the console user owner of this device. |
Rob Landley | 3ba7bd1 | 2006-08-09 19:51:13 +0000 | [diff] [blame] | 329 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 330 | // Standard mount options (from -o options or --options), |
| 331 | // with corresponding flags |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 332 | static const int32_t mount_options[] ALIGN4 = { |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 333 | // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs. |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 334 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 335 | IF_FEATURE_MOUNT_LOOP( |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 336 | /* "loop" */ 0, |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 337 | ) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 338 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 339 | IF_FEATURE_MOUNT_FSTAB( |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 340 | /* "defaults" */ 0, |
| 341 | /* "quiet" 0 - do not filter out, vfat wants to see it */ |
| 342 | /* "noauto" */ MOUNT_NOAUTO, |
| 343 | /* "sw" */ MOUNT_SWAP, |
| 344 | /* "swap" */ MOUNT_SWAP, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 345 | IF_DESKTOP(/* "user" */ MOUNT_USERS,) |
| 346 | IF_DESKTOP(/* "users" */ MOUNT_USERS,) |
Denys Vlasenko | 911d265 | 2015-12-30 20:11:34 +0100 | [diff] [blame] | 347 | IF_DESKTOP(/* "nofail" */ MOUNT_NOFAIL,) |
Denis Vlasenko | 8c638cb | 2008-01-29 09:31:09 +0000 | [diff] [blame] | 348 | /* "_netdev" */ 0, |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 349 | IF_DESKTOP(/* "comment=" */ 0,) /* systemd uses this in fstab */ |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 350 | ) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 351 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 352 | IF_FEATURE_MOUNT_FLAGS( |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 353 | // vfs flags |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 354 | /* "nosuid" */ MS_NOSUID, |
| 355 | /* "suid" */ ~MS_NOSUID, |
| 356 | /* "dev" */ ~MS_NODEV, |
| 357 | /* "nodev" */ MS_NODEV, |
| 358 | /* "exec" */ ~MS_NOEXEC, |
| 359 | /* "noexec" */ MS_NOEXEC, |
| 360 | /* "sync" */ MS_SYNCHRONOUS, |
Denis Vlasenko | c9ca0a3 | 2008-02-18 11:08:33 +0000 | [diff] [blame] | 361 | /* "dirsync" */ MS_DIRSYNC, |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 362 | /* "async" */ ~MS_SYNCHRONOUS, |
| 363 | /* "atime" */ ~MS_NOATIME, |
| 364 | /* "noatime" */ MS_NOATIME, |
| 365 | /* "diratime" */ ~MS_NODIRATIME, |
| 366 | /* "nodiratime" */ MS_NODIRATIME, |
Bernhard Reutner-Fischer | fb5902c | 2008-08-06 18:14:38 +0000 | [diff] [blame] | 367 | /* "relatime" */ MS_RELATIME, |
| 368 | /* "norelatime" */ ~MS_RELATIME, |
Denys Vlasenko | 9ad8979 | 2012-06-26 16:09:00 +0200 | [diff] [blame] | 369 | /* "strictatime" */ MS_STRICTATIME, |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 370 | /* "nostrictatime"*/ ~MS_STRICTATIME, |
| 371 | /* "lazytime" */ MS_LAZYTIME, |
| 372 | /* "nolazytime" */ ~MS_LAZYTIME, |
Denys Vlasenko | 34c5115 | 2020-12-06 21:47:24 +0100 | [diff] [blame] | 373 | /* "nosymfollow" */ MS_NOSYMFOLLOW, |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 374 | /* "mand" */ MS_MANDLOCK, |
| 375 | /* "nomand" */ ~MS_MANDLOCK, |
| 376 | /* "loud" */ ~MS_SILENT, |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 377 | |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 378 | // action flags |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 379 | /* "rbind" */ MS_BIND|MS_RECURSIVE, |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 380 | /* "bind" */ MS_BIND, |
| 381 | /* "move" */ MS_MOVE, |
| 382 | /* "shared" */ MS_SHARED, |
| 383 | /* "slave" */ MS_SLAVE, |
| 384 | /* "private" */ MS_PRIVATE, |
| 385 | /* "unbindable" */ MS_UNBINDABLE, |
| 386 | /* "rshared" */ MS_SHARED|MS_RECURSIVE, |
| 387 | /* "rslave" */ MS_SLAVE|MS_RECURSIVE, |
Roman Borisov | d3679d2 | 2011-03-23 11:20:25 +0300 | [diff] [blame] | 388 | /* "rprivate" */ MS_PRIVATE|MS_RECURSIVE, |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 389 | /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE, |
Rob Landley | e3781b7 | 2006-08-08 01:39:49 +0000 | [diff] [blame] | 390 | ) |
| 391 | |
| 392 | // Always understood. |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 393 | /* "ro" */ MS_RDONLY, // vfs flag |
| 394 | /* "rw" */ ~MS_RDONLY, // vfs flag |
| 395 | /* "remount" */ MS_REMOUNT // action flag |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 396 | }; |
| 397 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 398 | static const char mount_option_str[] ALIGN1 = |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 399 | IF_FEATURE_MOUNT_LOOP( |
Denis Vlasenko | 3f8f4b2 | 2008-12-10 11:28:30 +0000 | [diff] [blame] | 400 | "loop\0" |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 401 | ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 402 | IF_FEATURE_MOUNT_FSTAB( |
Denis Vlasenko | 3f8f4b2 | 2008-12-10 11:28:30 +0000 | [diff] [blame] | 403 | "defaults\0" |
| 404 | // "quiet\0" - do not filter out, vfat wants to see it |
| 405 | "noauto\0" |
| 406 | "sw\0" |
| 407 | "swap\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 408 | IF_DESKTOP("user\0") |
| 409 | IF_DESKTOP("users\0") |
Denys Vlasenko | 911d265 | 2015-12-30 20:11:34 +0100 | [diff] [blame] | 410 | IF_DESKTOP("nofail\0") |
Denis Vlasenko | 3f8f4b2 | 2008-12-10 11:28:30 +0000 | [diff] [blame] | 411 | "_netdev\0" |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 412 | IF_DESKTOP("comment=\0") /* systemd uses this in fstab */ |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 413 | ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 414 | IF_FEATURE_MOUNT_FLAGS( |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 415 | // vfs flags |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 416 | "nosuid" "\0" |
| 417 | "suid" "\0" |
| 418 | "dev" "\0" |
| 419 | "nodev" "\0" |
| 420 | "exec" "\0" |
| 421 | "noexec" "\0" |
| 422 | "sync" "\0" |
| 423 | "dirsync" "\0" |
| 424 | "async" "\0" |
| 425 | "atime" "\0" |
| 426 | "noatime" "\0" |
| 427 | "diratime" "\0" |
| 428 | "nodiratime" "\0" |
| 429 | "relatime" "\0" |
| 430 | "norelatime" "\0" |
| 431 | "strictatime" "\0" |
| 432 | "nostrictatime""\0" |
| 433 | "lazytime" "\0" |
| 434 | "nolazytime" "\0" |
Denys Vlasenko | 34c5115 | 2020-12-06 21:47:24 +0100 | [diff] [blame] | 435 | "nosymfollow" "\0" |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 436 | "mand" "\0" |
| 437 | "nomand" "\0" |
| 438 | "loud" "\0" |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 439 | |
| 440 | // action flags |
Denys Vlasenko | 696c38d | 2020-12-06 20:59:41 +0100 | [diff] [blame] | 441 | "rbind\0" |
Denis Vlasenko | 3f8f4b2 | 2008-12-10 11:28:30 +0000 | [diff] [blame] | 442 | "bind\0" |
| 443 | "move\0" |
Roman Borisov | 945fd17 | 2011-02-25 14:50:39 +0300 | [diff] [blame] | 444 | "make-shared\0" |
| 445 | "make-slave\0" |
| 446 | "make-private\0" |
| 447 | "make-unbindable\0" |
| 448 | "make-rshared\0" |
| 449 | "make-rslave\0" |
| 450 | "make-rprivate\0" |
| 451 | "make-runbindable\0" |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 452 | ) |
| 453 | |
| 454 | // Always understood. |
Denis Vlasenko | 3f8f4b2 | 2008-12-10 11:28:30 +0000 | [diff] [blame] | 455 | "ro\0" // vfs flag |
| 456 | "rw\0" // vfs flag |
| 457 | "remount\0" // action flag |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 458 | ; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 459 | |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 460 | |
| 461 | struct globals { |
| 462 | #if ENABLE_FEATURE_MOUNT_NFS |
| 463 | smalluint nfs_mount_version; |
| 464 | #endif |
| 465 | #if ENABLE_FEATURE_MOUNT_VERBOSE |
| 466 | unsigned verbose; |
| 467 | #endif |
| 468 | llist_t *fslist; |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 469 | char getmntent_buf[1]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 470 | } FIX_ALIASING; |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 471 | enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) }; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 472 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 473 | #define nfs_mount_version (G.nfs_mount_version) |
Denis Vlasenko | b413368 | 2008-02-18 13:05:38 +0000 | [diff] [blame] | 474 | #if ENABLE_FEATURE_MOUNT_VERBOSE |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 475 | #define verbose (G.verbose ) |
Denis Vlasenko | b413368 | 2008-02-18 13:05:38 +0000 | [diff] [blame] | 476 | #else |
| 477 | #define verbose 0 |
| 478 | #endif |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 479 | #define fslist (G.fslist ) |
| 480 | #define getmntent_buf (G.getmntent_buf ) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 481 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 482 | |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 483 | #if ENABLE_FEATURE_MTAB_SUPPORT |
| 484 | /* |
| 485 | * update_mtab_entry_on_move() is used to update entry in case of mount --move. |
| 486 | * we are looking for existing entries mnt_dir which is equal to mnt_fsname of |
| 487 | * input mntent and replace it by new one. |
| 488 | */ |
| 489 | static void FAST_FUNC update_mtab_entry_on_move(const struct mntent *mp) |
| 490 | { |
| 491 | struct mntent *entries, *m; |
| 492 | int i, count; |
| 493 | FILE *mountTable; |
| 494 | |
| 495 | mountTable = setmntent(bb_path_mtab_file, "r"); |
| 496 | if (!mountTable) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 497 | bb_simple_perror_msg(bb_path_mtab_file); |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | entries = NULL; |
| 502 | count = 0; |
| 503 | while ((m = getmntent(mountTable)) != NULL) { |
| 504 | entries = xrealloc_vector(entries, 3, count); |
| 505 | entries[count].mnt_fsname = xstrdup(m->mnt_fsname); |
| 506 | entries[count].mnt_dir = xstrdup(m->mnt_dir); |
| 507 | entries[count].mnt_type = xstrdup(m->mnt_type); |
| 508 | entries[count].mnt_opts = xstrdup(m->mnt_opts); |
| 509 | entries[count].mnt_freq = m->mnt_freq; |
| 510 | entries[count].mnt_passno = m->mnt_passno; |
| 511 | count++; |
| 512 | } |
| 513 | endmntent(mountTable); |
| 514 | |
| 515 | mountTable = setmntent(bb_path_mtab_file, "w"); |
| 516 | if (mountTable) { |
| 517 | for (i = 0; i < count; i++) { |
| 518 | if (strcmp(entries[i].mnt_dir, mp->mnt_fsname) != 0) |
| 519 | addmntent(mountTable, &entries[i]); |
| 520 | else |
| 521 | addmntent(mountTable, mp); |
| 522 | } |
| 523 | endmntent(mountTable); |
| 524 | } else if (errno != EROFS) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 525 | bb_simple_perror_msg(bb_path_mtab_file); |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 526 | |
| 527 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 528 | for (i = 0; i < count; i++) { |
| 529 | free(entries[i].mnt_fsname); |
| 530 | free(entries[i].mnt_dir); |
| 531 | free(entries[i].mnt_type); |
| 532 | free(entries[i].mnt_opts); |
| 533 | } |
| 534 | free(entries); |
| 535 | } |
| 536 | } |
| 537 | #endif |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 538 | |
| 539 | #if ENABLE_FEATURE_MOUNT_VERBOSE |
| 540 | static int verbose_mount(const char *source, const char *target, |
| 541 | const char *filesystemtype, |
| 542 | unsigned long mountflags, const void *data) |
| 543 | { |
| 544 | int rc; |
| 545 | |
| 546 | errno = 0; |
| 547 | rc = mount(source, target, filesystemtype, mountflags, data); |
| 548 | if (verbose >= 2) |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 549 | bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d", |
Denis Vlasenko | b413368 | 2008-02-18 13:05:38 +0000 | [diff] [blame] | 550 | source, target, filesystemtype, |
| 551 | mountflags, (char*)data, rc); |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 552 | return rc; |
| 553 | } |
| 554 | #else |
| 555 | #define verbose_mount(...) mount(__VA_ARGS__) |
| 556 | #endif |
| 557 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 558 | // Append mount options to string |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 559 | static void append_mount_options(char **oldopts, const char *newopts) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 560 | { |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 561 | if (*oldopts && **oldopts) { |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 562 | // Do not insert options which are already there |
Denis Vlasenko | c889d2b | 2006-09-17 15:08:12 +0000 | [diff] [blame] | 563 | while (newopts[0]) { |
| 564 | char *p; |
| 565 | int len = strlen(newopts); |
| 566 | p = strchr(newopts, ','); |
| 567 | if (p) len = p - newopts; |
| 568 | p = *oldopts; |
| 569 | while (1) { |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 570 | if (!strncmp(p, newopts, len) |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 571 | && (p[len] == ',' || p[len] == '\0')) |
Denis Vlasenko | c889d2b | 2006-09-17 15:08:12 +0000 | [diff] [blame] | 572 | goto skip; |
| 573 | p = strchr(p,','); |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 574 | if (!p) break; |
Denis Vlasenko | c889d2b | 2006-09-17 15:08:12 +0000 | [diff] [blame] | 575 | p++; |
| 576 | } |
| 577 | p = xasprintf("%s,%.*s", *oldopts, len, newopts); |
| 578 | free(*oldopts); |
| 579 | *oldopts = p; |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 580 | skip: |
Denis Vlasenko | c889d2b | 2006-09-17 15:08:12 +0000 | [diff] [blame] | 581 | newopts += len; |
| 582 | while (newopts[0] == ',') newopts++; |
| 583 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 584 | } else { |
| 585 | if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 586 | *oldopts = xstrdup(newopts); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 590 | // Use the mount_options list to parse options into flags. |
Alexander Shishkin | 7765095 | 2010-10-28 06:10:03 +0200 | [diff] [blame] | 591 | // Also update list of unrecognized options if unrecognized != NULL |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 592 | static unsigned long parse_mount_options(char *options, char **unrecognized) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 593 | { |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 594 | unsigned long flags = MS_SILENT; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 595 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 596 | // Loop through options |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 597 | for (;;) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 598 | unsigned i; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 599 | char *comma = strchr(options, ','); |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 600 | const char *option_str = mount_option_str; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 601 | |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 602 | if (comma) *comma = '\0'; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 603 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 604 | // FIXME: use hasmntopt() |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 605 | // Find this option in mount_options |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 606 | for (i = 0; i < ARRAY_SIZE(mount_options); i++) { |
Denys Vlasenko | 6ebb2b6 | 2012-06-22 15:17:18 +0200 | [diff] [blame] | 607 | unsigned opt_len = strlen(option_str); |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 608 | |
Denys Vlasenko | 6ebb2b6 | 2012-06-22 15:17:18 +0200 | [diff] [blame] | 609 | if (strncasecmp(option_str, options, opt_len) == 0 |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 610 | && (options[opt_len] == '\0' |
| 611 | /* or is it "comment=" thingy in fstab? */ |
| 612 | IF_FEATURE_MOUNT_FSTAB(IF_DESKTOP( || option_str[opt_len-1] == '=' )) |
| 613 | ) |
Denys Vlasenko | 6ebb2b6 | 2012-06-22 15:17:18 +0200 | [diff] [blame] | 614 | ) { |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 615 | unsigned long fl = mount_options[i]; |
Denys Vlasenko | 9ad8979 | 2012-06-26 16:09:00 +0200 | [diff] [blame] | 616 | if (fl & BB_MS_INVERTED_VALUE) |
Alexander Shishkin | 7765095 | 2010-10-28 06:10:03 +0200 | [diff] [blame] | 617 | flags &= fl; |
| 618 | else |
| 619 | flags |= fl; |
| 620 | goto found; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 621 | } |
Denys Vlasenko | 6ebb2b6 | 2012-06-22 15:17:18 +0200 | [diff] [blame] | 622 | option_str += opt_len + 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 623 | } |
Alexander Shishkin | 7765095 | 2010-10-28 06:10:03 +0200 | [diff] [blame] | 624 | // We did not recognize this option. |
| 625 | // If "unrecognized" is not NULL, append option there. |
| 626 | // Note that we should not append *empty* option - |
| 627 | // in this case we want to pass NULL, not "", to "data" |
| 628 | // parameter of mount(2) syscall. |
| 629 | // This is crucial for filesystems that don't accept |
| 630 | // any arbitrary mount options, like cgroup fs: |
| 631 | // "mount -t cgroup none /mnt" |
| 632 | if (options[0] && unrecognized) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 633 | // Add it to strflags, to pass on to kernel |
Alexander Shishkin | 7765095 | 2010-10-28 06:10:03 +0200 | [diff] [blame] | 634 | char *p = *unrecognized; |
| 635 | unsigned len = p ? strlen(p) : 0; |
| 636 | *unrecognized = p = xrealloc(p, len + strlen(options) + 2); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 637 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 638 | // Comma separated if it's not the first one |
Alexander Shishkin | 7765095 | 2010-10-28 06:10:03 +0200 | [diff] [blame] | 639 | if (len) p[len++] = ','; |
| 640 | strcpy(p + len, options); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 641 | } |
Alexander Shishkin | 7765095 | 2010-10-28 06:10:03 +0200 | [diff] [blame] | 642 | found: |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 643 | if (!comma) |
| 644 | break; |
| 645 | // Advance to next option |
| 646 | *comma = ','; |
| 647 | options = ++comma; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 648 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 649 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 650 | return flags; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 653 | // Return a list of all block device backed filesystems |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 654 | static llist_t *get_block_backed_filesystems(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 655 | { |
Denys Vlasenko | 6599e38 | 2020-11-30 15:10:43 +0100 | [diff] [blame] | 656 | static const char filesystems[2][sizeof("/proc/filesystems")] ALIGN1 = { |
Denis Vlasenko | 372686b | 2006-10-12 22:42:33 +0000 | [diff] [blame] | 657 | "/etc/filesystems", |
| 658 | "/proc/filesystems", |
Denis Vlasenko | 372686b | 2006-10-12 22:42:33 +0000 | [diff] [blame] | 659 | }; |
| 660 | char *fs, *buf; |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 661 | llist_t *list = NULL; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 662 | int i; |
| 663 | FILE *f; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 664 | |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 665 | for (i = 0; i < 2; i++) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 666 | f = fopen_for_read(filesystems[i]); |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 667 | if (!f) continue; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 668 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 669 | while ((buf = xmalloc_fgetline(f)) != NULL) { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 670 | if (is_prefixed_with(buf, "nodev") && isspace(buf[5])) |
Denys Vlasenko | f85554c | 2011-11-03 09:54:53 +0100 | [diff] [blame] | 671 | goto next; |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 672 | fs = skip_whitespace(buf); |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 673 | if (*fs == '#' || *fs == '*' || !*fs) |
Denys Vlasenko | f85554c | 2011-11-03 09:54:53 +0100 | [diff] [blame] | 674 | goto next; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 675 | |
Denis Vlasenko | 372686b | 2006-10-12 22:42:33 +0000 | [diff] [blame] | 676 | llist_add_to_end(&list, xstrdup(fs)); |
Denys Vlasenko | f85554c | 2011-11-03 09:54:53 +0100 | [diff] [blame] | 677 | next: |
Denis Vlasenko | 372686b | 2006-10-12 22:42:33 +0000 | [diff] [blame] | 678 | free(buf); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 679 | } |
| 680 | if (ENABLE_FEATURE_CLEAN_UP) fclose(f); |
| 681 | } |
| 682 | |
| 683 | return list; |
| 684 | } |
| 685 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 686 | #if ENABLE_FEATURE_CLEAN_UP |
| 687 | static void delete_block_backed_filesystems(void) |
| 688 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 689 | llist_free(fslist, free); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 690 | } |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 691 | #else |
| 692 | void delete_block_backed_filesystems(void); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 693 | #endif |
| 694 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 695 | // Perform actual mount of specific filesystem at specific location. |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 696 | // NB: mp->xxx fields may be trashed on exit |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 697 | static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filteropts) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 698 | { |
Denis Vlasenko | b172678 | 2006-09-29 14:43:20 +0000 | [diff] [blame] | 699 | int rc = 0; |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 700 | |
Denys Vlasenko | 911d265 | 2015-12-30 20:11:34 +0100 | [diff] [blame] | 701 | vfsflags &= ~(unsigned long)MOUNT_FAKEFLAGS; |
| 702 | |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 703 | if (FAKE_IT) { |
Denis Vlasenko | b413368 | 2008-02-18 13:05:38 +0000 | [diff] [blame] | 704 | if (verbose >= 2) |
| 705 | bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')", |
| 706 | mp->mnt_fsname, mp->mnt_dir, mp->mnt_type, |
| 707 | vfsflags, filteropts); |
| 708 | goto mtab; |
| 709 | } |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 710 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 711 | // Mount, with fallback to read-only if necessary. |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 712 | for (;;) { |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 713 | errno = 0; |
| 714 | rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type, |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 715 | vfsflags, filteropts); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 716 | |
| 717 | // If mount failed, try |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 718 | // helper program mount.<mnt_type> |
Denys Vlasenko | ba98603 | 2009-09-15 23:00:09 +0200 | [diff] [blame] | 719 | if (HELPERS_ALLOWED && rc && mp->mnt_type) { |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 720 | char *args[8]; |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 721 | int errno_save = errno; |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 722 | args[0] = xasprintf("mount.%s", mp->mnt_type); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 723 | rc = 1; |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 724 | if (FAKE_IT) |
| 725 | args[rc++] = (char *)"-f"; |
| 726 | if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB) |
| 727 | args[rc++] = (char *)"-n"; |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 728 | args[rc++] = mp->mnt_fsname; |
| 729 | args[rc++] = mp->mnt_dir; |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 730 | if (filteropts) { |
| 731 | args[rc++] = (char *)"-o"; |
| 732 | args[rc++] = filteropts; |
| 733 | } |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 734 | args[rc] = NULL; |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 735 | rc = spawn_and_wait(args); |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 736 | free(args[0]); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 737 | if (!rc) |
| 738 | break; |
| 739 | errno = errno_save; |
| 740 | } |
| 741 | |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 742 | if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS)) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 743 | break; |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 744 | if (!(vfsflags & MS_SILENT)) |
| 745 | bb_error_msg("%s is write-protected, mounting read-only", |
| 746 | mp->mnt_fsname); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 747 | vfsflags |= MS_RDONLY; |
| 748 | } |
| 749 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 750 | // Abort entirely if permission denied. |
| 751 | |
| 752 | if (rc && errno == EPERM) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 753 | bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 754 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 755 | // If the mount was successful, and we're maintaining an old-style |
| 756 | // mtab file by hand, add the new entry to it now. |
Denis Vlasenko | 6a353c8 | 2006-11-19 17:34:57 +0000 | [diff] [blame] | 757 | mtab: |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 758 | if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) { |
Denis Vlasenko | a52145a | 2006-09-17 15:09:48 +0000 | [diff] [blame] | 759 | char *fsname; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 760 | FILE *mountTable = setmntent(bb_path_mtab_file, "a+"); |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 761 | const char *option_str = mount_option_str; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 762 | int i; |
| 763 | |
Denis Vlasenko | 6a353c8 | 2006-11-19 17:34:57 +0000 | [diff] [blame] | 764 | if (!mountTable) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 765 | bb_simple_perror_msg(bb_path_mtab_file); |
Denis Vlasenko | 6a353c8 | 2006-11-19 17:34:57 +0000 | [diff] [blame] | 766 | goto ret; |
| 767 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 768 | |
| 769 | // Add vfs string flags |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 770 | for (i = 0; mount_options[i] != MS_REMOUNT; i++) { |
| 771 | if (mount_options[i] > 0 && (mount_options[i] & vfsflags)) |
| 772 | append_mount_options(&(mp->mnt_opts), option_str); |
| 773 | option_str += strlen(option_str) + 1; |
| 774 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 775 | |
| 776 | // Remove trailing / (if any) from directory we mounted on |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 777 | i = strlen(mp->mnt_dir) - 1; |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 778 | while (i > 0 && mp->mnt_dir[i] == '/') |
Denys Vlasenko | c6450c9 | 2011-02-28 11:09:49 +0100 | [diff] [blame] | 779 | mp->mnt_dir[i--] = '\0'; |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 780 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 781 | // Convert to canonical pathnames as needed |
Denis Vlasenko | a52145a | 2006-09-17 15:09:48 +0000 | [diff] [blame] | 782 | mp->mnt_dir = bb_simplify_path(mp->mnt_dir); |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 783 | fsname = NULL; |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 784 | if (!mp->mnt_type || !*mp->mnt_type) { // bind mount |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 785 | mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname); |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 786 | mp->mnt_type = (char*)"bind"; |
Denis Vlasenko | 727ef94 | 2006-09-14 13:19:19 +0000 | [diff] [blame] | 787 | } |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 788 | mp->mnt_freq = mp->mnt_passno = 0; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 789 | |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 790 | // Write and close |
| 791 | #if ENABLE_FEATURE_MTAB_SUPPORT |
| 792 | if (vfsflags & MS_MOVE) |
| 793 | update_mtab_entry_on_move(mp); |
| 794 | else |
| 795 | #endif |
| 796 | addmntent(mountTable, mp); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 797 | endmntent(mountTable); |
Roman Borisov | c8dc01d | 2011-02-28 05:06:01 +0100 | [diff] [blame] | 798 | |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 799 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | a52145a | 2006-09-17 15:09:48 +0000 | [diff] [blame] | 800 | free(mp->mnt_dir); |
Denis Vlasenko | fc56dd2 | 2006-09-17 15:01:53 +0000 | [diff] [blame] | 801 | free(fsname); |
| 802 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 803 | } |
Denis Vlasenko | 6a353c8 | 2006-11-19 17:34:57 +0000 | [diff] [blame] | 804 | ret: |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 805 | return rc; |
| 806 | } |
| 807 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 808 | #if ENABLE_FEATURE_MOUNT_NFS |
| 809 | |
| 810 | /* |
| 811 | * Linux NFS mount |
| 812 | * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com> |
| 813 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 814 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 815 | * |
| 816 | * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port |
| 817 | * numbers to be specified on the command line. |
| 818 | * |
| 819 | * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>: |
| 820 | * Omit the call to connect() for Linux version 1.3.11 or later. |
| 821 | * |
| 822 | * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com> |
| 823 | * Implemented the "bg", "fg" and "retry" mount options for NFS. |
| 824 | * |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 825 | * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org> |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 826 | * - added Native Language Support |
| 827 | * |
| 828 | * Modified by Olaf Kirch and Trond Myklebust for new NFS code, |
| 829 | * plus NFSv3 stuff. |
| 830 | */ |
| 831 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 832 | #define MOUNTPORT 635 |
| 833 | #define MNTPATHLEN 1024 |
| 834 | #define MNTNAMLEN 255 |
| 835 | #define FHSIZE 32 |
| 836 | #define FHSIZE3 64 |
| 837 | |
| 838 | typedef char fhandle[FHSIZE]; |
| 839 | |
| 840 | typedef struct { |
| 841 | unsigned int fhandle3_len; |
| 842 | char *fhandle3_val; |
| 843 | } fhandle3; |
| 844 | |
| 845 | enum mountstat3 { |
| 846 | MNT_OK = 0, |
| 847 | MNT3ERR_PERM = 1, |
| 848 | MNT3ERR_NOENT = 2, |
| 849 | MNT3ERR_IO = 5, |
| 850 | MNT3ERR_ACCES = 13, |
| 851 | MNT3ERR_NOTDIR = 20, |
| 852 | MNT3ERR_INVAL = 22, |
| 853 | MNT3ERR_NAMETOOLONG = 63, |
| 854 | MNT3ERR_NOTSUPP = 10004, |
| 855 | MNT3ERR_SERVERFAULT = 10006, |
| 856 | }; |
| 857 | typedef enum mountstat3 mountstat3; |
| 858 | |
| 859 | struct fhstatus { |
| 860 | unsigned int fhs_status; |
| 861 | union { |
| 862 | fhandle fhs_fhandle; |
| 863 | } fhstatus_u; |
| 864 | }; |
| 865 | typedef struct fhstatus fhstatus; |
| 866 | |
| 867 | struct mountres3_ok { |
| 868 | fhandle3 fhandle; |
| 869 | struct { |
| 870 | unsigned int auth_flavours_len; |
| 871 | char *auth_flavours_val; |
| 872 | } auth_flavours; |
| 873 | }; |
| 874 | typedef struct mountres3_ok mountres3_ok; |
| 875 | |
| 876 | struct mountres3 { |
| 877 | mountstat3 fhs_status; |
| 878 | union { |
| 879 | mountres3_ok mountinfo; |
| 880 | } mountres3_u; |
| 881 | }; |
| 882 | typedef struct mountres3 mountres3; |
| 883 | |
| 884 | typedef char *dirpath; |
| 885 | |
| 886 | typedef char *name; |
| 887 | |
| 888 | typedef struct mountbody *mountlist; |
| 889 | |
| 890 | struct mountbody { |
| 891 | name ml_hostname; |
| 892 | dirpath ml_directory; |
| 893 | mountlist ml_next; |
| 894 | }; |
| 895 | typedef struct mountbody mountbody; |
| 896 | |
| 897 | typedef struct groupnode *groups; |
| 898 | |
| 899 | struct groupnode { |
| 900 | name gr_name; |
| 901 | groups gr_next; |
| 902 | }; |
| 903 | typedef struct groupnode groupnode; |
| 904 | |
| 905 | typedef struct exportnode *exports; |
| 906 | |
| 907 | struct exportnode { |
| 908 | dirpath ex_dir; |
| 909 | groups ex_groups; |
| 910 | exports ex_next; |
| 911 | }; |
| 912 | typedef struct exportnode exportnode; |
| 913 | |
| 914 | struct ppathcnf { |
| 915 | int pc_link_max; |
| 916 | short pc_max_canon; |
| 917 | short pc_max_input; |
| 918 | short pc_name_max; |
| 919 | short pc_path_max; |
| 920 | short pc_pipe_buf; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 921 | uint8_t pc_vdisable; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 922 | char pc_xxx; |
| 923 | short pc_mask[2]; |
| 924 | }; |
| 925 | typedef struct ppathcnf ppathcnf; |
| 926 | |
| 927 | #define MOUNTPROG 100005 |
| 928 | #define MOUNTVERS 1 |
| 929 | |
| 930 | #define MOUNTPROC_NULL 0 |
| 931 | #define MOUNTPROC_MNT 1 |
| 932 | #define MOUNTPROC_DUMP 2 |
| 933 | #define MOUNTPROC_UMNT 3 |
| 934 | #define MOUNTPROC_UMNTALL 4 |
| 935 | #define MOUNTPROC_EXPORT 5 |
| 936 | #define MOUNTPROC_EXPORTALL 6 |
| 937 | |
| 938 | #define MOUNTVERS_POSIX 2 |
| 939 | |
| 940 | #define MOUNTPROC_PATHCONF 7 |
| 941 | |
| 942 | #define MOUNT_V3 3 |
| 943 | |
| 944 | #define MOUNTPROC3_NULL 0 |
| 945 | #define MOUNTPROC3_MNT 1 |
| 946 | #define MOUNTPROC3_DUMP 2 |
| 947 | #define MOUNTPROC3_UMNT 3 |
| 948 | #define MOUNTPROC3_UMNTALL 4 |
| 949 | #define MOUNTPROC3_EXPORT 5 |
| 950 | |
| 951 | enum { |
| 952 | #ifndef NFS_FHSIZE |
| 953 | NFS_FHSIZE = 32, |
| 954 | #endif |
| 955 | #ifndef NFS_PORT |
| 956 | NFS_PORT = 2049 |
| 957 | #endif |
| 958 | }; |
| 959 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 960 | /* |
| 961 | * We want to be able to compile mount on old kernels in such a way |
| 962 | * that the binary will work well on more recent kernels. |
| 963 | * Thus, if necessary we teach nfsmount.c the structure of new fields |
| 964 | * that will come later. |
| 965 | * |
| 966 | * Moreover, the new kernel includes conflict with glibc includes |
| 967 | * so it is easiest to ignore the kernel altogether (at compile time). |
| 968 | */ |
| 969 | |
| 970 | struct nfs2_fh { |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 971 | char data[32]; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 972 | }; |
| 973 | struct nfs3_fh { |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 974 | unsigned short size; |
| 975 | unsigned char data[64]; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 976 | }; |
| 977 | |
| 978 | struct nfs_mount_data { |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 979 | int version; /* 1 */ |
| 980 | int fd; /* 1 */ |
| 981 | struct nfs2_fh old_root; /* 1 */ |
| 982 | int flags; /* 1 */ |
| 983 | int rsize; /* 1 */ |
| 984 | int wsize; /* 1 */ |
| 985 | int timeo; /* 1 */ |
| 986 | int retrans; /* 1 */ |
| 987 | int acregmin; /* 1 */ |
| 988 | int acregmax; /* 1 */ |
| 989 | int acdirmin; /* 1 */ |
| 990 | int acdirmax; /* 1 */ |
| 991 | struct sockaddr_in addr; /* 1 */ |
| 992 | char hostname[256]; /* 1 */ |
| 993 | int namlen; /* 2 */ |
| 994 | unsigned int bsize; /* 3 */ |
| 995 | struct nfs3_fh root; /* 4 */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 996 | }; |
| 997 | |
| 998 | /* bits in the flags field */ |
| 999 | enum { |
| 1000 | NFS_MOUNT_SOFT = 0x0001, /* 1 */ |
| 1001 | NFS_MOUNT_INTR = 0x0002, /* 1 */ |
| 1002 | NFS_MOUNT_SECURE = 0x0004, /* 1 */ |
| 1003 | NFS_MOUNT_POSIX = 0x0008, /* 1 */ |
| 1004 | NFS_MOUNT_NOCTO = 0x0010, /* 1 */ |
| 1005 | NFS_MOUNT_NOAC = 0x0020, /* 1 */ |
| 1006 | NFS_MOUNT_TCP = 0x0040, /* 2 */ |
| 1007 | NFS_MOUNT_VER3 = 0x0080, /* 3 */ |
| 1008 | NFS_MOUNT_KERBEROS = 0x0100, /* 3 */ |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1009 | NFS_MOUNT_NONLM = 0x0200, /* 3 */ |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1010 | NFS_MOUNT_NOACL = 0x0800, /* 4 */ |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1011 | NFS_MOUNT_NORDIRPLUS = 0x4000 |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1012 | }; |
| 1013 | |
| 1014 | |
| 1015 | /* |
| 1016 | * We need to translate between nfs status return values and |
| 1017 | * the local errno values which may not be the same. |
| 1018 | * |
| 1019 | * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno: |
| 1020 | * "after #include <errno.h> the symbol errno is reserved for any use, |
| 1021 | * it cannot even be used as a struct tag or field name". |
| 1022 | */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1023 | #ifndef EDQUOT |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 1024 | # define EDQUOT ENOSPC |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1025 | #endif |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1026 | /* Convert each NFSERR_BLAH into EBLAH */ |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 1027 | static const uint8_t nfs_err_stat[] ALIGN1 = { |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 1028 | 1, 2, 5, 6, 13, 17, |
| 1029 | 19, 20, 21, 22, 27, 28, |
| 1030 | 30, 63, 66, 69, 70, 71 |
| 1031 | }; |
Denys Vlasenko | dc1fd2e | 2010-05-19 17:01:29 +0200 | [diff] [blame] | 1032 | #if ( \ |
| 1033 | EPERM | ENOENT | EIO | ENXIO | EACCES| EEXIST | \ |
| 1034 | ENODEV| ENOTDIR | EISDIR | EINVAL| EFBIG | ENOSPC | \ |
| 1035 | EROFS | ENAMETOOLONG| ENOTEMPTY| EDQUOT| ESTALE| EREMOTE) < 256 |
| 1036 | typedef uint8_t nfs_err_type; |
| 1037 | #else |
| 1038 | typedef uint16_t nfs_err_type; |
| 1039 | #endif |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 1040 | static const nfs_err_type nfs_err_errnum[] ALIGN2 = { |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 1041 | EPERM , ENOENT , EIO , ENXIO , EACCES, EEXIST, |
| 1042 | ENODEV, ENOTDIR , EISDIR , EINVAL, EFBIG , ENOSPC, |
| 1043 | EROFS , ENAMETOOLONG, ENOTEMPTY, EDQUOT, ESTALE, EREMOTE |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1044 | }; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1045 | static char *nfs_strerror(int status) |
| 1046 | { |
| 1047 | int i; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1048 | |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 1049 | for (i = 0; i < ARRAY_SIZE(nfs_err_stat); i++) { |
| 1050 | if (nfs_err_stat[i] == status) |
| 1051 | return strerror(nfs_err_errnum[i]); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1052 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1053 | return xasprintf("unknown nfs status return value: %d", status); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | static bool_t xdr_fhandle(XDR *xdrs, fhandle objp) |
| 1057 | { |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1058 | return xdr_opaque(xdrs, objp, FHSIZE); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp) |
| 1062 | { |
| 1063 | if (!xdr_u_int(xdrs, &objp->fhs_status)) |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1064 | return FALSE; |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1065 | if (objp->fhs_status == 0) |
| 1066 | return xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1067 | return TRUE; |
| 1068 | } |
| 1069 | |
| 1070 | static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp) |
| 1071 | { |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1072 | return xdr_string(xdrs, objp, MNTPATHLEN); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp) |
| 1076 | { |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1077 | return xdr_bytes(xdrs, (char **)&objp->fhandle3_val, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1078 | (unsigned int *) &objp->fhandle3_len, |
| 1079 | FHSIZE3); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp) |
| 1083 | { |
| 1084 | if (!xdr_fhandle3(xdrs, &objp->fhandle)) |
| 1085 | return FALSE; |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1086 | return xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1087 | &(objp->auth_flavours.auth_flavours_len), |
| 1088 | ~0, |
| 1089 | sizeof(int), |
| 1090 | (xdrproc_t) xdr_int); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp) |
| 1094 | { |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1095 | return xdr_enum(xdrs, (enum_t *) objp); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp) |
| 1099 | { |
| 1100 | if (!xdr_mountstat3(xdrs, &objp->fhs_status)) |
| 1101 | return FALSE; |
Alexander Shishkin | 54779a4 | 2010-10-22 13:35:47 +0200 | [diff] [blame] | 1102 | if (objp->fhs_status == MNT_OK) |
| 1103 | return xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1104 | return TRUE; |
| 1105 | } |
| 1106 | |
| 1107 | #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2) |
| 1108 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1109 | /* |
| 1110 | * Unfortunately, the kernel prints annoying console messages |
| 1111 | * in case of an unexpected nfs mount version (instead of |
| 1112 | * just returning some error). Therefore we'll have to try |
| 1113 | * and figure out what version the kernel expects. |
| 1114 | * |
| 1115 | * Variables: |
| 1116 | * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time |
| 1117 | * NFS_MOUNT_VERSION: these nfsmount sources at compile time |
| 1118 | * nfs_mount_version: version this source and running kernel can handle |
| 1119 | */ |
| 1120 | static void |
| 1121 | find_kernel_nfs_mount_version(void) |
| 1122 | { |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1123 | int kernel_version; |
| 1124 | |
| 1125 | if (nfs_mount_version) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1126 | return; |
| 1127 | |
| 1128 | nfs_mount_version = 4; /* default */ |
| 1129 | |
| 1130 | kernel_version = get_linux_version_code(); |
| 1131 | if (kernel_version) { |
Denys Vlasenko | cc42814 | 2009-12-16 02:06:56 +0100 | [diff] [blame] | 1132 | if (kernel_version < KERNEL_VERSION(2,2,18)) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1133 | nfs_mount_version = 3; |
| 1134 | /* else v4 since 2.3.99pre4 */ |
| 1135 | } |
| 1136 | } |
| 1137 | |
Denis Vlasenko | 3f5fdc7 | 2007-10-14 04:55:59 +0000 | [diff] [blame] | 1138 | static void |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1139 | get_mountport(struct pmap *pm_mnt, |
| 1140 | struct sockaddr_in *server_addr, |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1141 | long unsigned prog, |
| 1142 | long unsigned version, |
| 1143 | long unsigned proto, |
| 1144 | long unsigned port) |
| 1145 | { |
| 1146 | struct pmaplist *pmap; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1147 | |
| 1148 | server_addr->sin_port = PMAPPORT; |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 1149 | /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *). |
| 1150 | * I understand it like "IPv6 for this is not 100% ready" */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1151 | pmap = pmap_getmaps(server_addr); |
| 1152 | |
| 1153 | if (version > MAX_NFSPROT) |
| 1154 | version = MAX_NFSPROT; |
| 1155 | if (!prog) |
| 1156 | prog = MOUNTPROG; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1157 | pm_mnt->pm_prog = prog; |
| 1158 | pm_mnt->pm_vers = version; |
| 1159 | pm_mnt->pm_prot = proto; |
| 1160 | pm_mnt->pm_port = port; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 1161 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1162 | while (pmap) { |
| 1163 | if (pmap->pml_map.pm_prog != prog) |
| 1164 | goto next; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1165 | if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1166 | goto next; |
| 1167 | if (version > 2 && pmap->pml_map.pm_vers != version) |
| 1168 | goto next; |
| 1169 | if (version && version <= 2 && pmap->pml_map.pm_vers > 2) |
| 1170 | goto next; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 1171 | if (pmap->pml_map.pm_vers > MAX_NFSPROT |
| 1172 | || (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto) |
| 1173 | || (port && pmap->pml_map.pm_port != port) |
| 1174 | ) { |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1175 | goto next; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 1176 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1177 | memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt)); |
| 1178 | next: |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1179 | pmap = pmap->pml_next; |
| 1180 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1181 | if (!pm_mnt->pm_vers) |
| 1182 | pm_mnt->pm_vers = MOUNTVERS; |
| 1183 | if (!pm_mnt->pm_port) |
| 1184 | pm_mnt->pm_port = MOUNTPORT; |
| 1185 | if (!pm_mnt->pm_prot) |
| 1186 | pm_mnt->pm_prot = IPPROTO_TCP; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1189 | #if BB_MMU |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1190 | static int daemonize(void) |
| 1191 | { |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1192 | int pid = fork(); |
| 1193 | if (pid < 0) /* error */ |
| 1194 | return -errno; |
| 1195 | if (pid > 0) /* parent */ |
| 1196 | return 0; |
| 1197 | /* child */ |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1198 | close(0); |
| 1199 | xopen(bb_dev_null, O_RDWR); |
| 1200 | xdup2(0, 1); |
| 1201 | xdup2(0, 2); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1202 | setsid(); |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 1203 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1204 | logmode = LOGMODE_SYSLOG; |
| 1205 | return 1; |
| 1206 | } |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1207 | #else |
Denys Vlasenko | 8c317f0 | 2019-05-14 17:26:47 +0200 | [diff] [blame] | 1208 | static inline int daemonize(void) |
| 1209 | { |
| 1210 | return -ENOSYS; |
| 1211 | } |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1212 | #endif |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1213 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1214 | /* TODO */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1215 | static inline int we_saw_this_host_before(const char *hostname UNUSED_PARAM) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1216 | { |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | /* RPC strerror analogs are terminally idiotic: |
| 1221 | * *mandatory* prefix and \n at end. |
| 1222 | * This hopefully helps. Usage: |
| 1223 | * error_msg_rpc(clnt_*error*(" ")) */ |
| 1224 | static void error_msg_rpc(const char *msg) |
| 1225 | { |
Denis Vlasenko | 23514fe | 2006-09-19 14:07:52 +0000 | [diff] [blame] | 1226 | int len; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1227 | while (msg[0] == ' ' || msg[0] == ':') msg++; |
| 1228 | len = strlen(msg); |
| 1229 | while (len && msg[len-1] == '\n') len--; |
| 1230 | bb_error_msg("%.*s", len, msg); |
| 1231 | } |
| 1232 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1233 | /* NB: mp->xxx fields may be trashed on exit */ |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 1234 | static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1235 | { |
| 1236 | CLIENT *mclient; |
| 1237 | char *hostname; |
| 1238 | char *pathname; |
| 1239 | char *mounthost; |
Denys Vlasenko | e71dd7c | 2009-05-13 16:32:32 +0200 | [diff] [blame] | 1240 | /* prior to 2.6.23, kernel took NFS options in a form of this struct |
| 1241 | * only. 2.6.23+ looks at data->version, and if it's not 1..6, |
| 1242 | * then data pointer is interpreted as a string. */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1243 | struct nfs_mount_data data; |
| 1244 | char *opt; |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 1245 | char *tokstate; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1246 | struct hostent *hp; |
| 1247 | struct sockaddr_in server_addr; |
| 1248 | struct sockaddr_in mount_server_addr; |
| 1249 | int msock, fsock; |
| 1250 | union { |
| 1251 | struct fhstatus nfsv2; |
| 1252 | struct mountres3 nfsv3; |
| 1253 | } status; |
| 1254 | int daemonized; |
| 1255 | char *s; |
| 1256 | int port; |
| 1257 | int mountport; |
| 1258 | int proto; |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1259 | #if BB_MMU |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1260 | smallint bg = 0; |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1261 | #else |
| 1262 | enum { bg = 0 }; |
| 1263 | #endif |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1264 | int retry; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1265 | int mountprog; |
| 1266 | int mountvers; |
| 1267 | int nfsprog; |
| 1268 | int nfsvers; |
| 1269 | int retval; |
Denys Vlasenko | e71dd7c | 2009-05-13 16:32:32 +0200 | [diff] [blame] | 1270 | /* these all are one-bit really. gcc 4.3.1 likes this combination: */ |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1271 | smallint tcp; |
| 1272 | smallint soft; |
| 1273 | int intr; |
| 1274 | int posix; |
| 1275 | int nocto; |
| 1276 | int noac; |
| 1277 | int nordirplus; |
| 1278 | int nolock; |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1279 | int noacl; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1280 | |
| 1281 | find_kernel_nfs_mount_version(); |
| 1282 | |
| 1283 | daemonized = 0; |
| 1284 | mounthost = NULL; |
| 1285 | retval = ETIMEDOUT; |
| 1286 | msock = fsock = -1; |
| 1287 | mclient = NULL; |
| 1288 | |
| 1289 | /* NB: hostname, mounthost, filteropts must be free()d prior to return */ |
| 1290 | |
| 1291 | filteropts = xstrdup(filteropts); /* going to trash it later... */ |
| 1292 | |
| 1293 | hostname = xstrdup(mp->mnt_fsname); |
| 1294 | /* mount_main() guarantees that ':' is there */ |
| 1295 | s = strchr(hostname, ':'); |
| 1296 | pathname = s + 1; |
| 1297 | *s = '\0'; |
| 1298 | /* Ignore all but first hostname in replicated mounts |
Denys Vlasenko | a86e024 | 2011-11-10 16:53:35 +0100 | [diff] [blame] | 1299 | * until they can be fully supported. (mack@sgi.com) */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1300 | s = strchr(hostname, ','); |
| 1301 | if (s) { |
| 1302 | *s = '\0'; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1303 | bb_simple_error_msg("warning: multiple hostnames not supported"); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | server_addr.sin_family = AF_INET; |
| 1307 | if (!inet_aton(hostname, &server_addr.sin_addr)) { |
| 1308 | hp = gethostbyname(hostname); |
| 1309 | if (hp == NULL) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1310 | bb_simple_herror_msg(hostname); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1311 | goto fail; |
| 1312 | } |
Denys Vlasenko | 9906933 | 2010-02-27 19:38:19 +0100 | [diff] [blame] | 1313 | if (hp->h_length != (int)sizeof(struct in_addr)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1314 | bb_simple_error_msg_and_die("only IPv4 is supported"); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1315 | } |
Denys Vlasenko | 9906933 | 2010-02-27 19:38:19 +0100 | [diff] [blame] | 1316 | memcpy(&server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr)); |
| 1320 | |
| 1321 | /* add IP address to mtab options for use when unmounting */ |
| 1322 | |
| 1323 | if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */ |
| 1324 | mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr)); |
| 1325 | } else { |
| 1326 | char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts, |
| 1327 | mp->mnt_opts[0] ? "," : "", |
| 1328 | inet_ntoa(server_addr.sin_addr)); |
| 1329 | free(mp->mnt_opts); |
| 1330 | mp->mnt_opts = tmp; |
| 1331 | } |
| 1332 | |
| 1333 | /* Set default options. |
| 1334 | * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to |
| 1335 | * let the kernel decide. |
| 1336 | * timeo is filled in after we know whether it'll be TCP or UDP. */ |
| 1337 | memset(&data, 0, sizeof(data)); |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1338 | data.retrans = 3; |
| 1339 | data.acregmin = 3; |
| 1340 | data.acregmax = 60; |
| 1341 | data.acdirmin = 30; |
| 1342 | data.acdirmax = 60; |
| 1343 | data.namlen = NAME_MAX; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1344 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1345 | soft = 0; |
| 1346 | intr = 0; |
| 1347 | posix = 0; |
| 1348 | nocto = 0; |
| 1349 | nolock = 0; |
| 1350 | noac = 0; |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1351 | nordirplus = 0; |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1352 | noacl = 0; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1353 | retry = 10000; /* 10000 minutes ~ 1 week */ |
Bernhard Reutner-Fischer | 8820629 | 2011-05-04 19:03:30 +0200 | [diff] [blame] | 1354 | tcp = 1; /* nfs-utils uses tcp per default */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1355 | |
| 1356 | mountprog = MOUNTPROG; |
| 1357 | mountvers = 0; |
| 1358 | port = 0; |
| 1359 | mountport = 0; |
| 1360 | nfsprog = 100003; |
| 1361 | nfsvers = 0; |
| 1362 | |
| 1363 | /* parse options */ |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 1364 | if (filteropts) for (opt = strtok_r(filteropts, ",", &tokstate); opt; opt = strtok_r(NULL, ",", &tokstate)) { |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1365 | char *opteq = strchr(opt, '='); |
| 1366 | if (opteq) { |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1367 | int val, idx; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1368 | static const char options[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1369 | /* 0 */ "rsize\0" |
| 1370 | /* 1 */ "wsize\0" |
| 1371 | /* 2 */ "timeo\0" |
| 1372 | /* 3 */ "retrans\0" |
| 1373 | /* 4 */ "acregmin\0" |
| 1374 | /* 5 */ "acregmax\0" |
| 1375 | /* 6 */ "acdirmin\0" |
| 1376 | /* 7 */ "acdirmax\0" |
| 1377 | /* 8 */ "actimeo\0" |
| 1378 | /* 9 */ "retry\0" |
| 1379 | /* 10 */ "port\0" |
| 1380 | /* 11 */ "mountport\0" |
| 1381 | /* 12 */ "mounthost\0" |
| 1382 | /* 13 */ "mountprog\0" |
| 1383 | /* 14 */ "mountvers\0" |
| 1384 | /* 15 */ "nfsprog\0" |
| 1385 | /* 16 */ "nfsvers\0" |
| 1386 | /* 17 */ "vers\0" |
| 1387 | /* 18 */ "proto\0" |
| 1388 | /* 19 */ "namlen\0" |
| 1389 | /* 20 */ "addr\0"; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1390 | |
| 1391 | *opteq++ = '\0'; |
| 1392 | idx = index_in_strings(options, opt); |
| 1393 | switch (idx) { |
| 1394 | case 12: // "mounthost" |
| 1395 | mounthost = xstrndup(opteq, |
| 1396 | strcspn(opteq, " \t\n\r,")); |
| 1397 | continue; |
| 1398 | case 18: // "proto" |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 1399 | if (is_prefixed_with(opteq, "tcp")) |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1400 | tcp = 1; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 1401 | else if (is_prefixed_with(opteq, "udp")) |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1402 | tcp = 0; |
| 1403 | else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1404 | bb_simple_error_msg("warning: unrecognized proto= option"); |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1405 | continue; |
| 1406 | case 20: // "addr" - ignore |
| 1407 | continue; |
Peter Korsgaard | 301fe50 | 2011-02-21 17:52:13 +0100 | [diff] [blame] | 1408 | case -1: // unknown |
| 1409 | if (vfsflags & MS_REMOUNT) |
| 1410 | continue; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 1413 | val = xatoi_positive(opteq); |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1414 | switch (idx) { |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1415 | case 0: // "rsize" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1416 | data.rsize = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1417 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1418 | case 1: // "wsize" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1419 | data.wsize = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1420 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1421 | case 2: // "timeo" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1422 | data.timeo = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1423 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1424 | case 3: // "retrans" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1425 | data.retrans = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1426 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1427 | case 4: // "acregmin" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1428 | data.acregmin = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1429 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1430 | case 5: // "acregmax" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1431 | data.acregmax = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1432 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1433 | case 6: // "acdirmin" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1434 | data.acdirmin = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1435 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1436 | case 7: // "acdirmax" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1437 | data.acdirmax = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1438 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1439 | case 8: // "actimeo" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1440 | data.acregmin = val; |
| 1441 | data.acregmax = val; |
| 1442 | data.acdirmin = val; |
| 1443 | data.acdirmax = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1444 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1445 | case 9: // "retry" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1446 | retry = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1447 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1448 | case 10: // "port" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1449 | port = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1450 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1451 | case 11: // "mountport" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1452 | mountport = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1453 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1454 | case 13: // "mountprog" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1455 | mountprog = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1456 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1457 | case 14: // "mountvers" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1458 | mountvers = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1459 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1460 | case 15: // "nfsprog" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1461 | nfsprog = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1462 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1463 | case 16: // "nfsvers" |
| 1464 | case 17: // "vers" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1465 | nfsvers = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1466 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1467 | case 19: // "namlen" |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1468 | //if (nfs_mount_version >= 2) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1469 | data.namlen = val; |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1470 | //else |
| 1471 | // bb_error_msg("warning: option namlen is not supported\n"); |
| 1472 | continue; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1473 | default: |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1474 | bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val); |
| 1475 | goto fail; |
| 1476 | } |
| 1477 | } |
Denis Vlasenko | f26e3d2 | 2008-06-24 21:39:32 +0000 | [diff] [blame] | 1478 | else { /* not of the form opt=val */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1479 | static const char options[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1480 | "bg\0" |
| 1481 | "fg\0" |
| 1482 | "soft\0" |
| 1483 | "hard\0" |
| 1484 | "intr\0" |
| 1485 | "posix\0" |
| 1486 | "cto\0" |
| 1487 | "ac\0" |
| 1488 | "tcp\0" |
| 1489 | "udp\0" |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1490 | "lock\0" |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1491 | "rdirplus\0" |
Denys Vlasenko | 56443cd | 2012-04-20 15:07:22 +0200 | [diff] [blame] | 1492 | "acl\0"; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1493 | int val = 1; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 1494 | if (is_prefixed_with(opt, "no")) { |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1495 | val = 0; |
| 1496 | opt += 2; |
| 1497 | } |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1498 | switch (index_in_strings(options, opt)) { |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1499 | case 0: // "bg" |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1500 | #if BB_MMU |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1501 | bg = val; |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1502 | #endif |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1503 | break; |
| 1504 | case 1: // "fg" |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1505 | #if BB_MMU |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1506 | bg = !val; |
Denis Vlasenko | f000065 | 2007-09-04 18:30:26 +0000 | [diff] [blame] | 1507 | #endif |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | case 2: // "soft" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1510 | soft = val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1511 | break; |
| 1512 | case 3: // "hard" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1513 | soft = !val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1514 | break; |
| 1515 | case 4: // "intr" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1516 | intr = val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1517 | break; |
| 1518 | case 5: // "posix" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1519 | posix = val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1520 | break; |
| 1521 | case 6: // "cto" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1522 | nocto = !val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1523 | break; |
| 1524 | case 7: // "ac" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1525 | noac = !val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1526 | break; |
| 1527 | case 8: // "tcp" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1528 | tcp = val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1529 | break; |
| 1530 | case 9: // "udp" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1531 | tcp = !val; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1532 | break; |
| 1533 | case 10: // "lock" |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1534 | if (nfs_mount_version >= 3) |
| 1535 | nolock = !val; |
| 1536 | else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1537 | bb_simple_error_msg("warning: option nolock is not supported"); |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1538 | break; |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1539 | case 11: //rdirplus |
| 1540 | nordirplus = !val; |
| 1541 | break; |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1542 | case 12: // acl |
Denys Vlasenko | 56443cd | 2012-04-20 15:07:22 +0200 | [diff] [blame] | 1543 | noacl = !val; |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1544 | break; |
Denis Vlasenko | 68f2187 | 2006-10-26 01:47:34 +0000 | [diff] [blame] | 1545 | default: |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1546 | bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt); |
| 1547 | goto fail; |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP; |
| 1552 | |
| 1553 | data.flags = (soft ? NFS_MOUNT_SOFT : 0) |
| 1554 | | (intr ? NFS_MOUNT_INTR : 0) |
| 1555 | | (posix ? NFS_MOUNT_POSIX : 0) |
| 1556 | | (nocto ? NFS_MOUNT_NOCTO : 0) |
Denis Vlasenko | c29684a | 2008-07-19 22:40:30 +0000 | [diff] [blame] | 1557 | | (noac ? NFS_MOUNT_NOAC : 0) |
Dave Love | fae473c | 2011-11-10 15:19:25 +0100 | [diff] [blame] | 1558 | | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0) |
Denys Vlasenko | 56443cd | 2012-04-20 15:07:22 +0200 | [diff] [blame] | 1559 | | (noacl ? NFS_MOUNT_NOACL : 0); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1560 | if (nfs_mount_version >= 2) |
| 1561 | data.flags |= (tcp ? NFS_MOUNT_TCP : 0); |
| 1562 | if (nfs_mount_version >= 3) |
| 1563 | data.flags |= (nolock ? NFS_MOUNT_NONLM : 0); |
| 1564 | if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) { |
| 1565 | bb_error_msg("NFSv%d not supported", nfsvers); |
| 1566 | goto fail; |
| 1567 | } |
| 1568 | if (nfsvers && !mountvers) |
| 1569 | mountvers = (nfsvers < 3) ? 1 : nfsvers; |
| 1570 | if (nfsvers && nfsvers < mountvers) { |
| 1571 | mountvers = nfsvers; |
| 1572 | } |
| 1573 | |
| 1574 | /* Adjust options if none specified */ |
| 1575 | if (!data.timeo) |
| 1576 | data.timeo = tcp ? 70 : 7; |
| 1577 | |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1578 | data.version = nfs_mount_version; |
| 1579 | |
| 1580 | if (vfsflags & MS_REMOUNT) |
| 1581 | goto do_mount; |
| 1582 | |
| 1583 | /* |
| 1584 | * If the previous mount operation on the same host was |
| 1585 | * backgrounded, and the "bg" for this mount is also set, |
| 1586 | * give up immediately, to avoid the initial timeout. |
| 1587 | */ |
| 1588 | if (bg && we_saw_this_host_before(hostname)) { |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1589 | daemonized = daemonize(); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1590 | if (daemonized <= 0) { /* parent or error */ |
| 1591 | retval = -daemonized; |
| 1592 | goto ret; |
| 1593 | } |
| 1594 | } |
| 1595 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1596 | /* Create mount daemon client */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1597 | /* See if the nfs host = mount host. */ |
| 1598 | if (mounthost) { |
| 1599 | if (mounthost[0] >= '0' && mounthost[0] <= '9') { |
| 1600 | mount_server_addr.sin_family = AF_INET; |
| 1601 | mount_server_addr.sin_addr.s_addr = inet_addr(hostname); |
| 1602 | } else { |
| 1603 | hp = gethostbyname(mounthost); |
| 1604 | if (hp == NULL) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1605 | bb_simple_herror_msg(mounthost); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1606 | goto fail; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1607 | } |
Denys Vlasenko | 9906933 | 2010-02-27 19:38:19 +0100 | [diff] [blame] | 1608 | if (hp->h_length != (int)sizeof(struct in_addr)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1609 | bb_simple_error_msg_and_die("only IPv4 is supported"); |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 1610 | } |
| 1611 | mount_server_addr.sin_family = AF_INET; |
Denys Vlasenko | 9906933 | 2010-02-27 19:38:19 +0100 | [diff] [blame] | 1612 | memcpy(&mount_server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * The following loop implements the mount retries. When the mount |
| 1618 | * times out, and the "bg" option is set, we background ourself |
| 1619 | * and continue trying. |
| 1620 | * |
| 1621 | * The case where the mount point is not present and the "bg" |
| 1622 | * option is set, is treated as a timeout. This is done to |
| 1623 | * support nested mounts. |
| 1624 | * |
| 1625 | * The "retry" count specified by the user is the number of |
| 1626 | * minutes to retry before giving up. |
| 1627 | */ |
| 1628 | { |
| 1629 | struct timeval total_timeout; |
| 1630 | struct timeval retry_timeout; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1631 | struct pmap pm_mnt; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1632 | time_t t; |
| 1633 | time_t prevt; |
| 1634 | time_t timeout; |
| 1635 | |
| 1636 | retry_timeout.tv_sec = 3; |
| 1637 | retry_timeout.tv_usec = 0; |
| 1638 | total_timeout.tv_sec = 20; |
| 1639 | total_timeout.tv_usec = 0; |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1640 | /* FIXME: use monotonic()? */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1641 | timeout = time(NULL) + 60 * retry; |
| 1642 | prevt = 0; |
| 1643 | t = 30; |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 1644 | retry: |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1645 | /* Be careful not to use too many CPU cycles */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1646 | if (t - prevt < 30) |
| 1647 | sleep(30); |
| 1648 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1649 | get_mountport(&pm_mnt, &mount_server_addr, |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1650 | mountprog, |
| 1651 | mountvers, |
| 1652 | proto, |
| 1653 | mountport); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1654 | nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1655 | |
| 1656 | /* contact the mount daemon via TCP */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1657 | mount_server_addr.sin_port = htons(pm_mnt.pm_port); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1658 | msock = RPC_ANYSOCK; |
| 1659 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1660 | switch (pm_mnt.pm_prot) { |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1661 | case IPPROTO_UDP: |
| 1662 | mclient = clntudp_create(&mount_server_addr, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1663 | pm_mnt.pm_prog, |
| 1664 | pm_mnt.pm_vers, |
| 1665 | retry_timeout, |
| 1666 | &msock); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1667 | if (mclient) |
| 1668 | break; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1669 | mount_server_addr.sin_port = htons(pm_mnt.pm_port); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1670 | msock = RPC_ANYSOCK; |
| 1671 | case IPPROTO_TCP: |
| 1672 | mclient = clnttcp_create(&mount_server_addr, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1673 | pm_mnt.pm_prog, |
| 1674 | pm_mnt.pm_vers, |
| 1675 | &msock, 0, 0); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1676 | break; |
| 1677 | default: |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1678 | mclient = NULL; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1679 | } |
| 1680 | if (!mclient) { |
| 1681 | if (!daemonized && prevt == 0) |
| 1682 | error_msg_rpc(clnt_spcreateerror(" ")); |
| 1683 | } else { |
| 1684 | enum clnt_stat clnt_stat; |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1685 | |
| 1686 | /* Try to mount hostname:pathname */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1687 | mclient->cl_auth = authunix_create_default(); |
| 1688 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1689 | /* Make pointers in xdr_mountres3 NULL so |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1690 | * that xdr_array allocates memory for us |
| 1691 | */ |
| 1692 | memset(&status, 0, sizeof(status)); |
| 1693 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 1694 | if (pm_mnt.pm_vers == 3) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1695 | clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1696 | (xdrproc_t) xdr_dirpath, |
| 1697 | (caddr_t) &pathname, |
| 1698 | (xdrproc_t) xdr_mountres3, |
| 1699 | (caddr_t) &status, |
| 1700 | total_timeout); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1701 | else |
| 1702 | clnt_stat = clnt_call(mclient, MOUNTPROC_MNT, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1703 | (xdrproc_t) xdr_dirpath, |
| 1704 | (caddr_t) &pathname, |
| 1705 | (xdrproc_t) xdr_fhstatus, |
| 1706 | (caddr_t) &status, |
| 1707 | total_timeout); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (clnt_stat == RPC_SUCCESS) |
| 1710 | goto prepare_kernel_data; /* we're done */ |
| 1711 | if (errno != ECONNREFUSED) { |
| 1712 | error_msg_rpc(clnt_sperror(mclient, " ")); |
| 1713 | goto fail; /* don't retry */ |
| 1714 | } |
| 1715 | /* Connection refused */ |
| 1716 | if (!daemonized && prevt == 0) /* print just once */ |
| 1717 | error_msg_rpc(clnt_sperror(mclient, " ")); |
| 1718 | auth_destroy(mclient->cl_auth); |
| 1719 | clnt_destroy(mclient); |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1720 | mclient = NULL; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1721 | close(msock); |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1722 | msock = -1; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* Timeout. We are going to retry... maybe */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1726 | if (!bg) |
| 1727 | goto fail; |
| 1728 | if (!daemonized) { |
| 1729 | daemonized = daemonize(); |
| 1730 | if (daemonized <= 0) { /* parent or error */ |
| 1731 | retval = -daemonized; |
| 1732 | goto ret; |
| 1733 | } |
| 1734 | } |
| 1735 | prevt = t; |
| 1736 | t = time(NULL); |
| 1737 | if (t >= timeout) |
| 1738 | /* TODO error message */ |
| 1739 | goto fail; |
| 1740 | |
| 1741 | goto retry; |
| 1742 | } |
| 1743 | |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 1744 | prepare_kernel_data: |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1745 | |
| 1746 | if (nfsvers == 2) { |
| 1747 | if (status.nfsv2.fhs_status != 0) { |
| 1748 | bb_error_msg("%s:%s failed, reason given by server: %s", |
| 1749 | hostname, pathname, |
| 1750 | nfs_strerror(status.nfsv2.fhs_status)); |
| 1751 | goto fail; |
| 1752 | } |
| 1753 | memcpy(data.root.data, |
| 1754 | (char *) status.nfsv2.fhstatus_u.fhs_fhandle, |
| 1755 | NFS_FHSIZE); |
| 1756 | data.root.size = NFS_FHSIZE; |
| 1757 | memcpy(data.old_root.data, |
| 1758 | (char *) status.nfsv2.fhstatus_u.fhs_fhandle, |
| 1759 | NFS_FHSIZE); |
| 1760 | } else { |
| 1761 | fhandle3 *my_fhandle; |
| 1762 | if (status.nfsv3.fhs_status != 0) { |
| 1763 | bb_error_msg("%s:%s failed, reason given by server: %s", |
| 1764 | hostname, pathname, |
| 1765 | nfs_strerror(status.nfsv3.fhs_status)); |
| 1766 | goto fail; |
| 1767 | } |
| 1768 | my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle; |
| 1769 | memset(data.old_root.data, 0, NFS_FHSIZE); |
| 1770 | memset(&data.root, 0, sizeof(data.root)); |
| 1771 | data.root.size = my_fhandle->fhandle3_len; |
| 1772 | memcpy(data.root.data, |
| 1773 | (char *) my_fhandle->fhandle3_val, |
| 1774 | my_fhandle->fhandle3_len); |
| 1775 | |
| 1776 | data.flags |= NFS_MOUNT_VER3; |
| 1777 | } |
| 1778 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1779 | /* Create nfs socket for kernel */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1780 | if (tcp) { |
| 1781 | if (nfs_mount_version < 3) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1782 | bb_simple_error_msg("NFS over TCP is not supported"); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1783 | goto fail; |
| 1784 | } |
| 1785 | fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1786 | } else |
| 1787 | fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 1788 | if (fsock < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1789 | bb_simple_perror_msg("nfs socket"); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1790 | goto fail; |
| 1791 | } |
| 1792 | if (bindresvport(fsock, 0) < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1793 | bb_simple_perror_msg("nfs bindresvport"); |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1794 | goto fail; |
| 1795 | } |
| 1796 | if (port == 0) { |
| 1797 | server_addr.sin_port = PMAPPORT; |
| 1798 | port = pmap_getport(&server_addr, nfsprog, nfsvers, |
| 1799 | tcp ? IPPROTO_TCP : IPPROTO_UDP); |
| 1800 | if (port == 0) |
| 1801 | port = NFS_PORT; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1802 | } |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1803 | server_addr.sin_port = htons(port); |
| 1804 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1805 | /* Prepare data structure for kernel */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1806 | data.fd = fsock; |
| 1807 | memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr)); |
| 1808 | strncpy(data.hostname, hostname, sizeof(data.hostname)); |
| 1809 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1810 | /* Clean up */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1811 | auth_destroy(mclient->cl_auth); |
| 1812 | clnt_destroy(mclient); |
| 1813 | close(msock); |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1814 | msock = -1; |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1815 | |
| 1816 | if (bg) { |
| 1817 | /* We must wait until mount directory is available */ |
| 1818 | struct stat statbuf; |
| 1819 | int delay = 1; |
| 1820 | while (stat(mp->mnt_dir, &statbuf) == -1) { |
| 1821 | if (!daemonized) { |
| 1822 | daemonized = daemonize(); |
| 1823 | if (daemonized <= 0) { /* parent or error */ |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1824 | /* FIXME: parent doesn't close fsock - ??! */ |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1825 | retval = -daemonized; |
| 1826 | goto ret; |
| 1827 | } |
| 1828 | } |
| 1829 | sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */ |
| 1830 | delay *= 2; |
| 1831 | if (delay > 30) |
| 1832 | delay = 30; |
| 1833 | } |
| 1834 | } |
| 1835 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1836 | /* Perform actual mount */ |
| 1837 | do_mount: |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1838 | retval = mount_it_now(mp, vfsflags, (char*)&data); |
| 1839 | goto ret; |
| 1840 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1841 | /* Abort */ |
| 1842 | fail: |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1843 | if (msock >= 0) { |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1844 | if (mclient) { |
| 1845 | auth_destroy(mclient->cl_auth); |
| 1846 | clnt_destroy(mclient); |
| 1847 | } |
| 1848 | close(msock); |
| 1849 | } |
Denis Vlasenko | 7d8de4d | 2007-08-28 11:23:23 +0000 | [diff] [blame] | 1850 | if (fsock >= 0) |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1851 | close(fsock); |
| 1852 | |
Denis Vlasenko | 63430ae | 2007-10-29 19:18:39 +0000 | [diff] [blame] | 1853 | ret: |
Denis Vlasenko | 25098f7 | 2006-09-14 15:46:33 +0000 | [diff] [blame] | 1854 | free(hostname); |
| 1855 | free(mounthost); |
| 1856 | free(filteropts); |
| 1857 | return retval; |
| 1858 | } |
| 1859 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1860 | #else // !ENABLE_FEATURE_MOUNT_NFS |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1861 | |
Denys Vlasenko | a86e024 | 2011-11-10 16:53:35 +0100 | [diff] [blame] | 1862 | /* Linux 2.6.23+ supports nfs mounts with options passed as a string. |
| 1863 | * For older kernels, you must build busybox with ENABLE_FEATURE_MOUNT_NFS. |
| 1864 | * (However, note that then you lose any chances that NFS over IPv6 would work). |
| 1865 | */ |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 1866 | static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts) |
Denys Vlasenko | a86e024 | 2011-11-10 16:53:35 +0100 | [diff] [blame] | 1867 | { |
| 1868 | len_and_sockaddr *lsa; |
| 1869 | char *opts; |
| 1870 | char *end; |
| 1871 | char *dotted; |
| 1872 | int ret; |
| 1873 | |
| 1874 | # if ENABLE_FEATURE_IPV6 |
| 1875 | end = strchr(mp->mnt_fsname, ']'); |
| 1876 | if (end && end[1] == ':') |
| 1877 | end++; |
| 1878 | else |
| 1879 | # endif |
| 1880 | /* mount_main() guarantees that ':' is there */ |
| 1881 | end = strchr(mp->mnt_fsname, ':'); |
| 1882 | |
| 1883 | *end = '\0'; |
Denys Vlasenko | 39b2331 | 2011-11-10 17:01:39 +0100 | [diff] [blame] | 1884 | lsa = xhost2sockaddr(mp->mnt_fsname, /*port:*/ 0); |
Denys Vlasenko | a86e024 | 2011-11-10 16:53:35 +0100 | [diff] [blame] | 1885 | *end = ':'; |
| 1886 | dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); |
| 1887 | if (ENABLE_FEATURE_CLEAN_UP) free(lsa); |
| 1888 | opts = xasprintf("%s%saddr=%s", |
| 1889 | filteropts ? filteropts : "", |
| 1890 | filteropts ? "," : "", |
| 1891 | dotted |
| 1892 | ); |
| 1893 | if (ENABLE_FEATURE_CLEAN_UP) free(dotted); |
| 1894 | ret = mount_it_now(mp, vfsflags, opts); |
| 1895 | if (ENABLE_FEATURE_CLEAN_UP) free(opts); |
| 1896 | |
| 1897 | return ret; |
| 1898 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1899 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1900 | #endif // !ENABLE_FEATURE_MOUNT_NFS |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1901 | |
Steffen Trumtrar | 6561e07 | 2020-07-29 10:43:53 +0200 | [diff] [blame] | 1902 | // Find "...,NAME=NUM,..." in the option string, remove "NAME=NUM" option |
| 1903 | // and return NUM. |
| 1904 | // Return 0 if not found. |
| 1905 | // All instances must be parsed and removed (for example, since kernel 5.4 |
| 1906 | // squashfs: Unknown parameter 'sizelimit' |
| 1907 | // will result if loopback mount option "sizelimit=NNN" is not removed |
| 1908 | // and squashfs sees it in option string). |
| 1909 | static unsigned long long cut_out_ull_opt(char *opts, const char *name_eq) |
| 1910 | { |
| 1911 | unsigned long long ret = 0; |
| 1912 | |
| 1913 | if (!opts) // allow NULL opts (simplifies callers' work) |
| 1914 | return ret; |
| 1915 | |
| 1916 | for (;;) { |
| 1917 | char *end; |
| 1918 | char *opt; |
| 1919 | |
| 1920 | // Find comma-delimited "NAME=" |
| 1921 | for (;;) { |
| 1922 | opt = strstr(opts, name_eq); |
| 1923 | if (!opt) |
| 1924 | return ret; |
| 1925 | if (opt == opts) |
| 1926 | break; // found it (it's first opt) |
| 1927 | if (opt[-1] == ',') { |
| 1928 | opts = opt - 1; |
| 1929 | break; // found it (it's not a first opt) |
| 1930 | } |
| 1931 | // False positive like "VNAME=", we are at "N". |
| 1932 | // - skip it, loop back to searching |
| 1933 | opts = opt + 1; |
| 1934 | } |
| 1935 | |
| 1936 | ret = bb_strtoull(opt + strlen(name_eq), &end, 0); |
| 1937 | if (errno && errno != EINVAL) { |
| 1938 | err: |
| 1939 | bb_error_msg_and_die("bad option '%s'", opt); |
| 1940 | } |
| 1941 | if (*end == '\0') { |
| 1942 | // It is "[,]NAME=NUM\0" - truncate it and return |
| 1943 | *opts = '\0'; |
| 1944 | return ret; |
| 1945 | } |
| 1946 | if (*end != ',') |
| 1947 | goto err; |
| 1948 | // We are at trailing comma |
| 1949 | // Remove "NAME=NUM," and loop back to check for duplicate opts |
| 1950 | overlapping_strcpy(opt, end + 1); |
| 1951 | } |
| 1952 | } |
| 1953 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1954 | // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem |
| 1955 | // type detection. Returns 0 for success, nonzero for failure. |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 1956 | // NB: mp->xxx fields may be trashed on exit |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1957 | static int singlemount(struct mntent *mp, int ignore_busy) |
| 1958 | { |
Denys Vlasenko | ab518ee | 2017-03-16 16:49:37 +0100 | [diff] [blame] | 1959 | int loopfd = -1; |
Denis Vlasenko | b413368 | 2008-02-18 13:05:38 +0000 | [diff] [blame] | 1960 | int rc = -1; |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 1961 | unsigned long vfsflags; |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 1962 | char *loopFile = NULL, *filteropts = NULL; |
| 1963 | llist_t *fl = NULL; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1964 | struct stat st; |
| 1965 | |
Denys Vlasenko | b7a0e13 | 2009-12-15 16:36:14 +0100 | [diff] [blame] | 1966 | errno = 0; |
| 1967 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1968 | vfsflags = parse_mount_options(mp->mnt_opts, &filteropts); |
| 1969 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 1970 | // Treat fstype "auto" as unspecified |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 1971 | if (mp->mnt_type && strcmp(mp->mnt_type, "auto") == 0) |
| 1972 | mp->mnt_type = NULL; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 1973 | |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 1974 | // Might this be a virtual filesystem? |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 1975 | if (ENABLE_FEATURE_MOUNT_HELPERS && strchr(mp->mnt_fsname, '#')) { |
| 1976 | char *args[35]; |
| 1977 | char *s; |
| 1978 | int n; |
| 1979 | // fsname: "cmd#arg1#arg2..." |
| 1980 | // WARNING: allows execution of arbitrary commands! |
| 1981 | // Try "mount 'sh#-c#sh' bogus_dir". |
| 1982 | // It is safe ONLY because non-root |
| 1983 | // cannot use two-argument mount command |
| 1984 | // and using one-argument "mount 'sh#-c#sh'" doesn't work: |
| 1985 | // "mount: can't find sh#-c#sh in /etc/fstab" |
| 1986 | // (if /etc/fstab has it, it's ok: root sets up /etc/fstab). |
| 1987 | |
| 1988 | s = mp->mnt_fsname; |
| 1989 | n = 0; |
| 1990 | args[n++] = s; |
| 1991 | while (*s && n < 35 - 2) { |
| 1992 | if (*s++ == '#' && *s != '#') { |
| 1993 | s[-1] = '\0'; |
| 1994 | args[n++] = s; |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 1997 | args[n++] = mp->mnt_dir; |
| 1998 | args[n] = NULL; |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 1999 | rc = spawn_and_wait(args); |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 2000 | goto report_error; |
| 2001 | } |
| 2002 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2003 | // Might this be an CIFS filesystem? |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2004 | if (ENABLE_FEATURE_MOUNT_CIFS |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 2005 | && (!mp->mnt_type || strcmp(mp->mnt_type, "cifs") == 0) |
| 2006 | && (mp->mnt_fsname[0] == '/' || mp->mnt_fsname[0] == '\\') |
| 2007 | && mp->mnt_fsname[0] == mp->mnt_fsname[1] |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2008 | ) { |
Denys Vlasenko | 4e60f30 | 2009-12-15 01:28:59 +0100 | [diff] [blame] | 2009 | int len; |
| 2010 | char c; |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2011 | char *hostname, *share; |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 2012 | len_and_sockaddr *lsa; |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2013 | |
| 2014 | // Parse mp->mnt_fsname of the form "//hostname/share[/dir1/dir2]" |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2015 | |
Denys Vlasenko | b7a0e13 | 2009-12-15 16:36:14 +0100 | [diff] [blame] | 2016 | hostname = mp->mnt_fsname + 2; |
| 2017 | len = strcspn(hostname, "/\\"); |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2018 | share = hostname + len + 1; |
| 2019 | if (len == 0 // 3rd char is a [back]slash (IOW: empty hostname) |
| 2020 | || share[-1] == '\0' // no [back]slash after hostname |
| 2021 | || share[0] == '\0' // empty share name |
Martin Santesson | 406ea15 | 2013-01-16 00:47:19 +0100 | [diff] [blame] | 2022 | ) { |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 2023 | goto report_error; |
Martin Santesson | 406ea15 | 2013-01-16 00:47:19 +0100 | [diff] [blame] | 2024 | } |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2025 | c = share[-1]; |
| 2026 | share[-1] = '\0'; |
| 2027 | len = strcspn(share, "/\\"); |
Martin Santesson | 406ea15 | 2013-01-16 00:47:19 +0100 | [diff] [blame] | 2028 | |
| 2029 | // "unc=\\hostname\share" option is mandatory |
| 2030 | // after CIFS option parsing was rewritten in Linux 3.4. |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2031 | // Must use backslashes. |
| 2032 | // If /dir1/dir2 is present, also add "prefixpath=dir1/dir2" |
Martin Santesson | 406ea15 | 2013-01-16 00:47:19 +0100 | [diff] [blame] | 2033 | { |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2034 | char *unc = xasprintf( |
| 2035 | share[len] != '\0' /* "/dir1/dir2" exists? */ |
| 2036 | ? "unc=\\\\%s\\%.*s,prefixpath=%s" |
| 2037 | : "unc=\\\\%s\\%.*s", |
| 2038 | hostname, |
| 2039 | len, share, |
| 2040 | share + len + 1 /* "dir1/dir2" */ |
| 2041 | ); |
Denys Vlasenko | 9b7ebfe | 2013-01-22 11:00:45 +0100 | [diff] [blame] | 2042 | parse_mount_options(unc, &filteropts); |
| 2043 | if (ENABLE_FEATURE_CLEAN_UP) free(unc); |
Martin Santesson | 406ea15 | 2013-01-16 00:47:19 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Denys Vlasenko | b7a0e13 | 2009-12-15 16:36:14 +0100 | [diff] [blame] | 2046 | lsa = host2sockaddr(hostname, 0); |
Bernhard Reutner-Fischer | 20c5e5a | 2013-01-17 02:30:35 +0100 | [diff] [blame] | 2047 | share[-1] = c; |
Denis Vlasenko | 5c32993 | 2009-04-12 12:16:21 +0000 | [diff] [blame] | 2048 | if (!lsa) |
| 2049 | goto report_error; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2050 | |
Denys Vlasenko | b09ab44 | 2016-09-27 21:02:35 +0200 | [diff] [blame] | 2051 | // If there is no "ip=..." option yet |
| 2052 | if (!is_prefixed_with(filteropts, ",ip="+1) |
| 2053 | && !strstr(filteropts, ",ip=") |
| 2054 | ) { |
| 2055 | char *dotted, *ip; |
| 2056 | // Insert "ip=..." option into options |
| 2057 | dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); |
| 2058 | if (ENABLE_FEATURE_CLEAN_UP) free(lsa); |
| 2059 | ip = xasprintf("ip=%s", dotted); |
| 2060 | if (ENABLE_FEATURE_CLEAN_UP) free(dotted); |
Denys Vlasenko | 5093c8c | 2016-09-26 20:36:30 +0200 | [diff] [blame] | 2061 | // Note: IPv6 scoped addresses ("host%iface", see RFC 4007) should be |
Denys Vlasenko | 3c18e30 | 2016-09-26 19:53:04 +0200 | [diff] [blame] | 2062 | // handled by libc in getnameinfo() (inside xmalloc_sockaddr2dotted_noport()). |
| 2063 | // Currently, glibc does not support that (has no NI_NUMERICSCOPE), |
| 2064 | // musl apparently does. This results in "ip=numericIPv6%iface_name" |
| 2065 | // (instead of _numeric_ iface_id) with glibc. |
Denys Vlasenko | 5093c8c | 2016-09-26 20:36:30 +0200 | [diff] [blame] | 2066 | // This probably should be fixed in glibc, not here. |
Denys Vlasenko | b09ab44 | 2016-09-27 21:02:35 +0200 | [diff] [blame] | 2067 | // The workaround is to manually specify correct "ip=ADDR%n" option. |
| 2068 | parse_mount_options(ip, &filteropts); |
| 2069 | if (ENABLE_FEATURE_CLEAN_UP) free(ip); |
| 2070 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2071 | |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 2072 | mp->mnt_type = (char*)"cifs"; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2073 | rc = mount_it_now(mp, vfsflags, filteropts); |
Denys Vlasenko | 4e60f30 | 2009-12-15 01:28:59 +0100 | [diff] [blame] | 2074 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2075 | goto report_error; |
| 2076 | } |
| 2077 | |
| 2078 | // Might this be an NFS filesystem? |
Denys Vlasenko | b1eedfc | 2020-12-05 14:14:11 +0100 | [diff] [blame] | 2079 | if (!(vfsflags & (MS_BIND | MS_MOVE)) |
| 2080 | && (!mp->mnt_type || is_prefixed_with(mp->mnt_type, "nfs")) |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2081 | ) { |
Denys Vlasenko | b1eedfc | 2020-12-05 14:14:11 +0100 | [diff] [blame] | 2082 | char *colon = strchr(mp->mnt_fsname, ':'); |
Denys Vlasenko | 56ee576 | 2020-12-09 20:56:43 +0100 | [diff] [blame] | 2083 | if (colon // looks like "hostname:..." |
| 2084 | && strchrnul(mp->mnt_fsname, '/') > colon // "hostname:" has no slashes |
Denys Vlasenko | b1eedfc | 2020-12-05 14:14:11 +0100 | [diff] [blame] | 2085 | ) { |
| 2086 | if (!mp->mnt_type) |
| 2087 | mp->mnt_type = (char*)"nfs"; |
| 2088 | rc = nfsmount(mp, vfsflags, filteropts); |
| 2089 | goto report_error; |
| 2090 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | // Look at the file. (Not found isn't a failure for remount, or for |
| 2094 | // a synthetic filesystem like proc or sysfs.) |
Denis Vlasenko | 38ec147 | 2007-05-20 12:32:41 +0000 | [diff] [blame] | 2095 | // (We use stat, not lstat, in order to allow |
| 2096 | // mount symlink_to_file_or_blkdev dir) |
Denis Vlasenko | 38ec147 | 2007-05-20 12:32:41 +0000 | [diff] [blame] | 2097 | if (!stat(mp->mnt_fsname, &st) |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2098 | && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)) |
| 2099 | ) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2100 | // Do we need to allocate a loopback device for it? |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2101 | if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) { |
Steffen Trumtrar | 6561e07 | 2020-07-29 10:43:53 +0200 | [diff] [blame] | 2102 | unsigned long long offset; |
| 2103 | unsigned long long sizelimit; |
| 2104 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2105 | loopFile = bb_simplify_path(mp->mnt_fsname); |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 2106 | mp->mnt_fsname = NULL; // will receive malloced loop dev name |
Denys Vlasenko | ab518ee | 2017-03-16 16:49:37 +0100 | [diff] [blame] | 2107 | |
Steffen Trumtrar | 6561e07 | 2020-07-29 10:43:53 +0200 | [diff] [blame] | 2108 | // Parse and remove loopback options |
| 2109 | offset = cut_out_ull_opt(filteropts, "offset="); |
| 2110 | sizelimit = cut_out_ull_opt(filteropts, "sizelimit="); |
| 2111 | |
Denys Vlasenko | ab518ee | 2017-03-16 16:49:37 +0100 | [diff] [blame] | 2112 | // mount always creates AUTOCLEARed loopdevs, so that umounting |
| 2113 | // drops them without any code in the userspace. |
| 2114 | // This happens since circa linux-2.6.25: |
| 2115 | // commit 96c5865559cee0f9cbc5173f3c949f6ce3525581 |
| 2116 | // Date: Wed Feb 6 01:36:27 2008 -0800 |
| 2117 | // Subject: Allow auto-destruction of loop devices |
| 2118 | loopfd = set_loop(&mp->mnt_fsname, |
| 2119 | loopFile, |
Steffen Trumtrar | 6561e07 | 2020-07-29 10:43:53 +0200 | [diff] [blame] | 2120 | offset, |
| 2121 | sizelimit, |
Denys Vlasenko | ab518ee | 2017-03-16 16:49:37 +0100 | [diff] [blame] | 2122 | ((vfsflags & MS_RDONLY) ? BB_LO_FLAGS_READ_ONLY : 0) |
| 2123 | | BB_LO_FLAGS_AUTOCLEAR |
| 2124 | ); |
| 2125 | if (loopfd < 0) { |
Denis Vlasenko | 0e2c9fb | 2007-08-03 14:16:24 +0000 | [diff] [blame] | 2126 | if (errno == EPERM || errno == EACCES) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2127 | bb_simple_error_msg(bb_msg_perm_denied_are_you_root); |
Denis Vlasenko | 0e2c9fb | 2007-08-03 14:16:24 +0000 | [diff] [blame] | 2128 | else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2129 | bb_simple_perror_msg("can't setup loop device"); |
Denys Vlasenko | 56ee576 | 2020-12-09 20:56:43 +0100 | [diff] [blame] | 2130 | return loopfd; // was "return errno", but it can be 0 here |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | // Autodetect bind mounts |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2134 | } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) |
| 2135 | vfsflags |= MS_BIND; |
| 2136 | } |
| 2137 | |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 2138 | // If we know the fstype (or don't need to), jump straight |
| 2139 | // to the actual mount. |
Denys Vlasenko | fa1b370 | 2010-06-27 16:47:40 +0200 | [diff] [blame] | 2140 | if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) { |
Karol Lewandowski | b5ebe5f | 2011-11-03 10:02:31 +0100 | [diff] [blame] | 2141 | char *next; |
| 2142 | for (;;) { |
| 2143 | next = mp->mnt_type ? strchr(mp->mnt_type, ',') : NULL; |
| 2144 | if (next) |
| 2145 | *next = '\0'; |
| 2146 | rc = mount_it_now(mp, vfsflags, filteropts); |
| 2147 | if (rc == 0 || !next) |
| 2148 | break; |
| 2149 | mp->mnt_type = next + 1; |
| 2150 | } |
Denys Vlasenko | fa1b370 | 2010-06-27 16:47:40 +0200 | [diff] [blame] | 2151 | } else { |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2152 | // Loop through filesystem types until mount succeeds |
| 2153 | // or we run out |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2154 | |
Denys Vlasenko | b7a0e13 | 2009-12-15 16:36:14 +0100 | [diff] [blame] | 2155 | // Initialize list of block backed filesystems. |
| 2156 | // This has to be done here so that during "mount -a", |
| 2157 | // mounts after /proc shows up can autodetect. |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2158 | if (!fslist) { |
| 2159 | fslist = get_block_backed_filesystems(); |
| 2160 | if (ENABLE_FEATURE_CLEAN_UP && fslist) |
| 2161 | atexit(delete_block_backed_filesystems); |
| 2162 | } |
| 2163 | |
| 2164 | for (fl = fslist; fl; fl = fl->link) { |
| 2165 | mp->mnt_type = fl->data; |
Denis Vlasenko | 13b4924 | 2006-09-17 15:04:35 +0000 | [diff] [blame] | 2166 | rc = mount_it_now(mp, vfsflags, filteropts); |
Karol Lewandowski | b5ebe5f | 2011-11-03 10:02:31 +0100 | [diff] [blame] | 2167 | if (rc == 0) |
Denys Vlasenko | e2e4cc2 | 2009-06-19 11:48:29 +0200 | [diff] [blame] | 2168 | break; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | // If mount failed, clean up loop file (if any). |
Denys Vlasenko | ab518ee | 2017-03-16 16:49:37 +0100 | [diff] [blame] | 2173 | // (Newer kernels which support LO_FLAGS_AUTOCLEAR should not need this, |
| 2174 | // merely "close(loopfd)" should do it?) |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2175 | if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) { |
| 2176 | del_loop(mp->mnt_fsname); |
| 2177 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 2178 | free(loopFile); |
Denys Vlasenko | ecf25cb | 2016-06-20 11:04:04 +0200 | [diff] [blame] | 2179 | /* No, "rc != 0" needs it: free(mp->mnt_fsname); */ |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
| 2182 | |
Denis Vlasenko | 5870ad9 | 2007-02-04 02:39:55 +0000 | [diff] [blame] | 2183 | report_error: |
| 2184 | if (ENABLE_FEATURE_CLEAN_UP) |
| 2185 | free(filteropts); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2186 | |
Denys Vlasenko | ab518ee | 2017-03-16 16:49:37 +0100 | [diff] [blame] | 2187 | if (loopfd >= 0) |
| 2188 | close(loopfd); |
| 2189 | |
Denis Vlasenko | c8d4d2f | 2007-09-07 19:33:56 +0000 | [diff] [blame] | 2190 | if (errno == EBUSY && ignore_busy) |
| 2191 | return 0; |
Denys Vlasenko | 911d265 | 2015-12-30 20:11:34 +0100 | [diff] [blame] | 2192 | if (errno == ENOENT && (vfsflags & MOUNT_NOFAIL)) |
| 2193 | return 0; |
Denys Vlasenko | fa1b370 | 2010-06-27 16:47:40 +0200 | [diff] [blame] | 2194 | if (rc != 0) |
Denis Vlasenko | 0e2c9fb | 2007-08-03 14:16:24 +0000 | [diff] [blame] | 2195 | bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2196 | return rc; |
| 2197 | } |
| 2198 | |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2199 | // -O support |
| 2200 | // -O interprets a list of filter options which select whether a mount |
| 2201 | // point will be mounted: only mounts with options matching *all* filtering |
| 2202 | // options will be selected. |
| 2203 | // By default each -O filter option must be present in the list of mount |
| 2204 | // options, but if it is prefixed by "no" then it must be absent. |
| 2205 | // For example, |
| 2206 | // -O a,nob,c matches -o a,c but fails to match -o a,b,c |
| 2207 | // (and also fails to match -o a because -o c is absent). |
| 2208 | // |
| 2209 | // It is different from -t in that each option is matched exactly; a leading |
| 2210 | // "no" at the beginning of one option does not negate the rest. |
| 2211 | static int match_opt(const char *fs_opt_in, const char *O_opt) |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2212 | { |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2213 | if (!O_opt) |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2214 | return 1; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2215 | |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2216 | while (*O_opt) { |
| 2217 | const char *fs_opt = fs_opt_in; |
| 2218 | int O_len; |
| 2219 | int match; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2220 | |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2221 | // If option begins with "no" then treat as an inverted match: |
| 2222 | // matching is a failure |
| 2223 | match = 0; |
| 2224 | if (O_opt[0] == 'n' && O_opt[1] == 'o') { |
| 2225 | match = 1; |
| 2226 | O_opt += 2; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2227 | } |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2228 | // Isolate the current O option |
| 2229 | O_len = strchrnul(O_opt, ',') - O_opt; |
| 2230 | // Check for a match against existing options |
| 2231 | while (1) { |
| 2232 | if (strncmp(fs_opt, O_opt, O_len) == 0 |
| 2233 | && (fs_opt[O_len] == '\0' || fs_opt[O_len] == ',') |
| 2234 | ) { |
| 2235 | if (match) |
| 2236 | return 0; // "no" prefix, but option found |
| 2237 | match = 1; // current O option found, go check next one |
| 2238 | break; |
| 2239 | } |
| 2240 | fs_opt = strchr(fs_opt, ','); |
| 2241 | if (!fs_opt) |
| 2242 | break; |
| 2243 | fs_opt++; |
| 2244 | } |
| 2245 | if (match == 0) |
| 2246 | return 0; // match wanted but not found |
| 2247 | if (O_opt[O_len] == '\0') // end? |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2248 | break; |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2249 | // Step to the next O option |
| 2250 | O_opt += O_len + 1; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2251 | } |
Michael Abbott | 6b5accb | 2009-12-04 03:33:07 +0100 | [diff] [blame] | 2252 | // If we get here then everything matched |
| 2253 | return 1; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2256 | // Parse options, if necessary parse fstab/mtab, and call singlemount for |
| 2257 | // each directory to be mounted. |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 2258 | int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2259 | int mount_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2260 | { |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2261 | char *cmdopts = xzalloc(1); |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 2262 | char *fstype = NULL; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2263 | char *O_optmatch = NULL; |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2264 | char *storage_path; |
Denis Vlasenko | f9dde91 | 2008-10-18 19:15:57 +0000 | [diff] [blame] | 2265 | llist_t *lst_o = NULL; |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 2266 | const char *fstabname = "/etc/fstab"; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2267 | FILE *fstab; |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2268 | int i, j; |
| 2269 | int rc = EXIT_SUCCESS; |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 2270 | unsigned long cmdopt_flags; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 2271 | unsigned opt; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2272 | struct mntent mtpair[2], *mtcur = mtpair; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2273 | IF_NOT_DESKTOP(const int nonroot = 0;) |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2274 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2275 | IF_DESKTOP(int nonroot = ) sanitize_env_if_suid(); |
Denis Vlasenko | c9ca0a3 | 2008-02-18 11:08:33 +0000 | [diff] [blame] | 2276 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 2277 | INIT_G(); |
| 2278 | |
Denis Vlasenko | f732e96 | 2008-02-18 12:07:49 +0000 | [diff] [blame] | 2279 | // Parse long options, like --bind and --move. Note that -o option |
| 2280 | // and --option are synonymous. Yes, this means --remount,rw works. |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2281 | for (i = j = 1; argv[i]; i++) { |
| 2282 | if (argv[i][0] == '-' && argv[i][1] == '-') |
| 2283 | append_mount_options(&cmdopts, argv[i] + 2); |
| 2284 | else |
| 2285 | argv[j++] = argv[i]; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2286 | } |
Denis Vlasenko | a4522c5 | 2008-03-17 08:46:43 +0000 | [diff] [blame] | 2287 | argv[j] = NULL; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2288 | |
| 2289 | // Parse remaining options |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2290 | // Max 2 params; -o is a list, -v is a counter |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 2291 | opt = getopt32(argv, "^" |
| 2292 | OPTION_STR |
| 2293 | "\0" "?2"IF_FEATURE_MOUNT_VERBOSE("vv"), |
| 2294 | &lst_o, &fstype, &O_optmatch |
| 2295 | IF_FEATURE_MOUNT_OTHERTAB(, &fstabname) |
| 2296 | IF_FEATURE_MOUNT_VERBOSE(, &verbose) |
| 2297 | ); |
| 2298 | |
Denis Vlasenko | f9dde91 | 2008-10-18 19:15:57 +0000 | [diff] [blame] | 2299 | while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o |
Denis Vlasenko | b1d8e7d | 2008-02-16 23:28:42 +0000 | [diff] [blame] | 2300 | if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r |
| 2301 | if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w |
Denis Vlasenko | 3bc59aa | 2006-09-17 15:04:01 +0000 | [diff] [blame] | 2302 | argv += optind; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2303 | |
| 2304 | // If we have no arguments, show currently mounted filesystems |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2305 | if (!argv[0]) { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 2306 | if (!(opt & OPT_a)) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2307 | FILE *mountTable = setmntent(bb_path_mtab_file, "r"); |
| 2308 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2309 | if (!mountTable) |
| 2310 | bb_error_msg_and_die("no %s", bb_path_mtab_file); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2311 | |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 2312 | while (getmntent_r(mountTable, &mtpair[0], getmntent_buf, |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 2313 | GETMNTENT_BUFSIZE)) |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2314 | { |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 2315 | // Don't show rootfs. FIXME: why?? |
Denis Vlasenko | 908d6b7 | 2006-12-18 23:07:42 +0000 | [diff] [blame] | 2316 | // util-linux 2.12a happily shows rootfs... |
Denys Vlasenko | 4e60f30 | 2009-12-15 01:28:59 +0100 | [diff] [blame] | 2317 | //if (strcmp(mtpair->mnt_fsname, "rootfs") == 0) continue; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2318 | |
Denys Vlasenko | 4e60f30 | 2009-12-15 01:28:59 +0100 | [diff] [blame] | 2319 | if (!fstype || strcmp(mtpair->mnt_type, fstype) == 0) |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2320 | printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname, |
| 2321 | mtpair->mnt_dir, mtpair->mnt_type, |
| 2322 | mtpair->mnt_opts); |
| 2323 | } |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2324 | if (ENABLE_FEATURE_CLEAN_UP) |
| 2325 | endmntent(mountTable); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2326 | return EXIT_SUCCESS; |
| 2327 | } |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2328 | storage_path = NULL; |
| 2329 | } else { |
| 2330 | // When we have two arguments, the second is the directory and we can |
| 2331 | // skip looking at fstab entirely. We can always abspath() the directory |
| 2332 | // argument when we get it. |
| 2333 | if (argv[1]) { |
| 2334 | if (nonroot) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2335 | bb_simple_error_msg_and_die(bb_msg_you_must_be_root); |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2336 | mtpair->mnt_fsname = argv[0]; |
| 2337 | mtpair->mnt_dir = argv[1]; |
| 2338 | mtpair->mnt_type = fstype; |
| 2339 | mtpair->mnt_opts = cmdopts; |
Denis Vlasenko | 6a2d0d9 | 2008-12-10 11:39:18 +0000 | [diff] [blame] | 2340 | resolve_mount_spec(&mtpair->mnt_fsname); |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2341 | rc = singlemount(mtpair, /*ignore_busy:*/ 0); |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2342 | return rc; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 2343 | } |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2344 | storage_path = bb_simplify_path(argv[0]); // malloced |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2347 | // Past this point, we are handling either "mount -a [opts]" |
| 2348 | // or "mount [opts] single_param" |
| 2349 | |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 2350 | cmdopt_flags = parse_mount_options(cmdopts, NULL); |
| 2351 | if (nonroot && (cmdopt_flags & ~MS_SILENT)) // Non-root users cannot specify flags |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2352 | bb_simple_error_msg_and_die(bb_msg_you_must_be_root); |
Denis Vlasenko | 546cd18 | 2006-10-02 18:52:49 +0000 | [diff] [blame] | 2353 | |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2354 | // If we have a shared subtree flag, don't worry about fstab or mtab. |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2355 | if (ENABLE_FEATURE_MOUNT_FLAGS |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 2356 | && (cmdopt_flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) |
Denis Vlasenko | 6cd2d2b | 2007-01-22 14:06:03 +0000 | [diff] [blame] | 2357 | ) { |
Denis Vlasenko | 30e5cf8 | 2008-12-05 16:40:36 +0000 | [diff] [blame] | 2358 | // verbose_mount(source, target, type, flags, data) |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 2359 | rc = verbose_mount("", argv[0], "", cmdopt_flags, ""); |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2360 | if (rc) |
| 2361 | bb_simple_perror_msg_and_die(argv[0]); |
| 2362 | return rc; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2363 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 2364 | |
Isaac Dunham | 7b434a6 | 2015-03-11 16:07:24 +0100 | [diff] [blame] | 2365 | // A malicious user could overmount /usr without this. |
| 2366 | if (ENABLE_FEATURE_MOUNT_OTHERTAB && nonroot) |
| 2367 | fstabname = "/etc/fstab"; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2368 | // Open either fstab or mtab |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 2369 | if (cmdopt_flags & MS_REMOUNT) { |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2370 | // WARNING. I am not sure this matches util-linux's |
| 2371 | // behavior. It's possible util-linux does not |
| 2372 | // take -o opts from mtab (takes only mount source). |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2373 | fstabname = bb_path_mtab_file; |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 2374 | } |
| 2375 | fstab = setmntent(fstabname, "r"); |
Denis Vlasenko | 8d474b5 | 2006-09-17 15:00:58 +0000 | [diff] [blame] | 2376 | if (!fstab) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 2377 | bb_perror_msg_and_die("can't read '%s'", fstabname); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2378 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2379 | // Loop through entries until we find what we're looking for |
Denis Vlasenko | 546cd18 | 2006-10-02 18:52:49 +0000 | [diff] [blame] | 2380 | memset(mtpair, 0, sizeof(mtpair)); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2381 | for (;;) { |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2382 | struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2383 | |
| 2384 | // Get next fstab entry |
Denis Vlasenko | 2535f12 | 2007-09-15 13:28:30 +0000 | [diff] [blame] | 2385 | if (!getmntent_r(fstab, mtcur, getmntent_buf |
Denis Vlasenko | d0a071a | 2008-03-17 09:33:45 +0000 | [diff] [blame] | 2386 | + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0), |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2387 | GETMNTENT_BUFSIZE/2) |
| 2388 | ) { // End of fstab/mtab is reached |
| 2389 | mtcur = mtother; // the thing we found last time |
| 2390 | break; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2393 | // If we're trying to mount something specific and this isn't it, |
| 2394 | // skip it. Note we must match the exact text in fstab (ala |
| 2395 | // "proc") or a full path from root |
| 2396 | if (argv[0]) { |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2397 | |
| 2398 | // Is this what we're looking for? |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2399 | if (strcmp(argv[0], mtcur->mnt_fsname) != 0 |
| 2400 | && strcmp(storage_path, mtcur->mnt_fsname) != 0 |
| 2401 | && strcmp(argv[0], mtcur->mnt_dir) != 0 |
| 2402 | && strcmp(storage_path, mtcur->mnt_dir) != 0 |
| 2403 | ) { |
| 2404 | continue; // no |
| 2405 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2406 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2407 | // Remember this entry. Something later may have |
| 2408 | // overmounted it, and we want the _last_ match. |
| 2409 | mtcur = mtother; |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2410 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2411 | // If we're mounting all |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2412 | } else { |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2413 | struct mntent *mp; |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 2414 | // No, mount -a won't mount anything, |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2415 | // even user mounts, for mere humans |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 2416 | if (nonroot) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2417 | bb_simple_error_msg_and_die(bb_msg_you_must_be_root); |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 2418 | |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2419 | // Does type match? (NULL matches always) |
Denys Vlasenko | 35b54a3 | 2017-01-30 00:45:05 +0100 | [diff] [blame] | 2420 | if (!fstype_matches(mtcur->mnt_type, fstype)) |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2421 | continue; |
| 2422 | |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2423 | // Skip noauto and swap anyway |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2424 | if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP)) |
| 2425 | // swap is bogus "fstype", parse_mount_options can't check fstypes |
| 2426 | || strcasecmp(mtcur->mnt_type, "swap") == 0 |
| 2427 | ) { |
| 2428 | continue; |
| 2429 | } |
| 2430 | |
| 2431 | // Does (at least one) option match? |
| 2432 | // (NULL matches always) |
| 2433 | if (!match_opt(mtcur->mnt_opts, O_optmatch)) |
| 2434 | continue; |
| 2435 | |
| 2436 | resolve_mount_spec(&mtcur->mnt_fsname); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2437 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 2438 | // NFS mounts want this to be xrealloc-able |
| 2439 | mtcur->mnt_opts = xstrdup(mtcur->mnt_opts); |
Denis Vlasenko | 6a2d0d9 | 2008-12-10 11:39:18 +0000 | [diff] [blame] | 2440 | |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2441 | // If nothing is mounted on this directory... |
| 2442 | // (otherwise repeated "mount -a" mounts everything again) |
| 2443 | mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0); |
| 2444 | // We do not check fsname match of found mount point - |
| 2445 | // "/" may have fsname of "/dev/root" while fstab |
| 2446 | // says "/dev/something_else". |
| 2447 | if (mp) { |
Denys Vlasenko | 8656676 | 2009-12-10 21:32:28 +0100 | [diff] [blame] | 2448 | if (verbose) { |
| 2449 | bb_error_msg("according to %s, " |
| 2450 | "%s is already mounted on %s", |
| 2451 | bb_path_mtab_file, |
| 2452 | mp->mnt_fsname, mp->mnt_dir); |
| 2453 | } |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2454 | } else { |
| 2455 | // ...mount this thing |
| 2456 | if (singlemount(mtcur, /*ignore_busy:*/ 1)) { |
| 2457 | // Count number of failed mounts |
| 2458 | rc++; |
| 2459 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2460 | } |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 2461 | free(mtcur->mnt_opts); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2464 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2465 | // End of fstab/mtab is reached. |
| 2466 | // Were we looking for something specific? |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2467 | if (argv[0]) { // yes |
Denys Vlasenko | 9ee4266 | 2012-06-21 12:08:56 +0200 | [diff] [blame] | 2468 | unsigned long l; |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2469 | |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2470 | // If we didn't find anything, complain |
| 2471 | if (!mtcur->mnt_fsname) |
| 2472 | bb_error_msg_and_die("can't find %s in %s", |
| 2473 | argv[0], fstabname); |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2474 | |
| 2475 | // What happens when we try to "mount swap_partition"? |
| 2476 | // (fstab containts "swap_partition swap swap defaults 0 0") |
| 2477 | // util-linux-ng 2.13.1 does this: |
| 2478 | // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory) |
| 2479 | // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory) |
| 2480 | // lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory) |
| 2481 | // write(2, "mount: mount point swap does not exist\n", 39) = 39 |
| 2482 | // exit_group(32) = ? |
| 2483 | #if 0 |
| 2484 | // In case we want to simply skip swap partitions: |
| 2485 | l = parse_mount_options(mtcur->mnt_opts, NULL); |
| 2486 | if ((l & MOUNT_SWAP) |
| 2487 | // swap is bogus "fstype", parse_mount_options can't check fstypes |
| 2488 | || strcasecmp(mtcur->mnt_type, "swap") == 0 |
| 2489 | ) { |
| 2490 | goto ret; |
| 2491 | } |
| 2492 | #endif |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2493 | if (nonroot) { |
| 2494 | // fstab must have "users" or "user" |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2495 | l = parse_mount_options(mtcur->mnt_opts, NULL); |
| 2496 | if (!(l & MOUNT_USERS)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2497 | bb_simple_error_msg_and_die(bb_msg_you_must_be_root); |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2498 | } |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2499 | |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2500 | //util-linux-2.12 does not do this check. |
| 2501 | //// If nothing is mounted on this directory... |
| 2502 | //// (otherwise repeated "mount FOO" mounts FOO again) |
| 2503 | //mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0); |
| 2504 | //if (mp) { |
| 2505 | // bb_error_msg("according to %s, " |
| 2506 | // "%s is already mounted on %s", |
| 2507 | // bb_path_mtab_file, |
| 2508 | // mp->mnt_fsname, mp->mnt_dir); |
| 2509 | //} else { |
| 2510 | // ...mount the last thing we found |
| 2511 | mtcur->mnt_opts = xstrdup(mtcur->mnt_opts); |
| 2512 | append_mount_options(&(mtcur->mnt_opts), cmdopts); |
| 2513 | resolve_mount_spec(&mtpair->mnt_fsname); |
| 2514 | rc = singlemount(mtcur, /*ignore_busy:*/ 0); |
| 2515 | if (ENABLE_FEATURE_CLEAN_UP) |
| 2516 | free(mtcur->mnt_opts); |
| 2517 | //} |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Denis Vlasenko | 7aaedcf | 2009-03-14 22:57:20 +0000 | [diff] [blame] | 2520 | //ret: |
Denis Vlasenko | d0cc3f4 | 2008-06-24 18:59:59 +0000 | [diff] [blame] | 2521 | if (ENABLE_FEATURE_CLEAN_UP) |
| 2522 | endmntent(fstab); |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2523 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 2524 | free(storage_path); |
| 2525 | free(cmdopts); |
| 2526 | } |
Denys Vlasenko | a732966 | 2009-12-05 04:25:19 +0100 | [diff] [blame] | 2527 | |
| 2528 | //TODO: exitcode should be ORed mask of (from "man mount"): |
| 2529 | // 0 success |
| 2530 | // 1 incorrect invocation or permissions |
| 2531 | // 2 system error (out of memory, cannot fork, no more loop devices) |
| 2532 | // 4 internal mount bug or missing nfs support in mount |
| 2533 | // 8 user interrupt |
| 2534 | //16 problems writing or locking /etc/mtab |
| 2535 | //32 mount failure |
| 2536 | //64 some mount succeeded |
Denis Vlasenko | 30a64cd | 2006-09-15 15:12:00 +0000 | [diff] [blame] | 2537 | return rc; |
| 2538 | } |