blob: 3aadbf72dab0cfe1ffff5a0d556dde19631f8237 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.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 Vlasenko25098f72006-09-14 15:46:33 +0000123
Rob Landleydc0955b2006-03-14 18:16:25 +0000124/* Append mount options to string */
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000125static void append_mount_options(char **oldopts, const char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +0000126{
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000127 if (*oldopts && **oldopts) {
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000128 /* do not insert options which are already there */
129 while (newopts[0]) {
130 char *p;
131 int len = strlen(newopts);
132 p = strchr(newopts, ',');
133 if (p) len = p - newopts;
134 p = *oldopts;
135 while (1) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000136 if (!strncmp(p, newopts, len)
137 && (p[len]==',' || p[len]==0))
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000138 goto skip;
139 p = strchr(p,',');
Denis Vlasenko51742f42007-04-12 00:32:05 +0000140 if (!p) break;
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000141 p++;
142 }
143 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
144 free(*oldopts);
145 *oldopts = p;
146skip:
147 newopts += len;
148 while (newopts[0] == ',') newopts++;
149 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000150 } else {
151 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000152 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000153 }
154}
155
156/* Use the mount_options list to parse options into flags.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000157 * Also return list of unrecognized options if unrecognized!=NULL */
Rob Landleydc0955b2006-03-14 18:16:25 +0000158static int parse_mount_options(char *options, char **unrecognized)
159{
160 int flags = MS_SILENT;
161
Rob Landley6a6798b2005-08-10 20:35:54 +0000162 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000163 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000164 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000166
Rob Landleydc0955b2006-03-14 18:16:25 +0000167 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000168
Rob Landley6a6798b2005-08-10 20:35:54 +0000169 // Find this option in mount_options
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000170 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000171 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000172 long fl = mount_options[i].flags;
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000173 if (fl < 0) flags &= fl;
Rob Landleydc0955b2006-03-14 18:16:25 +0000174 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 break;
176 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000178 // If unrecognized not NULL, append unrecognized mount options */
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000179 if (unrecognized && i == ARRAY_SIZE(mount_options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000180 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000181 i = *unrecognized ? strlen(*unrecognized) : 0;
182 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000183
Rob Landley6a6798b2005-08-10 20:35:54 +0000184 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000185 if (i) (*unrecognized)[i++] = ',';
186 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000188
Rob Landley6a6798b2005-08-10 20:35:54 +0000189 // Advance to next option, or finish
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000190 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 *comma = ',';
192 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000193 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000194 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000195
Rob Landleydc0955b2006-03-14 18:16:25 +0000196 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000197}
198
Rob Landleydc0955b2006-03-14 18:16:25 +0000199// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000200
Rob Landleydc0955b2006-03-14 18:16:25 +0000201static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000202{
Denis Vlasenko87468852007-04-13 23:22:00 +0000203 static const char filesystems[2][sizeof("/proc/filesystems")] = {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000204 "/etc/filesystems",
205 "/proc/filesystems",
Denis Vlasenko372686b2006-10-12 22:42:33 +0000206 };
207 char *fs, *buf;
Rob Landleydc0955b2006-03-14 18:16:25 +0000208 llist_t *list = 0;
209 int i;
210 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000211
Denis Vlasenko87468852007-04-13 23:22:00 +0000212 for (i = 0; i < 2; i++) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000213 f = fopen(filesystems[i], "r");
214 if (!f) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000215
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000216 while ((buf = xmalloc_getline(f)) != 0) {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000217 if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
218 continue;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000219 fs = skip_whitespace(buf);
Denis Vlasenko372686b2006-10-12 22:42:33 +0000220 if (*fs=='#' || *fs=='*' || !*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000221
Denis Vlasenko372686b2006-10-12 22:42:33 +0000222 llist_add_to_end(&list, xstrdup(fs));
223 free(buf);
Rob Landleydc0955b2006-03-14 18:16:25 +0000224 }
225 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
226 }
227
228 return list;
229}
230
231llist_t *fslist = 0;
232
Rob Landleydc0955b2006-03-14 18:16:25 +0000233#if ENABLE_FEATURE_CLEAN_UP
234static void delete_block_backed_filesystems(void)
235{
Rob Landleya6b5b602006-05-08 19:03:07 +0000236 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000237}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000238#else
239void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000240#endif
241
242#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000243static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000244static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000245#else
Denis Vlasenko116080a2006-09-21 11:13:08 +0000246#define useMtab 0
247#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000248#endif
249
250// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000251// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000252static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000253{
Denis Vlasenkob1726782006-09-29 14:43:20 +0000254 int rc = 0;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000255
Denis Vlasenkob1726782006-09-29 14:43:20 +0000256 if (fakeIt) goto mtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000257
Rob Landleydc0955b2006-03-14 18:16:25 +0000258 // Mount, with fallback to read-only if necessary.
259
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000260 for (;;) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000261 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
262 vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000263 if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
Rob Landleydc0955b2006-03-14 18:16:25 +0000264 break;
265 bb_error_msg("%s is write-protected, mounting read-only",
266 mp->mnt_fsname);
267 vfsflags |= MS_RDONLY;
268 }
269
Rob Landleydc0955b2006-03-14 18:16:25 +0000270 // Abort entirely if permission denied.
271
272 if (rc && errno == EPERM)
273 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
274
275 /* If the mount was successful, and we're maintaining an old-style
276 * mtab file by hand, add the new entry to it now. */
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000277 mtab:
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000278 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000279 char *fsname;
Rob Landleydc0955b2006-03-14 18:16:25 +0000280 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
281 int i;
282
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000283 if (!mountTable) {
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000284 bb_error_msg("no %s",bb_path_mtab_file);
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000285 goto ret;
286 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000287
288 // Add vfs string flags
289
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000290 for (i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000291 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000292 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000293
294 // Remove trailing / (if any) from directory we mounted on
295
Denis Vlasenko727ef942006-09-14 13:19:19 +0000296 i = strlen(mp->mnt_dir) - 1;
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000297 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000298
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000299 // Convert to canonical pathnames as needed
Denis Vlasenko727ef942006-09-14 13:19:19 +0000300
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000301 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000302 fsname = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000303 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000304 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000305 mp->mnt_type = (char*)"bind";
Denis Vlasenko727ef942006-09-14 13:19:19 +0000306 }
Denis Vlasenko25098f72006-09-14 15:46:33 +0000307 mp->mnt_freq = mp->mnt_passno = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000308
309 // Write and close.
310
Denis Vlasenko727ef942006-09-14 13:19:19 +0000311 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000312 endmntent(mountTable);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000313 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000314 free(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000315 free(fsname);
316 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000317 }
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000318 ret:
Rob Landleydc0955b2006-03-14 18:16:25 +0000319 return rc;
320}
321
Denis Vlasenko25098f72006-09-14 15:46:33 +0000322#if ENABLE_FEATURE_MOUNT_NFS
323
324/*
325 * Linux NFS mount
326 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
327 *
328 * Licensed under GPLv2, see file LICENSE in this tarball for details.
329 *
330 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
331 * numbers to be specified on the command line.
332 *
333 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
334 * Omit the call to connect() for Linux version 1.3.11 or later.
335 *
336 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
337 * Implemented the "bg", "fg" and "retry" mount options for NFS.
338 *
339 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
340 * - added Native Language Support
341 *
342 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
343 * plus NFSv3 stuff.
344 */
345
Denis Vlasenko25098f72006-09-14 15:46:33 +0000346/* This is just a warning of a common mistake. Possibly this should be a
347 * uclibc faq entry rather than in busybox... */
Denis Vlasenko30a64cd2006-09-15 15:12:00 +0000348#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000349#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
350#endif
351
352#define MOUNTPORT 635
353#define MNTPATHLEN 1024
354#define MNTNAMLEN 255
355#define FHSIZE 32
356#define FHSIZE3 64
357
358typedef char fhandle[FHSIZE];
359
360typedef struct {
361 unsigned int fhandle3_len;
362 char *fhandle3_val;
363} fhandle3;
364
365enum mountstat3 {
366 MNT_OK = 0,
367 MNT3ERR_PERM = 1,
368 MNT3ERR_NOENT = 2,
369 MNT3ERR_IO = 5,
370 MNT3ERR_ACCES = 13,
371 MNT3ERR_NOTDIR = 20,
372 MNT3ERR_INVAL = 22,
373 MNT3ERR_NAMETOOLONG = 63,
374 MNT3ERR_NOTSUPP = 10004,
375 MNT3ERR_SERVERFAULT = 10006,
376};
377typedef enum mountstat3 mountstat3;
378
379struct fhstatus {
380 unsigned int fhs_status;
381 union {
382 fhandle fhs_fhandle;
383 } fhstatus_u;
384};
385typedef struct fhstatus fhstatus;
386
387struct mountres3_ok {
388 fhandle3 fhandle;
389 struct {
390 unsigned int auth_flavours_len;
391 char *auth_flavours_val;
392 } auth_flavours;
393};
394typedef struct mountres3_ok mountres3_ok;
395
396struct mountres3 {
397 mountstat3 fhs_status;
398 union {
399 mountres3_ok mountinfo;
400 } mountres3_u;
401};
402typedef struct mountres3 mountres3;
403
404typedef char *dirpath;
405
406typedef char *name;
407
408typedef struct mountbody *mountlist;
409
410struct mountbody {
411 name ml_hostname;
412 dirpath ml_directory;
413 mountlist ml_next;
414};
415typedef struct mountbody mountbody;
416
417typedef struct groupnode *groups;
418
419struct groupnode {
420 name gr_name;
421 groups gr_next;
422};
423typedef struct groupnode groupnode;
424
425typedef struct exportnode *exports;
426
427struct exportnode {
428 dirpath ex_dir;
429 groups ex_groups;
430 exports ex_next;
431};
432typedef struct exportnode exportnode;
433
434struct ppathcnf {
435 int pc_link_max;
436 short pc_max_canon;
437 short pc_max_input;
438 short pc_name_max;
439 short pc_path_max;
440 short pc_pipe_buf;
Denis Vlasenko28703012006-12-19 20:32:02 +0000441 uint8_t pc_vdisable;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000442 char pc_xxx;
443 short pc_mask[2];
444};
445typedef struct ppathcnf ppathcnf;
446
447#define MOUNTPROG 100005
448#define MOUNTVERS 1
449
450#define MOUNTPROC_NULL 0
451#define MOUNTPROC_MNT 1
452#define MOUNTPROC_DUMP 2
453#define MOUNTPROC_UMNT 3
454#define MOUNTPROC_UMNTALL 4
455#define MOUNTPROC_EXPORT 5
456#define MOUNTPROC_EXPORTALL 6
457
458#define MOUNTVERS_POSIX 2
459
460#define MOUNTPROC_PATHCONF 7
461
462#define MOUNT_V3 3
463
464#define MOUNTPROC3_NULL 0
465#define MOUNTPROC3_MNT 1
466#define MOUNTPROC3_DUMP 2
467#define MOUNTPROC3_UMNT 3
468#define MOUNTPROC3_UMNTALL 4
469#define MOUNTPROC3_EXPORT 5
470
471enum {
472#ifndef NFS_FHSIZE
473 NFS_FHSIZE = 32,
474#endif
475#ifndef NFS_PORT
476 NFS_PORT = 2049
477#endif
478};
479
Denis Vlasenko25098f72006-09-14 15:46:33 +0000480/*
481 * We want to be able to compile mount on old kernels in such a way
482 * that the binary will work well on more recent kernels.
483 * Thus, if necessary we teach nfsmount.c the structure of new fields
484 * that will come later.
485 *
486 * Moreover, the new kernel includes conflict with glibc includes
487 * so it is easiest to ignore the kernel altogether (at compile time).
488 */
489
490struct nfs2_fh {
491 char data[32];
492};
493struct nfs3_fh {
494 unsigned short size;
495 unsigned char data[64];
496};
497
498struct nfs_mount_data {
499 int version; /* 1 */
500 int fd; /* 1 */
501 struct nfs2_fh old_root; /* 1 */
502 int flags; /* 1 */
503 int rsize; /* 1 */
504 int wsize; /* 1 */
505 int timeo; /* 1 */
506 int retrans; /* 1 */
507 int acregmin; /* 1 */
508 int acregmax; /* 1 */
509 int acdirmin; /* 1 */
510 int acdirmax; /* 1 */
511 struct sockaddr_in addr; /* 1 */
512 char hostname[256]; /* 1 */
513 int namlen; /* 2 */
514 unsigned int bsize; /* 3 */
515 struct nfs3_fh root; /* 4 */
516};
517
518/* bits in the flags field */
519enum {
520 NFS_MOUNT_SOFT = 0x0001, /* 1 */
521 NFS_MOUNT_INTR = 0x0002, /* 1 */
522 NFS_MOUNT_SECURE = 0x0004, /* 1 */
523 NFS_MOUNT_POSIX = 0x0008, /* 1 */
524 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
525 NFS_MOUNT_NOAC = 0x0020, /* 1 */
526 NFS_MOUNT_TCP = 0x0040, /* 2 */
527 NFS_MOUNT_VER3 = 0x0080, /* 3 */
528 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
529 NFS_MOUNT_NONLM = 0x0200 /* 3 */
530};
531
532
533/*
534 * We need to translate between nfs status return values and
535 * the local errno values which may not be the same.
536 *
537 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
538 * "after #include <errno.h> the symbol errno is reserved for any use,
539 * it cannot even be used as a struct tag or field name".
540 */
541
542#ifndef EDQUOT
543#define EDQUOT ENOSPC
544#endif
545
546// Convert each NFSERR_BLAH into EBLAH
547
548static const struct {
549 int stat;
550 int errnum;
551} nfs_errtbl[] = {
552 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
553 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
554 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
555 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
556};
557
558static char *nfs_strerror(int status)
559{
560 int i;
Denis Vlasenkoc0975192006-09-17 15:06:34 +0000561 static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3];
Denis Vlasenko25098f72006-09-14 15:46:33 +0000562
563 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
564 if (nfs_errtbl[i].stat == status)
565 return strerror(nfs_errtbl[i].errnum);
566 }
567 sprintf(buf, "unknown nfs status return value: %d", status);
568 return buf;
569}
570
571static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
572{
573 if (!xdr_opaque(xdrs, objp, FHSIZE))
574 return FALSE;
575 return TRUE;
576}
577
578static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
579{
580 if (!xdr_u_int(xdrs, &objp->fhs_status))
581 return FALSE;
582 switch (objp->fhs_status) {
583 case 0:
584 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
585 return FALSE;
586 break;
587 default:
588 break;
589 }
590 return TRUE;
591}
592
593static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
594{
595 if (!xdr_string(xdrs, objp, MNTPATHLEN))
596 return FALSE;
597 return TRUE;
598}
599
600static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
601{
602 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
603 return FALSE;
604 return TRUE;
605}
606
607static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
608{
609 if (!xdr_fhandle3(xdrs, &objp->fhandle))
610 return FALSE;
611 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
612 sizeof (int), (xdrproc_t) xdr_int))
613 return FALSE;
614 return TRUE;
615}
616
617static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
618{
619 if (!xdr_enum(xdrs, (enum_t *) objp))
620 return FALSE;
621 return TRUE;
622}
623
624static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
625{
626 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
627 return FALSE;
628 switch (objp->fhs_status) {
629 case MNT_OK:
630 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
631 return FALSE;
632 break;
633 default:
634 break;
635 }
636 return TRUE;
637}
638
639#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
640
641/*
642 * nfs_mount_version according to the sources seen at compile time.
643 */
644static int nfs_mount_version;
645static int kernel_version;
646
647/*
648 * Unfortunately, the kernel prints annoying console messages
649 * in case of an unexpected nfs mount version (instead of
650 * just returning some error). Therefore we'll have to try
651 * and figure out what version the kernel expects.
652 *
653 * Variables:
654 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
655 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
656 * nfs_mount_version: version this source and running kernel can handle
657 */
658static void
659find_kernel_nfs_mount_version(void)
660{
661 if (kernel_version)
662 return;
663
664 nfs_mount_version = 4; /* default */
665
666 kernel_version = get_linux_version_code();
667 if (kernel_version) {
668 if (kernel_version < KERNEL_VERSION(2,1,32))
669 nfs_mount_version = 1;
670 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
671 (kernel_version >= KERNEL_VERSION(2,3,0) &&
672 kernel_version < KERNEL_VERSION(2,3,99)))
673 nfs_mount_version = 3;
674 /* else v4 since 2.3.99pre4 */
675 }
676}
677
678static struct pmap *
679get_mountport(struct sockaddr_in *server_addr,
680 long unsigned prog,
681 long unsigned version,
682 long unsigned proto,
683 long unsigned port)
684{
685 struct pmaplist *pmap;
686 static struct pmap p = {0, 0, 0, 0};
687
688 server_addr->sin_port = PMAPPORT;
Denis Vlasenko5870ad92007-02-04 02:39:55 +0000689/* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
690 * I understand it like "IPv6 for this is not 100% ready" */
Denis Vlasenko25098f72006-09-14 15:46:33 +0000691 pmap = pmap_getmaps(server_addr);
692
693 if (version > MAX_NFSPROT)
694 version = MAX_NFSPROT;
695 if (!prog)
696 prog = MOUNTPROG;
697 p.pm_prog = prog;
698 p.pm_vers = version;
699 p.pm_prot = proto;
700 p.pm_port = port;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000701
Denis Vlasenko25098f72006-09-14 15:46:33 +0000702 while (pmap) {
703 if (pmap->pml_map.pm_prog != prog)
704 goto next;
705 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
706 goto next;
707 if (version > 2 && pmap->pml_map.pm_vers != version)
708 goto next;
709 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
710 goto next;
711 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
712 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
713 (port && pmap->pml_map.pm_port != port))
714 goto next;
715 memcpy(&p, &pmap->pml_map, sizeof(p));
716next:
717 pmap = pmap->pml_next;
718 }
719 if (!p.pm_vers)
720 p.pm_vers = MOUNTVERS;
721 if (!p.pm_port)
722 p.pm_port = MOUNTPORT;
723 if (!p.pm_prot)
724 p.pm_prot = IPPROTO_TCP;
725 return &p;
726}
727
Denis Vlasenkof0000652007-09-04 18:30:26 +0000728#if BB_MMU
Denis Vlasenko25098f72006-09-14 15:46:33 +0000729static 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}
Denis Vlasenkof0000652007-09-04 18:30:26 +0000748#else
749static inline int daemonize(void) { return -ENOSYS; }
750#endif
Denis Vlasenko25098f72006-09-14 15:46:33 +0000751
752// TODO
753static inline int we_saw_this_host_before(const char *hostname)
754{
755 return 0;
756}
757
758/* RPC strerror analogs are terminally idiotic:
759 * *mandatory* prefix and \n at end.
760 * This hopefully helps. Usage:
761 * error_msg_rpc(clnt_*error*(" ")) */
762static void error_msg_rpc(const char *msg)
763{
Denis Vlasenko23514fe2006-09-19 14:07:52 +0000764 int len;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000765 while (msg[0] == ' ' || msg[0] == ':') msg++;
766 len = strlen(msg);
767 while (len && msg[len-1] == '\n') len--;
768 bb_error_msg("%.*s", len, msg);
769}
770
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +0000771// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000772static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
773{
774 CLIENT *mclient;
775 char *hostname;
776 char *pathname;
777 char *mounthost;
778 struct nfs_mount_data data;
779 char *opt;
780 struct hostent *hp;
781 struct sockaddr_in server_addr;
782 struct sockaddr_in mount_server_addr;
783 int msock, fsock;
784 union {
785 struct fhstatus nfsv2;
786 struct mountres3 nfsv3;
787 } status;
788 int daemonized;
789 char *s;
790 int port;
791 int mountport;
792 int proto;
Denis Vlasenkof0000652007-09-04 18:30:26 +0000793#if BB_MMU
794 int bg = 0;
795#else
796 enum { bg = 0 };
797#endif
Denis Vlasenko25098f72006-09-14 15:46:33 +0000798 int soft;
799 int intr;
800 int posix;
801 int nocto;
802 int noac;
803 int nolock;
804 int retry;
805 int tcp;
806 int mountprog;
807 int mountvers;
808 int nfsprog;
809 int nfsvers;
810 int retval;
811
812 find_kernel_nfs_mount_version();
813
814 daemonized = 0;
815 mounthost = NULL;
816 retval = ETIMEDOUT;
817 msock = fsock = -1;
818 mclient = NULL;
819
820 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
821
822 filteropts = xstrdup(filteropts); /* going to trash it later... */
823
824 hostname = xstrdup(mp->mnt_fsname);
825 /* mount_main() guarantees that ':' is there */
826 s = strchr(hostname, ':');
827 pathname = s + 1;
828 *s = '\0';
829 /* Ignore all but first hostname in replicated mounts
830 until they can be fully supported. (mack@sgi.com) */
831 s = strchr(hostname, ',');
832 if (s) {
833 *s = '\0';
834 bb_error_msg("warning: multiple hostnames not supported");
835 }
836
837 server_addr.sin_family = AF_INET;
838 if (!inet_aton(hostname, &server_addr.sin_addr)) {
839 hp = gethostbyname(hostname);
840 if (hp == NULL) {
841 bb_herror_msg("%s", hostname);
842 goto fail;
843 }
844 if (hp->h_length > sizeof(struct in_addr)) {
845 bb_error_msg("got bad hp->h_length");
846 hp->h_length = sizeof(struct in_addr);
847 }
848 memcpy(&server_addr.sin_addr,
849 hp->h_addr, hp->h_length);
850 }
851
852 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
853
854 /* add IP address to mtab options for use when unmounting */
855
856 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
857 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
858 } else {
859 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
860 mp->mnt_opts[0] ? "," : "",
861 inet_ntoa(server_addr.sin_addr));
862 free(mp->mnt_opts);
863 mp->mnt_opts = tmp;
864 }
865
866 /* Set default options.
867 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
868 * let the kernel decide.
869 * timeo is filled in after we know whether it'll be TCP or UDP. */
870 memset(&data, 0, sizeof(data));
871 data.retrans = 3;
872 data.acregmin = 3;
873 data.acregmax = 60;
874 data.acdirmin = 30;
875 data.acdirmax = 60;
876 data.namlen = NAME_MAX;
877
Denis Vlasenko25098f72006-09-14 15:46:33 +0000878 soft = 0;
879 intr = 0;
880 posix = 0;
881 nocto = 0;
882 nolock = 0;
883 noac = 0;
884 retry = 10000; /* 10000 minutes ~ 1 week */
885 tcp = 0;
886
887 mountprog = MOUNTPROG;
888 mountvers = 0;
889 port = 0;
890 mountport = 0;
891 nfsprog = 100003;
892 nfsvers = 0;
893
894 /* parse options */
Denis Vlasenko68de7202007-05-09 20:38:04 +0000895 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
Denis Vlasenko25098f72006-09-14 15:46:33 +0000896 char *opteq = strchr(opt, '=');
897 if (opteq) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000898 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000899 /* 0 */ "rsize\0"
900 /* 1 */ "wsize\0"
901 /* 2 */ "timeo\0"
902 /* 3 */ "retrans\0"
903 /* 4 */ "acregmin\0"
904 /* 5 */ "acregmax\0"
905 /* 6 */ "acdirmin\0"
906 /* 7 */ "acdirmax\0"
907 /* 8 */ "actimeo\0"
908 /* 9 */ "retry\0"
909 /* 10 */ "port\0"
910 /* 11 */ "mountport\0"
911 /* 12 */ "mounthost\0"
912 /* 13 */ "mountprog\0"
913 /* 14 */ "mountvers\0"
914 /* 15 */ "nfsprog\0"
915 /* 16 */ "nfsvers\0"
916 /* 17 */ "vers\0"
917 /* 18 */ "proto\0"
918 /* 19 */ "namlen\0"
919 /* 20 */ "addr\0";
Denis Vlasenko13858992006-10-08 12:49:22 +0000920 int val = xatoi_u(opteq + 1);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000921 *opteq = '\0';
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000922 switch (index_in_strings(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000923 case 0: // "rsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000924 data.rsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000925 break;
926 case 1: // "wsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000927 data.wsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000928 break;
929 case 2: // "timeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000930 data.timeo = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000931 break;
932 case 3: // "retrans"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000933 data.retrans = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000934 break;
935 case 4: // "acregmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000936 data.acregmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000937 break;
938 case 5: // "acregmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000939 data.acregmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000940 break;
941 case 6: // "acdirmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000942 data.acdirmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000943 break;
944 case 7: // "acdirmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000945 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000946 break;
947 case 8: // "actimeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000948 data.acregmin = val;
949 data.acregmax = val;
950 data.acdirmin = val;
951 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000952 break;
953 case 9: // "retry"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000954 retry = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000955 break;
956 case 10: // "port"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000957 port = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000958 break;
959 case 11: // "mountport"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000960 mountport = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000961 break;
962 case 12: // "mounthost"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000963 mounthost = xstrndup(opteq+1,
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000964 strcspn(opteq+1," \t\n\r,"));
Denis Vlasenko68f21872006-10-26 01:47:34 +0000965 break;
966 case 13: // "mountprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000967 mountprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000968 break;
969 case 14: // "mountvers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000970 mountvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000971 break;
972 case 15: // "nfsprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000973 nfsprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000974 break;
975 case 16: // "nfsvers"
976 case 17: // "vers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000977 nfsvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000978 break;
979 case 18: // "proto"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000980 if (!strncmp(opteq+1, "tcp", 3))
981 tcp = 1;
982 else if (!strncmp(opteq+1, "udp", 3))
983 tcp = 0;
984 else
985 bb_error_msg("warning: unrecognized proto= option");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000986 break;
987 case 19: // "namlen"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000988 if (nfs_mount_version >= 2)
989 data.namlen = val;
990 else
991 bb_error_msg("warning: option namlen is not supported\n");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000992 break;
993 case 20: // "addr" - ignore
994 break;
995 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +0000996 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
997 goto fail;
998 }
999 }
1000 else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001001 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001002 "bg\0"
1003 "fg\0"
1004 "soft\0"
1005 "hard\0"
1006 "intr\0"
1007 "posix\0"
1008 "cto\0"
1009 "ac\0"
1010 "tcp\0"
1011 "udp\0"
1012 "lock\0";
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 Vlasenko990d0f62007-07-24 15:54:42 +00001018 switch (index_in_strings(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +00001019 case 0: // "bg"
Denis Vlasenkof0000652007-09-04 18:30:26 +00001020#if BB_MMU
Denis Vlasenko25098f72006-09-14 15:46:33 +00001021 bg = val;
Denis Vlasenkof0000652007-09-04 18:30:26 +00001022#endif
Denis Vlasenko68f21872006-10-26 01:47:34 +00001023 break;
1024 case 1: // "fg"
Denis Vlasenkof0000652007-09-04 18:30:26 +00001025#if BB_MMU
Denis Vlasenko25098f72006-09-14 15:46:33 +00001026 bg = !val;
Denis Vlasenkof0000652007-09-04 18:30:26 +00001027#endif
Denis Vlasenko68f21872006-10-26 01:47:34 +00001028 break;
1029 case 2: // "soft"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001030 soft = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001031 break;
1032 case 3: // "hard"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001033 soft = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001034 break;
1035 case 4: // "intr"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001036 intr = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001037 break;
1038 case 5: // "posix"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001039 posix = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001040 break;
1041 case 6: // "cto"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001042 nocto = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001043 break;
1044 case 7: // "ac"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001045 noac = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001046 break;
1047 case 8: // "tcp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001048 tcp = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001049 break;
1050 case 9: // "udp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001051 tcp = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001052 break;
1053 case 10: // "lock"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001054 if (nfs_mount_version >= 3)
1055 nolock = !val;
1056 else
1057 bb_error_msg("warning: option nolock is not supported");
Denis Vlasenko68f21872006-10-26 01:47:34 +00001058 break;
1059 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +00001060 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1061 goto fail;
1062 }
1063 }
1064 }
1065 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1066
1067 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1068 | (intr ? NFS_MOUNT_INTR : 0)
1069 | (posix ? NFS_MOUNT_POSIX : 0)
1070 | (nocto ? NFS_MOUNT_NOCTO : 0)
1071 | (noac ? NFS_MOUNT_NOAC : 0);
1072 if (nfs_mount_version >= 2)
1073 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1074 if (nfs_mount_version >= 3)
1075 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1076 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1077 bb_error_msg("NFSv%d not supported", nfsvers);
1078 goto fail;
1079 }
1080 if (nfsvers && !mountvers)
1081 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1082 if (nfsvers && nfsvers < mountvers) {
1083 mountvers = nfsvers;
1084 }
1085
1086 /* Adjust options if none specified */
1087 if (!data.timeo)
1088 data.timeo = tcp ? 70 : 7;
1089
Denis Vlasenko25098f72006-09-14 15:46:33 +00001090 data.version = nfs_mount_version;
1091
1092 if (vfsflags & MS_REMOUNT)
1093 goto do_mount;
1094
1095 /*
1096 * If the previous mount operation on the same host was
1097 * backgrounded, and the "bg" for this mount is also set,
1098 * give up immediately, to avoid the initial timeout.
1099 */
1100 if (bg && we_saw_this_host_before(hostname)) {
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001101 daemonized = daemonize();
Denis Vlasenko25098f72006-09-14 15:46:33 +00001102 if (daemonized <= 0) { /* parent or error */
1103 retval = -daemonized;
1104 goto ret;
1105 }
1106 }
1107
1108 /* create mount daemon client */
1109 /* See if the nfs host = mount host. */
1110 if (mounthost) {
1111 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1112 mount_server_addr.sin_family = AF_INET;
1113 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1114 } else {
1115 hp = gethostbyname(mounthost);
1116 if (hp == NULL) {
1117 bb_herror_msg("%s", mounthost);
1118 goto fail;
1119 } else {
1120 if (hp->h_length > sizeof(struct in_addr)) {
1121 bb_error_msg("got bad hp->h_length?");
1122 hp->h_length = sizeof(struct in_addr);
1123 }
1124 mount_server_addr.sin_family = AF_INET;
1125 memcpy(&mount_server_addr.sin_addr,
1126 hp->h_addr, hp->h_length);
1127 }
1128 }
1129 }
1130
1131 /*
1132 * The following loop implements the mount retries. When the mount
1133 * times out, and the "bg" option is set, we background ourself
1134 * and continue trying.
1135 *
1136 * The case where the mount point is not present and the "bg"
1137 * option is set, is treated as a timeout. This is done to
1138 * support nested mounts.
1139 *
1140 * The "retry" count specified by the user is the number of
1141 * minutes to retry before giving up.
1142 */
1143 {
1144 struct timeval total_timeout;
1145 struct timeval retry_timeout;
1146 struct pmap* pm_mnt;
1147 time_t t;
1148 time_t prevt;
1149 time_t timeout;
1150
1151 retry_timeout.tv_sec = 3;
1152 retry_timeout.tv_usec = 0;
1153 total_timeout.tv_sec = 20;
1154 total_timeout.tv_usec = 0;
1155 timeout = time(NULL) + 60 * retry;
1156 prevt = 0;
1157 t = 30;
1158retry:
1159 /* be careful not to use too many CPU cycles */
1160 if (t - prevt < 30)
1161 sleep(30);
1162
1163 pm_mnt = get_mountport(&mount_server_addr,
1164 mountprog,
1165 mountvers,
1166 proto,
1167 mountport);
1168 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
1169
1170 /* contact the mount daemon via TCP */
1171 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1172 msock = RPC_ANYSOCK;
1173
1174 switch (pm_mnt->pm_prot) {
1175 case IPPROTO_UDP:
1176 mclient = clntudp_create(&mount_server_addr,
1177 pm_mnt->pm_prog,
1178 pm_mnt->pm_vers,
1179 retry_timeout,
1180 &msock);
1181 if (mclient)
1182 break;
1183 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1184 msock = RPC_ANYSOCK;
1185 case IPPROTO_TCP:
1186 mclient = clnttcp_create(&mount_server_addr,
1187 pm_mnt->pm_prog,
1188 pm_mnt->pm_vers,
1189 &msock, 0, 0);
1190 break;
1191 default:
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001192 mclient = NULL;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001193 }
1194 if (!mclient) {
1195 if (!daemonized && prevt == 0)
1196 error_msg_rpc(clnt_spcreateerror(" "));
1197 } else {
1198 enum clnt_stat clnt_stat;
1199 /* try to mount hostname:pathname */
1200 mclient->cl_auth = authunix_create_default();
1201
1202 /* make pointers in xdr_mountres3 NULL so
1203 * that xdr_array allocates memory for us
1204 */
1205 memset(&status, 0, sizeof(status));
1206
1207 if (pm_mnt->pm_vers == 3)
1208 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1209 (xdrproc_t) xdr_dirpath,
1210 (caddr_t) &pathname,
1211 (xdrproc_t) xdr_mountres3,
1212 (caddr_t) &status,
1213 total_timeout);
1214 else
1215 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1216 (xdrproc_t) xdr_dirpath,
1217 (caddr_t) &pathname,
1218 (xdrproc_t) xdr_fhstatus,
1219 (caddr_t) &status,
1220 total_timeout);
1221
1222 if (clnt_stat == RPC_SUCCESS)
1223 goto prepare_kernel_data; /* we're done */
1224 if (errno != ECONNREFUSED) {
1225 error_msg_rpc(clnt_sperror(mclient, " "));
1226 goto fail; /* don't retry */
1227 }
1228 /* Connection refused */
1229 if (!daemonized && prevt == 0) /* print just once */
1230 error_msg_rpc(clnt_sperror(mclient, " "));
1231 auth_destroy(mclient->cl_auth);
1232 clnt_destroy(mclient);
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001233 mclient = NULL;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001234 close(msock);
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001235 msock = -1;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001236 }
1237
1238 /* Timeout. We are going to retry... maybe */
1239
1240 if (!bg)
1241 goto fail;
1242 if (!daemonized) {
1243 daemonized = daemonize();
1244 if (daemonized <= 0) { /* parent or error */
1245 retval = -daemonized;
1246 goto ret;
1247 }
1248 }
1249 prevt = t;
1250 t = time(NULL);
1251 if (t >= timeout)
1252 /* TODO error message */
1253 goto fail;
1254
1255 goto retry;
1256 }
1257
1258prepare_kernel_data:
1259
1260 if (nfsvers == 2) {
1261 if (status.nfsv2.fhs_status != 0) {
1262 bb_error_msg("%s:%s failed, reason given by server: %s",
1263 hostname, pathname,
1264 nfs_strerror(status.nfsv2.fhs_status));
1265 goto fail;
1266 }
1267 memcpy(data.root.data,
1268 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1269 NFS_FHSIZE);
1270 data.root.size = NFS_FHSIZE;
1271 memcpy(data.old_root.data,
1272 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1273 NFS_FHSIZE);
1274 } else {
1275 fhandle3 *my_fhandle;
1276 if (status.nfsv3.fhs_status != 0) {
1277 bb_error_msg("%s:%s failed, reason given by server: %s",
1278 hostname, pathname,
1279 nfs_strerror(status.nfsv3.fhs_status));
1280 goto fail;
1281 }
1282 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1283 memset(data.old_root.data, 0, NFS_FHSIZE);
1284 memset(&data.root, 0, sizeof(data.root));
1285 data.root.size = my_fhandle->fhandle3_len;
1286 memcpy(data.root.data,
1287 (char *) my_fhandle->fhandle3_val,
1288 my_fhandle->fhandle3_len);
1289
1290 data.flags |= NFS_MOUNT_VER3;
1291 }
1292
1293 /* create nfs socket for kernel */
1294
1295 if (tcp) {
1296 if (nfs_mount_version < 3) {
1297 bb_error_msg("NFS over TCP is not supported");
1298 goto fail;
1299 }
1300 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1301 } else
1302 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1303 if (fsock < 0) {
1304 bb_perror_msg("nfs socket");
1305 goto fail;
1306 }
1307 if (bindresvport(fsock, 0) < 0) {
1308 bb_perror_msg("nfs bindresvport");
1309 goto fail;
1310 }
1311 if (port == 0) {
1312 server_addr.sin_port = PMAPPORT;
1313 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1314 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1315 if (port == 0)
1316 port = NFS_PORT;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001317 }
Denis Vlasenko25098f72006-09-14 15:46:33 +00001318 server_addr.sin_port = htons(port);
1319
1320 /* prepare data structure for kernel */
1321
1322 data.fd = fsock;
1323 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1324 strncpy(data.hostname, hostname, sizeof(data.hostname));
1325
1326 /* clean up */
1327
1328 auth_destroy(mclient->cl_auth);
1329 clnt_destroy(mclient);
1330 close(msock);
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001331 msock = -1;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001332
1333 if (bg) {
1334 /* We must wait until mount directory is available */
1335 struct stat statbuf;
1336 int delay = 1;
1337 while (stat(mp->mnt_dir, &statbuf) == -1) {
1338 if (!daemonized) {
1339 daemonized = daemonize();
1340 if (daemonized <= 0) { /* parent or error */
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001341 // FIXME: parent doesn't close fsock - ??!
Denis Vlasenko25098f72006-09-14 15:46:33 +00001342 retval = -daemonized;
1343 goto ret;
1344 }
1345 }
1346 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1347 delay *= 2;
1348 if (delay > 30)
1349 delay = 30;
1350 }
1351 }
1352
1353do_mount: /* perform actual mount */
1354
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001355 mp->mnt_type = (char*)"nfs";
Denis Vlasenko25098f72006-09-14 15:46:33 +00001356 retval = mount_it_now(mp, vfsflags, (char*)&data);
1357 goto ret;
1358
1359fail: /* abort */
1360
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001361 if (msock >= 0) {
Denis Vlasenko25098f72006-09-14 15:46:33 +00001362 if (mclient) {
1363 auth_destroy(mclient->cl_auth);
1364 clnt_destroy(mclient);
1365 }
1366 close(msock);
1367 }
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001368 if (fsock >= 0)
Denis Vlasenko25098f72006-09-14 15:46:33 +00001369 close(fsock);
1370
1371ret:
1372 free(hostname);
1373 free(mounthost);
1374 free(filteropts);
1375 return retval;
1376}
1377
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001378#else /* !ENABLE_FEATURE_MOUNT_NFS */
1379
1380/* Never called. Call should be optimized out. */
1381int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1382
1383#endif /* !ENABLE_FEATURE_MOUNT_NFS */
1384
1385// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1386// type detection. Returns 0 for success, nonzero for failure.
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001387// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001388static int singlemount(struct mntent *mp, int ignore_busy)
1389{
1390 int rc = -1, vfsflags;
1391 char *loopFile = 0, *filteropts = 0;
1392 llist_t *fl = 0;
1393 struct stat st;
1394
1395 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1396
1397 // Treat fstype "auto" as unspecified.
1398
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001399 if (mp->mnt_type && strcmp(mp->mnt_type,"auto") == 0)
1400 mp->mnt_type = 0;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001401
1402 // Might this be an CIFS filesystem?
1403
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001404 if (ENABLE_FEATURE_MOUNT_CIFS
1405 && (!mp->mnt_type || strcmp(mp->mnt_type,"cifs") == 0)
1406 && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')
1407 && mp->mnt_fsname[0]==mp->mnt_fsname[1]
1408 ) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001409 len_and_sockaddr *lsa;
1410 char *ip, *dotted;
1411 char *s;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001412
1413 rc = 1;
1414 // Replace '/' with '\' and verify that unc points to "//server/share".
1415
1416 for (s = mp->mnt_fsname; *s; ++s)
1417 if (*s == '/') *s = '\\';
1418
1419 // get server IP
1420
1421 s = strrchr(mp->mnt_fsname, '\\');
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001422 if (s <= mp->mnt_fsname+1) goto report_error;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001423 *s = '\0';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001424 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001425 *s = '\\';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001426 if (!lsa) goto report_error;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001427
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001428 // insert ip=... option into string flags.
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001429
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +00001430 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa);
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001431 ip = xasprintf("ip=%s", dotted);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001432 parse_mount_options(ip, &filteropts);
1433
1434 // compose new unc '\\server-ip\share'
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001435 // (s => slash after hostname)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001436
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001437 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001438
1439 // lock is required
1440 vfsflags |= MS_MANDLOCK;
1441
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001442 mp->mnt_type = (char*)"cifs";
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001443 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001444 if (ENABLE_FEATURE_CLEAN_UP) {
1445 free(mp->mnt_fsname);
1446 free(ip);
1447 free(dotted);
1448 free(lsa);
1449 }
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001450 goto report_error;
1451 }
1452
1453 // Might this be an NFS filesystem?
1454
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001455 if (ENABLE_FEATURE_MOUNT_NFS
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001456 && (!mp->mnt_type || !strcmp(mp->mnt_type, "nfs"))
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001457 && strchr(mp->mnt_fsname, ':') != NULL
1458 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001459 rc = nfsmount(mp, vfsflags, filteropts);
1460 goto report_error;
1461 }
1462
1463 // Look at the file. (Not found isn't a failure for remount, or for
1464 // a synthetic filesystem like proc or sysfs.)
Denis Vlasenko38ec1472007-05-20 12:32:41 +00001465 // (We use stat, not lstat, in order to allow
1466 // mount symlink_to_file_or_blkdev dir)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001467
Denis Vlasenko38ec1472007-05-20 12:32:41 +00001468 if (!stat(mp->mnt_fsname, &st)
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001469 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1470 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001471 // Do we need to allocate a loopback device for it?
1472
1473 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1474 loopFile = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001475 mp->mnt_fsname = NULL; /* will receive malloced loop dev name */
1476 if (set_loop(&(mp->mnt_fsname), loopFile, 0) < 0) {
1477 if (errno == EPERM || errno == EACCES)
1478 bb_error_msg(bb_msg_perm_denied_are_you_root);
1479 else
1480 bb_perror_msg("cannot setup loop device");
Denis Vlasenko13b49242006-09-17 15:04:35 +00001481 return errno;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001482 }
1483
1484 // Autodetect bind mounts
1485
1486 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1487 vfsflags |= MS_BIND;
1488 }
1489
1490 /* If we know the fstype (or don't need to), jump straight
1491 * to the actual mount. */
1492
1493 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1494 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001495 else {
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001496 // Loop through filesystem types until mount succeeds
1497 // or we run out
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001498
1499 /* Initialize list of block backed filesystems. This has to be
1500 * done here so that during "mount -a", mounts after /proc shows up
1501 * can autodetect. */
1502
1503 if (!fslist) {
1504 fslist = get_block_backed_filesystems();
1505 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1506 atexit(delete_block_backed_filesystems);
1507 }
1508
1509 for (fl = fslist; fl; fl = fl->link) {
1510 mp->mnt_type = fl->data;
Denis Vlasenko13b49242006-09-17 15:04:35 +00001511 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001512 if (!rc) break;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001513 }
1514 }
1515
1516 // If mount failed, clean up loop file (if any).
1517
1518 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1519 del_loop(mp->mnt_fsname);
1520 if (ENABLE_FEATURE_CLEAN_UP) {
1521 free(loopFile);
1522 free(mp->mnt_fsname);
1523 }
1524 }
1525
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001526 report_error:
1527 if (ENABLE_FEATURE_CLEAN_UP)
1528 free(filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001529
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001530 if (rc && errno == EBUSY && ignore_busy)
1531 rc = 0;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001532 if (rc < 0)
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001533 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001534
1535 return rc;
1536}
1537
1538// Parse options, if necessary parse fstab/mtab, and call singlemount for
1539// each directory to be mounted.
1540
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001541static const char must_be_root[] ALIGN1 = "you must be root";
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001542
Denis Vlasenko06af2162007-02-03 17:28:39 +00001543int mount_main(int argc, char **argv);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001544int mount_main(int argc, char **argv)
1545{
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001546 enum { OPT_ALL = 0x10 };
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001547
1548 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001549 char *opt_o;
1550 const char *fstabname;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001551 FILE *fstab;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001552 int i, j, rc = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001553 unsigned opt;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001554 struct mntent mtpair[2], *mtcur = mtpair;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001555 SKIP_DESKTOP(const int nonroot = 0;)
1556 USE_DESKTOP( int nonroot = (getuid() != 0);)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001557
1558 /* parse long options, like --bind and --move. Note that -o option
1559 * and --option are synonymous. Yes, this means --remount,rw works. */
1560
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001561 for (i = j = 0; i < argc; i++) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001562 if (argv[i][0] == '-' && argv[i][1] == '-') {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001563 append_mount_options(&cmdopts, argv[i]+2);
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001564 } else argv[j++] = argv[i];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001565 }
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001566 argv[j] = 0;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001567 argc = j;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001568
1569 // Parse remaining options
1570
Denis Vlasenkofb48f6c2007-08-29 11:49:41 +00001571 opt = getopt32(argv, "o:t:rwanfvsi", &opt_o, &fstype);
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001572 if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
1573 //if (opt & 0x2) // -t
1574 if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
1575 if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
1576 //if (opt & 0x10) // -a
Denis Vlasenkob1726782006-09-29 14:43:20 +00001577 if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
1578 if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
Denis Vlasenko546cd182006-10-02 18:52:49 +00001579 //if (opt & 0x80) // -v: verbose (ignore)
1580 //if (opt & 0x100) // -s: sloppy (ignore)
Denis Vlasenkofb48f6c2007-08-29 11:49:41 +00001581 //if (opt & 0x200) // -i: don't call mount.<fstype> (ignore)
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001582 argv += optind;
1583 argc -= optind;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001584
1585 // Three or more non-option arguments? Die with a usage message.
1586
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001587 if (argc > 2) bb_show_usage();
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001588
1589 // If we have no arguments, show currently mounted filesystems
1590
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001591 if (!argc) {
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001592 if (!(opt & OPT_ALL)) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001593 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1594
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001595 if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001596
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001597 while (getmntent_r(mountTable, mtpair, bb_common_bufsiz1,
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001598 sizeof(bb_common_bufsiz1)))
1599 {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001600 // Don't show rootfs. FIXME: why??
Denis Vlasenko908d6b72006-12-18 23:07:42 +00001601 // util-linux 2.12a happily shows rootfs...
1602 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001603
1604 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1605 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1606 mtpair->mnt_dir, mtpair->mnt_type,
1607 mtpair->mnt_opts);
1608 }
1609 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1610 return EXIT_SUCCESS;
1611 }
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001612 } else storage_path = bb_simplify_path(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001613
1614 // When we have two arguments, the second is the directory and we can
1615 // skip looking at fstab entirely. We can always abspath() the directory
1616 // argument when we get it.
1617
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001618 if (argc == 2) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001619 if (nonroot)
1620 bb_error_msg_and_die(must_be_root);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001621 mtpair->mnt_fsname = argv[0];
1622 mtpair->mnt_dir = argv[1];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001623 mtpair->mnt_type = fstype;
1624 mtpair->mnt_opts = cmdopts;
1625 rc = singlemount(mtpair, 0);
1626 goto clean_up;
1627 }
1628
Denis Vlasenko546cd182006-10-02 18:52:49 +00001629 i = parse_mount_options(cmdopts, 0);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001630 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1631 bb_error_msg_and_die(must_be_root);
Denis Vlasenko546cd182006-10-02 18:52:49 +00001632
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001633 // If we have a shared subtree flag, don't worry about fstab or mtab.
Denis Vlasenko546cd182006-10-02 18:52:49 +00001634
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001635 if (ENABLE_FEATURE_MOUNT_FLAGS
1636 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1637 ) {
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001638 rc = mount("", argv[0], "", i, "");
1639 if (rc) bb_perror_msg_and_die("%s", argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001640 goto clean_up;
1641 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001642
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001643 // Open either fstab or mtab
1644
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001645 fstabname = "/etc/fstab";
1646 if (i & MS_REMOUNT) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001647 fstabname = bb_path_mtab_file;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001648 }
1649 fstab = setmntent(fstabname, "r");
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001650 if (!fstab)
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001651 bb_perror_msg_and_die("cannot read %s", fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001652
1653 // Loop through entries until we find what we're looking for.
1654
Denis Vlasenko546cd182006-10-02 18:52:49 +00001655 memset(mtpair, 0, sizeof(mtpair));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001656 for (;;) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001657 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001658
1659 // Get next fstab entry
1660
1661 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
1662 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
1663 sizeof(bb_common_bufsiz1)/2))
1664 {
1665 // Were we looking for something specific?
1666
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001667 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001668
1669 // If we didn't find anything, complain.
1670
1671 if (!mtnext->mnt_fsname)
1672 bb_error_msg_and_die("can't find %s in %s",
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001673 argv[0], fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001674
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001675 mtcur = mtnext;
1676 if (nonroot) {
1677 // fstab must have "users" or "user"
1678 if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
1679 bb_error_msg_and_die(must_be_root);
1680 }
1681
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001682 // Mount the last thing we found.
1683
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001684 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001685 append_mount_options(&(mtcur->mnt_opts), cmdopts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001686 rc = singlemount(mtcur, 0);
1687 free(mtcur->mnt_opts);
1688 }
1689 goto clean_up;
1690 }
1691
1692 /* If we're trying to mount something specific and this isn't it,
1693 * skip it. Note we must match both the exact text in fstab (ala
1694 * "proc") or a full path from root */
1695
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001696 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001697
1698 // Is this what we're looking for?
1699
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001700 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1701 strcmp(storage_path, mtcur->mnt_fsname) &&
1702 strcmp(argv[0], mtcur->mnt_dir) &&
1703 strcmp(storage_path, mtcur->mnt_dir)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001704
1705 // Remember this entry. Something later may have overmounted
1706 // it, and we want the _last_ match.
1707
1708 mtcur = mtnext;
1709
1710 // If we're mounting all.
1711
1712 } else {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001713 // Do we need to match a filesystem type?
Denis Vlasenkobf295dd2007-04-05 21:57:47 +00001714 if (fstype && match_fstype(mtcur, fstype)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001715
1716 // Skip noauto and swap anyway.
1717
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001718 if (parse_mount_options(mtcur->mnt_opts, 0)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001719 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1720
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001721 // No, mount -a won't mount anything,
1722 // even user mounts, for mere humans.
1723
1724 if (nonroot)
1725 bb_error_msg_and_die(must_be_root);
1726
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001727 // Mount this thing.
1728
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001729 // NFS mounts want this to be xrealloc-able
1730 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001731 if (singlemount(mtcur, 1)) {
1732 /* Count number of failed mounts */
1733 rc++;
1734 }
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001735 free(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001736 }
1737 }
1738 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1739
1740clean_up:
1741
1742 if (ENABLE_FEATURE_CLEAN_UP) {
1743 free(storage_path);
1744 free(cmdopts);
1745 }
1746
1747 return rc;
1748}