More minor IPI work.
[dragonfly/vkernel-mp.git] / sbin / mount / mount.c
blob3c127a777f8b44887e75af5f930ed5d28fc89e08
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 19: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 prmount(struct statfs *);
76 static void putfsent(const struct statfs *);
77 static void usage(void);
78 static char *flags2opts(int);
79 static char *xstrdup(const char *str);
81 /* Map from mount options to printable formats. */
82 static struct opt {
83 int o_opt;
84 const char *o_name;
85 } optnames[] = {
86 { MNT_ASYNC, "asynchronous" },
87 { MNT_EXPORTED, "NFS exported" },
88 { MNT_LOCAL, "local" },
89 { MNT_NOATIME, "noatime" },
90 { MNT_NODEV, "nodev" },
91 { MNT_NOEXEC, "noexec" },
92 { MNT_NOSUID, "nosuid" },
93 { MNT_NOSYMFOLLOW, "nosymfollow" },
94 { MNT_QUOTA, "with quotas" },
95 { MNT_RDONLY, "read-only" },
96 { MNT_SYNCHRONOUS, "synchronous" },
97 { MNT_UNION, "union" },
98 { MNT_NOCLUSTERR, "noclusterr" },
99 { MNT_NOCLUSTERW, "noclusterw" },
100 { MNT_SUIDDIR, "suiddir" },
101 { MNT_SOFTDEP, "soft-updates" },
102 { 0, NULL }
106 * List of VFS types that can be remounted without becoming mounted on top
107 * of each other.
108 * XXX Is this list correct?
110 static const char *
111 remountable_fs_names[] = {
112 "ufs", "ffs", "ext2fs",
117 main(int argc, char **argv)
119 const char *mntfromname, **vfslist, *vfstype;
120 struct fstab *fs;
121 struct statfs *mntbuf;
122 FILE *mountdfp;
123 pid_t pid;
124 int all, ch, i, init_flags, mntsize, rval, have_fstab;
125 char *options;
127 all = init_flags = 0;
128 options = NULL;
129 vfslist = NULL;
130 vfstype = "ufs";
131 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
132 switch (ch) {
133 case 'a':
134 all = 1;
135 break;
136 case 'd':
137 debug = 1;
138 break;
139 case 'f':
140 init_flags |= MNT_FORCE;
141 break;
142 case 'o':
143 if (*optarg)
144 options = catopt(options, optarg);
145 break;
146 case 'p':
147 fstab_style = 1;
148 verbose = 1;
149 break;
150 case 'r':
151 options = catopt(options, "ro");
152 break;
153 case 't':
154 if (vfslist != NULL)
155 errx(1, "only one -t option may be specified");
156 vfslist = makevfslist(optarg);
157 vfstype = optarg;
158 break;
159 case 'u':
160 init_flags |= MNT_UPDATE;
161 break;
162 case 'v':
163 verbose = 1;
164 break;
165 case 'w':
166 options = catopt(options, "noro");
167 break;
168 case '?':
169 default:
170 usage();
171 /* NOTREACHED */
173 argc -= optind;
174 argv += optind;
176 #define BADTYPE(type) \
177 (strcmp(type, FSTAB_RO) && \
178 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
180 rval = 0;
181 switch (argc) {
182 case 0:
183 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
184 err(1, "getmntinfo");
185 if (all) {
186 while ((fs = getfsent()) != NULL) {
187 if (BADTYPE(fs->fs_type))
188 continue;
189 if (checkvfsname(fs->fs_vfstype, vfslist))
190 continue;
191 if (hasopt(fs->fs_mntops, "noauto"))
192 continue;
193 if (!(init_flags & MNT_UPDATE) &&
194 ismounted(fs, mntbuf, mntsize))
195 continue;
196 options = update_options(options,
197 fs->fs_mntops, mntbuf->f_flags);
198 if (mountfs(fs->fs_vfstype, fs->fs_spec,
199 fs->fs_file, init_flags, options,
200 fs->fs_mntops))
201 rval = 1;
203 } else if (fstab_style) {
204 for (i = 0; i < mntsize; i++) {
205 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
206 continue;
207 putfsent(&mntbuf[i]);
209 } else {
210 for (i = 0; i < mntsize; i++) {
211 if (checkvfsname(mntbuf[i].f_fstypename,
212 vfslist))
213 continue;
214 prmount(&mntbuf[i]);
217 exit(rval);
218 case 1:
219 if (vfslist != NULL)
220 usage();
222 rmslashes(*argv, *argv);
224 if (init_flags & MNT_UPDATE) {
225 mntfromname = NULL;
226 have_fstab = 0;
227 if ((mntbuf = getmntpt(*argv)) == NULL)
228 errx(1, "not currently mounted %s", *argv);
230 * Only get the mntflags from fstab if both mntpoint
231 * and mntspec are identical. Also handle the special
232 * case where just '/' is mounted and 'spec' is not
233 * identical with the one from fstab ('/dev' is missing
234 * in the spec-string at boot-time).
236 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
237 if (strcmp(fs->fs_spec,
238 mntbuf->f_mntfromname) == 0 &&
239 strcmp(fs->fs_file,
240 mntbuf->f_mntonname) == 0) {
241 have_fstab = 1;
242 mntfromname = mntbuf->f_mntfromname;
243 } else if (argv[0][0] == '/' &&
244 argv[0][1] == '\0') {
245 fs = getfsfile("/");
246 have_fstab = 1;
247 mntfromname = fs->fs_spec;
250 if (have_fstab) {
251 options = update_options(options, fs->fs_mntops,
252 mntbuf->f_flags);
253 } else {
254 mntfromname = mntbuf->f_mntfromname;
255 options = update_options(options, NULL,
256 mntbuf->f_flags);
258 rval = mountfs(mntbuf->f_fstypename, mntfromname,
259 mntbuf->f_mntonname, init_flags, options, 0);
260 break;
262 if ((fs = getfsfile(*argv)) == NULL &&
263 (fs = getfsspec(*argv)) == NULL)
264 errx(1, "%s: unknown special file or file system",
265 *argv);
266 if (BADTYPE(fs->fs_type))
267 errx(1, "%s has unknown file system type",
268 *argv);
269 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
270 init_flags, options, fs->fs_mntops);
271 break;
272 case 2:
274 * If -t flag has not been specified, the path cannot be
275 * found, spec contains either a ':' or a '@', and the
276 * spec is not a file with those characters, then assume
277 * that an NFS filesystem is being specified ala Sun.
279 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
280 access(argv[0], 0) == -1)
281 vfstype = "nfs";
282 rval = mountfs(vfstype,
283 argv[0], argv[1], init_flags, options, NULL);
284 break;
285 default:
286 usage();
287 /* NOTREACHED */
291 * If the mount was successfully, and done by root, tell mountd the
292 * good news. Pid checks are probably unnecessary, but don't hurt.
294 if (rval == 0 && getuid() == 0 &&
295 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
296 if (fscanf(mountdfp, "%d", &pid) == 1 &&
297 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
298 err(1, "signal mountd");
299 fclose(mountdfp);
302 exit(rval);
305 static int
306 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
308 int i;
310 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
311 /* the root file system can always be remounted */
312 return (0);
314 for (i = mntsize - 1; i >= 0; --i)
315 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
316 (!isremountable(fs->fs_vfstype) ||
317 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
318 return (1);
319 return (0);
322 static int
323 isremountable(const char *vfsname)
325 const char **cp;
327 for (cp = remountable_fs_names; *cp; cp++)
328 if (strcmp(*cp, vfsname) == 0)
329 return (1);
330 return (0);
333 static int
334 hasopt(const char *mntopts, const char *option)
336 int negative, found;
337 char *opt, *optbuf;
339 if (option[0] == 'n' && option[1] == 'o') {
340 negative = 1;
341 option += 2;
342 } else
343 negative = 0;
344 optbuf = xstrdup(mntopts);
345 found = 0;
346 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
347 if (opt[0] == 'n' && opt[1] == 'o') {
348 if (!strcasecmp(opt + 2, option))
349 found = negative;
350 } else if (!strcasecmp(opt, option))
351 found = !negative;
353 free(optbuf);
354 return (found);
357 static int
358 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
359 const char *options, const char *mntopts)
361 /* List of directories containing mount_xxx subcommands. */
362 static const char *edirs[] = {
363 _PATH_SBIN,
364 _PATH_USRSBIN,
365 NULL
367 const char *argv[100], **edir;
368 struct statfs sf;
369 pid_t pid;
370 int argc, i, status;
371 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
373 #if __GNUC__
374 (void)&optbuf;
375 (void)&name;
376 #endif
378 /* resolve the mountpoint with realpath(3) */
379 checkpath(name, mntpath);
380 name = mntpath;
382 if (mntopts == NULL)
383 mntopts = "";
384 if (options == NULL) {
385 if (*mntopts == '\0') {
386 options = "rw";
387 } else {
388 options = mntopts;
389 mntopts = "";
392 optbuf = catopt(xstrdup(mntopts), options);
394 if (strcmp(name, "/") == 0)
395 flags |= MNT_UPDATE;
396 if (flags & MNT_FORCE)
397 optbuf = catopt(optbuf, "force");
398 if (flags & MNT_RDONLY)
399 optbuf = catopt(optbuf, "ro");
401 * XXX
402 * The mount_mfs (newfs) command uses -o to select the
403 * optimization mode. We don't pass the default "-o rw"
404 * for that reason.
406 if (flags & MNT_UPDATE)
407 optbuf = catopt(optbuf, "update");
409 argc = 0;
410 argv[argc++] = vfstype;
411 mangle(optbuf, &argc, argv);
412 argv[argc++] = spec;
413 argv[argc++] = name;
414 argv[argc] = NULL;
416 if (debug) {
417 printf("exec: mount_%s", vfstype);
418 for (i = 1; i < argc; i++)
419 printf(" %s", argv[i]);
420 printf("\n");
421 return (0);
424 switch (pid = fork()) {
425 case -1: /* Error. */
426 warn("fork");
427 free(optbuf);
428 return (1);
429 case 0: /* Child. */
430 if (strcmp(vfstype, "ufs") == 0)
431 exit(mount_ufs(argc, argv));
433 /* Go find an executable. */
434 for (edir = edirs; *edir; edir++) {
435 snprintf(execname,
436 sizeof(execname), "%s/mount_%s", *edir, vfstype);
437 execv(execname, __DECONST(char * const *, argv));
439 if (errno == ENOENT) {
440 int len = 0;
441 char *cp;
442 for (edir = edirs; *edir; edir++)
443 len += strlen(*edir) + 2; /* ", " */
444 if ((cp = malloc(len)) == NULL)
445 errx(1, "malloc failed");
446 cp[0] = '\0';
447 for (edir = edirs; *edir; edir++) {
448 strcat(cp, *edir);
449 if (edir[1] != NULL)
450 strcat(cp, ", ");
452 warn("exec mount_%s not found in %s", vfstype, cp);
454 exit(1);
455 /* NOTREACHED */
456 default: /* Parent. */
457 free(optbuf);
459 if (waitpid(pid, &status, 0) < 0) {
460 warn("waitpid");
461 return (1);
464 if (WIFEXITED(status)) {
465 if (WEXITSTATUS(status) != 0)
466 return (WEXITSTATUS(status));
467 } else if (WIFSIGNALED(status)) {
468 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
469 return (1);
472 if (verbose) {
473 if (statfs(name, &sf) < 0) {
474 warn("statfs %s", name);
475 return (1);
477 if (fstab_style)
478 putfsent(&sf);
479 else
480 prmount(&sf);
482 break;
485 return (0);
488 static void
489 prmount(struct statfs *sfp)
491 int flags;
492 struct opt *o;
493 struct passwd *pw;
495 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
496 sfp->f_fstypename);
498 flags = sfp->f_flags & MNT_VISFLAGMASK;
499 for (o = optnames; flags && o->o_opt; o++)
500 if (flags & o->o_opt) {
501 printf(", %s", o->o_name);
502 flags &= ~o->o_opt;
504 if (sfp->f_owner) {
505 printf(", mounted by ");
506 if ((pw = getpwuid(sfp->f_owner)) != NULL)
507 printf("%s", pw->pw_name);
508 else
509 printf("%d", sfp->f_owner);
511 if (verbose) {
512 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
513 printf(", writes: sync %ld async %ld",
514 sfp->f_syncwrites, sfp->f_asyncwrites);
515 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
516 printf(", reads: sync %ld async %ld",
517 sfp->f_syncreads, sfp->f_asyncreads);
519 printf(")\n");
522 static struct statfs *
523 getmntpt(const char *name)
525 struct statfs *mntbuf;
526 int i, mntsize;
528 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
529 for (i = mntsize - 1; i >= 0; i--) {
530 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
531 strcmp(mntbuf[i].f_mntonname, name) == 0)
532 return (&mntbuf[i]);
534 return (NULL);
537 static char *
538 catopt(char *s0, const char *s1)
540 size_t i;
541 char *cp;
543 if (s1 == NULL || *s1 == '\0')
544 return s0;
546 if (s0 && *s0) {
547 i = strlen(s0) + strlen(s1) + 1 + 1;
548 if ((cp = malloc(i)) == NULL)
549 errx(1, "malloc failed");
550 snprintf(cp, i, "%s,%s", s0, s1);
551 } else
552 cp = xstrdup(s1);
554 if (s0)
555 free(s0);
556 return (cp);
559 static void
560 mangle(char *options, int *argcp, const char **argv)
562 char *p, *s;
563 int argc;
565 argc = *argcp;
566 for (s = options; (p = strsep(&s, ",")) != NULL;)
567 if (*p != '\0') {
568 if (*p == '-') {
569 argv[argc++] = p;
570 p = strchr(p, '=');
571 if (p) {
572 *p = '\0';
573 argv[argc++] = p+1;
575 } else if (strcmp(p, "rw") != 0) {
576 argv[argc++] = "-o";
577 argv[argc++] = p;
581 *argcp = argc;
585 static char *
586 update_options(char *opts, char *fstab, int curflags)
588 char *o, *p;
589 char *cur;
590 char *expopt, *newopt, *tmpopt;
592 if (opts == NULL)
593 return xstrdup("");
595 /* remove meta options from list */
596 remopt(fstab, MOUNT_META_OPTION_FSTAB);
597 remopt(fstab, MOUNT_META_OPTION_CURRENT);
598 cur = flags2opts(curflags);
601 * Expand all meta-options passed to us first.
603 expopt = NULL;
604 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
605 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
606 expopt = catopt(expopt, fstab);
607 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
608 expopt = catopt(expopt, cur);
609 else
610 expopt = catopt(expopt, o);
612 free(cur);
613 free(opts);
616 * Remove previous contradictory arguments. Given option "foo" we
617 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
618 * and "foo" - so we can deal with possible options like "notice".
620 newopt = NULL;
621 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
622 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
623 errx(1, "malloc failed");
625 strcpy(tmpopt, "no");
626 strcat(tmpopt, o);
627 remopt(newopt, tmpopt);
628 free(tmpopt);
630 if (strncmp("no", o, 2) == 0)
631 remopt(newopt, o+2);
633 newopt = catopt(newopt, o);
635 free(expopt);
637 return newopt;
640 static void
641 remopt(char *string, const char *opt)
643 char *o, *p, *r;
645 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
646 return;
648 r = string;
650 for (p = string; (o = strsep(&p, ",")) != NULL;) {
651 if (strcmp(opt, o) != 0) {
652 if (*r == ',' && *o != '\0')
653 r++;
654 while ((*r++ = *o++) != '\0')
656 *--r = ',';
659 *r = '\0';
662 static void
663 usage(void)
666 fprintf(stderr, "%s\n%s\n%s\n",
667 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
668 " mount [-adfpruvw] [-o options] [-t ufs | external_type]",
669 " mount [-dfpruvw] special | node");
670 exit(1);
673 static void
674 putfsent(const struct statfs *ent)
676 struct fstab *fst;
677 char *opts;
679 opts = flags2opts(ent->f_flags);
680 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
681 ent->f_fstypename, opts);
682 free(opts);
684 if ((fst = getfsspec(ent->f_mntfromname)))
685 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
686 else if ((fst = getfsfile(ent->f_mntonname)))
687 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
688 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
689 if (strcmp(ent->f_mntonname, "/") == 0)
690 printf("\t1 1\n");
691 else
692 printf("\t2 2\n");
693 } else
694 printf("\t0 0\n");
698 static char *
699 flags2opts(int flags)
701 char *res;
703 res = NULL;
705 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
707 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
708 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
709 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
710 if (flags & MNT_NODEV) res = catopt(res, "nodev");
711 if (flags & MNT_UNION) res = catopt(res, "union");
712 if (flags & MNT_ASYNC) res = catopt(res, "async");
713 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
714 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
715 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
716 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
717 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
719 return res;
722 static char*
723 xstrdup(const char *str)
725 char* ret = strdup(str);
726 if(ret == NULL) {
727 errx(1, "strdup failed (could not allocate memory)");
729 return ret;