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