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