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