blob: 35776e3e0ea615f6a6313c41f3bf49672a50fb1c [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 Landleydc0955b2006-03-14 18:16:25 +000012/* todo:
13 * bb_getopt_ulflags();
14 */
15
Rob Landley22d26fc2006-06-15 15:49:36 +000016/* Design notes: There is no spec for mount. Remind me to write one.
Rob Landleydc0955b2006-03-14 18:16:25 +000017
18 mount_main() calls singlemount() which calls mount_it_now().
Eric Andersen9601a1c2006-03-20 18:07:50 +000019
Rob Landleydc0955b2006-03-14 18:16:25 +000020 mount_main() can loop through /etc/fstab for mount -a
21 singlemount() can loop through /etc/filesystems for fstype detection.
22 mount_it_now() does the actual mount.
23*/
24
Rob Landley22d26fc2006-06-15 15:49:36 +000025#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <mntent.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000027
Denis Vlasenko30a64cd2006-09-15 15:12:00 +000028/* Needed for nfs support only... */
29#include <syslog.h>
30#include <sys/utsname.h>
31#undef TRUE
32#undef FALSE
33#include <rpc/rpc.h>
34#include <rpc/pmap_prot.h>
35#include <rpc/pmap_clnt.h>
36
37
Rob Landleydc0955b2006-03-14 18:16:25 +000038// Not real flags, but we want to be able to check for this.
39#define MOUNT_NOAUTO (1<<29)
40#define MOUNT_SWAP (1<<30)
Rob Landley3ba7bd12006-08-09 19:51:13 +000041
Rob Landleydc0955b2006-03-14 18:16:25 +000042/* Standard mount options (from -o options or --options), with corresponding
43 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000044
Rob Landley6a6798b2005-08-10 20:35:54 +000045struct {
Rob Landleye3781b72006-08-08 01:39:49 +000046 char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000047 long flags;
Rob Landleye3781b72006-08-08 01:39:49 +000048} static mount_options[] = {
49 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
Rob Landleydc0955b2006-03-14 18:16:25 +000050
Rob Landleye3781b72006-08-08 01:39:49 +000051 USE_FEATURE_MOUNT_LOOP(
52 {"loop", 0},
53 )
Rob Landleydc0955b2006-03-14 18:16:25 +000054
Rob Landleye3781b72006-08-08 01:39:49 +000055 USE_FEATURE_MOUNT_FSTAB(
56 {"defaults", 0},
57 {"quiet", 0},
58 {"noauto",MOUNT_NOAUTO},
59 {"swap",MOUNT_SWAP},
60 )
Rob Landleydc0955b2006-03-14 18:16:25 +000061
Rob Landleye3781b72006-08-08 01:39:49 +000062 USE_FEATURE_MOUNT_FLAGS(
63 // vfs flags
64 {"nosuid", MS_NOSUID},
65 {"suid", ~MS_NOSUID},
66 {"dev", ~MS_NODEV},
67 {"nodev", MS_NODEV},
68 {"exec", ~MS_NOEXEC},
69 {"noexec", MS_NOEXEC},
70 {"sync", MS_SYNCHRONOUS},
71 {"async", ~MS_SYNCHRONOUS},
72 {"atime", ~MS_NOATIME},
73 {"noatime", MS_NOATIME},
74 {"diratime", ~MS_NODIRATIME},
75 {"nodiratime", MS_NODIRATIME},
76 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +000077
Rob Landleye3781b72006-08-08 01:39:49 +000078 // action flags
Rob Landleydc0955b2006-03-14 18:16:25 +000079
Rob Landleye3781b72006-08-08 01:39:49 +000080 {"bind", MS_BIND},
81 {"move", MS_MOVE},
82 {"shared", MS_SHARED},
83 {"slave", MS_SLAVE},
84 {"private", MS_PRIVATE},
85 {"unbindable", MS_UNBINDABLE},
86 {"rshared", MS_SHARED|MS_RECURSIVE},
87 {"rslave", MS_SLAVE|MS_RECURSIVE},
88 {"rprivate", MS_SLAVE|MS_RECURSIVE},
89 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
90 )
91
92 // Always understood.
93
94 {"ro", MS_RDONLY}, // vfs flag
95 {"rw", ~MS_RDONLY}, // vfs flag
96 {"remount", MS_REMOUNT}, // action flag
Eric Andersencc8ed391999-10-05 16:24:54 +000097};
98
Denis Vlasenko30a64cd2006-09-15 15:12:00 +000099
Denis Vlasenko25098f72006-09-14 15:46:33 +0000100
Rob Landleydc0955b2006-03-14 18:16:25 +0000101/* Append mount options to string */
102static void append_mount_options(char **oldopts, char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +0000103{
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000104 if (*oldopts && **oldopts) {
Denis Vlasenkoc889d2b2006-09-17 15:08:12 +0000105 /* do not insert options which are already there */
106 while (newopts[0]) {
107 char *p;
108 int len = strlen(newopts);
109 p = strchr(newopts, ',');
110 if (p) len = p - newopts;
111 p = *oldopts;
112 while (1) {
113 if (!strncmp(p,newopts,len) && (p[len]==',' || p[len]==0))
114 goto skip;
115 p = strchr(p,',');
116 if(!p) break;
117 p++;
118 }
119 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
120 free(*oldopts);
121 *oldopts = p;
122skip:
123 newopts += len;
124 while (newopts[0] == ',') newopts++;
125 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000126 } else {
127 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000128 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000129 }
130}
131
132/* Use the mount_options list to parse options into flags.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000133 * Also return list of unrecognized options if unrecognized!=NULL */
Rob Landleydc0955b2006-03-14 18:16:25 +0000134static int parse_mount_options(char *options, char **unrecognized)
135{
136 int flags = MS_SILENT;
137
Rob Landley6a6798b2005-08-10 20:35:54 +0000138 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000139 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000140 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000142
Rob Landleydc0955b2006-03-14 18:16:25 +0000143 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000144
Rob Landley6a6798b2005-08-10 20:35:54 +0000145 // Find this option in mount_options
Rob Landleydc0955b2006-03-14 18:16:25 +0000146 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
147 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000148 long fl = mount_options[i].flags;
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000149 if (fl < 0) flags &= fl;
Rob Landleydc0955b2006-03-14 18:16:25 +0000150 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 break;
152 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000154 // If unrecognized not NULL, append unrecognized mount options */
155 if (unrecognized
156 && i == (sizeof(mount_options) / sizeof(*mount_options)))
Eric Andersen9601a1c2006-03-20 18:07:50 +0000157 {
Rob Landley6a6798b2005-08-10 20:35:54 +0000158 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000159 i = *unrecognized ? strlen(*unrecognized) : 0;
160 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000161
Rob Landley6a6798b2005-08-10 20:35:54 +0000162 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000163 if (i) (*unrecognized)[i++] = ',';
164 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000166
Rob Landley6a6798b2005-08-10 20:35:54 +0000167 // Advance to next option, or finish
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000168 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 *comma = ',';
170 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000171 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000172 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000173
Rob Landleydc0955b2006-03-14 18:16:25 +0000174 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000175}
176
Rob Landleydc0955b2006-03-14 18:16:25 +0000177// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000178
Rob Landleydc0955b2006-03-14 18:16:25 +0000179static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000180{
Rob Landleydc0955b2006-03-14 18:16:25 +0000181 char *fs, *buf,
182 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
183 llist_t *list = 0;
184 int i;
185 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000186
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000187 for (i = 0; filesystems[i]; i++) {
188 f = fopen(filesystems[i], "r");
189 if (!f) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000190
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000191 for (fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
Rob Landleydc0955b2006-03-14 18:16:25 +0000192 free(buf))
193 {
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000194 if (!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000195
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000196 while (isspace(*fs)) fs++;
197 if (*fs=='#' || *fs=='*') continue;
198 if (!*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000199
Rob Landleyd921b2e2006-08-03 15:41:12 +0000200 llist_add_to_end(&list,xstrdup(fs));
Rob Landleydc0955b2006-03-14 18:16:25 +0000201 }
202 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
203 }
204
205 return list;
206}
207
208llist_t *fslist = 0;
209
Rob Landleydc0955b2006-03-14 18:16:25 +0000210#if ENABLE_FEATURE_CLEAN_UP
211static void delete_block_backed_filesystems(void)
212{
Rob Landleya6b5b602006-05-08 19:03:07 +0000213 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000214}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000215#else
216void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000217#endif
218
219#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000220static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000221static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000222#else
Denis Vlasenko116080a2006-09-21 11:13:08 +0000223#define useMtab 0
224#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000225#endif
226
227// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000228// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000229static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000230{
231 int rc;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000232
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000233 if (fakeIt) return 0;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000234
Rob Landleydc0955b2006-03-14 18:16:25 +0000235 // Mount, with fallback to read-only if necessary.
236
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000237 for (;;) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000238 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
239 vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +0000240 if (!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
Rob Landleydc0955b2006-03-14 18:16:25 +0000241 break;
242 bb_error_msg("%s is write-protected, mounting read-only",
243 mp->mnt_fsname);
244 vfsflags |= MS_RDONLY;
245 }
246
Rob Landleydc0955b2006-03-14 18:16:25 +0000247 // Abort entirely if permission denied.
248
249 if (rc && errno == EPERM)
250 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
251
252 /* If the mount was successful, and we're maintaining an old-style
253 * mtab file by hand, add the new entry to it now. */
Eric Andersen9601a1c2006-03-20 18:07:50 +0000254
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000255 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000256 char *fsname;
Rob Landleydc0955b2006-03-14 18:16:25 +0000257 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
258 int i;
259
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000260 if (!mountTable)
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000261 bb_error_msg("no %s",bb_path_mtab_file);
Rob Landleydc0955b2006-03-14 18:16:25 +0000262
263 // Add vfs string flags
264
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000265 for (i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000266 if (mount_options[i].flags > 0 && (mount_options[i].flags & vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000267 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000268
269 // Remove trailing / (if any) from directory we mounted on
270
Denis Vlasenko727ef942006-09-14 13:19:19 +0000271 i = strlen(mp->mnt_dir) - 1;
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000272 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000273
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000274 // Convert to canonical pathnames as needed
Denis Vlasenko727ef942006-09-14 13:19:19 +0000275
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000276 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000277 fsname = 0;
Denis Vlasenko727ef942006-09-14 13:19:19 +0000278 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000279 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
Denis Vlasenko029c4692006-09-17 15:39:22 +0000280 mp->mnt_type = "bind";
Denis Vlasenko727ef942006-09-14 13:19:19 +0000281 }
Denis Vlasenko25098f72006-09-14 15:46:33 +0000282 mp->mnt_freq = mp->mnt_passno = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000283
284 // Write and close.
285
Denis Vlasenko727ef942006-09-14 13:19:19 +0000286 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000287 endmntent(mountTable);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000288 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoa52145a2006-09-17 15:09:48 +0000289 free(mp->mnt_dir);
Denis Vlasenkofc56dd22006-09-17 15:01:53 +0000290 free(fsname);
291 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000292 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000293
Rob Landleydc0955b2006-03-14 18:16:25 +0000294 return rc;
295}
296
Denis Vlasenko25098f72006-09-14 15:46:33 +0000297#if ENABLE_FEATURE_MOUNT_NFS
298
299/*
300 * Linux NFS mount
301 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
302 *
303 * Licensed under GPLv2, see file LICENSE in this tarball for details.
304 *
305 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
306 * numbers to be specified on the command line.
307 *
308 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
309 * Omit the call to connect() for Linux version 1.3.11 or later.
310 *
311 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
312 * Implemented the "bg", "fg" and "retry" mount options for NFS.
313 *
314 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
315 * - added Native Language Support
316 *
317 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
318 * plus NFSv3 stuff.
319 */
320
Denis Vlasenko25098f72006-09-14 15:46:33 +0000321/* This is just a warning of a common mistake. Possibly this should be a
322 * uclibc faq entry rather than in busybox... */
Denis Vlasenko30a64cd2006-09-15 15:12:00 +0000323#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
Denis Vlasenko25098f72006-09-14 15:46:33 +0000324#error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
325#endif
326
327#define MOUNTPORT 635
328#define MNTPATHLEN 1024
329#define MNTNAMLEN 255
330#define FHSIZE 32
331#define FHSIZE3 64
332
333typedef char fhandle[FHSIZE];
334
335typedef struct {
336 unsigned int fhandle3_len;
337 char *fhandle3_val;
338} fhandle3;
339
340enum mountstat3 {
341 MNT_OK = 0,
342 MNT3ERR_PERM = 1,
343 MNT3ERR_NOENT = 2,
344 MNT3ERR_IO = 5,
345 MNT3ERR_ACCES = 13,
346 MNT3ERR_NOTDIR = 20,
347 MNT3ERR_INVAL = 22,
348 MNT3ERR_NAMETOOLONG = 63,
349 MNT3ERR_NOTSUPP = 10004,
350 MNT3ERR_SERVERFAULT = 10006,
351};
352typedef enum mountstat3 mountstat3;
353
354struct fhstatus {
355 unsigned int fhs_status;
356 union {
357 fhandle fhs_fhandle;
358 } fhstatus_u;
359};
360typedef struct fhstatus fhstatus;
361
362struct mountres3_ok {
363 fhandle3 fhandle;
364 struct {
365 unsigned int auth_flavours_len;
366 char *auth_flavours_val;
367 } auth_flavours;
368};
369typedef struct mountres3_ok mountres3_ok;
370
371struct mountres3 {
372 mountstat3 fhs_status;
373 union {
374 mountres3_ok mountinfo;
375 } mountres3_u;
376};
377typedef struct mountres3 mountres3;
378
379typedef char *dirpath;
380
381typedef char *name;
382
383typedef struct mountbody *mountlist;
384
385struct mountbody {
386 name ml_hostname;
387 dirpath ml_directory;
388 mountlist ml_next;
389};
390typedef struct mountbody mountbody;
391
392typedef struct groupnode *groups;
393
394struct groupnode {
395 name gr_name;
396 groups gr_next;
397};
398typedef struct groupnode groupnode;
399
400typedef struct exportnode *exports;
401
402struct exportnode {
403 dirpath ex_dir;
404 groups ex_groups;
405 exports ex_next;
406};
407typedef struct exportnode exportnode;
408
409struct ppathcnf {
410 int pc_link_max;
411 short pc_max_canon;
412 short pc_max_input;
413 short pc_name_max;
414 short pc_path_max;
415 short pc_pipe_buf;
416 u_char pc_vdisable;
417 char pc_xxx;
418 short pc_mask[2];
419};
420typedef struct ppathcnf ppathcnf;
421
422#define MOUNTPROG 100005
423#define MOUNTVERS 1
424
425#define MOUNTPROC_NULL 0
426#define MOUNTPROC_MNT 1
427#define MOUNTPROC_DUMP 2
428#define MOUNTPROC_UMNT 3
429#define MOUNTPROC_UMNTALL 4
430#define MOUNTPROC_EXPORT 5
431#define MOUNTPROC_EXPORTALL 6
432
433#define MOUNTVERS_POSIX 2
434
435#define MOUNTPROC_PATHCONF 7
436
437#define MOUNT_V3 3
438
439#define MOUNTPROC3_NULL 0
440#define MOUNTPROC3_MNT 1
441#define MOUNTPROC3_DUMP 2
442#define MOUNTPROC3_UMNT 3
443#define MOUNTPROC3_UMNTALL 4
444#define MOUNTPROC3_EXPORT 5
445
446enum {
447#ifndef NFS_FHSIZE
448 NFS_FHSIZE = 32,
449#endif
450#ifndef NFS_PORT
451 NFS_PORT = 2049
452#endif
453};
454
Denis Vlasenko25098f72006-09-14 15:46:33 +0000455/*
456 * We want to be able to compile mount on old kernels in such a way
457 * that the binary will work well on more recent kernels.
458 * Thus, if necessary we teach nfsmount.c the structure of new fields
459 * that will come later.
460 *
461 * Moreover, the new kernel includes conflict with glibc includes
462 * so it is easiest to ignore the kernel altogether (at compile time).
463 */
464
465struct nfs2_fh {
466 char data[32];
467};
468struct nfs3_fh {
469 unsigned short size;
470 unsigned char data[64];
471};
472
473struct nfs_mount_data {
474 int version; /* 1 */
475 int fd; /* 1 */
476 struct nfs2_fh old_root; /* 1 */
477 int flags; /* 1 */
478 int rsize; /* 1 */
479 int wsize; /* 1 */
480 int timeo; /* 1 */
481 int retrans; /* 1 */
482 int acregmin; /* 1 */
483 int acregmax; /* 1 */
484 int acdirmin; /* 1 */
485 int acdirmax; /* 1 */
486 struct sockaddr_in addr; /* 1 */
487 char hostname[256]; /* 1 */
488 int namlen; /* 2 */
489 unsigned int bsize; /* 3 */
490 struct nfs3_fh root; /* 4 */
491};
492
493/* bits in the flags field */
494enum {
495 NFS_MOUNT_SOFT = 0x0001, /* 1 */
496 NFS_MOUNT_INTR = 0x0002, /* 1 */
497 NFS_MOUNT_SECURE = 0x0004, /* 1 */
498 NFS_MOUNT_POSIX = 0x0008, /* 1 */
499 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
500 NFS_MOUNT_NOAC = 0x0020, /* 1 */
501 NFS_MOUNT_TCP = 0x0040, /* 2 */
502 NFS_MOUNT_VER3 = 0x0080, /* 3 */
503 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
504 NFS_MOUNT_NONLM = 0x0200 /* 3 */
505};
506
507
508/*
509 * We need to translate between nfs status return values and
510 * the local errno values which may not be the same.
511 *
512 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
513 * "after #include <errno.h> the symbol errno is reserved for any use,
514 * it cannot even be used as a struct tag or field name".
515 */
516
517#ifndef EDQUOT
518#define EDQUOT ENOSPC
519#endif
520
521// Convert each NFSERR_BLAH into EBLAH
522
523static const struct {
524 int stat;
525 int errnum;
526} nfs_errtbl[] = {
527 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
528 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
529 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
530 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
531};
532
533static char *nfs_strerror(int status)
534{
535 int i;
Denis Vlasenkoc0975192006-09-17 15:06:34 +0000536 static char buf[sizeof("unknown nfs status return value: ") + sizeof(int)*3];
Denis Vlasenko25098f72006-09-14 15:46:33 +0000537
538 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
539 if (nfs_errtbl[i].stat == status)
540 return strerror(nfs_errtbl[i].errnum);
541 }
542 sprintf(buf, "unknown nfs status return value: %d", status);
543 return buf;
544}
545
546static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
547{
548 if (!xdr_opaque(xdrs, objp, FHSIZE))
549 return FALSE;
550 return TRUE;
551}
552
553static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
554{
555 if (!xdr_u_int(xdrs, &objp->fhs_status))
556 return FALSE;
557 switch (objp->fhs_status) {
558 case 0:
559 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
560 return FALSE;
561 break;
562 default:
563 break;
564 }
565 return TRUE;
566}
567
568static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
569{
570 if (!xdr_string(xdrs, objp, MNTPATHLEN))
571 return FALSE;
572 return TRUE;
573}
574
575static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
576{
577 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
578 return FALSE;
579 return TRUE;
580}
581
582static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
583{
584 if (!xdr_fhandle3(xdrs, &objp->fhandle))
585 return FALSE;
586 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
587 sizeof (int), (xdrproc_t) xdr_int))
588 return FALSE;
589 return TRUE;
590}
591
592static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
593{
594 if (!xdr_enum(xdrs, (enum_t *) objp))
595 return FALSE;
596 return TRUE;
597}
598
599static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
600{
601 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
602 return FALSE;
603 switch (objp->fhs_status) {
604 case MNT_OK:
605 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
606 return FALSE;
607 break;
608 default:
609 break;
610 }
611 return TRUE;
612}
613
614#define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
615
616/*
617 * nfs_mount_version according to the sources seen at compile time.
618 */
619static int nfs_mount_version;
620static int kernel_version;
621
622/*
623 * Unfortunately, the kernel prints annoying console messages
624 * in case of an unexpected nfs mount version (instead of
625 * just returning some error). Therefore we'll have to try
626 * and figure out what version the kernel expects.
627 *
628 * Variables:
629 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
630 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
631 * nfs_mount_version: version this source and running kernel can handle
632 */
633static void
634find_kernel_nfs_mount_version(void)
635{
636 if (kernel_version)
637 return;
638
639 nfs_mount_version = 4; /* default */
640
641 kernel_version = get_linux_version_code();
642 if (kernel_version) {
643 if (kernel_version < KERNEL_VERSION(2,1,32))
644 nfs_mount_version = 1;
645 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
646 (kernel_version >= KERNEL_VERSION(2,3,0) &&
647 kernel_version < KERNEL_VERSION(2,3,99)))
648 nfs_mount_version = 3;
649 /* else v4 since 2.3.99pre4 */
650 }
651}
652
653static struct pmap *
654get_mountport(struct sockaddr_in *server_addr,
655 long unsigned prog,
656 long unsigned version,
657 long unsigned proto,
658 long unsigned port)
659{
660 struct pmaplist *pmap;
661 static struct pmap p = {0, 0, 0, 0};
662
663 server_addr->sin_port = PMAPPORT;
664 pmap = pmap_getmaps(server_addr);
665
666 if (version > MAX_NFSPROT)
667 version = MAX_NFSPROT;
668 if (!prog)
669 prog = MOUNTPROG;
670 p.pm_prog = prog;
671 p.pm_vers = version;
672 p.pm_prot = proto;
673 p.pm_port = port;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000674
Denis Vlasenko25098f72006-09-14 15:46:33 +0000675 while (pmap) {
676 if (pmap->pml_map.pm_prog != prog)
677 goto next;
678 if (!version && p.pm_vers > pmap->pml_map.pm_vers)
679 goto next;
680 if (version > 2 && pmap->pml_map.pm_vers != version)
681 goto next;
682 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
683 goto next;
684 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
685 (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
686 (port && pmap->pml_map.pm_port != port))
687 goto next;
688 memcpy(&p, &pmap->pml_map, sizeof(p));
689next:
690 pmap = pmap->pml_next;
691 }
692 if (!p.pm_vers)
693 p.pm_vers = MOUNTVERS;
694 if (!p.pm_port)
695 p.pm_port = MOUNTPORT;
696 if (!p.pm_prot)
697 p.pm_prot = IPPROTO_TCP;
698 return &p;
699}
700
701static int daemonize(void)
702{
703 int fd;
704 int pid = fork();
705 if (pid < 0) /* error */
706 return -errno;
707 if (pid > 0) /* parent */
708 return 0;
709 /* child */
710 fd = xopen(bb_dev_null, O_RDWR);
711 dup2(fd, 0);
712 dup2(fd, 1);
713 dup2(fd, 2);
714 if (fd > 2) close(fd);
715 setsid();
716 openlog(bb_applet_name, LOG_PID, LOG_DAEMON);
717 logmode = LOGMODE_SYSLOG;
718 return 1;
719}
720
721// TODO
722static inline int we_saw_this_host_before(const char *hostname)
723{
724 return 0;
725}
726
727/* RPC strerror analogs are terminally idiotic:
728 * *mandatory* prefix and \n at end.
729 * This hopefully helps. Usage:
730 * error_msg_rpc(clnt_*error*(" ")) */
731static void error_msg_rpc(const char *msg)
732{
Denis Vlasenko23514fe2006-09-19 14:07:52 +0000733 int len;
Denis Vlasenko25098f72006-09-14 15:46:33 +0000734 while (msg[0] == ' ' || msg[0] == ':') msg++;
735 len = strlen(msg);
736 while (len && msg[len-1] == '\n') len--;
737 bb_error_msg("%.*s", len, msg);
738}
739
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +0000740// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko25098f72006-09-14 15:46:33 +0000741static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
742{
743 CLIENT *mclient;
744 char *hostname;
745 char *pathname;
746 char *mounthost;
747 struct nfs_mount_data data;
748 char *opt;
749 struct hostent *hp;
750 struct sockaddr_in server_addr;
751 struct sockaddr_in mount_server_addr;
752 int msock, fsock;
753 union {
754 struct fhstatus nfsv2;
755 struct mountres3 nfsv3;
756 } status;
757 int daemonized;
758 char *s;
759 int port;
760 int mountport;
761 int proto;
762 int bg;
763 int soft;
764 int intr;
765 int posix;
766 int nocto;
767 int noac;
768 int nolock;
769 int retry;
770 int tcp;
771 int mountprog;
772 int mountvers;
773 int nfsprog;
774 int nfsvers;
775 int retval;
776
777 find_kernel_nfs_mount_version();
778
779 daemonized = 0;
780 mounthost = NULL;
781 retval = ETIMEDOUT;
782 msock = fsock = -1;
783 mclient = NULL;
784
785 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
786
787 filteropts = xstrdup(filteropts); /* going to trash it later... */
788
789 hostname = xstrdup(mp->mnt_fsname);
790 /* mount_main() guarantees that ':' is there */
791 s = strchr(hostname, ':');
792 pathname = s + 1;
793 *s = '\0';
794 /* Ignore all but first hostname in replicated mounts
795 until they can be fully supported. (mack@sgi.com) */
796 s = strchr(hostname, ',');
797 if (s) {
798 *s = '\0';
799 bb_error_msg("warning: multiple hostnames not supported");
800 }
801
802 server_addr.sin_family = AF_INET;
803 if (!inet_aton(hostname, &server_addr.sin_addr)) {
804 hp = gethostbyname(hostname);
805 if (hp == NULL) {
806 bb_herror_msg("%s", hostname);
807 goto fail;
808 }
809 if (hp->h_length > sizeof(struct in_addr)) {
810 bb_error_msg("got bad hp->h_length");
811 hp->h_length = sizeof(struct in_addr);
812 }
813 memcpy(&server_addr.sin_addr,
814 hp->h_addr, hp->h_length);
815 }
816
817 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
818
819 /* add IP address to mtab options for use when unmounting */
820
821 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
822 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
823 } else {
824 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
825 mp->mnt_opts[0] ? "," : "",
826 inet_ntoa(server_addr.sin_addr));
827 free(mp->mnt_opts);
828 mp->mnt_opts = tmp;
829 }
830
831 /* Set default options.
832 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
833 * let the kernel decide.
834 * timeo is filled in after we know whether it'll be TCP or UDP. */
835 memset(&data, 0, sizeof(data));
836 data.retrans = 3;
837 data.acregmin = 3;
838 data.acregmax = 60;
839 data.acdirmin = 30;
840 data.acdirmax = 60;
841 data.namlen = NAME_MAX;
842
843 bg = 0;
844 soft = 0;
845 intr = 0;
846 posix = 0;
847 nocto = 0;
848 nolock = 0;
849 noac = 0;
850 retry = 10000; /* 10000 minutes ~ 1 week */
851 tcp = 0;
852
853 mountprog = MOUNTPROG;
854 mountvers = 0;
855 port = 0;
856 mountport = 0;
857 nfsprog = 100003;
858 nfsvers = 0;
859
860 /* parse options */
861
862 for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
863 char *opteq = strchr(opt, '=');
864 if (opteq) {
865 int val = atoi(opteq + 1);
866 *opteq = '\0';
867 if (!strcmp(opt, "rsize"))
868 data.rsize = val;
869 else if (!strcmp(opt, "wsize"))
870 data.wsize = val;
871 else if (!strcmp(opt, "timeo"))
872 data.timeo = val;
873 else if (!strcmp(opt, "retrans"))
874 data.retrans = val;
875 else if (!strcmp(opt, "acregmin"))
876 data.acregmin = val;
877 else if (!strcmp(opt, "acregmax"))
878 data.acregmax = val;
879 else if (!strcmp(opt, "acdirmin"))
880 data.acdirmin = val;
881 else if (!strcmp(opt, "acdirmax"))
882 data.acdirmax = val;
883 else if (!strcmp(opt, "actimeo")) {
884 data.acregmin = val;
885 data.acregmax = val;
886 data.acdirmin = val;
887 data.acdirmax = val;
888 }
889 else if (!strcmp(opt, "retry"))
890 retry = val;
891 else if (!strcmp(opt, "port"))
892 port = val;
893 else if (!strcmp(opt, "mountport"))
894 mountport = val;
895 else if (!strcmp(opt, "mounthost"))
896 mounthost = xstrndup(opteq+1,
897 strcspn(opteq+1," \t\n\r,"));
898 else if (!strcmp(opt, "mountprog"))
899 mountprog = val;
900 else if (!strcmp(opt, "mountvers"))
901 mountvers = val;
902 else if (!strcmp(opt, "nfsprog"))
903 nfsprog = val;
904 else if (!strcmp(opt, "nfsvers") ||
905 !strcmp(opt, "vers"))
906 nfsvers = val;
907 else if (!strcmp(opt, "proto")) {
908 if (!strncmp(opteq+1, "tcp", 3))
909 tcp = 1;
910 else if (!strncmp(opteq+1, "udp", 3))
911 tcp = 0;
912 else
913 bb_error_msg("warning: unrecognized proto= option");
914 } else if (!strcmp(opt, "namlen")) {
915 if (nfs_mount_version >= 2)
916 data.namlen = val;
917 else
918 bb_error_msg("warning: option namlen is not supported\n");
919 } else if (!strcmp(opt, "addr"))
920 /* ignore */;
921 else {
922 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
923 goto fail;
924 }
925 }
926 else {
927 int val = 1;
928 if (!strncmp(opt, "no", 2)) {
929 val = 0;
930 opt += 2;
931 }
932 if (!strcmp(opt, "bg"))
933 bg = val;
934 else if (!strcmp(opt, "fg"))
935 bg = !val;
936 else if (!strcmp(opt, "soft"))
937 soft = val;
938 else if (!strcmp(opt, "hard"))
939 soft = !val;
940 else if (!strcmp(opt, "intr"))
941 intr = val;
942 else if (!strcmp(opt, "posix"))
943 posix = val;
944 else if (!strcmp(opt, "cto"))
945 nocto = !val;
946 else if (!strcmp(opt, "ac"))
947 noac = !val;
948 else if (!strcmp(opt, "tcp"))
949 tcp = val;
950 else if (!strcmp(opt, "udp"))
951 tcp = !val;
952 else if (!strcmp(opt, "lock")) {
953 if (nfs_mount_version >= 3)
954 nolock = !val;
955 else
956 bb_error_msg("warning: option nolock is not supported");
957 } else {
958 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
959 goto fail;
960 }
961 }
962 }
963 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
964
965 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
966 | (intr ? NFS_MOUNT_INTR : 0)
967 | (posix ? NFS_MOUNT_POSIX : 0)
968 | (nocto ? NFS_MOUNT_NOCTO : 0)
969 | (noac ? NFS_MOUNT_NOAC : 0);
970 if (nfs_mount_version >= 2)
971 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
972 if (nfs_mount_version >= 3)
973 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
974 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
975 bb_error_msg("NFSv%d not supported", nfsvers);
976 goto fail;
977 }
978 if (nfsvers && !mountvers)
979 mountvers = (nfsvers < 3) ? 1 : nfsvers;
980 if (nfsvers && nfsvers < mountvers) {
981 mountvers = nfsvers;
982 }
983
984 /* Adjust options if none specified */
985 if (!data.timeo)
986 data.timeo = tcp ? 70 : 7;
987
Denis Vlasenko25098f72006-09-14 15:46:33 +0000988 data.version = nfs_mount_version;
989
990 if (vfsflags & MS_REMOUNT)
991 goto do_mount;
992
993 /*
994 * If the previous mount operation on the same host was
995 * backgrounded, and the "bg" for this mount is also set,
996 * give up immediately, to avoid the initial timeout.
997 */
998 if (bg && we_saw_this_host_before(hostname)) {
999 daemonized = daemonize(); /* parent or error */
1000 if (daemonized <= 0) { /* parent or error */
1001 retval = -daemonized;
1002 goto ret;
1003 }
1004 }
1005
1006 /* create mount daemon client */
1007 /* See if the nfs host = mount host. */
1008 if (mounthost) {
1009 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1010 mount_server_addr.sin_family = AF_INET;
1011 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1012 } else {
1013 hp = gethostbyname(mounthost);
1014 if (hp == NULL) {
1015 bb_herror_msg("%s", mounthost);
1016 goto fail;
1017 } else {
1018 if (hp->h_length > sizeof(struct in_addr)) {
1019 bb_error_msg("got bad hp->h_length?");
1020 hp->h_length = sizeof(struct in_addr);
1021 }
1022 mount_server_addr.sin_family = AF_INET;
1023 memcpy(&mount_server_addr.sin_addr,
1024 hp->h_addr, hp->h_length);
1025 }
1026 }
1027 }
1028
1029 /*
1030 * The following loop implements the mount retries. When the mount
1031 * times out, and the "bg" option is set, we background ourself
1032 * and continue trying.
1033 *
1034 * The case where the mount point is not present and the "bg"
1035 * option is set, is treated as a timeout. This is done to
1036 * support nested mounts.
1037 *
1038 * The "retry" count specified by the user is the number of
1039 * minutes to retry before giving up.
1040 */
1041 {
1042 struct timeval total_timeout;
1043 struct timeval retry_timeout;
1044 struct pmap* pm_mnt;
1045 time_t t;
1046 time_t prevt;
1047 time_t timeout;
1048
1049 retry_timeout.tv_sec = 3;
1050 retry_timeout.tv_usec = 0;
1051 total_timeout.tv_sec = 20;
1052 total_timeout.tv_usec = 0;
1053 timeout = time(NULL) + 60 * retry;
1054 prevt = 0;
1055 t = 30;
1056retry:
1057 /* be careful not to use too many CPU cycles */
1058 if (t - prevt < 30)
1059 sleep(30);
1060
1061 pm_mnt = get_mountport(&mount_server_addr,
1062 mountprog,
1063 mountvers,
1064 proto,
1065 mountport);
1066 nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
1067
1068 /* contact the mount daemon via TCP */
1069 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1070 msock = RPC_ANYSOCK;
1071
1072 switch (pm_mnt->pm_prot) {
1073 case IPPROTO_UDP:
1074 mclient = clntudp_create(&mount_server_addr,
1075 pm_mnt->pm_prog,
1076 pm_mnt->pm_vers,
1077 retry_timeout,
1078 &msock);
1079 if (mclient)
1080 break;
1081 mount_server_addr.sin_port = htons(pm_mnt->pm_port);
1082 msock = RPC_ANYSOCK;
1083 case IPPROTO_TCP:
1084 mclient = clnttcp_create(&mount_server_addr,
1085 pm_mnt->pm_prog,
1086 pm_mnt->pm_vers,
1087 &msock, 0, 0);
1088 break;
1089 default:
1090 mclient = 0;
1091 }
1092 if (!mclient) {
1093 if (!daemonized && prevt == 0)
1094 error_msg_rpc(clnt_spcreateerror(" "));
1095 } else {
1096 enum clnt_stat clnt_stat;
1097 /* try to mount hostname:pathname */
1098 mclient->cl_auth = authunix_create_default();
1099
1100 /* make pointers in xdr_mountres3 NULL so
1101 * that xdr_array allocates memory for us
1102 */
1103 memset(&status, 0, sizeof(status));
1104
1105 if (pm_mnt->pm_vers == 3)
1106 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1107 (xdrproc_t) xdr_dirpath,
1108 (caddr_t) &pathname,
1109 (xdrproc_t) xdr_mountres3,
1110 (caddr_t) &status,
1111 total_timeout);
1112 else
1113 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1114 (xdrproc_t) xdr_dirpath,
1115 (caddr_t) &pathname,
1116 (xdrproc_t) xdr_fhstatus,
1117 (caddr_t) &status,
1118 total_timeout);
1119
1120 if (clnt_stat == RPC_SUCCESS)
1121 goto prepare_kernel_data; /* we're done */
1122 if (errno != ECONNREFUSED) {
1123 error_msg_rpc(clnt_sperror(mclient, " "));
1124 goto fail; /* don't retry */
1125 }
1126 /* Connection refused */
1127 if (!daemonized && prevt == 0) /* print just once */
1128 error_msg_rpc(clnt_sperror(mclient, " "));
1129 auth_destroy(mclient->cl_auth);
1130 clnt_destroy(mclient);
1131 mclient = 0;
1132 close(msock);
1133 }
1134
1135 /* Timeout. We are going to retry... maybe */
1136
1137 if (!bg)
1138 goto fail;
1139 if (!daemonized) {
1140 daemonized = daemonize();
1141 if (daemonized <= 0) { /* parent or error */
1142 retval = -daemonized;
1143 goto ret;
1144 }
1145 }
1146 prevt = t;
1147 t = time(NULL);
1148 if (t >= timeout)
1149 /* TODO error message */
1150 goto fail;
1151
1152 goto retry;
1153 }
1154
1155prepare_kernel_data:
1156
1157 if (nfsvers == 2) {
1158 if (status.nfsv2.fhs_status != 0) {
1159 bb_error_msg("%s:%s failed, reason given by server: %s",
1160 hostname, pathname,
1161 nfs_strerror(status.nfsv2.fhs_status));
1162 goto fail;
1163 }
1164 memcpy(data.root.data,
1165 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1166 NFS_FHSIZE);
1167 data.root.size = NFS_FHSIZE;
1168 memcpy(data.old_root.data,
1169 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1170 NFS_FHSIZE);
1171 } else {
1172 fhandle3 *my_fhandle;
1173 if (status.nfsv3.fhs_status != 0) {
1174 bb_error_msg("%s:%s failed, reason given by server: %s",
1175 hostname, pathname,
1176 nfs_strerror(status.nfsv3.fhs_status));
1177 goto fail;
1178 }
1179 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1180 memset(data.old_root.data, 0, NFS_FHSIZE);
1181 memset(&data.root, 0, sizeof(data.root));
1182 data.root.size = my_fhandle->fhandle3_len;
1183 memcpy(data.root.data,
1184 (char *) my_fhandle->fhandle3_val,
1185 my_fhandle->fhandle3_len);
1186
1187 data.flags |= NFS_MOUNT_VER3;
1188 }
1189
1190 /* create nfs socket for kernel */
1191
1192 if (tcp) {
1193 if (nfs_mount_version < 3) {
1194 bb_error_msg("NFS over TCP is not supported");
1195 goto fail;
1196 }
1197 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1198 } else
1199 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1200 if (fsock < 0) {
1201 bb_perror_msg("nfs socket");
1202 goto fail;
1203 }
1204 if (bindresvport(fsock, 0) < 0) {
1205 bb_perror_msg("nfs bindresvport");
1206 goto fail;
1207 }
1208 if (port == 0) {
1209 server_addr.sin_port = PMAPPORT;
1210 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1211 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1212 if (port == 0)
1213 port = NFS_PORT;
Denis Vlasenko25098f72006-09-14 15:46:33 +00001214 }
Denis Vlasenko25098f72006-09-14 15:46:33 +00001215 server_addr.sin_port = htons(port);
1216
1217 /* prepare data structure for kernel */
1218
1219 data.fd = fsock;
1220 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1221 strncpy(data.hostname, hostname, sizeof(data.hostname));
1222
1223 /* clean up */
1224
1225 auth_destroy(mclient->cl_auth);
1226 clnt_destroy(mclient);
1227 close(msock);
1228
1229 if (bg) {
1230 /* We must wait until mount directory is available */
1231 struct stat statbuf;
1232 int delay = 1;
1233 while (stat(mp->mnt_dir, &statbuf) == -1) {
1234 if (!daemonized) {
1235 daemonized = daemonize();
1236 if (daemonized <= 0) { /* parent or error */
1237 retval = -daemonized;
1238 goto ret;
1239 }
1240 }
1241 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1242 delay *= 2;
1243 if (delay > 30)
1244 delay = 30;
1245 }
1246 }
1247
1248do_mount: /* perform actual mount */
1249
1250 mp->mnt_type = "nfs";
1251 retval = mount_it_now(mp, vfsflags, (char*)&data);
1252 goto ret;
1253
1254fail: /* abort */
1255
1256 if (msock != -1) {
1257 if (mclient) {
1258 auth_destroy(mclient->cl_auth);
1259 clnt_destroy(mclient);
1260 }
1261 close(msock);
1262 }
1263 if (fsock != -1)
1264 close(fsock);
1265
1266ret:
1267 free(hostname);
1268 free(mounthost);
1269 free(filteropts);
1270 return retval;
1271}
1272
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001273#else /* !ENABLE_FEATURE_MOUNT_NFS */
1274
1275/* Never called. Call should be optimized out. */
1276int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1277
1278#endif /* !ENABLE_FEATURE_MOUNT_NFS */
1279
1280// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1281// type detection. Returns 0 for success, nonzero for failure.
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001282// NB: mp->xxx fields may be trashed on exit
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001283static int singlemount(struct mntent *mp, int ignore_busy)
1284{
1285 int rc = -1, vfsflags;
1286 char *loopFile = 0, *filteropts = 0;
1287 llist_t *fl = 0;
1288 struct stat st;
1289
1290 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1291
1292 // Treat fstype "auto" as unspecified.
1293
1294 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
1295
1296 // Might this be an CIFS filesystem?
1297
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001298 if (ENABLE_FEATURE_MOUNT_CIFS &&
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001299 (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) &&
1300 (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')))
1301 {
1302 struct hostent *he;
1303 char ip[32], *s;
1304
1305 rc = 1;
1306 // Replace '/' with '\' and verify that unc points to "//server/share".
1307
1308 for (s = mp->mnt_fsname; *s; ++s)
1309 if (*s == '/') *s = '\\';
1310
1311 // get server IP
1312
1313 s = strrchr(mp->mnt_fsname, '\\');
1314 if (s == mp->mnt_fsname+1) goto report_error;
1315 *s = 0;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001316 he = gethostbyname(mp->mnt_fsname+2);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001317 *s = '\\';
1318 if (!he) goto report_error;
1319
1320 // Insert ip=... option into string flags. (NOTE: Add IPv6 support.)
1321
1322 sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1],
1323 he->h_addr[2], he->h_addr[3]);
1324 parse_mount_options(ip, &filteropts);
1325
1326 // compose new unc '\\server-ip\share'
1327
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001328 mp->mnt_fsname = xasprintf("\\\\%s%s", ip+3,
1329 strchr(mp->mnt_fsname+2,'\\'));
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001330
1331 // lock is required
1332 vfsflags |= MS_MANDLOCK;
1333
1334 mp->mnt_type = "cifs";
1335 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001336 if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001337 goto report_error;
1338 }
1339
1340 // Might this be an NFS filesystem?
1341
1342 if (ENABLE_FEATURE_MOUNT_NFS &&
1343 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
1344 strchr(mp->mnt_fsname, ':') != NULL)
1345 {
1346 rc = nfsmount(mp, vfsflags, filteropts);
1347 goto report_error;
1348 }
1349
1350 // Look at the file. (Not found isn't a failure for remount, or for
1351 // a synthetic filesystem like proc or sysfs.)
1352
1353 if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1354 {
1355 // Do we need to allocate a loopback device for it?
1356
1357 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1358 loopFile = bb_simplify_path(mp->mnt_fsname);
1359 mp->mnt_fsname = 0;
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001360 switch (set_loop(&(mp->mnt_fsname), loopFile, 0)) {
Denis Vlasenko13b49242006-09-17 15:04:35 +00001361 case 0:
1362 case 1:
1363 break;
1364 default:
1365 bb_error_msg( errno == EPERM || errno == EACCES
1366 ? bb_msg_perm_denied_are_you_root
1367 : "cannot setup loop device");
1368 return errno;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001369 }
1370
1371 // Autodetect bind mounts
1372
1373 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1374 vfsflags |= MS_BIND;
1375 }
1376
1377 /* If we know the fstype (or don't need to), jump straight
1378 * to the actual mount. */
1379
1380 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1381 rc = mount_it_now(mp, vfsflags, filteropts);
1382
1383 // Loop through filesystem types until mount succeeds or we run out
1384
1385 else {
1386
1387 /* Initialize list of block backed filesystems. This has to be
1388 * done here so that during "mount -a", mounts after /proc shows up
1389 * can autodetect. */
1390
1391 if (!fslist) {
1392 fslist = get_block_backed_filesystems();
1393 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1394 atexit(delete_block_backed_filesystems);
1395 }
1396
1397 for (fl = fslist; fl; fl = fl->link) {
1398 mp->mnt_type = fl->data;
Denis Vlasenko13b49242006-09-17 15:04:35 +00001399 rc = mount_it_now(mp, vfsflags, filteropts);
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001400 if (!rc) break;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001401 }
1402 }
1403
1404 // If mount failed, clean up loop file (if any).
1405
1406 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1407 del_loop(mp->mnt_fsname);
1408 if (ENABLE_FEATURE_CLEAN_UP) {
1409 free(loopFile);
1410 free(mp->mnt_fsname);
1411 }
1412 }
1413
1414report_error:
1415 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
1416
1417 if (rc && errno == EBUSY && ignore_busy) rc = 0;
1418 if (rc < 0)
1419 /* perror here sometimes says "mounting ... on ... failed: Success" */
1420 bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1421
1422 return rc;
1423}
1424
1425// Parse options, if necessary parse fstab/mtab, and call singlemount for
1426// each directory to be mounted.
1427
1428int mount_main(int argc, char **argv)
1429{
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001430 enum { OPT_ALL = 0x8 };
1431
1432 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001433 char *opt_o;
1434 const char *fstabname;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001435 FILE *fstab;
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001436 int i, j, rc = 0;
1437 unsigned long opt;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001438 struct mntent mtpair[2], *mtcur = mtpair;
1439
1440 /* parse long options, like --bind and --move. Note that -o option
1441 * and --option are synonymous. Yes, this means --remount,rw works. */
1442
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001443 for (i = j = 0; i < argc; i++) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001444 if (argv[i][0] == '-' && argv[i][1] == '-') {
1445 append_mount_options(&cmdopts,argv[i]+2);
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001446 } else argv[j++] = argv[i];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001447 }
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001448 argc = j;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001449
1450 // Parse remaining options
1451
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001452 opt = bb_getopt_ulflags(argc, argv, "o:t:rwavnf", &opt_o, &fstype);
1453 if (opt & 1) // -o
1454 append_mount_options(&cmdopts, opt_o);
1455 //if (opt & 1) // -t
1456 if (opt & 2) // -r
1457 append_mount_options(&cmdopts, "ro");
1458 if (opt & 4) // -w
1459 append_mount_options(&cmdopts, "rw");
1460 //if (opt & 8) // -a
1461 if (opt & 0x10) // -n
1462 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE);
1463 if (opt & 0x20) // -f
1464 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE);
1465 //if (opt & 0x40) // ignore -v
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001466 argv += optind;
1467 argc -= optind;
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001468
1469 // Three or more non-option arguments? Die with a usage message.
1470
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001471 if (argc > 2) bb_show_usage();
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001472
1473 // If we have no arguments, show currently mounted filesystems
1474
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001475 if (!argc) {
Denis Vlasenko9c99b622006-09-17 15:05:31 +00001476 if (!(opt & OPT_ALL)) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001477 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1478
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001479 if (!mountTable) bb_error_msg_and_die("no %s",bb_path_mtab_file);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001480
1481 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
1482 sizeof(bb_common_bufsiz1)))
1483 {
1484 // Don't show rootfs.
1485 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
1486
1487 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1488 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1489 mtpair->mnt_dir, mtpair->mnt_type,
1490 mtpair->mnt_opts);
1491 }
1492 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1493 return EXIT_SUCCESS;
1494 }
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001495 } else storage_path = bb_simplify_path(argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001496
1497 // When we have two arguments, the second is the directory and we can
1498 // skip looking at fstab entirely. We can always abspath() the directory
1499 // argument when we get it.
1500
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001501 if (argc == 2) {
1502 mtpair->mnt_fsname = argv[0];
1503 mtpair->mnt_dir = argv[1];
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001504 mtpair->mnt_type = fstype;
1505 mtpair->mnt_opts = cmdopts;
1506 rc = singlemount(mtpair, 0);
1507 goto clean_up;
1508 }
1509
1510 // If we have a shared subtree flag, don't worry about fstab or mtab.
1511 i = parse_mount_options(cmdopts,0);
1512 if (ENABLE_FEATURE_MOUNT_FLAGS &&
1513 (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
1514 {
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001515 rc = mount("", argv[0], "", i, "");
1516 if (rc) bb_perror_msg_and_die("%s", argv[0]);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001517 goto clean_up;
1518 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001519
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001520 // Open either fstab or mtab
1521
1522 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
1523 fstabname = bb_path_mtab_file;
1524 else fstabname="/etc/fstab";
1525
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001526 fstab = setmntent(fstabname,"r");
1527 if (!fstab)
Denis Vlasenko85f9e322006-09-19 14:14:12 +00001528 bb_perror_msg_and_die("cannot read %s", fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001529
1530 // Loop through entries until we find what we're looking for.
1531
1532 memset(mtpair,0,sizeof(mtpair));
1533 for (;;) {
Denis Vlasenko8d474b52006-09-17 15:00:58 +00001534 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001535
1536 // Get next fstab entry
1537
1538 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
1539 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
1540 sizeof(bb_common_bufsiz1)/2))
1541 {
1542 // Were we looking for something specific?
1543
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001544 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001545
1546 // If we didn't find anything, complain.
1547
1548 if (!mtnext->mnt_fsname)
1549 bb_error_msg_and_die("can't find %s in %s",
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001550 argv[0], fstabname);
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001551
1552 // Mount the last thing we found.
1553
1554 mtcur = mtnext;
1555 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
1556 append_mount_options(&(mtcur->mnt_opts),cmdopts);
1557 rc = singlemount(mtcur, 0);
1558 free(mtcur->mnt_opts);
1559 }
1560 goto clean_up;
1561 }
1562
1563 /* If we're trying to mount something specific and this isn't it,
1564 * skip it. Note we must match both the exact text in fstab (ala
1565 * "proc") or a full path from root */
1566
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001567 if (argc) {
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001568
1569 // Is this what we're looking for?
1570
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001571 if (strcmp(argv[0],mtcur->mnt_fsname) &&
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001572 strcmp(storage_path,mtcur->mnt_fsname) &&
Denis Vlasenko3bc59aa2006-09-17 15:04:01 +00001573 strcmp(argv[0],mtcur->mnt_dir) &&
Denis Vlasenko30a64cd2006-09-15 15:12:00 +00001574 strcmp(storage_path,mtcur->mnt_dir)) continue;
1575
1576 // Remember this entry. Something later may have overmounted
1577 // it, and we want the _last_ match.
1578
1579 mtcur = mtnext;
1580
1581 // If we're mounting all.
1582
1583 } else {
1584
1585 // Do we need to match a filesystem type?
1586 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
1587
1588 // Skip noauto and swap anyway.
1589
1590 if (parse_mount_options(mtcur->mnt_opts,0)
1591 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1592
1593 // Mount this thing.
1594
1595 if (singlemount(mtcur, 1)) {
1596 /* Count number of failed mounts */
1597 rc++;
1598 }
1599 }
1600 }
1601 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1602
1603clean_up:
1604
1605 if (ENABLE_FEATURE_CLEAN_UP) {
1606 free(storage_path);
1607 free(cmdopts);
1608 }
1609
1610 return rc;
1611}