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
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
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/mountctl.h>
57 #include "pathnames.h"
60 #define MOUNT_META_OPTION_FSTAB "fstab"
61 #define MOUNT_META_OPTION_CURRENT "current"
63 int debug, fstab_style, verbose;
65 static char *catopt(char *, const char *);
67 *getmntpt(const char *);
68 static int hasopt(const char *, const char *);
69 static int ismounted(struct fstab *, struct statfs *, int);
70 static int isremountable(const char *);
71 static void mangle(char *, int *, const char **);
72 static char *update_options(char *, char *, int);
73 static int mountfs(const char *, const char *, const char *,
74 int, const char *, const char *);
75 static void remopt(char *, const char *);
76 static void printdefvals(const struct statfs *);
77 static void prmount(struct statfs *);
78 static void putfsent(const struct statfs *);
79 static void usage(void);
80 static char *flags2opts(int);
81 static char *xstrdup(const char *str);
84 int mount_ufs(int, const char **);
87 * List of VFS types that can be remounted without becoming mounted on top
89 * XXX Is this list correct?
92 remountable_fs_names[] = {
93 "ufs", "ffs", "ext2fs",
98 main(int argc, char **argv)
100 const char *mntfromname, **vfslist, *vfstype;
102 struct statfs *mntbuf;
105 int all, ch, i, init_flags, mntsize, rval, have_fstab;
108 all = init_flags = 0;
112 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
124 init_flags |= MNT_FORCE;
128 options = catopt(options, optarg);
135 options = catopt(options, "ro");
139 errx(1, "only one -t option may be specified");
140 vfslist = makevfslist(optarg);
144 init_flags |= MNT_UPDATE;
150 options = catopt(options, "noro");
160 #define BADTYPE(type) \
161 (strcmp(type, FSTAB_RO) && \
162 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
167 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
168 err(1, "getmntinfo");
170 while ((fs = getfsent()) != NULL) {
171 if (BADTYPE(fs->fs_type))
173 if (checkvfsname(fs->fs_vfstype, vfslist))
175 if (hasopt(fs->fs_mntops, "noauto"))
177 if (!(init_flags & MNT_UPDATE) &&
178 ismounted(fs, mntbuf, mntsize))
180 options = update_options(options,
181 fs->fs_mntops, mntbuf->f_flags);
182 if (mountfs(fs->fs_vfstype, fs->fs_spec,
183 fs->fs_file, init_flags, options,
187 } else if (fstab_style) {
188 for (i = 0; i < mntsize; i++) {
189 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
191 putfsent(&mntbuf[i]);
194 for (i = 0; i < mntsize; i++) {
195 if (checkvfsname(mntbuf[i].f_fstypename,
206 rmslashes(*argv, *argv);
208 if (init_flags & MNT_UPDATE) {
211 if ((mntbuf = getmntpt(*argv)) == NULL)
212 errx(1, "not currently mounted %s", *argv);
214 * Only get the mntflags from fstab if both mntpoint
215 * and mntspec are identical. Also handle the special
216 * case where just '/' is mounted and 'spec' is not
217 * identical with the one from fstab ('/dev' is missing
218 * in the spec-string at boot-time).
220 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
221 if (strcmp(fs->fs_spec,
222 mntbuf->f_mntfromname) == 0 &&
224 mntbuf->f_mntonname) == 0) {
226 mntfromname = mntbuf->f_mntfromname;
227 } else if (argv[0][0] == '/' &&
228 argv[0][1] == '\0') {
231 mntfromname = fs->fs_spec;
235 options = update_options(options, fs->fs_mntops,
238 mntfromname = mntbuf->f_mntfromname;
239 options = update_options(options, NULL,
242 rval = mountfs(mntbuf->f_fstypename, mntfromname,
243 mntbuf->f_mntonname, init_flags, options, 0);
246 if ((fs = getfsfile(*argv)) == NULL &&
247 (fs = getfsspec(*argv)) == NULL)
248 errx(1, "%s: unknown special file or file system",
250 if (BADTYPE(fs->fs_type))
251 errx(1, "%s has unknown file system type",
253 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
254 init_flags, options, fs->fs_mntops);
258 * If -t flag has not been specified, the path cannot be
259 * found, spec contains either a ':' or a '@', and the
260 * spec is not a file with those characters, then assume
261 * that an NFS filesystem is being specified ala Sun.
263 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
264 access(argv[0], 0) == -1)
266 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
267 init_flags, options, NULL);
275 * If the mount was successfully, and done by root, tell mountd the
276 * good news. Pid checks are probably unnecessary, but don't hurt.
278 if (rval == 0 && getuid() == 0 &&
279 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
280 if (fscanf(mountdfp, "%d", &pid) == 1 &&
281 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
282 err(1, "signal mountd");
290 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
294 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
295 /* the root file system can always be remounted */
298 for (i = mntsize - 1; i >= 0; --i)
299 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
300 (!isremountable(fs->fs_vfstype) ||
301 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
307 isremountable(const char *vfsname)
311 for (cp = remountable_fs_names; *cp; cp++)
312 if (strcmp(*cp, vfsname) == 0)
318 hasopt(const char *mntopts, const char *option)
323 if (option[0] == 'n' && option[1] == 'o') {
328 optbuf = xstrdup(mntopts);
330 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
331 if (opt[0] == 'n' && opt[1] == 'o') {
332 if (!strcasecmp(opt + 2, option))
334 } else if (!strcasecmp(opt, option))
342 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
343 const char *options, const char *mntopts)
345 /* List of directories containing mount_xxx subcommands. */
346 static const char *edirs[] = {
351 const char *argv[100], **edir;
355 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
362 /* resolve the mountpoint with realpath(3) */
363 checkpath(name, mntpath);
368 if (options == NULL) {
369 if (*mntopts == '\0') {
376 optbuf = catopt(xstrdup(mntopts), options);
378 if (strcmp(name, "/") == 0)
380 if (flags & MNT_FORCE)
381 optbuf = catopt(optbuf, "force");
382 if (flags & MNT_RDONLY)
383 optbuf = catopt(optbuf, "ro");
386 * The mount_mfs (newfs) command uses -o to select the
387 * optimization mode. We don't pass the default "-o rw"
390 if (flags & MNT_UPDATE)
391 optbuf = catopt(optbuf, "update");
394 argv[argc++] = vfstype;
395 mangle(optbuf, &argc, argv);
401 printf("exec: mount_%s", vfstype);
402 for (i = 1; i < argc; i++)
403 printf(" %s", argv[i]);
408 switch (pid = fork()) {
409 case -1: /* Error. */
414 if (strcmp(vfstype, "ufs") == 0)
415 exit(mount_ufs(argc, argv));
417 /* Go find an executable. */
418 for (edir = edirs; *edir; edir++) {
420 sizeof(execname), "%s/mount_%s", *edir, vfstype);
421 execv(execname, __DECONST(char * const *, argv));
423 if (errno == ENOENT) {
426 for (edir = edirs; *edir; edir++)
427 len += strlen(*edir) + 2; /* ", " */
428 if ((cp = malloc(len)) == NULL)
429 errx(1, "malloc failed");
431 for (edir = edirs; *edir; edir++) {
436 warn("exec mount_%s not found in %s", vfstype, cp);
440 default: /* Parent. */
443 if (waitpid(pid, &status, 0) < 0) {
448 if (WIFEXITED(status)) {
449 if (WEXITSTATUS(status) != 0)
450 return (WEXITSTATUS(status));
451 } else if (WIFSIGNALED(status)) {
452 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
457 if (statfs(name, &sf) < 0) {
458 warn("statfs %s", name);
473 prmount(struct statfs *sfp)
483 if ((buf = malloc(len)) == NULL)
484 errx(1, "malloc failed");
486 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
489 /* Get a string buffer with all the used flags names */
490 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
491 -1, NULL, 0, buf, len);
494 printf(", mounted by ");
495 if ((pw = getpwuid(sfp->f_owner)) != NULL)
496 printf("%s", pw->pw_name);
498 printf("%d", sfp->f_owner);
505 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
506 printf(", writes: sync %ld async %ld",
507 sfp->f_syncwrites, sfp->f_asyncwrites);
508 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
509 printf(", reads: sync %ld async %ld",
510 sfp->f_syncreads, sfp->f_asyncreads);
515 static struct statfs *
516 getmntpt(const char *name)
518 struct statfs *mntbuf;
521 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
522 for (i = mntsize - 1; i >= 0; i--) {
523 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
524 strcmp(mntbuf[i].f_mntonname, name) == 0)
531 catopt(char *s0, const char *s1)
536 if (s1 == NULL || *s1 == '\0')
540 i = strlen(s0) + strlen(s1) + 1 + 1;
541 if ((cp = malloc(i)) == NULL)
542 errx(1, "malloc failed");
543 snprintf(cp, i, "%s,%s", s0, s1);
553 mangle(char *options, int *argcp, const char **argv)
559 for (s = options; (p = strsep(&s, ",")) != NULL;)
568 } else if (strcmp(p, "rw") != 0) {
579 update_options(char *opts, char *fstab, int curflags)
583 char *expopt, *newopt, *tmpopt;
588 /* remove meta options from list */
589 remopt(fstab, MOUNT_META_OPTION_FSTAB);
590 remopt(fstab, MOUNT_META_OPTION_CURRENT);
591 cur = flags2opts(curflags);
594 * Expand all meta-options passed to us first.
597 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
598 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
599 expopt = catopt(expopt, fstab);
600 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
601 expopt = catopt(expopt, cur);
603 expopt = catopt(expopt, o);
609 * Remove previous contradictory arguments. Given option "foo" we
610 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
611 * and "foo" - so we can deal with possible options like "notice".
614 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
615 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
616 errx(1, "malloc failed");
618 strcpy(tmpopt, "no");
620 remopt(newopt, tmpopt);
623 if (strncmp("no", o, 2) == 0)
626 newopt = catopt(newopt, o);
634 remopt(char *string, const char *opt)
638 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
643 for (p = string; (o = strsep(&p, ",")) != NULL;) {
644 if (strcmp(opt, o) != 0) {
645 if (*r == ',' && *o != '\0')
647 while ((*r++ = *o++) != '\0')
659 fprintf(stderr, "%s\n%s\n%s\n",
660 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
661 " mount [-dfpruvw] {special | node}",
662 " mount [-dfpruvw] [-o options] [-t type] special node");
667 * Prints the default values for dump frequency and pass number of fsck.
668 * This happens when mount(8) is called with -p and there is no fstab file
669 * or there is but the values aren't specified.
672 printdefvals(const struct statfs *ent)
674 if (strcmp(ent->f_fstypename, "ufs") == 0) {
675 if (strcmp(ent->f_mntonname, "/") == 0)
685 putfsent(const struct statfs *ent)
691 opts = flags2opts(ent->f_flags);
692 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
693 ent->f_fstypename, opts);
697 * If fstab file is missing don't call getfsspec() or getfsfile()
698 * at all, because the same warning will be printed twice for every
699 * mounted filesystem.
701 if (stat(_PATH_FSTAB, &sb) != -1) {
702 if ((fst = getfsspec(ent->f_mntfromname)))
703 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
704 else if ((fst = getfsfile(ent->f_mntonname)))
705 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
715 flags2opts(int flags)
721 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
723 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
724 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
725 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
726 if (flags & MNT_NODEV) res = catopt(res, "nodev");
727 if (flags & MNT_UNION) res = catopt(res, "union");
728 if (flags & MNT_ASYNC) res = catopt(res, "async");
729 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
730 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
731 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
732 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
733 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
734 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
740 xstrdup(const char *str)
742 char* ret = strdup(str);
744 errx(1, "strdup failed (could not allocate memory)");