blob: a9464baeee2f222fa4b90d8d59694eb7319e96b6 [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
Rob Landleydc0955b2006-03-14 18:16:25 +000034// Not real flags, but we want to be able to check for this.
Denis Vlasenko13c5a682006-10-16 22:39:51 +000035enum {
36 MOUNT_USERS = (1<<28)*ENABLE_DESKTOP,
37 MOUNT_NOAUTO = (1<<29),
38 MOUNT_SWAP = (1<<30),
39};
40// TODO: more "user" flag compatibility.
41// "user" option (from mount manpage):
42// Only the user that mounted a filesystem can unmount it again.
43// If any user should be able to unmount, then use users instead of user
44// in the fstab line. The owner option is similar to the user option,
45// with the restriction that the user must be the owner of the special file.
46// This may be useful e.g. for /dev/fd if a login script makes
47// the console user owner of this device.
Rob Landley3ba7bd12006-08-09 19:51:13 +000048
Rob Landleydc0955b2006-03-14 18:16:25 +000049/* Standard mount options (from -o options or --options), with corresponding
50 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Rob Landley6a6798b2005-08-10 20:35:54 +000052struct {
Rob Landleye3781b72006-08-08 01:39:49 +000053 char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000054 long flags;
Rob Landleye3781b72006-08-08 01:39:49 +000055} static mount_options[] = {
56 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
Rob Landleydc0955b2006-03-14 18:16:25 +000057
Rob Landleye3781b72006-08-08 01:39:49 +000058 USE_FEATURE_MOUNT_LOOP(
59 {"loop", 0},
60 )
Rob Landleydc0955b2006-03-14 18:16:25 +000061
Rob Landleye3781b72006-08-08 01:39:49 +000062 USE_FEATURE_MOUNT_FSTAB(
63 {"defaults", 0},
64 {"quiet", 0},
Denis Vlasenko13c5a682006-10-16 22:39:51 +000065 {"noauto", MOUNT_NOAUTO},
66 {"swap", MOUNT_SWAP},
67 USE_DESKTOP({"user", MOUNT_USERS},)
68 USE_DESKTOP({"users", MOUNT_USERS},)
Rob Landleye3781b72006-08-08 01:39:49 +000069 )
Rob Landleydc0955b2006-03-14 18:16:25 +000070
Rob Landleye3781b72006-08-08 01:39:49 +000071 USE_FEATURE_MOUNT_FLAGS(
72 // vfs flags
73 {"nosuid", MS_NOSUID},
74 {"suid", ~MS_NOSUID},
75 {"dev", ~MS_NODEV},
76 {"nodev", MS_NODEV},
77 {"exec", ~MS_NOEXEC},
78 {"noexec", MS_NOEXEC},
79 {"sync", MS_SYNCHRONOUS},
80 {"async", ~MS_SYNCHRONOUS},
81 {"atime", ~MS_NOATIME},
82 {"noatime", MS_NOATIME},
83 {"diratime", ~MS_NODIRATIME},
84 {"nodiratime", MS_NODIRATIME},
85 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +000086
Rob Landleye3781b72006-08-08 01:39:49 +000087 // action flags
Rob Landleydc0955b2006-03-14 18:16:25 +000088
Rob Landleye3781b72006-08-08 01:39:49 +000089 {"bind", MS_BIND},
90 {"move", MS_MOVE},
91 {"shared", MS_SHARED},
92 {"slave", MS_SLAVE},
93 {"private", MS_PRIVATE},
94 {"unbindable", MS_UNBINDABLE},
95 {"rshared", MS_SHARED|MS_RECURSIVE},
96 {"rslave", MS_SLAVE|MS_RECURSIVE},
97 {"rprivate", MS_SLAVE|MS_RECURSIVE},
98 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
99 )
100
101 // Always understood.
102
103 {"ro", MS_RDONLY}, // vfs flag
104 {"rw", ~MS_RDONLY}, // vfs flag
105 {"remount", MS_REMOUNT}, // action flag
Eric Andersencc8ed391999-10-05 16:24:54 +0000106};
107
Denis Vlasenko546cd182006-10-02 18:52:49 +0000108#define VECTOR_SIZE(v) (sizeof(v) / sizeof((v)[0]))
Denis Vlasenko25098f72006-09-14 15:46:33 +0000109
Rob Landleydc0955b2006-03-14 18:16:25 +0000110/* Append mount options to string */
111static void append_mount_options(char **oldopts, char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +0000112{
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000113 if (*oldopts && **oldopts) {
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000114 /* do not insert options which are already there */
115 while (newopts[0]) {
116 char *p;
117 int len = strlen(newopts);
118 p = strchr(newopts, ',');
119 if (p) len = p - newopts;
120 p = *oldopts;
121 while (1) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000122 if (!strncmp(p, newopts, len)
123 && (p[len]==',' || p[len]==0))
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000124 goto skip;
125 p = strchr(p,',');
126 if(!p) break;
127 p++;
128 }
129 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
130 free(*oldopts);
131 *oldopts = p;
132skip:
133 newopts += len;
134 while (newopts[0] == ',') newopts++;
135 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000136 } else {
137 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000138 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000139 }
140}
141
142/* Use the mount_options list to parse options into flags.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000143 * Also return list of unrecognized options if unrecognized!=NULL */
Rob Landleydc0955b2006-03-14 18:16:25 +0000144static int parse_mount_options(char *options, char **unrecognized)
145{
146 int flags = MS_SILENT;
147
Rob Landley6a6798b2005-08-10 20:35:54 +0000148 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000149 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000150 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000152
Rob Landleydc0955b2006-03-14 18:16:25 +0000153 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000154
Rob Landley6a6798b2005-08-10 20:35:54 +0000155 // Find this option in mount_options
Denis Vlasenko546cd182006-10-02 18:52:49 +0000156 for (i = 0; i < VECTOR_SIZE(mount_options); i++) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000157 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000158 long fl = mount_options[i].flags;
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000159 if (fl < 0) flags &= fl;
Rob Landleydc0955b2006-03-14 18:16:25 +0000160 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 break;
162 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000164 // If unrecognized not NULL, append unrecognized mount options */
Denis Vlasenko546cd182006-10-02 18:52:49 +0000165 if (unrecognized && i == VECTOR_SIZE(mount_options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000166 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000167 i = *unrecognized ? strlen(*unrecognized) : 0;
168 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000169
Rob Landley6a6798b2005-08-10 20:35:54 +0000170 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000171 if (i) (*unrecognized)[i++] = ',';
172 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000174
Rob Landley6a6798b2005-08-10 20:35:54 +0000175 // Advance to next option, or finish
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000176 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 *comma = ',';
178 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000179 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000180 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000181
Rob Landleydc0955b2006-03-14 18:16:25 +0000182 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000183}
184
Rob Landleydc0955b2006-03-14 18:16:25 +0000185// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000186
Rob Landleydc0955b2006-03-14 18:16:25 +0000187static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000188{
Denis Vlasenko372686b2006-10-12 22:42:33 +0000189 static const char *const filesystems[] = {
190 "/etc/filesystems",
191 "/proc/filesystems",
192 0
193 };
194 char *fs, *buf;
Rob Landleydc0955b2006-03-14 18:16:25 +0000195 llist_t *list = 0;
196 int i;
197 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000198
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000199 for (i = 0; filesystems[i]; i++) {
200 f = fopen(filesystems[i], "r");
201 if (!f) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000202
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000203 while ((buf = xmalloc_getline(f)) != 0) {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000204 if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
205 continue;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000206 fs = skip_whitespace(buf);
Denis Vlasenko372686b2006-10-12 22:42:33 +0000207 if (*fs=='#' || *fs=='*' || !*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000208
Denis Vlasenko372686b2006-10-12 22:42:33 +0000209 llist_add_to_end(&list, xstrdup(fs));
210 free(buf);
Rob Landleydc0955b2006-03-14 18:16:25 +0000211 }
212 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
213 }
214
215 return list;
216}
217
218llist_t *fslist = 0;
219
Rob Landleydc0955b2006-03-14 18:16:25 +0000220#if ENABLE_FEATURE_CLEAN_UP
221static void delete_block_backed_filesystems(void)
222{
Rob Landleya6b5b602006-05-08 19:03:07 +0000223 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000224}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000225#else
226void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000227#endif
228
229#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000230static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000231static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000232#else
Denis Vlasenko116080a2006-09-21 11:13:08 +0000233#define useMtab 0
234#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000235#endif
236
237// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000238// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000239static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000240{
Denis Vlasenkob1726782006-09-29 14:43:20 +0000241 int rc = 0;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000242
Denis Vlasenkob1726782006-09-29 14:43:20 +0000243 if (fakeIt) goto mtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000244
Rob Landleydc0955b2006-03-14 18:16:25 +0000245 // Mount, with fallback to read-only if necessary.
246
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000247 for (;;) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000248 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
249 vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000250 if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
Rob Landleydc0955b2006-03-14 18:16:25 +0000251 break;
252 bb_error_msg("%s is write-protected, mounting read-only",
253 mp->mnt_fsname);
254 vfsflags |= MS_RDONLY;
255 }
256
Rob Landleydc0955b2006-03-14 18:16:25 +0000257 // Abort entirely if permission denied.
258
259 if (rc && errno == EPERM)
260 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
261
262 /* If the mount was successful, and we're maintaining an old-style
263 * mtab file by hand, add the new entry to it now. */
Denis Vlasenkob1726782006-09-29 14:43:20 +0000264mtab:
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000265 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000266 char *fsname;
Rob Landleydc0955b2006-03-14 18:16:25 +0000267 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
268 int i;
269
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000270 if (!mountTable)
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000271 bb_error_msg("no %s",bb_path_mtab_file);
Rob Landleydc0955b2006-03-14 18:16:25 +0000272
273 // Add vfs string flags
274
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000275 for (i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000276 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000277 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000278
279 // Remove trailing / (if any) from directory we mounted on
280
Denis Vlasenko727ef942006-09-14 13:19:19 +0000281 i = strlen(mp->mnt_dir) - 1;
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000282 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000283
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000284 // Convert to canonical pathnames as needed
Denis Vlasenko727ef942006-09-14 13:19:19 +0000285
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000286 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000287 fsname = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000288 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000289 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko029c4692006-09-17 15:39:22 +0000290 mp->mnt_type = "bind";
Denis Vlasenko727ef942006-09-14 13:19:19 +0000291 }
Denis Vlasenko25098f72006-09-14 15:46:33 +0000292 mp->mnt_freq = mp->mnt_passno = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000293
294 // Write and close.
295
Denis Vlasenko727ef942006-09-14 13:19:19 +0000296 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000297 endmntent(mountTable);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000298 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000299 free(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000300 free(fsname);
301 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000302 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000303
Rob Landleydc0955b2006-03-14 18:16:25 +0000304 return rc;
305}
306
Denis Vlasenko25098f72006-09-14 15:46:33 +0000307#if ENABLE_FEATURE_MOUNT_NFS
308
309/*
310 * Linux NFS mount
311 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
312 *
313 * Licensed under GPLv2, see file LICENSE in this tarball for details.
314 *
315 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
316 * numbers to be specified on the command line.
317 *
318 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
319 * Omit the call to connect() for Linux version 1.3.11 or later.
320 *
321 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
322 * Implemented the "bg", "fg" and "retry" mount options for NFS.
323 *
324 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
325 * - added Native Language Support
326 *
327 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
328 * plus NFSv3 stuff.
329 */
330
Denis Vlasenko25098f72006-09-14 15:46:33 +0000331/* This is just a warning of a common mistake. Possibly this should be a
332 * uclibc faq entry rather than in busybox... */
Denis Vlasenko30a64cd2006-09-15 15:12:00 +0000333#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000334#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
335#endif
336
337#define MOUNTPORT 635
338#define MNTPATHLEN 1024
339#define MNTNAMLEN 255
340#define FHSIZE 32
341#define FHSIZE3 64
342
343typedef char fhandle[FHSIZE];
344
345typedef struct {
346 unsigned int fhandle3_len;
347 char *fhandle3_val;
348} fhandle3;
349
350enum mountstat3 {
351 MNT_OK = 0,
352 MNT3ERR_PERM = 1,
353 MNT3ERR_NOENT = 2,
354 MNT3ERR_IO = 5,
355 MNT3ERR_ACCES = 13,
356 MNT3ERR_NOTDIR = 20,
357 MNT3ERR_INVAL = 22,
358 MNT3ERR_NAMETOOLONG = 63,
359 MNT3ERR_NOTSUPP = 10004,
360 MNT3ERR_SERVERFAULT = 10006,
361};
362typedef enum mountstat3 mountstat3;
363
364struct fhstatus {
365 unsigned int fhs_status;
366 union {
367 fhandle fhs_fhandle;
368 } fhstatus_u;
369};
370typedef struct fhstatus fhstatus;
371
372struct mountres3_ok {
373 fhandle3 fhandle;
374 struct {
375 unsigned int auth_flavours_len;
376 char *auth_flavours_val;
377 } auth_flavours;
378};
379typedef struct mountres3_ok mountres3_ok;
380
381struct mountres3 {
382 mountstat3 fhs_status;
383 union {
384 mountres3_ok mountinfo;
385 } mountres3_u;
386};
387typedef struct mountres3 mountres3;
388
389typedef char *dirpath;
390
391typedef char *name;
392
393typedef struct mountbody *mountlist;
394
395struct mountbody {
396 name ml_hostname;
397 dirpath ml_directory;
398 mountlist ml_next;
399};
400typedef struct mountbody mountbody;
401
402typedef struct groupnode *groups;
403
404struct groupnode {
405 name gr_name;
406 groups gr_next;
407};
408typedef struct groupnode groupnode;
409
410typedef struct exportnode *exports;
411
412struct exportnode {
413 dirpath ex_dir;
414 groups ex_groups;
415 exports ex_next;
416};
417typedef struct exportnode exportnode;
418
419struct ppathcnf {
420 int pc_link_max;
421 short pc_max_canon;
422 short pc_max_input;
423 short pc_name_max;
424 short pc_path_max;
425 short pc_pipe_buf;
426 u_char pc_vdisable;
427 char pc_xxx;
428 short pc_mask[2];
429};
430typedef struct ppathcnf ppathcnf;
431
432#define MOUNTPROG 100005
433#define MOUNTVERS 1
434
435#define MOUNTPROC_NULL 0
436#define MOUNTPROC_MNT 1
437#define MOUNTPROC_DUMP 2
438#define MOUNTPROC_UMNT 3
439#define MOUNTPROC_UMNTALL 4
440#define MOUNTPROC_EXPORT 5
441#define MOUNTPROC_EXPORTALL 6
442
443#define MOUNTVERS_POSIX 2
444
445#define MOUNTPROC_PATHCONF 7
446
447#define MOUNT_V3 3
448
449#define MOUNTPROC3_NULL 0
450#define MOUNTPROC3_MNT 1
451#define MOUNTPROC3_DUMP 2
452#define MOUNTPROC3_UMNT 3
453#define MOUNTPROC3_UMNTALL 4
454#define MOUNTPROC3_EXPORT 5
455
456enum {
457#ifndef NFS_FHSIZE
458 NFS_FHSIZE = 32,
459#endif
460#ifndef NFS_PORT
461 NFS_PORT = 2049
462#endif
463};
464
Denis Vlasenko25098f72006-09-14 15:46:33 +0000465/*
466 * We want to be able to compile mount on old kernels in such a way
467 * that the binary will work well on more recent kernels.
468 * Thus, if necessary we teach nfsmount.c the structure of new fields
469 * that will come later.
470 *
471 * Moreover, the new kernel includes conflict with glibc includes
472 * so it is easiest to ignore the kernel altogether (at compile time).
473 */
474
475struct nfs2_fh {
476 char data[32];
477};
478struct nfs3_fh {
479 unsigned short size;
480 unsigned char data[64];
481};
482
483struct nfs_mount_data {
484 int version; /* 1 */
485 int fd; /* 1 */
486 struct nfs2_fh old_root; /* 1 */
487 int flags; /* 1 */
488 int rsize; /* 1 */
489 int wsize; /* 1 */
490 int timeo; /* 1 */
491 int retrans; /* 1 */
492 int acregmin; /* 1 */
493 int acregmax; /* 1 */
494 int acdirmin; /* 1 */
495 int acdirmax; /* 1 */
496 struct sockaddr_in addr; /* 1 */
497 char hostname[256]; /* 1 */
498 int namlen; /* 2 */
499 unsigned int bsize; /* 3 */
500 struct nfs3_fh root; /* 4 */
501};
502
503/* bits in the flags field */
504enum {
505 NFS_MOUNT_SOFT = 0x0001, /* 1 */
506 NFS_MOUNT_INTR = 0x0002, /* 1 */
507 NFS_MOUNT_SECURE = 0x0004, /* 1 */
508 NFS_MOUNT_POSIX = 0x0008, /* 1 */
509 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
510 NFS_MOUNT_NOAC = 0x0020, /* 1 */
511 NFS_MOUNT_TCP = 0x0040, /* 2 */
512 NFS_MOUNT_VER3 = 0x0080, /* 3 */
513 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
514 NFS_MOUNT_NONLM = 0x0200 /* 3 */
515};
516
517
518/*
519 * We need to translate between nfs status return values and
520 * the local errno values which may not be the same.
521 *
522 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
523 * "after #include <errno.h> the symbol errno is reserved for any use,
524 * it cannot even be used as a struct tag or field name".
525 */
526
527#ifndef EDQUOT
528#define EDQUOT ENOSPC
529#endif
530
531// Convert each NFSERR_BLAH into EBLAH
532
533static const struct {
534 int stat;
535 int errnum;
536} nfs_errtbl[] = {
537 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
538 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
539 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
540 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
541};
542
543static char *nfs_strerror(int status)
544{
545 int i;
Denis Vlasenkoc0975192006-09-17 15:06:34 +0000546 static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3];
Denis Vlasenko25098f72006-09-14 15:46:33 +0000547
548 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
549 if (nfs_errtbl[i].stat == status)
550 return strerror(nfs_errtbl[i].errnum);
551 }
552 sprintf(buf, "unknown nfs status return value: %d", status);
553 return buf;
554}
555
556static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
557{
558 if (!xdr_opaque(xdrs, objp, FHSIZE))
559 return FALSE;
560 return TRUE;
561}
562
563static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
564{
565 if (!xdr_u_int(xdrs, &objp->fhs_status))
566 return FALSE;
567 switch (objp->fhs_status) {
568 case 0:
569 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
570 return FALSE;
571 break;
572 default:
573 break;
574 }
575 return TRUE;
576}
577
578static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
579{
580 if (!xdr_string(xdrs, objp, MNTPATHLEN))
581 return FALSE;
582 return TRUE;
583}
584
585static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
586{
587 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
588 return FALSE;
589 return TRUE;
590}
591
592static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
593{
594 if (!xdr_fhandle3(xdrs, &objp->fhandle))
595 return FALSE;
596 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
597 sizeof (int), (xdrproc_t) xdr_int))
598 return FALSE;
599 return TRUE;
600}
601
602static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
603{
604 if (!xdr_enum(xdrs, (enum_t *) objp))
605 return FALSE;
606 return TRUE;
607}
608
609static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
610{
611 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
612 return FALSE;
613 switch (objp->fhs_status) {
614 case MNT_OK:
615 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
616 return FALSE;
617 break;
618 default:
619 break;
620 }
621 return TRUE;
622}
623
624#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
625
626/*
627 * nfs_mount_version according to the sources seen at compile time.
628 */
629static int nfs_mount_version;
630static int kernel_version;
631
632/*
633 * Unfortunately, the kernel prints annoying console messages
634 * in case of an unexpected nfs mount version (instead of
635 * just returning some error). Therefore we'll have to try
636 * and figure out what version the kernel expects.
637 *
638 * Variables:
639 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
640 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
641 * nfs_mount_version: version this source and running kernel can handle
642 */
643static void
644find_kernel_nfs_mount_version(void)
645{
646 if (kernel_version)
647 return;
648
649 nfs_mount_version = 4; /* default */
650
651 kernel_version = get_linux_version_code();
652 if (kernel_version) {
653 if (kernel_version < KERNEL_VERSION(2,1,32))
654 nfs_mount_version = 1;
655 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
656 (kernel_version >= KERNEL_VERSION(2,3,0) &&
657 kernel_version < KERNEL_VERSION(2,3,99)))
658 nfs_mount_version = 3;
659 /* else v4 since 2.3.99pre4 */
660 }
661}
662
663static struct pmap *
664get_mountport(struct sockaddr_in *server_addr,
665 long unsigned prog,
666 long unsigned version,
667 long unsigned proto,
668 long unsigned port)
669{
670 struct pmaplist *pmap;
671 static struct pmap p = {0, 0, 0, 0};
672
673 server_addr->sin_port = PMAPPORT;
674 pmap = pmap_getmaps(server_addr);
675
676 if (version > MAX_NFSPROT)
677 version = MAX_NFSPROT;
678 if (!prog)
679 prog = MOUNTPROG;
680 p.pm_prog = prog;
681 p.pm_vers = version;
682 p.pm_prot = proto;
683 p.pm_port = port;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000684
Denis Vlasenko25098f72006-09-14 15:46:33 +0000685 while (pmap) {
686 if (pmap->pml_map.pm_prog != prog)
687 goto next;
688 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
689 goto next;
690 if (version > 2 && pmap->pml_map.pm_vers != version)
691 goto next;
692 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
693 goto next;
694 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
695 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
696 (port && pmap->pml_map.pm_port != port))
697 goto next;
698 memcpy(&p, &pmap->pml_map, sizeof(p));
699next:
700 pmap = pmap->pml_next;
701 }
702 if (!p.pm_vers)
703 p.pm_vers = MOUNTVERS;
704 if (!p.pm_port)
705 p.pm_port = MOUNTPORT;
706 if (!p.pm_prot)
707 p.pm_prot = IPPROTO_TCP;
708 return &p;
709}
710
711static int daemonize(void)
712{
713 int fd;
714 int pid = fork();
715 if (pid < 0) /* error */
716 return -errno;
717 if (pid > 0) /* parent */
718 return 0;
719 /* child */
720 fd = xopen(bb_dev_null, O_RDWR);
721 dup2(fd, 0);
722 dup2(fd, 1);
723 dup2(fd, 2);
724 if (fd > 2) close(fd);
725 setsid();
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000726 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000727 logmode = LOGMODE_SYSLOG;
728 return 1;
729}
730
731// TODO
732static inline int we_saw_this_host_before(const char *hostname)
733{
734 return 0;
735}
736
737/* RPC strerror analogs are terminally idiotic:
738 * *mandatory* prefix and \n at end.
739 * This hopefully helps. Usage:
740 * error_msg_rpc(clnt_*error*(" ")) */
741static void error_msg_rpc(const char *msg)
742{
Denis Vlasenko23514fe2006-09-19 14:07:52 +0000743 int len;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000744 while (msg[0] == ' ' || msg[0] == ':') msg++;
745 len = strlen(msg);
746 while (len && msg[len-1] == '\n') len--;
747 bb_error_msg("%.*s", len, msg);
748}
749
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +0000750// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000751static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
752{
753 CLIENT *mclient;
754 char *hostname;
755 char *pathname;
756 char *mounthost;
757 struct nfs_mount_data data;
758 char *opt;
759 struct hostent *hp;
760 struct sockaddr_in server_addr;
761 struct sockaddr_in mount_server_addr;
762 int msock, fsock;
763 union {
764 struct fhstatus nfsv2;
765 struct mountres3 nfsv3;
766 } status;
767 int daemonized;
768 char *s;
769 int port;
770 int mountport;
771 int proto;
772 int bg;
773 int soft;
774 int intr;
775 int posix;
776 int nocto;
777 int noac;
778 int nolock;
779 int retry;
780 int tcp;
781 int mountprog;
782 int mountvers;
783 int nfsprog;
784 int nfsvers;
785 int retval;
786
787 find_kernel_nfs_mount_version();
788
789 daemonized = 0;
790 mounthost = NULL;
791 retval = ETIMEDOUT;
792 msock = fsock = -1;
793 mclient = NULL;
794
795 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
796
797 filteropts = xstrdup(filteropts); /* going to trash it later... */
798
799 hostname = xstrdup(mp->mnt_fsname);
800 /* mount_main() guarantees that ':' is there */
801 s = strchr(hostname, ':');
802 pathname = s + 1;
803 *s = '\0';
804 /* Ignore all but first hostname in replicated mounts
805 until they can be fully supported. (mack@sgi.com) */
806 s = strchr(hostname, ',');
807 if (s) {
808 *s = '\0';
809 bb_error_msg("warning: multiple hostnames not supported");
810 }
811
812 server_addr.sin_family = AF_INET;
813 if (!inet_aton(hostname, &server_addr.sin_addr)) {
814 hp = gethostbyname(hostname);
815 if (hp == NULL) {
816 bb_herror_msg("%s", hostname);
817 goto fail;
818 }
819 if (hp->h_length > sizeof(struct in_addr)) {
820 bb_error_msg("got bad hp->h_length");
821 hp->h_length = sizeof(struct in_addr);
822 }
823 memcpy(&server_addr.sin_addr,
824 hp->h_addr, hp->h_length);
825 }
826
827 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
828
829 /* add IP address to mtab options for use when unmounting */
830
831 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
832 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
833 } else {
834 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
835 mp->mnt_opts[0] ? "," : "",
836 inet_ntoa(server_addr.sin_addr));
837 free(mp->mnt_opts);
838 mp->mnt_opts = tmp;
839 }
840
841 /* Set default options.
842 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
843 * let the kernel decide.
844 * timeo is filled in after we know whether it'll be TCP or UDP. */
845 memset(&data, 0, sizeof(data));
846 data.retrans = 3;
847 data.acregmin = 3;
848 data.acregmax = 60;
849 data.acdirmin = 30;
850 data.acdirmax = 60;
851 data.namlen = NAME_MAX;
852
853 bg = 0;
854 soft = 0;
855 intr = 0;
856 posix = 0;
857 nocto = 0;
858 nolock = 0;
859 noac = 0;
860 retry = 10000; /* 10000 minutes ~ 1 week */
861 tcp = 0;
862
863 mountprog = MOUNTPROG;
864 mountvers = 0;
865 port = 0;
866 mountport = 0;
867 nfsprog = 100003;
868 nfsvers = 0;
869
870 /* parse options */
871
872 for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
873 char *opteq = strchr(opt, '=');
874 if (opteq) {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000875 const char *const options[] = {
876 /* 0 */ "rsize",
877 /* 1 */ "wsize",
878 /* 2 */ "timeo",
879 /* 3 */ "retrans",
880 /* 4 */ "acregmin",
881 /* 5 */ "acregmax",
882 /* 6 */ "acdirmin",
883 /* 7 */ "acdirmax",
884 /* 8 */ "actimeo",
885 /* 9 */ "retry",
886 /* 10 */ "port",
887 /* 11 */ "mountport",
888 /* 12 */ "mounthost",
889 /* 13 */ "mountprog",
890 /* 14 */ "mountvers",
891 /* 15 */ "nfsprog",
892 /* 16 */ "nfsvers",
893 /* 17 */ "vers",
894 /* 18 */ "proto",
895 /* 19 */ "namlen",
896 /* 20 */ "addr",
897 NULL
898 };
Denis Vlasenko13858992006-10-08 12:49:22 +0000899 int val = xatoi_u(opteq + 1);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000900 *opteq = '\0';
Denis Vlasenko68f21872006-10-26 01:47:34 +0000901 switch (compare_string_array(options, opt)) {
902 case 0: // "rsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000903 data.rsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000904 break;
905 case 1: // "wsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000906 data.wsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000907 break;
908 case 2: // "timeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000909 data.timeo = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000910 break;
911 case 3: // "retrans"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000912 data.retrans = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000913 break;
914 case 4: // "acregmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000915 data.acregmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000916 break;
917 case 5: // "acregmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000918 data.acregmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000919 break;
920 case 6: // "acdirmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000921 data.acdirmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000922 break;
923 case 7: // "acdirmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000924 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000925 break;
926 case 8: // "actimeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000927 data.acregmin = val;
928 data.acregmax = val;
929 data.acdirmin = val;
930 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000931 break;
932 case 9: // "retry"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000933 retry = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000934 break;
935 case 10: // "port"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000936 port = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000937 break;
938 case 11: // "mountport"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000939 mountport = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000940 break;
941 case 12: // "mounthost"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000942 mounthost = xstrndup(opteq+1,
943 strcspn(opteq+1," \t\n\r,"));
Denis Vlasenko68f21872006-10-26 01:47:34 +0000944 break;
945 case 13: // "mountprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000946 mountprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000947 break;
948 case 14: // "mountvers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000949 mountvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000950 break;
951 case 15: // "nfsprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000952 nfsprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000953 break;
954 case 16: // "nfsvers"
955 case 17: // "vers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000956 nfsvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000957 break;
958 case 18: // "proto"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000959 if (!strncmp(opteq+1, "tcp", 3))
960 tcp = 1;
961 else if (!strncmp(opteq+1, "udp", 3))
962 tcp = 0;
963 else
964 bb_error_msg("warning: unrecognized proto= option");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000965 break;
966 case 19: // "namlen"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000967 if (nfs_mount_version >= 2)
968 data.namlen = val;
969 else
970 bb_error_msg("warning: option namlen is not supported\n");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000971 break;
972 case 20: // "addr" - ignore
973 break;
974 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +0000975 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
976 goto fail;
977 }
978 }
979 else {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000980 const char *const options[] = {
981 "bg",
982 "fg",
983 "soft",
984 "hard",
985 "intr",
986 "posix",
987 "cto",
988 "ac",
989 "tcp",
990 "udp",
991 "lock",
992 NULL
993 };
Denis Vlasenko25098f72006-09-14 15:46:33 +0000994 int val = 1;
995 if (!strncmp(opt, "no", 2)) {
996 val = 0;
997 opt += 2;
998 }
Denis Vlasenko68f21872006-10-26 01:47:34 +0000999 switch (compare_string_array(options, opt)) {
1000 case 0: // "bg"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001001 bg = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001002 break;
1003 case 1: // "fg"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001004 bg = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001005 break;
1006 case 2: // "soft"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001007 soft = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001008 break;
1009 case 3: // "hard"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001010 soft = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001011 break;
1012 case 4: // "intr"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001013 intr = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001014 break;
1015 case 5: // "posix"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001016 posix = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001017 break;
1018 case 6: // "cto"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001019 nocto = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001020 break;
1021 case 7: // "ac"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001022 noac = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001023 break;
1024 case 8: // "tcp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001025 tcp = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001026 break;
1027 case 9: // "udp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001028 tcp = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001029 break;
1030 case 10: // "lock"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001031 if (nfs_mount_version >= 3)
1032 nolock = !val;
1033 else
1034 bb_error_msg("warning: option nolock is not supported");
Denis Vlasenko68f21872006-10-26 01:47:34 +00001035 break;
1036 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +00001037 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1038 goto fail;
1039 }
1040 }
1041 }
1042 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1043
1044 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1045 | (intr ? NFS_MOUNT_INTR : 0)
1046 | (posix ? NFS_MOUNT_POSIX : 0)
1047 | (nocto ? NFS_MOUNT_NOCTO : 0)
1048 | (noac ? NFS_MOUNT_NOAC : 0);
1049 if (nfs_mount_version >= 2)
1050 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1051 if (nfs_mount_version >= 3)
1052 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1053 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1054 bb_error_msg("NFSv%d not supported", nfsvers);
1055 goto fail;
1056 }
1057 if (nfsvers && !mountvers)
1058 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1059 if (nfsvers && nfsvers < mountvers) {
1060 mountvers = nfsvers;
1061 }
1062
1063 /* Adjust options if none specified */
1064 if (!data.timeo)
1065 data.timeo = tcp ? 70 : 7;
1066
Denis Vlasenko25098f72006-09-14 15:46:33 +00001067 data.version = nfs_mount_version;
1068
1069 if (vfsflags & MS_REMOUNT)
1070 goto do_mount;
1071
1072 /*
1073 * If the previous mount operation on the same host was
1074 * backgrounded, and the "bg" for this mount is also set,
1075 * give up immediately, to avoid the initial timeout.
1076 */
1077 if (bg && we_saw_this_host_before(hostname)) {
1078 daemonized = daemonize(); /* parent or error */
1079 if (daemonized <= 0) { /* parent or error */
1080 retval = -daemonized;
1081 goto ret;
1082 }
1083 }
1084
1085 /* create mount daemon client */
1086 /* See if the nfs host = mount host. */
1087 if (mounthost) {
1088 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1089 mount_server_addr.sin_family = AF_INET;
1090 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1091 } else {
1092 hp = gethostbyname(mounthost);
1093 if (hp == NULL) {
1094 bb_herror_msg("%s", mounthost);
1095 goto fail;
1096 } else {
1097 if (hp->h_length > sizeof(struct in_addr)) {
1098 bb_error_msg("got bad hp->h_length?");
1099 hp->h_length = sizeof(struct in_addr);
1100 }
1101 mount_server_addr.sin_family = AF_INET;
1102 memcpy(&mount_server_addr.sin_addr,
1103 hp->h_addr, hp->h_length);
1104 }
1105 }
1106 }
1107
1108 /*
1109 * The following loop implements the mount retries. When the mount
1110 * times out, and the "bg" option is set, we background ourself
1111 * and continue trying.
1112 *
1113 * The case where the mount point is not present and the "bg"
1114 * option is set, is treated as a timeout. This is done to
1115 * support nested mounts.
1116 *
1117 * The "retry" count specified by the user is the number of
1118 * minutes to retry before giving up.
1119 */
1120 {
1121 struct timeval total_timeout;
1122 struct timeval retry_timeout;
1123 struct pmap* pm_mnt;
1124 time_t t;
1125 time_t prevt;
1126 time_t timeout;
1127
1128 retry_timeout.tv_sec = 3;
1129 retry_timeout.tv_usec = 0;
1130 total_timeout.tv_sec = 20;
1131 total_timeout.tv_usec = 0;
1132 timeout = time(NULL) + 60 * retry;
1133 prevt = 0;
1134 t = 30;
1135retry:
1136 /* be careful not to use too many CPU cycles */
1137 if (t - prevt < 30)
1138 sleep(30);
1139
1140 pm_mnt = get_mountport(&mount_server_addr,
1141 mountprog,
1142 mountvers,
1143 proto,
1144 mountport);
1145 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
1146
1147 /* contact the mount daemon via TCP */
1148 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1149 msock = RPC_ANYSOCK;
1150
1151 switch (pm_mnt->pm_prot) {
1152 case IPPROTO_UDP:
1153 mclient = clntudp_create(&mount_server_addr,
1154 pm_mnt->pm_prog,
1155 pm_mnt->pm_vers,
1156 retry_timeout,
1157 &msock);
1158 if (mclient)
1159 break;
1160 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1161 msock = RPC_ANYSOCK;
1162 case IPPROTO_TCP:
1163 mclient = clnttcp_create(&mount_server_addr,
1164 pm_mnt->pm_prog,
1165 pm_mnt->pm_vers,
1166 &msock, 0, 0);
1167 break;
1168 default:
1169 mclient = 0;
1170 }
1171 if (!mclient) {
1172 if (!daemonized && prevt == 0)
1173 error_msg_rpc(clnt_spcreateerror(" "));
1174 } else {
1175 enum clnt_stat clnt_stat;
1176 /* try to mount hostname:pathname */
1177 mclient->cl_auth = authunix_create_default();
1178
1179 /* make pointers in xdr_mountres3 NULL so
1180 * that xdr_array allocates memory for us
1181 */
1182 memset(&status, 0, sizeof(status));
1183
1184 if (pm_mnt->pm_vers == 3)
1185 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1186 (xdrproc_t) xdr_dirpath,
1187 (caddr_t) &pathname,
1188 (xdrproc_t) xdr_mountres3,
1189 (caddr_t) &status,
1190 total_timeout);
1191 else
1192 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1193 (xdrproc_t) xdr_dirpath,
1194 (caddr_t) &pathname,
1195 (xdrproc_t) xdr_fhstatus,
1196 (caddr_t) &status,
1197 total_timeout);
1198
1199 if (clnt_stat == RPC_SUCCESS)
1200 goto prepare_kernel_data; /* we're done */
1201 if (errno != ECONNREFUSED) {
1202 error_msg_rpc(clnt_sperror(mclient, " "));
1203 goto fail; /* don't retry */
1204 }
1205 /* Connection refused */
1206 if (!daemonized && prevt == 0) /* print just once */
1207 error_msg_rpc(clnt_sperror(mclient, " "));
1208 auth_destroy(mclient->cl_auth);
1209 clnt_destroy(mclient);
1210 mclient = 0;
1211 close(msock);
1212 }
1213
1214 /* Timeout. We are going to retry... maybe */
1215
1216 if (!bg)
1217 goto fail;
1218 if (!daemonized) {
1219 daemonized = daemonize();
1220 if (daemonized <= 0) { /* parent or error */
1221 retval = -daemonized;
1222 goto ret;
1223 }
1224 }
1225 prevt = t;
1226 t = time(NULL);
1227 if (t >= timeout)
1228 /* TODO error message */
1229 goto fail;
1230
1231 goto retry;
1232 }
1233
1234prepare_kernel_data:
1235
1236 if (nfsvers == 2) {
1237 if (status.nfsv2.fhs_status != 0) {
1238 bb_error_msg("%s:%s failed, reason given by server: %s",
1239 hostname, pathname,
1240 nfs_strerror(status.nfsv2.fhs_status));
1241 goto fail;
1242 }
1243 memcpy(data.root.data,
1244 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1245 NFS_FHSIZE);
1246 data.root.size = NFS_FHSIZE;
1247 memcpy(data.old_root.data,
1248 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1249 NFS_FHSIZE);
1250 } else {
1251 fhandle3 *my_fhandle;
1252 if (status.nfsv3.fhs_status != 0) {
1253 bb_error_msg("%s:%s failed, reason given by server: %s",
1254 hostname, pathname,
1255 nfs_strerror(status.nfsv3.fhs_status));
1256 goto fail;
1257 }
1258 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1259 memset(data.old_root.data, 0, NFS_FHSIZE);
1260 memset(&data.root, 0, sizeof(data.root));
1261 data.root.size = my_fhandle->fhandle3_len;
1262 memcpy(data.root.data,
1263 (char *) my_fhandle->fhandle3_val,
1264 my_fhandle->fhandle3_len);
1265
1266 data.flags |= NFS_MOUNT_VER3;
1267 }
1268
1269 /* create nfs socket for kernel */
1270
1271 if (tcp) {
1272 if (nfs_mount_version < 3) {
1273 bb_error_msg("NFS over TCP is not supported");
1274 goto fail;
1275 }
1276 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1277 } else
1278 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1279 if (fsock < 0) {
1280 bb_perror_msg("nfs socket");
1281 goto fail;
1282 }
1283 if (bindresvport(fsock, 0) < 0) {
1284 bb_perror_msg("nfs bindresvport");
1285 goto fail;
1286 }
1287 if (port == 0) {
1288 server_addr.sin_port = PMAPPORT;
1289 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1290 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1291 if (port == 0)
1292 port = NFS_PORT;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001293 }
Denis Vlasenko25098f72006-09-14 15:46:33 +00001294 server_addr.sin_port = htons(port);
1295
1296 /* prepare data structure for kernel */
1297
1298 data.fd = fsock;
1299 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1300 strncpy(data.hostname, hostname, sizeof(data.hostname));
1301
1302 /* clean up */
1303
1304 auth_destroy(mclient->cl_auth);
1305 clnt_destroy(mclient);
1306 close(msock);
1307
1308 if (bg) {
1309 /* We must wait until mount directory is available */
1310 struct stat statbuf;
1311 int delay = 1;
1312 while (stat(mp->mnt_dir, &statbuf) == -1) {
1313 if (!daemonized) {
1314 daemonized = daemonize();
1315 if (daemonized <= 0) { /* parent or error */
1316 retval = -daemonized;
1317 goto ret;
1318 }
1319 }
1320 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1321 delay *= 2;
1322 if (delay > 30)
1323 delay = 30;
1324 }
1325 }
1326
1327do_mount: /* perform actual mount */
1328
1329 mp->mnt_type = "nfs";
1330 retval = mount_it_now(mp, vfsflags, (char*)&data);
1331 goto ret;
1332
1333fail: /* abort */
1334
1335 if (msock != -1) {
1336 if (mclient) {
1337 auth_destroy(mclient->cl_auth);
1338 clnt_destroy(mclient);
1339 }
1340 close(msock);
1341 }
1342 if (fsock != -1)
1343 close(fsock);
1344
1345ret:
1346 free(hostname);
1347 free(mounthost);
1348 free(filteropts);
1349 return retval;
1350}
1351
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001352#else /* !ENABLE_FEATURE_MOUNT_NFS */
1353
1354/* Never called. Call should be optimized out. */
1355int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1356
1357#endif /* !ENABLE_FEATURE_MOUNT_NFS */
1358
1359// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1360// type detection. Returns 0 for success, nonzero for failure.
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001361// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001362static int singlemount(struct mntent *mp, int ignore_busy)
1363{
1364 int rc = -1, vfsflags;
1365 char *loopFile = 0, *filteropts = 0;
1366 llist_t *fl = 0;
1367 struct stat st;
1368
1369 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1370
1371 // Treat fstype "auto" as unspecified.
1372
1373 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
1374
1375 // Might this be an CIFS filesystem?
1376
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001377 if (ENABLE_FEATURE_MOUNT_CIFS &&
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001378 (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) &&
1379 (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')))
1380 {
1381 struct hostent *he;
1382 char ip[32], *s;
1383
1384 rc = 1;
1385 // Replace '/' with '\' and verify that unc points to "//server/share".
1386
1387 for (s = mp->mnt_fsname; *s; ++s)
1388 if (*s == '/') *s = '\\';
1389
1390 // get server IP
1391
1392 s = strrchr(mp->mnt_fsname, '\\');
1393 if (s == mp->mnt_fsname+1) goto report_error;
1394 *s = 0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001395 he = gethostbyname(mp->mnt_fsname+2);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001396 *s = '\\';
1397 if (!he) goto report_error;
1398
1399 // Insert ip=... option into string flags. (NOTE: Add IPv6 support.)
1400
1401 sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1],
1402 he->h_addr[2], he->h_addr[3]);
1403 parse_mount_options(ip, &filteropts);
1404
1405 // compose new unc '\\server-ip\share'
1406
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001407 mp->mnt_fsname = xasprintf("\\\\%s%s", ip+3,
1408 strchr(mp->mnt_fsname+2,'\\'));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001409
1410 // lock is required
1411 vfsflags |= MS_MANDLOCK;
1412
1413 mp->mnt_type = "cifs";
1414 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001415 if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001416 goto report_error;
1417 }
1418
1419 // Might this be an NFS filesystem?
1420
1421 if (ENABLE_FEATURE_MOUNT_NFS &&
1422 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
1423 strchr(mp->mnt_fsname, ':') != NULL)
1424 {
1425 rc = nfsmount(mp, vfsflags, filteropts);
1426 goto report_error;
1427 }
1428
1429 // Look at the file. (Not found isn't a failure for remount, or for
1430 // a synthetic filesystem like proc or sysfs.)
1431
1432 if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1433 {
1434 // Do we need to allocate a loopback device for it?
1435
1436 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1437 loopFile = bb_simplify_path(mp->mnt_fsname);
1438 mp->mnt_fsname = 0;
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001439 switch (set_loop(&(mp->mnt_fsname), loopFile, 0)) {
Denis Vlasenko13b49242006-09-17 15:04:35 +00001440 case 0:
1441 case 1:
1442 break;
1443 default:
1444 bb_error_msg( errno == EPERM || errno == EACCES
1445 ? bb_msg_perm_denied_are_you_root
1446 : "cannot setup loop device");
1447 return errno;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001448 }
1449
1450 // Autodetect bind mounts
1451
1452 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1453 vfsflags |= MS_BIND;
1454 }
1455
1456 /* If we know the fstype (or don't need to), jump straight
1457 * to the actual mount. */
1458
1459 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1460 rc = mount_it_now(mp, vfsflags, filteropts);
1461
1462 // Loop through filesystem types until mount succeeds or we run out
1463
1464 else {
1465
1466 /* Initialize list of block backed filesystems. This has to be
1467 * done here so that during "mount -a", mounts after /proc shows up
1468 * can autodetect. */
1469
1470 if (!fslist) {
1471 fslist = get_block_backed_filesystems();
1472 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1473 atexit(delete_block_backed_filesystems);
1474 }
1475
1476 for (fl = fslist; fl; fl = fl->link) {
1477 mp->mnt_type = fl->data;
Denis Vlasenko13b49242006-09-17 15:04:35 +00001478 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001479 if (!rc) break;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001480 }
1481 }
1482
1483 // If mount failed, clean up loop file (if any).
1484
1485 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1486 del_loop(mp->mnt_fsname);
1487 if (ENABLE_FEATURE_CLEAN_UP) {
1488 free(loopFile);
1489 free(mp->mnt_fsname);
1490 }
1491 }
1492
1493report_error:
1494 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
1495
1496 if (rc && errno == EBUSY && ignore_busy) rc = 0;
1497 if (rc < 0)
1498 /* perror here sometimes says "mounting ... on ... failed: Success" */
1499 bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1500
1501 return rc;
1502}
1503
1504// Parse options, if necessary parse fstab/mtab, and call singlemount for
1505// each directory to be mounted.
1506
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001507const char must_be_root[] = "you must be root";
1508
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001509int mount_main(int argc, char **argv)
1510{
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001511 enum { OPT_ALL = 0x10 };
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001512
1513 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001514 char *opt_o;
1515 const char *fstabname;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001516 FILE *fstab;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001517 int i, j, rc = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001518 unsigned opt;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001519 struct mntent mtpair[2], *mtcur = mtpair;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001520 SKIP_DESKTOP(const int nonroot = 0;)
1521 USE_DESKTOP( int nonroot = (getuid() != 0);)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001522
1523 /* parse long options, like --bind and --move. Note that -o option
1524 * and --option are synonymous. Yes, this means --remount,rw works. */
1525
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001526 for (i = j = 0; i < argc; i++) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001527 if (argv[i][0] == '-' && argv[i][1] == '-') {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001528 append_mount_options(&cmdopts, argv[i]+2);
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001529 } else argv[j++] = argv[i];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001530 }
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001531 argv[j] = 0;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001532 argc = j;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001533
1534 // Parse remaining options
1535
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001536 opt = getopt32(argc, argv, "o:t:rwanfvs", &opt_o, &fstype);
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001537 if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
1538 //if (opt & 0x2) // -t
1539 if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
1540 if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
1541 //if (opt & 0x10) // -a
Denis Vlasenkob1726782006-09-29 14:43:20 +00001542 if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
1543 if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
Denis Vlasenko546cd182006-10-02 18:52:49 +00001544 //if (opt & 0x80) // -v: verbose (ignore)
1545 //if (opt & 0x100) // -s: sloppy (ignore)
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001546 argv += optind;
1547 argc -= optind;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001548
1549 // Three or more non-option arguments? Die with a usage message.
1550
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001551 if (argc > 2) bb_show_usage();
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001552
1553 // If we have no arguments, show currently mounted filesystems
1554
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001555 if (!argc) {
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001556 if (!(opt & OPT_ALL)) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001557 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1558
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001559 if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001560
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001561 while (getmntent_r(mountTable, mtpair, bb_common_bufsiz1,
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001562 sizeof(bb_common_bufsiz1)))
1563 {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001564 // Don't show rootfs. FIXME: why??
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001565 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
1566
1567 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1568 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1569 mtpair->mnt_dir, mtpair->mnt_type,
1570 mtpair->mnt_opts);
1571 }
1572 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1573 return EXIT_SUCCESS;
1574 }
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001575 } else storage_path = bb_simplify_path(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001576
1577 // When we have two arguments, the second is the directory and we can
1578 // skip looking at fstab entirely. We can always abspath() the directory
1579 // argument when we get it.
1580
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001581 if (argc == 2) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001582 if (nonroot)
1583 bb_error_msg_and_die(must_be_root);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001584 mtpair->mnt_fsname = argv[0];
1585 mtpair->mnt_dir = argv[1];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001586 mtpair->mnt_type = fstype;
1587 mtpair->mnt_opts = cmdopts;
1588 rc = singlemount(mtpair, 0);
1589 goto clean_up;
1590 }
1591
Denis Vlasenko546cd182006-10-02 18:52:49 +00001592 i = parse_mount_options(cmdopts, 0);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001593 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1594 bb_error_msg_and_die(must_be_root);
Denis Vlasenko546cd182006-10-02 18:52:49 +00001595
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001596 // If we have a shared subtree flag, don't worry about fstab or mtab.
Denis Vlasenko546cd182006-10-02 18:52:49 +00001597
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001598 if (ENABLE_FEATURE_MOUNT_FLAGS &&
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001599 (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)))
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001600 {
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001601 rc = mount("", argv[0], "", i, "");
1602 if (rc) bb_perror_msg_and_die("%s", argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001603 goto clean_up;
1604 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001605
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001606 // Open either fstab or mtab
1607
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001608 fstabname = "/etc/fstab";
1609 if (i & MS_REMOUNT) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001610 fstabname = bb_path_mtab_file;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001611 }
1612 fstab = setmntent(fstabname, "r");
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001613 if (!fstab)
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001614 bb_perror_msg_and_die("cannot read %s", fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001615
1616 // Loop through entries until we find what we're looking for.
1617
Denis Vlasenko546cd182006-10-02 18:52:49 +00001618 memset(mtpair, 0, sizeof(mtpair));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001619 for (;;) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001620 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001621
1622 // Get next fstab entry
1623
1624 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
1625 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
1626 sizeof(bb_common_bufsiz1)/2))
1627 {
1628 // Were we looking for something specific?
1629
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001630 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001631
1632 // If we didn't find anything, complain.
1633
1634 if (!mtnext->mnt_fsname)
1635 bb_error_msg_and_die("can't find %s in %s",
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001636 argv[0], fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001637
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001638 mtcur = mtnext;
1639 if (nonroot) {
1640 // fstab must have "users" or "user"
1641 if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
1642 bb_error_msg_and_die(must_be_root);
1643 }
1644
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001645 // Mount the last thing we found.
1646
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001647 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001648 append_mount_options(&(mtcur->mnt_opts), cmdopts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001649 rc = singlemount(mtcur, 0);
1650 free(mtcur->mnt_opts);
1651 }
1652 goto clean_up;
1653 }
1654
1655 /* If we're trying to mount something specific and this isn't it,
1656 * skip it. Note we must match both the exact text in fstab (ala
1657 * "proc") or a full path from root */
1658
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001659 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001660
1661 // Is this what we're looking for?
1662
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001663 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1664 strcmp(storage_path, mtcur->mnt_fsname) &&
1665 strcmp(argv[0], mtcur->mnt_dir) &&
1666 strcmp(storage_path, mtcur->mnt_dir)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001667
1668 // Remember this entry. Something later may have overmounted
1669 // it, and we want the _last_ match.
1670
1671 mtcur = mtnext;
1672
1673 // If we're mounting all.
1674
1675 } else {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001676 // Do we need to match a filesystem type?
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001677 // TODO: support "-t type1,type2"; "-t notype1,type2"
1678
1679 if (fstype && strcmp(mtcur->mnt_type, fstype)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001680
1681 // Skip noauto and swap anyway.
1682
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001683 if (parse_mount_options(mtcur->mnt_opts, 0)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001684 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1685
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001686 // No, mount -a won't mount anything,
1687 // even user mounts, for mere humans.
1688
1689 if (nonroot)
1690 bb_error_msg_and_die(must_be_root);
1691
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001692 // Mount this thing.
1693
1694 if (singlemount(mtcur, 1)) {
1695 /* Count number of failed mounts */
1696 rc++;
1697 }
1698 }
1699 }
1700 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1701
1702clean_up:
1703
1704 if (ENABLE_FEATURE_CLEAN_UP) {
1705 free(storage_path);
1706 free(cmdopts);
1707 }
1708
1709 return rc;
1710}