mount.c revision 1.39 1 /* $NetBSD: mount.c,v 1.39 1997/11/01 18:38:29 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.39 1997/11/01 18:38:29 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 }
384
385 switch (pid = vfork()) {
386 case -1: /* Error. */
387 warn("vfork");
388 if (optbuf)
389 free(optbuf);
390 return (1);
391
392 case 0: /* Child. */
393 if (debug)
394 _exit(0);
395
396 /* Go find an executable. */
397 edir = edirs;
398 do {
399 (void)snprintf(execname,
400 sizeof(execname), "%s/%s", *edir, execbase);
401 execv(execname, (char * const *)argv);
402 if (errno != ENOENT)
403 warn("exec %s for %s", execname, name);
404 } while (*++edir != NULL);
405
406 if (errno == ENOENT)
407 warnx("%s not found for %s", execbase, name);
408 _exit(1);
409 /* NOTREACHED */
410
411 default: /* Parent. */
412 if (optbuf)
413 free(optbuf);
414
415 if (waitpid(pid, &status, 0) < 0) {
416 warn("waitpid");
417 return (1);
418 }
419
420 if (WIFEXITED(status)) {
421 if (WEXITSTATUS(status) != 0)
422 return (WEXITSTATUS(status));
423 } else if (WIFSIGNALED(status)) {
424 warnx("%s: %s", name, strsignal(WTERMSIG(status)));
425 return (1);
426 }
427
428 if (verbose) {
429 if (statfs(name, &sf) < 0) {
430 warn("statfs %s", name);
431 return (1);
432 }
433 prmount(&sf);
434 }
435 break;
436 }
437
438 return (0);
439 }
440
441 void
442 prmount(sfp)
443 struct statfs *sfp;
444 {
445 int flags;
446 struct opt *o;
447 struct passwd *pw;
448 int f;
449
450 (void)printf("%s on %s type %.*s", sfp->f_mntfromname, sfp->f_mntonname,
451 MFSNAMELEN, sfp->f_fstypename);
452
453 flags = sfp->f_flags & MNT_VISFLAGMASK;
454 for (f = 0, o = optnames; flags && o->o_opt; o++)
455 if (flags & o->o_opt) {
456 if (!o->o_silent)
457 (void)printf("%s%s", !f++ ? " (" : ", ",
458 o->o_name);
459 flags &= ~o->o_opt;
460 }
461 if (flags)
462 (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
463 flags & (flags - 1) ? "s" : "", flags);
464 if (sfp->f_owner) {
465 (void)printf("%smounted by ", !f++ ? " (" : ", ");
466 if ((pw = getpwuid(sfp->f_owner)) != NULL)
467 (void)printf("%s", pw->pw_name);
468 else
469 (void)printf("%d", sfp->f_owner);
470 }
471 (void)printf(f ? ")\n" : "\n");
472 }
473
474 struct statfs *
475 getmntpt(name)
476 const char *name;
477 {
478 struct statfs *mntbuf;
479 int i, mntsize;
480
481 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
482 for (i = 0; i < mntsize; i++)
483 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
484 strcmp(mntbuf[i].f_mntonname, name) == 0)
485 return (&mntbuf[i]);
486 return (NULL);
487 }
488
489 void
490 catopt(sp, o)
491 char **sp;
492 const char *o;
493 {
494 char *s;
495 size_t i, j;
496
497 s = *sp;
498 if (s) {
499 i = strlen(s);
500 j = i + 1 + strlen(o) + 1;
501 s = realloc(s, j);
502 (void)snprintf(s + i, j, ",%s", o);
503 } else
504 s = strdup(o);
505 *sp = s;
506 }
507
508 static void
509 mangle(options, argcp, argvp, maxargcp)
510 char *options;
511 int *argcp, *maxargcp;
512 const char ***argvp;
513 {
514 char *p, *s;
515 int argc, maxargc;
516 const char **argv;
517
518 argc = *argcp;
519 argv = *argvp;
520 maxargc = *maxargcp;
521
522 for (s = options; (p = strsep(&s, ",")) != NULL;) {
523 /* Always leave space for one more argument and the NULL. */
524 if (argc >= maxargc - 4) {
525 maxargc <<= 1;
526 argv = realloc(argv, maxargc * sizeof(char *));
527 }
528 if (*p != '\0')
529 if (*p == '-') {
530 argv[argc++] = p;
531 p = strchr(p, '=');
532 if (p) {
533 *p = '\0';
534 argv[argc++] = p+1;
535 }
536 } else if (strcmp(p, "rw") != 0) {
537 argv[argc++] = "-o";
538 argv[argc++] = p;
539 }
540 }
541
542 *argcp = argc;
543 *argvp = argv;
544 *maxargcp = maxargc;
545 }
546
547 void
548 usage()
549 {
550
551 (void)fprintf(stderr,
552 "usage: mount %s %s\n mount %s\n mount %s\n",
553 "[-dfruvw] [-o options] [-t ffs | external_type]",
554 "special node",
555 "[-adfruvw] [-t ffs | external_type]",
556 "[-dfruvw] special | node");
557 exit(1);
558 }
559