mount.c revision 1.64 1 /* $NetBSD: mount.c,v 1.64 2002/09/23 03:39:41 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.64 2002/09/23 03:39:41 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
456 if (buf == NULL) {
457 ptr = tbuf;
458 buflen = sizeof(tbuf) - 1;
459 } else {
460 ptr = buf;
461 buflen--;
462 }
463 (void)close(pfd[1]);
464 (void)signal(SIGPIPE, SIG_IGN);
465 while ((nread = read(pfd[0], ptr, buflen)) > 0) {
466 buflen -= nread;
467 ptr += nread;
468 }
469 *ptr = '\0';
470 if (buflen == 0) {
471 while (read(pfd[0], &nread, sizeof(nread)) > 0)
472 continue;
473 }
474 if (buf == NULL)
475 (void)fprintf(stdout, "%s", tbuf);
476 }
477
478 if (waitpid(pid, &status, 0) < 0) {
479 warn("waitpid");
480 return (1);
481 }
482
483 if (WIFEXITED(status)) {
484 if (WEXITSTATUS(status) != 0)
485 return (WEXITSTATUS(status));
486 } else if (WIFSIGNALED(status)) {
487 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
488 return (1);
489 }
490
491 if (buf == NULL) {
492 if (verbose) {
493 if (statfs(name, &sf) < 0) {
494 warn("statfs %s", name);
495 return (1);
496 }
497 prmount(&sf);
498 }
499 }
500 break;
501 }
502
503 return (0);
504 }
505
506 static void
507 prmount(sfp)
508 struct statfs *sfp;
509 {
510 int flags;
511 const struct opt *o;
512 struct passwd *pw;
513 int f;
514
515 (void)printf("%s on %s type %.*s", sfp->f_mntfromname,
516 sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
517
518 flags = sfp->f_flags & MNT_VISFLAGMASK;
519 for (f = 0, o = optnames; flags && o <
520 &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
521 if (flags & o->o_opt) {
522 if (!o->o_silent || verbose)
523 (void)printf("%s%s", !f++ ? " (" : ", ",
524 o->o_name);
525 flags &= ~o->o_opt;
526 }
527 if (flags)
528 (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
529 flags & (flags - 1) ? "s" : "", flags);
530 if (sfp->f_owner) {
531 (void)printf("%smounted by ", !f++ ? " (" : ", ");
532 if ((pw = getpwuid(sfp->f_owner)) != NULL)
533 (void)printf("%s", pw->pw_name);
534 else
535 (void)printf("%d", sfp->f_owner);
536 }
537 if (verbose) {
538 (void)printf("%swrites: sync %ld async %ld",
539 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
540 if (verbose > 1) {
541 char buf[2048];
542
543 if (getmntargs(sfp, buf, sizeof(buf)))
544 printf(", [%s: %s]", sfp->f_fstypename, buf);
545 }
546 printf(")\n");
547 } else
548 (void)printf("%s", f ? ")\n" : "\n");
549 }
550
551 static int
552 getmntargs(sfs, buf, buflen)
553 struct statfs *sfs;
554 char *buf;
555 size_t buflen;
556 {
557
558 if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
559 "getargs", NULL, 0, buf, buflen))
560 return (0);
561 else {
562 if (*buf == '\0')
563 return (0);
564 if ((buf = strchr(buf, '\n')) != NULL)
565 *buf = '\0';
566 return (1);
567 }
568 }
569
570 static struct statfs *
571 getmntpt(name)
572 const char *name;
573 {
574 struct statfs *mntbuf;
575 int i, mntsize;
576
577 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
578 for (i = 0; i < mntsize; i++)
579 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
580 strcmp(mntbuf[i].f_mntonname, name) == 0)
581 return (&mntbuf[i]);
582 return (NULL);
583 }
584
585 static void
586 catopt(sp, o)
587 char **sp;
588 const char *o;
589 {
590 char *s;
591 size_t i, j;
592
593 s = *sp;
594 if (s) {
595 i = strlen(s);
596 j = i + 1 + strlen(o) + 1;
597 s = realloc(s, j);
598 (void)snprintf(s + i, j, ",%s", o);
599 } else
600 s = strdup(o);
601 *sp = s;
602 }
603
604 static void
605 mangle(options, argcp, argvp, maxargcp)
606 char *options;
607 int *argcp, *maxargcp;
608 const char ***argvp;
609 {
610 char *p, *s;
611 int argc, maxargc;
612 const char **argv;
613
614 argc = *argcp;
615 argv = *argvp;
616 maxargc = *maxargcp;
617
618 for (s = options; (p = strsep(&s, ",")) != NULL;) {
619 /* Always leave space for one more argument and the NULL. */
620 if (argc >= maxargc - 4) {
621 maxargc <<= 1;
622 argv = realloc(argv, maxargc * sizeof(char *));
623 }
624 if (*p != '\0') {
625 if (*p == '-') {
626 argv[argc++] = p;
627 p = strchr(p, '=');
628 if (p) {
629 *p = '\0';
630 argv[argc++] = p+1;
631 }
632 } else if (strcmp(p, "rw") != 0) {
633 argv[argc++] = "-o";
634 argv[argc++] = p;
635 }
636 }
637 }
638
639 *argcp = argc;
640 *argvp = argv;
641 *maxargcp = maxargc;
642 }
643
644 /* Deduce the filesystem type from the disk label. */
645 static const char *
646 getfslab(str)
647 const char *str;
648 {
649 struct disklabel dl;
650 int fd;
651 int part;
652 const char *vfstype;
653 u_char fstype;
654 char buf[MAXPATHLEN + 1];
655 char *sp, *ep;
656
657 if ((fd = open(str, O_RDONLY)) == -1) {
658 /*
659 * Iff we get EBUSY try the raw device. Since mount always uses
660 * the block device we know we are never passed a raw device.
661 */
662 if (errno != EBUSY)
663 err(1, "cannot open `%s'", str);
664 strlcpy(buf, str, MAXPATHLEN);
665 if ((sp = strrchr(buf, '/')) != NULL)
666 ++sp;
667 else
668 sp = buf;
669 for (ep = sp + strlen(sp) + 1; ep > sp; ep--)
670 *ep = *(ep - 1);
671 *sp = 'r';
672
673 /* Silently fail here - mount call can display error */
674 if ((fd = open(buf, O_RDONLY)) == -1)
675 return (NULL);
676 }
677
678 if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
679 (void) close(fd);
680 return (NULL);
681 }
682
683 (void) close(fd);
684
685 part = str[strlen(str) - 1] - 'a';
686
687 if (part < 0 || part >= dl.d_npartitions)
688 return (NULL);
689
690 /* Return NULL for unknown types - caller can fall back to ffs */
691 if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
692 vfstype = NULL;
693 else
694 vfstype = mountnames[fstype];
695
696 return (vfstype);
697 }
698
699 static void
700 usage()
701 {
702
703 (void)fprintf(stderr,
704 "usage: mount %s\n mount %s\n mount %s\n",
705 "[-Aadfruvw] [-t type]",
706 "[-dfruvw] special | node",
707 "[-dfruvw] [-o options] [-t type] special node");
708 exit(1);
709 /* NOTREACHED */
710 }
711