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