mount.c revision 1.14 1 /*
2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)mount.c 8.19 (Berkeley) 4/19/94";*/
42 static char *rcsid = "$Id: mount.c,v 1.14 1994/12/18 16:03:02 cgd Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/wait.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <fstab.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include "pathnames.h"
59
60 int debug, verbose, skipvfs;
61
62 int badvfsname __P((const char *, const char **));
63 char *catopt __P((char *, const char *));
64 struct statfs
65 *getmntpt __P((const char *));
66 int hasopt __P((const char *, const char *));
67 const char
68 **makevfslist __P((char *));
69 void mangle __P((char *, int *, const char **));
70 int mountfs __P((const char *, const char *, const char *,
71 int, const char *, const char *));
72 void prmount __P((struct statfs *));
73 void usage __P((void));
74
75 /* From mount_ufs.c. */
76 int mount_ufs __P((int, char * const *));
77
78 /* Map from mount otions to printable formats. */
79 static struct opt {
80 int o_opt;
81 const char *o_name;
82 } optnames[] = {
83 { MNT_ASYNC, "asynchronous" },
84 { MNT_EXPORTED, "NFS exported" },
85 { MNT_LOCAL, "local" },
86 { MNT_NODEV, "nodev" },
87 { MNT_NOEXEC, "noexec" },
88 { MNT_NOSUID, "nosuid" },
89 { MNT_QUOTA, "with quotas" },
90 { MNT_RDONLY, "read-only" },
91 { MNT_SYNCHRONOUS, "synchronous" },
92 { MNT_UNION, "union" },
93 { NULL }
94 };
95
96 int
97 main(argc, argv)
98 int argc;
99 char * const argv[];
100 {
101 const char *mntonname, **vfslist, *vfstype;
102 struct fstab *fs;
103 struct statfs *mntbuf;
104 FILE *mountdfp;
105 pid_t pid;
106 int all, ch, i, init_flags, mntsize, rval;
107 char *options;
108
109 all = init_flags = 0;
110 options = NULL;
111 vfslist = NULL;
112 vfstype = "ufs";
113 while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF)
114 switch (ch) {
115 case 'a':
116 all = 1;
117 break;
118 case 'd':
119 debug = 1;
120 break;
121 case 'f':
122 init_flags |= MNT_FORCE;
123 break;
124 case 'o':
125 if (*optarg)
126 options = catopt(options, optarg);
127 break;
128 case 'r':
129 init_flags |= MNT_RDONLY;
130 break;
131 case 't':
132 if (vfslist != NULL)
133 errx(1, "only one -t option may be specified.");
134 vfslist = makevfslist(optarg);
135 vfstype = optarg;
136 break;
137 case 'u':
138 init_flags |= MNT_UPDATE;
139 break;
140 case 'v':
141 verbose = 1;
142 break;
143 case 'w':
144 init_flags &= ~MNT_RDONLY;
145 break;
146 case '?':
147 default:
148 usage();
149 /* NOTREACHED */
150 }
151 argc -= optind;
152 argv += optind;
153
154 #define BADTYPE(type) \
155 (strcmp(type, FSTAB_RO) && \
156 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
157
158 rval = 0;
159 switch (argc) {
160 case 0:
161 if (all)
162 while ((fs = getfsent()) != NULL) {
163 if (BADTYPE(fs->fs_type))
164 continue;
165 if (badvfsname(fs->fs_vfstype, vfslist))
166 continue;
167 if (hasopt(fs->fs_mntops, "noauto"))
168 continue;
169 if (mountfs(fs->fs_vfstype, fs->fs_spec,
170 fs->fs_file, init_flags, options,
171 fs->fs_mntops))
172 rval = 1;
173 }
174 else {
175 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
176 err(1, "getmntinfo");
177 for (i = 0; i < mntsize; i++) {
178 if (badvfsname(mntbuf[i].f_fstypename, vfslist))
179 continue;
180 prmount(&mntbuf[i]);
181 }
182 }
183 exit(rval);
184 case 1:
185 if (vfslist != NULL)
186 usage();
187
188 if (init_flags & MNT_UPDATE) {
189 if ((mntbuf = getmntpt(*argv)) == NULL)
190 errx(1,
191 "unknown special file or file system %s.",
192 *argv);
193 if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
194 errx(1, "can't find fstab entry for %s.",
195 *argv);
196 /* If it's an update, ignore the fstab file options. */
197 fs->fs_mntops = NULL;
198 mntonname = mntbuf->f_mntonname;
199 } else {
200 if ((fs = getfsfile(*argv)) == NULL &&
201 (fs = getfsspec(*argv)) == NULL)
202 errx(1,
203 "%s: unknown special file or file system.",
204 *argv);
205 if (BADTYPE(fs->fs_type))
206 errx(1, "%s has unknown file system type.",
207 *argv);
208 mntonname = fs->fs_file;
209 }
210 rval = mountfs(fs->fs_vfstype, fs->fs_spec,
211 mntonname, init_flags, options, fs->fs_mntops);
212 break;
213 case 2:
214 /*
215 * If -t flag has not been specified, and spec contains either
216 * a ':' or a '@' then assume that an NFS filesystem is being
217 * specified ala Sun.
218 */
219 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
220 vfstype = "nfs";
221 rval = mountfs(vfstype,
222 argv[0], argv[1], init_flags, options, NULL);
223 break;
224 default:
225 usage();
226 /* NOTREACHED */
227 }
228
229 /*
230 * If the mount was successfully, and done by root, tell mountd the
231 * good news. Pid checks are probably unnecessary, but don't hurt.
232 */
233 if (rval == 0 && getuid() == 0 &&
234 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
235 if (fscanf(mountdfp, "%ld", &pid) == 1 &&
236 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
237 err(1, "signal mountd");
238 (void)fclose(mountdfp);
239 }
240
241 exit(rval);
242 }
243
244 int
245 hasopt(mntopts, option)
246 const char *mntopts, *option;
247 {
248 int negative, found;
249 char *opt, *optbuf;
250
251 if (option[0] == 'n' && option[1] == 'o') {
252 negative = 1;
253 option += 2;
254 } else
255 negative = 0;
256 optbuf = strdup(mntopts);
257 found = 0;
258 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
259 if (opt[0] == 'n' && opt[1] == 'o') {
260 if (!strcasecmp(opt + 2, option))
261 found = negative;
262 } else if (!strcasecmp(opt, option))
263 found = !negative;
264 }
265 free(optbuf);
266 return (found);
267 }
268
269 int
270 mountfs(vfstype, spec, name, flags, options, mntopts)
271 const char *vfstype, *spec, *name, *options, *mntopts;
272 int flags;
273 {
274 /* List of directories containing mount_xxx subcommands. */
275 static const char *edirs[] = {
276 _PATH_SBIN,
277 _PATH_USRSBIN,
278 NULL
279 };
280 const char *argv[100], **edir;
281 struct statfs sf;
282 pid_t pid;
283 int argc, i, status;
284 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
285
286 if (realpath(name, mntpath) == NULL) {
287 warn("realpath %s", mntpath);
288 return (1);
289 }
290
291 name = mntpath;
292
293 if (mntopts == NULL)
294 mntopts = "";
295 if (options == NULL) {
296 if (*mntopts == '\0')
297 options = "rw";
298 else {
299 options = mntopts;
300 mntopts = "";
301 }
302 }
303 optbuf = catopt(strdup(mntopts), options);
304
305 if (strcmp(name, "/") == 0)
306 flags |= MNT_UPDATE;
307 if (flags & MNT_FORCE)
308 optbuf = catopt(optbuf, "force");
309 if (flags & MNT_RDONLY)
310 optbuf = catopt(optbuf, "ro");
311 /*
312 * XXX
313 * The mount_mfs (newfs) command uses -o to select the
314 * optimisation mode. We don't pass the default "-o rw"
315 * for that reason.
316 */
317 if (flags & MNT_UPDATE)
318 optbuf = catopt(optbuf, "update");
319
320 argc = 0;
321 argv[argc++] = vfstype;
322 mangle(optbuf, &argc, argv);
323 argv[argc++] = spec;
324 argv[argc++] = name;
325 argv[argc] = NULL;
326
327 if (debug) {
328 (void)printf("exec: mount_%s", vfstype);
329 for (i = 1; i < argc; i++)
330 (void)printf(" %s", argv[i]);
331 (void)printf("\n");
332 return (0);
333 }
334
335 switch (pid = vfork()) {
336 case -1: /* Error. */
337 warn("vfork");
338 free(optbuf);
339 return (1);
340 case 0: /* Child. */
341 if (strcmp(vfstype, "ufs") == 0)
342 exit(mount_ufs(argc, (char * const *) argv));
343
344 /* Go find an executable. */
345 edir = edirs;
346 do {
347 (void)snprintf(execname,
348 sizeof(execname), "%s/mount_%s", *edir, vfstype);
349 execv(execname, (char * const *)argv);
350 if (errno != ENOENT)
351 warn("exec %s for %s", execname, name);
352 } while (*++edir != NULL);
353
354 if (errno == ENOENT)
355 warn("exec %s for %s", execname, name);
356 exit(1);
357 /* NOTREACHED */
358 default: /* Parent. */
359 free(optbuf);
360
361 if (waitpid(pid, &status, 0) < 0) {
362 warn("waitpid");
363 return (1);
364 }
365
366 if (WIFEXITED(status)) {
367 if (WEXITSTATUS(status) != 0)
368 return (WEXITSTATUS(status));
369 } else if (WIFSIGNALED(status)) {
370 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
371 return (1);
372 }
373
374 if (verbose) {
375 if (statfs(name, &sf) < 0) {
376 warn("statfs %s", name);
377 return (1);
378 }
379 prmount(&sf);
380 }
381 break;
382 }
383
384 return (0);
385 }
386
387 void
388 prmount(sf)
389 struct statfs *sf;
390 {
391 int flags;
392 struct opt *o;
393 int f;
394
395 (void)printf("%s on %s type %s", sf->f_mntfromname, sf->f_mntonname,
396 sf->f_fstypename);
397
398 flags = sf->f_flags & MNT_VISFLAGMASK;
399 for (f = 0, o = optnames; flags && o->o_opt; o++)
400 if (flags & o->o_opt) {
401 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
402 flags &= ~o->o_opt;
403 }
404 (void)printf(f ? ")\n" : "\n");
405 }
406
407 struct statfs *
408 getmntpt(name)
409 const char *name;
410 {
411 struct statfs *mntbuf;
412 int i, mntsize;
413
414 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
415 for (i = 0; i < mntsize; i++)
416 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
417 strcmp(mntbuf[i].f_mntonname, name) == 0)
418 return (&mntbuf[i]);
419 return (NULL);
420 }
421
422 int
423 badvfsname(vfsname, vfslist)
424 const char *vfsname;
425 const char **vfslist;
426 {
427
428 if (vfslist == NULL)
429 return (0);
430 while (*vfslist != NULL) {
431 if (strcmp(vfsname, *vfslist) == 0)
432 return (skipvfs);
433 ++vfslist;
434 }
435 return (!skipvfs);
436 }
437
438 const char **
439 makevfslist(fslist)
440 char *fslist;
441 {
442 const char **av;
443 int i;
444 char *nextcp;
445
446 if (fslist == NULL)
447 return (NULL);
448 if (fslist[0] == 'n' && fslist[1] == 'o') {
449 fslist += 2;
450 skipvfs = 1;
451 }
452 for (i = 0, nextcp = fslist; *nextcp; nextcp++)
453 if (*nextcp == ',')
454 i++;
455 if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) {
456 warn(NULL);
457 return (NULL);
458 }
459 nextcp = fslist;
460 i = 0;
461 av[i++] = nextcp;
462 while ((nextcp = strchr(nextcp, ',')) != NULL) {
463 *nextcp++ = '\0';
464 av[i++] = nextcp;
465 }
466 av[i++] = NULL;
467 return (av);
468 }
469
470 char *
471 catopt(s0, s1)
472 char *s0;
473 const char *s1;
474 {
475 size_t i;
476 char *cp;
477
478 if (s0 && *s0) {
479 i = strlen(s0) + strlen(s1) + 1 + 1;
480 if ((cp = malloc(i)) == NULL)
481 err(1, NULL);
482 (void)snprintf(cp, i, "%s,%s", s0, s1);
483 } else
484 cp = strdup(s1);
485
486 if (s0)
487 free(s0);
488 return (cp);
489 }
490
491 void
492 mangle(options, argcp, argv)
493 char *options;
494 int *argcp;
495 const char **argv;
496 {
497 char *p, *s;
498 int argc;
499
500 argc = *argcp;
501 for (s = options; (p = strsep(&s, ",")) != NULL;)
502 if (*p != '\0')
503 if (*p == '-') {
504 argv[argc++] = p;
505 p = strchr(p, '=');
506 if (p) {
507 *p = '\0';
508 argv[argc++] = p+1;
509 }
510 } else if (strcmp(p, "rw") != 0) {
511 argv[argc++] = "-o";
512 argv[argc++] = p;
513 }
514
515 *argcp = argc;
516 }
517
518 void
519 usage()
520 {
521
522 (void)fprintf(stderr,
523 "usage: mount %s %s\n mount %s\n mount %s\n",
524 "[-dfruvw] [-o options] [-t ufs | external_type]",
525 "special node",
526 "[-adfruvw] [-t ufs | external_type]",
527 "[-dfruvw] special | node");
528 exit(1);
529 }
530