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