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