Small adjustment to committer(7): cherry-pick takes one argument.
[dragonfly.git] / sbin / mount / mount.c
blob71ef05b6dcfcf0ca5bb8e7f46005eb6e6af1def1
1 /*-
2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)mount.c 8.25 (Berkeley) 5/8/95
35 * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
36 * $DragonFly: src/sbin/mount/mount.c,v 1.10 2005/04/03 17:13:08 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <pwd.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 #include "extern.h"
55 #include "mntopts.h"
56 #include "pathnames.h"
58 /* `meta' options */
59 #define MOUNT_META_OPTION_FSTAB "fstab"
60 #define MOUNT_META_OPTION_CURRENT "current"
62 int debug, fstab_style, verbose;
64 static char *catopt(char *, const char *);
65 static struct statfs
66 *getmntpt(const char *);
67 static int hasopt(const char *, const char *);
68 static int ismounted(struct fstab *, struct statfs *, int);
69 static int isremountable(const char *);
70 static void mangle(char *, int *, const char **);
71 static char *update_options(char *, char *, int);
72 static int mountfs(const char *, const char *, const char *,
73 int, const char *, const char *);
74 static void remopt(char *, const char *);
75 static void printdefvals(const struct statfs *);
76 static void prmount(struct statfs *);
77 static void putfsent(const struct statfs *);
78 static void usage(void);
79 static char *flags2opts(int);
80 static char *xstrdup(const char *str);
82 /* Map from mount options to printable formats. */
83 static struct opt {
84 int o_opt;
85 const char *o_name;
86 } optnames[] = {
87 { MNT_ASYNC, "asynchronous" },
88 { MNT_EXPORTED, "NFS exported" },
89 { MNT_LOCAL, "local" },
90 { MNT_NOATIME, "noatime" },
91 { MNT_NODEV, "nodev" },
92 { MNT_NOEXEC, "noexec" },
93 { MNT_NOSUID, "nosuid" },
94 { MNT_NOSYMFOLLOW, "nosymfollow" },
95 { MNT_QUOTA, "with quotas" },
96 { MNT_RDONLY, "read-only" },
97 { MNT_SYNCHRONOUS, "synchronous" },
98 { MNT_UNION, "union" },
99 { MNT_NOCLUSTERR, "noclusterr" },
100 { MNT_NOCLUSTERW, "noclusterw" },
101 { MNT_SUIDDIR, "suiddir" },
102 { MNT_SOFTDEP, "soft-updates" },
103 { 0, NULL }
107 * List of VFS types that can be remounted without becoming mounted on top
108 * of each other.
109 * XXX Is this list correct?
111 static const char *
112 remountable_fs_names[] = {
113 "ufs", "ffs", "ext2fs",
118 main(int argc, char **argv)
120 const char *mntfromname, **vfslist, *vfstype;
121 struct fstab *fs;
122 struct statfs *mntbuf;
123 FILE *mountdfp;
124 pid_t pid;
125 int all, ch, i, init_flags, mntsize, rval, have_fstab;
126 char *options;
128 all = init_flags = 0;
129 options = NULL;
130 vfslist = NULL;
131 vfstype = "ufs";
132 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
133 switch (ch) {
134 case 'a':
135 all = 1;
136 break;
137 case 'd':
138 debug = 1;
139 break;
140 case 'f':
141 init_flags |= MNT_FORCE;
142 break;
143 case 'o':
144 if (*optarg)
145 options = catopt(options, optarg);
146 break;
147 case 'p':
148 fstab_style = 1;
149 verbose = 1;
150 break;
151 case 'r':
152 options = catopt(options, "ro");
153 break;
154 case 't':
155 if (vfslist != NULL)
156 errx(1, "only one -t option may be specified");
157 vfslist = makevfslist(optarg);
158 vfstype = optarg;
159 break;
160 case 'u':
161 init_flags |= MNT_UPDATE;
162 break;
163 case 'v':
164 verbose = 1;
165 break;
166 case 'w':
167 options = catopt(options, "noro");
168 break;
169 case '?':
170 default:
171 usage();
172 /* NOTREACHED */
174 argc -= optind;
175 argv += optind;
177 #define BADTYPE(type) \
178 (strcmp(type, FSTAB_RO) && \
179 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
181 rval = 0;
182 switch (argc) {
183 case 0:
184 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
185 err(1, "getmntinfo");
186 if (all) {
187 while ((fs = getfsent()) != NULL) {
188 if (BADTYPE(fs->fs_type))
189 continue;
190 if (checkvfsname(fs->fs_vfstype, vfslist))
191 continue;
192 if (hasopt(fs->fs_mntops, "noauto"))
193 continue;
194 if (!(init_flags & MNT_UPDATE) &&
195 ismounted(fs, mntbuf, mntsize))
196 continue;
197 options = update_options(options,
198 fs->fs_mntops, mntbuf->f_flags);
199 if (mountfs(fs->fs_vfstype, fs->fs_spec,
200 fs->fs_file, init_flags, options,
201 fs->fs_mntops))
202 rval = 1;
204 } else if (fstab_style) {
205 for (i = 0; i < mntsize; i++) {
206 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
207 continue;
208 putfsent(&mntbuf[i]);
210 } else {
211 for (i = 0; i < mntsize; i++) {
212 if (checkvfsname(mntbuf[i].f_fstypename,
213 vfslist))
214 continue;
215 prmount(&mntbuf[i]);
218 exit(rval);
219 case 1:
220 if (vfslist != NULL)
221 usage();
223 rmslashes(*argv, *argv);
225 if (init_flags & MNT_UPDATE) {
226 mntfromname = NULL;
227 have_fstab = 0;
228 if ((mntbuf = getmntpt(*argv)) == NULL)
229 errx(1, "not currently mounted %s", *argv);
231 * Only get the mntflags from fstab if both mntpoint
232 * and mntspec are identical. Also handle the special
233 * case where just '/' is mounted and 'spec' is not
234 * identical with the one from fstab ('/dev' is missing
235 * in the spec-string at boot-time).
237 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
238 if (strcmp(fs->fs_spec,
239 mntbuf->f_mntfromname) == 0 &&
240 strcmp(fs->fs_file,
241 mntbuf->f_mntonname) == 0) {
242 have_fstab = 1;
243 mntfromname = mntbuf->f_mntfromname;
244 } else if (argv[0][0] == '/' &&
245 argv[0][1] == '\0') {
246 fs = getfsfile("/");
247 have_fstab = 1;
248 mntfromname = fs->fs_spec;
251 if (have_fstab) {
252 options = update_options(options, fs->fs_mntops,
253 mntbuf->f_flags);
254 } else {
255 mntfromname = mntbuf->f_mntfromname;
256 options = update_options(options, NULL,
257 mntbuf->f_flags);
259 rval = mountfs(mntbuf->f_fstypename, mntfromname,
260 mntbuf->f_mntonname, init_flags, options, 0);
261 break;
263 if ((fs = getfsfile(*argv)) == NULL &&
264 (fs = getfsspec(*argv)) == NULL)
265 errx(1, "%s: unknown special file or file system",
266 *argv);
267 if (BADTYPE(fs->fs_type))
268 errx(1, "%s has unknown file system type",
269 *argv);
270 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
271 init_flags, options, fs->fs_mntops);
272 break;
273 case 2:
275 * If -t flag has not been specified, the path cannot be
276 * found, spec contains either a ':' or a '@', and the
277 * spec is not a file with those characters, then assume
278 * that an NFS filesystem is being specified ala Sun.
280 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
281 access(argv[0], 0) == -1)
282 vfstype = "nfs";
283 rval = mountfs(vfstype,
284 argv[0], argv[1], init_flags, options, NULL);
285 break;
286 default:
287 usage();
288 /* NOTREACHED */
292 * If the mount was successfully, and done by root, tell mountd the
293 * good news. Pid checks are probably unnecessary, but don't hurt.
295 if (rval == 0 && getuid() == 0 &&
296 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
297 if (fscanf(mountdfp, "%d", &pid) == 1 &&
298 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
299 err(1, "signal mountd");
300 fclose(mountdfp);
303 exit(rval);
306 static int
307 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
309 int i;
311 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
312 /* the root file system can always be remounted */
313 return (0);
315 for (i = mntsize - 1; i >= 0; --i)
316 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
317 (!isremountable(fs->fs_vfstype) ||
318 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
319 return (1);
320 return (0);
323 static int
324 isremountable(const char *vfsname)
326 const char **cp;
328 for (cp = remountable_fs_names; *cp; cp++)
329 if (strcmp(*cp, vfsname) == 0)
330 return (1);
331 return (0);
334 static int
335 hasopt(const char *mntopts, const char *option)
337 int negative, found;
338 char *opt, *optbuf;
340 if (option[0] == 'n' && option[1] == 'o') {
341 negative = 1;
342 option += 2;
343 } else
344 negative = 0;
345 optbuf = xstrdup(mntopts);
346 found = 0;
347 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
348 if (opt[0] == 'n' && opt[1] == 'o') {
349 if (!strcasecmp(opt + 2, option))
350 found = negative;
351 } else if (!strcasecmp(opt, option))
352 found = !negative;
354 free(optbuf);
355 return (found);
358 static int
359 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
360 const char *options, const char *mntopts)
362 /* List of directories containing mount_xxx subcommands. */
363 static const char *edirs[] = {
364 _PATH_SBIN,
365 _PATH_USRSBIN,
366 NULL
368 const char *argv[100], **edir;
369 struct statfs sf;
370 pid_t pid;
371 int argc, i, status;
372 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
374 #if __GNUC__
375 (void)&optbuf;
376 (void)&name;
377 #endif
379 /* resolve the mountpoint with realpath(3) */
380 checkpath(name, mntpath);
381 name = mntpath;
383 if (mntopts == NULL)
384 mntopts = "";
385 if (options == NULL) {
386 if (*mntopts == '\0') {
387 options = "rw";
388 } else {
389 options = mntopts;
390 mntopts = "";
393 optbuf = catopt(xstrdup(mntopts), options);
395 if (strcmp(name, "/") == 0)
396 flags |= MNT_UPDATE;
397 if (flags & MNT_FORCE)
398 optbuf = catopt(optbuf, "force");
399 if (flags & MNT_RDONLY)
400 optbuf = catopt(optbuf, "ro");
402 * XXX
403 * The mount_mfs (newfs) command uses -o to select the
404 * optimization mode. We don't pass the default "-o rw"
405 * for that reason.
407 if (flags & MNT_UPDATE)
408 optbuf = catopt(optbuf, "update");
410 argc = 0;
411 argv[argc++] = vfstype;
412 mangle(optbuf, &argc, argv);
413 argv[argc++] = spec;
414 argv[argc++] = name;
415 argv[argc] = NULL;
417 if (debug) {
418 printf("exec: mount_%s", vfstype);
419 for (i = 1; i < argc; i++)
420 printf(" %s", argv[i]);
421 printf("\n");
422 return (0);
425 switch (pid = fork()) {
426 case -1: /* Error. */
427 warn("fork");
428 free(optbuf);
429 return (1);
430 case 0: /* Child. */
431 if (strcmp(vfstype, "ufs") == 0)
432 exit(mount_ufs(argc, argv));
434 /* Go find an executable. */
435 for (edir = edirs; *edir; edir++) {
436 snprintf(execname,
437 sizeof(execname), "%s/mount_%s", *edir, vfstype);
438 execv(execname, __DECONST(char * const *, argv));
440 if (errno == ENOENT) {
441 int len = 0;
442 char *cp;
443 for (edir = edirs; *edir; edir++)
444 len += strlen(*edir) + 2; /* ", " */
445 if ((cp = malloc(len)) == NULL)
446 errx(1, "malloc failed");
447 cp[0] = '\0';
448 for (edir = edirs; *edir; edir++) {
449 strcat(cp, *edir);
450 if (edir[1] != NULL)
451 strcat(cp, ", ");
453 warn("exec mount_%s not found in %s", vfstype, cp);
455 exit(1);
456 /* NOTREACHED */
457 default: /* Parent. */
458 free(optbuf);
460 if (waitpid(pid, &status, 0) < 0) {
461 warn("waitpid");
462 return (1);
465 if (WIFEXITED(status)) {
466 if (WEXITSTATUS(status) != 0)
467 return (WEXITSTATUS(status));
468 } else if (WIFSIGNALED(status)) {
469 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
470 return (1);
473 if (verbose) {
474 if (statfs(name, &sf) < 0) {
475 warn("statfs %s", name);
476 return (1);
478 if (fstab_style)
479 putfsent(&sf);
480 else
481 prmount(&sf);
483 break;
486 return (0);
489 static void
490 prmount(struct statfs *sfp)
492 int flags;
493 struct opt *o;
494 struct passwd *pw;
496 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
497 sfp->f_fstypename);
499 flags = sfp->f_flags & MNT_VISFLAGMASK;
500 for (o = optnames; flags && o->o_opt; o++)
501 if (flags & o->o_opt) {
502 printf(", %s", o->o_name);
503 flags &= ~o->o_opt;
505 if (sfp->f_owner) {
506 printf(", mounted by ");
507 if ((pw = getpwuid(sfp->f_owner)) != NULL)
508 printf("%s", pw->pw_name);
509 else
510 printf("%d", sfp->f_owner);
512 if (verbose) {
513 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
514 printf(", writes: sync %ld async %ld",
515 sfp->f_syncwrites, sfp->f_asyncwrites);
516 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
517 printf(", reads: sync %ld async %ld",
518 sfp->f_syncreads, sfp->f_asyncreads);
520 printf(")\n");
523 static struct statfs *
524 getmntpt(const char *name)
526 struct statfs *mntbuf;
527 int i, mntsize;
529 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
530 for (i = mntsize - 1; i >= 0; i--) {
531 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
532 strcmp(mntbuf[i].f_mntonname, name) == 0)
533 return (&mntbuf[i]);
535 return (NULL);
538 static char *
539 catopt(char *s0, const char *s1)
541 size_t i;
542 char *cp;
544 if (s1 == NULL || *s1 == '\0')
545 return s0;
547 if (s0 && *s0) {
548 i = strlen(s0) + strlen(s1) + 1 + 1;
549 if ((cp = malloc(i)) == NULL)
550 errx(1, "malloc failed");
551 snprintf(cp, i, "%s,%s", s0, s1);
552 } else
553 cp = xstrdup(s1);
555 if (s0)
556 free(s0);
557 return (cp);
560 static void
561 mangle(char *options, int *argcp, const char **argv)
563 char *p, *s;
564 int argc;
566 argc = *argcp;
567 for (s = options; (p = strsep(&s, ",")) != NULL;)
568 if (*p != '\0') {
569 if (*p == '-') {
570 argv[argc++] = p;
571 p = strchr(p, '=');
572 if (p) {
573 *p = '\0';
574 argv[argc++] = p+1;
576 } else if (strcmp(p, "rw") != 0) {
577 argv[argc++] = "-o";
578 argv[argc++] = p;
582 *argcp = argc;
586 static char *
587 update_options(char *opts, char *fstab, int curflags)
589 char *o, *p;
590 char *cur;
591 char *expopt, *newopt, *tmpopt;
593 if (opts == NULL)
594 return xstrdup("");
596 /* remove meta options from list */
597 remopt(fstab, MOUNT_META_OPTION_FSTAB);
598 remopt(fstab, MOUNT_META_OPTION_CURRENT);
599 cur = flags2opts(curflags);
602 * Expand all meta-options passed to us first.
604 expopt = NULL;
605 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
606 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
607 expopt = catopt(expopt, fstab);
608 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
609 expopt = catopt(expopt, cur);
610 else
611 expopt = catopt(expopt, o);
613 free(cur);
614 free(opts);
617 * Remove previous contradictory arguments. Given option "foo" we
618 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
619 * and "foo" - so we can deal with possible options like "notice".
621 newopt = NULL;
622 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
623 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
624 errx(1, "malloc failed");
626 strcpy(tmpopt, "no");
627 strcat(tmpopt, o);
628 remopt(newopt, tmpopt);
629 free(tmpopt);
631 if (strncmp("no", o, 2) == 0)
632 remopt(newopt, o+2);
634 newopt = catopt(newopt, o);
636 free(expopt);
638 return newopt;
641 static void
642 remopt(char *string, const char *opt)
644 char *o, *p, *r;
646 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
647 return;
649 r = string;
651 for (p = string; (o = strsep(&p, ",")) != NULL;) {
652 if (strcmp(opt, o) != 0) {
653 if (*r == ',' && *o != '\0')
654 r++;
655 while ((*r++ = *o++) != '\0')
657 *--r = ',';
660 *r = '\0';
663 static void
664 usage(void)
667 fprintf(stderr, "%s\n%s\n%s\n",
668 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
669 " mount [-adfpruvw] [-o options] [-t ufs | external_type]",
670 " mount [-dfpruvw] special | node");
671 exit(1);
675 * Prints the default values for dump frequency and pass number of fsck.
676 * This happens when mount(8) is called with -p and there is no fstab file
677 * or there is but the values aren't specified.
679 static void
680 printdefvals(const struct statfs *ent)
682 if (strcmp(ent->f_fstypename, "ufs") == 0) {
683 if (strcmp(ent->f_mntonname, "/") == 0)
684 printf("\t1 1\n");
685 else
686 printf("\t2 2\n");
687 } else {
688 printf("\t0 0\n");
692 static void
693 putfsent(const struct statfs *ent)
695 struct stat sb;
696 struct fstab *fst;
697 char *opts;
699 opts = flags2opts(ent->f_flags);
700 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
701 ent->f_fstypename, opts);
702 free(opts);
705 * If fstab file is missing don't call getfsspec() or getfsfile()
706 * at all, because the same warning will be printed twice for every
707 * mounted filesystem.
709 if (stat(_PATH_FSTAB, &sb) != -1) {
710 if ((fst = getfsspec(ent->f_mntfromname)))
711 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
712 else if ((fst = getfsfile(ent->f_mntonname)))
713 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
714 else
715 printdefvals(ent);
716 } else {
717 printdefvals(ent);
722 static char *
723 flags2opts(int flags)
725 char *res;
727 res = NULL;
729 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
731 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
732 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
733 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
734 if (flags & MNT_NODEV) res = catopt(res, "nodev");
735 if (flags & MNT_UNION) res = catopt(res, "union");
736 if (flags & MNT_ASYNC) res = catopt(res, "async");
737 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
738 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
739 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
740 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
741 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
743 return res;
746 static char*
747 xstrdup(const char *str)
749 char* ret = strdup(str);
750 if(ret == NULL) {
751 errx(1, "strdup failed (could not allocate memory)");
753 return ret;