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