mount - Add missing LDADD and #include
[dragonfly.git] / sbin / mount / mount.c
blob74dbfb51afa4be6af6f7d1ce2994cf15f454ea03
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/stat.h>
42 #include <sys/wait.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <libutil.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);
83 /* Map from mount options to printable formats. */
84 static struct opt {
85 int o_opt;
86 const char *o_name;
87 } optnames[] = {
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" },
105 { 0, NULL }
109 * List of VFS types that can be remounted without becoming mounted on top
110 * of each other.
111 * XXX Is this list correct?
113 static const char *
114 remountable_fs_names[] = {
115 "ufs", "ffs", "ext2fs",
120 main(int argc, char **argv)
122 const char *mntfromname, **vfslist, *vfstype;
123 struct fstab *fs;
124 struct statfs *mntbuf;
125 FILE *mountdfp;
126 pid_t pid;
127 int all, ch, i, init_flags, mntsize, rval, have_fstab;
128 char *options;
130 all = init_flags = 0;
131 options = NULL;
132 vfslist = NULL;
133 vfstype = "ufs";
134 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
135 switch (ch) {
136 case 'a':
137 all = 1;
138 break;
139 case 'd':
140 debug = 1;
141 break;
142 case 'f':
143 init_flags |= MNT_FORCE;
144 break;
145 case 'o':
146 if (*optarg)
147 options = catopt(options, optarg);
148 break;
149 case 'p':
150 fstab_style = 1;
151 verbose = 1;
152 break;
153 case 'r':
154 options = catopt(options, "ro");
155 break;
156 case 't':
157 if (vfslist != NULL)
158 errx(1, "only one -t option may be specified");
159 vfslist = makevfslist(optarg);
160 vfstype = optarg;
161 break;
162 case 'u':
163 init_flags |= MNT_UPDATE;
164 break;
165 case 'v':
166 verbose = 1;
167 break;
168 case 'w':
169 options = catopt(options, "noro");
170 break;
171 case '?':
172 default:
173 usage();
174 /* NOTREACHED */
176 argc -= optind;
177 argv += optind;
179 #define BADTYPE(type) \
180 (strcmp(type, FSTAB_RO) && \
181 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
183 rval = 0;
184 switch (argc) {
185 case 0:
186 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
187 err(1, "getmntinfo");
188 if (all) {
189 while ((fs = getfsent()) != NULL) {
190 if (BADTYPE(fs->fs_type))
191 continue;
192 if (checkvfsname(fs->fs_vfstype, vfslist))
193 continue;
194 if (hasopt(fs->fs_mntops, "noauto"))
195 continue;
196 if (!(init_flags & MNT_UPDATE) &&
197 ismounted(fs, mntbuf, mntsize))
198 continue;
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,
203 fs->fs_mntops))
204 rval = 1;
206 } else if (fstab_style) {
207 for (i = 0; i < mntsize; i++) {
208 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
209 continue;
210 putfsent(&mntbuf[i]);
212 } else {
213 for (i = 0; i < mntsize; i++) {
214 if (checkvfsname(mntbuf[i].f_fstypename,
215 vfslist))
216 continue;
217 prmount(&mntbuf[i]);
220 exit(rval);
221 case 1:
222 if (vfslist != NULL)
223 usage();
225 rmslashes(*argv, *argv);
227 if (init_flags & MNT_UPDATE) {
228 mntfromname = NULL;
229 have_fstab = 0;
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 &&
242 strcmp(fs->fs_file,
243 mntbuf->f_mntonname) == 0) {
244 have_fstab = 1;
245 mntfromname = mntbuf->f_mntfromname;
246 } else if (argv[0][0] == '/' &&
247 argv[0][1] == '\0') {
248 fs = getfsfile("/");
249 have_fstab = 1;
250 mntfromname = fs->fs_spec;
253 if (have_fstab) {
254 options = update_options(options, fs->fs_mntops,
255 mntbuf->f_flags);
256 } else {
257 mntfromname = mntbuf->f_mntfromname;
258 options = update_options(options, NULL,
259 mntbuf->f_flags);
261 rval = mountfs(mntbuf->f_fstypename, mntfromname,
262 mntbuf->f_mntonname, init_flags, options, 0);
263 break;
265 if ((fs = getfsfile(*argv)) == NULL &&
266 (fs = getfsspec(*argv)) == NULL)
267 errx(1, "%s: unknown special file or file system",
268 *argv);
269 if (BADTYPE(fs->fs_type))
270 errx(1, "%s has unknown file system type",
271 *argv);
272 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
273 init_flags, options, fs->fs_mntops);
274 break;
275 case 2:
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)
284 vfstype = "nfs";
285 rval = mountfs(vfstype, getdevpath(argv[0], 0), argv[1],
286 init_flags, options, NULL);
287 break;
288 default:
289 usage();
290 /* NOTREACHED */
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");
302 fclose(mountdfp);
305 exit(rval);
308 static int
309 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
311 int i;
313 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
314 /* the root file system can always be remounted */
315 return (0);
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))
321 return (1);
322 return (0);
325 static int
326 isremountable(const char *vfsname)
328 const char **cp;
330 for (cp = remountable_fs_names; *cp; cp++)
331 if (strcmp(*cp, vfsname) == 0)
332 return (1);
333 return (0);
336 static int
337 hasopt(const char *mntopts, const char *option)
339 int negative, found;
340 char *opt, *optbuf;
342 if (option[0] == 'n' && option[1] == 'o') {
343 negative = 1;
344 option += 2;
345 } else
346 negative = 0;
347 optbuf = xstrdup(mntopts);
348 found = 0;
349 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
350 if (opt[0] == 'n' && opt[1] == 'o') {
351 if (!strcasecmp(opt + 2, option))
352 found = negative;
353 } else if (!strcasecmp(opt, option))
354 found = !negative;
356 free(optbuf);
357 return (found);
360 static int
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[] = {
366 _PATH_SBIN,
367 _PATH_USRSBIN,
368 NULL
370 const char *argv[100], **edir;
371 struct statfs sf;
372 pid_t pid;
373 int argc, i, status;
374 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
376 #if __GNUC__
377 (void)&optbuf;
378 (void)&name;
379 #endif
381 /* resolve the mountpoint with realpath(3) */
382 checkpath(name, mntpath);
383 name = mntpath;
385 if (mntopts == NULL)
386 mntopts = "";
387 if (options == NULL) {
388 if (*mntopts == '\0') {
389 options = "rw";
390 } else {
391 options = mntopts;
392 mntopts = "";
395 optbuf = catopt(xstrdup(mntopts), options);
397 if (strcmp(name, "/") == 0)
398 flags |= MNT_UPDATE;
399 if (flags & MNT_FORCE)
400 optbuf = catopt(optbuf, "force");
401 if (flags & MNT_RDONLY)
402 optbuf = catopt(optbuf, "ro");
404 * XXX
405 * The mount_mfs (newfs) command uses -o to select the
406 * optimization mode. We don't pass the default "-o rw"
407 * for that reason.
409 if (flags & MNT_UPDATE)
410 optbuf = catopt(optbuf, "update");
412 argc = 0;
413 argv[argc++] = vfstype;
414 mangle(optbuf, &argc, argv);
415 argv[argc++] = spec;
416 argv[argc++] = name;
417 argv[argc] = NULL;
419 if (debug) {
420 printf("exec: mount_%s", vfstype);
421 for (i = 1; i < argc; i++)
422 printf(" %s", argv[i]);
423 printf("\n");
424 return (0);
427 switch (pid = fork()) {
428 case -1: /* Error. */
429 warn("fork");
430 free(optbuf);
431 return (1);
432 case 0: /* Child. */
433 if (strcmp(vfstype, "ufs") == 0)
434 exit(mount_ufs(argc, argv));
436 /* Go find an executable. */
437 for (edir = edirs; *edir; edir++) {
438 snprintf(execname,
439 sizeof(execname), "%s/mount_%s", *edir, vfstype);
440 execv(execname, __DECONST(char * const *, argv));
442 if (errno == ENOENT) {
443 int len = 0;
444 char *cp;
445 for (edir = edirs; *edir; edir++)
446 len += strlen(*edir) + 2; /* ", " */
447 if ((cp = malloc(len)) == NULL)
448 errx(1, "malloc failed");
449 cp[0] = '\0';
450 for (edir = edirs; *edir; edir++) {
451 strcat(cp, *edir);
452 if (edir[1] != NULL)
453 strcat(cp, ", ");
455 warn("exec mount_%s not found in %s", vfstype, cp);
457 exit(1);
458 /* NOTREACHED */
459 default: /* Parent. */
460 free(optbuf);
462 if (waitpid(pid, &status, 0) < 0) {
463 warn("waitpid");
464 return (1);
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)]);
472 return (1);
475 if (verbose) {
476 if (statfs(name, &sf) < 0) {
477 warn("statfs %s", name);
478 return (1);
480 if (fstab_style)
481 putfsent(&sf);
482 else
483 prmount(&sf);
485 break;
488 return (0);
491 static void
492 prmount(struct statfs *sfp)
494 int flags;
495 struct opt *o;
496 struct passwd *pw;
498 printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
499 sfp->f_fstypename);
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);
505 flags &= ~o->o_opt;
507 if (sfp->f_owner) {
508 printf(", mounted by ");
509 if ((pw = getpwuid(sfp->f_owner)) != NULL)
510 printf("%s", pw->pw_name);
511 else
512 printf("%d", sfp->f_owner);
514 if (verbose) {
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);
522 printf(")\n");
525 static struct statfs *
526 getmntpt(const char *name)
528 struct statfs *mntbuf;
529 int i, mntsize;
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)
535 return (&mntbuf[i]);
537 return (NULL);
540 static char *
541 catopt(char *s0, const char *s1)
543 size_t i;
544 char *cp;
546 if (s1 == NULL || *s1 == '\0')
547 return s0;
549 if (s0 && *s0) {
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);
554 } else
555 cp = xstrdup(s1);
557 if (s0)
558 free(s0);
559 return (cp);
562 static void
563 mangle(char *options, int *argcp, const char **argv)
565 char *p, *s;
566 int argc;
568 argc = *argcp;
569 for (s = options; (p = strsep(&s, ",")) != NULL;)
570 if (*p != '\0') {
571 if (*p == '-') {
572 argv[argc++] = p;
573 p = strchr(p, '=');
574 if (p) {
575 *p = '\0';
576 argv[argc++] = p+1;
578 } else if (strcmp(p, "rw") != 0) {
579 argv[argc++] = "-o";
580 argv[argc++] = p;
584 *argcp = argc;
588 static char *
589 update_options(char *opts, char *fstab, int curflags)
591 char *o, *p;
592 char *cur;
593 char *expopt, *newopt, *tmpopt;
595 if (opts == NULL)
596 return xstrdup("");
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.
606 expopt = NULL;
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);
612 else
613 expopt = catopt(expopt, o);
615 free(cur);
616 free(opts);
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".
623 newopt = NULL;
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");
629 strcat(tmpopt, o);
630 remopt(newopt, tmpopt);
631 free(tmpopt);
633 if (strncmp("no", o, 2) == 0)
634 remopt(newopt, o+2);
636 newopt = catopt(newopt, o);
638 free(expopt);
640 return newopt;
643 static void
644 remopt(char *string, const char *opt)
646 char *o, *p, *r;
648 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
649 return;
651 r = string;
653 for (p = string; (o = strsep(&p, ",")) != NULL;) {
654 if (strcmp(opt, o) != 0) {
655 if (*r == ',' && *o != '\0')
656 r++;
657 while ((*r++ = *o++) != '\0')
659 *--r = ',';
662 *r = '\0';
665 static void
666 usage(void)
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");
673 exit(1);
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.
681 static void
682 printdefvals(const struct statfs *ent)
684 if (strcmp(ent->f_fstypename, "ufs") == 0) {
685 if (strcmp(ent->f_mntonname, "/") == 0)
686 printf("\t1 1\n");
687 else
688 printf("\t2 2\n");
689 } else {
690 printf("\t0 0\n");
694 static void
695 putfsent(const struct statfs *ent)
697 struct stat sb;
698 struct fstab *fst;
699 char *opts;
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);
704 free(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);
716 else
717 printdefvals(ent);
718 } else {
719 printdefvals(ent);
724 static char *
725 flags2opts(int flags)
727 char *res;
729 res = NULL;
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");
746 return res;
749 static char*
750 xstrdup(const char *str)
752 char* ret = strdup(str);
753 if(ret == NULL) {
754 errx(1, "strdup failed (could not allocate memory)");
756 return ret;