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_UNION (1 << 8)
26 # define MS_BIND (1 << 12)
29 # define MS_MOVE (1 << 13)
32 # define MS_RECURSIVE (1 << 14)
35 # define MS_SILENT (1 << 15)
37 /* The shared subtree stuff, which went in around 2.6.15. */
39 # define MS_UNBINDABLE (1 << 17)
42 # define MS_PRIVATE (1 << 18)
45 # define MS_SLAVE (1 << 19)
48 # define MS_SHARED (1 << 20)
51 # define MS_RELATIME (1 << 21)
55 #if ENABLE_FEATURE_MOUNT_LABEL
56 # include "volume_id.h"
58 # define resolve_mount_spec(fsname) ((void)0)
61 // Needed for nfs support only
62 #include <sys/utsname.h>
66 #include <rpc/pmap_prot.h>
67 #include <rpc/pmap_clnt.h>
70 #define MS_SILENT (1 << 15)
72 // Grab more as needed from util-linux's mount/mount_constants.h
74 #define MS_DIRSYNC 128 // Directory modifications are synchronous
78 #if defined(__dietlibc__)
79 // 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
80 // dietlibc-0.30 does not have implementation of getmntent_r()
81 static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
82 char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
84 struct mntent* ment = getmntent(stream);
85 return memcpy(result, ment, sizeof(*ment));
90 // Not real flags, but we want to be able to check for this.
92 MOUNT_USERS = (1 << 28) * ENABLE_DESKTOP,
93 MOUNT_NOAUTO = (1 << 29),
94 MOUNT_SWAP = (1 << 30),
98 #define OPTION_STR "o:t:rwanfvsiO:"
113 #if ENABLE_FEATURE_MTAB_SUPPORT
114 #define USE_MTAB (!(option_mask32 & OPT_n))
119 #if ENABLE_FEATURE_MOUNT_FAKE
120 #define FAKE_IT (option_mask32 & OPT_f)
125 #if ENABLE_FEATURE_MOUNT_HELPERS
126 #define HELPERS_ALLOWED (!(option_mask32 & OPT_i))
128 #define HELPERS_ALLOWED 0
132 // TODO: more "user" flag compatibility.
133 // "user" option (from mount manpage):
134 // Only the user that mounted a filesystem can unmount it again.
135 // If any user should be able to unmount, then use users instead of user
136 // in the fstab line. The owner option is similar to the user option,
137 // with the restriction that the user must be the owner of the special file.
138 // This may be useful e.g. for /dev/fd if a login script makes
139 // the console user owner of this device.
141 // Standard mount options (from -o options or --options),
142 // with corresponding flags
143 static const int32_t mount_options[] = {
144 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
146 IF_FEATURE_MOUNT_LOOP(
150 IF_FEATURE_MOUNT_FSTAB(
152 /* "quiet" 0 - do not filter out, vfat wants to see it */
153 /* "noauto" */ MOUNT_NOAUTO,
154 /* "sw" */ MOUNT_SWAP,
155 /* "swap" */ MOUNT_SWAP,
156 IF_DESKTOP(/* "user" */ MOUNT_USERS,)
157 IF_DESKTOP(/* "users" */ MOUNT_USERS,)
161 IF_FEATURE_MOUNT_FLAGS(
163 /* "nosuid" */ MS_NOSUID,
164 /* "suid" */ ~MS_NOSUID,
165 /* "dev" */ ~MS_NODEV,
166 /* "nodev" */ MS_NODEV,
167 /* "exec" */ ~MS_NOEXEC,
168 /* "noexec" */ MS_NOEXEC,
169 /* "sync" */ MS_SYNCHRONOUS,
170 /* "dirsync" */ MS_DIRSYNC,
171 /* "async" */ ~MS_SYNCHRONOUS,
172 /* "atime" */ ~MS_NOATIME,
173 /* "noatime" */ MS_NOATIME,
174 /* "diratime" */ ~MS_NODIRATIME,
175 /* "nodiratime" */ MS_NODIRATIME,
176 /* "mand" */ MS_MANDLOCK,
177 /* "nomand" */ ~MS_MANDLOCK,
178 /* "relatime" */ MS_RELATIME,
179 /* "norelatime" */ ~MS_RELATIME,
180 /* "loud" */ ~MS_SILENT,
183 /* "union" */ MS_UNION,
184 /* "bind" */ MS_BIND,
185 /* "move" */ MS_MOVE,
186 /* "shared" */ MS_SHARED,
187 /* "slave" */ MS_SLAVE,
188 /* "private" */ MS_PRIVATE,
189 /* "unbindable" */ MS_UNBINDABLE,
190 /* "rshared" */ MS_SHARED|MS_RECURSIVE,
191 /* "rslave" */ MS_SLAVE|MS_RECURSIVE,
192 /* "rprivate" */ MS_SLAVE|MS_RECURSIVE,
193 /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
196 // Always understood.
197 /* "ro" */ MS_RDONLY, // vfs flag
198 /* "rw" */ ~MS_RDONLY, // vfs flag
199 /* "remount" */ MS_REMOUNT // action flag
202 static const char mount_option_str[] =
203 IF_FEATURE_MOUNT_LOOP(
206 IF_FEATURE_MOUNT_FSTAB(
208 // "quiet\0" - do not filter out, vfat wants to see it
213 IF_DESKTOP("users\0")
216 IF_FEATURE_MOUNT_FLAGS(
251 // Always understood.
254 "remount\0" // action flag
259 #if ENABLE_FEATURE_MOUNT_NFS
260 smalluint nfs_mount_version;
262 #if ENABLE_FEATURE_MOUNT_VERBOSE
266 char getmntent_buf[1];
269 enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
270 #define G (*(struct globals*)&bb_common_bufsiz1)
271 #define nfs_mount_version (G.nfs_mount_version)
272 #if ENABLE_FEATURE_MOUNT_VERBOSE
273 #define verbose (G.verbose )
277 #define fslist (G.fslist )
278 #define getmntent_buf (G.getmntent_buf )
281 #if ENABLE_FEATURE_MOUNT_VERBOSE
282 static int verbose_mount(const char *source, const char *target,
283 const char *filesystemtype,
284 unsigned long mountflags, const void *data)
289 rc = mount(source, target, filesystemtype, mountflags, data);
291 bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
292 source, target, filesystemtype,
293 mountflags, (char*)data, rc);
297 #define verbose_mount(...) mount(__VA_ARGS__)
300 // Append mount options to string
301 static void append_mount_options(char **oldopts, const char *newopts)
303 if (*oldopts && **oldopts) {
304 // Do not insert options which are already there
307 int len = strlen(newopts);
308 p = strchr(newopts, ',');
309 if (p) len = p - newopts;
312 if (!strncmp(p, newopts, len)
313 && (p[len] == ',' || p[len] == '\0'))
319 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
324 while (newopts[0] == ',') newopts++;
327 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
328 *oldopts = xstrdup(newopts);
332 // Use the mount_options list to parse options into flags.
333 // Also return list of unrecognized options if unrecognized != NULL
334 static long parse_mount_options(char *options, char **unrecognized)
336 long flags = MS_SILENT;
338 // Loop through options
341 char *comma = strchr(options, ',');
342 const char *option_str = mount_option_str;
344 if (comma) *comma = '\0';
346 // FIXME: use hasmntopt()
347 // Find this option in mount_options
348 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
349 if (!strcasecmp(option_str, options)) {
350 long fl = mount_options[i];
351 if (fl < 0) flags &= fl;
355 option_str += strlen(option_str) + 1;
357 // If unrecognized not NULL, append unrecognized mount options
358 if (unrecognized && i == ARRAY_SIZE(mount_options)) {
359 // Add it to strflags, to pass on to kernel
360 i = *unrecognized ? strlen(*unrecognized) : 0;
361 *unrecognized = xrealloc(*unrecognized, i + strlen(options) + 2);
363 // Comma separated if it's not the first one
364 if (i) (*unrecognized)[i++] = ',';
365 strcpy((*unrecognized)+i, options);
370 // Advance to next option
378 // Return a list of all block device backed filesystems
379 static llist_t *get_block_backed_filesystems(void)
381 static const char filesystems[2][sizeof("/proc/filesystems")] = {
386 llist_t *list = NULL;
390 for (i = 0; i < 2; i++) {
391 f = fopen_for_read(filesystems[i]);
394 while ((buf = xmalloc_fgetline(f)) != NULL) {
395 if (strncmp(buf, "nodev", 5) == 0 && isspace(buf[5]))
397 fs = skip_whitespace(buf);
398 if (*fs == '#' || *fs == '*' || !*fs)
401 llist_add_to_end(&list, xstrdup(fs));
404 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
410 #if ENABLE_FEATURE_CLEAN_UP
411 static void delete_block_backed_filesystems(void)
413 llist_free(fslist, free);
416 void delete_block_backed_filesystems(void);
419 // Perform actual mount of specific filesystem at specific location.
420 // NB: mp->xxx fields may be trashed on exit
421 static int mount_it_now(struct mntent *mp, long vfsflags, char *filteropts)
427 bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')",
428 mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
429 vfsflags, filteropts);
433 // Mount, with fallback to read-only if necessary.
436 rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
437 vfsflags, filteropts);
439 // If mount failed, try
440 // helper program mount.<mnt_type>
441 if (HELPERS_ALLOWED && rc && mp->mnt_type) {
443 int errno_save = errno;
444 args[0] = xasprintf("mount.%s", mp->mnt_type);
447 args[rc++] = (char *)"-f";
448 if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB)
449 args[rc++] = (char *)"-n";
450 args[rc++] = mp->mnt_fsname;
451 args[rc++] = mp->mnt_dir;
453 args[rc++] = (char *)"-o";
454 args[rc++] = filteropts;
457 rc = wait4pid(spawn(args));
464 if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
466 if (!(vfsflags & MS_SILENT))
467 bb_error_msg("%s is write-protected, mounting read-only",
469 vfsflags |= MS_RDONLY;
472 // Abort entirely if permission denied.
474 if (rc && errno == EPERM)
475 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
477 // If the mount was successful, and we're maintaining an old-style
478 // mtab file by hand, add the new entry to it now.
480 if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) {
482 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
483 const char *option_str = mount_option_str;
487 bb_error_msg("no %s", bb_path_mtab_file);
491 // Add vfs string flags
493 for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
494 if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
495 append_mount_options(&(mp->mnt_opts), option_str);
496 option_str += strlen(option_str) + 1;
499 // Remove trailing / (if any) from directory we mounted on
501 i = strlen(mp->mnt_dir) - 1;
502 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = '\0';
504 // Convert to canonical pathnames as needed
506 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
508 if (!mp->mnt_type || !*mp->mnt_type) { // bind mount
509 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
510 mp->mnt_type = (char*)"bind";
512 mp->mnt_freq = mp->mnt_passno = 0;
516 addmntent(mountTable, mp);
517 endmntent(mountTable);
518 if (ENABLE_FEATURE_CLEAN_UP) {
527 #if ENABLE_FEATURE_MOUNT_NFS
531 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
533 * Licensed under GPLv2, see file LICENSE in this tarball for details.
535 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
536 * numbers to be specified on the command line.
538 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
539 * Omit the call to connect() for Linux version 1.3.11 or later.
541 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
542 * Implemented the "bg", "fg" and "retry" mount options for NFS.
544 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
545 * - added Native Language Support
547 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
551 /* This is just a warning of a common mistake. Possibly this should be a
552 * uclibc faq entry rather than in busybox... */
553 #if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
554 #error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
557 #define MOUNTPORT 635
558 #define MNTPATHLEN 1024
559 #define MNTNAMLEN 255
563 typedef char fhandle[FHSIZE];
566 unsigned int fhandle3_len;
578 MNT3ERR_NAMETOOLONG = 63,
579 MNT3ERR_NOTSUPP = 10004,
580 MNT3ERR_SERVERFAULT = 10006,
582 typedef enum mountstat3 mountstat3;
585 unsigned int fhs_status;
590 typedef struct fhstatus fhstatus;
592 struct mountres3_ok {
595 unsigned int auth_flavours_len;
596 char *auth_flavours_val;
599 typedef struct mountres3_ok mountres3_ok;
602 mountstat3 fhs_status;
604 mountres3_ok mountinfo;
607 typedef struct mountres3 mountres3;
609 typedef char *dirpath;
613 typedef struct mountbody *mountlist;
617 dirpath ml_directory;
620 typedef struct mountbody mountbody;
622 typedef struct groupnode *groups;
628 typedef struct groupnode groupnode;
630 typedef struct exportnode *exports;
637 typedef struct exportnode exportnode;
650 typedef struct ppathcnf ppathcnf;
652 #define MOUNTPROG 100005
655 #define MOUNTPROC_NULL 0
656 #define MOUNTPROC_MNT 1
657 #define MOUNTPROC_DUMP 2
658 #define MOUNTPROC_UMNT 3
659 #define MOUNTPROC_UMNTALL 4
660 #define MOUNTPROC_EXPORT 5
661 #define MOUNTPROC_EXPORTALL 6
663 #define MOUNTVERS_POSIX 2
665 #define MOUNTPROC_PATHCONF 7
669 #define MOUNTPROC3_NULL 0
670 #define MOUNTPROC3_MNT 1
671 #define MOUNTPROC3_DUMP 2
672 #define MOUNTPROC3_UMNT 3
673 #define MOUNTPROC3_UMNTALL 4
674 #define MOUNTPROC3_EXPORT 5
686 * We want to be able to compile mount on old kernels in such a way
687 * that the binary will work well on more recent kernels.
688 * Thus, if necessary we teach nfsmount.c the structure of new fields
689 * that will come later.
691 * Moreover, the new kernel includes conflict with glibc includes
692 * so it is easiest to ignore the kernel altogether (at compile time).
700 unsigned char data[64];
703 struct nfs_mount_data {
706 struct nfs2_fh old_root; /* 1 */
712 int acregmin; /* 1 */
713 int acregmax; /* 1 */
714 int acdirmin; /* 1 */
715 int acdirmax; /* 1 */
716 struct sockaddr_in addr; /* 1 */
717 char hostname[256]; /* 1 */
719 unsigned int bsize; /* 3 */
720 struct nfs3_fh root; /* 4 */
723 /* bits in the flags field */
725 NFS_MOUNT_SOFT = 0x0001, /* 1 */
726 NFS_MOUNT_INTR = 0x0002, /* 1 */
727 NFS_MOUNT_SECURE = 0x0004, /* 1 */
728 NFS_MOUNT_POSIX = 0x0008, /* 1 */
729 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
730 NFS_MOUNT_NOAC = 0x0020, /* 1 */
731 NFS_MOUNT_TCP = 0x0040, /* 2 */
732 NFS_MOUNT_VER3 = 0x0080, /* 3 */
733 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
734 NFS_MOUNT_NONLM = 0x0200, /* 3 */
735 NFS_MOUNT_NORDIRPLUS = 0x4000
740 * We need to translate between nfs status return values and
741 * the local errno values which may not be the same.
743 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
744 * "after #include <errno.h> the symbol errno is reserved for any use,
745 * it cannot even be used as a struct tag or field name".
749 #define EDQUOT ENOSPC
752 /* Convert each NFSERR_BLAH into EBLAH */
753 static const struct {
757 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
758 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
759 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
760 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
762 static char *nfs_strerror(int status)
766 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
767 if (nfs_errtbl[i].stat == status)
768 return strerror(nfs_errtbl[i].errnum);
770 return xasprintf("unknown nfs status return value: %d", status);
773 static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
775 if (!xdr_opaque(xdrs, objp, FHSIZE))
780 static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
782 if (!xdr_u_int(xdrs, &objp->fhs_status))
784 switch (objp->fhs_status) {
786 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
795 static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
797 if (!xdr_string(xdrs, objp, MNTPATHLEN))
802 static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
804 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
809 static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
811 if (!xdr_fhandle3(xdrs, &objp->fhandle))
813 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
814 sizeof(int), (xdrproc_t) xdr_int))
819 static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
821 if (!xdr_enum(xdrs, (enum_t *) objp))
826 static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
828 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
830 switch (objp->fhs_status) {
832 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
841 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
844 * Unfortunately, the kernel prints annoying console messages
845 * in case of an unexpected nfs mount version (instead of
846 * just returning some error). Therefore we'll have to try
847 * and figure out what version the kernel expects.
850 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
851 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
852 * nfs_mount_version: version this source and running kernel can handle
855 find_kernel_nfs_mount_version(void)
859 if (nfs_mount_version)
862 nfs_mount_version = 4; /* default */
864 kernel_version = get_linux_version_code();
865 if (kernel_version) {
866 if (kernel_version < KERNEL_VERSION(2,1,32))
867 nfs_mount_version = 1;
868 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
869 (kernel_version >= KERNEL_VERSION(2,3,0) &&
870 kernel_version < KERNEL_VERSION(2,3,99)))
871 nfs_mount_version = 3;
872 /* else v4 since 2.3.99pre4 */
877 get_mountport(struct pmap *pm_mnt,
878 struct sockaddr_in *server_addr,
880 long unsigned version,
884 struct pmaplist *pmap;
886 server_addr->sin_port = PMAPPORT;
887 /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
888 * I understand it like "IPv6 for this is not 100% ready" */
889 pmap = pmap_getmaps(server_addr);
891 if (version > MAX_NFSPROT)
892 version = MAX_NFSPROT;
895 pm_mnt->pm_prog = prog;
896 pm_mnt->pm_vers = version;
897 pm_mnt->pm_prot = proto;
898 pm_mnt->pm_port = port;
901 if (pmap->pml_map.pm_prog != prog)
903 if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
905 if (version > 2 && pmap->pml_map.pm_vers != version)
907 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
909 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
910 (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto) ||
911 (port && pmap->pml_map.pm_port != port))
913 memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
915 pmap = pmap->pml_next;
917 if (!pm_mnt->pm_vers)
918 pm_mnt->pm_vers = MOUNTVERS;
919 if (!pm_mnt->pm_port)
920 pm_mnt->pm_port = MOUNTPORT;
921 if (!pm_mnt->pm_prot)
922 pm_mnt->pm_prot = IPPROTO_TCP;
926 static int daemonize(void)
929 if (pid < 0) /* error */
931 if (pid > 0) /* parent */
935 xopen(bb_dev_null, O_RDWR);
939 openlog(applet_name, LOG_PID, LOG_DAEMON);
940 logmode = LOGMODE_SYSLOG;
944 static inline int daemonize(void) { return -ENOSYS; }
948 static inline int we_saw_this_host_before(const char *hostname UNUSED_PARAM)
953 /* RPC strerror analogs are terminally idiotic:
954 * *mandatory* prefix and \n at end.
955 * This hopefully helps. Usage:
956 * error_msg_rpc(clnt_*error*(" ")) */
957 static void error_msg_rpc(const char *msg)
960 while (msg[0] == ' ' || msg[0] == ':') msg++;
962 while (len && msg[len-1] == '\n') len--;
963 bb_error_msg("%.*s", len, msg);
966 /* NB: mp->xxx fields may be trashed on exit */
967 static NOINLINE int nfsmount(struct mntent *mp, long vfsflags, char *filteropts)
973 /* prior to 2.6.23, kernel took NFS options in a form of this struct
974 * only. 2.6.23+ looks at data->version, and if it's not 1..6,
975 * then data pointer is interpreted as a string. */
976 struct nfs_mount_data data;
979 struct sockaddr_in server_addr;
980 struct sockaddr_in mount_server_addr;
983 struct fhstatus nfsv2;
984 struct mountres3 nfsv3;
1002 /* these all are one-bit really. gcc 4.3.1 likes this combination: */
1012 find_kernel_nfs_mount_version();
1020 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
1022 filteropts = xstrdup(filteropts); /* going to trash it later... */
1024 hostname = xstrdup(mp->mnt_fsname);
1025 /* mount_main() guarantees that ':' is there */
1026 s = strchr(hostname, ':');
1029 /* Ignore all but first hostname in replicated mounts
1030 until they can be fully supported. (mack@sgi.com) */
1031 s = strchr(hostname, ',');
1034 bb_error_msg("warning: multiple hostnames not supported");
1037 server_addr.sin_family = AF_INET;
1038 if (!inet_aton(hostname, &server_addr.sin_addr)) {
1039 hp = gethostbyname(hostname);
1041 bb_herror_msg("%s", hostname);
1044 if ((size_t)hp->h_length > sizeof(struct in_addr)) {
1045 bb_error_msg("got bad hp->h_length");
1046 hp->h_length = sizeof(struct in_addr);
1048 memcpy(&server_addr.sin_addr,
1049 hp->h_addr, hp->h_length);
1052 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
1054 /* add IP address to mtab options for use when unmounting */
1056 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
1057 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
1059 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
1060 mp->mnt_opts[0] ? "," : "",
1061 inet_ntoa(server_addr.sin_addr));
1066 /* Set default options.
1067 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
1068 * let the kernel decide.
1069 * timeo is filled in after we know whether it'll be TCP or UDP. */
1070 memset(&data, 0, sizeof(data));
1076 data.namlen = NAME_MAX;
1085 retry = 10000; /* 10000 minutes ~ 1 week */
1088 mountprog = MOUNTPROG;
1096 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
1097 char *opteq = strchr(opt, '=');
1100 static const char options[] ALIGN1 =
1105 /* 4 */ "acregmin\0"
1106 /* 5 */ "acregmax\0"
1107 /* 6 */ "acdirmin\0"
1108 /* 7 */ "acdirmax\0"
1112 /* 11 */ "mountport\0"
1113 /* 12 */ "mounthost\0"
1114 /* 13 */ "mountprog\0"
1115 /* 14 */ "mountvers\0"
1116 /* 15 */ "nfsprog\0"
1117 /* 16 */ "nfsvers\0"
1124 idx = index_in_strings(options, opt);
1126 case 12: // "mounthost"
1127 mounthost = xstrndup(opteq,
1128 strcspn(opteq, " \t\n\r,"));
1131 if (!strncmp(opteq, "tcp", 3))
1133 else if (!strncmp(opteq, "udp", 3))
1136 bb_error_msg("warning: unrecognized proto= option");
1138 case 20: // "addr" - ignore
1142 val = xatoi_u(opteq);
1153 case 3: // "retrans"
1156 case 4: // "acregmin"
1157 data.acregmin = val;
1159 case 5: // "acregmax"
1160 data.acregmax = val;
1162 case 6: // "acdirmin"
1163 data.acdirmin = val;
1165 case 7: // "acdirmax"
1166 data.acdirmax = val;
1168 case 8: // "actimeo"
1169 data.acregmin = val;
1170 data.acregmax = val;
1171 data.acdirmin = val;
1172 data.acdirmax = val;
1180 case 11: // "mountport"
1183 case 13: // "mountprog"
1186 case 14: // "mountvers"
1189 case 15: // "nfsprog"
1192 case 16: // "nfsvers"
1196 case 19: // "namlen"
1197 //if (nfs_mount_version >= 2)
1200 // bb_error_msg("warning: option namlen is not supported\n");
1203 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
1207 else { /* not of the form opt=val */
1208 static const char options[] ALIGN1 =
1222 if (!strncmp(opt, "no", 2)) {
1226 switch (index_in_strings(options, opt)) {
1262 if (nfs_mount_version >= 3)
1265 bb_error_msg("warning: option nolock is not supported");
1271 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1276 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1278 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1279 | (intr ? NFS_MOUNT_INTR : 0)
1280 | (posix ? NFS_MOUNT_POSIX : 0)
1281 | (nocto ? NFS_MOUNT_NOCTO : 0)
1282 | (noac ? NFS_MOUNT_NOAC : 0)
1283 | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0);
1284 if (nfs_mount_version >= 2)
1285 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1286 if (nfs_mount_version >= 3)
1287 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1288 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1289 bb_error_msg("NFSv%d not supported", nfsvers);
1292 if (nfsvers && !mountvers)
1293 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1294 if (nfsvers && nfsvers < mountvers) {
1295 mountvers = nfsvers;
1298 /* Adjust options if none specified */
1300 data.timeo = tcp ? 70 : 7;
1302 data.version = nfs_mount_version;
1304 if (vfsflags & MS_REMOUNT)
1308 * If the previous mount operation on the same host was
1309 * backgrounded, and the "bg" for this mount is also set,
1310 * give up immediately, to avoid the initial timeout.
1312 if (bg && we_saw_this_host_before(hostname)) {
1313 daemonized = daemonize();
1314 if (daemonized <= 0) { /* parent or error */
1315 retval = -daemonized;
1320 /* Create mount daemon client */
1321 /* See if the nfs host = mount host. */
1323 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1324 mount_server_addr.sin_family = AF_INET;
1325 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1327 hp = gethostbyname(mounthost);
1329 bb_herror_msg("%s", mounthost);
1332 if ((size_t)hp->h_length > sizeof(struct in_addr)) {
1333 bb_error_msg("got bad hp->h_length");
1334 hp->h_length = sizeof(struct in_addr);
1336 mount_server_addr.sin_family = AF_INET;
1337 memcpy(&mount_server_addr.sin_addr,
1338 hp->h_addr, hp->h_length);
1343 * The following loop implements the mount retries. When the mount
1344 * times out, and the "bg" option is set, we background ourself
1345 * and continue trying.
1347 * The case where the mount point is not present and the "bg"
1348 * option is set, is treated as a timeout. This is done to
1349 * support nested mounts.
1351 * The "retry" count specified by the user is the number of
1352 * minutes to retry before giving up.
1355 struct timeval total_timeout;
1356 struct timeval retry_timeout;
1362 retry_timeout.tv_sec = 3;
1363 retry_timeout.tv_usec = 0;
1364 total_timeout.tv_sec = 20;
1365 total_timeout.tv_usec = 0;
1366 /* FIXME: use monotonic()? */
1367 timeout = time(NULL) + 60 * retry;
1371 /* Be careful not to use too many CPU cycles */
1375 get_mountport(&pm_mnt, &mount_server_addr,
1380 nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
1382 /* contact the mount daemon via TCP */
1383 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1384 msock = RPC_ANYSOCK;
1386 switch (pm_mnt.pm_prot) {
1388 mclient = clntudp_create(&mount_server_addr,
1395 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1396 msock = RPC_ANYSOCK;
1398 mclient = clnttcp_create(&mount_server_addr,
1407 if (!daemonized && prevt == 0)
1408 error_msg_rpc(clnt_spcreateerror(" "));
1410 enum clnt_stat clnt_stat;
1412 /* Try to mount hostname:pathname */
1413 mclient->cl_auth = authunix_create_default();
1415 /* Make pointers in xdr_mountres3 NULL so
1416 * that xdr_array allocates memory for us
1418 memset(&status, 0, sizeof(status));
1420 if (pm_mnt.pm_vers == 3)
1421 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1422 (xdrproc_t) xdr_dirpath,
1423 (caddr_t) &pathname,
1424 (xdrproc_t) xdr_mountres3,
1428 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1429 (xdrproc_t) xdr_dirpath,
1430 (caddr_t) &pathname,
1431 (xdrproc_t) xdr_fhstatus,
1435 if (clnt_stat == RPC_SUCCESS)
1436 goto prepare_kernel_data; /* we're done */
1437 if (errno != ECONNREFUSED) {
1438 error_msg_rpc(clnt_sperror(mclient, " "));
1439 goto fail; /* don't retry */
1441 /* Connection refused */
1442 if (!daemonized && prevt == 0) /* print just once */
1443 error_msg_rpc(clnt_sperror(mclient, " "));
1444 auth_destroy(mclient->cl_auth);
1445 clnt_destroy(mclient);
1451 /* Timeout. We are going to retry... maybe */
1455 daemonized = daemonize();
1456 if (daemonized <= 0) { /* parent or error */
1457 retval = -daemonized;
1464 /* TODO error message */
1470 prepare_kernel_data:
1473 if (status.nfsv2.fhs_status != 0) {
1474 bb_error_msg("%s:%s failed, reason given by server: %s",
1476 nfs_strerror(status.nfsv2.fhs_status));
1479 memcpy(data.root.data,
1480 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1482 data.root.size = NFS_FHSIZE;
1483 memcpy(data.old_root.data,
1484 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1487 fhandle3 *my_fhandle;
1488 if (status.nfsv3.fhs_status != 0) {
1489 bb_error_msg("%s:%s failed, reason given by server: %s",
1491 nfs_strerror(status.nfsv3.fhs_status));
1494 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1495 memset(data.old_root.data, 0, NFS_FHSIZE);
1496 memset(&data.root, 0, sizeof(data.root));
1497 data.root.size = my_fhandle->fhandle3_len;
1498 memcpy(data.root.data,
1499 (char *) my_fhandle->fhandle3_val,
1500 my_fhandle->fhandle3_len);
1502 data.flags |= NFS_MOUNT_VER3;
1505 /* Create nfs socket for kernel */
1507 if (nfs_mount_version < 3) {
1508 bb_error_msg("NFS over TCP is not supported");
1511 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1513 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1515 bb_perror_msg("nfs socket");
1518 if (bindresvport(fsock, 0) < 0) {
1519 bb_perror_msg("nfs bindresvport");
1523 server_addr.sin_port = PMAPPORT;
1524 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1525 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1529 server_addr.sin_port = htons(port);
1531 /* Prepare data structure for kernel */
1533 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1534 strncpy(data.hostname, hostname, sizeof(data.hostname));
1537 auth_destroy(mclient->cl_auth);
1538 clnt_destroy(mclient);
1543 /* We must wait until mount directory is available */
1544 struct stat statbuf;
1546 while (stat(mp->mnt_dir, &statbuf) == -1) {
1548 daemonized = daemonize();
1549 if (daemonized <= 0) { /* parent or error */
1550 /* FIXME: parent doesn't close fsock - ??! */
1551 retval = -daemonized;
1555 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1562 /* Perform actual mount */
1564 mp->mnt_type = (char*)"nfs";
1565 retval = mount_it_now(mp, vfsflags, (char*)&data);
1572 auth_destroy(mclient->cl_auth);
1573 clnt_destroy(mclient);
1587 #else // !ENABLE_FEATURE_MOUNT_NFS
1589 // Never called. Call should be optimized out.
1590 int nfsmount(struct mntent *mp, long vfsflags, char *filteropts);
1592 #endif // !ENABLE_FEATURE_MOUNT_NFS
1594 // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1595 // type detection. Returns 0 for success, nonzero for failure.
1596 // NB: mp->xxx fields may be trashed on exit
1597 static int singlemount(struct mntent *mp, int ignore_busy)
1601 char *loopFile = NULL, *filteropts = NULL;
1605 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1607 // Treat fstype "auto" as unspecified
1608 if (mp->mnt_type && strcmp(mp->mnt_type, "auto") == 0)
1609 mp->mnt_type = NULL;
1611 // Might this be a virtual filesystem?
1612 if (ENABLE_FEATURE_MOUNT_HELPERS && strchr(mp->mnt_fsname, '#')) {
1616 // fsname: "cmd#arg1#arg2..."
1617 // WARNING: allows execution of arbitrary commands!
1618 // Try "mount 'sh#-c#sh' bogus_dir".
1619 // It is safe ONLY because non-root
1620 // cannot use two-argument mount command
1621 // and using one-argument "mount 'sh#-c#sh'" doesn't work:
1622 // "mount: can't find sh#-c#sh in /etc/fstab"
1623 // (if /etc/fstab has it, it's ok: root sets up /etc/fstab).
1628 while (*s && n < 35 - 2) {
1629 if (*s++ == '#' && *s != '#') {
1634 args[n++] = mp->mnt_dir;
1636 rc = wait4pid(xspawn(args));
1640 // Might this be an CIFS filesystem?
1641 if (ENABLE_FEATURE_MOUNT_CIFS
1642 && (!mp->mnt_type || strcmp(mp->mnt_type, "cifs") == 0)
1643 && (mp->mnt_fsname[0] == '/' || mp->mnt_fsname[0] == '\\')
1644 && mp->mnt_fsname[0] == mp->mnt_fsname[1]
1646 #if 0 /* reported to break things */
1647 len_and_sockaddr *lsa;
1651 // Replace '/' with '\' and verify that unc points to "//server/share".
1652 for (s = mp->mnt_fsname; *s; ++s)
1653 if (*s == '/') *s = '\\';
1656 s = strrchr(mp->mnt_fsname, '\\');
1657 if (s <= mp->mnt_fsname+1)
1660 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
1665 // Insert ip=... option into string flags.
1666 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1667 ip = xasprintf("ip=%s", dotted);
1668 parse_mount_options(ip, &filteropts);
1670 // Compose new unc '\\server-ip\share'
1671 // (s => slash after hostname)
1672 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
1674 // Lock is required [why?]
1675 vfsflags |= MS_MANDLOCK;
1676 mp->mnt_type = (char*)"cifs";
1677 rc = mount_it_now(mp, vfsflags, filteropts);
1679 if (ENABLE_FEATURE_CLEAN_UP) {
1680 free(mp->mnt_fsname);
1689 // Might this be an NFS filesystem?
1690 if (ENABLE_FEATURE_MOUNT_NFS
1691 && (!mp->mnt_type || !strcmp(mp->mnt_type, "nfs"))
1692 && strchr(mp->mnt_fsname, ':') != NULL
1694 rc = nfsmount(mp, vfsflags, filteropts);
1698 // Look at the file. (Not found isn't a failure for remount, or for
1699 // a synthetic filesystem like proc or sysfs.)
1700 // (We use stat, not lstat, in order to allow
1701 // mount symlink_to_file_or_blkdev dir)
1702 if (!stat(mp->mnt_fsname, &st)
1703 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1705 // Do we need to allocate a loopback device for it?
1706 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1707 loopFile = bb_simplify_path(mp->mnt_fsname);
1708 mp->mnt_fsname = NULL; // will receive malloced loop dev name
1709 if (set_loop(&(mp->mnt_fsname), loopFile, 0) < 0) {
1710 if (errno == EPERM || errno == EACCES)
1711 bb_error_msg(bb_msg_perm_denied_are_you_root);
1713 bb_perror_msg("cannot setup loop device");
1717 // Autodetect bind mounts
1718 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1719 vfsflags |= MS_BIND;
1722 // If we know the fstype (or don't need to), jump straight
1723 // to the actual mount.
1724 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1725 rc = mount_it_now(mp, vfsflags, filteropts);
1727 // Loop through filesystem types until mount succeeds
1730 // Initialize list of block backed filesystems. This has to be
1731 // done here so that during "mount -a", mounts after /proc shows up
1734 fslist = get_block_backed_filesystems();
1735 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1736 atexit(delete_block_backed_filesystems);
1739 for (fl = fslist; fl; fl = fl->link) {
1740 mp->mnt_type = fl->data;
1741 rc = mount_it_now(mp, vfsflags, filteropts);
1747 // If mount failed, clean up loop file (if any).
1748 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1749 del_loop(mp->mnt_fsname);
1750 if (ENABLE_FEATURE_CLEAN_UP) {
1752 free(mp->mnt_fsname);
1757 if (ENABLE_FEATURE_CLEAN_UP)
1760 if (errno == EBUSY && ignore_busy)
1763 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1768 * Unlike -t, -O should interpret "no" prefix differently:
1769 * -t noa,b,c = -t no(a,b,c) = mount all except fs'es with types a,b, and c
1770 * -O noa,b,c = -O noa,b,c = mount all with without option a,
1771 * or with option b or c.
1772 * But for now we do not support -O a,b,c at all (only -O a).
1774 * Another difference from -t support (match_fstype) is that
1775 * we need to examine the _list_ of options in fsopt, not just a string.
1777 static int match_opt(const char *fs_opt, const char *O_opt)
1785 if (O_opt[0] == 'n' && O_opt[1] == 'o') {
1790 len = strlen(O_opt);
1792 if (strncmp(fs_opt, O_opt, len) == 0
1793 && (fs_opt[len] == '\0' || fs_opt[len] == ',')
1797 fs_opt = strchr(fs_opt, ',');
1806 // Parse options, if necessary parse fstab/mtab, and call singlemount for
1807 // each directory to be mounted.
1808 static const char must_be_root[] ALIGN1 = "you must be root";
1810 int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1811 int mount_main(int argc UNUSED_PARAM, char **argv)
1813 char *cmdopts = xzalloc(1);
1814 char *fstype = NULL;
1815 char *O_optmatch = NULL;
1817 llist_t *lst_o = NULL;
1818 const char *fstabname;
1822 struct mntent mtpair[2], *mtcur = mtpair;
1823 IF_NOT_DESKTOP(const int nonroot = 0;)
1825 IF_DESKTOP(int nonroot = ) sanitize_env_if_suid();
1827 // Parse long options, like --bind and --move. Note that -o option
1828 // and --option are synonymous. Yes, this means --remount,rw works.
1829 for (i = j = 1; argv[i]; i++) {
1830 if (argv[i][0] == '-' && argv[i][1] == '-')
1831 append_mount_options(&cmdopts, argv[i] + 2);
1833 argv[j++] = argv[i];
1837 // Parse remaining options
1838 // Max 2 params; -o is a list, -v is a counter
1839 opt_complementary = "?2o::" IF_FEATURE_MOUNT_VERBOSE("vv");
1840 opt = getopt32(argv, OPTION_STR, &lst_o, &fstype, &O_optmatch
1841 IF_FEATURE_MOUNT_VERBOSE(, &verbose));
1842 while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
1843 if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
1844 if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w
1847 // If we have no arguments, show currently mounted filesystems
1849 if (!(opt & OPT_a)) {
1850 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1853 bb_error_msg_and_die("no %s", bb_path_mtab_file);
1855 while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
1858 // Don't show rootfs. FIXME: why??
1859 // util-linux 2.12a happily shows rootfs...
1860 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
1862 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1863 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1864 mtpair->mnt_dir, mtpair->mnt_type,
1867 if (ENABLE_FEATURE_CLEAN_UP)
1868 endmntent(mountTable);
1869 return EXIT_SUCCESS;
1871 storage_path = NULL;
1873 // When we have two arguments, the second is the directory and we can
1874 // skip looking at fstab entirely. We can always abspath() the directory
1875 // argument when we get it.
1878 bb_error_msg_and_die(must_be_root);
1879 mtpair->mnt_fsname = argv[0];
1880 mtpair->mnt_dir = argv[1];
1881 mtpair->mnt_type = fstype;
1882 mtpair->mnt_opts = cmdopts;
1883 resolve_mount_spec(&mtpair->mnt_fsname);
1884 rc = singlemount(mtpair, 0);
1887 storage_path = bb_simplify_path(argv[0]); // malloced
1890 // Past this point, we are handling either "mount -a [opts]"
1891 // or "mount [opts] single_param"
1893 i = parse_mount_options(cmdopts, NULL); // FIXME: should be "long", not "int"
1894 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1895 bb_error_msg_and_die(must_be_root);
1897 // If we have a shared subtree flag, don't worry about fstab or mtab.
1898 if (ENABLE_FEATURE_MOUNT_FLAGS
1899 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1901 // verbose_mount(source, target, type, flags, data)
1902 rc = verbose_mount("", argv[0], "", i, "");
1904 bb_simple_perror_msg_and_die(argv[0]);
1908 // Open either fstab or mtab
1909 fstabname = "/etc/fstab";
1910 if (i & MS_REMOUNT) {
1911 // WARNING. I am not sure this matches util-linux's
1912 // behavior. It's possible util-linux does not
1913 // take -o opts from mtab (takes only mount source).
1914 fstabname = bb_path_mtab_file;
1916 fstab = setmntent(fstabname, "r");
1918 bb_perror_msg_and_die("cannot read %s", fstabname);
1920 // Loop through entries until we find what we're looking for
1921 memset(mtpair, 0, sizeof(mtpair));
1923 struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair);
1925 // Get next fstab entry
1926 if (!getmntent_r(fstab, mtcur, getmntent_buf
1927 + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0),
1928 GETMNTENT_BUFSIZE/2)
1929 ) { // End of fstab/mtab is reached
1930 mtcur = mtother; // the thing we found last time
1934 // If we're trying to mount something specific and this isn't it,
1935 // skip it. Note we must match the exact text in fstab (ala
1936 // "proc") or a full path from root
1939 // Is this what we're looking for?
1940 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1941 strcmp(storage_path, mtcur->mnt_fsname) &&
1942 strcmp(argv[0], mtcur->mnt_dir) &&
1943 strcmp(storage_path, mtcur->mnt_dir)) continue;
1945 // Remember this entry. Something later may have
1946 // overmounted it, and we want the _last_ match.
1949 // If we're mounting all
1951 // No, mount -a won't mount anything,
1952 // even user mounts, for mere humans
1954 bb_error_msg_and_die(must_be_root);
1956 // Does type match? (NULL matches always)
1957 if (!match_fstype(mtcur, fstype))
1960 // Skip noauto and swap anyway.
1961 if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
1962 // swap is bogus "fstype", parse_mount_options can't check fstypes
1963 || strcasecmp(mtcur->mnt_type, "swap") == 0
1968 // Does (at least one) option match?
1969 // (NULL matches always)
1970 if (!match_opt(mtcur->mnt_opts, O_optmatch))
1973 resolve_mount_spec(&mtcur->mnt_fsname);
1975 // NFS mounts want this to be xrealloc-able
1976 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
1979 if (singlemount(mtcur, 1)) {
1980 // Count number of failed mounts
1983 free(mtcur->mnt_opts);
1987 // End of fstab/mtab is reached.
1988 // Were we looking for something specific?
1992 // If we didn't find anything, complain
1993 if (!mtcur->mnt_fsname)
1994 bb_error_msg_and_die("can't find %s in %s",
1995 argv[0], fstabname);
1997 // What happens when we try to "mount swap_partition"?
1998 // (fstab containts "swap_partition swap swap defaults 0 0")
1999 // util-linux-ng 2.13.1 does this:
2000 // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
2001 // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
2002 // lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory)
2003 // write(2, "mount: mount point swap does not exist\n", 39) = 39
2004 // exit_group(32) = ?
2006 // In case we want to simply skip swap partitions:
2007 l = parse_mount_options(mtcur->mnt_opts, NULL);
2008 if ((l & MOUNT_SWAP)
2009 // swap is bogus "fstype", parse_mount_options can't check fstypes
2010 || strcasecmp(mtcur->mnt_type, "swap") == 0
2016 // fstab must have "users" or "user"
2017 l = parse_mount_options(mtcur->mnt_opts, NULL);
2018 if (!(l & MOUNT_USERS))
2019 bb_error_msg_and_die(must_be_root);
2022 // Mount the last thing we found
2023 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
2024 append_mount_options(&(mtcur->mnt_opts), cmdopts);
2025 resolve_mount_spec(&mtpair->mnt_fsname);
2026 rc = singlemount(mtcur, 0);
2027 if (ENABLE_FEATURE_CLEAN_UP)
2028 free(mtcur->mnt_opts);
2032 if (ENABLE_FEATURE_CLEAN_UP)
2034 if (ENABLE_FEATURE_CLEAN_UP) {