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