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