nlookup.9 - document nlookup_init_root
[dragonfly.git] / sbin / mount / mount.c
blob151e163f673af252703d29b804e877d5faa5484a
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/mountctl.h>
42 #include <sys/stat.h>
43 #include <sys/wait.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fstab.h>
48 #include <pwd.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
55 #include "extern.h"
56 #include "mntopts.h"
57 #include "pathnames.h"
59 /* `meta' options */
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 *);
66 static struct statfs
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 * List of VFS types that can be remounted without becoming mounted on top
85 * of each other.
86 * XXX Is this list correct?
88 static const char *
89 remountable_fs_names[] = {
90 "ufs", "ffs", "ext2fs",
94 int
95 main(int argc, char **argv)
97 const char *mntfromname, **vfslist, *vfstype;
98 struct fstab *fs;
99 struct statfs *mntbuf;
100 FILE *mountdfp;
101 pid_t pid;
102 int all, ch, i, init_flags, mntsize, rval, have_fstab;
103 char *options;
105 all = init_flags = 0;
106 options = NULL;
107 vfslist = NULL;
108 vfstype = "ufs";
109 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
110 switch (ch) {
111 case 'a':
112 all = 1;
113 break;
114 case 'd':
115 debug = 1;
116 break;
117 case 'f':
118 init_flags |= MNT_FORCE;
119 break;
120 case 'o':
121 if (*optarg)
122 options = catopt(options, optarg);
123 break;
124 case 'p':
125 fstab_style = 1;
126 verbose = 1;
127 break;
128 case 'r':
129 options = catopt(options, "ro");
130 break;
131 case 't':
132 if (vfslist != NULL)
133 errx(1, "only one -t option may be specified");
134 vfslist = makevfslist(optarg);
135 vfstype = optarg;
136 break;
137 case 'u':
138 init_flags |= MNT_UPDATE;
139 break;
140 case 'v':
141 verbose = 1;
142 break;
143 case 'w':
144 options = catopt(options, "noro");
145 break;
146 case '?':
147 default:
148 usage();
149 /* NOTREACHED */
151 argc -= optind;
152 argv += optind;
154 #define BADTYPE(type) \
155 (strcmp(type, FSTAB_RO) && \
156 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
158 rval = 0;
159 switch (argc) {
160 case 0:
161 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
162 err(1, "getmntinfo");
163 if (all) {
164 while ((fs = getfsent()) != NULL) {
165 if (BADTYPE(fs->fs_type))
166 continue;
167 if (checkvfsname(fs->fs_vfstype, vfslist))
168 continue;
169 if (hasopt(fs->fs_mntops, "noauto"))
170 continue;
171 if (!(init_flags & MNT_UPDATE) &&
172 ismounted(fs, mntbuf, mntsize))
173 continue;
174 options = update_options(options,
175 fs->fs_mntops, mntbuf->f_flags);
176 if (mountfs(fs->fs_vfstype, fs->fs_spec,
177 fs->fs_file, init_flags, options,
178 fs->fs_mntops))
179 rval = 1;
181 } else if (fstab_style) {
182 for (i = 0; i < mntsize; i++) {
183 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
184 continue;
185 putfsent(&mntbuf[i]);
187 } else {
188 for (i = 0; i < mntsize; i++) {
189 if (checkvfsname(mntbuf[i].f_fstypename,
190 vfslist))
191 continue;
192 prmount(&mntbuf[i]);
195 exit(rval);
196 case 1:
197 if (vfslist != NULL)
198 usage();
200 rmslashes(*argv, *argv);
202 if (init_flags & MNT_UPDATE) {
203 mntfromname = NULL;
204 have_fstab = 0;
205 if ((mntbuf = getmntpt(*argv)) == NULL)
206 errx(1, "not currently mounted %s", *argv);
208 * Only get the mntflags from fstab if both mntpoint
209 * and mntspec are identical. Also handle the special
210 * case where just '/' is mounted and 'spec' is not
211 * identical with the one from fstab ('/dev' is missing
212 * in the spec-string at boot-time).
214 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
215 if (strcmp(fs->fs_spec,
216 mntbuf->f_mntfromname) == 0 &&
217 strcmp(fs->fs_file,
218 mntbuf->f_mntonname) == 0) {
219 have_fstab = 1;
220 mntfromname = mntbuf->f_mntfromname;
221 } else if (argv[0][0] == '/' &&
222 argv[0][1] == '\0') {
223 fs = getfsfile("/");
224 have_fstab = 1;
225 mntfromname = fs->fs_spec;
228 if (have_fstab) {
229 options = update_options(options, fs->fs_mntops,
230 mntbuf->f_flags);
231 } else {
232 mntfromname = mntbuf->f_mntfromname;
233 options = update_options(options, NULL,
234 mntbuf->f_flags);
236 rval = mountfs(mntbuf->f_fstypename, mntfromname,
237 mntbuf->f_mntonname, init_flags, options, 0);
238 break;
240 if ((fs = getfsfile(*argv)) == NULL &&
241 (fs = getfsspec(*argv)) == NULL)
242 errx(1, "%s: unknown special file or file system",
243 *argv);
244 if (BADTYPE(fs->fs_type))
245 errx(1, "%s has unknown file system type",
246 *argv);
247 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
248 init_flags, options, fs->fs_mntops);
249 break;
250 case 2:
252 * If -t flag has not been specified, the path cannot be
253 * found, spec contains either a ':' or a '@', and the
254 * spec is not a file with those characters, then assume
255 * that an NFS filesystem is being specified ala Sun.
257 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
258 access(argv[0], 0) == -1)
259 vfstype = "nfs";
260 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
261 init_flags, options, NULL);
262 break;
263 default:
264 usage();
265 /* NOTREACHED */
269 * If the mount was successfully, and done by root, tell mountd the
270 * good news. Pid checks are probably unnecessary, but don't hurt.
272 if (rval == 0 && getuid() == 0 &&
273 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
274 if (fscanf(mountdfp, "%d", &pid) == 1 &&
275 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
276 err(1, "signal mountd");
277 fclose(mountdfp);
280 exit(rval);
283 static int
284 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
286 int i;
288 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
289 /* the root file system can always be remounted */
290 return (0);
292 for (i = mntsize - 1; i >= 0; --i)
293 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
294 (!isremountable(fs->fs_vfstype) ||
295 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
296 return (1);
297 return (0);
300 static int
301 isremountable(const char *vfsname)
303 const char **cp;
305 for (cp = remountable_fs_names; *cp; cp++)
306 if (strcmp(*cp, vfsname) == 0)
307 return (1);
308 return (0);
311 static int
312 hasopt(const char *mntopts, const char *option)
314 int negative, found;
315 char *opt, *optbuf;
317 if (option[0] == 'n' && option[1] == 'o') {
318 negative = 1;
319 option += 2;
320 } else
321 negative = 0;
322 optbuf = xstrdup(mntopts);
323 found = 0;
324 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
325 if (opt[0] == 'n' && opt[1] == 'o') {
326 if (!strcasecmp(opt + 2, option))
327 found = negative;
328 } else if (!strcasecmp(opt, option))
329 found = !negative;
331 free(optbuf);
332 return (found);
335 static int
336 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
337 const char *options, const char *mntopts)
339 /* List of directories containing mount_xxx subcommands. */
340 static const char *edirs[] = {
341 _PATH_SBIN,
342 _PATH_USRSBIN,
343 NULL
345 const char *argv[100], **edir;
346 struct statfs sf;
347 pid_t pid;
348 int argc, i, status;
349 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
351 #if __GNUC__
352 (void)&optbuf;
353 (void)&name;
354 #endif
356 /* resolve the mountpoint with realpath(3) */
357 checkpath(name, mntpath);
358 name = mntpath;
360 if (mntopts == NULL)
361 mntopts = "";
362 if (options == NULL) {
363 if (*mntopts == '\0') {
364 options = "rw";
365 } else {
366 options = mntopts;
367 mntopts = "";
370 optbuf = catopt(xstrdup(mntopts), options);
372 if (strcmp(name, "/") == 0)
373 flags |= MNT_UPDATE;
374 if (flags & MNT_FORCE)
375 optbuf = catopt(optbuf, "force");
376 if (flags & MNT_RDONLY)
377 optbuf = catopt(optbuf, "ro");
379 * XXX
380 * The mount_mfs (newfs) command uses -o to select the
381 * optimization mode. We don't pass the default "-o rw"
382 * for that reason.
384 if (flags & MNT_UPDATE)
385 optbuf = catopt(optbuf, "update");
387 argc = 0;
388 argv[argc++] = vfstype;
389 mangle(optbuf, &argc, argv);
390 argv[argc++] = spec;
391 argv[argc++] = name;
392 argv[argc] = NULL;
394 if (debug) {
395 printf("exec: mount_%s", vfstype);
396 for (i = 1; i < argc; i++)
397 printf(" %s", argv[i]);
398 printf("\n");
399 return (0);
402 switch (pid = fork()) {
403 case -1: /* Error. */
404 warn("fork");
405 free(optbuf);
406 return (1);
407 case 0: /* Child. */
408 if (strcmp(vfstype, "ufs") == 0)
409 exit(mount_ufs(argc, argv));
411 /* Go find an executable. */
412 for (edir = edirs; *edir; edir++) {
413 snprintf(execname,
414 sizeof(execname), "%s/mount_%s", *edir, vfstype);
415 execv(execname, __DECONST(char * const *, argv));
417 if (errno == ENOENT) {
418 int len = 0;
419 char *cp;
420 for (edir = edirs; *edir; edir++)
421 len += strlen(*edir) + 2; /* ", " */
422 if ((cp = malloc(len)) == NULL)
423 errx(1, "malloc failed");
424 cp[0] = '\0';
425 for (edir = edirs; *edir; edir++) {
426 strcat(cp, *edir);
427 if (edir[1] != NULL)
428 strcat(cp, ", ");
430 warn("exec mount_%s not found in %s", vfstype, cp);
432 exit(1);
433 /* NOTREACHED */
434 default: /* Parent. */
435 free(optbuf);
437 if (waitpid(pid, &status, 0) < 0) {
438 warn("waitpid");
439 return (1);
442 if (WIFEXITED(status)) {
443 if (WEXITSTATUS(status) != 0)
444 return (WEXITSTATUS(status));
445 } else if (WIFSIGNALED(status)) {
446 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
447 return (1);
450 if (verbose) {
451 if (statfs(name, &sf) < 0) {
452 warn("statfs %s", name);
453 return (1);
455 if (fstab_style)
456 putfsent(&sf);
457 else
458 prmount(&sf);
460 break;
463 return (0);
466 static void
467 prmount(struct statfs *sfp)
469 struct passwd *pw;
470 char *buf;
471 int error;
472 int len;
474 error = 0;
475 len = 256;
477 if ((buf = malloc(len)) == NULL)
478 errx(1, "malloc failed");
480 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
481 sfp->f_fstypename);
483 /* Get a string buffer with all the used flags names */
484 error = mountctl(sfp->f_mntonname, MOUNTCTL_MOUNTFLAGS,
485 -1, NULL, 0, buf, len);
487 if (sfp->f_owner) {
488 printf(", mounted by ");
489 if ((pw = getpwuid(sfp->f_owner)) != NULL)
490 printf("%s", pw->pw_name);
491 else
492 printf("%d", sfp->f_owner);
495 if (strlen(buf))
496 printf(", %s", buf);
498 if (verbose) {
499 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
500 printf(", writes: sync %ld async %ld",
501 sfp->f_syncwrites, sfp->f_asyncwrites);
502 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
503 printf(", reads: sync %ld async %ld",
504 sfp->f_syncreads, sfp->f_asyncreads);
506 printf(")\n");
509 static struct statfs *
510 getmntpt(const char *name)
512 struct statfs *mntbuf;
513 int i, mntsize;
515 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
516 for (i = mntsize - 1; i >= 0; i--) {
517 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
518 strcmp(mntbuf[i].f_mntonname, name) == 0)
519 return (&mntbuf[i]);
521 return (NULL);
524 static char *
525 catopt(char *s0, const char *s1)
527 size_t i;
528 char *cp;
530 if (s1 == NULL || *s1 == '\0')
531 return s0;
533 if (s0 && *s0) {
534 i = strlen(s0) + strlen(s1) + 1 + 1;
535 if ((cp = malloc(i)) == NULL)
536 errx(1, "malloc failed");
537 snprintf(cp, i, "%s,%s", s0, s1);
538 } else
539 cp = xstrdup(s1);
541 if (s0)
542 free(s0);
543 return (cp);
546 static void
547 mangle(char *options, int *argcp, const char **argv)
549 char *p, *s;
550 int argc;
552 argc = *argcp;
553 for (s = options; (p = strsep(&s, ",")) != NULL;)
554 if (*p != '\0') {
555 if (*p == '-') {
556 argv[argc++] = p;
557 p = strchr(p, '=');
558 if (p) {
559 *p = '\0';
560 argv[argc++] = p+1;
562 } else if (strcmp(p, "rw") != 0) {
563 argv[argc++] = "-o";
564 argv[argc++] = p;
568 *argcp = argc;
572 static char *
573 update_options(char *opts, char *fstab, int curflags)
575 char *o, *p;
576 char *cur;
577 char *expopt, *newopt, *tmpopt;
579 if (opts == NULL)
580 return xstrdup("");
582 /* remove meta options from list */
583 remopt(fstab, MOUNT_META_OPTION_FSTAB);
584 remopt(fstab, MOUNT_META_OPTION_CURRENT);
585 cur = flags2opts(curflags);
588 * Expand all meta-options passed to us first.
590 expopt = NULL;
591 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
592 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
593 expopt = catopt(expopt, fstab);
594 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
595 expopt = catopt(expopt, cur);
596 else
597 expopt = catopt(expopt, o);
599 free(cur);
600 free(opts);
603 * Remove previous contradictory arguments. Given option "foo" we
604 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
605 * and "foo" - so we can deal with possible options like "notice".
607 newopt = NULL;
608 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
609 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
610 errx(1, "malloc failed");
612 strcpy(tmpopt, "no");
613 strcat(tmpopt, o);
614 remopt(newopt, tmpopt);
615 free(tmpopt);
617 if (strncmp("no", o, 2) == 0)
618 remopt(newopt, o+2);
620 newopt = catopt(newopt, o);
622 free(expopt);
624 return newopt;
627 static void
628 remopt(char *string, const char *opt)
630 char *o, *p, *r;
632 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
633 return;
635 r = string;
637 for (p = string; (o = strsep(&p, ",")) != NULL;) {
638 if (strcmp(opt, o) != 0) {
639 if (*r == ',' && *o != '\0')
640 r++;
641 while ((*r++ = *o++) != '\0')
643 *--r = ',';
646 *r = '\0';
649 static void
650 usage(void)
653 fprintf(stderr, "%s\n%s\n%s\n",
654 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
655 " mount [-adfpruvw] [-o options] [-t ufs | external_type]",
656 " mount [-dfpruvw] special | node");
657 exit(1);
661 * Prints the default values for dump frequency and pass number of fsck.
662 * This happens when mount(8) is called with -p and there is no fstab file
663 * or there is but the values aren't specified.
665 static void
666 printdefvals(const struct statfs *ent)
668 if (strcmp(ent->f_fstypename, "ufs") == 0) {
669 if (strcmp(ent->f_mntonname, "/") == 0)
670 printf("\t1 1\n");
671 else
672 printf("\t2 2\n");
673 } else {
674 printf("\t0 0\n");
678 static void
679 putfsent(const struct statfs *ent)
681 struct stat sb;
682 struct fstab *fst;
683 char *opts;
685 opts = flags2opts(ent->f_flags);
686 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
687 ent->f_fstypename, opts);
688 free(opts);
691 * If fstab file is missing don't call getfsspec() or getfsfile()
692 * at all, because the same warning will be printed twice for every
693 * mounted filesystem.
695 if (stat(_PATH_FSTAB, &sb) != -1) {
696 if ((fst = getfsspec(ent->f_mntfromname)))
697 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
698 else if ((fst = getfsfile(ent->f_mntonname)))
699 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
700 else
701 printdefvals(ent);
702 } else {
703 printdefvals(ent);
708 static char *
709 flags2opts(int flags)
711 char *res;
713 res = NULL;
715 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
717 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
718 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
719 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
720 if (flags & MNT_NODEV) res = catopt(res, "nodev");
721 if (flags & MNT_UNION) res = catopt(res, "union");
722 if (flags & MNT_ASYNC) res = catopt(res, "async");
723 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
724 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
725 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
726 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
727 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
728 if (flags & MNT_IGNORE) res = catopt(res, "ignore");
730 return res;
733 static char*
734 xstrdup(const char *str)
736 char* ret = strdup(str);
737 if(ret == NULL) {
738 errx(1, "strdup failed (could not allocate memory)");
740 return ret;