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