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