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