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. 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.
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
29 * @(#) Copyright (c) 1980, 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)mount.c 8.25 (Berkeley) 5/8/95
31 * $FreeBSD: src/sbin/mount/mount.c,v 1.39.2.3 2001/08/01 08:26:23 obrien Exp $
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/mountctl.h>
52 #include "pathnames.h"
55 #define MOUNT_META_OPTION_FSTAB "fstab"
56 #define MOUNT_META_OPTION_CURRENT "current"
58 int debug
, fstab_style
, verbose
;
60 static char *catopt(char *, const char *);
62 *getmntpt(const char *);
63 static int hasopt(const char *, const char *);
64 static int ismounted(struct fstab
*, struct statfs
*, int);
65 static int isremountable(const char *);
66 static void mangle(char *, int *, const char **);
67 static char *update_options(char *, char *, int);
68 static int mountfs(const char *, const char *, const char *,
69 int, const char *, const char *);
70 static void remopt(char *, const char *);
71 static void printdefvals(const struct statfs
*);
72 static void prmount(struct statfs
*);
73 static void putfsent(const struct statfs
*);
74 static void usage(void);
75 static char *flags2opts(int);
76 static char *xstrdup(const char *str
);
79 int mount_ufs(int, const char **);
82 * List of VFS types that can be remounted without becoming mounted on top
84 * XXX Is this list correct?
87 remountable_fs_names
[] = {
88 "ufs", "ffs", "ext2fs",
93 main(int argc
, char **argv
)
95 const char *mntfromname
, **vfslist
, *vfstype
;
97 struct statfs
*mntbuf
;
100 int all
, ch
, i
, init_flags
, mntsize
, rval
, have_fstab
;
103 all
= init_flags
= 0;
107 while ((ch
= getopt(argc
, argv
, "adF:fo:prwt:uv")) != -1)
119 init_flags
|= MNT_FORCE
;
123 options
= catopt(options
, optarg
);
130 options
= catopt(options
, "ro");
134 errx(1, "only one -t option may be specified");
135 vfslist
= makevfslist(optarg
);
139 init_flags
|= MNT_UPDATE
;
145 options
= catopt(options
, "noro");
155 #define BADTYPE(type) \
156 (strcmp(type, FSTAB_RO) && \
157 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
162 if ((mntsize
= getmntinfo(&mntbuf
, MNT_NOWAIT
)) == 0)
163 err(1, "getmntinfo");
165 while ((fs
= getfsent()) != NULL
) {
166 if (BADTYPE(fs
->fs_type
))
168 if (checkvfsname(fs
->fs_vfstype
, vfslist
))
170 if (hasopt(fs
->fs_mntops
, "noauto"))
172 if (!(init_flags
& MNT_UPDATE
) &&
173 ismounted(fs
, mntbuf
, mntsize
))
175 options
= update_options(options
,
176 fs
->fs_mntops
, mntbuf
->f_flags
);
177 if (mountfs(fs
->fs_vfstype
, fs
->fs_spec
,
178 fs
->fs_file
, init_flags
, options
,
182 } else if (fstab_style
) {
183 for (i
= 0; i
< mntsize
; i
++) {
184 if (checkvfsname(mntbuf
[i
].f_fstypename
, vfslist
))
186 putfsent(&mntbuf
[i
]);
189 for (i
= 0; i
< mntsize
; i
++) {
190 if (checkvfsname(mntbuf
[i
].f_fstypename
,
201 rmslashes(*argv
, *argv
);
203 if (init_flags
& MNT_UPDATE
) {
206 if ((mntbuf
= getmntpt(*argv
)) == NULL
)
207 errx(1, "not currently mounted %s", *argv
);
209 * Only get the mntflags from fstab if both mntpoint
210 * and mntspec are identical. Also handle the special
211 * case where just '/' is mounted and 'spec' is not
212 * identical with the one from fstab ('/dev' is missing
213 * in the spec-string at boot-time).
215 if ((fs
= getfsfile(mntbuf
->f_mntonname
)) != NULL
) {
216 if (strcmp(fs
->fs_spec
,
217 mntbuf
->f_mntfromname
) == 0 &&
219 mntbuf
->f_mntonname
) == 0) {
221 mntfromname
= mntbuf
->f_mntfromname
;
222 } else if (argv
[0][0] == '/' &&
223 argv
[0][1] == '\0') {
226 mntfromname
= fs
->fs_spec
;
230 options
= update_options(options
, fs
->fs_mntops
,
233 mntfromname
= mntbuf
->f_mntfromname
;
234 options
= update_options(options
, NULL
,
237 rval
= mountfs(mntbuf
->f_fstypename
, mntfromname
,
238 mntbuf
->f_mntonname
, init_flags
, options
, 0);
241 if ((fs
= getfsfile(*argv
)) == NULL
&&
242 (fs
= getfsspec(*argv
)) == NULL
)
243 errx(1, "%s: unknown special file or file system",
245 if (BADTYPE(fs
->fs_type
))
246 errx(1, "%s has unknown file system type",
248 rval
= mountfs(fs
->fs_vfstype
, fs
->fs_spec
, fs
->fs_file
,
249 init_flags
, options
, fs
->fs_mntops
);
253 * If -t flag has not been specified, the path cannot be
254 * found, spec contains either a ':' or a '@', and the
255 * spec is not a file with those characters, then assume
256 * that an NFS filesystem is being specified ala Sun.
258 if (vfslist
== NULL
&& strpbrk(argv
[0], ":@") != NULL
&&
259 access(argv
[0], 0) == -1)
261 rval
= mountfs(vfstype
, getdevpath(argv
[0], 0), argv
[1],
262 init_flags
, options
, NULL
);
270 * If the mount was successfully, and done by root, tell mountd the
271 * good news. Pid checks are probably unnecessary, but don't hurt.
273 if (rval
== 0 && getuid() == 0 &&
274 (mountdfp
= fopen(_PATH_MOUNTDPID
, "r")) != NULL
) {
275 if (fscanf(mountdfp
, "%d", &pid
) == 1 &&
276 pid
> 0 && kill(pid
, SIGHUP
) == -1 && errno
!= ESRCH
)
277 err(1, "signal mountd");
285 ismounted(struct fstab
*fs
, struct statfs
*mntbuf
, int mntsize
)
289 if (fs
->fs_file
[0] == '/' && fs
->fs_file
[1] == '\0')
290 /* the root file system can always be remounted */
293 for (i
= mntsize
- 1; i
>= 0; --i
)
294 if (strcmp(fs
->fs_file
, mntbuf
[i
].f_mntonname
) == 0 &&
295 (!isremountable(fs
->fs_vfstype
) ||
296 strcmp(fs
->fs_spec
, mntbuf
[i
].f_mntfromname
) == 0))
302 isremountable(const char *vfsname
)
306 for (cp
= remountable_fs_names
; *cp
; cp
++)
307 if (strcmp(*cp
, vfsname
) == 0)
313 hasopt(const char *mntopts
, const char *option
)
318 if (option
[0] == 'n' && option
[1] == 'o') {
323 optbuf
= xstrdup(mntopts
);
325 for (opt
= optbuf
; (opt
= strtok(opt
, ",")) != NULL
; opt
= NULL
) {
326 if (opt
[0] == 'n' && opt
[1] == 'o') {
327 if (!strcasecmp(opt
+ 2, option
))
329 } else if (!strcasecmp(opt
, option
))
337 mountfs(const char *vfstype
, const char *spec
, const char *name
, int flags
,
338 const char *options
, const char *mntopts
)
340 /* List of directories containing mount_xxx subcommands. */
341 static const char *edirs
[] = {
346 const char *argv
[100], **edir
;
350 char *optbuf
, execname
[MAXPATHLEN
+ 1], mntpath
[MAXPATHLEN
];
352 /* resolve the mountpoint with realpath(3) */
353 checkpath(name
, mntpath
);
358 if (options
== NULL
) {
359 if (*mntopts
== '\0') {
366 optbuf
= catopt(xstrdup(mntopts
), options
);
368 if (strcmp(name
, "/") == 0)
370 if (flags
& MNT_FORCE
)
371 optbuf
= catopt(optbuf
, "force");
372 if (flags
& MNT_RDONLY
)
373 optbuf
= catopt(optbuf
, "ro");
376 * The mount_mfs (newfs) command uses -o to select the
377 * optimization mode. We don't pass the default "-o rw"
380 if (flags
& MNT_UPDATE
)
381 optbuf
= catopt(optbuf
, "update");
384 argv
[argc
++] = vfstype
;
385 mangle(optbuf
, &argc
, argv
);
391 printf("exec: mount_%s", vfstype
);
392 for (i
= 1; i
< argc
; i
++)
393 printf(" %s", argv
[i
]);
398 switch (pid
= fork()) {
399 case -1: /* Error. */
404 if (strcmp(vfstype
, "ufs") == 0)
405 exit(mount_ufs(argc
, argv
));
407 /* Go find an executable. */
408 for (edir
= edirs
; *edir
; edir
++) {
410 sizeof(execname
), "%s/mount_%s", *edir
, vfstype
);
411 execv(execname
, __DECONST(char * const *, argv
));
413 if (errno
== ENOENT
) {
416 for (edir
= edirs
; *edir
; edir
++)
417 len
+= strlen(*edir
) + 2; /* ", " */
418 if ((cp
= malloc(len
)) == NULL
)
419 errx(1, "malloc failed");
421 for (edir
= edirs
; *edir
; edir
++) {
426 warn("exec mount_%s not found in %s", vfstype
, cp
);
430 default: /* Parent. */
433 if (waitpid(pid
, &status
, 0) < 0) {
438 if (WIFEXITED(status
)) {
439 if (WEXITSTATUS(status
) != 0)
440 return (WEXITSTATUS(status
));
441 } else if (WIFSIGNALED(status
)) {
442 warnx("%s: %s", name
, sys_siglist
[WTERMSIG(status
)]);
447 if (statfs(name
, &sf
) < 0) {
448 warn("statfs %s", name
);
463 prmount(struct statfs
*sfp
)
473 if ((buf
= malloc(len
)) == NULL
)
474 errx(1, "malloc failed");
476 printf("%s on %s (%s", sfp
->f_mntfromname
, sfp
->f_mntonname
,
479 /* Get a string buffer with all the used flags names */
480 error
= mountctl(sfp
->f_mntonname
, MOUNTCTL_MOUNTFLAGS
,
481 -1, NULL
, 0, buf
, len
);
484 printf(", mounted by ");
485 if ((pw
= getpwuid(sfp
->f_owner
)) != NULL
)
486 printf("%s", pw
->pw_name
);
488 printf("%d", sfp
->f_owner
);
491 if (error
!= -1 && strlen(buf
))
495 if (sfp
->f_syncwrites
!= 0 || sfp
->f_asyncwrites
!= 0)
496 printf(", writes: sync %ld async %ld",
497 sfp
->f_syncwrites
, sfp
->f_asyncwrites
);
498 if (sfp
->f_syncreads
!= 0 || sfp
->f_asyncreads
!= 0)
499 printf(", reads: sync %ld async %ld",
500 sfp
->f_syncreads
, sfp
->f_asyncreads
);
505 static struct statfs
*
506 getmntpt(const char *name
)
508 struct statfs
*mntbuf
;
511 mntsize
= getmntinfo(&mntbuf
, MNT_NOWAIT
);
512 for (i
= mntsize
- 1; i
>= 0; i
--) {
513 if (strcmp(mntbuf
[i
].f_mntfromname
, name
) == 0 ||
514 strcmp(mntbuf
[i
].f_mntonname
, name
) == 0)
521 catopt(char *s0
, const char *s1
)
526 if (s1
== NULL
|| *s1
== '\0')
530 i
= strlen(s0
) + strlen(s1
) + 1 + 1;
531 if ((cp
= malloc(i
)) == NULL
)
532 errx(1, "malloc failed");
533 snprintf(cp
, i
, "%s,%s", s0
, s1
);
543 mangle(char *options
, int *argcp
, const char **argv
)
549 for (s
= options
; (p
= strsep(&s
, ",")) != NULL
;)
558 } else if (strcmp(p
, "rw") != 0) {
569 update_options(char *opts
, char *fstab
, int curflags
)
573 char *expopt
, *newopt
, *tmpopt
;
578 /* remove meta options from list */
579 remopt(fstab
, MOUNT_META_OPTION_FSTAB
);
580 remopt(fstab
, MOUNT_META_OPTION_CURRENT
);
581 cur
= flags2opts(curflags
);
584 * Expand all meta-options passed to us first.
587 for (p
= opts
; (o
= strsep(&p
, ",")) != NULL
;) {
588 if (strcmp(MOUNT_META_OPTION_FSTAB
, o
) == 0)
589 expopt
= catopt(expopt
, fstab
);
590 else if (strcmp(MOUNT_META_OPTION_CURRENT
, o
) == 0)
591 expopt
= catopt(expopt
, cur
);
593 expopt
= catopt(expopt
, o
);
599 * Remove previous contradictory arguments. Given option "foo" we
600 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
601 * and "foo" - so we can deal with possible options like "notice".
604 for (p
= expopt
; (o
= strsep(&p
, ",")) != NULL
;) {
605 if ((tmpopt
= malloc( strlen(o
) + 2 + 1 )) == NULL
)
606 errx(1, "malloc failed");
608 strcpy(tmpopt
, "no");
610 remopt(newopt
, tmpopt
);
613 if (strncmp("no", o
, 2) == 0)
616 newopt
= catopt(newopt
, o
);
624 remopt(char *string
, const char *opt
)
628 if (string
== NULL
|| *string
== '\0' || opt
== NULL
|| *opt
== '\0')
633 for (p
= string
; (o
= strsep(&p
, ",")) != NULL
;) {
634 if (strcmp(opt
, o
) != 0) {
635 if (*r
== ',' && *o
!= '\0')
637 while ((*r
++ = *o
++) != '\0')
649 fprintf(stderr
, "%s\n%s\n%s\n",
650 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t type]",
651 " mount [-dfpruvw] {special | node}",
652 " mount [-dfpruvw] [-o options] [-t type] special node");
657 * Prints the default values for dump frequency and pass number of fsck.
658 * This happens when mount(8) is called with -p and there is no fstab file
659 * or there is but the values aren't specified.
662 printdefvals(const struct statfs
*ent
)
664 if (strcmp(ent
->f_fstypename
, "ufs") == 0) {
665 if (strcmp(ent
->f_mntonname
, "/") == 0)
675 putfsent(const struct statfs
*ent
)
681 opts
= flags2opts(ent
->f_flags
);
682 printf("%s\t%s\t%s %s", ent
->f_mntfromname
, ent
->f_mntonname
,
683 ent
->f_fstypename
, opts
);
687 * If fstab file is missing don't call getfsspec() or getfsfile()
688 * at all, because the same warning will be printed twice for every
689 * mounted filesystem.
691 if (stat(_PATH_FSTAB
, &sb
) != -1) {
692 if ((fst
= getfsspec(ent
->f_mntfromname
)))
693 printf("\t%u %u\n", fst
->fs_freq
, fst
->fs_passno
);
694 else if ((fst
= getfsfile(ent
->f_mntonname
)))
695 printf("\t%u %u\n", fst
->fs_freq
, fst
->fs_passno
);
705 flags2opts(int flags
)
711 res
= catopt(res
, (flags
& MNT_RDONLY
) ? "ro" : "rw");
713 if (flags
& MNT_SYNCHRONOUS
) res
= catopt(res
, "sync");
714 if (flags
& MNT_NOEXEC
) res
= catopt(res
, "noexec");
715 if (flags
& MNT_NOSUID
) res
= catopt(res
, "nosuid");
716 if (flags
& MNT_NODEV
) res
= catopt(res
, "nodev");
717 if (flags
& MNT_ASYNC
) res
= catopt(res
, "async");
718 if (flags
& MNT_NOATIME
) res
= catopt(res
, "noatime");
719 if (flags
& MNT_NOCLUSTERR
) res
= catopt(res
, "noclusterr");
720 if (flags
& MNT_NOCLUSTERW
) res
= catopt(res
, "noclusterw");
721 if (flags
& MNT_NOSYMFOLLOW
) res
= catopt(res
, "nosymfollow");
722 if (flags
& MNT_SUIDDIR
) res
= catopt(res
, "suiddir");
723 if (flags
& MNT_IGNORE
) res
= catopt(res
, "ignore");
729 xstrdup(const char *str
)
731 char* ret
= strdup(str
);
733 errx(1, "strdup failed (could not allocate memory)");