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