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>
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
);
83 /* Map from mount options to printable formats. */
88 { MNT_ASYNC
, "asynchronous" },
89 { MNT_EXPORTED
, "NFS exported" },
90 { MNT_LOCAL
, "local" },
91 { MNT_NOATIME
, "noatime" },
92 { MNT_NODEV
, "nodev" },
93 { MNT_NOEXEC
, "noexec" },
94 { MNT_NOSUID
, "nosuid" },
95 { MNT_NOSYMFOLLOW
, "nosymfollow" },
96 { MNT_QUOTA
, "with quotas" },
97 { MNT_RDONLY
, "read-only" },
98 { MNT_SYNCHRONOUS
, "synchronous" },
99 { MNT_UNION
, "union" },
100 { MNT_NOCLUSTERR
, "noclusterr" },
101 { MNT_NOCLUSTERW
, "noclusterw" },
102 { MNT_SUIDDIR
, "suiddir" },
103 { MNT_SOFTDEP
, "soft-updates" },
104 { MNT_IGNORE
, "ignore" },
109 * List of VFS types that can be remounted without becoming mounted on top
111 * XXX Is this list correct?
114 remountable_fs_names
[] = {
115 "ufs", "ffs", "ext2fs",
120 main(int argc
, char **argv
)
122 const char *mntfromname
, **vfslist
, *vfstype
;
124 struct statfs
*mntbuf
;
127 int all
, ch
, i
, init_flags
, mntsize
, rval
, have_fstab
;
130 all
= init_flags
= 0;
134 while ((ch
= getopt(argc
, argv
, "adfo:prwt:uv")) != -1)
143 init_flags
|= MNT_FORCE
;
147 options
= catopt(options
, optarg
);
154 options
= catopt(options
, "ro");
158 errx(1, "only one -t option may be specified");
159 vfslist
= makevfslist(optarg
);
163 init_flags
|= MNT_UPDATE
;
169 options
= catopt(options
, "noro");
179 #define BADTYPE(type) \
180 (strcmp(type, FSTAB_RO) && \
181 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
186 if ((mntsize
= getmntinfo(&mntbuf
, MNT_NOWAIT
)) == 0)
187 err(1, "getmntinfo");
189 while ((fs
= getfsent()) != NULL
) {
190 if (BADTYPE(fs
->fs_type
))
192 if (checkvfsname(fs
->fs_vfstype
, vfslist
))
194 if (hasopt(fs
->fs_mntops
, "noauto"))
196 if (!(init_flags
& MNT_UPDATE
) &&
197 ismounted(fs
, mntbuf
, mntsize
))
199 options
= update_options(options
,
200 fs
->fs_mntops
, mntbuf
->f_flags
);
201 if (mountfs(fs
->fs_vfstype
, fs
->fs_spec
,
202 fs
->fs_file
, init_flags
, options
,
206 } else if (fstab_style
) {
207 for (i
= 0; i
< mntsize
; i
++) {
208 if (checkvfsname(mntbuf
[i
].f_fstypename
, vfslist
))
210 putfsent(&mntbuf
[i
]);
213 for (i
= 0; i
< mntsize
; i
++) {
214 if (checkvfsname(mntbuf
[i
].f_fstypename
,
225 rmslashes(*argv
, *argv
);
227 if (init_flags
& MNT_UPDATE
) {
230 if ((mntbuf
= getmntpt(*argv
)) == NULL
)
231 errx(1, "not currently mounted %s", *argv
);
233 * Only get the mntflags from fstab if both mntpoint
234 * and mntspec are identical. Also handle the special
235 * case where just '/' is mounted and 'spec' is not
236 * identical with the one from fstab ('/dev' is missing
237 * in the spec-string at boot-time).
239 if ((fs
= getfsfile(mntbuf
->f_mntonname
)) != NULL
) {
240 if (strcmp(fs
->fs_spec
,
241 mntbuf
->f_mntfromname
) == 0 &&
243 mntbuf
->f_mntonname
) == 0) {
245 mntfromname
= mntbuf
->f_mntfromname
;
246 } else if (argv
[0][0] == '/' &&
247 argv
[0][1] == '\0') {
250 mntfromname
= fs
->fs_spec
;
254 options
= update_options(options
, fs
->fs_mntops
,
257 mntfromname
= mntbuf
->f_mntfromname
;
258 options
= update_options(options
, NULL
,
261 rval
= mountfs(mntbuf
->f_fstypename
, mntfromname
,
262 mntbuf
->f_mntonname
, init_flags
, options
, 0);
265 if ((fs
= getfsfile(*argv
)) == NULL
&&
266 (fs
= getfsspec(*argv
)) == NULL
)
267 errx(1, "%s: unknown special file or file system",
269 if (BADTYPE(fs
->fs_type
))
270 errx(1, "%s has unknown file system type",
272 rval
= mountfs(fs
->fs_vfstype
, fs
->fs_spec
, fs
->fs_file
,
273 init_flags
, options
, fs
->fs_mntops
);
277 * If -t flag has not been specified, the path cannot be
278 * found, spec contains either a ':' or a '@', and the
279 * spec is not a file with those characters, then assume
280 * that an NFS filesystem is being specified ala Sun.
282 if (vfslist
== NULL
&& strpbrk(argv
[0], ":@") != NULL
&&
283 access(argv
[0], 0) == -1)
285 rval
= mountfs(vfstype
, getdevpath(argv
[0], 0), argv
[1],
286 init_flags
, options
, NULL
);
294 * If the mount was successfully, and done by root, tell mountd the
295 * good news. Pid checks are probably unnecessary, but don't hurt.
297 if (rval
== 0 && getuid() == 0 &&
298 (mountdfp
= fopen(_PATH_MOUNTDPID
, "r")) != NULL
) {
299 if (fscanf(mountdfp
, "%d", &pid
) == 1 &&
300 pid
> 0 && kill(pid
, SIGHUP
) == -1 && errno
!= ESRCH
)
301 err(1, "signal mountd");
309 ismounted(struct fstab
*fs
, struct statfs
*mntbuf
, int mntsize
)
313 if (fs
->fs_file
[0] == '/' && fs
->fs_file
[1] == '\0')
314 /* the root file system can always be remounted */
317 for (i
= mntsize
- 1; i
>= 0; --i
)
318 if (strcmp(fs
->fs_file
, mntbuf
[i
].f_mntonname
) == 0 &&
319 (!isremountable(fs
->fs_vfstype
) ||
320 strcmp(fs
->fs_spec
, mntbuf
[i
].f_mntfromname
) == 0))
326 isremountable(const char *vfsname
)
330 for (cp
= remountable_fs_names
; *cp
; cp
++)
331 if (strcmp(*cp
, vfsname
) == 0)
337 hasopt(const char *mntopts
, const char *option
)
342 if (option
[0] == 'n' && option
[1] == 'o') {
347 optbuf
= xstrdup(mntopts
);
349 for (opt
= optbuf
; (opt
= strtok(opt
, ",")) != NULL
; opt
= NULL
) {
350 if (opt
[0] == 'n' && opt
[1] == 'o') {
351 if (!strcasecmp(opt
+ 2, option
))
353 } else if (!strcasecmp(opt
, option
))
361 mountfs(const char *vfstype
, const char *spec
, const char *name
, int flags
,
362 const char *options
, const char *mntopts
)
364 /* List of directories containing mount_xxx subcommands. */
365 static const char *edirs
[] = {
370 const char *argv
[100], **edir
;
374 char *optbuf
, execname
[MAXPATHLEN
+ 1], mntpath
[MAXPATHLEN
];
381 /* resolve the mountpoint with realpath(3) */
382 checkpath(name
, mntpath
);
387 if (options
== NULL
) {
388 if (*mntopts
== '\0') {
395 optbuf
= catopt(xstrdup(mntopts
), options
);
397 if (strcmp(name
, "/") == 0)
399 if (flags
& MNT_FORCE
)
400 optbuf
= catopt(optbuf
, "force");
401 if (flags
& MNT_RDONLY
)
402 optbuf
= catopt(optbuf
, "ro");
405 * The mount_mfs (newfs) command uses -o to select the
406 * optimization mode. We don't pass the default "-o rw"
409 if (flags
& MNT_UPDATE
)
410 optbuf
= catopt(optbuf
, "update");
413 argv
[argc
++] = vfstype
;
414 mangle(optbuf
, &argc
, argv
);
420 printf("exec: mount_%s", vfstype
);
421 for (i
= 1; i
< argc
; i
++)
422 printf(" %s", argv
[i
]);
427 switch (pid
= fork()) {
428 case -1: /* Error. */
433 if (strcmp(vfstype
, "ufs") == 0)
434 exit(mount_ufs(argc
, argv
));
436 /* Go find an executable. */
437 for (edir
= edirs
; *edir
; edir
++) {
439 sizeof(execname
), "%s/mount_%s", *edir
, vfstype
);
440 execv(execname
, __DECONST(char * const *, argv
));
442 if (errno
== ENOENT
) {
445 for (edir
= edirs
; *edir
; edir
++)
446 len
+= strlen(*edir
) + 2; /* ", " */
447 if ((cp
= malloc(len
)) == NULL
)
448 errx(1, "malloc failed");
450 for (edir
= edirs
; *edir
; edir
++) {
455 warn("exec mount_%s not found in %s", vfstype
, cp
);
459 default: /* Parent. */
462 if (waitpid(pid
, &status
, 0) < 0) {
467 if (WIFEXITED(status
)) {
468 if (WEXITSTATUS(status
) != 0)
469 return (WEXITSTATUS(status
));
470 } else if (WIFSIGNALED(status
)) {
471 warnx("%s: %s", name
, sys_siglist
[WTERMSIG(status
)]);
476 if (statfs(name
, &sf
) < 0) {
477 warn("statfs %s", name
);
492 prmount(struct statfs
*sfp
)
498 printf("%s on %s (%s", sfp
->f_mntfromname
, sfp
->f_mntonname
,
501 flags
= sfp
->f_flags
& MNT_VISFLAGMASK
;
502 for (o
= optnames
; flags
&& o
->o_opt
; o
++)
503 if (flags
& o
->o_opt
) {
504 printf(", %s", o
->o_name
);
508 printf(", mounted by ");
509 if ((pw
= getpwuid(sfp
->f_owner
)) != NULL
)
510 printf("%s", pw
->pw_name
);
512 printf("%d", sfp
->f_owner
);
515 if (sfp
->f_syncwrites
!= 0 || sfp
->f_asyncwrites
!= 0)
516 printf(", writes: sync %ld async %ld",
517 sfp
->f_syncwrites
, sfp
->f_asyncwrites
);
518 if (sfp
->f_syncreads
!= 0 || sfp
->f_asyncreads
!= 0)
519 printf(", reads: sync %ld async %ld",
520 sfp
->f_syncreads
, sfp
->f_asyncreads
);
525 static struct statfs
*
526 getmntpt(const char *name
)
528 struct statfs
*mntbuf
;
531 mntsize
= getmntinfo(&mntbuf
, MNT_NOWAIT
);
532 for (i
= mntsize
- 1; i
>= 0; i
--) {
533 if (strcmp(mntbuf
[i
].f_mntfromname
, name
) == 0 ||
534 strcmp(mntbuf
[i
].f_mntonname
, name
) == 0)
541 catopt(char *s0
, const char *s1
)
546 if (s1
== NULL
|| *s1
== '\0')
550 i
= strlen(s0
) + strlen(s1
) + 1 + 1;
551 if ((cp
= malloc(i
)) == NULL
)
552 errx(1, "malloc failed");
553 snprintf(cp
, i
, "%s,%s", s0
, s1
);
563 mangle(char *options
, int *argcp
, const char **argv
)
569 for (s
= options
; (p
= strsep(&s
, ",")) != NULL
;)
578 } else if (strcmp(p
, "rw") != 0) {
589 update_options(char *opts
, char *fstab
, int curflags
)
593 char *expopt
, *newopt
, *tmpopt
;
598 /* remove meta options from list */
599 remopt(fstab
, MOUNT_META_OPTION_FSTAB
);
600 remopt(fstab
, MOUNT_META_OPTION_CURRENT
);
601 cur
= flags2opts(curflags
);
604 * Expand all meta-options passed to us first.
607 for (p
= opts
; (o
= strsep(&p
, ",")) != NULL
;) {
608 if (strcmp(MOUNT_META_OPTION_FSTAB
, o
) == 0)
609 expopt
= catopt(expopt
, fstab
);
610 else if (strcmp(MOUNT_META_OPTION_CURRENT
, o
) == 0)
611 expopt
= catopt(expopt
, cur
);
613 expopt
= catopt(expopt
, o
);
619 * Remove previous contradictory arguments. Given option "foo" we
620 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
621 * and "foo" - so we can deal with possible options like "notice".
624 for (p
= expopt
; (o
= strsep(&p
, ",")) != NULL
;) {
625 if ((tmpopt
= malloc( strlen(o
) + 2 + 1 )) == NULL
)
626 errx(1, "malloc failed");
628 strcpy(tmpopt
, "no");
630 remopt(newopt
, tmpopt
);
633 if (strncmp("no", o
, 2) == 0)
636 newopt
= catopt(newopt
, o
);
644 remopt(char *string
, const char *opt
)
648 if (string
== NULL
|| *string
== '\0' || opt
== NULL
|| *opt
== '\0')
653 for (p
= string
; (o
= strsep(&p
, ",")) != NULL
;) {
654 if (strcmp(opt
, o
) != 0) {
655 if (*r
== ',' && *o
!= '\0')
657 while ((*r
++ = *o
++) != '\0')
669 fprintf(stderr
, "%s\n%s\n%s\n",
670 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
671 " mount [-adfpruvw] [-o options] [-t ufs | external_type]",
672 " mount [-dfpruvw] special | node");
677 * Prints the default values for dump frequency and pass number of fsck.
678 * This happens when mount(8) is called with -p and there is no fstab file
679 * or there is but the values aren't specified.
682 printdefvals(const struct statfs
*ent
)
684 if (strcmp(ent
->f_fstypename
, "ufs") == 0) {
685 if (strcmp(ent
->f_mntonname
, "/") == 0)
695 putfsent(const struct statfs
*ent
)
701 opts
= flags2opts(ent
->f_flags
);
702 printf("%s\t%s\t%s %s", ent
->f_mntfromname
, ent
->f_mntonname
,
703 ent
->f_fstypename
, opts
);
707 * If fstab file is missing don't call getfsspec() or getfsfile()
708 * at all, because the same warning will be printed twice for every
709 * mounted filesystem.
711 if (stat(_PATH_FSTAB
, &sb
) != -1) {
712 if ((fst
= getfsspec(ent
->f_mntfromname
)))
713 printf("\t%u %u\n", fst
->fs_freq
, fst
->fs_passno
);
714 else if ((fst
= getfsfile(ent
->f_mntonname
)))
715 printf("\t%u %u\n", fst
->fs_freq
, fst
->fs_passno
);
725 flags2opts(int flags
)
731 res
= catopt(res
, (flags
& MNT_RDONLY
) ? "ro" : "rw");
733 if (flags
& MNT_SYNCHRONOUS
) res
= catopt(res
, "sync");
734 if (flags
& MNT_NOEXEC
) res
= catopt(res
, "noexec");
735 if (flags
& MNT_NOSUID
) res
= catopt(res
, "nosuid");
736 if (flags
& MNT_NODEV
) res
= catopt(res
, "nodev");
737 if (flags
& MNT_UNION
) res
= catopt(res
, "union");
738 if (flags
& MNT_ASYNC
) res
= catopt(res
, "async");
739 if (flags
& MNT_NOATIME
) res
= catopt(res
, "noatime");
740 if (flags
& MNT_NOCLUSTERR
) res
= catopt(res
, "noclusterr");
741 if (flags
& MNT_NOCLUSTERW
) res
= catopt(res
, "noclusterw");
742 if (flags
& MNT_NOSYMFOLLOW
) res
= catopt(res
, "nosymfollow");
743 if (flags
& MNT_SUIDDIR
) res
= catopt(res
, "suiddir");
744 if (flags
& MNT_IGNORE
) res
= catopt(res
, "ignore");
750 xstrdup(const char *str
)
752 char* ret
= strdup(str
);
754 errx(1, "strdup failed (could not allocate memory)");