mount.c revision 1.22 1 /* $NetBSD: mount.c,v 1.22 1995/07/12 06:05:04 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.22 1995/07/12 06:05:04 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 const char *o_name;
84 } optnames[] = {
85 { MNT_ASYNC, "asynchronous" },
86 { MNT_EXPORTED, "NFS exported" },
87 { MNT_LOCAL, "local" },
88 { MNT_NODEV, "nodev" },
89 { MNT_NOEXEC, "noexec" },
90 { MNT_NOSUID, "nosuid" },
91 { MNT_QUOTA, "with quotas" },
92 { MNT_RDONLY, "read-only" },
93 { MNT_SYNCHRONOUS, "synchronous" },
94 { MNT_UNION, "union" },
95 { NULL }
96 };
97
98 int
99 main(argc, argv)
100 int argc;
101 char * const argv[];
102 {
103 const char *mntonname, *vfstype;
104 struct fstab *fs;
105 struct statfs *mntbuf;
106 FILE *mountdfp;
107 pid_t pid;
108 int all, ch, forceall, i, init_flags, mntsize, rval;
109 char *options;
110
111 all = forceall = init_flags = 0;
112 options = NULL;
113 vfstype = "ufs";
114 while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != EOF)
115 switch (ch) {
116 case 'A':
117 all = forceall = 1;
118 break;
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, !forceall))
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, 0);
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, 0);
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, skipmounted)
275 const char *vfstype, *spec, *name, *options, *mntopts;
276 int flags, skipmounted;
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", name);
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 else if (skipmounted) {
312 if (statfs(name, &sf) < 0) {
313 warn("statfs %s", name);
314 return (1);
315 }
316 /* XXX can't check f_mntfromname, thanks to mfs, union, etc. */
317 if (strncmp(name, sf.f_mntonname, MNAMELEN) == 0 &&
318 strncmp(vfstype, sf.f_fstypename, MFSNAMELEN) == 0) {
319 if (verbose)
320 (void)printf("%s on %s type %.*s: %s\n",
321 sf.f_mntfromname, sf.f_mntonname,
322 MFSNAMELEN, sf.f_fstypename,
323 "already mounted");
324 return (0);
325 }
326 }
327 if (flags & MNT_FORCE)
328 optbuf = catopt(optbuf, "force");
329 if (flags & MNT_RDONLY)
330 optbuf = catopt(optbuf, "ro");
331 /*
332 * XXX
333 * The mount_mfs (newfs) command uses -o to select the
334 * optimisation mode. We don't pass the default "-o rw"
335 * for that reason.
336 */
337 if (flags & MNT_UPDATE)
338 optbuf = catopt(optbuf, "update");
339
340 argc = 0;
341 argv[argc++] = vfstype;
342 mangle(optbuf, &argc, argv);
343 argv[argc++] = spec;
344 argv[argc++] = name;
345 argv[argc] = NULL;
346
347 if (debug) {
348 (void)printf("exec: mount_%s", vfstype);
349 for (i = 1; i < argc; i++)
350 (void)printf(" %s", argv[i]);
351 (void)printf("\n");
352 return (0);
353 }
354
355 switch (pid = vfork()) {
356 case -1: /* Error. */
357 warn("vfork");
358 free(optbuf);
359 return (1);
360 case 0: /* Child. */
361 /* Go find an executable. */
362 edir = edirs;
363 do {
364 (void)snprintf(execname,
365 sizeof(execname), "%s/mount_%s", *edir, vfstype);
366 execv(execname, (char * const *)argv);
367 if (errno != ENOENT)
368 warn("exec %s for %s", execname, name);
369 } while (*++edir != NULL);
370
371 if (errno == ENOENT)
372 warn("exec %s for %s", execname, name);
373 exit(1);
374 /* NOTREACHED */
375 default: /* Parent. */
376 free(optbuf);
377
378 if (waitpid(pid, &status, 0) < 0) {
379 warn("waitpid");
380 return (1);
381 }
382
383 if (WIFEXITED(status)) {
384 if (WEXITSTATUS(status) != 0)
385 return (WEXITSTATUS(status));
386 } else if (WIFSIGNALED(status)) {
387 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
388 return (1);
389 }
390
391 if (verbose) {
392 if (statfs(name, &sf) < 0) {
393 warn("statfs %s", name);
394 return (1);
395 }
396 prmount(&sf);
397 }
398 break;
399 }
400
401 return (0);
402 }
403
404 void
405 prmount(sf)
406 struct statfs *sf;
407 {
408 int flags;
409 struct opt *o;
410 int f;
411
412 (void)printf("%s on %s type %.*s", sf->f_mntfromname, sf->f_mntonname,
413 MFSNAMELEN, sf->f_fstypename);
414
415 flags = sf->f_flags & MNT_VISFLAGMASK;
416 for (f = 0, o = optnames; flags && o->o_opt; o++)
417 if (flags & o->o_opt) {
418 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
419 flags &= ~o->o_opt;
420 }
421 (void)printf(f ? ")\n" : "\n");
422 }
423
424 struct statfs *
425 getmntpt(name)
426 const char *name;
427 {
428 struct statfs *mntbuf;
429 int i, mntsize;
430
431 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
432 for (i = 0; i < mntsize; i++)
433 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
434 strcmp(mntbuf[i].f_mntonname, name) == 0)
435 return (&mntbuf[i]);
436 return (NULL);
437 }
438
439 static enum { IN_LIST, NOT_IN_LIST } which;
440
441 int
442 selected(type)
443 const char *type;
444 {
445 char **av;
446
447 /* If no type specified, it's always selected. */
448 if (typelist == NULL)
449 return (1);
450 for (av = typelist; *av != NULL; ++av)
451 if (!strncmp(type, *av, MFSNAMELEN))
452 return (which == IN_LIST ? 1 : 0);
453 return (which == IN_LIST ? 0 : 1);
454 }
455
456 void
457 maketypelist(fslist)
458 char *fslist;
459 {
460 int i;
461 char *nextcp, **av;
462
463 if ((fslist == NULL) || (fslist[0] == '\0'))
464 errx(1, "empty type list");
465
466 /*
467 * XXX
468 * Note: the syntax is "noxxx,yyy" for no xxx's and
469 * no yyy's, not the more intuitive "noyyy,noyyy".
470 */
471 if (fslist[0] == 'n' && fslist[1] == 'o') {
472 fslist += 2;
473 which = NOT_IN_LIST;
474 } else
475 which = IN_LIST;
476
477 /* Count the number of types. */
478 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
479 ++nextcp;
480
481 /* Build an array of that many types. */
482 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
483 err(1, NULL);
484 av[0] = fslist;
485 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
486 *nextcp = '\0';
487 av[i] = ++nextcp;
488 }
489 /* Terminate the array. */
490 av[i] = NULL;
491 }
492
493 char *
494 catopt(s0, s1)
495 char *s0;
496 const char *s1;
497 {
498 size_t i;
499 char *cp;
500
501 if (s0 && *s0) {
502 i = strlen(s0) + strlen(s1) + 1 + 1;
503 if ((cp = malloc(i)) == NULL)
504 err(1, NULL);
505 (void)snprintf(cp, i, "%s,%s", s0, s1);
506 } else
507 cp = strdup(s1);
508
509 if (s0)
510 free(s0);
511 return (cp);
512 }
513
514 void
515 mangle(options, argcp, argv)
516 char *options;
517 int *argcp;
518 const char **argv;
519 {
520 char *p, *s;
521 int argc;
522
523 argc = *argcp;
524 for (s = options; (p = strsep(&s, ",")) != NULL;)
525 if (*p != '\0')
526 if (*p == '-') {
527 argv[argc++] = p;
528 p = strchr(p, '=');
529 if (p) {
530 *p = '\0';
531 argv[argc++] = p+1;
532 }
533 } else if (strcmp(p, "rw") != 0) {
534 argv[argc++] = "-o";
535 argv[argc++] = p;
536 }
537
538 *argcp = argc;
539 }
540
541 void
542 usage()
543 {
544
545 (void)fprintf(stderr,
546 "usage: mount %s %s\n mount %s\n mount %s\n",
547 "[-dfruvw] [-o options] [-t ufs | external_type]",
548 "special node",
549 "[-adfruvw] [-t ufs | external_type]",
550 "[-dfruvw] special | node");
551 exit(1);
552 }
553