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