mount.c revision 1.55 1 /* $NetBSD: mount.c,v 1.55 2000/11/01 04:10:02 enami Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
45 #else
46 __RCSID("$NetBSD: mount.c,v 1.55 2000/11/01 04:10:02 enami Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/mount.h>
52 #include <sys/wait.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <fstab.h>
57 #include <pwd.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #define MOUNTNAMES
65 #include <fcntl.h>
66 #include <sys/disklabel.h>
67 #include <sys/ioctl.h>
68
69 #include "pathnames.h"
70 #include "vfslist.h"
71
72 static int debug, verbose;
73
74 static void catopt __P((char **, const char *));
75 static const char *
76 getfslab __P((const char *str));
77 static struct statfs *
78 getmntpt __P((const char *));
79 static int hasopt __P((const char *, const char *));
80 static void mangle __P((char *, int *, const char ***, int *));
81 static int mountfs __P((const char *, const char *, const char *,
82 int, const char *, const char *, int));
83 static void prmount __P((struct statfs *));
84 static void usage __P((void));
85
86 void checkname __P((int, char *[]));
87 int main __P((int, char *[]));
88
89 /* Map from mount otions to printable formats. */
90 static const struct opt {
91 int o_opt;
92 int o_silent;
93 const char *o_name;
94 } optnames[] = {
95 { MNT_ASYNC, 0, "asynchronous" },
96 { MNT_DEFEXPORTED, 1, "exported to the world" },
97 { MNT_EXKERB, 1, "kerberos uid mapping" },
98 { MNT_EXPORTED, 0, "NFS exported" },
99 { MNT_EXPORTANON, 1, "anon uid mapping" },
100 { MNT_EXRDONLY, 1, "exported read-only" },
101 { MNT_LOCAL, 0, "local" },
102 { MNT_NOATIME, 0, "noatime" },
103 { MNT_NOCOREDUMP, 0, "nocoredump" },
104 { MNT_NODEV, 0, "nodev" },
105 { MNT_NODEVMTIME, 0, "nodevmtime" },
106 { MNT_NOEXEC, 0, "noexec" },
107 { MNT_NOSUID, 0, "nosuid" },
108 { MNT_QUOTA, 0, "with quotas" },
109 { MNT_RDONLY, 0, "read-only" },
110 { MNT_ROOTFS, 1, "root file system" },
111 { MNT_SYMPERM, 0, "symperm" },
112 { MNT_SYNCHRONOUS, 0, "synchronous" },
113 { MNT_UNION, 0, "union" },
114 { MNT_SOFTDEP, 0, "soft dependencies" },
115 { 0 }
116 };
117
118 static char ffs_fstype[] = "ffs";
119
120 int
121 main(argc, argv)
122 int argc;
123 char *argv[];
124 {
125 const char *mntfromname, *mntonname, **vfslist, *vfstype;
126 struct fstab *fs;
127 struct statfs *mntbuf;
128 FILE *mountdfp;
129 int all, ch, forceall, i, init_flags, mntsize, rval;
130 char *options;
131 const char *mountopts, *fstypename;
132
133 /* if called as specific mount, call it's main mount routine */
134 checkname(argc, argv);
135
136 /* started as "mount" */
137 all = forceall = init_flags = 0;
138 options = NULL;
139 vfslist = NULL;
140 vfstype = ffs_fstype;
141 while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
142 switch (ch) {
143 case 'A':
144 all = forceall = 1;
145 break;
146 case 'a':
147 all = 1;
148 break;
149 case 'd':
150 debug = 1;
151 break;
152 case 'f':
153 init_flags |= MNT_FORCE;
154 break;
155 case 'o':
156 if (*optarg)
157 catopt(&options, optarg);
158 break;
159 case 'r':
160 init_flags |= MNT_RDONLY;
161 break;
162 case 't':
163 if (vfslist != NULL)
164 errx(1,
165 "only one -t option may be specified.");
166 vfslist = makevfslist(optarg);
167 vfstype = optarg;
168 break;
169 case 'u':
170 init_flags |= MNT_UPDATE;
171 break;
172 case 'v':
173 verbose = 1;
174 break;
175 case 'w':
176 init_flags &= ~MNT_RDONLY;
177 break;
178 case '?':
179 default:
180 usage();
181 /* NOTREACHED */
182 }
183 argc -= optind;
184 argv += optind;
185
186 #define BADTYPE(type) \
187 (strcmp(type, FSTAB_RO) && \
188 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
189
190 rval = 0;
191 switch (argc) {
192 case 0:
193 if (all)
194 while ((fs = getfsent()) != NULL) {
195 if (BADTYPE(fs->fs_type))
196 continue;
197 if (checkvfsname(fs->fs_vfstype, vfslist))
198 continue;
199 if (hasopt(fs->fs_mntops, "noauto"))
200 continue;
201 if (mountfs(fs->fs_vfstype, fs->fs_spec,
202 fs->fs_file, init_flags, options,
203 fs->fs_mntops, !forceall))
204 rval = 1;
205 }
206 else {
207 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
208 err(1, "getmntinfo");
209 for (i = 0; i < mntsize; i++) {
210 if (checkvfsname(mntbuf[i].f_fstypename,
211 vfslist))
212 continue;
213 prmount(&mntbuf[i]);
214 }
215 }
216 exit(rval);
217 /* NOTREACHED */
218 case 1:
219 if (vfslist != NULL) {
220 usage();
221 /* NOTREACHED */
222 }
223
224 if (init_flags & MNT_UPDATE) {
225 if ((mntbuf = getmntpt(*argv)) == NULL)
226 errx(1,
227 "unknown special file or file system %s.",
228 *argv);
229 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
230 mntfromname = fs->fs_spec;
231 /* ignore the fstab file options. */
232 fs->fs_mntops = NULL;
233 } else
234 mntfromname = mntbuf->f_mntfromname;
235 mntonname = mntbuf->f_mntonname;
236 fstypename = mntbuf->f_fstypename;
237 mountopts = NULL;
238 } else {
239 if ((fs = getfsfile(*argv)) == NULL &&
240 (fs = getfsspec(*argv)) == NULL)
241 errx(1,
242 "%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 mntfromname = fs->fs_spec;
248 mntonname = fs->fs_file;
249 fstypename = fs->fs_vfstype;
250 mountopts = fs->fs_mntops;
251 }
252 rval = mountfs(fstypename, mntfromname,
253 mntonname, init_flags, options, mountopts, 0);
254 break;
255 case 2:
256 /*
257 * If -t flag has not been specified, and spec contains either
258 * a ':' or a '@' then assume that an NFS filesystem is being
259 * specified ala Sun.
260 */
261 if (vfslist == NULL) {
262 if (strpbrk(argv[0], ":@") != NULL)
263 vfstype = "nfs";
264 else {
265 vfstype = getfslab(argv[0]);
266 if (vfstype == NULL)
267 vfstype = ffs_fstype;
268 }
269 }
270 rval = mountfs(vfstype,
271 argv[0], argv[1], init_flags, options, NULL, 0);
272 break;
273 default:
274 usage();
275 /* NOTREACHED */
276 }
277
278 /*
279 * If the mount was successfully, and done by root, tell mountd the
280 * good news. Pid checks are probably unnecessary, but don't hurt.
281 */
282 if (rval == 0 && getuid() == 0 &&
283 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
284 int pid;
285
286 if (fscanf(mountdfp, "%d", &pid) == 1 &&
287 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
288 err(1, "signal mountd");
289 (void)fclose(mountdfp);
290 }
291
292 exit(rval);
293 /* NOTREACHED */
294 }
295
296 int
297 hasopt(mntopts, option)
298 const char *mntopts, *option;
299 {
300 int negative, found;
301 char *opt, *optbuf;
302
303 if (option[0] == 'n' && option[1] == 'o') {
304 negative = 1;
305 option += 2;
306 } else
307 negative = 0;
308 optbuf = strdup(mntopts);
309 found = 0;
310 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
311 if (opt[0] == 'n' && opt[1] == 'o') {
312 if (!strcasecmp(opt + 2, option))
313 found = negative;
314 } else if (!strcasecmp(opt, option))
315 found = !negative;
316 }
317 free(optbuf);
318 return (found);
319 }
320
321 static int
322 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
323 const char *vfstype, *spec, *name, *options, *mntopts;
324 int flags, skipmounted;
325 {
326 /* List of directories containing mount_xxx subcommands. */
327 static const char *edirs[] = {
328 _PATH_SBIN,
329 _PATH_USRSBIN,
330 NULL
331 };
332 const char **argv, **edir;
333 struct statfs *sfp, sf;
334 pid_t pid;
335 int argc, numfs, i, status, maxargc;
336 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
337 mntpath[MAXPATHLEN];
338
339 #ifdef __GNUC__
340 (void) &name;
341 (void) &optbuf;
342 (void) &vfstype;
343 #endif
344
345 if (realpath(name, mntpath) == NULL) {
346 warn("realpath %s", name);
347 return (1);
348 }
349
350 name = mntpath;
351
352 optbuf = NULL;
353 if (mntopts)
354 catopt(&optbuf, mntopts);
355 if (options)
356 catopt(&optbuf, options);
357 if (!mntopts && !options)
358 catopt(&optbuf, "rw");
359
360 if (!strcmp(name, "/"))
361 flags |= MNT_UPDATE;
362 else if (skipmounted) {
363 if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
364 warn("getmntinfo");
365 return (1);
366 }
367 for(i = 0; i < numfs; i++) {
368 /*
369 * XXX can't check f_mntfromname,
370 * thanks to mfs, union, etc.
371 */
372 if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
373 strncmp(vfstype, sfp[i].f_fstypename,
374 MFSNAMELEN) == 0) {
375 if (verbose)
376 (void)printf("%s on %s type %.*s: "
377 "%s\n",
378 sfp[i].f_mntfromname,
379 sfp[i].f_mntonname,
380 MFSNAMELEN,
381 sfp[i].f_fstypename,
382 "already mounted");
383 return (0);
384 }
385 }
386 }
387 if (flags & MNT_FORCE)
388 catopt(&optbuf, "force");
389 if (flags & MNT_RDONLY)
390 catopt(&optbuf, "ro");
391
392 if (flags & MNT_UPDATE) {
393 catopt(&optbuf, "update");
394 /* Figure out the fstype only if we defaulted to ffs */
395 if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
396 vfstype = sf.f_fstypename;
397 }
398
399 maxargc = 64;
400 argv = malloc(sizeof(char *) * maxargc);
401
402 (void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
403 argc = 0;
404 argv[argc++] = execbase;
405 if (optbuf)
406 mangle(optbuf, &argc, &argv, &maxargc);
407 argv[argc++] = spec;
408 argv[argc++] = name;
409 argv[argc] = NULL;
410
411 if (verbose) {
412 (void)printf("exec:");
413 for (i = 0; i < argc; i++)
414 (void)printf(" %s", argv[i]);
415 (void)printf("\n");
416 }
417
418 switch (pid = vfork()) {
419 case -1: /* Error. */
420 warn("vfork");
421 if (optbuf)
422 free(optbuf);
423 return (1);
424
425 case 0: /* Child. */
426 if (debug)
427 _exit(0);
428
429 /* Go find an executable. */
430 edir = edirs;
431 do {
432 (void)snprintf(execname,
433 sizeof(execname), "%s/%s", *edir, execbase);
434 (void)execv(execname, (char * const *)argv);
435 if (errno != ENOENT)
436 warn("exec %s for %s", execname, name);
437 } while (*++edir != NULL);
438
439 if (errno == ENOENT)
440 warnx("%s not found for %s", execbase, name);
441 _exit(1);
442 /* NOTREACHED */
443
444 default: /* Parent. */
445 if (optbuf)
446 free(optbuf);
447
448 if (waitpid(pid, &status, 0) < 0) {
449 warn("waitpid");
450 return (1);
451 }
452
453 if (WIFEXITED(status)) {
454 if (WEXITSTATUS(status) != 0)
455 return (WEXITSTATUS(status));
456 } else if (WIFSIGNALED(status)) {
457 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
458 return (1);
459 }
460
461 if (verbose) {
462 if (statfs(name, &sf) < 0) {
463 warn("statfs %s", name);
464 return (1);
465 }
466 prmount(&sf);
467 }
468 break;
469 }
470
471 return (0);
472 }
473
474 static void
475 prmount(sfp)
476 struct statfs *sfp;
477 {
478 int flags;
479 const struct opt *o;
480 struct passwd *pw;
481 int f;
482
483 (void)printf("%s on %s type %.*s", sfp->f_mntfromname,
484 sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
485
486 flags = sfp->f_flags & MNT_VISFLAGMASK;
487 for (f = 0, o = optnames; flags && o->o_opt; o++)
488 if (flags & o->o_opt) {
489 if (!o->o_silent)
490 (void)printf("%s%s", !f++ ? " (" : ", ",
491 o->o_name);
492 flags &= ~o->o_opt;
493 }
494 if (flags)
495 (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
496 flags & (flags - 1) ? "s" : "", flags);
497 if (sfp->f_owner) {
498 (void)printf("%smounted by ", !f++ ? " (" : ", ");
499 if ((pw = getpwuid(sfp->f_owner)) != NULL)
500 (void)printf("%s", pw->pw_name);
501 else
502 (void)printf("%d", sfp->f_owner);
503 }
504 if (verbose)
505 (void)printf("%swrites: sync %ld async %ld)\n",
506 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
507 else
508 (void)printf("%s", f ? ")\n" : "\n");
509 }
510
511 static struct statfs *
512 getmntpt(name)
513 const char *name;
514 {
515 struct statfs *mntbuf;
516 int i, mntsize;
517
518 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
519 for (i = 0; i < mntsize; i++)
520 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
521 strcmp(mntbuf[i].f_mntonname, name) == 0)
522 return (&mntbuf[i]);
523 return (NULL);
524 }
525
526 static void
527 catopt(sp, o)
528 char **sp;
529 const char *o;
530 {
531 char *s;
532 size_t i, j;
533
534 s = *sp;
535 if (s) {
536 i = strlen(s);
537 j = i + 1 + strlen(o) + 1;
538 s = realloc(s, j);
539 (void)snprintf(s + i, j, ",%s", o);
540 } else
541 s = strdup(o);
542 *sp = s;
543 }
544
545 static void
546 mangle(options, argcp, argvp, maxargcp)
547 char *options;
548 int *argcp, *maxargcp;
549 const char ***argvp;
550 {
551 char *p, *s;
552 int argc, maxargc;
553 const char **argv;
554
555 argc = *argcp;
556 argv = *argvp;
557 maxargc = *maxargcp;
558
559 for (s = options; (p = strsep(&s, ",")) != NULL;) {
560 /* Always leave space for one more argument and the NULL. */
561 if (argc >= maxargc - 4) {
562 maxargc <<= 1;
563 argv = realloc(argv, maxargc * sizeof(char *));
564 }
565 if (*p != '\0') {
566 if (*p == '-') {
567 argv[argc++] = p;
568 p = strchr(p, '=');
569 if (p) {
570 *p = '\0';
571 argv[argc++] = p+1;
572 }
573 } else if (strcmp(p, "rw") != 0) {
574 argv[argc++] = "-o";
575 argv[argc++] = p;
576 }
577 }
578 }
579
580 *argcp = argc;
581 *argvp = argv;
582 *maxargcp = maxargc;
583 }
584
585 /* Deduce the filesystem type from the disk label. */
586 static const char *
587 getfslab(str)
588 const char *str;
589 {
590 struct disklabel dl;
591 int fd;
592 int part;
593 const char *vfstype;
594 u_char fstype;
595 char buf[MAXPATHLEN + 1];
596 char *sp, *ep;
597
598 if ((fd = open(str, O_RDONLY)) == -1) {
599 /*
600 * Iff we get EBUSY try the raw device. Since mount always uses
601 * the block device we know we are never passed a raw device.
602 */
603 if (errno != EBUSY)
604 err(1, "cannot open `%s'", str);
605 strlcpy(buf, str, MAXPATHLEN);
606 if ((sp = strrchr(buf, '/')) != NULL)
607 ++sp;
608 else
609 sp = buf;
610 for (ep = sp + strlen(sp) + 1; ep > sp; ep--)
611 *ep = *(ep - 1);
612 *sp = 'r';
613
614 /* Silently fail here - mount call can display error */
615 if ((fd = open(buf, O_RDONLY)) == -1)
616 return (NULL);
617 }
618
619 if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
620 (void) close(fd);
621 return (NULL);
622 }
623
624 (void) close(fd);
625
626 part = str[strlen(str) - 1] - 'a';
627
628 if (part < 0 || part >= dl.d_npartitions)
629 errx(1, "partition `%s' is not defined on disk", str);
630
631 /* Return NULL for unknown types - caller can fall back to ffs */
632 if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
633 vfstype = NULL;
634 else
635 vfstype = mountnames[fstype];
636
637 return (vfstype);
638 }
639
640 static void
641 usage()
642 {
643
644 (void)fprintf(stderr,
645 "usage: mount %s\n mount %s\n mount %s\n",
646 "[-dfruvw] [-o options] [-t ffs | external_type] special node",
647 "[-adfruvw] [-t ffs | external_type]",
648 "[-dfruvw] special | node");
649 exit(1);
650 /* NOTREACHED */
651 }
652