1 /*
2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
11 The Regents of the University of California. All rights reserved.\n";
12 #endif /* not lint */
13
14 #ifndef lint
15 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 05/08/95";
16 #endif /* not lint */
17
18 #include <sys/param.h>
19 #include <sys/mount.h>
20 #include <sys/wait.h>
21
22 #include <err.h>
23 #include <errno.h>
24 #include <fstab.h>
25 #include <pwd.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "pathnames.h"
33
34 int debug, verbose;
35
36 int checkvfsname __P((const char *, const char **));
37 char *catopt __P((char *, const char *));
38 struct statfs
39 *getmntpt __P((const char *));
40 int hasopt __P((const char *, const char *));
41 const char
42 **makevfslist __P((char *));
43 void mangle __P((char *, int *, const char **));
44 int mountfs __P((const char *, const char *, const char *,
45 int, const char *, const char *));
46 void prmount __P((struct statfs *));
47 void usage __P((void));
48
49 /* From mount_ufs.c. */
50 int mount_ufs __P((int, char * const *));
51
52 /* Map from mount otions to printable formats. */
53 static struct opt {
54 int o_opt;
55 const char *o_name;
56 } optnames[] = {
57 { MNT_ASYNC, "asynchronous" },
58 { MNT_EXPORTED, "NFS exported" },
59 { MNT_LOCAL, "local" },
60 { MNT_NODEV, "nodev" },
61 { MNT_NOEXEC, "noexec" },
62 { MNT_NOSUID, "nosuid" },
63 { MNT_QUOTA, "with quotas" },
64 { MNT_RDONLY, "read-only" },
65 { MNT_SYNCHRONOUS, "synchronous" },
66 { MNT_UNION, "union" },
67 { NULL }
68 };
69
70 int
main(argc,argv)71 main(argc, argv)
72 int argc;
73 char * const argv[];
74 {
75 const char *mntfromname, **vfslist, *vfstype;
76 struct fstab *fs;
77 struct statfs *mntbuf;
78 FILE *mountdfp;
79 pid_t pid;
80 int all, ch, i, init_flags, mntsize, rval;
81 char *options;
82
83 all = init_flags = 0;
84 options = NULL;
85 vfslist = NULL;
86 vfstype = "ufs";
87 while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF)
88 switch (ch) {
89 case 'a':
90 all = 1;
91 break;
92 case 'd':
93 debug = 1;
94 break;
95 case 'f':
96 init_flags |= MNT_FORCE;
97 break;
98 case 'o':
99 if (*optarg)
100 options = catopt(options, optarg);
101 break;
102 case 'r':
103 init_flags |= MNT_RDONLY;
104 break;
105 case 't':
106 if (vfslist != NULL)
107 errx(1, "only one -t option may be specified.");
108 vfslist = makevfslist(optarg);
109 vfstype = optarg;
110 break;
111 case 'u':
112 init_flags |= MNT_UPDATE;
113 break;
114 case 'v':
115 verbose = 1;
116 break;
117 case 'w':
118 init_flags &= ~MNT_RDONLY;
119 break;
120 case '?':
121 default:
122 usage();
123 /* NOTREACHED */
124 }
125 argc -= optind;
126 argv += optind;
127
128 #define BADTYPE(type) \
129 (strcmp(type, FSTAB_RO) && \
130 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
131
132 rval = 0;
133 switch (argc) {
134 case 0:
135 if (all)
136 while ((fs = getfsent()) != NULL) {
137 if (BADTYPE(fs->fs_type))
138 continue;
139 if (checkvfsname(fs->fs_vfstype, vfslist))
140 continue;
141 if (hasopt(fs->fs_mntops, "noauto"))
142 continue;
143 if (mountfs(fs->fs_vfstype, fs->fs_spec,
144 fs->fs_file, init_flags, options,
145 fs->fs_mntops))
146 rval = 1;
147 }
148 else {
149 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
150 err(1, "getmntinfo");
151 for (i = 0; i < mntsize; i++) {
152 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
153 continue;
154 prmount(&mntbuf[i]);
155 }
156 }
157 exit(rval);
158 case 1:
159 if (vfslist != NULL)
160 usage();
161
162 if (init_flags & MNT_UPDATE) {
163 if ((mntbuf = getmntpt(*argv)) == NULL)
164 errx(1,
165 "unknown special file or file system %s.",
166 *argv);
167 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL)
168 mntfromname = fs->fs_spec;
169 else
170 mntfromname = mntbuf->f_mntfromname;
171 rval = mountfs(mntbuf->f_fstypename, mntfromname,
172 mntbuf->f_mntonname, init_flags, options, 0);
173 break;
174 }
175 if ((fs = getfsfile(*argv)) == NULL &&
176 (fs = getfsspec(*argv)) == NULL)
177 errx(1, "%s: unknown special file or file system.",
178 *argv);
179 if (BADTYPE(fs->fs_type))
180 errx(1, "%s has unknown file system type.",
181 *argv);
182 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
183 init_flags, options, fs->fs_mntops);
184 break;
185 case 2:
186 /*
187 * If -t flag has not been specified, and spec contains either
188 * a ':' or a '@' then assume that an NFS filesystem is being
189 * specified ala Sun.
190 */
191 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
192 vfstype = "nfs";
193 rval = mountfs(vfstype,
194 argv[0], argv[1], init_flags, options, NULL);
195 break;
196 default:
197 usage();
198 /* NOTREACHED */
199 }
200
201 /*
202 * If the mount was successfully, and done by root, tell mountd the
203 * good news. Pid checks are probably unnecessary, but don't hurt.
204 */
205 if (rval == 0 && getuid() == 0 &&
206 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
207 if (fscanf(mountdfp, "%ld", &pid) == 1 &&
208 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
209 err(1, "signal mountd");
210 (void)fclose(mountdfp);
211 }
212
213 exit(rval);
214 }
215
216 int
hasopt(mntopts,option)217 hasopt(mntopts, option)
218 const char *mntopts, *option;
219 {
220 int negative, found;
221 char *opt, *optbuf;
222
223 if (option[0] == 'n' && option[1] == 'o') {
224 negative = 1;
225 option += 2;
226 } else
227 negative = 0;
228 optbuf = strdup(mntopts);
229 found = 0;
230 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
231 if (opt[0] == 'n' && opt[1] == 'o') {
232 if (!strcasecmp(opt + 2, option))
233 found = negative;
234 } else if (!strcasecmp(opt, option))
235 found = !negative;
236 }
237 free(optbuf);
238 return (found);
239 }
240
241 int
mountfs(vfstype,spec,name,flags,options,mntopts)242 mountfs(vfstype, spec, name, flags, options, mntopts)
243 const char *vfstype, *spec, *name, *options, *mntopts;
244 int flags;
245 {
246 /* List of directories containing mount_xxx subcommands. */
247 static const char *edirs[] = {
248 _PATH_SBIN,
249 _PATH_USRSBIN,
250 NULL
251 };
252 const char *argv[100], **edir;
253 struct statfs sf;
254 pid_t pid;
255 int argc, i, status;
256 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
257
258 if (realpath(name, mntpath) == NULL) {
259 warn("realpath %s", mntpath);
260 return (1);
261 }
262
263 name = mntpath;
264
265 if (mntopts == NULL)
266 mntopts = "";
267 if (options == NULL) {
268 if (*mntopts == '\0') {
269 options = "rw";
270 } else {
271 options = mntopts;
272 mntopts = "";
273 }
274 }
275 optbuf = catopt(strdup(mntopts), options);
276
277 if (strcmp(name, "/") == 0)
278 flags |= MNT_UPDATE;
279 if (flags & MNT_FORCE)
280 optbuf = catopt(optbuf, "force");
281 if (flags & MNT_RDONLY)
282 optbuf = catopt(optbuf, "ro");
283 /*
284 * XXX
285 * The mount_mfs (newfs) command uses -o to select the
286 * optimisation mode. We don't pass the default "-o rw"
287 * for that reason.
288 */
289 if (flags & MNT_UPDATE)
290 optbuf = catopt(optbuf, "update");
291
292 argc = 0;
293 argv[argc++] = vfstype;
294 mangle(optbuf, &argc, argv);
295 argv[argc++] = spec;
296 argv[argc++] = name;
297 argv[argc] = NULL;
298
299 if (debug) {
300 (void)printf("exec: mount_%s", vfstype);
301 for (i = 1; i < argc; i++)
302 (void)printf(" %s", argv[i]);
303 (void)printf("\n");
304 return (0);
305 }
306
307 switch (pid = vfork()) {
308 case -1: /* Error. */
309 warn("vfork");
310 free(optbuf);
311 return (1);
312 case 0: /* Child. */
313 if (strcmp(vfstype, "ufs") == 0)
314 exit(mount_ufs(argc, (char * const *) argv));
315
316 /* Go find an executable. */
317 edir = edirs;
318 do {
319 (void)snprintf(execname,
320 sizeof(execname), "%s/mount_%s", *edir, vfstype);
321 execv(execname, (char * const *)argv);
322 if (errno != ENOENT)
323 warn("exec %s for %s", execname, name);
324 } while (*++edir != NULL);
325
326 if (errno == ENOENT)
327 warn("exec %s for %s", execname, name);
328 exit(1);
329 /* NOTREACHED */
330 default: /* Parent. */
331 free(optbuf);
332
333 if (waitpid(pid, &status, 0) < 0) {
334 warn("waitpid");
335 return (1);
336 }
337
338 if (WIFEXITED(status)) {
339 if (WEXITSTATUS(status) != 0)
340 return (WEXITSTATUS(status));
341 } else if (WIFSIGNALED(status)) {
342 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
343 return (1);
344 }
345
346 if (verbose) {
347 if (statfs(name, &sf) < 0) {
348 warn("statfs %s", name);
349 return (1);
350 }
351 prmount(&sf);
352 }
353 break;
354 }
355
356 return (0);
357 }
358
359 void
prmount(sfp)360 prmount(sfp)
361 struct statfs *sfp;
362 {
363 int flags;
364 struct opt *o;
365 struct passwd *pw;
366 int f;
367
368 (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname);
369
370 flags = sfp->f_flags & MNT_VISFLAGMASK;
371 for (f = 0, o = optnames; flags && o->o_opt; o++)
372 if (flags & o->o_opt) {
373 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
374 flags &= ~o->o_opt;
375 }
376 if (sfp->f_owner) {
377 (void)printf("%smounted by ", !f++ ? " (" : ", ");
378 if ((pw = getpwuid(sfp->f_owner)) != NULL)
379 (void)printf("%s", pw->pw_name);
380 else
381 (void)printf("%d", sfp->f_owner);
382 }
383 (void)printf(f ? ")\n" : "\n");
384 }
385
386 struct statfs *
getmntpt(name)387 getmntpt(name)
388 const char *name;
389 {
390 struct statfs *mntbuf;
391 int i, mntsize;
392
393 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
394 for (i = 0; i < mntsize; i++)
395 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
396 strcmp(mntbuf[i].f_mntonname, name) == 0)
397 return (&mntbuf[i]);
398 return (NULL);
399 }
400
401 char *
catopt(s0,s1)402 catopt(s0, s1)
403 char *s0;
404 const char *s1;
405 {
406 size_t i;
407 char *cp;
408
409 if (s0 && *s0) {
410 i = strlen(s0) + strlen(s1) + 1 + 1;
411 if ((cp = malloc(i)) == NULL)
412 err(1, NULL);
413 (void)snprintf(cp, i, "%s,%s", s0, s1);
414 } else
415 cp = strdup(s1);
416
417 if (s0)
418 free(s0);
419 return (cp);
420 }
421
422 void
mangle(options,argcp,argv)423 mangle(options, argcp, argv)
424 char *options;
425 int *argcp;
426 const char **argv;
427 {
428 char *p, *s;
429 int argc;
430
431 argc = *argcp;
432 for (s = options; (p = strsep(&s, ",")) != NULL;)
433 if (*p != '\0')
434 if (*p == '-') {
435 argv[argc++] = p;
436 p = strchr(p, '=');
437 if (p) {
438 *p = '\0';
439 argv[argc++] = p+1;
440 }
441 } else if (strcmp(p, "rw") != 0) {
442 argv[argc++] = "-o";
443 argv[argc++] = p;
444 }
445
446 *argcp = argc;
447 }
448
449 void
usage()450 usage()
451 {
452
453 (void)fprintf(stderr,
454 "usage: mount %s %s\n mount %s\n mount %s\n",
455 "[-dfruvw] [-o options] [-t ufs | external_type]",
456 "special node",
457 "[-adfruvw] [-t ufs | external_type]",
458 "[-dfruvw] special | node");
459 exit(1);
460 }
461