mount.c revision 1.47 1 /* $NetBSD: mount.c,v 1.47 1999/11/16 11:53:17 enami 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.47 1999/11/16 11:53:17 enami 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 #include "pathnames.h"
65
66 int debug, verbose;
67
68 int checkvfsname __P((const char *, const char **));
69 void catopt __P((char **, const char *));
70 struct statfs
71 *getmntpt __P((const char *));
72 int hasopt __P((const char *, const char *));
73 const char
74 **makevfslist __P((char *));
75 static void
76 mangle __P((char *, int *, const char ***, int *));
77 int mountfs __P((const char *, const char *, const char *,
78 int, const char *, const char *, int));
79 void prmount __P((struct statfs *));
80 void usage __P((void));
81 int main __P((int, char *[]));
82
83 /* Map from mount otions to printable formats. */
84 static struct opt {
85 int o_opt;
86 int o_silent;
87 const char *o_name;
88 } optnames[] = {
89 { MNT_ASYNC, 0, "asynchronous" },
90 { MNT_DEFEXPORTED, 1, "exported to the world" },
91 { MNT_EXKERB, 1, "kerberos uid mapping" },
92 { MNT_EXPORTED, 0, "NFS exported" },
93 { MNT_EXPORTANON, 1, "anon uid mapping" },
94 { MNT_EXRDONLY, 1, "exported read-only" },
95 { MNT_LOCAL, 0, "local" },
96 { MNT_NOATIME, 0, "noatime" },
97 { MNT_NOCOREDUMP, 0, "nocoredump" },
98 { MNT_NODEV, 0, "nodev" },
99 { MNT_NODEVMTIME, 0, "nodevmtime" },
100 { MNT_NOEXEC, 0, "noexec" },
101 { MNT_NOSUID, 0, "nosuid" },
102 { MNT_QUOTA, 0, "with quotas" },
103 { MNT_RDONLY, 0, "read-only" },
104 { MNT_ROOTFS, 1, "root file system" },
105 { MNT_SYMPERM, 0, "symperm" },
106 { MNT_SYNCHRONOUS, 0, "synchronous" },
107 { MNT_UNION, 0, "union" },
108 { MNT_SOFTDEP, 0, "soft dependencies" },
109 { 0 }
110 };
111
112 static char ffs_fstype[] = "ffs";
113
114 int
115 main(argc, argv)
116 int argc;
117 char *argv[];
118 {
119 const char *mntfromname, *mntonname, **vfslist, *vfstype;
120 struct fstab *fs;
121 struct statfs *mntbuf;
122 FILE *mountdfp;
123 int all, ch, forceall, i, init_flags, mntsize, rval;
124 char *options;
125 const char *mountopts, *fstypename;
126
127 all = forceall = init_flags = 0;
128 options = NULL;
129 vfslist = NULL;
130 vfstype = ffs_fstype;
131 while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
132 switch (ch) {
133 case 'A':
134 all = forceall = 1;
135 break;
136 case 'a':
137 all = 1;
138 break;
139 case 'd':
140 debug = 1;
141 break;
142 case 'f':
143 init_flags |= MNT_FORCE;
144 break;
145 case 'o':
146 if (*optarg)
147 catopt(&options, optarg);
148 break;
149 case 'r':
150 init_flags |= MNT_RDONLY;
151 break;
152 case 't':
153 if (vfslist != NULL)
154 errx(1, "only one -t option may be specified.");
155 vfslist = makevfslist(optarg);
156 vfstype = optarg;
157 break;
158 case 'u':
159 init_flags |= MNT_UPDATE;
160 break;
161 case 'v':
162 verbose = 1;
163 break;
164 case 'w':
165 init_flags &= ~MNT_RDONLY;
166 break;
167 case '?':
168 default:
169 usage();
170 /* NOTREACHED */
171 }
172 argc -= optind;
173 argv += optind;
174
175 #define BADTYPE(type) \
176 (strcmp(type, FSTAB_RO) && \
177 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
178
179 rval = 0;
180 switch (argc) {
181 case 0:
182 if (all)
183 while ((fs = getfsent()) != NULL) {
184 if (BADTYPE(fs->fs_type))
185 continue;
186 if (checkvfsname(fs->fs_vfstype, vfslist))
187 continue;
188 if (hasopt(fs->fs_mntops, "noauto"))
189 continue;
190 if (mountfs(fs->fs_vfstype, fs->fs_spec,
191 fs->fs_file, init_flags, options,
192 fs->fs_mntops, !forceall))
193 rval = 1;
194 }
195 else {
196 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
197 err(1, "getmntinfo");
198 for (i = 0; i < mntsize; i++) {
199 if (checkvfsname(mntbuf[i].f_fstypename,
200 vfslist))
201 continue;
202 prmount(&mntbuf[i]);
203 }
204 }
205 exit(rval);
206 /* NOTREACHED */
207 case 1:
208 if (vfslist != NULL) {
209 usage();
210 /* NOTREACHED */
211 }
212
213 if (init_flags & MNT_UPDATE) {
214 if ((mntbuf = getmntpt(*argv)) == NULL)
215 errx(1,
216 "unknown special file or file system %s.",
217 *argv);
218 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
219 mntfromname = fs->fs_spec;
220 /* ignore the fstab file options. */
221 fs->fs_mntops = NULL;
222 } else
223 mntfromname = mntbuf->f_mntfromname;
224 mntonname = mntbuf->f_mntonname;
225 fstypename = mntbuf->f_fstypename;
226 mountopts = NULL;
227 } else {
228 if ((fs = getfsfile(*argv)) == NULL &&
229 (fs = getfsspec(*argv)) == NULL)
230 errx(1,
231 "%s: unknown special file or file system.",
232 *argv);
233 if (BADTYPE(fs->fs_type))
234 errx(1, "%s has unknown file system type.",
235 *argv);
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);
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 && strpbrk(argv[0], ":@") != NULL)
251 vfstype = "nfs";
252 rval = mountfs(vfstype,
253 argv[0], argv[1], init_flags, options, NULL, 0);
254 break;
255 default:
256 usage();
257 /* NOTREACHED */
258 }
259
260 /*
261 * If the mount was successfully, and done by root, tell mountd the
262 * good news. Pid checks are probably unnecessary, but don't hurt.
263 */
264 if (rval == 0 && getuid() == 0 &&
265 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
266 int pid;
267
268 if (fscanf(mountdfp, "%d", &pid) == 1 &&
269 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
270 err(1, "signal mountd");
271 (void)fclose(mountdfp);
272 }
273
274 exit(rval);
275 /* NOTREACHED */
276 }
277
278 int
279 hasopt(mntopts, option)
280 const char *mntopts, *option;
281 {
282 int negative, found;
283 char *opt, *optbuf;
284
285 if (option[0] == 'n' && option[1] == 'o') {
286 negative = 1;
287 option += 2;
288 } else
289 negative = 0;
290 optbuf = strdup(mntopts);
291 found = 0;
292 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
293 if (opt[0] == 'n' && opt[1] == 'o') {
294 if (!strcasecmp(opt + 2, option))
295 found = negative;
296 } else if (!strcasecmp(opt, option))
297 found = !negative;
298 }
299 free(optbuf);
300 return (found);
301 }
302
303 int
304 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
305 const char *vfstype, *spec, *name, *options, *mntopts;
306 int flags, skipmounted;
307 {
308 /* List of directories containing mount_xxx subcommands. */
309 static const char *edirs[] = {
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 argc, numfs, i, status, maxargc;
318 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
319 mntpath[MAXPATHLEN];
320
321 #ifdef __GNUC__
322 (void) &name;
323 (void) &optbuf;
324 (void) &vfstype;
325 #endif
326
327 if (realpath(name, mntpath) == NULL) {
328 warn("realpath %s", name);
329 return (1);
330 }
331
332 name = mntpath;
333
334 optbuf = NULL;
335 if (mntopts)
336 catopt(&optbuf, mntopts);
337 if (options)
338 catopt(&optbuf, options);
339 if (!mntopts && !options)
340 catopt(&optbuf, "rw");
341
342 if (!strcmp(name, "/"))
343 flags |= MNT_UPDATE;
344 else if (skipmounted) {
345 if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
346 warn("getmntinfo");
347 return (1);
348 }
349 for(i = 0; i < numfs; i++) {
350 /* XXX can't check f_mntfromname,
351 thanks to mfs, union, etc. */
352 if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
353 strncmp(vfstype, sfp[i].f_fstypename,
354 MFSNAMELEN) == 0) {
355 if (verbose)
356 (void)printf("%s on %s type %.*s: %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) {
391 (void)printf("exec:");
392 for (i = 0; i < argc; i++)
393 (void)printf(" %s", argv[i]);
394 (void)printf("\n");
395 }
396
397 switch (pid = vfork()) {
398 case -1: /* Error. */
399 warn("vfork");
400 if (optbuf)
401 free(optbuf);
402 return (1);
403
404 case 0: /* Child. */
405 if (debug)
406 _exit(0);
407
408 /* Go find an executable. */
409 edir = edirs;
410 do {
411 (void)snprintf(execname,
412 sizeof(execname), "%s/%s", *edir, execbase);
413 (void)execv(execname, (char * const *)argv);
414 if (errno != ENOENT)
415 warn("exec %s for %s", execname, name);
416 } while (*++edir != NULL);
417
418 if (errno == ENOENT)
419 warnx("%s not found for %s", execbase, name);
420 _exit(1);
421 /* NOTREACHED */
422
423 default: /* Parent. */
424 if (optbuf)
425 free(optbuf);
426
427 if (waitpid(pid, &status, 0) < 0) {
428 warn("waitpid");
429 return (1);
430 }
431
432 if (WIFEXITED(status)) {
433 if (WEXITSTATUS(status) != 0)
434 return (WEXITSTATUS(status));
435 } else if (WIFSIGNALED(status)) {
436 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
437 return (1);
438 }
439
440 if (verbose) {
441 if (statfs(name, &sf) < 0) {
442 warn("statfs %s", name);
443 return (1);
444 }
445 prmount(&sf);
446 }
447 break;
448 }
449
450 return (0);
451 }
452
453 void
454 prmount(sfp)
455 struct statfs *sfp;
456 {
457 int flags;
458 struct opt *o;
459 struct passwd *pw;
460 int f;
461
462 (void)printf("%s on %s type %.*s", sfp->f_mntfromname, sfp->f_mntonname,
463 MFSNAMELEN, sfp->f_fstypename);
464
465 flags = sfp->f_flags & MNT_VISFLAGMASK;
466 for (f = 0, o = optnames; flags && o->o_opt; o++)
467 if (flags & o->o_opt) {
468 if (!o->o_silent)
469 (void)printf("%s%s", !f++ ? " (" : ", ",
470 o->o_name);
471 flags &= ~o->o_opt;
472 }
473 if (flags)
474 (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
475 flags & (flags - 1) ? "s" : "", flags);
476 if (sfp->f_owner) {
477 (void)printf("%smounted by ", !f++ ? " (" : ", ");
478 if ((pw = getpwuid(sfp->f_owner)) != NULL)
479 (void)printf("%s", pw->pw_name);
480 else
481 (void)printf("%d", sfp->f_owner);
482 }
483 if (verbose)
484 (void)printf("%swrites: sync %ld async %ld)\n",
485 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
486 else
487 (void)printf(f ? ")\n" : "\n");
488 }
489
490 struct statfs *
491 getmntpt(name)
492 const char *name;
493 {
494 struct statfs *mntbuf;
495 int i, mntsize;
496
497 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
498 for (i = 0; i < mntsize; i++)
499 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
500 strcmp(mntbuf[i].f_mntonname, name) == 0)
501 return (&mntbuf[i]);
502 return (NULL);
503 }
504
505 void
506 catopt(sp, o)
507 char **sp;
508 const char *o;
509 {
510 char *s;
511 size_t i, j;
512
513 s = *sp;
514 if (s) {
515 i = strlen(s);
516 j = i + 1 + strlen(o) + 1;
517 s = realloc(s, j);
518 (void)snprintf(s + i, j, ",%s", o);
519 } else
520 s = strdup(o);
521 *sp = s;
522 }
523
524 static void
525 mangle(options, argcp, argvp, maxargcp)
526 char *options;
527 int *argcp, *maxargcp;
528 const char ***argvp;
529 {
530 char *p, *s;
531 int argc, maxargc;
532 const char **argv;
533
534 argc = *argcp;
535 argv = *argvp;
536 maxargc = *maxargcp;
537
538 for (s = options; (p = strsep(&s, ",")) != NULL;) {
539 /* Always leave space for one more argument and the NULL. */
540 if (argc >= maxargc - 4) {
541 maxargc <<= 1;
542 argv = realloc(argv, maxargc * sizeof(char *));
543 }
544 if (*p != '\0') {
545 if (*p == '-') {
546 argv[argc++] = p;
547 p = strchr(p, '=');
548 if (p) {
549 *p = '\0';
550 argv[argc++] = p+1;
551 }
552 } else if (strcmp(p, "rw") != 0) {
553 argv[argc++] = "-o";
554 argv[argc++] = p;
555 }
556 }
557 }
558
559 *argcp = argc;
560 *argvp = argv;
561 *maxargcp = maxargc;
562 }
563
564 void
565 usage()
566 {
567
568 (void)fprintf(stderr,
569 "usage: mount %s %s\n mount %s\n mount %s\n",
570 "[-dfruvw] [-o options] [-t ffs | external_type]",
571 "special node",
572 "[-adfruvw] [-t ffs | external_type]",
573 "[-dfruvw] special | node");
574 exit(1);
575 /* NOTREACHED */
576 }
577