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