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