mount.c revision 1.15 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.15 1995/01/30 17:22:42 mycroft 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;
61 char **typelist = NULL;
62
63 int selected __P((const char *));
64 char *catopt __P((char *, const char *));
65 struct statfs
66 *getmntpt __P((const char *));
67 int hasopt __P((const char *, const char *));
68 void maketypelist __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, *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 vfstype = "ufs";
112 while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF)
113 switch (ch) {
114 case 'a':
115 all = 1;
116 break;
117 case 'd':
118 debug = 1;
119 break;
120 case 'f':
121 init_flags |= MNT_FORCE;
122 break;
123 case 'o':
124 if (*optarg)
125 options = catopt(options, optarg);
126 break;
127 case 'r':
128 init_flags |= MNT_RDONLY;
129 break;
130 case 't':
131 if (typelist != NULL)
132 errx(1, "only one -t option may be specified.");
133 maketypelist(optarg);
134 vfstype = optarg;
135 break;
136 case 'u':
137 init_flags |= MNT_UPDATE;
138 break;
139 case 'v':
140 verbose = 1;
141 break;
142 case 'w':
143 init_flags &= ~MNT_RDONLY;
144 break;
145 case '?':
146 default:
147 usage();
148 /* NOTREACHED */
149 }
150 argc -= optind;
151 argv += optind;
152
153 #define BADTYPE(type) \
154 (strcmp(type, FSTAB_RO) && \
155 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
156
157 rval = 0;
158 switch (argc) {
159 case 0:
160 if (all)
161 while ((fs = getfsent()) != NULL) {
162 if (BADTYPE(fs->fs_type))
163 continue;
164 if (!selected(fs->fs_vfstype))
165 continue;
166 if (hasopt(fs->fs_mntops, "noauto"))
167 continue;
168 if (mountfs(fs->fs_vfstype, fs->fs_spec,
169 fs->fs_file, init_flags, options,
170 fs->fs_mntops))
171 rval = 1;
172 }
173 else {
174 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
175 err(1, "getmntinfo");
176 for (i = 0; i < mntsize; i++) {
177 if (!selected(mntbuf[i].f_fstypename))
178 continue;
179 prmount(&mntbuf[i]);
180 }
181 }
182 exit(rval);
183 case 1:
184 if (typelist != NULL)
185 usage();
186
187 if (init_flags & MNT_UPDATE) {
188 if ((mntbuf = getmntpt(*argv)) == NULL)
189 errx(1,
190 "unknown special file or file system %s.",
191 *argv);
192 if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
193 errx(1, "can't find fstab entry for %s.",
194 *argv);
195 /* If it's an update, ignore the fstab file options. */
196 fs->fs_mntops = NULL;
197 mntonname = mntbuf->f_mntonname;
198 } else {
199 if ((fs = getfsfile(*argv)) == NULL &&
200 (fs = getfsspec(*argv)) == NULL)
201 errx(1,
202 "%s: unknown special file or file system.",
203 *argv);
204 if (BADTYPE(fs->fs_type))
205 errx(1, "%s has unknown file system type.",
206 *argv);
207 mntonname = fs->fs_file;
208 }
209 rval = mountfs(fs->fs_vfstype, fs->fs_spec,
210 mntonname, init_flags, options, fs->fs_mntops);
211 break;
212 case 2:
213 /*
214 * If -t flag has not been specified, and spec contains either
215 * a ':' or a '@' then assume that an NFS filesystem is being
216 * specified ala Sun.
217 */
218 if (typelist == NULL && strpbrk(argv[0], ":@") != NULL)
219 vfstype = "nfs";
220 rval = mountfs(vfstype,
221 argv[0], argv[1], init_flags, options, NULL);
222 break;
223 default:
224 usage();
225 /* NOTREACHED */
226 }
227
228 /*
229 * If the mount was successfully, and done by root, tell mountd the
230 * good news. Pid checks are probably unnecessary, but don't hurt.
231 */
232 if (rval == 0 && getuid() == 0 &&
233 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
234 if (fscanf(mountdfp, "%ld", &pid) == 1 &&
235 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
236 err(1, "signal mountd");
237 (void)fclose(mountdfp);
238 }
239
240 exit(rval);
241 }
242
243 int
244 hasopt(mntopts, option)
245 const char *mntopts, *option;
246 {
247 int negative, found;
248 char *opt, *optbuf;
249
250 if (option[0] == 'n' && option[1] == 'o') {
251 negative = 1;
252 option += 2;
253 } else
254 negative = 0;
255 optbuf = strdup(mntopts);
256 found = 0;
257 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
258 if (opt[0] == 'n' && opt[1] == 'o') {
259 if (!strcasecmp(opt + 2, option))
260 found = negative;
261 } else if (!strcasecmp(opt, option))
262 found = !negative;
263 }
264 free(optbuf);
265 return (found);
266 }
267
268 int
269 mountfs(vfstype, spec, name, flags, options, mntopts)
270 const char *vfstype, *spec, *name, *options, *mntopts;
271 int flags;
272 {
273 /* List of directories containing mount_xxx subcommands. */
274 static const char *edirs[] = {
275 _PATH_SBIN,
276 _PATH_USRSBIN,
277 NULL
278 };
279 const char *argv[100], **edir;
280 struct statfs sf;
281 pid_t pid;
282 int argc, i, status;
283 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
284
285 if (realpath(name, mntpath) == NULL) {
286 warn("realpath %s", mntpath);
287 return (1);
288 }
289
290 name = mntpath;
291
292 if (mntopts == NULL)
293 mntopts = "";
294 if (options == NULL) {
295 if (*mntopts == '\0')
296 options = "rw";
297 else {
298 options = mntopts;
299 mntopts = "";
300 }
301 }
302 optbuf = catopt(strdup(mntopts), options);
303
304 if (strcmp(name, "/") == 0)
305 flags |= MNT_UPDATE;
306 if (flags & MNT_FORCE)
307 optbuf = catopt(optbuf, "force");
308 if (flags & MNT_RDONLY)
309 optbuf = catopt(optbuf, "ro");
310 /*
311 * XXX
312 * The mount_mfs (newfs) command uses -o to select the
313 * optimisation mode. We don't pass the default "-o rw"
314 * for that reason.
315 */
316 if (flags & MNT_UPDATE)
317 optbuf = catopt(optbuf, "update");
318
319 argc = 0;
320 argv[argc++] = vfstype;
321 mangle(optbuf, &argc, argv);
322 argv[argc++] = spec;
323 argv[argc++] = name;
324 argv[argc] = NULL;
325
326 if (debug) {
327 (void)printf("exec: mount_%s", vfstype);
328 for (i = 1; i < argc; i++)
329 (void)printf(" %s", argv[i]);
330 (void)printf("\n");
331 return (0);
332 }
333
334 switch (pid = vfork()) {
335 case -1: /* Error. */
336 warn("vfork");
337 free(optbuf);
338 return (1);
339 case 0: /* Child. */
340 if (strcmp(vfstype, "ufs") == 0)
341 exit(mount_ufs(argc, (char * const *) argv));
342
343 /* Go find an executable. */
344 edir = edirs;
345 do {
346 (void)snprintf(execname,
347 sizeof(execname), "%s/mount_%s", *edir, vfstype);
348 execv(execname, (char * const *)argv);
349 if (errno != ENOENT)
350 warn("exec %s for %s", execname, name);
351 } while (*++edir != NULL);
352
353 if (errno == ENOENT)
354 warn("exec %s for %s", execname, name);
355 exit(1);
356 /* NOTREACHED */
357 default: /* Parent. */
358 free(optbuf);
359
360 if (waitpid(pid, &status, 0) < 0) {
361 warn("waitpid");
362 return (1);
363 }
364
365 if (WIFEXITED(status)) {
366 if (WEXITSTATUS(status) != 0)
367 return (WEXITSTATUS(status));
368 } else if (WIFSIGNALED(status)) {
369 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
370 return (1);
371 }
372
373 if (verbose) {
374 if (statfs(name, &sf) < 0) {
375 warn("statfs %s", name);
376 return (1);
377 }
378 prmount(&sf);
379 }
380 break;
381 }
382
383 return (0);
384 }
385
386 void
387 prmount(sf)
388 struct statfs *sf;
389 {
390 int flags;
391 struct opt *o;
392 int f;
393
394 (void)printf("%s on %s type %s", sf->f_mntfromname, sf->f_mntonname,
395 sf->f_fstypename);
396
397 flags = sf->f_flags & MNT_VISFLAGMASK;
398 for (f = 0, o = optnames; flags && o->o_opt; o++)
399 if (flags & o->o_opt) {
400 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
401 flags &= ~o->o_opt;
402 }
403 (void)printf(f ? ")\n" : "\n");
404 }
405
406 struct statfs *
407 getmntpt(name)
408 const char *name;
409 {
410 struct statfs *mntbuf;
411 int i, mntsize;
412
413 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
414 for (i = 0; i < mntsize; i++)
415 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
416 strcmp(mntbuf[i].f_mntonname, name) == 0)
417 return (&mntbuf[i]);
418 return (NULL);
419 }
420
421 static enum { IN_LIST, NOT_IN_LIST } which;
422
423 int
424 selected(type)
425 const char *type;
426 {
427 char **av;
428
429 /* If no type specified, it's always selected. */
430 if (typelist == NULL)
431 return (1);
432 for (av = typelist; *av != NULL; ++av)
433 if (!strcmp(type, *av))
434 return (which == IN_LIST ? 1 : 0);
435 return (which == IN_LIST ? 0 : 1);
436 }
437
438 void
439 maketypelist(fslist)
440 char *fslist;
441 {
442 int i;
443 char *nextcp, **av;
444
445 if ((fslist == NULL) || (fslist[0] == '\0'))
446 errx(1, "empty type list");
447
448 /*
449 * XXX
450 * Note: the syntax is "noxxx,yyy" for no xxx's and
451 * no yyy's, not the more intuitive "noyyy,noyyy".
452 */
453 if (fslist[0] == 'n' && fslist[1] == 'o') {
454 fslist += 2;
455 which = NOT_IN_LIST;
456 } else
457 which = IN_LIST;
458
459 /* Count the number of types. */
460 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
461 ++nextcp;
462
463 /* Build an array of that many types. */
464 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
465 err(1, NULL);
466 av[0] = fslist;
467 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
468 *nextcp = '\0';
469 av[i] = ++nextcp;
470 }
471 /* Terminate the array. */
472 av[i] = 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, NULL);
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 ufs | external_type]",
530 "special node",
531 "[-adfruvw] [-t ufs | external_type]",
532 "[-dfruvw] special | node");
533 exit(1);
534 }
535