blob: b76c38ab1d4e2a499b5a5d857a4af66b0af613d3 [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,',');
Denis Vlasenko51742f42007-04-12 00:32:05 +0000141 if (!p) break;
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000142 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 Vlasenko87468852007-04-13 23:22:00 +0000204 static const char filesystems[2][sizeof("/proc/filesystems")] = {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000205 "/etc/filesystems",
206 "/proc/filesystems",
Denis Vlasenko372686b2006-10-12 22:42:33 +0000207 };
208 char *fs, *buf;
Rob Landleydc0955b2006-03-14 18:16:25 +0000209 llist_t *list = 0;
210 int i;
211 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000212
Denis Vlasenko87468852007-04-13 23:22:00 +0000213 for (i = 0; i < 2; i++) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000214 f = fopen(filesystems[i], "r");
215 if (!f) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000216
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000217 while ((buf = xmalloc_getline(f)) != 0) {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000218 if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
219 continue;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000220 fs = skip_whitespace(buf);
Denis Vlasenko372686b2006-10-12 22:42:33 +0000221 if (*fs=='#' || *fs=='*' || !*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000222
Denis Vlasenko372686b2006-10-12 22:42:33 +0000223 llist_add_to_end(&list, xstrdup(fs));
224 free(buf);
Rob Landleydc0955b2006-03-14 18:16:25 +0000225 }
226 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
227 }
228
229 return list;
230}
231
232llist_t *fslist = 0;
233
Rob Landleydc0955b2006-03-14 18:16:25 +0000234#if ENABLE_FEATURE_CLEAN_UP
235static void delete_block_backed_filesystems(void)
236{
Rob Landleya6b5b602006-05-08 19:03:07 +0000237 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000238}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000239#else
240void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000241#endif
242
243#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000244static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000245static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000246#else
Denis Vlasenko116080a2006-09-21 11:13:08 +0000247#define useMtab 0
248#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000249#endif
250
251// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000252// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000253static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000254{
Denis Vlasenkob1726782006-09-29 14:43:20 +0000255 int rc = 0;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000256
Denis Vlasenkob1726782006-09-29 14:43:20 +0000257 if (fakeIt) goto mtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000258
Rob Landleydc0955b2006-03-14 18:16:25 +0000259 // Mount, with fallback to read-only if necessary.
260
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000261 for (;;) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000262 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
263 vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000264 if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
Rob Landleydc0955b2006-03-14 18:16:25 +0000265 break;
266 bb_error_msg("%s is write-protected, mounting read-only",
267 mp->mnt_fsname);
268 vfsflags |= MS_RDONLY;
269 }
270
Rob Landleydc0955b2006-03-14 18:16:25 +0000271 // Abort entirely if permission denied.
272
273 if (rc && errno == EPERM)
274 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
275
276 /* If the mount was successful, and we're maintaining an old-style
277 * mtab file by hand, add the new entry to it now. */
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000278 mtab:
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000279 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000280 char *fsname;
Rob Landleydc0955b2006-03-14 18:16:25 +0000281 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
282 int i;
283
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000284 if (!mountTable) {
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000285 bb_error_msg("no %s",bb_path_mtab_file);
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000286 goto ret;
287 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000288
289 // Add vfs string flags
290
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000291 for (i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000292 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000293 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000294
295 // Remove trailing / (if any) from directory we mounted on
296
Denis Vlasenko727ef942006-09-14 13:19:19 +0000297 i = strlen(mp->mnt_dir) - 1;
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000298 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000299
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000300 // Convert to canonical pathnames as needed
Denis Vlasenko727ef942006-09-14 13:19:19 +0000301
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000302 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000303 fsname = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000304 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000305 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000306 mp->mnt_type = (char*)"bind";
Denis Vlasenko727ef942006-09-14 13:19:19 +0000307 }
Denis Vlasenko25098f72006-09-14 15:46:33 +0000308 mp->mnt_freq = mp->mnt_passno = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000309
310 // Write and close.
311
Denis Vlasenko727ef942006-09-14 13:19:19 +0000312 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000313 endmntent(mountTable);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000314 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000315 free(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000316 free(fsname);
317 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000318 }
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000319 ret:
Rob Landleydc0955b2006-03-14 18:16:25 +0000320 return rc;
321}
322
Denis Vlasenko25098f72006-09-14 15:46:33 +0000323#if ENABLE_FEATURE_MOUNT_NFS
324
325/*
326 * Linux NFS mount
327 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
328 *
329 * Licensed under GPLv2, see file LICENSE in this tarball for details.
330 *
331 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
332 * numbers to be specified on the command line.
333 *
334 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
335 * Omit the call to connect() for Linux version 1.3.11 or later.
336 *
337 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
338 * Implemented the "bg", "fg" and "retry" mount options for NFS.
339 *
340 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
341 * - added Native Language Support
342 *
343 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
344 * plus NFSv3 stuff.
345 */
346
Denis Vlasenko25098f72006-09-14 15:46:33 +0000347/* This is just a warning of a common mistake. Possibly this should be a
348 * uclibc faq entry rather than in busybox... */
Denis Vlasenko30a64cd2006-09-15 15:12:00 +0000349#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000350#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
351#endif
352
353#define MOUNTPORT 635
354#define MNTPATHLEN 1024
355#define MNTNAMLEN 255
356#define FHSIZE 32
357#define FHSIZE3 64
358
359typedef char fhandle[FHSIZE];
360
361typedef struct {
362 unsigned int fhandle3_len;
363 char *fhandle3_val;
364} fhandle3;
365
366enum mountstat3 {
367 MNT_OK = 0,
368 MNT3ERR_PERM = 1,
369 MNT3ERR_NOENT = 2,
370 MNT3ERR_IO = 5,
371 MNT3ERR_ACCES = 13,
372 MNT3ERR_NOTDIR = 20,
373 MNT3ERR_INVAL = 22,
374 MNT3ERR_NAMETOOLONG = 63,
375 MNT3ERR_NOTSUPP = 10004,
376 MNT3ERR_SERVERFAULT = 10006,
377};
378typedef enum mountstat3 mountstat3;
379
380struct fhstatus {
381 unsigned int fhs_status;
382 union {
383 fhandle fhs_fhandle;
384 } fhstatus_u;
385};
386typedef struct fhstatus fhstatus;
387
388struct mountres3_ok {
389 fhandle3 fhandle;
390 struct {
391 unsigned int auth_flavours_len;
392 char *auth_flavours_val;
393 } auth_flavours;
394};
395typedef struct mountres3_ok mountres3_ok;
396
397struct mountres3 {
398 mountstat3 fhs_status;
399 union {
400 mountres3_ok mountinfo;
401 } mountres3_u;
402};
403typedef struct mountres3 mountres3;
404
405typedef char *dirpath;
406
407typedef char *name;
408
409typedef struct mountbody *mountlist;
410
411struct mountbody {
412 name ml_hostname;
413 dirpath ml_directory;
414 mountlist ml_next;
415};
416typedef struct mountbody mountbody;
417
418typedef struct groupnode *groups;
419
420struct groupnode {
421 name gr_name;
422 groups gr_next;
423};
424typedef struct groupnode groupnode;
425
426typedef struct exportnode *exports;
427
428struct exportnode {
429 dirpath ex_dir;
430 groups ex_groups;
431 exports ex_next;
432};
433typedef struct exportnode exportnode;
434
435struct ppathcnf {
436 int pc_link_max;
437 short pc_max_canon;
438 short pc_max_input;
439 short pc_name_max;
440 short pc_path_max;
441 short pc_pipe_buf;
Denis Vlasenko28703012006-12-19 20:32:02 +0000442 uint8_t pc_vdisable;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000443 char pc_xxx;
444 short pc_mask[2];
445};
446typedef struct ppathcnf ppathcnf;
447
448#define MOUNTPROG 100005
449#define MOUNTVERS 1
450
451#define MOUNTPROC_NULL 0
452#define MOUNTPROC_MNT 1
453#define MOUNTPROC_DUMP 2
454#define MOUNTPROC_UMNT 3
455#define MOUNTPROC_UMNTALL 4
456#define MOUNTPROC_EXPORT 5
457#define MOUNTPROC_EXPORTALL 6
458
459#define MOUNTVERS_POSIX 2
460
461#define MOUNTPROC_PATHCONF 7
462
463#define MOUNT_V3 3
464
465#define MOUNTPROC3_NULL 0
466#define MOUNTPROC3_MNT 1
467#define MOUNTPROC3_DUMP 2
468#define MOUNTPROC3_UMNT 3
469#define MOUNTPROC3_UMNTALL 4
470#define MOUNTPROC3_EXPORT 5
471
472enum {
473#ifndef NFS_FHSIZE
474 NFS_FHSIZE = 32,
475#endif
476#ifndef NFS_PORT
477 NFS_PORT = 2049
478#endif
479};
480
Denis Vlasenko25098f72006-09-14 15:46:33 +0000481/*
482 * We want to be able to compile mount on old kernels in such a way
483 * that the binary will work well on more recent kernels.
484 * Thus, if necessary we teach nfsmount.c the structure of new fields
485 * that will come later.
486 *
487 * Moreover, the new kernel includes conflict with glibc includes
488 * so it is easiest to ignore the kernel altogether (at compile time).
489 */
490
491struct nfs2_fh {
492 char data[32];
493};
494struct nfs3_fh {
495 unsigned short size;
496 unsigned char data[64];
497};
498
499struct nfs_mount_data {
500 int version; /* 1 */
501 int fd; /* 1 */
502 struct nfs2_fh old_root; /* 1 */
503 int flags; /* 1 */
504 int rsize; /* 1 */
505 int wsize; /* 1 */
506 int timeo; /* 1 */
507 int retrans; /* 1 */
508 int acregmin; /* 1 */
509 int acregmax; /* 1 */
510 int acdirmin; /* 1 */
511 int acdirmax; /* 1 */
512 struct sockaddr_in addr; /* 1 */
513 char hostname[256]; /* 1 */
514 int namlen; /* 2 */
515 unsigned int bsize; /* 3 */
516 struct nfs3_fh root; /* 4 */
517};
518
519/* bits in the flags field */
520enum {
521 NFS_MOUNT_SOFT = 0x0001, /* 1 */
522 NFS_MOUNT_INTR = 0x0002, /* 1 */
523 NFS_MOUNT_SECURE = 0x0004, /* 1 */
524 NFS_MOUNT_POSIX = 0x0008, /* 1 */
525 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
526 NFS_MOUNT_NOAC = 0x0020, /* 1 */
527 NFS_MOUNT_TCP = 0x0040, /* 2 */
528 NFS_MOUNT_VER3 = 0x0080, /* 3 */
529 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
530 NFS_MOUNT_NONLM = 0x0200 /* 3 */
531};
532
533
534/*
535 * We need to translate between nfs status return values and
536 * the local errno values which may not be the same.
537 *
538 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
539 * "after #include <errno.h> the symbol errno is reserved for any use,
540 * it cannot even be used as a struct tag or field name".
541 */
542
543#ifndef EDQUOT
544#define EDQUOT ENOSPC
545#endif
546
547// Convert each NFSERR_BLAH into EBLAH
548
549static const struct {
550 int stat;
551 int errnum;
552} nfs_errtbl[] = {
553 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
554 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
555 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
556 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
557};
558
559static char *nfs_strerror(int status)
560{
561 int i;
Denis Vlasenkoc0975192006-09-17 15:06:34 +0000562 static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3];
Denis Vlasenko25098f72006-09-14 15:46:33 +0000563
564 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
565 if (nfs_errtbl[i].stat == status)
566 return strerror(nfs_errtbl[i].errnum);
567 }
568 sprintf(buf, "unknown nfs status return value: %d", status);
569 return buf;
570}
571
572static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
573{
574 if (!xdr_opaque(xdrs, objp, FHSIZE))
575 return FALSE;
576 return TRUE;
577}
578
579static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
580{
581 if (!xdr_u_int(xdrs, &objp->fhs_status))
582 return FALSE;
583 switch (objp->fhs_status) {
584 case 0:
585 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
586 return FALSE;
587 break;
588 default:
589 break;
590 }
591 return TRUE;
592}
593
594static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
595{
596 if (!xdr_string(xdrs, objp, MNTPATHLEN))
597 return FALSE;
598 return TRUE;
599}
600
601static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
602{
603 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
604 return FALSE;
605 return TRUE;
606}
607
608static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
609{
610 if (!xdr_fhandle3(xdrs, &objp->fhandle))
611 return FALSE;
612 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
613 sizeof (int), (xdrproc_t) xdr_int))
614 return FALSE;
615 return TRUE;
616}
617
618static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
619{
620 if (!xdr_enum(xdrs, (enum_t *) objp))
621 return FALSE;
622 return TRUE;
623}
624
625static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
626{
627 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
628 return FALSE;
629 switch (objp->fhs_status) {
630 case MNT_OK:
631 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
632 return FALSE;
633 break;
634 default:
635 break;
636 }
637 return TRUE;
638}
639
640#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
641
642/*
643 * nfs_mount_version according to the sources seen at compile time.
644 */
645static int nfs_mount_version;
646static int kernel_version;
647
648/*
649 * Unfortunately, the kernel prints annoying console messages
650 * in case of an unexpected nfs mount version (instead of
651 * just returning some error). Therefore we'll have to try
652 * and figure out what version the kernel expects.
653 *
654 * Variables:
655 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
656 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
657 * nfs_mount_version: version this source and running kernel can handle
658 */
659static void
660find_kernel_nfs_mount_version(void)
661{
662 if (kernel_version)
663 return;
664
665 nfs_mount_version = 4; /* default */
666
667 kernel_version = get_linux_version_code();
668 if (kernel_version) {
669 if (kernel_version < KERNEL_VERSION(2,1,32))
670 nfs_mount_version = 1;
671 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
672 (kernel_version >= KERNEL_VERSION(2,3,0) &&
673 kernel_version < KERNEL_VERSION(2,3,99)))
674 nfs_mount_version = 3;
675 /* else v4 since 2.3.99pre4 */
676 }
677}
678
679static struct pmap *
680get_mountport(struct sockaddr_in *server_addr,
681 long unsigned prog,
682 long unsigned version,
683 long unsigned proto,
684 long unsigned port)
685{
686 struct pmaplist *pmap;
687 static struct pmap p = {0, 0, 0, 0};
688
689 server_addr->sin_port = PMAPPORT;
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000690/* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
691 * I understand it like "IPv6 for this is not 100% ready" */
Denis Vlasenko25098f72006-09-14 15:46:33 +0000692 pmap = pmap_getmaps(server_addr);
693
694 if (version > MAX_NFSPROT)
695 version = MAX_NFSPROT;
696 if (!prog)
697 prog = MOUNTPROG;
698 p.pm_prog = prog;
699 p.pm_vers = version;
700 p.pm_prot = proto;
701 p.pm_port = port;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000702
Denis Vlasenko25098f72006-09-14 15:46:33 +0000703 while (pmap) {
704 if (pmap->pml_map.pm_prog != prog)
705 goto next;
706 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
707 goto next;
708 if (version > 2 && pmap->pml_map.pm_vers != version)
709 goto next;
710 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
711 goto next;
712 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
713 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
714 (port && pmap->pml_map.pm_port != port))
715 goto next;
716 memcpy(&p, &pmap->pml_map, sizeof(p));
717next:
718 pmap = pmap->pml_next;
719 }
720 if (!p.pm_vers)
721 p.pm_vers = MOUNTVERS;
722 if (!p.pm_port)
723 p.pm_port = MOUNTPORT;
724 if (!p.pm_prot)
725 p.pm_prot = IPPROTO_TCP;
726 return &p;
727}
728
729static int daemonize(void)
730{
731 int fd;
732 int pid = fork();
733 if (pid < 0) /* error */
734 return -errno;
735 if (pid > 0) /* parent */
736 return 0;
737 /* child */
738 fd = xopen(bb_dev_null, O_RDWR);
739 dup2(fd, 0);
740 dup2(fd, 1);
741 dup2(fd, 2);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000742 while (fd > 2) close(fd--);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000743 setsid();
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000744 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000745 logmode = LOGMODE_SYSLOG;
746 return 1;
747}
748
749// TODO
750static inline int we_saw_this_host_before(const char *hostname)
751{
752 return 0;
753}
754
755/* RPC strerror analogs are terminally idiotic:
756 * *mandatory* prefix and \n at end.
757 * This hopefully helps. Usage:
758 * error_msg_rpc(clnt_*error*(" ")) */
759static void error_msg_rpc(const char *msg)
760{
Denis Vlasenko23514fe2006-09-19 14:07:52 +0000761 int len;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000762 while (msg[0] == ' ' || msg[0] == ':') msg++;
763 len = strlen(msg);
764 while (len && msg[len-1] == '\n') len--;
765 bb_error_msg("%.*s", len, msg);
766}
767
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +0000768// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000769static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
770{
771 CLIENT *mclient;
772 char *hostname;
773 char *pathname;
774 char *mounthost;
775 struct nfs_mount_data data;
776 char *opt;
777 struct hostent *hp;
778 struct sockaddr_in server_addr;
779 struct sockaddr_in mount_server_addr;
780 int msock, fsock;
781 union {
782 struct fhstatus nfsv2;
783 struct mountres3 nfsv3;
784 } status;
785 int daemonized;
786 char *s;
787 int port;
788 int mountport;
789 int proto;
790 int bg;
791 int soft;
792 int intr;
793 int posix;
794 int nocto;
795 int noac;
796 int nolock;
797 int retry;
798 int tcp;
799 int mountprog;
800 int mountvers;
801 int nfsprog;
802 int nfsvers;
803 int retval;
804
805 find_kernel_nfs_mount_version();
806
807 daemonized = 0;
808 mounthost = NULL;
809 retval = ETIMEDOUT;
810 msock = fsock = -1;
811 mclient = NULL;
812
813 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
814
815 filteropts = xstrdup(filteropts); /* going to trash it later... */
816
817 hostname = xstrdup(mp->mnt_fsname);
818 /* mount_main() guarantees that ':' is there */
819 s = strchr(hostname, ':');
820 pathname = s + 1;
821 *s = '\0';
822 /* Ignore all but first hostname in replicated mounts
823 until they can be fully supported. (mack@sgi.com) */
824 s = strchr(hostname, ',');
825 if (s) {
826 *s = '\0';
827 bb_error_msg("warning: multiple hostnames not supported");
828 }
829
830 server_addr.sin_family = AF_INET;
831 if (!inet_aton(hostname, &server_addr.sin_addr)) {
832 hp = gethostbyname(hostname);
833 if (hp == NULL) {
834 bb_herror_msg("%s", hostname);
835 goto fail;
836 }
837 if (hp->h_length > sizeof(struct in_addr)) {
838 bb_error_msg("got bad hp->h_length");
839 hp->h_length = sizeof(struct in_addr);
840 }
841 memcpy(&server_addr.sin_addr,
842 hp->h_addr, hp->h_length);
843 }
844
845 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
846
847 /* add IP address to mtab options for use when unmounting */
848
849 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
850 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
851 } else {
852 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
853 mp->mnt_opts[0] ? "," : "",
854 inet_ntoa(server_addr.sin_addr));
855 free(mp->mnt_opts);
856 mp->mnt_opts = tmp;
857 }
858
859 /* Set default options.
860 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
861 * let the kernel decide.
862 * timeo is filled in after we know whether it'll be TCP or UDP. */
863 memset(&data, 0, sizeof(data));
864 data.retrans = 3;
865 data.acregmin = 3;
866 data.acregmax = 60;
867 data.acdirmin = 30;
868 data.acdirmax = 60;
869 data.namlen = NAME_MAX;
870
871 bg = 0;
872 soft = 0;
873 intr = 0;
874 posix = 0;
875 nocto = 0;
876 nolock = 0;
877 noac = 0;
878 retry = 10000; /* 10000 minutes ~ 1 week */
879 tcp = 0;
880
881 mountprog = MOUNTPROG;
882 mountvers = 0;
883 port = 0;
884 mountport = 0;
885 nfsprog = 100003;
886 nfsvers = 0;
887
888 /* parse options */
Denis Vlasenko68de7202007-05-09 20:38:04 +0000889 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
Denis Vlasenko25098f72006-09-14 15:46:33 +0000890 char *opteq = strchr(opt, '=');
891 if (opteq) {
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000892 static const char *const options[] = {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000893 /* 0 */ "rsize",
894 /* 1 */ "wsize",
895 /* 2 */ "timeo",
896 /* 3 */ "retrans",
897 /* 4 */ "acregmin",
898 /* 5 */ "acregmax",
899 /* 6 */ "acdirmin",
900 /* 7 */ "acdirmax",
901 /* 8 */ "actimeo",
902 /* 9 */ "retry",
903 /* 10 */ "port",
904 /* 11 */ "mountport",
905 /* 12 */ "mounthost",
906 /* 13 */ "mountprog",
907 /* 14 */ "mountvers",
908 /* 15 */ "nfsprog",
909 /* 16 */ "nfsvers",
910 /* 17 */ "vers",
911 /* 18 */ "proto",
912 /* 19 */ "namlen",
913 /* 20 */ "addr",
914 NULL
915 };
Denis Vlasenko13858992006-10-08 12:49:22 +0000916 int val = xatoi_u(opteq + 1);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000917 *opteq = '\0';
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000918 switch (index_in_str_array(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000919 case 0: // "rsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000920 data.rsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000921 break;
922 case 1: // "wsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000923 data.wsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000924 break;
925 case 2: // "timeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000926 data.timeo = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000927 break;
928 case 3: // "retrans"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000929 data.retrans = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000930 break;
931 case 4: // "acregmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000932 data.acregmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000933 break;
934 case 5: // "acregmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000935 data.acregmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000936 break;
937 case 6: // "acdirmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000938 data.acdirmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000939 break;
940 case 7: // "acdirmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000941 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000942 break;
943 case 8: // "actimeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000944 data.acregmin = val;
945 data.acregmax = val;
946 data.acdirmin = val;
947 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000948 break;
949 case 9: // "retry"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000950 retry = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000951 break;
952 case 10: // "port"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000953 port = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000954 break;
955 case 11: // "mountport"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000956 mountport = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000957 break;
958 case 12: // "mounthost"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000959 mounthost = xstrndup(opteq+1,
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000960 strcspn(opteq+1," \t\n\r,"));
Denis Vlasenko68f21872006-10-26 01:47:34 +0000961 break;
962 case 13: // "mountprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000963 mountprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000964 break;
965 case 14: // "mountvers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000966 mountvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000967 break;
968 case 15: // "nfsprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000969 nfsprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000970 break;
971 case 16: // "nfsvers"
972 case 17: // "vers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000973 nfsvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000974 break;
975 case 18: // "proto"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000976 if (!strncmp(opteq+1, "tcp", 3))
977 tcp = 1;
978 else if (!strncmp(opteq+1, "udp", 3))
979 tcp = 0;
980 else
981 bb_error_msg("warning: unrecognized proto= option");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000982 break;
983 case 19: // "namlen"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000984 if (nfs_mount_version >= 2)
985 data.namlen = val;
986 else
987 bb_error_msg("warning: option namlen is not supported\n");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000988 break;
989 case 20: // "addr" - ignore
990 break;
991 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +0000992 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
993 goto fail;
994 }
995 }
996 else {
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000997 static const char *const options[] = {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000998 "bg",
999 "fg",
1000 "soft",
1001 "hard",
1002 "intr",
1003 "posix",
1004 "cto",
1005 "ac",
1006 "tcp",
1007 "udp",
1008 "lock",
1009 NULL
1010 };
Denis Vlasenko25098f72006-09-14 15:46:33 +00001011 int val = 1;
1012 if (!strncmp(opt, "no", 2)) {
1013 val = 0;
1014 opt += 2;
1015 }
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001016 switch (index_in_str_array(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +00001017 case 0: // "bg"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001018 bg = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001019 break;
1020 case 1: // "fg"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001021 bg = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001022 break;
1023 case 2: // "soft"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001024 soft = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001025 break;
1026 case 3: // "hard"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001027 soft = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001028 break;
1029 case 4: // "intr"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001030 intr = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001031 break;
1032 case 5: // "posix"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001033 posix = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001034 break;
1035 case 6: // "cto"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001036 nocto = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001037 break;
1038 case 7: // "ac"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001039 noac = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001040 break;
1041 case 8: // "tcp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001042 tcp = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001043 break;
1044 case 9: // "udp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001045 tcp = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001046 break;
1047 case 10: // "lock"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001048 if (nfs_mount_version >= 3)
1049 nolock = !val;
1050 else
1051 bb_error_msg("warning: option nolock is not supported");
Denis Vlasenko68f21872006-10-26 01:47:34 +00001052 break;
1053 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +00001054 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1055 goto fail;
1056 }
1057 }
1058 }
1059 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1060
1061 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1062 | (intr ? NFS_MOUNT_INTR : 0)
1063 | (posix ? NFS_MOUNT_POSIX : 0)
1064 | (nocto ? NFS_MOUNT_NOCTO : 0)
1065 | (noac ? NFS_MOUNT_NOAC : 0);
1066 if (nfs_mount_version >= 2)
1067 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1068 if (nfs_mount_version >= 3)
1069 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1070 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1071 bb_error_msg("NFSv%d not supported", nfsvers);
1072 goto fail;
1073 }
1074 if (nfsvers && !mountvers)
1075 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1076 if (nfsvers && nfsvers < mountvers) {
1077 mountvers = nfsvers;
1078 }
1079
1080 /* Adjust options if none specified */
1081 if (!data.timeo)
1082 data.timeo = tcp ? 70 : 7;
1083
Denis Vlasenko25098f72006-09-14 15:46:33 +00001084 data.version = nfs_mount_version;
1085
1086 if (vfsflags & MS_REMOUNT)
1087 goto do_mount;
1088
1089 /*
1090 * If the previous mount operation on the same host was
1091 * backgrounded, and the "bg" for this mount is also set,
1092 * give up immediately, to avoid the initial timeout.
1093 */
1094 if (bg && we_saw_this_host_before(hostname)) {
1095 daemonized = daemonize(); /* parent or error */
1096 if (daemonized <= 0) { /* parent or error */
1097 retval = -daemonized;
1098 goto ret;
1099 }
1100 }
1101
1102 /* create mount daemon client */
1103 /* See if the nfs host = mount host. */
1104 if (mounthost) {
1105 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1106 mount_server_addr.sin_family = AF_INET;
1107 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1108 } else {
1109 hp = gethostbyname(mounthost);
1110 if (hp == NULL) {
1111 bb_herror_msg("%s", mounthost);
1112 goto fail;
1113 } else {
1114 if (hp->h_length > sizeof(struct in_addr)) {
1115 bb_error_msg("got bad hp->h_length?");
1116 hp->h_length = sizeof(struct in_addr);
1117 }
1118 mount_server_addr.sin_family = AF_INET;
1119 memcpy(&mount_server_addr.sin_addr,
1120 hp->h_addr, hp->h_length);
1121 }
1122 }
1123 }
1124
1125 /*
1126 * The following loop implements the mount retries. When the mount
1127 * times out, and the "bg" option is set, we background ourself
1128 * and continue trying.
1129 *
1130 * The case where the mount point is not present and the "bg"
1131 * option is set, is treated as a timeout. This is done to
1132 * support nested mounts.
1133 *
1134 * The "retry" count specified by the user is the number of
1135 * minutes to retry before giving up.
1136 */
1137 {
1138 struct timeval total_timeout;
1139 struct timeval retry_timeout;
1140 struct pmap* pm_mnt;
1141 time_t t;
1142 time_t prevt;
1143 time_t timeout;
1144
1145 retry_timeout.tv_sec = 3;
1146 retry_timeout.tv_usec = 0;
1147 total_timeout.tv_sec = 20;
1148 total_timeout.tv_usec = 0;
1149 timeout = time(NULL) + 60 * retry;
1150 prevt = 0;
1151 t = 30;
1152retry:
1153 /* be careful not to use too many CPU cycles */
1154 if (t - prevt < 30)
1155 sleep(30);
1156
1157 pm_mnt = get_mountport(&mount_server_addr,
1158 mountprog,
1159 mountvers,
1160 proto,
1161 mountport);
1162 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
1163
1164 /* contact the mount daemon via TCP */
1165 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1166 msock = RPC_ANYSOCK;
1167
1168 switch (pm_mnt->pm_prot) {
1169 case IPPROTO_UDP:
1170 mclient = clntudp_create(&mount_server_addr,
1171 pm_mnt->pm_prog,
1172 pm_mnt->pm_vers,
1173 retry_timeout,
1174 &msock);
1175 if (mclient)
1176 break;
1177 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1178 msock = RPC_ANYSOCK;
1179 case IPPROTO_TCP:
1180 mclient = clnttcp_create(&mount_server_addr,
1181 pm_mnt->pm_prog,
1182 pm_mnt->pm_vers,
1183 &msock, 0, 0);
1184 break;
1185 default:
1186 mclient = 0;
1187 }
1188 if (!mclient) {
1189 if (!daemonized && prevt == 0)
1190 error_msg_rpc(clnt_spcreateerror(" "));
1191 } else {
1192 enum clnt_stat clnt_stat;
1193 /* try to mount hostname:pathname */
1194 mclient->cl_auth = authunix_create_default();
1195
1196 /* make pointers in xdr_mountres3 NULL so
1197 * that xdr_array allocates memory for us
1198 */
1199 memset(&status, 0, sizeof(status));
1200
1201 if (pm_mnt->pm_vers == 3)
1202 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1203 (xdrproc_t) xdr_dirpath,
1204 (caddr_t) &pathname,
1205 (xdrproc_t) xdr_mountres3,
1206 (caddr_t) &status,
1207 total_timeout);
1208 else
1209 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1210 (xdrproc_t) xdr_dirpath,
1211 (caddr_t) &pathname,
1212 (xdrproc_t) xdr_fhstatus,
1213 (caddr_t) &status,
1214 total_timeout);
1215
1216 if (clnt_stat == RPC_SUCCESS)
1217 goto prepare_kernel_data; /* we're done */
1218 if (errno != ECONNREFUSED) {
1219 error_msg_rpc(clnt_sperror(mclient, " "));
1220 goto fail; /* don't retry */
1221 }
1222 /* Connection refused */
1223 if (!daemonized && prevt == 0) /* print just once */
1224 error_msg_rpc(clnt_sperror(mclient, " "));
1225 auth_destroy(mclient->cl_auth);
1226 clnt_destroy(mclient);
1227 mclient = 0;
1228 close(msock);
1229 }
1230
1231 /* Timeout. We are going to retry... maybe */
1232
1233 if (!bg)
1234 goto fail;
1235 if (!daemonized) {
1236 daemonized = daemonize();
1237 if (daemonized <= 0) { /* parent or error */
1238 retval = -daemonized;
1239 goto ret;
1240 }
1241 }
1242 prevt = t;
1243 t = time(NULL);
1244 if (t >= timeout)
1245 /* TODO error message */
1246 goto fail;
1247
1248 goto retry;
1249 }
1250
1251prepare_kernel_data:
1252
1253 if (nfsvers == 2) {
1254 if (status.nfsv2.fhs_status != 0) {
1255 bb_error_msg("%s:%s failed, reason given by server: %s",
1256 hostname, pathname,
1257 nfs_strerror(status.nfsv2.fhs_status));
1258 goto fail;
1259 }
1260 memcpy(data.root.data,
1261 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1262 NFS_FHSIZE);
1263 data.root.size = NFS_FHSIZE;
1264 memcpy(data.old_root.data,
1265 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1266 NFS_FHSIZE);
1267 } else {
1268 fhandle3 *my_fhandle;
1269 if (status.nfsv3.fhs_status != 0) {
1270 bb_error_msg("%s:%s failed, reason given by server: %s",
1271 hostname, pathname,
1272 nfs_strerror(status.nfsv3.fhs_status));
1273 goto fail;
1274 }
1275 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1276 memset(data.old_root.data, 0, NFS_FHSIZE);
1277 memset(&data.root, 0, sizeof(data.root));
1278 data.root.size = my_fhandle->fhandle3_len;
1279 memcpy(data.root.data,
1280 (char *) my_fhandle->fhandle3_val,
1281 my_fhandle->fhandle3_len);
1282
1283 data.flags |= NFS_MOUNT_VER3;
1284 }
1285
1286 /* create nfs socket for kernel */
1287
1288 if (tcp) {
1289 if (nfs_mount_version < 3) {
1290 bb_error_msg("NFS over TCP is not supported");
1291 goto fail;
1292 }
1293 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1294 } else
1295 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1296 if (fsock < 0) {
1297 bb_perror_msg("nfs socket");
1298 goto fail;
1299 }
1300 if (bindresvport(fsock, 0) < 0) {
1301 bb_perror_msg("nfs bindresvport");
1302 goto fail;
1303 }
1304 if (port == 0) {
1305 server_addr.sin_port = PMAPPORT;
1306 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1307 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1308 if (port == 0)
1309 port = NFS_PORT;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001310 }
Denis Vlasenko25098f72006-09-14 15:46:33 +00001311 server_addr.sin_port = htons(port);
1312
1313 /* prepare data structure for kernel */
1314
1315 data.fd = fsock;
1316 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1317 strncpy(data.hostname, hostname, sizeof(data.hostname));
1318
1319 /* clean up */
1320
1321 auth_destroy(mclient->cl_auth);
1322 clnt_destroy(mclient);
1323 close(msock);
1324
1325 if (bg) {
1326 /* We must wait until mount directory is available */
1327 struct stat statbuf;
1328 int delay = 1;
1329 while (stat(mp->mnt_dir, &statbuf) == -1) {
1330 if (!daemonized) {
1331 daemonized = daemonize();
1332 if (daemonized <= 0) { /* parent or error */
1333 retval = -daemonized;
1334 goto ret;
1335 }
1336 }
1337 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1338 delay *= 2;
1339 if (delay > 30)
1340 delay = 30;
1341 }
1342 }
1343
1344do_mount: /* perform actual mount */
1345
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001346 mp->mnt_type = (char*)"nfs";
Denis Vlasenko25098f72006-09-14 15:46:33 +00001347 retval = mount_it_now(mp, vfsflags, (char*)&data);
1348 goto ret;
1349
1350fail: /* abort */
1351
1352 if (msock != -1) {
1353 if (mclient) {
1354 auth_destroy(mclient->cl_auth);
1355 clnt_destroy(mclient);
1356 }
1357 close(msock);
1358 }
1359 if (fsock != -1)
1360 close(fsock);
1361
1362ret:
1363 free(hostname);
1364 free(mounthost);
1365 free(filteropts);
1366 return retval;
1367}
1368
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001369#else /* !ENABLE_FEATURE_MOUNT_NFS */
1370
1371/* Never called. Call should be optimized out. */
1372int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1373
1374#endif /* !ENABLE_FEATURE_MOUNT_NFS */
1375
1376// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1377// type detection. Returns 0 for success, nonzero for failure.
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001378// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001379static int singlemount(struct mntent *mp, int ignore_busy)
1380{
1381 int rc = -1, vfsflags;
1382 char *loopFile = 0, *filteropts = 0;
1383 llist_t *fl = 0;
1384 struct stat st;
1385
1386 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1387
1388 // Treat fstype "auto" as unspecified.
1389
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001390 if (mp->mnt_type && strcmp(mp->mnt_type,"auto") == 0)
1391 mp->mnt_type = 0;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001392
1393 // Might this be an CIFS filesystem?
1394
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001395 if (ENABLE_FEATURE_MOUNT_CIFS
1396 && (!mp->mnt_type || strcmp(mp->mnt_type,"cifs") == 0)
1397 && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')
1398 && mp->mnt_fsname[0]==mp->mnt_fsname[1]
1399 ) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001400 len_and_sockaddr *lsa;
1401 char *ip, *dotted;
1402 char *s;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001403
1404 rc = 1;
1405 // Replace '/' with '\' and verify that unc points to "//server/share".
1406
1407 for (s = mp->mnt_fsname; *s; ++s)
1408 if (*s == '/') *s = '\\';
1409
1410 // get server IP
1411
1412 s = strrchr(mp->mnt_fsname, '\\');
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001413 if (s <= mp->mnt_fsname+1) goto report_error;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001414 *s = '\0';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001415 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001416 *s = '\\';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001417 if (!lsa) goto report_error;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001418
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001419 // insert ip=... option into string flags.
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001420
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001421 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa, lsa->len);
1422 ip = xasprintf("ip=%s", dotted);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001423 parse_mount_options(ip, &filteropts);
1424
1425 // compose new unc '\\server-ip\share'
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001426 // (s => slash after hostname)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001427
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001428 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001429
1430 // lock is required
1431 vfsflags |= MS_MANDLOCK;
1432
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001433 mp->mnt_type = (char*)"cifs";
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001434 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001435 if (ENABLE_FEATURE_CLEAN_UP) {
1436 free(mp->mnt_fsname);
1437 free(ip);
1438 free(dotted);
1439 free(lsa);
1440 }
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001441 goto report_error;
1442 }
1443
1444 // Might this be an NFS filesystem?
1445
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001446 if (ENABLE_FEATURE_MOUNT_NFS
1447 && (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs"))
1448 && strchr(mp->mnt_fsname, ':') != NULL
1449 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001450 rc = nfsmount(mp, vfsflags, filteropts);
1451 goto report_error;
1452 }
1453
1454 // Look at the file. (Not found isn't a failure for remount, or for
1455 // a synthetic filesystem like proc or sysfs.)
1456
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001457 if (!lstat(mp->mnt_fsname, &st)
1458 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1459 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001460 // Do we need to allocate a loopback device for it?
1461
1462 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1463 loopFile = bb_simplify_path(mp->mnt_fsname);
1464 mp->mnt_fsname = 0;
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001465 switch (set_loop(&(mp->mnt_fsname), loopFile, 0)) {
Denis Vlasenko13b49242006-09-17 15:04:35 +00001466 case 0:
1467 case 1:
1468 break;
1469 default:
1470 bb_error_msg( errno == EPERM || errno == EACCES
1471 ? bb_msg_perm_denied_are_you_root
1472 : "cannot setup loop device");
1473 return errno;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001474 }
1475
1476 // Autodetect bind mounts
1477
1478 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1479 vfsflags |= MS_BIND;
1480 }
1481
1482 /* If we know the fstype (or don't need to), jump straight
1483 * to the actual mount. */
1484
1485 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1486 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001487 else {
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001488 // Loop through filesystem types until mount succeeds
1489 // or we run out
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001490
1491 /* Initialize list of block backed filesystems. This has to be
1492 * done here so that during "mount -a", mounts after /proc shows up
1493 * can autodetect. */
1494
1495 if (!fslist) {
1496 fslist = get_block_backed_filesystems();
1497 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1498 atexit(delete_block_backed_filesystems);
1499 }
1500
1501 for (fl = fslist; fl; fl = fl->link) {
1502 mp->mnt_type = fl->data;
Denis Vlasenko13b49242006-09-17 15:04:35 +00001503 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001504 if (!rc) break;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001505 }
1506 }
1507
1508 // If mount failed, clean up loop file (if any).
1509
1510 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1511 del_loop(mp->mnt_fsname);
1512 if (ENABLE_FEATURE_CLEAN_UP) {
1513 free(loopFile);
1514 free(mp->mnt_fsname);
1515 }
1516 }
1517
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001518 report_error:
1519 if (ENABLE_FEATURE_CLEAN_UP)
1520 free(filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001521
1522 if (rc && errno == EBUSY && ignore_busy) rc = 0;
1523 if (rc < 0)
1524 /* perror here sometimes says "mounting ... on ... failed: Success" */
1525 bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1526
1527 return rc;
1528}
1529
1530// Parse options, if necessary parse fstab/mtab, and call singlemount for
1531// each directory to be mounted.
1532
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001533const char must_be_root[] = "you must be root";
1534
Denis Vlasenko06af2162007-02-03 17:28:39 +00001535int mount_main(int argc, char **argv);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001536int mount_main(int argc, char **argv)
1537{
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001538 enum { OPT_ALL = 0x10 };
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001539
1540 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001541 char *opt_o;
1542 const char *fstabname;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001543 FILE *fstab;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001544 int i, j, rc = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001545 unsigned opt;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001546 struct mntent mtpair[2], *mtcur = mtpair;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001547 SKIP_DESKTOP(const int nonroot = 0;)
1548 USE_DESKTOP( int nonroot = (getuid() != 0);)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001549
1550 /* parse long options, like --bind and --move. Note that -o option
1551 * and --option are synonymous. Yes, this means --remount,rw works. */
1552
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001553 for (i = j = 0; i < argc; i++) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001554 if (argv[i][0] == '-' && argv[i][1] == '-') {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001555 append_mount_options(&cmdopts, argv[i]+2);
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001556 } else argv[j++] = argv[i];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001557 }
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001558 argv[j] = 0;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001559 argc = j;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001560
1561 // Parse remaining options
1562
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001563 opt = getopt32(argc, argv, "o:t:rwanfvs", &opt_o, &fstype);
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001564 if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
1565 //if (opt & 0x2) // -t
1566 if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
1567 if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
1568 //if (opt & 0x10) // -a
Denis Vlasenkob1726782006-09-29 14:43:20 +00001569 if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
1570 if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
Denis Vlasenko546cd182006-10-02 18:52:49 +00001571 //if (opt & 0x80) // -v: verbose (ignore)
1572 //if (opt & 0x100) // -s: sloppy (ignore)
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001573 argv += optind;
1574 argc -= optind;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001575
1576 // Three or more non-option arguments? Die with a usage message.
1577
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001578 if (argc > 2) bb_show_usage();
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001579
1580 // If we have no arguments, show currently mounted filesystems
1581
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001582 if (!argc) {
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001583 if (!(opt & OPT_ALL)) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001584 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1585
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001586 if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001587
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001588 while (getmntent_r(mountTable, mtpair, bb_common_bufsiz1,
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001589 sizeof(bb_common_bufsiz1)))
1590 {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001591 // Don't show rootfs. FIXME: why??
Denis Vlasenko908d6b72006-12-18 23:07:42 +00001592 // util-linux 2.12a happily shows rootfs...
1593 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001594
1595 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1596 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1597 mtpair->mnt_dir, mtpair->mnt_type,
1598 mtpair->mnt_opts);
1599 }
1600 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1601 return EXIT_SUCCESS;
1602 }
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001603 } else storage_path = bb_simplify_path(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001604
1605 // When we have two arguments, the second is the directory and we can
1606 // skip looking at fstab entirely. We can always abspath() the directory
1607 // argument when we get it.
1608
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001609 if (argc == 2) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001610 if (nonroot)
1611 bb_error_msg_and_die(must_be_root);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001612 mtpair->mnt_fsname = argv[0];
1613 mtpair->mnt_dir = argv[1];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001614 mtpair->mnt_type = fstype;
1615 mtpair->mnt_opts = cmdopts;
1616 rc = singlemount(mtpair, 0);
1617 goto clean_up;
1618 }
1619
Denis Vlasenko546cd182006-10-02 18:52:49 +00001620 i = parse_mount_options(cmdopts, 0);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001621 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1622 bb_error_msg_and_die(must_be_root);
Denis Vlasenko546cd182006-10-02 18:52:49 +00001623
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001624 // If we have a shared subtree flag, don't worry about fstab or mtab.
Denis Vlasenko546cd182006-10-02 18:52:49 +00001625
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001626 if (ENABLE_FEATURE_MOUNT_FLAGS
1627 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1628 ) {
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001629 rc = mount("", argv[0], "", i, "");
1630 if (rc) bb_perror_msg_and_die("%s", argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001631 goto clean_up;
1632 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001633
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001634 // Open either fstab or mtab
1635
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001636 fstabname = "/etc/fstab";
1637 if (i & MS_REMOUNT) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001638 fstabname = bb_path_mtab_file;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001639 }
1640 fstab = setmntent(fstabname, "r");
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001641 if (!fstab)
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001642 bb_perror_msg_and_die("cannot read %s", fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001643
1644 // Loop through entries until we find what we're looking for.
1645
Denis Vlasenko546cd182006-10-02 18:52:49 +00001646 memset(mtpair, 0, sizeof(mtpair));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001647 for (;;) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001648 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001649
1650 // Get next fstab entry
1651
1652 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
1653 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
1654 sizeof(bb_common_bufsiz1)/2))
1655 {
1656 // Were we looking for something specific?
1657
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001658 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001659
1660 // If we didn't find anything, complain.
1661
1662 if (!mtnext->mnt_fsname)
1663 bb_error_msg_and_die("can't find %s in %s",
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001664 argv[0], fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001665
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001666 mtcur = mtnext;
1667 if (nonroot) {
1668 // fstab must have "users" or "user"
1669 if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
1670 bb_error_msg_and_die(must_be_root);
1671 }
1672
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001673 // Mount the last thing we found.
1674
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001675 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001676 append_mount_options(&(mtcur->mnt_opts), cmdopts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001677 rc = singlemount(mtcur, 0);
1678 free(mtcur->mnt_opts);
1679 }
1680 goto clean_up;
1681 }
1682
1683 /* If we're trying to mount something specific and this isn't it,
1684 * skip it. Note we must match both the exact text in fstab (ala
1685 * "proc") or a full path from root */
1686
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001687 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001688
1689 // Is this what we're looking for?
1690
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001691 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1692 strcmp(storage_path, mtcur->mnt_fsname) &&
1693 strcmp(argv[0], mtcur->mnt_dir) &&
1694 strcmp(storage_path, mtcur->mnt_dir)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001695
1696 // Remember this entry. Something later may have overmounted
1697 // it, and we want the _last_ match.
1698
1699 mtcur = mtnext;
1700
1701 // If we're mounting all.
1702
1703 } else {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001704 // Do we need to match a filesystem type?
Denis Vlasenkobf295dd2007-04-05 21:57:47 +00001705 if (fstype && match_fstype(mtcur, fstype)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001706
1707 // Skip noauto and swap anyway.
1708
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001709 if (parse_mount_options(mtcur->mnt_opts, 0)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001710 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1711
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001712 // No, mount -a won't mount anything,
1713 // even user mounts, for mere humans.
1714
1715 if (nonroot)
1716 bb_error_msg_and_die(must_be_root);
1717
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001718 // Mount this thing.
1719
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001720 // NFS mounts want this to be xrealloc-able
1721 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001722 if (singlemount(mtcur, 1)) {
1723 /* Count number of failed mounts */
1724 rc++;
1725 }
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001726 free(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001727 }
1728 }
1729 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1730
1731clean_up:
1732
1733 if (ENABLE_FEATURE_CLEAN_UP) {
1734 free(storage_path);
1735 free(cmdopts);
1736 }
1737
1738 return rc;
1739}