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