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