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