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