blob: 10ada5426577bd4214cae7cea6588fca5a1cb812 [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
Eric Andersencc8ed391999-10-05 16:24:54 +000021#include <mntent.h>
Denis Vlasenko2535f122007-09-15 13:28:30 +000022#include "libbb.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
Denis Vlasenko2535f122007-09-15 13:28:30 +000033#ifndef MS_SILENT
34#define MS_SILENT (1 << 15)
35#endif
Denis Vlasenko30a64cd2006-09-15 15:12:00 +000036
Denis Vlasenko908d6b72006-12-18 23:07:42 +000037#if defined(__dietlibc__)
38/* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
39 * dietlibc-0.30 does not have implementation of getmntent_r() */
Denis Vlasenko908d6b72006-12-18 23:07:42 +000040struct mntent *getmntent_r(FILE* stream, struct mntent* result, char* buffer, int bufsize)
41{
Denis Vlasenko908d6b72006-12-18 23:07:42 +000042 struct mntent* ment = getmntent(stream);
43 memcpy(result, ment, sizeof(struct mntent));
44 return result;
45}
46#endif
47
Denis Vlasenko2535f122007-09-15 13:28:30 +000048#define getmntent_buf bb_common_bufsiz1
49
Denis Vlasenko908d6b72006-12-18 23:07:42 +000050
Rob Landleydc0955b2006-03-14 18:16:25 +000051// Not real flags, but we want to be able to check for this.
Denis Vlasenko13c5a682006-10-16 22:39:51 +000052enum {
53 MOUNT_USERS = (1<<28)*ENABLE_DESKTOP,
54 MOUNT_NOAUTO = (1<<29),
55 MOUNT_SWAP = (1<<30),
56};
57// TODO: more "user" flag compatibility.
58// "user" option (from mount manpage):
59// Only the user that mounted a filesystem can unmount it again.
60// If any user should be able to unmount, then use users instead of user
61// in the fstab line. The owner option is similar to the user option,
62// with the restriction that the user must be the owner of the special file.
63// This may be useful e.g. for /dev/fd if a login script makes
64// the console user owner of this device.
Rob Landley3ba7bd12006-08-09 19:51:13 +000065
Rob Landleydc0955b2006-03-14 18:16:25 +000066/* Standard mount options (from -o options or --options), with corresponding
67 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000068
Denis Vlasenkob9256052007-09-28 10:29:17 +000069static const struct {
Denis Vlasenko06c0a712007-01-29 22:51:44 +000070 const char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000071 long flags;
Denis Vlasenkob9256052007-09-28 10:29:17 +000072} mount_options[] = {
Rob Landleye3781b72006-08-08 01:39:49 +000073 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
Rob Landleydc0955b2006-03-14 18:16:25 +000074
Rob Landleye3781b72006-08-08 01:39:49 +000075 USE_FEATURE_MOUNT_LOOP(
76 {"loop", 0},
77 )
Rob Landleydc0955b2006-03-14 18:16:25 +000078
Rob Landleye3781b72006-08-08 01:39:49 +000079 USE_FEATURE_MOUNT_FSTAB(
80 {"defaults", 0},
Denis Vlasenko5e90e102006-11-27 19:50:16 +000081 /* {"quiet", 0}, - do not filter out, vfat wants to see it */
Denis Vlasenko13c5a682006-10-16 22:39:51 +000082 {"noauto", MOUNT_NOAUTO},
Denis Vlasenkobf295dd2007-04-05 21:57:47 +000083 {"sw", MOUNT_SWAP},
Denis Vlasenko13c5a682006-10-16 22:39:51 +000084 {"swap", MOUNT_SWAP},
85 USE_DESKTOP({"user", MOUNT_USERS},)
86 USE_DESKTOP({"users", MOUNT_USERS},)
Rob Landleye3781b72006-08-08 01:39:49 +000087 )
Rob Landleydc0955b2006-03-14 18:16:25 +000088
Rob Landleye3781b72006-08-08 01:39:49 +000089 USE_FEATURE_MOUNT_FLAGS(
90 // vfs flags
91 {"nosuid", MS_NOSUID},
92 {"suid", ~MS_NOSUID},
93 {"dev", ~MS_NODEV},
94 {"nodev", MS_NODEV},
95 {"exec", ~MS_NOEXEC},
96 {"noexec", MS_NOEXEC},
97 {"sync", MS_SYNCHRONOUS},
98 {"async", ~MS_SYNCHRONOUS},
99 {"atime", ~MS_NOATIME},
100 {"noatime", MS_NOATIME},
101 {"diratime", ~MS_NODIRATIME},
102 {"nodiratime", MS_NODIRATIME},
103 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +0000104
Rob Landleye3781b72006-08-08 01:39:49 +0000105 // action flags
Rob Landleydc0955b2006-03-14 18:16:25 +0000106
Rob Landleye3781b72006-08-08 01:39:49 +0000107 {"bind", MS_BIND},
108 {"move", MS_MOVE},
109 {"shared", MS_SHARED},
110 {"slave", MS_SLAVE},
111 {"private", MS_PRIVATE},
112 {"unbindable", MS_UNBINDABLE},
113 {"rshared", MS_SHARED|MS_RECURSIVE},
114 {"rslave", MS_SLAVE|MS_RECURSIVE},
115 {"rprivate", MS_SLAVE|MS_RECURSIVE},
116 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
117 )
118
119 // Always understood.
120
121 {"ro", MS_RDONLY}, // vfs flag
122 {"rw", ~MS_RDONLY}, // vfs flag
123 {"remount", MS_REMOUNT}, // action flag
Eric Andersencc8ed391999-10-05 16:24:54 +0000124};
125
Denis Vlasenko25098f72006-09-14 15:46:33 +0000126
Rob Landleydc0955b2006-03-14 18:16:25 +0000127/* Append mount options to string */
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000128static void append_mount_options(char **oldopts, const char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +0000129{
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000130 if (*oldopts && **oldopts) {
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000131 /* do not insert options which are already there */
132 while (newopts[0]) {
133 char *p;
134 int len = strlen(newopts);
135 p = strchr(newopts, ',');
136 if (p) len = p - newopts;
137 p = *oldopts;
138 while (1) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000139 if (!strncmp(p, newopts, len)
140 && (p[len]==',' || p[len]==0))
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000141 goto skip;
142 p = strchr(p,',');
Denis Vlasenko51742f42007-04-12 00:32:05 +0000143 if (!p) break;
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000144 p++;
145 }
146 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
147 free(*oldopts);
148 *oldopts = p;
149skip:
150 newopts += len;
151 while (newopts[0] == ',') newopts++;
152 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000153 } else {
154 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000155 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000156 }
157}
158
159/* Use the mount_options list to parse options into flags.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000160 * Also return list of unrecognized options if unrecognized!=NULL */
Rob Landleydc0955b2006-03-14 18:16:25 +0000161static int parse_mount_options(char *options, char **unrecognized)
162{
163 int flags = MS_SILENT;
164
Rob Landley6a6798b2005-08-10 20:35:54 +0000165 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000166 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000167 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Rob Landleydc0955b2006-03-14 18:16:25 +0000170 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000171
Rob Landley6a6798b2005-08-10 20:35:54 +0000172 // Find this option in mount_options
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000173 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000174 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000175 long fl = mount_options[i].flags;
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000176 if (fl < 0) flags &= fl;
Rob Landleydc0955b2006-03-14 18:16:25 +0000177 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178 break;
179 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000181 // If unrecognized not NULL, append unrecognized mount options */
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000182 if (unrecognized && i == ARRAY_SIZE(mount_options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000183 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000184 i = *unrecognized ? strlen(*unrecognized) : 0;
185 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000186
Rob Landley6a6798b2005-08-10 20:35:54 +0000187 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000188 if (i) (*unrecognized)[i++] = ',';
189 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000191
Denis Vlasenko2535f122007-09-15 13:28:30 +0000192 if (!comma)
193 break;
194 // Advance to next option
195 *comma = ',';
196 options = ++comma;
Eric Andersencc8ed391999-10-05 16:24:54 +0000197 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000198
Rob Landleydc0955b2006-03-14 18:16:25 +0000199 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000200}
201
Rob Landleydc0955b2006-03-14 18:16:25 +0000202// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000203
Rob Landleydc0955b2006-03-14 18:16:25 +0000204static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000205{
Denis Vlasenko87468852007-04-13 23:22:00 +0000206 static const char filesystems[2][sizeof("/proc/filesystems")] = {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000207 "/etc/filesystems",
208 "/proc/filesystems",
Denis Vlasenko372686b2006-10-12 22:42:33 +0000209 };
210 char *fs, *buf;
Rob Landleydc0955b2006-03-14 18:16:25 +0000211 llist_t *list = 0;
212 int i;
213 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000214
Denis Vlasenko87468852007-04-13 23:22:00 +0000215 for (i = 0; i < 2; i++) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000216 f = fopen(filesystems[i], "r");
217 if (!f) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000218
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000219 while ((buf = xmalloc_getline(f)) != 0) {
Denis Vlasenko372686b2006-10-12 22:42:33 +0000220 if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
221 continue;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000222 fs = skip_whitespace(buf);
Denis Vlasenko372686b2006-10-12 22:42:33 +0000223 if (*fs=='#' || *fs=='*' || !*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000224
Denis Vlasenko372686b2006-10-12 22:42:33 +0000225 llist_add_to_end(&list, xstrdup(fs));
226 free(buf);
Rob Landleydc0955b2006-03-14 18:16:25 +0000227 }
228 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
229 }
230
231 return list;
232}
233
Denis Vlasenkob9256052007-09-28 10:29:17 +0000234static llist_t *fslist;
Rob Landleydc0955b2006-03-14 18:16:25 +0000235
Rob Landleydc0955b2006-03-14 18:16:25 +0000236#if ENABLE_FEATURE_CLEAN_UP
237static void delete_block_backed_filesystems(void)
238{
Rob Landleya6b5b602006-05-08 19:03:07 +0000239 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000240}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000241#else
242void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000243#endif
244
245#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000246static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000247static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000248#else
Denis Vlasenko116080a2006-09-21 11:13:08 +0000249#define useMtab 0
250#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000251#endif
252
253// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000254// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000255static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000256{
Denis Vlasenkob1726782006-09-29 14:43:20 +0000257 int rc = 0;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000258
Denis Vlasenkob1726782006-09-29 14:43:20 +0000259 if (fakeIt) goto mtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000260
Rob Landleydc0955b2006-03-14 18:16:25 +0000261 // Mount, with fallback to read-only if necessary.
262
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000263 for (;;) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000264 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
265 vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000266 if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
Rob Landleydc0955b2006-03-14 18:16:25 +0000267 break;
Denis Vlasenko2535f122007-09-15 13:28:30 +0000268 if (!(vfsflags & MS_SILENT))
269 bb_error_msg("%s is write-protected, mounting read-only",
270 mp->mnt_fsname);
Rob Landleydc0955b2006-03-14 18:16:25 +0000271 vfsflags |= MS_RDONLY;
272 }
273
Rob Landleydc0955b2006-03-14 18:16:25 +0000274 // Abort entirely if permission denied.
275
276 if (rc && errno == EPERM)
277 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
278
279 /* If the mount was successful, and we're maintaining an old-style
280 * mtab file by hand, add the new entry to it now. */
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000281 mtab:
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000282 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000283 char *fsname;
Rob Landleydc0955b2006-03-14 18:16:25 +0000284 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
285 int i;
286
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000287 if (!mountTable) {
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000288 bb_error_msg("no %s",bb_path_mtab_file);
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000289 goto ret;
290 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000291
292 // Add vfs string flags
293
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000294 for (i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000295 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000296 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000297
298 // Remove trailing / (if any) from directory we mounted on
299
Denis Vlasenko727ef942006-09-14 13:19:19 +0000300 i = strlen(mp->mnt_dir) - 1;
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000301 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000302
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000303 // Convert to canonical pathnames as needed
Denis Vlasenko727ef942006-09-14 13:19:19 +0000304
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000305 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000306 fsname = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000307 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000308 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000309 mp->mnt_type = (char*)"bind";
Denis Vlasenko727ef942006-09-14 13:19:19 +0000310 }
Denis Vlasenko25098f72006-09-14 15:46:33 +0000311 mp->mnt_freq = mp->mnt_passno = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000312
313 // Write and close.
314
Denis Vlasenko727ef942006-09-14 13:19:19 +0000315 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000316 endmntent(mountTable);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000317 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000318 free(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000319 free(fsname);
320 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000321 }
Denis Vlasenko6a353c82006-11-19 17:34:57 +0000322 ret:
Rob Landleydc0955b2006-03-14 18:16:25 +0000323 return rc;
324}
325
Denis Vlasenko25098f72006-09-14 15:46:33 +0000326#if ENABLE_FEATURE_MOUNT_NFS
327
328/*
329 * Linux NFS mount
330 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
331 *
332 * Licensed under GPLv2, see file LICENSE in this tarball for details.
333 *
334 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
335 * numbers to be specified on the command line.
336 *
337 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
338 * Omit the call to connect() for Linux version 1.3.11 or later.
339 *
340 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
341 * Implemented the "bg", "fg" and "retry" mount options for NFS.
342 *
343 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
344 * - added Native Language Support
345 *
346 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
347 * plus NFSv3 stuff.
348 */
349
Denis Vlasenko25098f72006-09-14 15:46:33 +0000350/* This is just a warning of a common mistake. Possibly this should be a
351 * uclibc faq entry rather than in busybox... */
Denis Vlasenko30a64cd2006-09-15 15:12:00 +0000352#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000353#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
354#endif
355
356#define MOUNTPORT 635
357#define MNTPATHLEN 1024
358#define MNTNAMLEN 255
359#define FHSIZE 32
360#define FHSIZE3 64
361
362typedef char fhandle[FHSIZE];
363
364typedef struct {
365 unsigned int fhandle3_len;
366 char *fhandle3_val;
367} fhandle3;
368
369enum mountstat3 {
370 MNT_OK = 0,
371 MNT3ERR_PERM = 1,
372 MNT3ERR_NOENT = 2,
373 MNT3ERR_IO = 5,
374 MNT3ERR_ACCES = 13,
375 MNT3ERR_NOTDIR = 20,
376 MNT3ERR_INVAL = 22,
377 MNT3ERR_NAMETOOLONG = 63,
378 MNT3ERR_NOTSUPP = 10004,
379 MNT3ERR_SERVERFAULT = 10006,
380};
381typedef enum mountstat3 mountstat3;
382
383struct fhstatus {
384 unsigned int fhs_status;
385 union {
386 fhandle fhs_fhandle;
387 } fhstatus_u;
388};
389typedef struct fhstatus fhstatus;
390
391struct mountres3_ok {
392 fhandle3 fhandle;
393 struct {
394 unsigned int auth_flavours_len;
395 char *auth_flavours_val;
396 } auth_flavours;
397};
398typedef struct mountres3_ok mountres3_ok;
399
400struct mountres3 {
401 mountstat3 fhs_status;
402 union {
403 mountres3_ok mountinfo;
404 } mountres3_u;
405};
406typedef struct mountres3 mountres3;
407
408typedef char *dirpath;
409
410typedef char *name;
411
412typedef struct mountbody *mountlist;
413
414struct mountbody {
415 name ml_hostname;
416 dirpath ml_directory;
417 mountlist ml_next;
418};
419typedef struct mountbody mountbody;
420
421typedef struct groupnode *groups;
422
423struct groupnode {
424 name gr_name;
425 groups gr_next;
426};
427typedef struct groupnode groupnode;
428
429typedef struct exportnode *exports;
430
431struct exportnode {
432 dirpath ex_dir;
433 groups ex_groups;
434 exports ex_next;
435};
436typedef struct exportnode exportnode;
437
438struct ppathcnf {
439 int pc_link_max;
440 short pc_max_canon;
441 short pc_max_input;
442 short pc_name_max;
443 short pc_path_max;
444 short pc_pipe_buf;
Denis Vlasenko28703012006-12-19 20:32:02 +0000445 uint8_t pc_vdisable;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000446 char pc_xxx;
447 short pc_mask[2];
448};
449typedef struct ppathcnf ppathcnf;
450
451#define MOUNTPROG 100005
452#define MOUNTVERS 1
453
454#define MOUNTPROC_NULL 0
455#define MOUNTPROC_MNT 1
456#define MOUNTPROC_DUMP 2
457#define MOUNTPROC_UMNT 3
458#define MOUNTPROC_UMNTALL 4
459#define MOUNTPROC_EXPORT 5
460#define MOUNTPROC_EXPORTALL 6
461
462#define MOUNTVERS_POSIX 2
463
464#define MOUNTPROC_PATHCONF 7
465
466#define MOUNT_V3 3
467
468#define MOUNTPROC3_NULL 0
469#define MOUNTPROC3_MNT 1
470#define MOUNTPROC3_DUMP 2
471#define MOUNTPROC3_UMNT 3
472#define MOUNTPROC3_UMNTALL 4
473#define MOUNTPROC3_EXPORT 5
474
475enum {
476#ifndef NFS_FHSIZE
477 NFS_FHSIZE = 32,
478#endif
479#ifndef NFS_PORT
480 NFS_PORT = 2049
481#endif
482};
483
Denis Vlasenko25098f72006-09-14 15:46:33 +0000484/*
485 * We want to be able to compile mount on old kernels in such a way
486 * that the binary will work well on more recent kernels.
487 * Thus, if necessary we teach nfsmount.c the structure of new fields
488 * that will come later.
489 *
490 * Moreover, the new kernel includes conflict with glibc includes
491 * so it is easiest to ignore the kernel altogether (at compile time).
492 */
493
494struct nfs2_fh {
495 char data[32];
496};
497struct nfs3_fh {
498 unsigned short size;
499 unsigned char data[64];
500};
501
502struct nfs_mount_data {
503 int version; /* 1 */
504 int fd; /* 1 */
505 struct nfs2_fh old_root; /* 1 */
506 int flags; /* 1 */
507 int rsize; /* 1 */
508 int wsize; /* 1 */
509 int timeo; /* 1 */
510 int retrans; /* 1 */
511 int acregmin; /* 1 */
512 int acregmax; /* 1 */
513 int acdirmin; /* 1 */
514 int acdirmax; /* 1 */
515 struct sockaddr_in addr; /* 1 */
516 char hostname[256]; /* 1 */
517 int namlen; /* 2 */
518 unsigned int bsize; /* 3 */
519 struct nfs3_fh root; /* 4 */
520};
521
522/* bits in the flags field */
523enum {
524 NFS_MOUNT_SOFT = 0x0001, /* 1 */
525 NFS_MOUNT_INTR = 0x0002, /* 1 */
526 NFS_MOUNT_SECURE = 0x0004, /* 1 */
527 NFS_MOUNT_POSIX = 0x0008, /* 1 */
528 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
529 NFS_MOUNT_NOAC = 0x0020, /* 1 */
530 NFS_MOUNT_TCP = 0x0040, /* 2 */
531 NFS_MOUNT_VER3 = 0x0080, /* 3 */
532 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
533 NFS_MOUNT_NONLM = 0x0200 /* 3 */
534};
535
536
537/*
538 * We need to translate between nfs status return values and
539 * the local errno values which may not be the same.
540 *
541 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
542 * "after #include <errno.h> the symbol errno is reserved for any use,
543 * it cannot even be used as a struct tag or field name".
544 */
545
546#ifndef EDQUOT
547#define EDQUOT ENOSPC
548#endif
549
550// Convert each NFSERR_BLAH into EBLAH
551
552static const struct {
553 int stat;
554 int errnum;
555} nfs_errtbl[] = {
556 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
557 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
558 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
559 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
560};
561
562static char *nfs_strerror(int status)
563{
564 int i;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000565
566 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
567 if (nfs_errtbl[i].stat == status)
568 return strerror(nfs_errtbl[i].errnum);
569 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000570 return xasprintf("unknown nfs status return value: %d", status);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000571}
572
573static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
574{
575 if (!xdr_opaque(xdrs, objp, FHSIZE))
576 return FALSE;
577 return TRUE;
578}
579
580static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
581{
582 if (!xdr_u_int(xdrs, &objp->fhs_status))
583 return FALSE;
584 switch (objp->fhs_status) {
585 case 0:
586 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
587 return FALSE;
588 break;
589 default:
590 break;
591 }
592 return TRUE;
593}
594
595static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
596{
597 if (!xdr_string(xdrs, objp, MNTPATHLEN))
598 return FALSE;
599 return TRUE;
600}
601
602static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
603{
604 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
605 return FALSE;
606 return TRUE;
607}
608
609static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
610{
611 if (!xdr_fhandle3(xdrs, &objp->fhandle))
612 return FALSE;
613 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
614 sizeof (int), (xdrproc_t) xdr_int))
615 return FALSE;
616 return TRUE;
617}
618
619static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
620{
621 if (!xdr_enum(xdrs, (enum_t *) objp))
622 return FALSE;
623 return TRUE;
624}
625
626static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
627{
628 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
629 return FALSE;
630 switch (objp->fhs_status) {
631 case MNT_OK:
632 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
633 return FALSE;
634 break;
635 default:
636 break;
637 }
638 return TRUE;
639}
640
641#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
642
Denis Vlasenkob9256052007-09-28 10:29:17 +0000643static smalluint nfs_mount_version;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000644
645/*
646 * Unfortunately, the kernel prints annoying console messages
647 * in case of an unexpected nfs mount version (instead of
648 * just returning some error). Therefore we'll have to try
649 * and figure out what version the kernel expects.
650 *
651 * Variables:
652 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
653 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
654 * nfs_mount_version: version this source and running kernel can handle
655 */
656static void
657find_kernel_nfs_mount_version(void)
658{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000659 int kernel_version;
660
661 if (nfs_mount_version)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000662 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
Denis Vlasenkob9256052007-09-28 10:29:17 +0000678static void
679get_mountport(struct pmap *pm_mnt,
680 struct sockaddr_in *server_addr,
Denis Vlasenko25098f72006-09-14 15:46:33 +0000681 long unsigned prog,
682 long unsigned version,
683 long unsigned proto,
684 long unsigned port)
685{
686 struct pmaplist *pmap;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000687
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;
Denis Vlasenkob9256052007-09-28 10:29:17 +0000697 pm_mnt->pm_prog = prog;
698 pm_mnt->pm_vers = version;
699 pm_mnt->pm_prot = proto;
700 pm_mnt->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;
Denis Vlasenkob9256052007-09-28 10:29:17 +0000705 if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000706 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 ||
Denis Vlasenkob9256052007-09-28 10:29:17 +0000712 (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto) ||
Denis Vlasenko25098f72006-09-14 15:46:33 +0000713 (port && pmap->pml_map.pm_port != port))
714 goto next;
Denis Vlasenkob9256052007-09-28 10:29:17 +0000715 memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
716 next:
Denis Vlasenko25098f72006-09-14 15:46:33 +0000717 pmap = pmap->pml_next;
718 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000719 if (!pm_mnt->pm_vers)
720 pm_mnt->pm_vers = MOUNTVERS;
721 if (!pm_mnt->pm_port)
722 pm_mnt->pm_port = MOUNTPORT;
723 if (!pm_mnt->pm_prot)
724 pm_mnt->pm_prot = IPPROTO_TCP;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000725}
726
Denis Vlasenkof0000652007-09-04 18:30:26 +0000727#if BB_MMU
Denis Vlasenko25098f72006-09-14 15:46:33 +0000728static int daemonize(void)
729{
730 int fd;
731 int pid = fork();
732 if (pid < 0) /* error */
733 return -errno;
734 if (pid > 0) /* parent */
735 return 0;
736 /* child */
737 fd = xopen(bb_dev_null, O_RDWR);
738 dup2(fd, 0);
739 dup2(fd, 1);
740 dup2(fd, 2);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000741 while (fd > 2) close(fd--);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000742 setsid();
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000743 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000744 logmode = LOGMODE_SYSLOG;
745 return 1;
746}
Denis Vlasenkof0000652007-09-04 18:30:26 +0000747#else
748static inline int daemonize(void) { return -ENOSYS; }
749#endif
Denis Vlasenko25098f72006-09-14 15:46:33 +0000750
751// TODO
752static inline int we_saw_this_host_before(const char *hostname)
753{
754 return 0;
755}
756
757/* RPC strerror analogs are terminally idiotic:
758 * *mandatory* prefix and \n at end.
759 * This hopefully helps. Usage:
760 * error_msg_rpc(clnt_*error*(" ")) */
761static void error_msg_rpc(const char *msg)
762{
Denis Vlasenko23514fe2006-09-19 14:07:52 +0000763 int len;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000764 while (msg[0] == ' ' || msg[0] == ':') msg++;
765 len = strlen(msg);
766 while (len && msg[len-1] == '\n') len--;
767 bb_error_msg("%.*s", len, msg);
768}
769
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +0000770// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000771static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
772{
773 CLIENT *mclient;
774 char *hostname;
775 char *pathname;
776 char *mounthost;
777 struct nfs_mount_data data;
778 char *opt;
779 struct hostent *hp;
780 struct sockaddr_in server_addr;
781 struct sockaddr_in mount_server_addr;
782 int msock, fsock;
783 union {
784 struct fhstatus nfsv2;
785 struct mountres3 nfsv3;
786 } status;
787 int daemonized;
788 char *s;
789 int port;
790 int mountport;
791 int proto;
Denis Vlasenkof0000652007-09-04 18:30:26 +0000792#if BB_MMU
793 int bg = 0;
794#else
795 enum { bg = 0 };
796#endif
Denis Vlasenko25098f72006-09-14 15:46:33 +0000797 int soft;
798 int intr;
799 int posix;
800 int nocto;
801 int noac;
802 int nolock;
803 int retry;
804 int tcp;
805 int mountprog;
806 int mountvers;
807 int nfsprog;
808 int nfsvers;
809 int retval;
810
811 find_kernel_nfs_mount_version();
812
813 daemonized = 0;
814 mounthost = NULL;
815 retval = ETIMEDOUT;
816 msock = fsock = -1;
817 mclient = NULL;
818
819 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
820
821 filteropts = xstrdup(filteropts); /* going to trash it later... */
822
823 hostname = xstrdup(mp->mnt_fsname);
824 /* mount_main() guarantees that ':' is there */
825 s = strchr(hostname, ':');
826 pathname = s + 1;
827 *s = '\0';
828 /* Ignore all but first hostname in replicated mounts
829 until they can be fully supported. (mack@sgi.com) */
830 s = strchr(hostname, ',');
831 if (s) {
832 *s = '\0';
833 bb_error_msg("warning: multiple hostnames not supported");
834 }
835
836 server_addr.sin_family = AF_INET;
837 if (!inet_aton(hostname, &server_addr.sin_addr)) {
838 hp = gethostbyname(hostname);
839 if (hp == NULL) {
840 bb_herror_msg("%s", hostname);
841 goto fail;
842 }
843 if (hp->h_length > sizeof(struct in_addr)) {
844 bb_error_msg("got bad hp->h_length");
845 hp->h_length = sizeof(struct in_addr);
846 }
847 memcpy(&server_addr.sin_addr,
848 hp->h_addr, hp->h_length);
849 }
850
851 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
852
853 /* add IP address to mtab options for use when unmounting */
854
855 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
856 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
857 } else {
858 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
859 mp->mnt_opts[0] ? "," : "",
860 inet_ntoa(server_addr.sin_addr));
861 free(mp->mnt_opts);
862 mp->mnt_opts = tmp;
863 }
864
865 /* Set default options.
866 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
867 * let the kernel decide.
868 * timeo is filled in after we know whether it'll be TCP or UDP. */
869 memset(&data, 0, sizeof(data));
870 data.retrans = 3;
871 data.acregmin = 3;
872 data.acregmax = 60;
873 data.acdirmin = 30;
874 data.acdirmax = 60;
875 data.namlen = NAME_MAX;
876
Denis Vlasenko25098f72006-09-14 15:46:33 +0000877 soft = 0;
878 intr = 0;
879 posix = 0;
880 nocto = 0;
881 nolock = 0;
882 noac = 0;
883 retry = 10000; /* 10000 minutes ~ 1 week */
884 tcp = 0;
885
886 mountprog = MOUNTPROG;
887 mountvers = 0;
888 port = 0;
889 mountport = 0;
890 nfsprog = 100003;
891 nfsvers = 0;
892
893 /* parse options */
Denis Vlasenko68de7202007-05-09 20:38:04 +0000894 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
Denis Vlasenko25098f72006-09-14 15:46:33 +0000895 char *opteq = strchr(opt, '=');
896 if (opteq) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000897 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000898 /* 0 */ "rsize\0"
899 /* 1 */ "wsize\0"
900 /* 2 */ "timeo\0"
901 /* 3 */ "retrans\0"
902 /* 4 */ "acregmin\0"
903 /* 5 */ "acregmax\0"
904 /* 6 */ "acdirmin\0"
905 /* 7 */ "acdirmax\0"
906 /* 8 */ "actimeo\0"
907 /* 9 */ "retry\0"
908 /* 10 */ "port\0"
909 /* 11 */ "mountport\0"
910 /* 12 */ "mounthost\0"
911 /* 13 */ "mountprog\0"
912 /* 14 */ "mountvers\0"
913 /* 15 */ "nfsprog\0"
914 /* 16 */ "nfsvers\0"
915 /* 17 */ "vers\0"
916 /* 18 */ "proto\0"
917 /* 19 */ "namlen\0"
918 /* 20 */ "addr\0";
Denis Vlasenko13858992006-10-08 12:49:22 +0000919 int val = xatoi_u(opteq + 1);
Denis Vlasenko25098f72006-09-14 15:46:33 +0000920 *opteq = '\0';
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000921 switch (index_in_strings(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +0000922 case 0: // "rsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000923 data.rsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000924 break;
925 case 1: // "wsize"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000926 data.wsize = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000927 break;
928 case 2: // "timeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000929 data.timeo = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000930 break;
931 case 3: // "retrans"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000932 data.retrans = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000933 break;
934 case 4: // "acregmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000935 data.acregmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000936 break;
937 case 5: // "acregmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000938 data.acregmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000939 break;
940 case 6: // "acdirmin"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000941 data.acdirmin = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000942 break;
943 case 7: // "acdirmax"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000944 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000945 break;
946 case 8: // "actimeo"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000947 data.acregmin = val;
948 data.acregmax = val;
949 data.acdirmin = val;
950 data.acdirmax = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000951 break;
952 case 9: // "retry"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000953 retry = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000954 break;
955 case 10: // "port"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000956 port = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000957 break;
958 case 11: // "mountport"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000959 mountport = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000960 break;
961 case 12: // "mounthost"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000962 mounthost = xstrndup(opteq+1,
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000963 strcspn(opteq+1," \t\n\r,"));
Denis Vlasenko68f21872006-10-26 01:47:34 +0000964 break;
965 case 13: // "mountprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000966 mountprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000967 break;
968 case 14: // "mountvers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000969 mountvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000970 break;
971 case 15: // "nfsprog"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000972 nfsprog = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000973 break;
974 case 16: // "nfsvers"
975 case 17: // "vers"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000976 nfsvers = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +0000977 break;
978 case 18: // "proto"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000979 if (!strncmp(opteq+1, "tcp", 3))
980 tcp = 1;
981 else if (!strncmp(opteq+1, "udp", 3))
982 tcp = 0;
983 else
984 bb_error_msg("warning: unrecognized proto= option");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000985 break;
986 case 19: // "namlen"
Denis Vlasenko25098f72006-09-14 15:46:33 +0000987 if (nfs_mount_version >= 2)
988 data.namlen = val;
989 else
990 bb_error_msg("warning: option namlen is not supported\n");
Denis Vlasenko68f21872006-10-26 01:47:34 +0000991 break;
992 case 20: // "addr" - ignore
993 break;
994 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +0000995 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
996 goto fail;
997 }
998 }
999 else {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001000 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001001 "bg\0"
1002 "fg\0"
1003 "soft\0"
1004 "hard\0"
1005 "intr\0"
1006 "posix\0"
1007 "cto\0"
1008 "ac\0"
1009 "tcp\0"
1010 "udp\0"
1011 "lock\0";
Denis Vlasenko25098f72006-09-14 15:46:33 +00001012 int val = 1;
1013 if (!strncmp(opt, "no", 2)) {
1014 val = 0;
1015 opt += 2;
1016 }
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001017 switch (index_in_strings(options, opt)) {
Denis Vlasenko68f21872006-10-26 01:47:34 +00001018 case 0: // "bg"
Denis Vlasenkof0000652007-09-04 18:30:26 +00001019#if BB_MMU
Denis Vlasenko25098f72006-09-14 15:46:33 +00001020 bg = val;
Denis Vlasenkof0000652007-09-04 18:30:26 +00001021#endif
Denis Vlasenko68f21872006-10-26 01:47:34 +00001022 break;
1023 case 1: // "fg"
Denis Vlasenkof0000652007-09-04 18:30:26 +00001024#if BB_MMU
Denis Vlasenko25098f72006-09-14 15:46:33 +00001025 bg = !val;
Denis Vlasenkof0000652007-09-04 18:30:26 +00001026#endif
Denis Vlasenko68f21872006-10-26 01:47:34 +00001027 break;
1028 case 2: // "soft"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001029 soft = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001030 break;
1031 case 3: // "hard"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001032 soft = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001033 break;
1034 case 4: // "intr"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001035 intr = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001036 break;
1037 case 5: // "posix"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001038 posix = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001039 break;
1040 case 6: // "cto"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001041 nocto = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001042 break;
1043 case 7: // "ac"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001044 noac = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001045 break;
1046 case 8: // "tcp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001047 tcp = val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001048 break;
1049 case 9: // "udp"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001050 tcp = !val;
Denis Vlasenko68f21872006-10-26 01:47:34 +00001051 break;
1052 case 10: // "lock"
Denis Vlasenko25098f72006-09-14 15:46:33 +00001053 if (nfs_mount_version >= 3)
1054 nolock = !val;
1055 else
1056 bb_error_msg("warning: option nolock is not supported");
Denis Vlasenko68f21872006-10-26 01:47:34 +00001057 break;
1058 default:
Denis Vlasenko25098f72006-09-14 15:46:33 +00001059 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1060 goto fail;
1061 }
1062 }
1063 }
1064 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1065
1066 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1067 | (intr ? NFS_MOUNT_INTR : 0)
1068 | (posix ? NFS_MOUNT_POSIX : 0)
1069 | (nocto ? NFS_MOUNT_NOCTO : 0)
1070 | (noac ? NFS_MOUNT_NOAC : 0);
1071 if (nfs_mount_version >= 2)
1072 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1073 if (nfs_mount_version >= 3)
1074 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1075 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1076 bb_error_msg("NFSv%d not supported", nfsvers);
1077 goto fail;
1078 }
1079 if (nfsvers && !mountvers)
1080 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1081 if (nfsvers && nfsvers < mountvers) {
1082 mountvers = nfsvers;
1083 }
1084
1085 /* Adjust options if none specified */
1086 if (!data.timeo)
1087 data.timeo = tcp ? 70 : 7;
1088
Denis Vlasenko25098f72006-09-14 15:46:33 +00001089 data.version = nfs_mount_version;
1090
1091 if (vfsflags & MS_REMOUNT)
1092 goto do_mount;
1093
1094 /*
1095 * If the previous mount operation on the same host was
1096 * backgrounded, and the "bg" for this mount is also set,
1097 * give up immediately, to avoid the initial timeout.
1098 */
1099 if (bg && we_saw_this_host_before(hostname)) {
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001100 daemonized = daemonize();
Denis Vlasenko25098f72006-09-14 15:46:33 +00001101 if (daemonized <= 0) { /* parent or error */
1102 retval = -daemonized;
1103 goto ret;
1104 }
1105 }
1106
1107 /* create mount daemon client */
1108 /* See if the nfs host = mount host. */
1109 if (mounthost) {
1110 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1111 mount_server_addr.sin_family = AF_INET;
1112 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1113 } else {
1114 hp = gethostbyname(mounthost);
1115 if (hp == NULL) {
1116 bb_herror_msg("%s", mounthost);
1117 goto fail;
1118 } else {
1119 if (hp->h_length > sizeof(struct in_addr)) {
1120 bb_error_msg("got bad hp->h_length?");
1121 hp->h_length = sizeof(struct in_addr);
1122 }
1123 mount_server_addr.sin_family = AF_INET;
1124 memcpy(&mount_server_addr.sin_addr,
1125 hp->h_addr, hp->h_length);
1126 }
1127 }
1128 }
1129
1130 /*
1131 * The following loop implements the mount retries. When the mount
1132 * times out, and the "bg" option is set, we background ourself
1133 * and continue trying.
1134 *
1135 * The case where the mount point is not present and the "bg"
1136 * option is set, is treated as a timeout. This is done to
1137 * support nested mounts.
1138 *
1139 * The "retry" count specified by the user is the number of
1140 * minutes to retry before giving up.
1141 */
1142 {
1143 struct timeval total_timeout;
1144 struct timeval retry_timeout;
Denis Vlasenkob9256052007-09-28 10:29:17 +00001145 struct pmap pm_mnt;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001146 time_t t;
1147 time_t prevt;
1148 time_t timeout;
1149
1150 retry_timeout.tv_sec = 3;
1151 retry_timeout.tv_usec = 0;
1152 total_timeout.tv_sec = 20;
1153 total_timeout.tv_usec = 0;
1154 timeout = time(NULL) + 60 * retry;
1155 prevt = 0;
1156 t = 30;
1157retry:
1158 /* be careful not to use too many CPU cycles */
1159 if (t - prevt < 30)
1160 sleep(30);
1161
Denis Vlasenkob9256052007-09-28 10:29:17 +00001162 get_mountport(&pm_mnt, &mount_server_addr,
Denis Vlasenko25098f72006-09-14 15:46:33 +00001163 mountprog,
1164 mountvers,
1165 proto,
1166 mountport);
Denis Vlasenkob9256052007-09-28 10:29:17 +00001167 nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001168
1169 /* contact the mount daemon via TCP */
Denis Vlasenkob9256052007-09-28 10:29:17 +00001170 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
Denis Vlasenko25098f72006-09-14 15:46:33 +00001171 msock = RPC_ANYSOCK;
1172
Denis Vlasenkob9256052007-09-28 10:29:17 +00001173 switch (pm_mnt.pm_prot) {
Denis Vlasenko25098f72006-09-14 15:46:33 +00001174 case IPPROTO_UDP:
1175 mclient = clntudp_create(&mount_server_addr,
Denis Vlasenkob9256052007-09-28 10:29:17 +00001176 pm_mnt.pm_prog,
1177 pm_mnt.pm_vers,
Denis Vlasenko25098f72006-09-14 15:46:33 +00001178 retry_timeout,
1179 &msock);
1180 if (mclient)
1181 break;
Denis Vlasenkob9256052007-09-28 10:29:17 +00001182 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
Denis Vlasenko25098f72006-09-14 15:46:33 +00001183 msock = RPC_ANYSOCK;
1184 case IPPROTO_TCP:
1185 mclient = clnttcp_create(&mount_server_addr,
Denis Vlasenkob9256052007-09-28 10:29:17 +00001186 pm_mnt.pm_prog,
1187 pm_mnt.pm_vers,
Denis Vlasenko25098f72006-09-14 15:46:33 +00001188 &msock, 0, 0);
1189 break;
1190 default:
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001191 mclient = NULL;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001192 }
1193 if (!mclient) {
1194 if (!daemonized && prevt == 0)
1195 error_msg_rpc(clnt_spcreateerror(" "));
1196 } else {
1197 enum clnt_stat clnt_stat;
1198 /* try to mount hostname:pathname */
1199 mclient->cl_auth = authunix_create_default();
1200
1201 /* make pointers in xdr_mountres3 NULL so
1202 * that xdr_array allocates memory for us
1203 */
1204 memset(&status, 0, sizeof(status));
1205
Denis Vlasenkob9256052007-09-28 10:29:17 +00001206 if (pm_mnt.pm_vers == 3)
Denis Vlasenko25098f72006-09-14 15:46:33 +00001207 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1208 (xdrproc_t) xdr_dirpath,
1209 (caddr_t) &pathname,
1210 (xdrproc_t) xdr_mountres3,
1211 (caddr_t) &status,
1212 total_timeout);
1213 else
1214 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1215 (xdrproc_t) xdr_dirpath,
1216 (caddr_t) &pathname,
1217 (xdrproc_t) xdr_fhstatus,
1218 (caddr_t) &status,
1219 total_timeout);
1220
1221 if (clnt_stat == RPC_SUCCESS)
1222 goto prepare_kernel_data; /* we're done */
1223 if (errno != ECONNREFUSED) {
1224 error_msg_rpc(clnt_sperror(mclient, " "));
1225 goto fail; /* don't retry */
1226 }
1227 /* Connection refused */
1228 if (!daemonized && prevt == 0) /* print just once */
1229 error_msg_rpc(clnt_sperror(mclient, " "));
1230 auth_destroy(mclient->cl_auth);
1231 clnt_destroy(mclient);
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001232 mclient = NULL;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001233 close(msock);
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001234 msock = -1;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001235 }
1236
1237 /* Timeout. We are going to retry... maybe */
1238
1239 if (!bg)
1240 goto fail;
1241 if (!daemonized) {
1242 daemonized = daemonize();
1243 if (daemonized <= 0) { /* parent or error */
1244 retval = -daemonized;
1245 goto ret;
1246 }
1247 }
1248 prevt = t;
1249 t = time(NULL);
1250 if (t >= timeout)
1251 /* TODO error message */
1252 goto fail;
1253
1254 goto retry;
1255 }
1256
1257prepare_kernel_data:
1258
1259 if (nfsvers == 2) {
1260 if (status.nfsv2.fhs_status != 0) {
1261 bb_error_msg("%s:%s failed, reason given by server: %s",
1262 hostname, pathname,
1263 nfs_strerror(status.nfsv2.fhs_status));
1264 goto fail;
1265 }
1266 memcpy(data.root.data,
1267 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1268 NFS_FHSIZE);
1269 data.root.size = NFS_FHSIZE;
1270 memcpy(data.old_root.data,
1271 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1272 NFS_FHSIZE);
1273 } else {
1274 fhandle3 *my_fhandle;
1275 if (status.nfsv3.fhs_status != 0) {
1276 bb_error_msg("%s:%s failed, reason given by server: %s",
1277 hostname, pathname,
1278 nfs_strerror(status.nfsv3.fhs_status));
1279 goto fail;
1280 }
1281 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1282 memset(data.old_root.data, 0, NFS_FHSIZE);
1283 memset(&data.root, 0, sizeof(data.root));
1284 data.root.size = my_fhandle->fhandle3_len;
1285 memcpy(data.root.data,
1286 (char *) my_fhandle->fhandle3_val,
1287 my_fhandle->fhandle3_len);
1288
1289 data.flags |= NFS_MOUNT_VER3;
1290 }
1291
1292 /* create nfs socket for kernel */
1293
1294 if (tcp) {
1295 if (nfs_mount_version < 3) {
1296 bb_error_msg("NFS over TCP is not supported");
1297 goto fail;
1298 }
1299 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1300 } else
1301 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1302 if (fsock < 0) {
1303 bb_perror_msg("nfs socket");
1304 goto fail;
1305 }
1306 if (bindresvport(fsock, 0) < 0) {
1307 bb_perror_msg("nfs bindresvport");
1308 goto fail;
1309 }
1310 if (port == 0) {
1311 server_addr.sin_port = PMAPPORT;
1312 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1313 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1314 if (port == 0)
1315 port = NFS_PORT;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001316 }
Denis Vlasenko25098f72006-09-14 15:46:33 +00001317 server_addr.sin_port = htons(port);
1318
1319 /* prepare data structure for kernel */
1320
1321 data.fd = fsock;
1322 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1323 strncpy(data.hostname, hostname, sizeof(data.hostname));
1324
1325 /* clean up */
1326
1327 auth_destroy(mclient->cl_auth);
1328 clnt_destroy(mclient);
1329 close(msock);
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001330 msock = -1;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001331
1332 if (bg) {
1333 /* We must wait until mount directory is available */
1334 struct stat statbuf;
1335 int delay = 1;
1336 while (stat(mp->mnt_dir, &statbuf) == -1) {
1337 if (!daemonized) {
1338 daemonized = daemonize();
1339 if (daemonized <= 0) { /* parent or error */
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001340 // FIXME: parent doesn't close fsock - ??!
Denis Vlasenko25098f72006-09-14 15:46:33 +00001341 retval = -daemonized;
1342 goto ret;
1343 }
1344 }
1345 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1346 delay *= 2;
1347 if (delay > 30)
1348 delay = 30;
1349 }
1350 }
1351
1352do_mount: /* perform actual mount */
1353
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001354 mp->mnt_type = (char*)"nfs";
Denis Vlasenko25098f72006-09-14 15:46:33 +00001355 retval = mount_it_now(mp, vfsflags, (char*)&data);
1356 goto ret;
1357
1358fail: /* abort */
1359
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001360 if (msock >= 0) {
Denis Vlasenko25098f72006-09-14 15:46:33 +00001361 if (mclient) {
1362 auth_destroy(mclient->cl_auth);
1363 clnt_destroy(mclient);
1364 }
1365 close(msock);
1366 }
Denis Vlasenko7d8de4d2007-08-28 11:23:23 +00001367 if (fsock >= 0)
Denis Vlasenko25098f72006-09-14 15:46:33 +00001368 close(fsock);
1369
1370ret:
1371 free(hostname);
1372 free(mounthost);
1373 free(filteropts);
1374 return retval;
1375}
1376
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001377#else /* !ENABLE_FEATURE_MOUNT_NFS */
1378
1379/* Never called. Call should be optimized out. */
1380int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1381
1382#endif /* !ENABLE_FEATURE_MOUNT_NFS */
1383
1384// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1385// type detection. Returns 0 for success, nonzero for failure.
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001386// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001387static int singlemount(struct mntent *mp, int ignore_busy)
1388{
1389 int rc = -1, vfsflags;
1390 char *loopFile = 0, *filteropts = 0;
1391 llist_t *fl = 0;
1392 struct stat st;
1393
1394 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1395
1396 // Treat fstype "auto" as unspecified.
1397
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001398 if (mp->mnt_type && strcmp(mp->mnt_type,"auto") == 0)
1399 mp->mnt_type = 0;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001400
Denis Vlasenko2535f122007-09-15 13:28:30 +00001401 // Might this be a virtual filesystem?
1402
1403 if (ENABLE_FEATURE_MOUNT_HELPERS
1404 && (strchr(mp->mnt_fsname,'#'))
1405 ) {
1406 char *s, *p, *args[35];
1407 int n = 0;
1408 for (s = p = mp->mnt_fsname; *s && n < 35-3; ++s) {
1409 if (s[0] == '#' && s[1] != '#') {
1410 *s = '\0';
1411 args[n++] = p;
1412 p = s + 1;
1413 }
1414 }
1415 args[n++] = p;
1416 args[n++] = mp->mnt_dir;
1417 args[n] = NULL;
1418 rc = wait4pid(xspawn(args));
1419 goto report_error;
1420 }
1421
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001422 // Might this be an CIFS filesystem?
1423
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001424 if (ENABLE_FEATURE_MOUNT_CIFS
1425 && (!mp->mnt_type || strcmp(mp->mnt_type,"cifs") == 0)
1426 && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')
1427 && mp->mnt_fsname[0]==mp->mnt_fsname[1]
1428 ) {
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001429 len_and_sockaddr *lsa;
1430 char *ip, *dotted;
1431 char *s;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001432
1433 rc = 1;
1434 // Replace '/' with '\' and verify that unc points to "//server/share".
1435
1436 for (s = mp->mnt_fsname; *s; ++s)
1437 if (*s == '/') *s = '\\';
1438
1439 // get server IP
1440
1441 s = strrchr(mp->mnt_fsname, '\\');
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001442 if (s <= mp->mnt_fsname+1) goto report_error;
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001443 *s = '\0';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001444 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001445 *s = '\\';
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001446 if (!lsa) goto report_error;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001447
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001448 // insert ip=... option into string flags.
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001449
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +00001450 dotted = xmalloc_sockaddr2dotted_noport(&lsa->sa);
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001451 ip = xasprintf("ip=%s", dotted);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001452 parse_mount_options(ip, &filteropts);
1453
1454 // compose new unc '\\server-ip\share'
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001455 // (s => slash after hostname)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001456
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001457 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001458
1459 // lock is required
1460 vfsflags |= MS_MANDLOCK;
1461
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001462 mp->mnt_type = (char*)"cifs";
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001463 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001464 if (ENABLE_FEATURE_CLEAN_UP) {
1465 free(mp->mnt_fsname);
1466 free(ip);
1467 free(dotted);
1468 free(lsa);
1469 }
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001470 goto report_error;
1471 }
1472
1473 // Might this be an NFS filesystem?
1474
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001475 if (ENABLE_FEATURE_MOUNT_NFS
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001476 && (!mp->mnt_type || !strcmp(mp->mnt_type, "nfs"))
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001477 && strchr(mp->mnt_fsname, ':') != NULL
1478 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001479 rc = nfsmount(mp, vfsflags, filteropts);
1480 goto report_error;
1481 }
1482
1483 // Look at the file. (Not found isn't a failure for remount, or for
1484 // a synthetic filesystem like proc or sysfs.)
Denis Vlasenko38ec1472007-05-20 12:32:41 +00001485 // (We use stat, not lstat, in order to allow
1486 // mount symlink_to_file_or_blkdev dir)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001487
Denis Vlasenko38ec1472007-05-20 12:32:41 +00001488 if (!stat(mp->mnt_fsname, &st)
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001489 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1490 ) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001491 // Do we need to allocate a loopback device for it?
1492
1493 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1494 loopFile = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001495 mp->mnt_fsname = NULL; /* will receive malloced loop dev name */
1496 if (set_loop(&(mp->mnt_fsname), loopFile, 0) < 0) {
1497 if (errno == EPERM || errno == EACCES)
1498 bb_error_msg(bb_msg_perm_denied_are_you_root);
1499 else
1500 bb_perror_msg("cannot setup loop device");
Denis Vlasenko13b49242006-09-17 15:04:35 +00001501 return errno;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001502 }
1503
1504 // Autodetect bind mounts
1505
1506 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1507 vfsflags |= MS_BIND;
1508 }
1509
1510 /* If we know the fstype (or don't need to), jump straight
1511 * to the actual mount. */
1512
1513 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1514 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001515 else {
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001516 // Loop through filesystem types until mount succeeds
1517 // or we run out
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001518
1519 /* Initialize list of block backed filesystems. This has to be
1520 * done here so that during "mount -a", mounts after /proc shows up
1521 * can autodetect. */
1522
1523 if (!fslist) {
1524 fslist = get_block_backed_filesystems();
1525 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1526 atexit(delete_block_backed_filesystems);
1527 }
1528
1529 for (fl = fslist; fl; fl = fl->link) {
1530 mp->mnt_type = fl->data;
Denis Vlasenko13b49242006-09-17 15:04:35 +00001531 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001532 if (!rc) break;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001533 }
1534 }
1535
1536 // If mount failed, clean up loop file (if any).
1537
1538 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1539 del_loop(mp->mnt_fsname);
1540 if (ENABLE_FEATURE_CLEAN_UP) {
1541 free(loopFile);
1542 free(mp->mnt_fsname);
1543 }
1544 }
1545
Denis Vlasenko5870ad92007-02-04 02:39:55 +00001546 report_error:
1547 if (ENABLE_FEATURE_CLEAN_UP)
1548 free(filteropts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001549
Denis Vlasenkoc8d4d2f2007-09-07 19:33:56 +00001550 if (errno == EBUSY && ignore_busy)
1551 return 0;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001552 if (rc < 0)
Denis Vlasenko0e2c9fb2007-08-03 14:16:24 +00001553 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001554 return rc;
1555}
1556
1557// Parse options, if necessary parse fstab/mtab, and call singlemount for
1558// each directory to be mounted.
1559
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001560static const char must_be_root[] ALIGN1 = "you must be root";
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001561
Denis Vlasenko06af2162007-02-03 17:28:39 +00001562int mount_main(int argc, char **argv);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001563int mount_main(int argc, char **argv)
1564{
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001565 enum { OPT_ALL = 0x10 };
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001566
1567 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001568 char *opt_o;
1569 const char *fstabname;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001570 FILE *fstab;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001571 int i, j, rc = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001572 unsigned opt;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001573 struct mntent mtpair[2], *mtcur = mtpair;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001574 SKIP_DESKTOP(const int nonroot = 0;)
1575 USE_DESKTOP( int nonroot = (getuid() != 0);)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001576
1577 /* parse long options, like --bind and --move. Note that -o option
1578 * and --option are synonymous. Yes, this means --remount,rw works. */
1579
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001580 for (i = j = 0; i < argc; i++) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001581 if (argv[i][0] == '-' && argv[i][1] == '-') {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001582 append_mount_options(&cmdopts, argv[i]+2);
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001583 } else argv[j++] = argv[i];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001584 }
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001585 argv[j] = 0;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001586 argc = j;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001587
1588 // Parse remaining options
1589
Denis Vlasenkofb48f6c2007-08-29 11:49:41 +00001590 opt = getopt32(argv, "o:t:rwanfvsi", &opt_o, &fstype);
Denis Vlasenkoda3cec92006-09-24 01:01:01 +00001591 if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
1592 //if (opt & 0x2) // -t
1593 if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
1594 if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
1595 //if (opt & 0x10) // -a
Denis Vlasenkob1726782006-09-29 14:43:20 +00001596 if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
1597 if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
Denis Vlasenko546cd182006-10-02 18:52:49 +00001598 //if (opt & 0x80) // -v: verbose (ignore)
1599 //if (opt & 0x100) // -s: sloppy (ignore)
Denis Vlasenkofb48f6c2007-08-29 11:49:41 +00001600 //if (opt & 0x200) // -i: don't call mount.<fstype> (ignore)
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001601 argv += optind;
1602 argc -= optind;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001603
1604 // Three or more non-option arguments? Die with a usage message.
1605
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001606 if (argc > 2) bb_show_usage();
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001607
1608 // If we have no arguments, show currently mounted filesystems
1609
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001610 if (!argc) {
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001611 if (!(opt & OPT_ALL)) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001612 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1613
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001614 if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001615
Denis Vlasenko2535f122007-09-15 13:28:30 +00001616 while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
1617 sizeof(getmntent_buf)))
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001618 {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001619 // Don't show rootfs. FIXME: why??
Denis Vlasenko908d6b72006-12-18 23:07:42 +00001620 // util-linux 2.12a happily shows rootfs...
1621 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001622
1623 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1624 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1625 mtpair->mnt_dir, mtpair->mnt_type,
1626 mtpair->mnt_opts);
1627 }
1628 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1629 return EXIT_SUCCESS;
1630 }
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001631 } else storage_path = bb_simplify_path(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001632
1633 // When we have two arguments, the second is the directory and we can
1634 // skip looking at fstab entirely. We can always abspath() the directory
1635 // argument when we get it.
1636
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001637 if (argc == 2) {
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001638 if (nonroot)
1639 bb_error_msg_and_die(must_be_root);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001640 mtpair->mnt_fsname = argv[0];
1641 mtpair->mnt_dir = argv[1];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001642 mtpair->mnt_type = fstype;
1643 mtpair->mnt_opts = cmdopts;
1644 rc = singlemount(mtpair, 0);
1645 goto clean_up;
1646 }
1647
Denis Vlasenko546cd182006-10-02 18:52:49 +00001648 i = parse_mount_options(cmdopts, 0);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001649 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1650 bb_error_msg_and_die(must_be_root);
Denis Vlasenko546cd182006-10-02 18:52:49 +00001651
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001652 // If we have a shared subtree flag, don't worry about fstab or mtab.
Denis Vlasenko546cd182006-10-02 18:52:49 +00001653
Denis Vlasenko6cd2d2b2007-01-22 14:06:03 +00001654 if (ENABLE_FEATURE_MOUNT_FLAGS
1655 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1656 ) {
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001657 rc = mount("", argv[0], "", i, "");
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001658 if (rc) bb_simple_perror_msg_and_die(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001659 goto clean_up;
1660 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001661
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001662 // Open either fstab or mtab
1663
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001664 fstabname = "/etc/fstab";
1665 if (i & MS_REMOUNT) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001666 fstabname = bb_path_mtab_file;
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001667 }
1668 fstab = setmntent(fstabname, "r");
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001669 if (!fstab)
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001670 bb_perror_msg_and_die("cannot read %s", fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001671
1672 // Loop through entries until we find what we're looking for.
1673
Denis Vlasenko546cd182006-10-02 18:52:49 +00001674 memset(mtpair, 0, sizeof(mtpair));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001675 for (;;) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001676 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001677
1678 // Get next fstab entry
1679
Denis Vlasenko2535f122007-09-15 13:28:30 +00001680 if (!getmntent_r(fstab, mtcur, getmntent_buf
1681 + (mtcur==mtpair ? sizeof(getmntent_buf)/2 : 0),
1682 sizeof(getmntent_buf)/2))
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001683 {
1684 // Were we looking for something specific?
1685
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001686 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001687
1688 // If we didn't find anything, complain.
1689
1690 if (!mtnext->mnt_fsname)
1691 bb_error_msg_and_die("can't find %s in %s",
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001692 argv[0], fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001693
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001694 mtcur = mtnext;
1695 if (nonroot) {
1696 // fstab must have "users" or "user"
1697 if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
1698 bb_error_msg_and_die(must_be_root);
1699 }
1700
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001701 // Mount the last thing we found.
1702
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001703 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001704 append_mount_options(&(mtcur->mnt_opts), cmdopts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001705 rc = singlemount(mtcur, 0);
1706 free(mtcur->mnt_opts);
1707 }
1708 goto clean_up;
1709 }
1710
1711 /* If we're trying to mount something specific and this isn't it,
1712 * skip it. Note we must match both the exact text in fstab (ala
1713 * "proc") or a full path from root */
1714
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001715 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001716
1717 // Is this what we're looking for?
1718
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001719 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1720 strcmp(storage_path, mtcur->mnt_fsname) &&
1721 strcmp(argv[0], mtcur->mnt_dir) &&
1722 strcmp(storage_path, mtcur->mnt_dir)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001723
1724 // Remember this entry. Something later may have overmounted
1725 // it, and we want the _last_ match.
1726
1727 mtcur = mtnext;
1728
1729 // If we're mounting all.
1730
1731 } else {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001732 // Do we need to match a filesystem type?
Denis Vlasenkobf295dd2007-04-05 21:57:47 +00001733 if (fstype && match_fstype(mtcur, fstype)) continue;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001734
1735 // Skip noauto and swap anyway.
1736
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001737 if (parse_mount_options(mtcur->mnt_opts, 0)
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001738 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1739
Denis Vlasenko13c5a682006-10-16 22:39:51 +00001740 // No, mount -a won't mount anything,
1741 // even user mounts, for mere humans.
1742
1743 if (nonroot)
1744 bb_error_msg_and_die(must_be_root);
1745
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001746 // Mount this thing.
1747
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001748 // NFS mounts want this to be xrealloc-able
1749 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001750 if (singlemount(mtcur, 1)) {
1751 /* Count number of failed mounts */
1752 rc++;
1753 }
Denis Vlasenko666da5e2006-12-26 18:17:42 +00001754 free(mtcur->mnt_opts);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001755 }
1756 }
1757 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1758
1759clean_up:
1760
1761 if (ENABLE_FEATURE_CLEAN_UP) {
1762 free(storage_path);
1763 free(cmdopts);
1764 }
1765
1766 return rc;
1767}