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