mount.c revision 1.63 1 /* $NetBSD: mount.c,v 1.63 2002/09/23 03:35:22 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.63 2002/09/23 03:35:22 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 getmntargs __P((struct statfs *, char *, size_t));
80 static int hasopt __P((const char *, const char *));
81 static void mangle __P((char *, int *, const char ***, int *));
82 static int mountfs __P((const char *, const char *, const char *,
83 int, const char *, const char *, int, char *, size_t));
84 static void prmount __P((struct statfs *));
85 static void usage __P((void));
86
87 #ifndef NO_MOUNT_PROGS
88 void checkname __P((int, char *[]));
89 #endif
90 int main __P((int, char *[]));
91
92 /* Map from mount otions to printable formats. */
93 static const struct opt {
94 int o_opt;
95 int o_silent;
96 const char *o_name;
97 } optnames[] = {
98 __MNT_FLAGS
99 };
100
101 static char ffs_fstype[] = "ffs";
102
103 int
104 main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108 const char *mntfromname, *mntonname, **vfslist, *vfstype;
109 struct fstab *fs;
110 struct statfs *mntbuf;
111 FILE *mountdfp;
112 int all, ch, forceall, i, init_flags, mntsize, rval;
113 char *options;
114 const char *mountopts, *fstypename;
115
116 #ifndef NO_MOUNT_PROGS
117 /* if called as specific mount, call it's main mount routine */
118 checkname(argc, argv);
119 #endif
120
121 /* started as "mount" */
122 all = forceall = init_flags = 0;
123 options = NULL;
124 vfslist = NULL;
125 vfstype = ffs_fstype;
126 while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
127 switch (ch) {
128 case 'A':
129 all = forceall = 1;
130 break;
131 case 'a':
132 all = 1;
133 break;
134 case 'd':
135 debug = 1;
136 break;
137 case 'f':
138 init_flags |= MNT_FORCE;
139 break;
140 case 'o':
141 if (*optarg)
142 catopt(&options, optarg);
143 break;
144 case 'r':
145 init_flags |= MNT_RDONLY;
146 break;
147 case 't':
148 if (vfslist != NULL)
149 errx(1,
150 "only one -t option may be specified.");
151 vfslist = makevfslist(optarg);
152 vfstype = optarg;
153 break;
154 case 'u':
155 init_flags |= MNT_UPDATE;
156 break;
157 case 'v':
158 verbose++;
159 break;
160 case 'w':
161 init_flags &= ~MNT_RDONLY;
162 break;
163 case '?':
164 default:
165 usage();
166 /* NOTREACHED */
167 }
168 argc -= optind;
169 argv += optind;
170
171 #define BADTYPE(type) \
172 (strcmp(type, FSTAB_RO) && \
173 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
174
175 rval = 0;
176 switch (argc) {
177 case 0:
178 if (all)
179 while ((fs = getfsent()) != NULL) {
180 if (BADTYPE(fs->fs_type))
181 continue;
182 if (checkvfsname(fs->fs_vfstype, vfslist))
183 continue;
184 if (hasopt(fs->fs_mntops, "noauto"))
185 continue;
186 if (mountfs(fs->fs_vfstype, fs->fs_spec,
187 fs->fs_file, init_flags, options,
188 fs->fs_mntops, !forceall, NULL, 0))
189 rval = 1;
190 }
191 else {
192 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
193 err(1, "getmntinfo");
194 for (i = 0; i < mntsize; i++) {
195 if (checkvfsname(mntbuf[i].f_fstypename,
196 vfslist))
197 continue;
198 prmount(&mntbuf[i]);
199 }
200 }
201 exit(rval);
202 /* NOTREACHED */
203 case 1:
204 if (vfslist != NULL) {
205 usage();
206 /* NOTREACHED */
207 }
208
209 if (init_flags & MNT_UPDATE) {
210 if ((mntbuf = getmntpt(*argv)) == NULL)
211 errx(1,
212 "unknown special file or file system %s.",
213 *argv);
214 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
215 mntfromname = fs->fs_spec;
216 /* ignore the fstab file options. */
217 fs->fs_mntops = NULL;
218 } else
219 mntfromname = mntbuf->f_mntfromname;
220 mntonname = mntbuf->f_mntonname;
221 fstypename = mntbuf->f_fstypename;
222 mountopts = NULL;
223 } else {
224 if ((fs = getfsfile(*argv)) == NULL &&
225 (fs = getfsspec(*argv)) == NULL)
226 errx(1,
227 "%s: unknown special file or file system.",
228 *argv);
229 if (BADTYPE(fs->fs_type))
230 errx(1, "%s has unknown file system type.",
231 *argv);
232 mntfromname = fs->fs_spec;
233 mntonname = fs->fs_file;
234 fstypename = fs->fs_vfstype;
235 mountopts = fs->fs_mntops;
236 }
237 rval = mountfs(fstypename, mntfromname,
238 mntonname, init_flags, options, mountopts, 0, NULL, 0);
239 break;
240 case 2:
241 /*
242 * If -t flag has not been specified, and spec contains either
243 * a ':' or a '@' then assume that an NFS filesystem is being
244 * specified ala Sun.
245 */
246 if (vfslist == NULL) {
247 if (strpbrk(argv[0], ":@") != NULL)
248 vfstype = "nfs";
249 else {
250 vfstype = getfslab(argv[0]);
251 if (vfstype == NULL)
252 vfstype = ffs_fstype;
253 }
254 }
255 rval = mountfs(vfstype,
256 argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
257 break;
258 default:
259 usage();
260 /* NOTREACHED */
261 }
262
263 /*
264 * If the mount was successfully, and done by root, tell mountd the
265 * good news. Pid checks are probably unnecessary, but don't hurt.
266 */
267 if (rval == 0 && getuid() == 0 &&
268 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
269 int pid;
270
271 if (fscanf(mountdfp, "%d", &pid) == 1 &&
272 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
273 err(1, "signal mountd");
274 (void)fclose(mountdfp);
275 }
276
277 exit(rval);
278 /* NOTREACHED */
279 }
280
281 int
282 hasopt(mntopts, option)
283 const char *mntopts, *option;
284 {
285 int negative, found;
286 char *opt, *optbuf;
287
288 if (option[0] == 'n' && option[1] == 'o') {
289 negative = 1;
290 option += 2;
291 } else
292 negative = 0;
293 optbuf = strdup(mntopts);
294 found = 0;
295 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
296 if (opt[0] == 'n' && opt[1] == 'o') {
297 if (!strcasecmp(opt + 2, option))
298 found = negative;
299 } else if (!strcasecmp(opt, option))
300 found = !negative;
301 }
302 free(optbuf);
303 return (found);
304 }
305
306 static int
307 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted, buf, buflen)
308 const char *vfstype, *spec, *name, *options, *mntopts;
309 int flags, skipmounted;
310 char *buf;
311 size_t buflen;
312 {
313 /* List of directories containing mount_xxx subcommands. */
314 static const char *edirs[] = {
315 #ifdef _PATH_RESCUE
316 _PATH_RESCUE,
317 #endif
318 _PATH_SBIN,
319 _PATH_USRSBIN,
320 NULL
321 };
322 const char **argv, **edir;
323 struct statfs *sfp, sf;
324 pid_t pid;
325 int pfd[2];
326 int argc, numfs, i, status, maxargc;
327 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
328 mntpath[MAXPATHLEN];
329
330 #ifdef __GNUC__
331 (void) &name;
332 (void) &optbuf;
333 (void) &vfstype;
334 #endif
335
336 if (realpath(name, mntpath) == NULL) {
337 warn("realpath %s", name);
338 return (1);
339 }
340
341 name = mntpath;
342
343 optbuf = NULL;
344 if (mntopts)
345 catopt(&optbuf, mntopts);
346 if (options)
347 catopt(&optbuf, options);
348 if (!mntopts && !options)
349 catopt(&optbuf, "rw");
350
351 if (!strcmp(name, "/"))
352 flags |= MNT_UPDATE;
353 else if (skipmounted) {
354 if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
355 warn("getmntinfo");
356 return (1);
357 }
358 for(i = 0; i < numfs; i++) {
359 /*
360 * XXX can't check f_mntfromname,
361 * thanks to mfs, union, etc.
362 */
363 if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
364 strncmp(vfstype, sfp[i].f_fstypename,
365 MFSNAMELEN) == 0) {
366 if (verbose)
367 (void)printf("%s on %s type %.*s: "
368 "%s\n",
369 sfp[i].f_mntfromname,
370 sfp[i].f_mntonname,
371 MFSNAMELEN,
372 sfp[i].f_fstypename,
373 "already mounted");
374 return (0);
375 }
376 }
377 }
378 if (flags & MNT_FORCE)
379 catopt(&optbuf, "force");
380 if (flags & MNT_RDONLY)
381 catopt(&optbuf, "ro");
382
383 if (flags & MNT_UPDATE) {
384 catopt(&optbuf, "update");
385 /* Figure out the fstype only if we defaulted to ffs */
386 if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
387 vfstype = sf.f_fstypename;
388 }
389
390 maxargc = 64;
391 argv = malloc(sizeof(char *) * maxargc);
392
393 (void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
394 argc = 0;
395 argv[argc++] = execbase;
396 if (optbuf)
397 mangle(optbuf, &argc, &argv, &maxargc);
398 argv[argc++] = spec;
399 argv[argc++] = name;
400 argv[argc] = NULL;
401
402 if (verbose && buf == NULL) {
403 (void)printf("exec:");
404 for (i = 0; i < argc; i++)
405 (void)printf(" %s", argv[i]);
406 (void)printf("\n");
407 }
408
409 if (buf) {
410 if (pipe(pfd) == -1)
411 warn("Cannot create pipe");
412 }
413
414 switch (pid = vfork()) {
415 case -1: /* Error. */
416 warn("vfork");
417 if (optbuf)
418 free(optbuf);
419 return (1);
420
421 case 0: /* Child. */
422 if (debug)
423 _exit(0);
424
425 if (buf) {
426 (void)close(pfd[0]);
427 (void)close(STDOUT_FILENO);
428 if (dup2(pfd[1], STDOUT_FILENO) == -1)
429 warn("Cannot open fd to mount program");
430 }
431
432 /* Go find an executable. */
433 edir = edirs;
434 do {
435 (void)snprintf(execname,
436 sizeof(execname), "%s/%s", *edir, execbase);
437 (void)execv(execname, (char * const *)argv);
438 if (errno != ENOENT)
439 warn("exec %s for %s", execname, name);
440 } while (*++edir != NULL);
441
442 if (errno == ENOENT)
443 warnx("%s not found for %s", execbase, name);
444 _exit(1);
445 /* NOTREACHED */
446
447 default: /* Parent. */
448 if (optbuf)
449 free(optbuf);
450
451 if (buf || (options != NULL
452 && strstr(options, "getargs") != NULL)) {
453 char tbuf[1024], *ptr;
454 int nread;
455 if (buf == NULL) {
456 ptr = tbuf;
457 buflen = sizeof(tbuf) - 1;
458 } else {
459 ptr = buf;
460 buflen--;
461 }
462 (void)close(pfd[1]);
463 (void)signal(SIGPIPE, SIG_IGN);
464 while ((nread = read(pfd[0], ptr, buflen)) > 0) {
465 buflen -= nread;
466 ptr += nread;
467 }
468 *ptr = '\0';
469 if (buflen == 0) {
470 while (read(pfd[0], &nread, sizeof(nread)) > 0)
471 continue;
472 }
473 if (buf == NULL)
474 (void)fprintf(stdout, "%s", tbuf);
475 }
476
477 if (waitpid(pid, &status, 0) < 0) {
478 warn("waitpid");
479 return (1);
480 }
481
482 if (WIFEXITED(status)) {
483 if (WEXITSTATUS(status) != 0)
484 return (WEXITSTATUS(status));
485 } else if (WIFSIGNALED(status)) {
486 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
487 return (1);
488 }
489
490 if (buf == NULL) {
491 if (verbose) {
492 if (statfs(name, &sf) < 0) {
493 warn("statfs %s", name);
494 return (1);
495 }
496 prmount(&sf);
497 }
498 }
499 break;
500 }
501
502 return (0);
503 }
504
505 static void
506 prmount(sfp)
507 struct statfs *sfp;
508 {
509 int flags;
510 const struct opt *o;
511 struct passwd *pw;
512 int f;
513
514 (void)printf("%s on %s type %.*s", sfp->f_mntfromname,
515 sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
516
517 flags = sfp->f_flags & MNT_VISFLAGMASK;
518 for (f = 0, o = optnames; flags && o <
519 &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
520 if (flags & o->o_opt) {
521 if (!o->o_silent || verbose)
522 (void)printf("%s%s", !f++ ? " (" : ", ",
523 o->o_name);
524 flags &= ~o->o_opt;
525 }
526 if (flags)
527 (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
528 flags & (flags - 1) ? "s" : "", flags);
529 if (sfp->f_owner) {
530 (void)printf("%smounted by ", !f++ ? " (" : ", ");
531 if ((pw = getpwuid(sfp->f_owner)) != NULL)
532 (void)printf("%s", pw->pw_name);
533 else
534 (void)printf("%d", sfp->f_owner);
535 }
536 if (verbose) {
537 (void)printf("%swrites: sync %ld async %ld",
538 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
539 if (verbose > 1) {
540 char buf[2048];
541
542 if (getmntargs(sfp, buf, sizeof(buf)))
543 printf(", [%s: %s]", sfp->f_fstypename, buf);
544 }
545 printf(")\n");
546 } else
547 (void)printf("%s", f ? ")\n" : "\n");
548 }
549
550 static int
551 getmntargs(sfs, buf, buflen)
552 struct statfs *sfs;
553 char *buf;
554 size_t buflen;
555 {
556 if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
557 "getargs", NULL, 0, buf, buflen))
558 return 0;
559 else {
560 if (*buf == '\0')
561 return 0;
562 if ((buf = strchr(buf, '\n')) != NULL)
563 *buf = '\0';
564 return 1;
565 }
566 }
567
568 static struct statfs *
569 getmntpt(name)
570 const char *name;
571 {
572 struct statfs *mntbuf;
573 int i, mntsize;
574
575 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
576 for (i = 0; i < mntsize; i++)
577 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
578 strcmp(mntbuf[i].f_mntonname, name) == 0)
579 return (&mntbuf[i]);
580 return (NULL);
581 }
582
583 static void
584 catopt(sp, o)
585 char **sp;
586 const char *o;
587 {
588 char *s;
589 size_t i, j;
590
591 s = *sp;
592 if (s) {
593 i = strlen(s);
594 j = i + 1 + strlen(o) + 1;
595 s = realloc(s, j);
596 (void)snprintf(s + i, j, ",%s", o);
597 } else
598 s = strdup(o);
599 *sp = s;
600 }
601
602 static void
603 mangle(options, argcp, argvp, maxargcp)
604 char *options;
605 int *argcp, *maxargcp;
606 const char ***argvp;
607 {
608 char *p, *s;
609 int argc, maxargc;
610 const char **argv;
611
612 argc = *argcp;
613 argv = *argvp;
614 maxargc = *maxargcp;
615
616 for (s = options; (p = strsep(&s, ",")) != NULL;) {
617 /* Always leave space for one more argument and the NULL. */
618 if (argc >= maxargc - 4) {
619 maxargc <<= 1;
620 argv = realloc(argv, maxargc * sizeof(char *));
621 }
622 if (*p != '\0') {
623 if (*p == '-') {
624 argv[argc++] = p;
625 p = strchr(p, '=');
626 if (p) {
627 *p = '\0';
628 argv[argc++] = p+1;
629 }
630 } else if (strcmp(p, "rw") != 0) {
631 argv[argc++] = "-o";
632 argv[argc++] = p;
633 }
634 }
635 }
636
637 *argcp = argc;
638 *argvp = argv;
639 *maxargcp = maxargc;
640 }
641
642 /* Deduce the filesystem type from the disk label. */
643 static const char *
644 getfslab(str)
645 const char *str;
646 {
647 struct disklabel dl;
648 int fd;
649 int part;
650 const char *vfstype;
651 u_char fstype;
652 char buf[MAXPATHLEN + 1];
653 char *sp, *ep;
654
655 if ((fd = open(str, O_RDONLY)) == -1) {
656 /*
657 * Iff we get EBUSY try the raw device. Since mount always uses
658 * the block device we know we are never passed a raw device.
659 */
660 if (errno != EBUSY)
661 err(1, "cannot open `%s'", str);
662 strlcpy(buf, str, MAXPATHLEN);
663 if ((sp = strrchr(buf, '/')) != NULL)
664 ++sp;
665 else
666 sp = buf;
667 for (ep = sp + strlen(sp) + 1; ep > sp; ep--)
668 *ep = *(ep - 1);
669 *sp = 'r';
670
671 /* Silently fail here - mount call can display error */
672 if ((fd = open(buf, O_RDONLY)) == -1)
673 return (NULL);
674 }
675
676 if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
677 (void) close(fd);
678 return (NULL);
679 }
680
681 (void) close(fd);
682
683 part = str[strlen(str) - 1] - 'a';
684
685 if (part < 0 || part >= dl.d_npartitions)
686 return (NULL);
687
688 /* Return NULL for unknown types - caller can fall back to ffs */
689 if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
690 vfstype = NULL;
691 else
692 vfstype = mountnames[fstype];
693
694 return (vfstype);
695 }
696
697 static void
698 usage()
699 {
700
701 (void)fprintf(stderr,
702 "usage: mount %s\n mount %s\n mount %s\n",
703 "[-Aadfruvw] [-t type]",
704 "[-dfruvw] special | node",
705 "[-dfruvw] [-o options] [-t type] special node");
706 exit(1);
707 /* NOTREACHED */
708 }
709