1 /* vi: set sw=4 ts=4: */
3 * Mini mount implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 // Design notes: There is no spec for mount. Remind me to write one.
13 // mount_main() calls singlemount() which calls mount_it_now().
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.
21 #include <sys/mount.h>
23 # define MS_BIND (1 << 12)
26 # define MS_MOVE (1 << 13)
29 # define MS_RECURSIVE (1 << 14)
32 # define MS_SILENT (1 << 15)
34 /* The shared subtree stuff, which went in around 2.6.15. */
36 # define MS_UNBINDABLE (1 << 17)
39 # define MS_PRIVATE (1 << 18)
42 # define MS_SLAVE (1 << 19)
45 # define MS_SHARED (1 << 20)
48 # define MS_RELATIME (1 << 21)
52 #if ENABLE_FEATURE_MOUNT_LABEL
53 # include "volume_id.h"
55 # define resolve_mount_spec(fsname) ((void)0)
58 // Needed for nfs support only
59 #include <sys/utsname.h>
63 #include <rpc/pmap_prot.h>
64 #include <rpc/pmap_clnt.h>
67 #define MS_SILENT (1 << 15)
69 // Grab more as needed from util-linux's mount/mount_constants.h
71 #define MS_DIRSYNC 128 // Directory modifications are synchronous
75 #if defined(__dietlibc__)
76 // 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
77 // dietlibc-0.30 does not have implementation of getmntent_r()
78 static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
79 char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
81 struct mntent* ment = getmntent(stream);
82 return memcpy(result, ment, sizeof(*ment));
87 // Not real flags, but we want to be able to check for this.
89 MOUNT_USERS = (1 << 28) * ENABLE_DESKTOP,
90 MOUNT_NOAUTO = (1 << 29),
91 MOUNT_SWAP = (1 << 30),
95 #define OPTION_STR "o:t:rwanfvsiO:"
110 #if ENABLE_FEATURE_MTAB_SUPPORT
111 #define USE_MTAB (!(option_mask32 & OPT_n))
116 #if ENABLE_FEATURE_MOUNT_FAKE
117 #define FAKE_IT (option_mask32 & OPT_f)
122 #if ENABLE_FEATURE_MOUNT_HELPERS
123 #define HELPERS_ALLOWED (!(option_mask32 & OPT_i))
125 #define HELPERS_ALLOWED 0
129 // TODO: more "user" flag compatibility.
130 // "user" option (from mount manpage):
131 // Only the user that mounted a filesystem can unmount it again.
132 // If any user should be able to unmount, then use users instead of user
133 // in the fstab line. The owner option is similar to the user option,
134 // with the restriction that the user must be the owner of the special file.
135 // This may be useful e.g. for /dev/fd if a login script makes
136 // the console user owner of this device.
138 // Standard mount options (from -o options or --options),
139 // with corresponding flags
140 static const int32_t mount_options[] = {
141 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
143 IF_FEATURE_MOUNT_LOOP(
147 IF_FEATURE_MOUNT_FSTAB(
149 /* "quiet" 0 - do not filter out, vfat wants to see it */
150 /* "noauto" */ MOUNT_NOAUTO,
151 /* "sw" */ MOUNT_SWAP,
152 /* "swap" */ MOUNT_SWAP,
153 IF_DESKTOP(/* "user" */ MOUNT_USERS,)
154 IF_DESKTOP(/* "users" */ MOUNT_USERS,)
158 IF_FEATURE_MOUNT_FLAGS(
160 /* "nosuid" */ MS_NOSUID,
161 /* "suid" */ ~MS_NOSUID,
162 /* "dev" */ ~MS_NODEV,
163 /* "nodev" */ MS_NODEV,
164 /* "exec" */ ~MS_NOEXEC,
165 /* "noexec" */ MS_NOEXEC,
166 /* "sync" */ MS_SYNCHRONOUS,
167 /* "dirsync" */ MS_DIRSYNC,
168 /* "async" */ ~MS_SYNCHRONOUS,
169 /* "atime" */ ~MS_NOATIME,
170 /* "noatime" */ MS_NOATIME,
171 /* "diratime" */ ~MS_NODIRATIME,
172 /* "nodiratime" */ MS_NODIRATIME,
173 /* "mand" */ MS_MANDLOCK,
174 /* "nomand" */ ~MS_MANDLOCK,
175 /* "relatime" */ MS_RELATIME,
176 /* "norelatime" */ ~MS_RELATIME,
177 /* "loud" */ ~MS_SILENT,
180 /* "bind" */ MS_BIND,
181 /* "move" */ MS_MOVE,
182 /* "shared" */ MS_SHARED,
183 /* "slave" */ MS_SLAVE,
184 /* "private" */ MS_PRIVATE,
185 /* "unbindable" */ MS_UNBINDABLE,
186 /* "rshared" */ MS_SHARED|MS_RECURSIVE,
187 /* "rslave" */ MS_SLAVE|MS_RECURSIVE,
188 /* "rprivate" */ MS_SLAVE|MS_RECURSIVE,
189 /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
192 // Always understood.
193 /* "ro" */ MS_RDONLY, // vfs flag
194 /* "rw" */ ~MS_RDONLY, // vfs flag
195 /* "remount" */ MS_REMOUNT // action flag
198 static const char mount_option_str[] =
199 IF_FEATURE_MOUNT_LOOP(
202 IF_FEATURE_MOUNT_FSTAB(
204 // "quiet\0" - do not filter out, vfat wants to see it
209 IF_DESKTOP("users\0")
212 IF_FEATURE_MOUNT_FLAGS(
246 // Always understood.
249 "remount\0" // action flag
254 #if ENABLE_FEATURE_MOUNT_NFS
255 smalluint nfs_mount_version;
257 #if ENABLE_FEATURE_MOUNT_VERBOSE
261 char getmntent_buf[1];
264 enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
265 #define G (*(struct globals*)&bb_common_bufsiz1)
266 #define nfs_mount_version (G.nfs_mount_version)
267 #if ENABLE_FEATURE_MOUNT_VERBOSE
268 #define verbose (G.verbose )
272 #define fslist (G.fslist )
273 #define getmntent_buf (G.getmntent_buf )
276 #if ENABLE_FEATURE_MOUNT_VERBOSE
277 static int verbose_mount(const char *source, const char *target,
278 const char *filesystemtype,
279 unsigned long mountflags, const void *data)
284 rc = mount(source, target, filesystemtype, mountflags, data);
286 bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
287 source, target, filesystemtype,
288 mountflags, (char*)data, rc);
292 #define verbose_mount(...) mount(__VA_ARGS__)
295 // Append mount options to string
296 static void append_mount_options(char **oldopts, const char *newopts)
298 if (*oldopts && **oldopts) {
299 // Do not insert options which are already there
302 int len = strlen(newopts);
303 p = strchr(newopts, ',');
304 if (p) len = p - newopts;
307 if (!strncmp(p, newopts, len)
308 && (p[len] == ',' || p[len] == '\0'))
314 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
319 while (newopts[0] == ',') newopts++;
322 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
323 *oldopts = xstrdup(newopts);
327 // Use the mount_options list to parse options into flags.
328 // Also return list of unrecognized options if unrecognized != NULL
329 static long parse_mount_options(char *options, char **unrecognized)
331 long flags = MS_SILENT;
333 // Loop through options
336 char *comma = strchr(options, ',');
337 const char *option_str = mount_option_str;
339 if (comma) *comma = '\0';
341 // FIXME: use hasmntopt()
342 // Find this option in mount_options
343 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
344 if (!strcasecmp(option_str, options)) {
345 long fl = mount_options[i];
346 if (fl < 0) flags &= fl;
350 option_str += strlen(option_str) + 1;
352 // If unrecognized not NULL, append unrecognized mount options
353 if (unrecognized && i == ARRAY_SIZE(mount_options)) {
354 // Add it to strflags, to pass on to kernel
355 i = *unrecognized ? strlen(*unrecognized) : 0;
356 *unrecognized = xrealloc(*unrecognized, i + strlen(options) + 2);
358 // Comma separated if it's not the first one
359 if (i) (*unrecognized)[i++] = ',';
360 strcpy((*unrecognized)+i, options);
365 // Advance to next option
373 // Return a list of all block device backed filesystems
374 static llist_t *get_block_backed_filesystems(void)
376 static const char filesystems[2][sizeof("/proc/filesystems")] = {
381 llist_t *list = NULL;
385 for (i = 0; i < 2; i++) {
386 f = fopen_for_read(filesystems[i]);
389 while ((buf = xmalloc_fgetline(f)) != NULL) {
390 if (strncmp(buf, "nodev", 5) == 0 && isspace(buf[5]))
392 fs = skip_whitespace(buf);
393 if (*fs == '#' || *fs == '*' || !*fs)
396 llist_add_to_end(&list, xstrdup(fs));
399 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
405 #if ENABLE_FEATURE_CLEAN_UP
406 static void delete_block_backed_filesystems(void)
408 llist_free(fslist, free);
411 void delete_block_backed_filesystems(void);
414 // Perform actual mount of specific filesystem at specific location.
415 // NB: mp->xxx fields may be trashed on exit
416 static int mount_it_now(struct mntent *mp, long vfsflags, char *filteropts)
422 bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')",
423 mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
424 vfsflags, filteropts);
428 // Mount, with fallback to read-only if necessary.
431 rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
432 vfsflags, filteropts);
434 // If mount failed, try
435 // helper program mount.<mnt_type>
436 if (HELPERS_ALLOWED && rc && mp->mnt_type) {
438 int errno_save = errno;
439 args[0] = xasprintf("mount.%s", mp->mnt_type);
442 args[rc++] = (char *)"-f";
443 if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB)
444 args[rc++] = (char *)"-n";
445 args[rc++] = mp->mnt_fsname;
446 args[rc++] = mp->mnt_dir;
448 args[rc++] = (char *)"-o";
449 args[rc++] = filteropts;
452 rc = wait4pid(spawn(args));
459 if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
461 if (!(vfsflags & MS_SILENT))
462 bb_error_msg("%s is write-protected, mounting read-only",
464 vfsflags |= MS_RDONLY;
467 // Abort entirely if permission denied.
469 if (rc && errno == EPERM)
470 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
472 // If the mount was successful, and we're maintaining an old-style
473 // mtab file by hand, add the new entry to it now.
475 if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) {
477 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
478 const char *option_str = mount_option_str;
482 bb_error_msg("no %s", bb_path_mtab_file);
486 // Add vfs string flags
488 for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
489 if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
490 append_mount_options(&(mp->mnt_opts), option_str);
491 option_str += strlen(option_str) + 1;
494 // Remove trailing / (if any) from directory we mounted on
496 i = strlen(mp->mnt_dir) - 1;
497 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = '\0';
499 // Convert to canonical pathnames as needed
501 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
503 if (!mp->mnt_type || !*mp->mnt_type) { // bind mount
504 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
505 mp->mnt_type = (char*)"bind";
507 mp->mnt_freq = mp->mnt_passno = 0;
511 addmntent(mountTable, mp);
512 endmntent(mountTable);
513 if (ENABLE_FEATURE_CLEAN_UP) {
522 #if ENABLE_FEATURE_MOUNT_NFS
526 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
528 * Licensed under GPLv2, see file LICENSE in this tarball for details.
530 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
531 * numbers to be specified on the command line.
533 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
534 * Omit the call to connect() for Linux version 1.3.11 or later.
536 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
537 * Implemented the "bg", "fg" and "retry" mount options for NFS.
539 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
540 * - added Native Language Support
542 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
546 /* This is just a warning of a common mistake. Possibly this should be a
547 * uclibc faq entry rather than in busybox... */
548 #if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
549 #error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
552 #define MOUNTPORT 635
553 #define MNTPATHLEN 1024
554 #define MNTNAMLEN 255
558 typedef char fhandle[FHSIZE];
561 unsigned int fhandle3_len;
573 MNT3ERR_NAMETOOLONG = 63,
574 MNT3ERR_NOTSUPP = 10004,
575 MNT3ERR_SERVERFAULT = 10006,
577 typedef enum mountstat3 mountstat3;
580 unsigned int fhs_status;
585 typedef struct fhstatus fhstatus;
587 struct mountres3_ok {
590 unsigned int auth_flavours_len;
591 char *auth_flavours_val;
594 typedef struct mountres3_ok mountres3_ok;
597 mountstat3 fhs_status;
599 mountres3_ok mountinfo;
602 typedef struct mountres3 mountres3;
604 typedef char *dirpath;
608 typedef struct mountbody *mountlist;
612 dirpath ml_directory;
615 typedef struct mountbody mountbody;
617 typedef struct groupnode *groups;
623 typedef struct groupnode groupnode;
625 typedef struct exportnode *exports;
632 typedef struct exportnode exportnode;
645 typedef struct ppathcnf ppathcnf;
647 #define MOUNTPROG 100005
650 #define MOUNTPROC_NULL 0
651 #define MOUNTPROC_MNT 1
652 #define MOUNTPROC_DUMP 2
653 #define MOUNTPROC_UMNT 3
654 #define MOUNTPROC_UMNTALL 4
655 #define MOUNTPROC_EXPORT 5
656 #define MOUNTPROC_EXPORTALL 6
658 #define MOUNTVERS_POSIX 2
660 #define MOUNTPROC_PATHCONF 7
664 #define MOUNTPROC3_NULL 0
665 #define MOUNTPROC3_MNT 1
666 #define MOUNTPROC3_DUMP 2
667 #define MOUNTPROC3_UMNT 3
668 #define MOUNTPROC3_UMNTALL 4
669 #define MOUNTPROC3_EXPORT 5
681 * We want to be able to compile mount on old kernels in such a way
682 * that the binary will work well on more recent kernels.
683 * Thus, if necessary we teach nfsmount.c the structure of new fields
684 * that will come later.
686 * Moreover, the new kernel includes conflict with glibc includes
687 * so it is easiest to ignore the kernel altogether (at compile time).
695 unsigned char data[64];
698 struct nfs_mount_data {
701 struct nfs2_fh old_root; /* 1 */
707 int acregmin; /* 1 */
708 int acregmax; /* 1 */
709 int acdirmin; /* 1 */
710 int acdirmax; /* 1 */
711 struct sockaddr_in addr; /* 1 */
712 char hostname[256]; /* 1 */
714 unsigned int bsize; /* 3 */
715 struct nfs3_fh root; /* 4 */
718 /* bits in the flags field */
720 NFS_MOUNT_SOFT = 0x0001, /* 1 */
721 NFS_MOUNT_INTR = 0x0002, /* 1 */
722 NFS_MOUNT_SECURE = 0x0004, /* 1 */
723 NFS_MOUNT_POSIX = 0x0008, /* 1 */
724 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
725 NFS_MOUNT_NOAC = 0x0020, /* 1 */
726 NFS_MOUNT_TCP = 0x0040, /* 2 */
727 NFS_MOUNT_VER3 = 0x0080, /* 3 */
728 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
729 NFS_MOUNT_NONLM = 0x0200, /* 3 */
730 NFS_MOUNT_NORDIRPLUS = 0x4000
735 * We need to translate between nfs status return values and
736 * the local errno values which may not be the same.
738 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
739 * "after #include <errno.h> the symbol errno is reserved for any use,
740 * it cannot even be used as a struct tag or field name".
744 #define EDQUOT ENOSPC
747 /* Convert each NFSERR_BLAH into EBLAH */
748 static const struct {
752 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
753 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
754 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
755 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
757 static char *nfs_strerror(int status)
761 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
762 if (nfs_errtbl[i].stat == status)
763 return strerror(nfs_errtbl[i].errnum);
765 return xasprintf("unknown nfs status return value: %d", status);
768 static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
770 if (!xdr_opaque(xdrs, objp, FHSIZE))
775 static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
777 if (!xdr_u_int(xdrs, &objp->fhs_status))
779 switch (objp->fhs_status) {
781 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
790 static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
792 if (!xdr_string(xdrs, objp, MNTPATHLEN))
797 static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
799 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
804 static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
806 if (!xdr_fhandle3(xdrs, &objp->fhandle))
808 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
809 sizeof(int), (xdrproc_t) xdr_int))
814 static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
816 if (!xdr_enum(xdrs, (enum_t *) objp))
821 static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
823 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
825 switch (objp->fhs_status) {
827 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
836 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
839 * Unfortunately, the kernel prints annoying console messages
840 * in case of an unexpected nfs mount version (instead of
841 * just returning some error). Therefore we'll have to try
842 * and figure out what version the kernel expects.
845 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
846 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
847 * nfs_mount_version: version this source and running kernel can handle
850 find_kernel_nfs_mount_version(void)
854 if (nfs_mount_version)
857 nfs_mount_version = 4; /* default */
859 kernel_version = get_linux_version_code();
860 if (kernel_version) {
861 if (kernel_version < KERNEL_VERSION(2,1,32))
862 nfs_mount_version = 1;
863 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
864 (kernel_version >= KERNEL_VERSION(2,3,0) &&
865 kernel_version < KERNEL_VERSION(2,3,99)))
866 nfs_mount_version = 3;
867 /* else v4 since 2.3.99pre4 */
872 get_mountport(struct pmap *pm_mnt,
873 struct sockaddr_in *server_addr,
875 long unsigned version,
879 struct pmaplist *pmap;
881 server_addr->sin_port = PMAPPORT;
882 /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
883 * I understand it like "IPv6 for this is not 100% ready" */
884 pmap = pmap_getmaps(server_addr);
886 if (version > MAX_NFSPROT)
887 version = MAX_NFSPROT;
890 pm_mnt->pm_prog = prog;
891 pm_mnt->pm_vers = version;
892 pm_mnt->pm_prot = proto;
893 pm_mnt->pm_port = port;
896 if (pmap->pml_map.pm_prog != prog)
898 if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
900 if (version > 2 && pmap->pml_map.pm_vers != version)
902 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
904 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
905 (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto) ||
906 (port && pmap->pml_map.pm_port != port))
908 memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
910 pmap = pmap->pml_next;
912 if (!pm_mnt->pm_vers)
913 pm_mnt->pm_vers = MOUNTVERS;
914 if (!pm_mnt->pm_port)
915 pm_mnt->pm_port = MOUNTPORT;
916 if (!pm_mnt->pm_prot)
917 pm_mnt->pm_prot = IPPROTO_TCP;
921 static int daemonize(void)
924 if (pid < 0) /* error */
926 if (pid > 0) /* parent */
930 xopen(bb_dev_null, O_RDWR);
934 openlog(applet_name, LOG_PID, LOG_DAEMON);
935 logmode = LOGMODE_SYSLOG;
939 static inline int daemonize(void) { return -ENOSYS; }
943 static inline int we_saw_this_host_before(const char *hostname UNUSED_PARAM)
948 /* RPC strerror analogs are terminally idiotic:
949 * *mandatory* prefix and \n at end.
950 * This hopefully helps. Usage:
951 * error_msg_rpc(clnt_*error*(" ")) */
952 static void error_msg_rpc(const char *msg)
955 while (msg[0] == ' ' || msg[0] == ':') msg++;
957 while (len && msg[len-1] == '\n') len--;
958 bb_error_msg("%.*s", len, msg);
961 /* NB: mp->xxx fields may be trashed on exit */
962 static NOINLINE int nfsmount(struct mntent *mp, long vfsflags, char *filteropts)
968 /* prior to 2.6.23, kernel took NFS options in a form of this struct
969 * only. 2.6.23+ looks at data->version, and if it's not 1..6,
970 * then data pointer is interpreted as a string. */
971 struct nfs_mount_data data;
974 struct sockaddr_in server_addr;
975 struct sockaddr_in mount_server_addr;
978 struct fhstatus nfsv2;
979 struct mountres3 nfsv3;
997 /* these all are one-bit really. gcc 4.3.1 likes this combination: */
1007 find_kernel_nfs_mount_version();
1015 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
1017 filteropts = xstrdup(filteropts); /* going to trash it later... */
1019 hostname = xstrdup(mp->mnt_fsname);
1020 /* mount_main() guarantees that ':' is there */
1021 s = strchr(hostname, ':');
1024 /* Ignore all but first hostname in replicated mounts
1025 until they can be fully supported. (mack@sgi.com) */
1026 s = strchr(hostname, ',');
1029 bb_error_msg("warning: multiple hostnames not supported");
1032 server_addr.sin_family = AF_INET;
1033 if (!inet_aton(hostname, &server_addr.sin_addr)) {
1034 hp = gethostbyname(hostname);
1036 bb_herror_msg("%s", hostname);
1039 if ((size_t)hp->h_length > sizeof(struct in_addr)) {
1040 bb_error_msg("got bad hp->h_length");
1041 hp->h_length = sizeof(struct in_addr);
1043 memcpy(&server_addr.sin_addr,
1044 hp->h_addr, hp->h_length);
1047 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
1049 /* add IP address to mtab options for use when unmounting */
1051 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
1052 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
1054 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
1055 mp->mnt_opts[0] ? "," : "",
1056 inet_ntoa(server_addr.sin_addr));
1061 /* Set default options.
1062 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
1063 * let the kernel decide.
1064 * timeo is filled in after we know whether it'll be TCP or UDP. */
1065 memset(&data, 0, sizeof(data));
1071 data.namlen = NAME_MAX;
1080 retry = 10000; /* 10000 minutes ~ 1 week */
1083 mountprog = MOUNTPROG;
1091 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
1092 char *opteq = strchr(opt, '=');
1095 static const char options[] ALIGN1 =
1100 /* 4 */ "acregmin\0"
1101 /* 5 */ "acregmax\0"
1102 /* 6 */ "acdirmin\0"
1103 /* 7 */ "acdirmax\0"
1107 /* 11 */ "mountport\0"
1108 /* 12 */ "mounthost\0"
1109 /* 13 */ "mountprog\0"
1110 /* 14 */ "mountvers\0"
1111 /* 15 */ "nfsprog\0"
1112 /* 16 */ "nfsvers\0"
1119 idx = index_in_strings(options, opt);
1121 case 12: // "mounthost"
1122 mounthost = xstrndup(opteq,
1123 strcspn(opteq, " \t\n\r,"));
1126 if (!strncmp(opteq, "tcp", 3))
1128 else if (!strncmp(opteq, "udp", 3))
1131 bb_error_msg("warning: unrecognized proto= option");
1133 case 20: // "addr" - ignore
1137 val = xatoi_u(opteq);
1148 case 3: // "retrans"
1151 case 4: // "acregmin"
1152 data.acregmin = val;
1154 case 5: // "acregmax"
1155 data.acregmax = val;
1157 case 6: // "acdirmin"
1158 data.acdirmin = val;
1160 case 7: // "acdirmax"
1161 data.acdirmax = val;
1163 case 8: // "actimeo"
1164 data.acregmin = val;
1165 data.acregmax = val;
1166 data.acdirmin = val;
1167 data.acdirmax = val;
1175 case 11: // "mountport"
1178 case 13: // "mountprog"
1181 case 14: // "mountvers"
1184 case 15: // "nfsprog"
1187 case 16: // "nfsvers"
1191 case 19: // "namlen"
1192 //if (nfs_mount_version >= 2)
1195 // bb_error_msg("warning: option namlen is not supported\n");
1198 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
1202 else { /* not of the form opt=val */
1203 static const char options[] ALIGN1 =
1217 if (!strncmp(opt, "no", 2)) {
1221 switch (index_in_strings(options, opt)) {
1257 if (nfs_mount_version >= 3)
1260 bb_error_msg("warning: option nolock is not supported");
1266 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1271 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1273 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1274 | (intr ? NFS_MOUNT_INTR : 0)
1275 | (posix ? NFS_MOUNT_POSIX : 0)
1276 | (nocto ? NFS_MOUNT_NOCTO : 0)
1277 | (noac ? NFS_MOUNT_NOAC : 0)
1278 | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0);
1279 if (nfs_mount_version >= 2)
1280 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1281 if (nfs_mount_version >= 3)
1282 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1283 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1284 bb_error_msg("NFSv%d not supported", nfsvers);
1287 if (nfsvers && !mountvers)
1288 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1289 if (nfsvers && nfsvers < mountvers) {
1290 mountvers = nfsvers;
1293 /* Adjust options if none specified */
1295 data.timeo = tcp ? 70 : 7;
1297 data.version = nfs_mount_version;
1299 if (vfsflags & MS_REMOUNT)
1303 * If the previous mount operation on the same host was
1304 * backgrounded, and the "bg" for this mount is also set,
1305 * give up immediately, to avoid the initial timeout.
1307 if (bg && we_saw_this_host_before(hostname)) {
1308 daemonized = daemonize();
1309 if (daemonized <= 0) { /* parent or error */
1310 retval = -daemonized;
1315 /* Create mount daemon client */
1316 /* See if the nfs host = mount host. */
1318 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1319 mount_server_addr.sin_family = AF_INET;
1320 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1322 hp = gethostbyname(mounthost);
1324 bb_herror_msg("%s", mounthost);
1327 if ((size_t)hp->h_length > sizeof(struct in_addr)) {
1328 bb_error_msg("got bad hp->h_length");
1329 hp->h_length = sizeof(struct in_addr);
1331 mount_server_addr.sin_family = AF_INET;
1332 memcpy(&mount_server_addr.sin_addr,
1333 hp->h_addr, hp->h_length);
1338 * The following loop implements the mount retries. When the mount
1339 * times out, and the "bg" option is set, we background ourself
1340 * and continue trying.
1342 * The case where the mount point is not present and the "bg"
1343 * option is set, is treated as a timeout. This is done to
1344 * support nested mounts.
1346 * The "retry" count specified by the user is the number of
1347 * minutes to retry before giving up.
1350 struct timeval total_timeout;
1351 struct timeval retry_timeout;
1357 retry_timeout.tv_sec = 3;
1358 retry_timeout.tv_usec = 0;
1359 total_timeout.tv_sec = 20;
1360 total_timeout.tv_usec = 0;
1361 /* FIXME: use monotonic()? */
1362 timeout = time(NULL) + 60 * retry;
1366 /* Be careful not to use too many CPU cycles */
1370 get_mountport(&pm_mnt, &mount_server_addr,
1375 nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
1377 /* contact the mount daemon via TCP */
1378 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1379 msock = RPC_ANYSOCK;
1381 switch (pm_mnt.pm_prot) {
1383 mclient = clntudp_create(&mount_server_addr,
1390 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1391 msock = RPC_ANYSOCK;
1393 mclient = clnttcp_create(&mount_server_addr,
1402 if (!daemonized && prevt == 0)
1403 error_msg_rpc(clnt_spcreateerror(" "));
1405 enum clnt_stat clnt_stat;
1407 /* Try to mount hostname:pathname */
1408 mclient->cl_auth = authunix_create_default();
1410 /* Make pointers in xdr_mountres3 NULL so
1411 * that xdr_array allocates memory for us
1413 memset(&status, 0, sizeof(status));
1415 if (pm_mnt.pm_vers == 3)
1416 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1417 (xdrproc_t) xdr_dirpath,
1418 (caddr_t) &pathname,
1419 (xdrproc_t) xdr_mountres3,
1423 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1424 (xdrproc_t) xdr_dirpath,
1425 (caddr_t) &pathname,
1426 (xdrproc_t) xdr_fhstatus,
1430 if (clnt_stat == RPC_SUCCESS)
1431 goto prepare_kernel_data; /* we're done */
1432 if (errno != ECONNREFUSED) {
1433 error_msg_rpc(clnt_sperror(mclient, " "));
1434 goto fail; /* don't retry */
1436 /* Connection refused */
1437 if (!daemonized && prevt == 0) /* print just once */
1438 error_msg_rpc(clnt_sperror(mclient, " "));
1439 auth_destroy(mclient->cl_auth);
1440 clnt_destroy(mclient);
1446 /* Timeout. We are going to retry... maybe */
1450 daemonized = daemonize();
1451 if (daemonized <= 0) { /* parent or error */
1452 retval = -daemonized;
1459 /* TODO error message */
1465 prepare_kernel_data:
1468 if (status.nfsv2.fhs_status != 0) {
1469 bb_error_msg("%s:%s failed, reason given by server: %s",
1471 nfs_strerror(status.nfsv2.fhs_status));
1474 memcpy(data.root.data,
1475 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1477 data.root.size = NFS_FHSIZE;
1478 memcpy(data.old_root.data,
1479 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1482 fhandle3 *my_fhandle;
1483 if (status.nfsv3.fhs_status != 0) {
1484 bb_error_msg("%s:%s failed, reason given by server: %s",
1486 nfs_strerror(status.nfsv3.fhs_status));
1489 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1490 memset(data.old_root.data, 0, NFS_FHSIZE);
1491 memset(&data.root, 0, sizeof(data.root));
1492 data.root.size = my_fhandle->fhandle3_len;
1493 memcpy(data.root.data,
1494 (char *) my_fhandle->fhandle3_val,
1495 my_fhandle->fhandle3_len);
1497 data.flags |= NFS_MOUNT_VER3;
1500 /* Create nfs socket for kernel */
1502 if (nfs_mount_version < 3) {
1503 bb_error_msg("NFS over TCP is not supported");
1506 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1508 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1510 bb_perror_msg("nfs socket");
1513 if (bindresvport(fsock, 0) < 0) {
1514 bb_perror_msg("nfs bindresvport");
1518 server_addr.sin_port = PMAPPORT;
1519 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1520 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1524 server_addr.sin_port = htons(port);
1526 /* Prepare data structure for kernel */
1528 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1529 strncpy(data.hostname, hostname, sizeof(data.hostname));
1532 auth_destroy(mclient->cl_auth);
1533 clnt_destroy(mclient);
1538 /* We must wait until mount directory is available */
1539 struct stat statbuf;
1541 while (stat(mp->mnt_dir, &statbuf) == -1) {
1543 daemonized = daemonize();
1544 if (daemonized <= 0) { /* parent or error */
1545 /* FIXME: parent doesn't close fsock - ??! */
1546 retval = -daemonized;
1550 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1557 /* Perform actual mount */
1559 mp->mnt_type = (char*)"nfs";
1560 retval = mount_it_now(mp, vfsflags, (char*)&data);
1567 auth_destroy(mclient->cl_auth);
1568 clnt_destroy(mclient);
1582 #else // !ENABLE_FEATURE_MOUNT_NFS
1584 // Never called. Call should be optimized out.
1585 int nfsmount(struct mntent *mp, long vfsflags, char *filteropts);
1587 #endif // !ENABLE_FEATURE_MOUNT_NFS
1589 // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1590 // type detection. Returns 0 for success, nonzero for failure.
1591 // NB: mp->xxx fields may be trashed on exit
1592 static int singlemount(struct mntent *mp, int ignore_busy)
1596 char *loopFile = NULL, *filteropts = NULL;
1600 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1602 // Treat fstype "auto" as unspecified
1603 if (mp->mnt_type && strcmp(mp->mnt_type, "auto") == 0)
1604 mp->mnt_type = NULL;
1606 // Might this be a virtual filesystem?
1607 if (ENABLE_FEATURE_MOUNT_HELPERS && strchr(mp->mnt_fsname, '#')) {
1611 // fsname: "cmd#arg1#arg2..."
1612 // WARNING: allows execution of arbitrary commands!
1613 // Try "mount 'sh#-c#sh' bogus_dir".
1614 // It is safe ONLY because non-root
1615 // cannot use two-argument mount command
1616 // and using one-argument "mount 'sh#-c#sh'" doesn't work:
1617 // "mount: can't find sh#-c#sh in /etc/fstab"
1618 // (if /etc/fstab has it, it's ok: root sets up /etc/fstab).
1623 while (*s && n < 35 - 2) {
1624 if (*s++ == '#' && *s != '#') {
1629 args[n++] = mp->mnt_dir;
1631 rc = wait4pid(xspawn(args));
1635 // Might this be an CIFS filesystem?
1636 if (ENABLE_FEATURE_MOUNT_CIFS
1637 && (!mp->mnt_type || strcmp(mp->mnt_type, "cifs") == 0)
1638 && (mp->mnt_fsname[0] == '/' || mp->mnt_fsname[0] == '\\')
1639 && mp->mnt_fsname[0] == mp->mnt_fsname[1]
1641 #if 0 /* reported to break things */
1642 len_and_sockaddr *lsa;
1646 // Replace '/' with '\' and verify that unc points to "//server/share".
1647 for (s = mp->mnt_fsname; *s; ++s)
1648 if (*s == '/') *s = '\\';
1651 s = strrchr(mp->mnt_fsname, '\\');
1652 if (s <= mp->mnt_fsname+1)
1655 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
1660 // Insert ip=... option into string flags.
1661 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1662 ip = xasprintf("ip=%s", dotted);
1663 parse_mount_options(ip, &filteropts);
1665 // Compose new unc '\\server-ip\share'
1666 // (s => slash after hostname)
1667 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
1669 // Lock is required [why?]
1670 vfsflags |= MS_MANDLOCK;
1671 mp->mnt_type = (char*)"cifs";
1672 rc = mount_it_now(mp, vfsflags, filteropts);
1674 if (ENABLE_FEATURE_CLEAN_UP) {
1675 free(mp->mnt_fsname);
1684 // Might this be an NFS filesystem?
1685 if (ENABLE_FEATURE_MOUNT_NFS
1686 && (!mp->mnt_type || !strcmp(mp->mnt_type, "nfs"))
1687 && strchr(mp->mnt_fsname, ':') != NULL
1689 rc = nfsmount(mp, vfsflags, filteropts);
1693 // Look at the file. (Not found isn't a failure for remount, or for
1694 // a synthetic filesystem like proc or sysfs.)
1695 // (We use stat, not lstat, in order to allow
1696 // mount symlink_to_file_or_blkdev dir)
1697 if (!stat(mp->mnt_fsname, &st)
1698 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1700 // Do we need to allocate a loopback device for it?
1701 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1702 loopFile = bb_simplify_path(mp->mnt_fsname);
1703 mp->mnt_fsname = NULL; // will receive malloced loop dev name
1704 if (set_loop(&(mp->mnt_fsname), loopFile, 0) < 0) {
1705 if (errno == EPERM || errno == EACCES)
1706 bb_error_msg(bb_msg_perm_denied_are_you_root);
1708 bb_perror_msg("cannot setup loop device");
1712 // Autodetect bind mounts
1713 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1714 vfsflags |= MS_BIND;
1717 // If we know the fstype (or don't need to), jump straight
1718 // to the actual mount.
1719 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1720 rc = mount_it_now(mp, vfsflags, filteropts);
1722 // Loop through filesystem types until mount succeeds
1725 // Initialize list of block backed filesystems. This has to be
1726 // done here so that during "mount -a", mounts after /proc shows up
1729 fslist = get_block_backed_filesystems();
1730 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1731 atexit(delete_block_backed_filesystems);
1734 for (fl = fslist; fl; fl = fl->link) {
1735 mp->mnt_type = fl->data;
1736 rc = mount_it_now(mp, vfsflags, filteropts);
1742 // If mount failed, clean up loop file (if any).
1743 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1744 del_loop(mp->mnt_fsname);
1745 if (ENABLE_FEATURE_CLEAN_UP) {
1747 free(mp->mnt_fsname);
1752 if (ENABLE_FEATURE_CLEAN_UP)
1755 if (errno == EBUSY && ignore_busy)
1758 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1763 * Unlike -t, -O should interpret "no" prefix differently:
1764 * -t noa,b,c = -t no(a,b,c) = mount all except fs'es with types a,b, and c
1765 * -O noa,b,c = -O noa,b,c = mount all with without option a,
1766 * or with option b or c.
1767 * But for now we do not support -O a,b,c at all (only -O a).
1769 * Another difference from -t support (match_fstype) is that
1770 * we need to examine the _list_ of options in fsopt, not just a string.
1772 static int match_opt(const char *fs_opt, const char *O_opt)
1780 if (O_opt[0] == 'n' && O_opt[1] == 'o') {
1785 len = strlen(O_opt);
1787 if (strncmp(fs_opt, O_opt, len) == 0
1788 && (fs_opt[len] == '\0' || fs_opt[len] == ',')
1792 fs_opt = strchr(fs_opt, ',');
1801 // Parse options, if necessary parse fstab/mtab, and call singlemount for
1802 // each directory to be mounted.
1803 static const char must_be_root[] ALIGN1 = "you must be root";
1805 int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1806 int mount_main(int argc UNUSED_PARAM, char **argv)
1808 char *cmdopts = xzalloc(1);
1809 char *fstype = NULL;
1810 char *O_optmatch = NULL;
1812 llist_t *lst_o = NULL;
1813 const char *fstabname;
1817 struct mntent mtpair[2], *mtcur = mtpair;
1818 IF_NOT_DESKTOP(const int nonroot = 0;)
1820 IF_DESKTOP(int nonroot = ) sanitize_env_if_suid();
1822 // Parse long options, like --bind and --move. Note that -o option
1823 // and --option are synonymous. Yes, this means --remount,rw works.
1824 for (i = j = 1; argv[i]; i++) {
1825 if (argv[i][0] == '-' && argv[i][1] == '-')
1826 append_mount_options(&cmdopts, argv[i] + 2);
1828 argv[j++] = argv[i];
1832 // Parse remaining options
1833 // Max 2 params; -o is a list, -v is a counter
1834 opt_complementary = "?2o::" IF_FEATURE_MOUNT_VERBOSE("vv");
1835 opt = getopt32(argv, OPTION_STR, &lst_o, &fstype, &O_optmatch
1836 IF_FEATURE_MOUNT_VERBOSE(, &verbose));
1837 while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
1838 if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
1839 if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w
1842 // If we have no arguments, show currently mounted filesystems
1844 if (!(opt & OPT_a)) {
1845 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1848 bb_error_msg_and_die("no %s", bb_path_mtab_file);
1850 while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
1853 // Don't show rootfs. FIXME: why??
1854 // util-linux 2.12a happily shows rootfs...
1855 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
1857 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1858 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1859 mtpair->mnt_dir, mtpair->mnt_type,
1862 if (ENABLE_FEATURE_CLEAN_UP)
1863 endmntent(mountTable);
1864 return EXIT_SUCCESS;
1866 storage_path = NULL;
1868 // When we have two arguments, the second is the directory and we can
1869 // skip looking at fstab entirely. We can always abspath() the directory
1870 // argument when we get it.
1873 bb_error_msg_and_die(must_be_root);
1874 mtpair->mnt_fsname = argv[0];
1875 mtpair->mnt_dir = argv[1];
1876 mtpair->mnt_type = fstype;
1877 mtpair->mnt_opts = cmdopts;
1878 resolve_mount_spec(&mtpair->mnt_fsname);
1879 rc = singlemount(mtpair, 0);
1882 storage_path = bb_simplify_path(argv[0]); // malloced
1885 // Past this point, we are handling either "mount -a [opts]"
1886 // or "mount [opts] single_param"
1888 i = parse_mount_options(cmdopts, NULL); // FIXME: should be "long", not "int"
1889 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1890 bb_error_msg_and_die(must_be_root);
1892 // If we have a shared subtree flag, don't worry about fstab or mtab.
1893 if (ENABLE_FEATURE_MOUNT_FLAGS
1894 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1896 // verbose_mount(source, target, type, flags, data)
1897 rc = verbose_mount("", argv[0], "", i, "");
1899 bb_simple_perror_msg_and_die(argv[0]);
1903 // Open either fstab or mtab
1904 fstabname = "/etc/fstab";
1905 if (i & MS_REMOUNT) {
1906 // WARNING. I am not sure this matches util-linux's
1907 // behavior. It's possible util-linux does not
1908 // take -o opts from mtab (takes only mount source).
1909 fstabname = bb_path_mtab_file;
1911 fstab = setmntent(fstabname, "r");
1913 bb_perror_msg_and_die("cannot read %s", fstabname);
1915 // Loop through entries until we find what we're looking for
1916 memset(mtpair, 0, sizeof(mtpair));
1918 struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair);
1920 // Get next fstab entry
1921 if (!getmntent_r(fstab, mtcur, getmntent_buf
1922 + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0),
1923 GETMNTENT_BUFSIZE/2)
1924 ) { // End of fstab/mtab is reached
1925 mtcur = mtother; // the thing we found last time
1929 // If we're trying to mount something specific and this isn't it,
1930 // skip it. Note we must match the exact text in fstab (ala
1931 // "proc") or a full path from root
1934 // Is this what we're looking for?
1935 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1936 strcmp(storage_path, mtcur->mnt_fsname) &&
1937 strcmp(argv[0], mtcur->mnt_dir) &&
1938 strcmp(storage_path, mtcur->mnt_dir)) continue;
1940 // Remember this entry. Something later may have
1941 // overmounted it, and we want the _last_ match.
1944 // If we're mounting all
1946 // No, mount -a won't mount anything,
1947 // even user mounts, for mere humans
1949 bb_error_msg_and_die(must_be_root);
1951 // Does type match? (NULL matches always)
1952 if (!match_fstype(mtcur, fstype))
1955 // Skip noauto and swap anyway.
1956 if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
1957 // swap is bogus "fstype", parse_mount_options can't check fstypes
1958 || strcasecmp(mtcur->mnt_type, "swap") == 0
1963 // Does (at least one) option match?
1964 // (NULL matches always)
1965 if (!match_opt(mtcur->mnt_opts, O_optmatch))
1968 resolve_mount_spec(&mtcur->mnt_fsname);
1970 // NFS mounts want this to be xrealloc-able
1971 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
1974 if (singlemount(mtcur, 1)) {
1975 // Count number of failed mounts
1978 free(mtcur->mnt_opts);
1982 // End of fstab/mtab is reached.
1983 // Were we looking for something specific?
1987 // If we didn't find anything, complain
1988 if (!mtcur->mnt_fsname)
1989 bb_error_msg_and_die("can't find %s in %s",
1990 argv[0], fstabname);
1992 // What happens when we try to "mount swap_partition"?
1993 // (fstab containts "swap_partition swap swap defaults 0 0")
1994 // util-linux-ng 2.13.1 does this:
1995 // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
1996 // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
1997 // lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory)
1998 // write(2, "mount: mount point swap does not exist\n", 39) = 39
1999 // exit_group(32) = ?
2001 // In case we want to simply skip swap partitions:
2002 l = parse_mount_options(mtcur->mnt_opts, NULL);
2003 if ((l & MOUNT_SWAP)
2004 // swap is bogus "fstype", parse_mount_options can't check fstypes
2005 || strcasecmp(mtcur->mnt_type, "swap") == 0
2011 // fstab must have "users" or "user"
2012 l = parse_mount_options(mtcur->mnt_opts, NULL);
2013 if (!(l & MOUNT_USERS))
2014 bb_error_msg_and_die(must_be_root);
2017 // Mount the last thing we found
2018 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
2019 append_mount_options(&(mtcur->mnt_opts), cmdopts);
2020 resolve_mount_spec(&mtpair->mnt_fsname);
2021 rc = singlemount(mtcur, 0);
2022 if (ENABLE_FEATURE_CLEAN_UP)
2023 free(mtcur->mnt_opts);
2027 if (ENABLE_FEATURE_CLEAN_UP)
2029 if (ENABLE_FEATURE_CLEAN_UP) {