]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/mount/mount.c
MFC r309400:
[FreeBSD/stable/8.git] / sbin / mount / mount.c
1 /*-
2  * Copyright (c) 1980, 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #if 0
35 static char sccsid[] = "@(#)mount.c     8.25 (Berkeley) 5/8/95";
36 #endif
37 #endif /* not lint */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/wait.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fstab.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <signal.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <libutil.h>
60
61 #include "extern.h"
62 #include "mntopts.h"
63 #include "pathnames.h"
64
65 /* `meta' options */
66 #define MOUNT_META_OPTION_FSTAB         "fstab"
67 #define MOUNT_META_OPTION_CURRENT       "current"
68
69 int debug, fstab_style, verbose;
70
71 struct cpa {
72         char    **a;
73         ssize_t sz;
74         int     c;
75 };
76
77 char   *catopt(char *, const char *);
78 struct statfs *getmntpt(const char *);
79 int     hasopt(const char *, const char *);
80 int     ismounted(struct fstab *, struct statfs *, int);
81 int     isremountable(const char *);
82 void    mangle(char *, struct cpa *);
83 char   *update_options(char *, char *, int);
84 int     mountfs(const char *, const char *, const char *,
85                         int, const char *, const char *);
86 void    remopt(char *, const char *);
87 void    prmount(struct statfs *);
88 void    putfsent(struct statfs *);
89 void    usage(void);
90 char   *flags2opts(int);
91
92 /* Map from mount options to printable formats. */
93 static struct opt {
94         uint64_t o_opt;
95         const char *o_name;
96 } optnames[] = {
97         { MNT_ASYNC,            "asynchronous" },
98         { MNT_EXPORTED,         "NFS exported" },
99         { MNT_LOCAL,            "local" },
100         { MNT_NOATIME,          "noatime" },
101         { MNT_NOEXEC,           "noexec" },
102         { MNT_NOSUID,           "nosuid" },
103         { MNT_NOSYMFOLLOW,      "nosymfollow" },
104         { MNT_QUOTA,            "with quotas" },
105         { MNT_RDONLY,           "read-only" },
106         { MNT_SYNCHRONOUS,      "synchronous" },
107         { MNT_UNION,            "union" },
108         { MNT_NOCLUSTERR,       "noclusterr" },
109         { MNT_NOCLUSTERW,       "noclusterw" },
110         { MNT_SUIDDIR,          "suiddir" },
111         { MNT_SOFTDEP,          "soft-updates" },
112         { MNT_MULTILABEL,       "multilabel" },
113         { MNT_ACLS,             "acls" },
114         { MNT_NFS4ACLS,         "nfsv4acls" },
115         { MNT_GJOURNAL,         "gjournal" },
116         { 0, NULL }
117 };
118
119 /*
120  * List of VFS types that can be remounted without becoming mounted on top
121  * of each other.
122  * XXX Is this list correct?
123  */
124 static const char *
125 remountable_fs_names[] = {
126         "ufs", "ffs", "ext2fs",
127         0
128 };
129
130 static const char userquotaeq[] = "userquota=";
131 static const char groupquotaeq[] = "groupquota=";
132
133 static char *mountprog = NULL;
134
135 static int
136 use_mountprog(const char *vfstype)
137 {
138         /* XXX: We need to get away from implementing external mount
139          *      programs for every filesystem, and move towards having
140          *      each filesystem properly implement the nmount() system call.
141          */
142         unsigned int i;
143         const char *fs[] = {
144         "cd9660", "mfs", "msdosfs", "newnfs", "nfs", "ntfs",
145         "nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs",
146         NULL
147         };
148
149         if (mountprog != NULL)
150                 return (1);
151
152         for (i = 0; fs[i] != NULL; ++i) {
153                 if (strcmp(vfstype, fs[i]) == 0)
154                         return (1);
155         }
156
157         return (0);
158 }
159
160 static int
161 exec_mountprog(const char *name, const char *execname, char *const argv[])
162 {
163         pid_t pid;
164         int status;
165
166         switch (pid = fork()) {
167         case -1:                                /* Error. */
168                 warn("fork");
169                 exit (1);
170         case 0:                                 /* Child. */
171                 /* Go find an executable. */
172                 execvP(execname, _PATH_SYSPATH, argv);
173                 if (errno == ENOENT) {
174                         warn("exec %s not found", execname);
175                         if (execname[0] != '/') {
176                                 warnx("in path: %s", _PATH_SYSPATH);
177                         }
178                 }
179                 exit(1);
180         default:                                /* Parent. */
181                 if (waitpid(pid, &status, 0) < 0) {
182                         warn("waitpid");
183                         return (1);
184                 }
185
186                 if (WIFEXITED(status)) {
187                         if (WEXITSTATUS(status) != 0)
188                                 return (WEXITSTATUS(status));
189                 } else if (WIFSIGNALED(status)) {
190                         warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
191                         return (1);
192                 }
193                 break;
194         }
195
196         return (0);
197 }
198
199 static int
200 specified_ro(const char *arg)
201 {
202         char *optbuf, *opt;
203         int ret = 0;
204
205         optbuf = strdup(arg);
206         if (optbuf == NULL)
207                  err(1, NULL);
208
209         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
210                 if (strcmp(opt, "ro") == 0) {
211                         ret = 1;
212                         break;
213                 }
214         }
215         free(optbuf);
216         return (ret);
217 }
218
219 static void
220 restart_mountd(void)
221 {
222         struct pidfh *pfh;
223         pid_t mountdpid;
224
225         pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
226         if (pfh != NULL) {
227                 /* Mountd is not running. */
228                 pidfile_remove(pfh);
229                 return;
230         }
231         if (errno != EEXIST) {
232                 /* Cannot open pidfile for some reason. */
233                 return;
234         }
235         /* We have mountd(8) PID in mountdpid varible, let's signal it. */
236         if (kill(mountdpid, SIGHUP) == -1)
237                 err(1, "signal mountd");
238 }
239
240 int
241 main(int argc, char *argv[])
242 {
243         const char *mntfromname, **vfslist, *vfstype;
244         struct fstab *fs;
245         struct statfs *mntbuf;
246         int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
247         char *cp, *ep, *options;
248
249         all = init_flags = late = 0;
250         ro = 0;
251         options = NULL;
252         vfslist = NULL;
253         vfstype = "ufs";
254         while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
255                 switch (ch) {
256                 case 'a':
257                         all = 1;
258                         break;
259                 case 'd':
260                         debug = 1;
261                         break;
262                 case 'F':
263                         setfstab(optarg);
264                         break;
265                 case 'f':
266                         init_flags |= MNT_FORCE;
267                         break;
268                 case 'l':
269                         late = 1;
270                         break;
271                 case 'o':
272                         if (*optarg) {
273                                 options = catopt(options, optarg);
274                                 if (specified_ro(optarg))
275                                         ro = 1;
276                         }
277                         break;
278                 case 'p':
279                         fstab_style = 1;
280                         verbose = 1;
281                         break;
282                 case 'r':
283                         options = catopt(options, "ro");
284                         ro = 1;
285                         break;
286                 case 't':
287                         if (vfslist != NULL)
288                                 errx(1, "only one -t option may be specified");
289                         vfslist = makevfslist(optarg);
290                         vfstype = optarg;
291                         break;
292                 case 'u':
293                         init_flags |= MNT_UPDATE;
294                         break;
295                 case 'v':
296                         verbose = 1;
297                         break;
298                 case 'w':
299                         options = catopt(options, "noro");
300                         break;
301                 case '?':
302                 default:
303                         usage();
304                         /* NOTREACHED */
305                 }
306         argc -= optind;
307         argv += optind;
308
309 #define BADTYPE(type)                                                   \
310         (strcmp(type, FSTAB_RO) &&                                      \
311             strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
312
313         if ((init_flags & MNT_UPDATE) && (ro == 0))
314                 options = catopt(options, "noro");
315
316         rval = 0;
317         switch (argc) {
318         case 0:
319                 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
320                         err(1, "getmntinfo");
321                 if (all) {
322                         while ((fs = getfsent()) != NULL) {
323                                 if (BADTYPE(fs->fs_type))
324                                         continue;
325                                 if (checkvfsname(fs->fs_vfstype, vfslist))
326                                         continue;
327                                 if (hasopt(fs->fs_mntops, "noauto"))
328                                         continue;
329                                 if (hasopt(fs->fs_mntops, "late") && !late)
330                                         continue;
331                                 if (hasopt(fs->fs_mntops, "failok"))
332                                         failok = 1;
333                                 else
334                                         failok = 0;
335                                 if (!(init_flags & MNT_UPDATE) &&
336                                     ismounted(fs, mntbuf, mntsize))
337                                         continue;
338                                 options = update_options(options, fs->fs_mntops,
339                                     mntbuf->f_flags);
340                                 if (mountfs(fs->fs_vfstype, fs->fs_spec,
341                                     fs->fs_file, init_flags, options,
342                                     fs->fs_mntops) && !failok)
343                                         rval = 1;
344                         }
345                 } else if (fstab_style) {
346                         for (i = 0; i < mntsize; i++) {
347                                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
348                                         continue;
349                                 putfsent(&mntbuf[i]);
350                         }
351                 } else {
352                         for (i = 0; i < mntsize; i++) {
353                                 if (checkvfsname(mntbuf[i].f_fstypename,
354                                     vfslist))
355                                         continue;
356                                 if (!verbose &&
357                                     (mntbuf[i].f_flags & MNT_IGNORE) != 0)
358                                         continue;
359                                 prmount(&mntbuf[i]);
360                         }
361                 }
362                 exit(rval);
363         case 1:
364                 if (vfslist != NULL)
365                         usage();
366
367                 rmslashes(*argv, *argv);
368                 if (init_flags & MNT_UPDATE) {
369                         mntfromname = NULL;
370                         have_fstab = 0;
371                         if ((mntbuf = getmntpt(*argv)) == NULL)
372                                 errx(1, "not currently mounted %s", *argv);
373                         /*
374                          * Only get the mntflags from fstab if both mntpoint
375                          * and mntspec are identical. Also handle the special
376                          * case where just '/' is mounted and 'spec' is not
377                          * identical with the one from fstab ('/dev' is missing
378                          * in the spec-string at boot-time).
379                          */
380                         if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
381                                 if (strcmp(fs->fs_spec,
382                                     mntbuf->f_mntfromname) == 0 &&
383                                     strcmp(fs->fs_file,
384                                     mntbuf->f_mntonname) == 0) {
385                                         have_fstab = 1;
386                                         mntfromname = mntbuf->f_mntfromname;
387                                 } else if (argv[0][0] == '/' &&
388                                     argv[0][1] == '\0') {
389                                         fs = getfsfile("/");
390                                         have_fstab = 1;
391                                         mntfromname = fs->fs_spec;
392                                 }
393                         }
394                         if (have_fstab) {
395                                 options = update_options(options, fs->fs_mntops,
396                                     mntbuf->f_flags);
397                         } else {
398                                 mntfromname = mntbuf->f_mntfromname;
399                                 options = update_options(options, NULL,
400                                     mntbuf->f_flags);
401                         }
402                         rval = mountfs(mntbuf->f_fstypename, mntfromname,
403                             mntbuf->f_mntonname, init_flags, options, 0);
404                         break;
405                 }
406                 if ((fs = getfsfile(*argv)) == NULL &&
407                     (fs = getfsspec(*argv)) == NULL)
408                         errx(1, "%s: unknown special file or file system",
409                             *argv);
410                 if (BADTYPE(fs->fs_type))
411                         errx(1, "%s has unknown file system type",
412                             *argv);
413                 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
414                     init_flags, options, fs->fs_mntops);
415                 break;
416         case 2:
417                 /*
418                  * If -t flag has not been specified, the path cannot be
419                  * found, spec contains either a ':' or a '@', then assume
420                  * that an NFS file system is being specified ala Sun.
421                  * Check if the hostname contains only allowed characters
422                  * to reduce false positives.  IPv6 addresses containing
423                  * ':' will be correctly parsed only if the separator is '@'.
424                  * The definition of a valid hostname is taken from RFC 1034.
425                  */
426                 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
427                     (ep = strchr(argv[0], ':')) != NULL)) {
428                         if (*ep == '@') {
429                                 cp = ep + 1;
430                                 ep = cp + strlen(cp);
431                         } else
432                                 cp = argv[0];
433                         while (cp != ep) {
434                                 if (!isdigit(*cp) && !isalpha(*cp) &&
435                                     *cp != '.' && *cp != '-' && *cp != ':')
436                                         break;
437                                 cp++;
438                         }
439                         if (cp == ep)
440                                 vfstype = "nfs";
441                 }
442                 rval = mountfs(vfstype,
443                     argv[0], argv[1], init_flags, options, NULL);
444                 break;
445         default:
446                 usage();
447                 /* NOTREACHED */
448         }
449
450         /*
451          * If the mount was successfully, and done by root, tell mountd the
452          * good news.
453          */
454         if (rval == 0 && getuid() == 0)
455                 restart_mountd();
456
457         exit(rval);
458 }
459
460 int
461 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
462 {
463         char realfsfile[PATH_MAX];
464         int i;
465
466         if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
467                 /* the root file system can always be remounted */
468                 return (0);
469
470         /* The user may have specified a symlink in fstab, resolve the path */
471         if (realpath(fs->fs_file, realfsfile) == NULL) {
472                 /* Cannot resolve the path, use original one */
473                 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
474         }
475
476         for (i = mntsize - 1; i >= 0; --i)
477                 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
478                     (!isremountable(fs->fs_vfstype) ||
479                      strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
480                         return (1);
481         return (0);
482 }
483
484 int
485 isremountable(const char *vfsname)
486 {
487         const char **cp;
488
489         for (cp = remountable_fs_names; *cp; cp++)
490                 if (strcmp(*cp, vfsname) == 0)
491                         return (1);
492         return (0);
493 }
494
495 int
496 hasopt(const char *mntopts, const char *option)
497 {
498         int negative, found;
499         char *opt, *optbuf;
500
501         if (option[0] == 'n' && option[1] == 'o') {
502                 negative = 1;
503                 option += 2;
504         } else
505                 negative = 0;
506         optbuf = strdup(mntopts);
507         found = 0;
508         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
509                 if (opt[0] == 'n' && opt[1] == 'o') {
510                         if (!strcasecmp(opt + 2, option))
511                                 found = negative;
512                 } else if (!strcasecmp(opt, option))
513                         found = !negative;
514         }
515         free(optbuf);
516         return (found);
517 }
518
519 static void
520 append_arg(struct cpa *sa, char *arg)
521 {
522         if (sa->c + 1 == sa->sz) {
523                 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
524                 sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
525                 if (sa->a == NULL)
526                         errx(1, "realloc failed");
527         }
528         sa->a[++sa->c] = arg;
529 }
530
531 int
532 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
533         const char *options, const char *mntopts)
534 {
535         struct statfs sf;
536         int i, ret;
537         char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
538         static struct cpa mnt_argv;
539
540         /* resolve the mountpoint with realpath(3) */
541         (void)checkpath(name, mntpath);
542         name = mntpath;
543
544         if (mntopts == NULL)
545                 mntopts = "";
546         optbuf = catopt(strdup(mntopts), options);
547
548         if (strcmp(name, "/") == 0)
549                 flags |= MNT_UPDATE;
550         if (flags & MNT_FORCE)
551                 optbuf = catopt(optbuf, "force");
552         if (flags & MNT_RDONLY)
553                 optbuf = catopt(optbuf, "ro");
554         /*
555          * XXX
556          * The mount_mfs (newfs) command uses -o to select the
557          * optimization mode.  We don't pass the default "-o rw"
558          * for that reason.
559          */
560         if (flags & MNT_UPDATE)
561                 optbuf = catopt(optbuf, "update");
562
563         /* Compatibility glue. */
564         if (strcmp(vfstype, "msdos") == 0) {
565                 warnx(
566                     "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
567                 vfstype = "msdosfs";
568         }
569
570         /* Construct the name of the appropriate mount command */
571         (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
572
573         mnt_argv.c = -1;
574         append_arg(&mnt_argv, execname);
575         mangle(optbuf, &mnt_argv);
576         if (mountprog != NULL)
577                 strcpy(execname, mountprog);
578
579         append_arg(&mnt_argv, strdup(spec));
580         append_arg(&mnt_argv, strdup(name));
581         append_arg(&mnt_argv, NULL);
582
583         if (debug) {
584                 if (use_mountprog(vfstype))
585                         printf("exec: %s", execname);
586                 else
587                         printf("mount -t %s", vfstype);
588                 for (i = 1; i < mnt_argv.c; i++)
589                         (void)printf(" %s", mnt_argv.a[i]);
590                 (void)printf("\n");
591                 free(optbuf);
592                 free(mountprog);
593                 mountprog = NULL;
594                 return (0);
595         }
596
597         if (use_mountprog(vfstype)) {
598                 ret = exec_mountprog(name, execname, mnt_argv.a);
599         } else {
600                 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
601         }
602
603         free(optbuf);
604         free(mountprog);
605         mountprog = NULL;
606
607         if (verbose) {
608                 if (statfs(name, &sf) < 0) {
609                         warn("statfs %s", name);
610                         return (1);
611                 }
612                 if (fstab_style)
613                         putfsent(&sf);
614                 else
615                         prmount(&sf);
616         }
617
618         return (ret);
619 }
620
621 void
622 prmount(struct statfs *sfp)
623 {
624         uint64_t flags;
625         unsigned int i;
626         struct opt *o;
627         struct passwd *pw;
628
629         (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
630             sfp->f_fstypename);
631
632         flags = sfp->f_flags & MNT_VISFLAGMASK;
633         for (o = optnames; flags != 0 && o->o_opt != 0; o++)
634                 if (flags & o->o_opt) {
635                         (void)printf(", %s", o->o_name);
636                         flags &= ~o->o_opt;
637                 }
638         /*
639          * Inform when file system is mounted by an unprivileged user
640          * or privileged non-root user.
641          */
642         if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
643                 (void)printf(", mounted by ");
644                 if ((pw = getpwuid(sfp->f_owner)) != NULL)
645                         (void)printf("%s", pw->pw_name);
646                 else
647                         (void)printf("%d", sfp->f_owner);
648         }
649         if (verbose) {
650                 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
651                         (void)printf(", writes: sync %ju async %ju",
652                             (uintmax_t)sfp->f_syncwrites,
653                             (uintmax_t)sfp->f_asyncwrites);
654                 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
655                         (void)printf(", reads: sync %ju async %ju",
656                             (uintmax_t)sfp->f_syncreads,
657                             (uintmax_t)sfp->f_asyncreads);
658                 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
659                         printf(", fsid ");
660                         for (i = 0; i < sizeof(sfp->f_fsid); i++)
661                                 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
662                 }
663         }
664         (void)printf(")\n");
665 }
666
667 struct statfs *
668 getmntpt(const char *name)
669 {
670         struct statfs *mntbuf;
671         int i, mntsize;
672
673         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
674         for (i = mntsize - 1; i >= 0; i--) {
675                 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
676                     strcmp(mntbuf[i].f_mntonname, name) == 0)
677                         return (&mntbuf[i]);
678         }
679         return (NULL);
680 }
681
682 char *
683 catopt(char *s0, const char *s1)
684 {
685         size_t i;
686         char *cp;
687
688         if (s1 == NULL || *s1 == '\0')
689                 return (s0);
690
691         if (s0 && *s0) {
692                 i = strlen(s0) + strlen(s1) + 1 + 1;
693                 if ((cp = malloc(i)) == NULL)
694                         errx(1, "malloc failed");
695                 (void)snprintf(cp, i, "%s,%s", s0, s1);
696         } else
697                 cp = strdup(s1);
698
699         if (s0)
700                 free(s0);
701         return (cp);
702 }
703
704 void
705 mangle(char *options, struct cpa *a)
706 {
707         char *p, *s, *val;
708
709         for (s = options; (p = strsep(&s, ",")) != NULL;)
710                 if (*p != '\0') {
711                         if (strcmp(p, "noauto") == 0) {
712                                 /*
713                                  * Do not pass noauto option to nmount().
714                                  * or external mount program.  noauto is
715                                  * only used to prevent mounting a filesystem
716                                  * when 'mount -a' is specified, and is
717                                  * not a real mount option.
718                                  */
719                                 continue;
720                         } else if (strcmp(p, "late") == 0) {
721                                 /*
722                                  * "late" is used to prevent certain file
723                                  * systems from being mounted before late
724                                  * in the boot cycle; for instance,
725                                  * loopback NFS mounts can't be mounted
726                                  * before mountd starts.
727                                  */
728                                 continue;
729                         } else if (strcmp(p, "failok") == 0) {
730                                 /*
731                                  * "failok" is used to prevent certain file
732                                  * systems from being causing the system to
733                                  * drop into single user mode in the boot
734                                  * cycle, and is not a real mount option.
735                                  */
736                                 continue;
737                         } else if (strncmp(p, "mountprog", 9) == 0) {
738                                 /*
739                                  * "mountprog" is used to force the use of
740                                  * userland mount programs.
741                                  */
742                                 val = strchr(p, '=');
743                                 if (val != NULL) {
744                                         ++val;
745                                         if (*val != '\0')
746                                                 mountprog = strdup(val);
747                                 }
748
749                                 if (mountprog == NULL) {
750                                         errx(1, "Need value for -o mountprog");
751                                 }
752                                 continue;
753                         } else if (strcmp(p, "userquota") == 0) {
754                                 continue;
755                         } else if (strncmp(p, userquotaeq,
756                             sizeof(userquotaeq) - 1) == 0) {
757                                 continue;
758                         } else if (strcmp(p, "groupquota") == 0) {
759                                 continue;
760                         } else if (strncmp(p, groupquotaeq,
761                             sizeof(groupquotaeq) - 1) == 0) {
762                                 continue;
763                         } else if (*p == '-') {
764                                 append_arg(a, p);
765                                 p = strchr(p, '=');
766                                 if (p != NULL) {
767                                         *p = '\0';
768                                         append_arg(a, p + 1);
769                                 }
770                         } else {
771                                 append_arg(a, strdup("-o"));
772                                 append_arg(a, p);
773                         }
774                 }
775 }
776
777
778 char *
779 update_options(char *opts, char *fstab, int curflags)
780 {
781         char *o, *p;
782         char *cur;
783         char *expopt, *newopt, *tmpopt;
784
785         if (opts == NULL)
786                 return (strdup(""));
787
788         /* remove meta options from list */
789         remopt(fstab, MOUNT_META_OPTION_FSTAB);
790         remopt(fstab, MOUNT_META_OPTION_CURRENT);
791         cur = flags2opts(curflags);
792
793         /*
794          * Expand all meta-options passed to us first.
795          */
796         expopt = NULL;
797         for (p = opts; (o = strsep(&p, ",")) != NULL;) {
798                 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
799                         expopt = catopt(expopt, fstab);
800                 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
801                         expopt = catopt(expopt, cur);
802                 else
803                         expopt = catopt(expopt, o);
804         }
805         free(cur);
806         free(opts);
807
808         /*
809          * Remove previous contradictory arguments. Given option "foo" we
810          * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
811          * and "foo" - so we can deal with possible options like "notice".
812          */
813         newopt = NULL;
814         for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
815                 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
816                         errx(1, "malloc failed");
817
818                 strcpy(tmpopt, "no");
819                 strcat(tmpopt, o);
820                 remopt(newopt, tmpopt);
821                 free(tmpopt);
822
823                 if (strncmp("no", o, 2) == 0)
824                         remopt(newopt, o+2);
825
826                 newopt = catopt(newopt, o);
827         }
828         free(expopt);
829
830         return (newopt);
831 }
832
833 void
834 remopt(char *string, const char *opt)
835 {
836         char *o, *p, *r;
837
838         if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
839                 return;
840
841         r = string;
842
843         for (p = string; (o = strsep(&p, ",")) != NULL;) {
844                 if (strcmp(opt, o) != 0) {
845                         if (*r == ',' && *o != '\0')
846                                 r++;
847                         while ((*r++ = *o++) != '\0')
848                             ;
849                         *--r = ',';
850                 }
851         }
852         *r = '\0';
853 }
854
855 void
856 usage(void)
857 {
858
859         (void)fprintf(stderr, "%s\n%s\n%s\n",
860 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
861 "       mount [-dfpruvw] special | node",
862 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
863         exit(1);
864 }
865
866 void
867 putfsent(struct statfs *ent)
868 {
869         struct fstab *fst;
870         char *opts, *rw;
871         int l;
872
873         opts = NULL;
874         /* flags2opts() doesn't return the "rw" option. */
875         if ((ent->f_flags & MNT_RDONLY) != 0)
876                 rw = NULL;
877         else
878                 rw = catopt(NULL, "rw");
879
880         opts = flags2opts(ent->f_flags);
881         opts = catopt(rw, opts);
882
883         if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
884             strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
885                 strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
886                     +1));
887         }
888
889         l = strlen(ent->f_mntfromname);
890         printf("%s%s%s%s", ent->f_mntfromname,
891             l < 8 ? "\t" : "",
892             l < 16 ? "\t" : "",
893             l < 24 ? "\t" : " ");
894         l = strlen(ent->f_mntonname);
895         printf("%s%s%s%s", ent->f_mntonname,
896             l < 8 ? "\t" : "",
897             l < 16 ? "\t" : "",
898             l < 24 ? "\t" : " ");
899         printf("%s\t", ent->f_fstypename);
900         l = strlen(opts);
901         printf("%s%s", opts,
902             l < 8 ? "\t" : " ");
903         free(opts);
904
905         if ((fst = getfsspec(ent->f_mntfromname)))
906                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
907         else if ((fst = getfsfile(ent->f_mntonname)))
908                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
909         else if (strcmp(ent->f_fstypename, "ufs") == 0) {
910                 if (strcmp(ent->f_mntonname, "/") == 0)
911                         printf("\t1 1\n");
912                 else
913                         printf("\t2 2\n");
914         } else
915                 printf("\t0 0\n");
916 }
917
918
919 char *
920 flags2opts(int flags)
921 {
922         char *res;
923
924         res = NULL;
925
926         if (flags & MNT_RDONLY)         res = catopt(res, "ro");
927         if (flags & MNT_SYNCHRONOUS)    res = catopt(res, "sync");
928         if (flags & MNT_NOEXEC)         res = catopt(res, "noexec");
929         if (flags & MNT_NOSUID)         res = catopt(res, "nosuid");
930         if (flags & MNT_UNION)          res = catopt(res, "union");
931         if (flags & MNT_ASYNC)          res = catopt(res, "async");
932         if (flags & MNT_NOATIME)        res = catopt(res, "noatime");
933         if (flags & MNT_NOCLUSTERR)     res = catopt(res, "noclusterr");
934         if (flags & MNT_NOCLUSTERW)     res = catopt(res, "noclusterw");
935         if (flags & MNT_NOSYMFOLLOW)    res = catopt(res, "nosymfollow");
936         if (flags & MNT_SUIDDIR)        res = catopt(res, "suiddir");
937         if (flags & MNT_MULTILABEL)     res = catopt(res, "multilabel");
938         if (flags & MNT_ACLS)           res = catopt(res, "acls");
939         if (flags & MNT_NFS4ACLS)       res = catopt(res, "nfsv4acls");
940
941         return (res);
942 }