mount.c revision 1.40 1 /* $NetBSD: mount.c,v 1.40 1997/11/05 21:29:33 cgd 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.40 1997/11/05 21:29:33 cgd 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_NOEXEC, 0, "noexec" },
100 { MNT_NOSUID, 0, "nosuid" },
101 { MNT_QUOTA, 0, "with quotas" },
102 { MNT_RDONLY, 0, "read-only" },
103 { MNT_ROOTFS, 1, "root file system" },
104 { MNT_SYNCHRONOUS, 0, "synchronous" },
105 { MNT_UNION, 0, "union" },
106 { 0 }
107 };
108
109 static char ffs_fstype[] = "ffs";
110
111 int
112 main(argc, argv)
113 int argc;
114 char *argv[];
115 {
116 const char *mntfromname, *mntonname, **vfslist, *vfstype;
117 struct fstab *fs;
118 struct statfs *mntbuf;
119 FILE *mountdfp;
120 int all, ch, forceall, i, init_flags, mntsize, rval;
121 char *options;
122
123 all = forceall = init_flags = 0;
124 options = NULL;
125 vfslist = NULL;
126 vfstype = ffs_fstype;
127 while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
128 switch (ch) {
129 case 'A':
130 all = forceall = 1;
131 break;
132 case 'a':
133 all = 1;
134 break;
135 case 'd':
136 debug = 1;
137 break;
138 case 'f':
139 init_flags |= MNT_FORCE;
140 break;
141 case 'o':
142 if (*optarg)
143 catopt(&options, optarg);
144 break;
145 case 'r':
146 init_flags |= MNT_RDONLY;
147 break;
148 case 't':
149 if (vfslist != NULL)
150 errx(1, "only one -t option may be specified.");
151 vfslist = makevfslist(optarg);
152 vfstype = optarg;
153 break;
154 case 'u':
155 init_flags |= MNT_UPDATE;
156 break;
157 case 'v':
158 verbose = 1;
159 break;
160 case 'w':
161 init_flags &= ~MNT_RDONLY;
162 break;
163 case '?':
164 default:
165 usage();
166 /* NOTREACHED */
167 }
168 argc -= optind;
169 argv += optind;
170
171 #define BADTYPE(type) \
172 (strcmp(type, FSTAB_RO) && \
173 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
174
175 rval = 0;
176 switch (argc) {
177 case 0:
178 if (all)
179 while ((fs = getfsent()) != NULL) {
180 if (BADTYPE(fs->fs_type))
181 continue;
182 if (checkvfsname(fs->fs_vfstype, vfslist))
183 continue;
184 if (hasopt(fs->fs_mntops, "noauto"))
185 continue;
186 if (mountfs(fs->fs_vfstype, fs->fs_spec,
187 fs->fs_file, init_flags, options,
188 fs->fs_mntops, !forceall))
189 rval = 1;
190 }
191 else {
192 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
193 err(1, "getmntinfo");
194 for (i = 0; i < mntsize; i++) {
195 if (checkvfsname(mntbuf[i].f_fstypename,
196 vfslist))
197 continue;
198 prmount(&mntbuf[i]);
199 }
200 }
201 exit(rval);
202 /* NOTREACHED */
203 case 1:
204 if (vfslist != NULL) {
205 usage();
206 /* NOTREACHED */
207 }
208
209 if (init_flags & MNT_UPDATE) {
210 if ((mntbuf = getmntpt(*argv)) == NULL)
211 errx(1,
212 "unknown special file or file system %s.",
213 *argv);
214 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL)
215 mntfromname = fs->fs_spec;
216 else
217 mntfromname = mntbuf->f_mntfromname;
218 /* If it's an update, ignore the fstab file options. */
219 fs->fs_mntops = NULL;
220 mntonname = mntbuf->f_mntonname;
221 } else {
222 if ((fs = getfsfile(*argv)) == NULL &&
223 (fs = getfsspec(*argv)) == NULL)
224 errx(1,
225 "%s: unknown special file or file system.",
226 *argv);
227 if (BADTYPE(fs->fs_type))
228 errx(1, "%s has unknown file system type.",
229 *argv);
230 mntfromname = fs->fs_spec;
231 mntonname = fs->fs_file;
232 }
233 rval = mountfs(fs->fs_vfstype, mntfromname,
234 mntonname, init_flags, options, fs->fs_mntops, 0);
235 break;
236 case 2:
237 /*
238 * If -t flag has not been specified, and spec contains either
239 * a ':' or a '@' then assume that an NFS filesystem is being
240 * specified ala Sun.
241 */
242 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
243 vfstype = "nfs";
244 rval = mountfs(vfstype,
245 argv[0], argv[1], init_flags, options, NULL, 0);
246 break;
247 default:
248 usage();
249 /* NOTREACHED */
250 }
251
252 /*
253 * If the mount was successfully, and done by root, tell mountd the
254 * good news. Pid checks are probably unnecessary, but don't hurt.
255 */
256 if (rval == 0 && getuid() == 0 &&
257 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
258 int pid;
259
260 if (fscanf(mountdfp, "%d", &pid) == 1 &&
261 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
262 err(1, "signal mountd");
263 (void)fclose(mountdfp);
264 }
265
266 exit(rval);
267 /* NOTREACHED */
268 }
269
270 int
271 hasopt(mntopts, option)
272 const char *mntopts, *option;
273 {
274 int negative, found;
275 char *opt, *optbuf;
276
277 if (option[0] == 'n' && option[1] == 'o') {
278 negative = 1;
279 option += 2;
280 } else
281 negative = 0;
282 optbuf = strdup(mntopts);
283 found = 0;
284 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
285 if (opt[0] == 'n' && opt[1] == 'o') {
286 if (!strcasecmp(opt + 2, option))
287 found = negative;
288 } else if (!strcasecmp(opt, option))
289 found = !negative;
290 }
291 free(optbuf);
292 return (found);
293 }
294
295 int
296 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted)
297 const char *vfstype, *spec, *name, *options, *mntopts;
298 int flags, skipmounted;
299 {
300 /* List of directories containing mount_xxx subcommands. */
301 static const char *edirs[] = {
302 _PATH_SBIN,
303 _PATH_USRSBIN,
304 NULL
305 };
306 const char **argv, **edir;
307 struct statfs *sfp, sf;
308 pid_t pid;
309 int argc, numfs, i, status, maxargc;
310 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
311 mntpath[MAXPATHLEN];
312
313 #ifdef __GNUC__
314 (void) &name;
315 (void) &optbuf;
316 (void) &vfstype;
317 #endif
318
319 if (realpath(name, mntpath) == NULL) {
320 warn("realpath %s", name);
321 return (1);
322 }
323
324 name = mntpath;
325
326 optbuf = NULL;
327 if (mntopts)
328 catopt(&optbuf, mntopts);
329 if (options)
330 catopt(&optbuf, options);
331 if (!mntopts && !options)
332 catopt(&optbuf, "rw");
333
334 if (!strcmp(name, "/"))
335 flags |= MNT_UPDATE;
336 else if (skipmounted) {
337 if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
338 warn("getmntinfo");
339 return (1);
340 }
341 for(i = 0; i < numfs; i++) {
342 /* XXX can't check f_mntfromname,
343 thanks to mfs, union, etc. */
344 if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
345 strncmp(vfstype, sfp[i].f_fstypename,
346 MFSNAMELEN) == 0) {
347 if (verbose)
348 (void)printf("%s on %s type %.*s: %s\n",
349 sfp[i].f_mntfromname,
350 sfp[i].f_mntonname,
351 MFSNAMELEN,
352 sfp[i].f_fstypename,
353 "already mounted");
354 return (0);
355 }
356 }
357 }
358 if (flags & MNT_FORCE)
359 catopt(&optbuf, "force");
360 if (flags & MNT_RDONLY)
361 catopt(&optbuf, "ro");
362
363 if (flags & MNT_UPDATE) {
364 catopt(&optbuf, "update");
365 /* Figure out the fstype only if we defaulted to ffs */
366 if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
367 vfstype = sf.f_fstypename;
368 }
369
370 maxargc = 64;
371 argv = malloc(sizeof(char *) * maxargc);
372
373 (void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
374 argc = 0;
375 argv[argc++] = execbase;
376 if (optbuf)
377 mangle(optbuf, &argc, &argv, &maxargc);
378 argv[argc++] = spec;
379 argv[argc++] = name;
380 argv[argc] = NULL;
381
382 if (verbose) {
383 (void)printf("exec:");
384 for (i = 0; i < argc; i++)
385 (void)printf(" %s", argv[i]);
386 (void)printf("\n");
387 }
388
389 switch (pid = vfork()) {
390 case -1: /* Error. */
391 warn("vfork");
392 if (optbuf)
393 free(optbuf);
394 return (1);
395
396 case 0: /* Child. */
397 if (debug)
398 _exit(0);
399
400 /* Go find an executable. */
401 edir = edirs;
402 do {
403 (void)snprintf(execname,
404 sizeof(execname), "%s/%s", *edir, execbase);
405 (void)execv(execname, (char * const *)argv);
406 if (errno != ENOENT)
407 warn("exec %s for %s", execname, name);
408 } while (*++edir != NULL);
409
410 if (errno == ENOENT)
411 warnx("%s not found for %s", execbase, name);
412 _exit(1);
413 /* NOTREACHED */
414
415 default: /* Parent. */
416 if (optbuf)
417 free(optbuf);
418
419 if (waitpid(pid, &status, 0) < 0) {
420 warn("waitpid");
421 return (1);
422 }
423
424 if (WIFEXITED(status)) {
425 if (WEXITSTATUS(status) != 0)
426 return (WEXITSTATUS(status));
427 } else if (WIFSIGNALED(status)) {
428 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
429 return (1);
430 }
431
432 if (verbose) {
433 if (statfs(name, &sf) < 0) {
434 warn("statfs %s", name);
435 return (1);
436 }
437 prmount(&sf);
438 }
439 break;
440 }
441
442 return (0);
443 }
444
445 void
446 prmount(sfp)
447 struct statfs *sfp;
448 {
449 int flags;
450 struct opt *o;
451 struct passwd *pw;
452 int f;
453
454 (void)printf("%s on %s type %.*s", sfp->f_mntfromname, sfp->f_mntonname,
455 MFSNAMELEN, sfp->f_fstypename);
456
457 flags = sfp->f_flags & MNT_VISFLAGMASK;
458 for (f = 0, o = optnames; flags && o->o_opt; o++)
459 if (flags & o->o_opt) {
460 if (!o->o_silent)
461 (void)printf("%s%s", !f++ ? " (" : ", ",
462 o->o_name);
463 flags &= ~o->o_opt;
464 }
465 if (flags)
466 (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
467 flags & (flags - 1) ? "s" : "", flags);
468 if (sfp->f_owner) {
469 (void)printf("%smounted by ", !f++ ? " (" : ", ");
470 if ((pw = getpwuid(sfp->f_owner)) != NULL)
471 (void)printf("%s", pw->pw_name);
472 else
473 (void)printf("%d", sfp->f_owner);
474 }
475 (void)printf(f ? ")\n" : "\n");
476 }
477
478 struct statfs *
479 getmntpt(name)
480 const char *name;
481 {
482 struct statfs *mntbuf;
483 int i, mntsize;
484
485 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
486 for (i = 0; i < mntsize; i++)
487 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
488 strcmp(mntbuf[i].f_mntonname, name) == 0)
489 return (&mntbuf[i]);
490 return (NULL);
491 }
492
493 void
494 catopt(sp, o)
495 char **sp;
496 const char *o;
497 {
498 char *s;
499 size_t i, j;
500
501 s = *sp;
502 if (s) {
503 i = strlen(s);
504 j = i + 1 + strlen(o) + 1;
505 s = realloc(s, j);
506 (void)snprintf(s + i, j, ",%s", o);
507 } else
508 s = strdup(o);
509 *sp = s;
510 }
511
512 static void
513 mangle(options, argcp, argvp, maxargcp)
514 char *options;
515 int *argcp, *maxargcp;
516 const char ***argvp;
517 {
518 char *p, *s;
519 int argc, maxargc;
520 const char **argv;
521
522 argc = *argcp;
523 argv = *argvp;
524 maxargc = *maxargcp;
525
526 for (s = options; (p = strsep(&s, ",")) != NULL;) {
527 /* Always leave space for one more argument and the NULL. */
528 if (argc >= maxargc - 4) {
529 maxargc <<= 1;
530 argv = realloc(argv, maxargc * sizeof(char *));
531 }
532 if (*p != '\0')
533 if (*p == '-') {
534 argv[argc++] = p;
535 p = strchr(p, '=');
536 if (p) {
537 *p = '\0';
538 argv[argc++] = p+1;
539 }
540 } else if (strcmp(p, "rw") != 0) {
541 argv[argc++] = "-o";
542 argv[argc++] = p;
543 }
544 }
545
546 *argcp = argc;
547 *argvp = argv;
548 *maxargcp = maxargc;
549 }
550
551 void
552 usage()
553 {
554
555 (void)fprintf(stderr,
556 "usage: mount %s %s\n mount %s\n mount %s\n",
557 "[-dfruvw] [-o options] [-t ffs | external_type]",
558 "special node",
559 "[-adfruvw] [-t ffs | external_type]",
560 "[-dfruvw] special | node");
561 exit(1);
562 /* NOTREACHED */
563 }
564