blob: 4a0237196287ec711f383169626c1f0ea5c8e538 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersen596e5461999-10-07 08:30:23 +00003 * Mini mount implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Rob Landleydc0955b2006-03-14 18:16:25 +00007 * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
Eric Andersen596e5461999-10-07 08:30:23 +00008 *
Rob Landley7b363fd2005-12-20 17:18:01 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersenb7cc49d2000-01-13 06:38:14 +000010 */
Eric Andersencc8ed391999-10-05 16:24:54 +000011
Rob Landley22d26fc2006-06-15 15:49:36 +000012/* Design notes: There is no spec for mount. Remind me to write one.
Rob Landleydc0955b2006-03-14 18:16:25 +000013
14 mount_main() calls singlemount() which calls mount_it_now().
Eric Andersen9601a1c2006-03-20 18:07:50 +000015
Rob Landleydc0955b2006-03-14 18:16:25 +000016 mount_main() can loop through /etc/fstab for mount -a
17 singlemount() can loop through /etc/filesystems for fstype detection.
18 mount_it_now() does the actual mount.
19*/
20
Rob Landley22d26fc2006-06-15 15:49:36 +000021#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000022#include <mntent.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000023
Denis Vlasenko30a64cd2006-09-15 15:12:00 +000024/* Needed for nfs support only... */
25#include <syslog.h>
26#include <sys/utsname.h>
27#undef TRUE
28#undef FALSE
29#include <rpc/rpc.h>
30#include <rpc/pmap_prot.h>
31#include <rpc/pmap_clnt.h>
32
33
Denis Vlasenko908d6b72006-12-18 23:07:42 +000034#if defined(__dietlibc__)
35/* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
36 * dietlibc-0.30 does not have implementation of getmntent_r() */
37/* OTOH: why we use getmntent_r instead of getmntent? TODO... */
38struct mntent *getmntent_r(FILE* stream, struct mntent* result, char* buffer, int bufsize)
39{
40 /* *** XXX FIXME WARNING: This hack is NOT thread safe. --Sampo */
41 struct mntent* ment = getmntent(stream);
42 memcpy(result, ment, sizeof(struct mntent));
43 return result;
44}
45#endif
46
47
Rob Landleydc0955b2006-03-14 18:16:25 +000048// Not real flags, but we want to be able to check for this.
Denis Vlasenko13c5a682006-10-16 22:39:51 +000049enum {
50 MOUNT_USERS = (1<<28)*ENABLE_DESKTOP,
51 MOUNT_NOAUTO = (1<<29),
52 MOUNT_SWAP = (1<<30),
53};
54// TODO: more "user" flag compatibility.
55// "user" option (from mount manpage):
56// Only the user that mounted a filesystem can unmount it again.
57// If any user should be able to unmount, then use users instead of user
58// in the fstab line. The owner option is similar to the user option,
59// with the restriction that the user must be the owner of the special file.
60// This may be useful e.g. for /dev/fd if a login script makes
61// the console user owner of this device.
Rob Landley3ba7bd12006-08-09 19:51:13 +000062
Rob Landleydc0955b2006-03-14 18:16:25 +000063/* Standard mount options (from -o options or --options), with corresponding
64 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000065
Rob Landley6a6798b2005-08-10 20:35:54 +000066struct {
Denis Vlasenko06c0a712007-01-29 22:51:44 +000067 const char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000068 long flags;
Rob Landleye3781b72006-08-08 01:39:49 +000069} static mount_options[] = {
70 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
Rob Landleydc0955b2006-03-14 18:16:25 +000071
Rob Landleye3781b72006-08-08 01:39:49 +000072 USE_FEATURE_MOUNT_LOOP(
73 {"loop", 0},
74 )
Rob Landleydc0955b2006-03-14 18:16:25 +000075
Rob Landleye3781b72006-08-08 01:39:49 +000076 USE_FEATURE_MOUNT_FSTAB(
77 {"defaults", 0},
Denis Vlasenko5e90e102006-11-27 19:50:16 +000078 /* {"quiet", 0}, - do not filter out, vfat wants to see it */
Denis Vlasenko13c5a682006-10-16 22:39:51 +000079 {"noauto", MOUNT_NOAUTO},
Denis Vlasenkobf295dd2007-04-05 21:57:47 +000080 {"sw", MOUNT_SWAP},
Denis Vlasenko13c5a682006-10-16 22:39:51 +000081 {"swap", MOUNT_SWAP},
82 USE_DESKTOP({"user", MOUNT_USERS},)
83 USE_DESKTOP({"users", MOUNT_USERS},)
Rob Landleye3781b72006-08-08 01:39:49 +000084 )
Rob Landleydc0955b2006-03-14 18:16:25 +000085
Rob Landleye3781b72006-08-08 01:39:49 +000086 USE_FEATURE_MOUNT_FLAGS(
87 // vfs flags
88 {"nosuid", MS_NOSUID},
89 {"suid", ~MS_NOSUID},
90 {"dev", ~MS_NODEV},
91 {"nodev", MS_NODEV},
92 {"exec", ~MS_NOEXEC},
93 {"noexec", MS_NOEXEC},
94 {"sync", MS_SYNCHRONOUS},
95 {"async", ~MS_SYNCHRONOUS},
96 {"atime", ~MS_NOATIME},
97 {"noatime", MS_NOATIME},
98 {"diratime", ~MS_NODIRATIME},
99 {"nodiratime", MS_NODIRATIME},
100 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +0000101
Rob Landleye3781b72006-08-08 01:39:49 +0000102 // action flags
Rob Landleydc0955b2006-03-14 18:16:25 +0000103
Rob Landleye3781b72006-08-08 01:39:49 +0000104 {"bind", MS_BIND},
105 {"move", MS_MOVE},
106 {"shared", MS_SHARED},
107 {"slave", MS_SLAVE},
108 {"private", MS_PRIVATE},
109 {"unbindable", MS_UNBINDABLE},
110 {"rshared", MS_SHARED|MS_RECURSIVE},
111 {"rslave", MS_SLAVE|MS_RECURSIVE},
112 {"rprivate", MS_SLAVE|MS_RECURSIVE},
113 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
114 )
115
116 // Always understood.
117
118 {"ro", MS_RDONLY}, // vfs flag
119 {"rw", ~MS_RDONLY}, // vfs flag
120 {"remount", MS_REMOUNT}, // action flag
Eric Andersencc8ed391999-10-05 16:24:54 +0000121};
122
Denis Vlasenko546cd182006-10-02 18:52:49 +0000123#define VECTOR_SIZE(v) (sizeof(v) / sizeof((v)[0]))
Denis Vlasenko25098f72006-09-14 15:46:33 +0000124
Rob Landleydc0955b2006-03-14 18:16:25 +0000125/* Append mount options to string */
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000126static void append_mount_options(char **oldopts, const char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +0000127{
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000128 if (*oldopts && **oldopts) {
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000129 /* do not insert options which are already there */
130 while (newopts[0]) {
131 char *p;
132 int len = strlen(newopts);
133 p = strchr(newopts, ',');
134 if (p) len = p - newopts;
135 p = *oldopts;
136 while (1) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000137 if (!strncmp(p, newopts, len)
138 && (p[len]==',' || p[len]==0))
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000139 goto skip;
140 p = strchr(p,',');
141 if(!p) break;
142 p++;
143 }
144 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
145 free(*oldopts);
146 *oldopts = p;
147skip:
148 newopts += len;
149 while (newopts[0] == ',') newopts++;
150 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000151 } else {
152 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000153 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000154 }
155}
156
157/* Use the mount_options list to parse options into flags.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000158 * Also return list of unrecognized options if unrecognized!=NULL */
Rob Landleydc0955b2006-03-14 18:16:25 +0000159static int parse_mount_options(char *options, char **unrecognized)
160{
161 int flags = MS_SILENT;
162
Rob Landley6a6798b2005-08-10 20:35:54 +0000163 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000164 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000165 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Rob Landleydc0955b2006-03-14 18:16:25 +0000168 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000169
Rob Landley6a6798b2005-08-10 20:35:54 +0000170 // Find this option in mount_options
Denis Vlasenko546cd182006-10-02 18:52:49 +0000171 for (i = 0; i < VECTOR_SIZE(mount_options); i++) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000172 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000173 long fl = mount_options[i].flags;
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000174 if (fl < 0) flags &= fl;
Rob Landleydc0955b2006-03-14 18:16:25 +0000175 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 break;
177 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000179 // If unrecognized not NULL, append unrecognized mount options */
Denis Vlasenko546cd182006-10-02 18:52:49 +0000180 if (unrecognized && i == VECTOR_SIZE(mount_options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000181 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000182 i = *unrecognized ? strlen(*unrecognized) : 0;
183 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000184
Rob Landley6a6798b2005-08-10 20:35:54 +0000185 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000186 if (i) (*unrecognized)[i++] = ',';
187 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000189
Rob Landley6a6798b2005-08-10 20:35:54 +0000190 // Advance to next option, or finish
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000191 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 *comma = ',';
193 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000194 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000195 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000196
Rob Landleydc0955b2006-03-14 18:16:25 +0000197 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000198}
199
Rob Landleydc0955b2006-03-14 18:16:25 +0000200// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000201
Rob Landleydc0955b2006-03-14 18:16:25 +0000202static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000203{
Denis Vlasenko372686b2006-10-12 22:42:33 +0000204 static const char *const filesystems[] = {
205 "/etc/filesystems",
206 "/proc/filesystems",
207 0
208 };
209 char *fs, *buf;
Rob Landleydc0955b2006-03-14 18:16:25 +0000210 llist_t *list = 0;
211 int i;
212 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000213
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000214 for (i = 0; filesystems[i]; i++) {
215 f = fopen(filesystems[i], "r");
216 if (!f) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000217
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000218 while ((buf = xmalloc_getline(f)) != 0) {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000219 if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
220 continue;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000221 fs = skip_whitespace(buf);
Denis Vlasenko372686b2006-10-12 22:42:33 +0000222 if (*fs=='#' || *fs=='*' || !*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000223
Denis Vlasenko372686b2006-10-12 22:42:33 +0000224 llist_add_to_end(&list, xstrdup(fs));
225 free(buf);
Rob Landleydc0955b2006-03-14 18:16:25 +0000226 }
227 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
228 }
229
230 return list;
231}
232
233llist_t *fslist = 0;
234
Rob Landleydc0955b2006-03-14 18:16:25 +0000235#if ENABLE_FEATURE_CLEAN_UP
236static void delete_block_backed_filesystems(void)
237{
Rob Landleya6b5b602006-05-08 19:03:07 +0000238 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000239}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000240#else
241void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000242#endif
243
244#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000245static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000246static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000247#else
Denis Vlasenko116080a2006-09-21 11:13:08 +0000248#define useMtab 0
249#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000250#endif
251
252// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000253// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000254static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000255{
Denis Vlasenkob1726782006-09-29 14:43:20 +0000256 int rc = 0;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000257
Denis Vlasenkob1726782006-09-29 14:43:20 +0000258 if (fakeIt) goto mtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000259
Rob Landleydc0955b2006-03-14 18:16:25 +0000260 // Mount, with fallback to read-only if necessary.
261
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000262 for (;;) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000263 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
264 vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000265 if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
Rob Landleydc0955b2006-03-14 18:16:25 +0000266 break;
267 bb_error_msg("%s is write-protected, mounting read-only",
268 mp->mnt_fsname);
269 vfsflags |= MS_RDONLY;
270 }
271
Rob Landleydc0955b2006-03-14 18:16:25 +0000272 // Abort entirely if permission denied.
273
274 if (rc && errno == EPERM)
275 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
276
277 /* If the mount was successful, and we're maintaining an old-style
278 * mtab file by hand, add the new entry to it now. */
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000279 mtab:
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000280 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000281 char *fsname;
Rob Landleydc0955b2006-03-14 18:16:25 +0000282 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
283 int i;
284
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000285 if (!mountTable) {
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000286 bb_error_msg("no %s",bb_path_mtab_file);
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000287 goto ret;
288 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000289
290 // Add vfs string flags
291
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000292 for (i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000293 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000294 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000295
296 // Remove trailing / (if any) from directory we mounted on
297
Denis Vlasenko727ef942006-09-14 13:19:19 +0000298 i = strlen(mp->mnt_dir) - 1;
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000299 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000300
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000301 // Convert to canonical pathnames as needed
Denis Vlasenko727ef942006-09-14 13:19:19 +0000302
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000303 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000304 fsname = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000305 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000306 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000307 mp->mnt_type = (char*)"bind";
Denis Vlasenko727ef942006-09-14 13:19:19 +0000308 }
Denis Vlasenko25098f72006-09-14 15:46:33 +0000309 mp->mnt_freq = mp->mnt_passno = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000310
311 // Write and close.
312
Denis Vlasenko727ef942006-09-14 13:19:19 +0000313 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000314 endmntent(mountTable);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000315 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000316 free(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000317 free(fsname);
318 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000319 }
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000320 ret:
Rob Landleydc0955b2006-03-14 18:16:25 +0000321 return rc;
322}
323
Denis Vlasenko25098f72006-09-14 15:46:33 +0000324#if ENABLE_FEATURE_MOUNT_NFS
325
326/*
327 * Linux NFS mount
328 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
329 *
330 * Licensed under GPLv2, see file LICENSE in this tarball for details.
331 *
332 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
333 * numbers to be specified on the command line.
334 *
335 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
336 * Omit the call to connect() for Linux version 1.3.11 or later.
337 *
338 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
339 * Implemented the "bg", "fg" and "retry" mount options for NFS.
340 *
341 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
342 * - added Native Language Support
343 *
344 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
345 * plus NFSv3 stuff.
346 */
347
Denis Vlasenko25098f72006-09-14 15:46:33 +0000348/* This is just a warning of a common mistake. Possibly this should be a
349 * uclibc faq entry rather than in busybox... */
Denis Vlasenko30a64cd2006-09-15 15:12:00 +0000350#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000351#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
352#endif
353
354#define MOUNTPORT 635
355#define MNTPATHLEN 1024
356#define MNTNAMLEN 255
357#define FHSIZE 32
358#define FHSIZE3 64
359
360typedef char fhandle[FHSIZE];
361
362typedef struct {
363 unsigned int fhandle3_len;
364 char *fhandle3_val;
365} fhandle3;
366
367enum mountstat3 {
368 MNT_OK = 0,
369 MNT3ERR_PERM = 1,
370 MNT3ERR_NOENT = 2,
371 MNT3ERR_IO = 5,
372 MNT3ERR_ACCES = 13,
373 MNT3ERR_NOTDIR = 20,
374 MNT3ERR_INVAL = 22,
375 MNT3ERR_NAMETOOLONG = 63,
376 MNT3ERR_NOTSUPP = 10004,
377 MNT3ERR_SERVERFAULT = 10006,
378};
379typedef enum mountstat3 mountstat3;
380
381struct fhstatus {
382 unsigned int fhs_status;
383 union {
384 fhandle fhs_fhandle;
385 } fhstatus_u;
386};
387typedef struct fhstatus fhstatus;
388
389struct mountres3_ok {
390 fhandle3 fhandle;
391 struct {
392 unsigned int auth_flavours_len;
393 char *auth_flavours_val;
394 } auth_flavours;
395};
396typedef struct mountres3_ok mountres3_ok;
397
398struct mountres3 {
399 mountstat3 fhs_status;
400 union {
401 mountres3_ok mountinfo;
402 } mountres3_u;
403};
404typedef struct mountres3 mountres3;
405
406typedef char *dirpath;
407
408typedef char *name;
409
410typedef struct mountbody *mountlist;
411
412struct mountbody {
413 name ml_hostname;
414 dirpath ml_directory;
415 mountlist ml_next;
416};
417typedef struct mountbody mountbody;
418
419typedef struct groupnode *groups;
420
421struct groupnode {
422 name gr_name;
423 groups gr_next;
424};
425typedef struct groupnode groupnode;
426
427typedef struct exportnode *exports;
428
429struct exportnode {
430 dirpath ex_dir;
431 groups ex_groups;
432 exports ex_next;
433};
434typedef struct exportnode exportnode;
435
436struct ppathcnf {
437 int pc_link_max;
438 short pc_max_canon;
439 short pc_max_input;
440 short pc_name_max;
441 short pc_path_max;
442 short pc_pipe_buf;
Denis Vlasenko28703012006-12-19 20:32:02 +0000443 uint8_t pc_vdisable;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000444 char pc_xxx;
445 short pc_mask[2];
446};
447typedef struct ppathcnf ppathcnf;
448
449#define MOUNTPROG 100005
450#define MOUNTVERS 1
451
452#define MOUNTPROC_NULL 0
453#define MOUNTPROC_MNT 1
454#define MOUNTPROC_DUMP 2
455#define MOUNTPROC_UMNT 3
456#define MOUNTPROC_UMNTALL 4
457#define MOUNTPROC_EXPORT 5
458#define MOUNTPROC_EXPORTALL 6
459
460#define MOUNTVERS_POSIX 2
461
462#define MOUNTPROC_PATHCONF 7
463
464#define MOUNT_V3 3
465
466#define MOUNTPROC3_NULL 0
467#define MOUNTPROC3_MNT 1
468#define MOUNTPROC3_DUMP 2
469#define MOUNTPROC3_UMNT 3
470#define MOUNTPROC3_UMNTALL 4
471#define MOUNTPROC3_EXPORT 5
472
473enum {
474#ifndef NFS_FHSIZE
475 NFS_FHSIZE = 32,
476#endif
477#ifndef NFS_PORT
478 NFS_PORT = 2049
479#endif
480};
481
Denis Vlasenko25098f72006-09-14 15:46:33 +0000482/*
483 * We want to be able to compile mount on old kernels in such a way
484 * that the binary will work well on more recent kernels.
485 * Thus, if necessary we teach nfsmount.c the structure of new fields
486 * that will come later.
487 *
488 * Moreover, the new kernel includes conflict with glibc includes
489 * so it is easiest to ignore the kernel altogether (at compile time).
490 */
491
492struct nfs2_fh {
493 char data[32];
494};
495struct nfs3_fh {
496 unsigned short size;
497 unsigned char data[64];
498};
499
500struct nfs_mount_data {
501 int version; /* 1 */
502 int fd; /* 1 */
503 struct nfs2_fh old_root; /* 1 */
504 int flags; /* 1 */
505 int rsize; /* 1 */
506 int wsize; /* 1 */
507 int timeo; /* 1 */
508 int retrans; /* 1 */
509 int acregmin; /* 1 */
510 int acregmax; /* 1 */
511 int acdirmin; /* 1 */
512 int acdirmax; /* 1 */
513 struct sockaddr_in addr; /* 1 */
514 char hostname[256]; /* 1 */
515 int namlen; /* 2 */
516 unsigned int bsize; /* 3 */
517 struct nfs3_fh root; /* 4 */
518};
519
520/* bits in the flags field */
521enum {
522 NFS_MOUNT_SOFT = 0x0001, /* 1 */
523 NFS_MOUNT_INTR = 0x0002, /* 1 */
524 NFS_MOUNT_SECURE = 0x0004, /* 1 */
525 NFS_MOUNT_POSIX = 0x0008, /* 1 */
526 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
527 NFS_MOUNT_NOAC = 0x0020, /* 1 */
528 NFS_MOUNT_TCP = 0x0040, /* 2 */
529 NFS_MOUNT_VER3 = 0x0080, /* 3 */
530 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
531 NFS_MOUNT_NONLM = 0x0200 /* 3 */
532};
533
534
535/*
536 * We need to translate between nfs status return values and
537 * the local errno values which may not be the same.
538 *
539 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
540 * "after #include <errno.h> the symbol errno is reserved for any use,
541 * it cannot even be used as a struct tag or field name".
542 */
543
544#ifndef EDQUOT
545#define EDQUOT ENOSPC
546#endif
547
548// Convert each NFSERR_BLAH into EBLAH
549
550static const struct {
551 int stat;
552 int errnum;
553} nfs_errtbl[] = {
554 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
555 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
556 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
557 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
558};
559
560static char *nfs_strerror(int status)
561{
562 int i;
Denis Vlasenkoc0975192006-09-17 15:06:34 +0000563 static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3];
Denis Vlasenko25098f72006-09-14 15:46:33 +0000564
565 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
566 if (nfs_errtbl[i].stat == status)
567 return strerror(nfs_errtbl[i].errnum);
568 }
569 sprintf(buf, "unknown nfs status return value: %d", status);
570 return buf;
571}
572
573static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
574{
575 if (!xdr_opaque(xdrs, objp, FHSIZE))
576 return FALSE;
577 return TRUE;
578}
579
580static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
581{
582 if (!xdr_u_int(xdrs, &objp->fhs_status))
583 return FALSE;
584 switch (objp->fhs_status) {
585 case 0:
586 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
587 return FALSE;
588 break;
589 default:
590 break;
591 }
592 return TRUE;
593}
594
595static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
596{
597 if (!xdr_string(xdrs, objp, MNTPATHLEN))
598 return FALSE;
599 return TRUE;
600}
601
602static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
603{
604 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
605 return FALSE;
606 return TRUE;
607}
608
609static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
610{
611 if (!xdr_fhandle3(xdrs, &objp->fhandle))
612 return FALSE;
613 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
614 sizeof (int), (xdrproc_t) xdr_int))
615 return FALSE;
616 return TRUE;
617}
618
619static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
620{
621 if (!xdr_enum(xdrs, (enum_t *) objp))
622 return FALSE;
623 return TRUE;
624}
625
626static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
627{
628 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
629 return FALSE;
630 switch (objp->fhs_status) {
631 case MNT_OK:
632 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
633 return FALSE;
634 break;
635 default:
636 break;
637 }
638 return TRUE;
639}
640
641#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
642
643/*
644 * nfs_mount_version according to the sources seen at compile time.
645 */
646static int nfs_mount_version;
647static int kernel_version;
648
649/*
650 * Unfortunately, the kernel prints annoying console messages
651 * in case of an unexpected nfs mount version (instead of
652 * just returning some error). Therefore we'll have to try
653 * and figure out what version the kernel expects.
654 *
655 * Variables:
656 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
657 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
658 * nfs_mount_version: version this source and running kernel can handle
659 */
660static void
661find_kernel_nfs_mount_version(void)
662{
663 if (kernel_version)
664 return;
665
666 nfs_mount_version = 4; /* default */
667
668 kernel_version = get_linux_version_code();
669 if (kernel_version) {
670 if (kernel_version < KERNEL_VERSION(2,1,32))
671 nfs_mount_version = 1;
672 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
673 (kernel_version >= KERNEL_VERSION(2,3,0) &&
674 kernel_version < KERNEL_VERSION(2,3,99)))
675 nfs_mount_version = 3;
676 /* else v4 since 2.3.99pre4 */
677 }
678}
679
680static struct pmap *
681get_mountport(struct sockaddr_in *server_addr,
682 long unsigned prog,
683 long unsigned version,
684 long unsigned proto,
685 long unsigned port)
686{
687 struct pmaplist *pmap;
688 static struct pmap p = {0, 0, 0, 0};
689
690 server_addr->sin_port = PMAPPORT;
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000691/* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
692 * I understand it like "IPv6 for this is not 100% ready" */
Denis Vlasenko25098f72006-09-14 15:46:33 +0000693 pmap = pmap_getmaps(server_addr);
694
695 if (version > MAX_NFSPROT)
696 version = MAX_NFSPROT;
697 if (!prog)
698 prog = MOUNTPROG;
699 p.pm_prog = prog;
700 p.pm_vers = version;
701 p.pm_prot = proto;
702 p.pm_port = port;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000703
Denis Vlasenko25098f72006-09-14 15:46:33 +0000704 while (pmap) {
705 if (pmap->pml_map.pm_prog != prog)
706 goto next;
707 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
708 goto next;
709 if (version > 2 && pmap->pml_map.pm_vers != version)
710 goto next;
711 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
712 goto next;
713 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
714 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
715 (port && pmap->pml_map.pm_port != port))
716 goto next;
717 memcpy(&p, &pmap->pml_map, sizeof(p));
718next:
719 pmap = pmap->pml_next;
720 }
721 if (!p.pm_vers)
722 p.pm_vers = MOUNTVERS;
723 if (!p.pm_port)
724 p.pm_port = MOUNTPORT;
725 if (!p.pm_prot)
726 p.pm_prot = IPPROTO_TCP;
727 return &p;
728}
729
730static int daemonize(void)
731{
732 int fd;
733 int pid = fork();
734 if (pid < 0) /* error */
735 return -errno;
736 if (pid > 0) /* parent */
737 return 0;
738 /* child */
739 fd = xopen(bb_dev_null, O_RDWR);
740 dup2(fd, 0);
741 dup2(fd, 1);
742 dup2(fd, 2);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000743 while (fd > 2) close(fd--);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000744 setsid();
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000745 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000746 logmode = LOGMODE_SYSLOG;
747 return 1;
748}
749
750// TODO
751static inline int we_saw_this_host_before(const char *hostname)
752{
753 return 0;
754}
755
756/* RPC strerror analogs are terminally idiotic:
757 * *mandatory* prefix and \n at end.
758 * This hopefully helps. Usage:
759 * error_msg_rpc(clnt_*error*(" ")) */
760static void error_msg_rpc(const char *msg)
761{
Denis Vlasenko23514fe2006-09-19 14:07:52 +0000762 int len;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000763 while (msg[0] == ' ' || msg[0] == ':') msg++;
764 len = strlen(msg);
765 while (len && msg[len-1] == '\n') len--;
766 bb_error_msg("%.*s", len, msg);
767}
768
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +0000769// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000770static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
771{
772 CLIENT *mclient;
773 char *hostname;
774 char *pathname;
775 char *mounthost;
776 struct nfs_mount_data data;
777 char *opt;
778 struct hostent *hp;
779 struct sockaddr_in server_addr;
780 struct sockaddr_in mount_server_addr;
781 int msock, fsock;
782 union {
783 struct fhstatus nfsv2;
784 struct mountres3 nfsv3;
785 } status;
786 int daemonized;
787 char *s;
788 int port;
789 int mountport;
790 int proto;
791 int bg;
792 int soft;
793 int intr;
794 int posix;
795 int nocto;
796 int noac;
797 int nolock;
798 int retry;
799 int tcp;
800 int mountprog;
801 int mountvers;
802 int nfsprog;
803 int nfsvers;
804 int retval;
805
806 find_kernel_nfs_mount_version();
807
808 daemonized = 0;
809 mounthost = NULL;
810 retval = ETIMEDOUT;
811 msock = fsock = -1;
812 mclient = NULL;
813
814 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
815
816 filteropts = xstrdup(filteropts); /* going to trash it later... */
817
818 hostname = xstrdup(mp->mnt_fsname);
819 /* mount_main() guarantees that ':' is there */
820 s = strchr(hostname, ':');
821 pathname = s + 1;
822 *s = '\0';
823 /* Ignore all but first hostname in replicated mounts
824 until they can be fully supported. (mack@sgi.com) */
825 s = strchr(hostname, ',');
826 if (s) {
827 *s = '\0';
828 bb_error_msg("warning: multiple hostnames not supported");
829 }
830
831 server_addr.sin_family = AF_INET;
832 if (!inet_aton(hostname, &server_addr.sin_addr)) {
833 hp = gethostbyname(hostname);
834 if (hp == NULL) {
835 bb_herror_msg("%s", hostname);
836 goto fail;
837 }
838 if (hp->h_length > sizeof(struct in_addr)) {
839 bb_error_msg("got bad hp->h_length");
840 hp->h_length = sizeof(struct in_addr);
841 }
842 memcpy(&server_addr.sin_addr,
843 hp->h_addr, hp->h_length);
844 }
845
846 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
847
848 /* add IP address to mtab options for use when unmounting */
849
850 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
851 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
852 } else {
853 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
854 mp->mnt_opts[0] ? "," : "",
855 inet_ntoa(server_addr.sin_addr));
856 free(mp->mnt_opts);
857 mp->mnt_opts = tmp;
858 }
859
860 /* Set default options.
861 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
862 * let the kernel decide.
863 * timeo is filled in after we know whether it'll be TCP or UDP. */
864 memset(&data, 0, sizeof(data));
865 data.retrans = 3;
866 data.acregmin = 3;
867 data.acregmax = 60;
868 data.acdirmin = 30;
869 data.acdirmax = 60;
870 data.namlen = NAME_MAX;
871
872 bg = 0;
873 soft = 0;
874 intr = 0;
875 posix = 0;
876 nocto = 0;
877 nolock = 0;
878 noac = 0;
879 retry = 10000; /* 10000 minutes ~ 1 week */
880 tcp = 0;
881
882 mountprog = MOUNTPROG;
883 mountvers = 0;
884 port = 0;
885 mountport = 0;
886 nfsprog = 100003;
887 nfsvers = 0;
888
889 /* parse options */
890
891 for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
892 char *opteq = strchr(opt, '=');
893 if (opteq) {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000894 const char *const options[] = {
895 /* 0 */ "rsize",
896 /* 1 */ "wsize",
897 /* 2 */ "timeo",
898 /* 3 */ "retrans",
899 /* 4 */ "acregmin",
900 /* 5 */ "acregmax",
901 /* 6 */ "acdirmin",
902 /* 7 */ "acdirmax",
903 /* 8 */ "actimeo",
904 /* 9 */ "retry",
905 /* 10 */ "port",
906 /* 11 */ "mountport",
907 /* 12 */ "mounthost",
908 /* 13 */ "mountprog",
909 /* 14 */ "mountvers",
910 /* 15 */ "nfsprog",
911 /* 16 */ "nfsvers",
912 /* 17 */ "vers",
913 /* 18 */ "proto",
914 /* 19 */ "namlen",
915 /* 20 */ "addr",
916 NULL
917 };
Denis Vlasenko13858992006-10-08 12:49:22 +0000918 int val = xatoi_u(opteq + 1);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000919 *opteq = '\0';
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000920 switch (index_in_str_array(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000921 case 0: // "rsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000922 data.rsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000923 break;
924 case 1: // "wsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000925 data.wsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000926 break;
927 case 2: // "timeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000928 data.timeo = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000929 break;
930 case 3: // "retrans"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000931 data.retrans = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000932 break;
933 case 4: // "acregmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000934 data.acregmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000935 break;
936 case 5: // "acregmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000937 data.acregmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000938 break;
939 case 6: // "acdirmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000940 data.acdirmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000941 break;
942 case 7: // "acdirmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000943 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000944 break;
945 case 8: // "actimeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000946 data.acregmin = val;
947 data.acregmax = val;
948 data.acdirmin = val;
949 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000950 break;
951 case 9: // "retry"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000952 retry = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000953 break;
954 case 10: // "port"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000955 port = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000956 break;
957 case 11: // "mountport"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000958 mountport = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000959 break;
960 case 12: // "mounthost"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000961 mounthost = xstrndup(opteq+1,
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000962 strcspn(opteq+1," \t\n\r,"));
Denis Vlasenko68f21872006-10-26 01:47:34 +0000963 break;
964 case 13: // "mountprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000965 mountprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000966 break;
967 case 14: // "mountvers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000968 mountvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000969 break;
970 case 15: // "nfsprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000971 nfsprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000972 break;
973 case 16: // "nfsvers"
974 case 17: // "vers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000975 nfsvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000976 break;
977 case 18: // "proto"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000978 if (!strncmp(opteq+1, "tcp", 3))
979 tcp = 1;
980 else if (!strncmp(opteq+1, "udp", 3))
981 tcp = 0;
982 else
983 bb_error_msg("warning: unrecognized proto= option");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000984 break;
985 case 19: // "namlen"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000986 if (nfs_mount_version >= 2)
987 data.namlen = val;
988 else
989 bb_error_msg("warning: option namlen is not supported\n");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000990 break;
991 case 20: // "addr" - ignore
992 break;
993 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +0000994 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
995 goto fail;
996 }
997 }
998 else {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000999 const char *const options[] = {
1000 "bg",
1001 "fg",
1002 "soft",
1003 "hard",
1004 "intr",
1005 "posix",
1006 "cto",
1007 "ac",
1008 "tcp",
1009 "udp",
1010 "lock",
1011 NULL
1012 };
Denis Vlasenko25098f72006-09-14 15:46:33 +00001013 int val = 1;
1014 if (!strncmp(opt, "no", 2)) {
1015 val = 0;
1016 opt += 2;
1017 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001018 switch (index_in_str_array(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +00001019 case 0: // "bg"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001020 bg = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001021 break;
1022 case 1: // "fg"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001023 bg = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001024 break;
1025 case 2: // "soft"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001026 soft = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001027 break;
1028 case 3: // "hard"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001029 soft = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001030 break;
1031 case 4: // "intr"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001032 intr = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001033 break;
1034 case 5: // "posix"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001035 posix = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001036 break;
1037 case 6: // "cto"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001038 nocto = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001039 break;
1040 case 7: // "ac"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001041 noac = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001042 break;
1043 case 8: // "tcp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001044 tcp = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001045 break;
1046 case 9: // "udp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001047 tcp = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001048 break;
1049 case 10: // "lock"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001050 if (nfs_mount_version >= 3)
1051 nolock = !val;
1052 else
1053 bb_error_msg("warning: option nolock is not supported");
Denis Vlasenko68f21872006-10-26 01:47:34 +00001054 break;
1055 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +00001056 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1057 goto fail;
1058 }
1059 }
1060 }
1061 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1062
1063 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1064 | (intr ? NFS_MOUNT_INTR : 0)
1065 | (posix ? NFS_MOUNT_POSIX : 0)
1066 | (nocto ? NFS_MOUNT_NOCTO : 0)
1067 | (noac ? NFS_MOUNT_NOAC : 0);
1068 if (nfs_mount_version >= 2)
1069 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1070 if (nfs_mount_version >= 3)
1071 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1072 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1073 bb_error_msg("NFSv%d not supported", nfsvers);
1074 goto fail;
1075 }
1076 if (nfsvers && !mountvers)
1077 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1078 if (nfsvers && nfsvers < mountvers) {
1079 mountvers = nfsvers;
1080 }
1081
1082 /* Adjust options if none specified */
1083 if (!data.timeo)
1084 data.timeo = tcp ? 70 : 7;
1085
Denis Vlasenko25098f72006-09-14 15:46:33 +00001086 data.version = nfs_mount_version;
1087
1088 if (vfsflags & MS_REMOUNT)
1089 goto do_mount;
1090
1091 /*
1092 * If the previous mount operation on the same host was
1093 * backgrounded, and the "bg" for this mount is also set,
1094 * give up immediately, to avoid the initial timeout.
1095 */
1096 if (bg && we_saw_this_host_before(hostname)) {
1097 daemonized = daemonize(); /* parent or error */
1098 if (daemonized <= 0) { /* parent or error */
1099 retval = -daemonized;
1100 goto ret;
1101 }
1102 }
1103
1104 /* create mount daemon client */
1105 /* See if the nfs host = mount host. */
1106 if (mounthost) {
1107 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1108 mount_server_addr.sin_family = AF_INET;
1109 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1110 } else {
1111 hp = gethostbyname(mounthost);
1112 if (hp == NULL) {
1113 bb_herror_msg("%s", mounthost);
1114 goto fail;
1115 } else {
1116 if (hp->h_length > sizeof(struct in_addr)) {
1117 bb_error_msg("got bad hp->h_length?");
1118 hp->h_length = sizeof(struct in_addr);
1119 }
1120 mount_server_addr.sin_family = AF_INET;
1121 memcpy(&mount_server_addr.sin_addr,
1122 hp->h_addr, hp->h_length);
1123 }
1124 }
1125 }
1126
1127 /*
1128 * The following loop implements the mount retries. When the mount
1129 * times out, and the "bg" option is set, we background ourself
1130 * and continue trying.
1131 *
1132 * The case where the mount point is not present and the "bg"
1133 * option is set, is treated as a timeout. This is done to
1134 * support nested mounts.
1135 *
1136 * The "retry" count specified by the user is the number of
1137 * minutes to retry before giving up.
1138 */
1139 {
1140 struct timeval total_timeout;
1141 struct timeval retry_timeout;
1142 struct pmap* pm_mnt;
1143 time_t t;
1144 time_t prevt;
1145 time_t timeout;
1146
1147 retry_timeout.tv_sec = 3;
1148 retry_timeout.tv_usec = 0;
1149 total_timeout.tv_sec = 20;
1150 total_timeout.tv_usec = 0;
1151 timeout = time(NULL) + 60 * retry;
1152 prevt = 0;
1153 t = 30;
1154retry:
1155 /* be careful not to use too many CPU cycles */
1156 if (t - prevt < 30)
1157 sleep(30);
1158
1159 pm_mnt = get_mountport(&mount_server_addr,
1160 mountprog,
1161 mountvers,
1162 proto,
1163 mountport);
1164 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
1165
1166 /* contact the mount daemon via TCP */
1167 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1168 msock = RPC_ANYSOCK;
1169
1170 switch (pm_mnt->pm_prot) {
1171 case IPPROTO_UDP:
1172 mclient = clntudp_create(&mount_server_addr,
1173 pm_mnt->pm_prog,
1174 pm_mnt->pm_vers,
1175 retry_timeout,
1176 &msock);
1177 if (mclient)
1178 break;
1179 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1180 msock = RPC_ANYSOCK;
1181 case IPPROTO_TCP:
1182 mclient = clnttcp_create(&mount_server_addr,
1183 pm_mnt->pm_prog,
1184 pm_mnt->pm_vers,
1185 &msock, 0, 0);
1186 break;
1187 default:
1188 mclient = 0;
1189 }
1190 if (!mclient) {
1191 if (!daemonized && prevt == 0)
1192 error_msg_rpc(clnt_spcreateerror(" "));
1193 } else {
1194 enum clnt_stat clnt_stat;
1195 /* try to mount hostname:pathname */
1196 mclient->cl_auth = authunix_create_default();
1197
1198 /* make pointers in xdr_mountres3 NULL so
1199 * that xdr_array allocates memory for us
1200 */
1201 memset(&status, 0, sizeof(status));
1202
1203 if (pm_mnt->pm_vers == 3)
1204 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1205 (xdrproc_t) xdr_dirpath,
1206 (caddr_t) &pathname,
1207 (xdrproc_t) xdr_mountres3,
1208 (caddr_t) &status,
1209 total_timeout);
1210 else
1211 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1212 (xdrproc_t) xdr_dirpath,
1213 (caddr_t) &pathname,
1214 (xdrproc_t) xdr_fhstatus,
1215 (caddr_t) &status,
1216 total_timeout);
1217
1218 if (clnt_stat == RPC_SUCCESS)
1219 goto prepare_kernel_data; /* we're done */
1220 if (errno != ECONNREFUSED) {
1221 error_msg_rpc(clnt_sperror(mclient, " "));
1222 goto fail; /* don't retry */
1223 }
1224 /* Connection refused */
1225 if (!daemonized && prevt == 0) /* print just once */
1226 error_msg_rpc(clnt_sperror(mclient, " "));
1227 auth_destroy(mclient->cl_auth);
1228 clnt_destroy(mclient);
1229 mclient = 0;
1230 close(msock);
1231 }
1232
1233 /* Timeout. We are going to retry... maybe */
1234
1235 if (!bg)
1236 goto fail;
1237 if (!daemonized) {
1238 daemonized = daemonize();
1239 if (daemonized <= 0) { /* parent or error */
1240 retval = -daemonized;
1241 goto ret;
1242 }
1243 }
1244 prevt = t;
1245 t = time(NULL);
1246 if (t >= timeout)
1247 /* TODO error message */
1248 goto fail;
1249
1250 goto retry;
1251 }
1252
1253prepare_kernel_data:
1254
1255 if (nfsvers == 2) {
1256 if (status.nfsv2.fhs_status != 0) {
1257 bb_error_msg("%s:%s failed, reason given by server: %s",
1258 hostname, pathname,
1259 nfs_strerror(status.nfsv2.fhs_status));
1260 goto fail;
1261 }
1262 memcpy(data.root.data,
1263 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1264 NFS_FHSIZE);
1265 data.root.size = NFS_FHSIZE;
1266 memcpy(data.old_root.data,
1267 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1268 NFS_FHSIZE);
1269 } else {
1270 fhandle3 *my_fhandle;
1271 if (status.nfsv3.fhs_status != 0) {
1272 bb_error_msg("%s:%s failed, reason given by server: %s",
1273 hostname, pathname,
1274 nfs_strerror(status.nfsv3.fhs_status));
1275 goto fail;
1276 }
1277 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1278 memset(data.old_root.data, 0, NFS_FHSIZE);
1279 memset(&data.root, 0, sizeof(data.root));
1280 data.root.size = my_fhandle->fhandle3_len;
1281 memcpy(data.root.data,
1282 (char *) my_fhandle->fhandle3_val,
1283 my_fhandle->fhandle3_len);
1284
1285 data.flags |= NFS_MOUNT_VER3;
1286 }
1287
1288 /* create nfs socket for kernel */
1289
1290 if (tcp) {
1291 if (nfs_mount_version < 3) {
1292 bb_error_msg("NFS over TCP is not supported");
1293 goto fail;
1294 }
1295 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1296 } else
1297 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1298 if (fsock < 0) {
1299 bb_perror_msg("nfs socket");
1300 goto fail;
1301 }
1302 if (bindresvport(fsock, 0) < 0) {
1303 bb_perror_msg("nfs bindresvport");
1304 goto fail;
1305 }
1306 if (port == 0) {
1307 server_addr.sin_port = PMAPPORT;
1308 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1309 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1310 if (port == 0)
1311 port = NFS_PORT;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001312 }
Denis Vlasenko25098f72006-09-14 15:46:33 +00001313 server_addr.sin_port = htons(port);
1314
1315 /* prepare data structure for kernel */
1316
1317 data.fd = fsock;
1318 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1319 strncpy(data.hostname, hostname, sizeof(data.hostname));
1320
1321 /* clean up */
1322
1323 auth_destroy(mclient->cl_auth);
1324 clnt_destroy(mclient);
1325 close(msock);
1326
1327 if (bg) {
1328 /* We must wait until mount directory is available */
1329 struct stat statbuf;
1330 int delay = 1;
1331 while (stat(mp->mnt_dir, &statbuf) == -1) {
1332 if (!daemonized) {
1333 daemonized = daemonize();
1334 if (daemonized <= 0) { /* parent or error */
1335 retval = -daemonized;
1336 goto ret;
1337 }
1338 }
1339 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1340 delay *= 2;
1341 if (delay > 30)
1342 delay = 30;
1343 }
1344 }
1345
1346do_mount: /* perform actual mount */
1347
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001348 mp->mnt_type = (char*)"nfs";
Denis Vlasenko25098f72006-09-14 15:46:33 +00001349 retval = mount_it_now(mp, vfsflags, (char*)&data);
1350 goto ret;
1351
1352fail: /* abort */
1353
1354 if (msock != -1) {
1355 if (mclient) {
1356 auth_destroy(mclient->cl_auth);
1357 clnt_destroy(mclient);
1358 }
1359 close(msock);
1360 }
1361 if (fsock != -1)
1362 close(fsock);
1363
1364ret:
1365 free(hostname);
1366 free(mounthost);
1367 free(filteropts);
1368 return retval;
1369}
1370
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001371#else /* !ENABLE_FEATURE_MOUNT_NFS */
1372
1373/* Never called. Call should be optimized out. */
1374int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1375
1376#endif /* !ENABLE_FEATURE_MOUNT_NFS */
1377
1378// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1379// type detection. Returns 0 for success, nonzero for failure.
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001380// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001381static int singlemount(struct mntent *mp, int ignore_busy)
1382{
1383 int rc = -1, vfsflags;
1384 char *loopFile = 0, *filteropts = 0;
1385 llist_t *fl = 0;
1386 struct stat st;
1387
1388 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1389
1390 // Treat fstype "auto" as unspecified.
1391
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001392 if (mp->mnt_type && strcmp(mp->mnt_type,"auto") == 0)
1393 mp->mnt_type = 0;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001394
1395 // Might this be an CIFS filesystem?
1396
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001397 if (ENABLE_FEATURE_MOUNT_CIFS
1398 && (!mp->mnt_type || strcmp(mp->mnt_type,"cifs") == 0)
1399 && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')
1400 && mp->mnt_fsname[0]==mp->mnt_fsname[1]
1401 ) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001402 len_and_sockaddr *lsa;
1403 char *ip, *dotted;
1404 char *s;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001405
1406 rc = 1;
1407 // Replace '/' with '\' and verify that unc points to "//server/share".
1408
1409 for (s = mp->mnt_fsname; *s; ++s)
1410 if (*s == '/') *s = '\\';
1411
1412 // get server IP
1413
1414 s = strrchr(mp->mnt_fsname, '\\');
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001415 if (s <= mp->mnt_fsname+1) goto report_error;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001416 *s = '\0';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001417 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001418 *s = '\\';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001419 if (!lsa) goto report_error;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001420
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001421 // insert ip=... option into string flags.
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001422
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001423 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa, lsa->len);
1424 ip = xasprintf("ip=%s", dotted);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001425 parse_mount_options(ip, &filteropts);
1426
1427 // compose new unc '\\server-ip\share'
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001428 // (s => slash after hostname)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001429
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001430 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001431
1432 // lock is required
1433 vfsflags |= MS_MANDLOCK;
1434
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001435 mp->mnt_type = (char*)"cifs";
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001436 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001437 if (ENABLE_FEATURE_CLEAN_UP) {
1438 free(mp->mnt_fsname);
1439 free(ip);
1440 free(dotted);
1441 free(lsa);
1442 }
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001443 goto report_error;
1444 }
1445
1446 // Might this be an NFS filesystem?
1447
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001448 if (ENABLE_FEATURE_MOUNT_NFS
1449 && (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs"))
1450 && strchr(mp->mnt_fsname, ':') != NULL
1451 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001452 rc = nfsmount(mp, vfsflags, filteropts);
1453 goto report_error;
1454 }
1455
1456 // Look at the file. (Not found isn't a failure for remount, or for
1457 // a synthetic filesystem like proc or sysfs.)
1458
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001459 if (!lstat(mp->mnt_fsname, &st)
1460 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1461 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001462 // Do we need to allocate a loopback device for it?
1463
1464 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1465 loopFile = bb_simplify_path(mp->mnt_fsname);
1466 mp->mnt_fsname = 0;
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001467 switch (set_loop(&(mp->mnt_fsname), loopFile, 0)) {
Denis Vlasenko13b49242006-09-17 15:04:35 +00001468 case 0:
1469 case 1:
1470 break;
1471 default:
1472 bb_error_msg( errno == EPERM || errno == EACCES
1473 ? bb_msg_perm_denied_are_you_root
1474 : "cannot setup loop device");
1475 return errno;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001476 }
1477
1478 // Autodetect bind mounts
1479
1480 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1481 vfsflags |= MS_BIND;
1482 }
1483
1484 /* If we know the fstype (or don't need to), jump straight
1485 * to the actual mount. */
1486
1487 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1488 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001489 else {
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001490 // Loop through filesystem types until mount succeeds
1491 // or we run out
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001492
1493 /* Initialize list of block backed filesystems. This has to be
1494 * done here so that during "mount -a", mounts after /proc shows up
1495 * can autodetect. */
1496
1497 if (!fslist) {
1498 fslist = get_block_backed_filesystems();
1499 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1500 atexit(delete_block_backed_filesystems);
1501 }
1502
1503 for (fl = fslist; fl; fl = fl->link) {
1504 mp->mnt_type = fl->data;
Denis Vlasenko13b49242006-09-17 15:04:35 +00001505 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001506 if (!rc) break;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001507 }
1508 }
1509
1510 // If mount failed, clean up loop file (if any).
1511
1512 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1513 del_loop(mp->mnt_fsname);
1514 if (ENABLE_FEATURE_CLEAN_UP) {
1515 free(loopFile);
1516 free(mp->mnt_fsname);
1517 }
1518 }
1519
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001520 report_error:
1521 if (ENABLE_FEATURE_CLEAN_UP)
1522 free(filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001523
1524 if (rc && errno == EBUSY && ignore_busy) rc = 0;
1525 if (rc < 0)
1526 /* perror here sometimes says "mounting ... on ... failed: Success" */
1527 bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1528
1529 return rc;
1530}
1531
1532// Parse options, if necessary parse fstab/mtab, and call singlemount for
1533// each directory to be mounted.
1534
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001535const char must_be_root[] = "you must be root";
1536
Denis Vlasenko06af2162007-02-03 17:28:39 +00001537int mount_main(int argc, char **argv);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001538int mount_main(int argc, char **argv)
1539{
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001540 enum { OPT_ALL = 0x10 };
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001541
1542 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001543 char *opt_o;
1544 const char *fstabname;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001545 FILE *fstab;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001546 int i, j, rc = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001547 unsigned opt;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001548 struct mntent mtpair[2], *mtcur = mtpair;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001549 SKIP_DESKTOP(const int nonroot = 0;)
1550 USE_DESKTOP( int nonroot = (getuid() != 0);)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001551
1552 /* parse long options, like --bind and --move. Note that -o option
1553 * and --option are synonymous. Yes, this means --remount,rw works. */
1554
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001555 for (i = j = 0; i < argc; i++) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001556 if (argv[i][0] == '-' && argv[i][1] == '-') {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001557 append_mount_options(&cmdopts, argv[i]+2);
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001558 } else argv[j++] = argv[i];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001559 }
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001560 argv[j] = 0;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001561 argc = j;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001562
1563 // Parse remaining options
1564
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001565 opt = getopt32(argc, argv, "o:t:rwanfvs", &opt_o, &fstype);
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001566 if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
1567 //if (opt & 0x2) // -t
1568 if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
1569 if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
1570 //if (opt & 0x10) // -a
Denis Vlasenkob1726782006-09-29 14:43:20 +00001571 if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
1572 if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
Denis Vlasenko546cd182006-10-02 18:52:49 +00001573 //if (opt & 0x80) // -v: verbose (ignore)
1574 //if (opt & 0x100) // -s: sloppy (ignore)
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001575 argv += optind;
1576 argc -= optind;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001577
1578 // Three or more non-option arguments? Die with a usage message.
1579
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001580 if (argc > 2) bb_show_usage();
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001581
1582 // If we have no arguments, show currently mounted filesystems
1583
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001584 if (!argc) {
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001585 if (!(opt & OPT_ALL)) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001586 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1587
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001588 if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001589
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001590 while (getmntent_r(mountTable, mtpair, bb_common_bufsiz1,
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001591 sizeof(bb_common_bufsiz1)))
1592 {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001593 // Don't show rootfs. FIXME: why??
Denis Vlasenko908d6b72006-12-18 23:07:42 +00001594 // util-linux 2.12a happily shows rootfs...
1595 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001596
1597 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1598 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1599 mtpair->mnt_dir, mtpair->mnt_type,
1600 mtpair->mnt_opts);
1601 }
1602 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1603 return EXIT_SUCCESS;
1604 }
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001605 } else storage_path = bb_simplify_path(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001606
1607 // When we have two arguments, the second is the directory and we can
1608 // skip looking at fstab entirely. We can always abspath() the directory
1609 // argument when we get it.
1610
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001611 if (argc == 2) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001612 if (nonroot)
1613 bb_error_msg_and_die(must_be_root);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001614 mtpair->mnt_fsname = argv[0];
1615 mtpair->mnt_dir = argv[1];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001616 mtpair->mnt_type = fstype;
1617 mtpair->mnt_opts = cmdopts;
1618 rc = singlemount(mtpair, 0);
1619 goto clean_up;
1620 }
1621
Denis Vlasenko546cd182006-10-02 18:52:49 +00001622 i = parse_mount_options(cmdopts, 0);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001623 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1624 bb_error_msg_and_die(must_be_root);
Denis Vlasenko546cd182006-10-02 18:52:49 +00001625
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001626 // If we have a shared subtree flag, don't worry about fstab or mtab.
Denis Vlasenko546cd182006-10-02 18:52:49 +00001627
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001628 if (ENABLE_FEATURE_MOUNT_FLAGS
1629 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1630 ) {
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001631 rc = mount("", argv[0], "", i, "");
1632 if (rc) bb_perror_msg_and_die("%s", argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001633 goto clean_up;
1634 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001635
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001636 // Open either fstab or mtab
1637
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001638 fstabname = "/etc/fstab";
1639 if (i & MS_REMOUNT) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001640 fstabname = bb_path_mtab_file;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001641 }
1642 fstab = setmntent(fstabname, "r");
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001643 if (!fstab)
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001644 bb_perror_msg_and_die("cannot read %s", fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001645
1646 // Loop through entries until we find what we're looking for.
1647
Denis Vlasenko546cd182006-10-02 18:52:49 +00001648 memset(mtpair, 0, sizeof(mtpair));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001649 for (;;) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001650 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001651
1652 // Get next fstab entry
1653
1654 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
1655 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
1656 sizeof(bb_common_bufsiz1)/2))
1657 {
1658 // Were we looking for something specific?
1659
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001660 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001661
1662 // If we didn't find anything, complain.
1663
1664 if (!mtnext->mnt_fsname)
1665 bb_error_msg_and_die("can't find %s in %s",
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001666 argv[0], fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001667
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001668 mtcur = mtnext;
1669 if (nonroot) {
1670 // fstab must have "users" or "user"
1671 if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
1672 bb_error_msg_and_die(must_be_root);
1673 }
1674
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001675 // Mount the last thing we found.
1676
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001677 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001678 append_mount_options(&(mtcur->mnt_opts), cmdopts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001679 rc = singlemount(mtcur, 0);
1680 free(mtcur->mnt_opts);
1681 }
1682 goto clean_up;
1683 }
1684
1685 /* If we're trying to mount something specific and this isn't it,
1686 * skip it. Note we must match both the exact text in fstab (ala
1687 * "proc") or a full path from root */
1688
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001689 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001690
1691 // Is this what we're looking for?
1692
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001693 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1694 strcmp(storage_path, mtcur->mnt_fsname) &&
1695 strcmp(argv[0], mtcur->mnt_dir) &&
1696 strcmp(storage_path, mtcur->mnt_dir)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001697
1698 // Remember this entry. Something later may have overmounted
1699 // it, and we want the _last_ match.
1700
1701 mtcur = mtnext;
1702
1703 // If we're mounting all.
1704
1705 } else {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001706 // Do we need to match a filesystem type?
Denis Vlasenkobf295dd2007-04-05 21:57:47 +00001707 if (fstype && match_fstype(mtcur, fstype)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001708
1709 // Skip noauto and swap anyway.
1710
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001711 if (parse_mount_options(mtcur->mnt_opts, 0)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001712 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1713
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001714 // No, mount -a won't mount anything,
1715 // even user mounts, for mere humans.
1716
1717 if (nonroot)
1718 bb_error_msg_and_die(must_be_root);
1719
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001720 // Mount this thing.
1721
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001722 // NFS mounts want this to be xrealloc-able
1723 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001724 if (singlemount(mtcur, 1)) {
1725 /* Count number of failed mounts */
1726 rc++;
1727 }
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001728 free(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001729 }
1730 }
1731 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1732
1733clean_up:
1734
1735 if (ENABLE_FEATURE_CLEAN_UP) {
1736 free(storage_path);
1737 free(cmdopts);
1738 }
1739
1740 return rc;
1741}