mount.c revision 1.70 1 1.70 hannken /* $NetBSD: mount.c,v 1.70 2004/04/22 10:17:00 hannken Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1980, 1989, 1993, 1994
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.66 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.29 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.29 christos __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
35 1.29 christos The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.16 cgd #if 0
40 1.32 lukem static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
41 1.16 cgd #else
42 1.70 hannken __RCSID("$NetBSD: mount.c,v 1.70 2004/04/22 10:17:00 hannken Exp $");
43 1.16 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd #include <sys/param.h>
47 1.12 mycroft #include <sys/mount.h>
48 1.1 cgd #include <sys/wait.h>
49 1.12 mycroft
50 1.12 mycroft #include <err.h>
51 1.12 mycroft #include <errno.h>
52 1.1 cgd #include <fstab.h>
53 1.32 lukem #include <pwd.h>
54 1.12 mycroft #include <signal.h>
55 1.1 cgd #include <stdio.h>
56 1.1 cgd #include <stdlib.h>
57 1.12 mycroft #include <string.h>
58 1.12 mycroft #include <unistd.h>
59 1.12 mycroft
60 1.48 abs #define MOUNTNAMES
61 1.48 abs #include <fcntl.h>
62 1.48 abs #include <sys/disklabel.h>
63 1.48 abs #include <sys/ioctl.h>
64 1.48 abs
65 1.1 cgd #include "pathnames.h"
66 1.55 enami #include "vfslist.h"
67 1.1 cgd
68 1.52 jdolecek static int debug, verbose;
69 1.1 cgd
70 1.52 jdolecek static void catopt __P((char **, const char *));
71 1.53 enami static const char *
72 1.53 enami getfslab __P((const char *str));
73 1.69 christos static struct statvfs *
74 1.53 enami getmntpt __P((const char *));
75 1.69 christos static int getmntargs __P((struct statvfs *, char *, size_t));
76 1.52 jdolecek static int hasopt __P((const char *, const char *));
77 1.53 enami static void mangle __P((char *, int *, const char ***, int *));
78 1.52 jdolecek static int mountfs __P((const char *, const char *, const char *,
79 1.61 christos int, const char *, const char *, int, char *, size_t));
80 1.69 christos static void prmount __P((struct statvfs *));
81 1.52 jdolecek static void usage __P((void));
82 1.53 enami
83 1.29 christos int main __P((int, char *[]));
84 1.12 mycroft
85 1.12 mycroft /* Map from mount otions to printable formats. */
86 1.52 jdolecek static const struct opt {
87 1.12 mycroft int o_opt;
88 1.24 cgd int o_silent;
89 1.12 mycroft const char *o_name;
90 1.12 mycroft } optnames[] = {
91 1.58 christos __MNT_FLAGS
92 1.1 cgd };
93 1.1 cgd
94 1.40 cgd static char ffs_fstype[] = "ffs";
95 1.30 christos
96 1.12 mycroft int
97 1.12 mycroft main(argc, argv)
98 1.1 cgd int argc;
99 1.29 christos char *argv[];
100 1.1 cgd {
101 1.32 lukem const char *mntfromname, *mntonname, **vfslist, *vfstype;
102 1.12 mycroft struct fstab *fs;
103 1.69 christos struct statvfs *mntbuf;
104 1.12 mycroft FILE *mountdfp;
105 1.22 cgd int all, ch, forceall, i, init_flags, mntsize, rval;
106 1.12 mycroft char *options;
107 1.43 mycroft const char *mountopts, *fstypename;
108 1.52 jdolecek
109 1.52 jdolecek /* started as "mount" */
110 1.22 cgd all = forceall = init_flags = 0;
111 1.12 mycroft options = NULL;
112 1.32 lukem vfslist = NULL;
113 1.40 cgd vfstype = ffs_fstype;
114 1.31 lukem while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
115 1.12 mycroft switch (ch) {
116 1.22 cgd case 'A':
117 1.22 cgd all = forceall = 1;
118 1.22 cgd break;
119 1.1 cgd case 'a':
120 1.1 cgd all = 1;
121 1.1 cgd break;
122 1.12 mycroft case 'd':
123 1.12 mycroft debug = 1;
124 1.12 mycroft break;
125 1.1 cgd case 'f':
126 1.12 mycroft init_flags |= MNT_FORCE;
127 1.12 mycroft break;
128 1.12 mycroft case 'o':
129 1.12 mycroft if (*optarg)
130 1.35 mycroft catopt(&options, optarg);
131 1.1 cgd break;
132 1.1 cgd case 'r':
133 1.12 mycroft init_flags |= MNT_RDONLY;
134 1.12 mycroft break;
135 1.12 mycroft case 't':
136 1.32 lukem if (vfslist != NULL)
137 1.53 enami errx(1,
138 1.53 enami "only one -t option may be specified.");
139 1.32 lukem vfslist = makevfslist(optarg);
140 1.12 mycroft vfstype = optarg;
141 1.1 cgd break;
142 1.1 cgd case 'u':
143 1.12 mycroft init_flags |= MNT_UPDATE;
144 1.1 cgd break;
145 1.1 cgd case 'v':
146 1.61 christos verbose++;
147 1.1 cgd break;
148 1.1 cgd case 'w':
149 1.12 mycroft init_flags &= ~MNT_RDONLY;
150 1.1 cgd break;
151 1.1 cgd case '?':
152 1.1 cgd default:
153 1.1 cgd usage();
154 1.1 cgd /* NOTREACHED */
155 1.1 cgd }
156 1.1 cgd argc -= optind;
157 1.1 cgd argv += optind;
158 1.1 cgd
159 1.12 mycroft #define BADTYPE(type) \
160 1.12 mycroft (strcmp(type, FSTAB_RO) && \
161 1.12 mycroft strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
162 1.12 mycroft
163 1.12 mycroft rval = 0;
164 1.12 mycroft switch (argc) {
165 1.12 mycroft case 0:
166 1.12 mycroft if (all)
167 1.12 mycroft while ((fs = getfsent()) != NULL) {
168 1.12 mycroft if (BADTYPE(fs->fs_type))
169 1.12 mycroft continue;
170 1.32 lukem if (checkvfsname(fs->fs_vfstype, vfslist))
171 1.12 mycroft continue;
172 1.12 mycroft if (hasopt(fs->fs_mntops, "noauto"))
173 1.12 mycroft continue;
174 1.68 cgd if (strcmp(fs->fs_spec, "from_mount") == 0) {
175 1.68 cgd if ((mntbuf = getmntpt(fs->fs_file)) == NULL)
176 1.68 cgd errx(1,
177 1.68 cgd "unknown file system %s.",
178 1.68 cgd fs->fs_file);
179 1.68 cgd mntfromname = mntbuf->f_mntfromname;
180 1.68 cgd } else
181 1.68 cgd mntfromname = fs->fs_spec;
182 1.68 cgd if (mountfs(fs->fs_vfstype, mntfromname,
183 1.12 mycroft fs->fs_file, init_flags, options,
184 1.61 christos fs->fs_mntops, !forceall, NULL, 0))
185 1.12 mycroft rval = 1;
186 1.12 mycroft }
187 1.12 mycroft else {
188 1.12 mycroft if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
189 1.12 mycroft err(1, "getmntinfo");
190 1.12 mycroft for (i = 0; i < mntsize; i++) {
191 1.32 lukem if (checkvfsname(mntbuf[i].f_fstypename,
192 1.32 lukem vfslist))
193 1.12 mycroft continue;
194 1.13 mycroft prmount(&mntbuf[i]);
195 1.12 mycroft }
196 1.1 cgd }
197 1.1 cgd exit(rval);
198 1.40 cgd /* NOTREACHED */
199 1.12 mycroft case 1:
200 1.40 cgd if (vfslist != NULL) {
201 1.1 cgd usage();
202 1.40 cgd /* NOTREACHED */
203 1.40 cgd }
204 1.1 cgd
205 1.12 mycroft if (init_flags & MNT_UPDATE) {
206 1.12 mycroft if ((mntbuf = getmntpt(*argv)) == NULL)
207 1.12 mycroft errx(1,
208 1.12 mycroft "unknown special file or file system %s.",
209 1.12 mycroft *argv);
210 1.68 cgd mntfromname = mntbuf->f_mntfromname;
211 1.42 ross if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
212 1.68 cgd if (strcmp(fs->fs_spec, "from_mount") != 0)
213 1.68 cgd mntfromname = fs->fs_spec;
214 1.42 ross /* ignore the fstab file options. */
215 1.42 ross fs->fs_mntops = NULL;
216 1.68 cgd }
217 1.42 ross mntonname = mntbuf->f_mntonname;
218 1.42 ross fstypename = mntbuf->f_fstypename;
219 1.42 ross mountopts = NULL;
220 1.12 mycroft } else {
221 1.12 mycroft if ((fs = getfsfile(*argv)) == NULL &&
222 1.12 mycroft (fs = getfsspec(*argv)) == NULL)
223 1.12 mycroft errx(1,
224 1.12 mycroft "%s: unknown special file or file system.",
225 1.12 mycroft *argv);
226 1.12 mycroft if (BADTYPE(fs->fs_type))
227 1.12 mycroft errx(1, "%s has unknown file system type.",
228 1.12 mycroft *argv);
229 1.68 cgd if (strcmp(fs->fs_spec, "from_mount") == 0) {
230 1.68 cgd if ((mntbuf = getmntpt(*argv)) == NULL)
231 1.68 cgd errx(1,
232 1.68 cgd "unknown special file or file system %s.",
233 1.68 cgd *argv);
234 1.68 cgd mntfromname = mntbuf->f_mntfromname;
235 1.68 cgd } else
236 1.68 cgd mntfromname = fs->fs_spec;
237 1.42 ross mntonname = fs->fs_file;
238 1.42 ross fstypename = fs->fs_vfstype;
239 1.42 ross mountopts = fs->fs_mntops;
240 1.12 mycroft }
241 1.42 ross rval = mountfs(fstypename, mntfromname,
242 1.61 christos mntonname, init_flags, options, mountopts, 0, NULL, 0);
243 1.12 mycroft break;
244 1.12 mycroft case 2:
245 1.1 cgd /*
246 1.12 mycroft * If -t flag has not been specified, and spec contains either
247 1.12 mycroft * a ':' or a '@' then assume that an NFS filesystem is being
248 1.12 mycroft * specified ala Sun.
249 1.1 cgd */
250 1.48 abs if (vfslist == NULL) {
251 1.48 abs if (strpbrk(argv[0], ":@") != NULL)
252 1.48 abs vfstype = "nfs";
253 1.48 abs else {
254 1.48 abs vfstype = getfslab(argv[0]);
255 1.48 abs if (vfstype == NULL)
256 1.48 abs vfstype = ffs_fstype;
257 1.48 abs }
258 1.48 abs }
259 1.12 mycroft rval = mountfs(vfstype,
260 1.61 christos argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
261 1.12 mycroft break;
262 1.12 mycroft default:
263 1.12 mycroft usage();
264 1.12 mycroft /* NOTREACHED */
265 1.1 cgd }
266 1.1 cgd
267 1.12 mycroft /*
268 1.12 mycroft * If the mount was successfully, and done by root, tell mountd the
269 1.12 mycroft * good news. Pid checks are probably unnecessary, but don't hurt.
270 1.12 mycroft */
271 1.12 mycroft if (rval == 0 && getuid() == 0 &&
272 1.12 mycroft (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
273 1.29 christos int pid;
274 1.29 christos
275 1.29 christos if (fscanf(mountdfp, "%d", &pid) == 1 &&
276 1.53 enami pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
277 1.12 mycroft err(1, "signal mountd");
278 1.12 mycroft (void)fclose(mountdfp);
279 1.1 cgd }
280 1.1 cgd
281 1.12 mycroft exit(rval);
282 1.40 cgd /* NOTREACHED */
283 1.1 cgd }
284 1.1 cgd
285 1.12 mycroft int
286 1.12 mycroft hasopt(mntopts, option)
287 1.12 mycroft const char *mntopts, *option;
288 1.1 cgd {
289 1.12 mycroft int negative, found;
290 1.12 mycroft char *opt, *optbuf;
291 1.1 cgd
292 1.12 mycroft if (option[0] == 'n' && option[1] == 'o') {
293 1.12 mycroft negative = 1;
294 1.12 mycroft option += 2;
295 1.12 mycroft } else
296 1.12 mycroft negative = 0;
297 1.12 mycroft optbuf = strdup(mntopts);
298 1.12 mycroft found = 0;
299 1.12 mycroft for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
300 1.12 mycroft if (opt[0] == 'n' && opt[1] == 'o') {
301 1.12 mycroft if (!strcasecmp(opt + 2, option))
302 1.12 mycroft found = negative;
303 1.12 mycroft } else if (!strcasecmp(opt, option))
304 1.12 mycroft found = !negative;
305 1.12 mycroft }
306 1.12 mycroft free(optbuf);
307 1.12 mycroft return (found);
308 1.1 cgd }
309 1.1 cgd
310 1.52 jdolecek static int
311 1.61 christos mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted, buf, buflen)
312 1.12 mycroft const char *vfstype, *spec, *name, *options, *mntopts;
313 1.22 cgd int flags, skipmounted;
314 1.61 christos char *buf;
315 1.61 christos size_t buflen;
316 1.1 cgd {
317 1.12 mycroft /* List of directories containing mount_xxx subcommands. */
318 1.12 mycroft static const char *edirs[] = {
319 1.60 lukem #ifdef _PATH_RESCUE
320 1.60 lukem _PATH_RESCUE,
321 1.60 lukem #endif
322 1.12 mycroft _PATH_SBIN,
323 1.12 mycroft _PATH_USRSBIN,
324 1.12 mycroft NULL
325 1.12 mycroft };
326 1.35 mycroft const char **argv, **edir;
327 1.69 christos struct statvfs *sfp, sf;
328 1.12 mycroft pid_t pid;
329 1.61 christos int pfd[2];
330 1.36 drochner int argc, numfs, i, status, maxargc;
331 1.33 christos char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
332 1.33 christos mntpath[MAXPATHLEN];
333 1.35 mycroft
334 1.29 christos #ifdef __GNUC__
335 1.29 christos (void) &name;
336 1.29 christos (void) &optbuf;
337 1.30 christos (void) &vfstype;
338 1.29 christos #endif
339 1.1 cgd
340 1.12 mycroft if (realpath(name, mntpath) == NULL) {
341 1.20 ghudson warn("realpath %s", name);
342 1.12 mycroft return (1);
343 1.12 mycroft }
344 1.1 cgd
345 1.12 mycroft name = mntpath;
346 1.1 cgd
347 1.35 mycroft optbuf = NULL;
348 1.35 mycroft if (mntopts)
349 1.35 mycroft catopt(&optbuf, mntopts);
350 1.35 mycroft if (options)
351 1.35 mycroft catopt(&optbuf, options);
352 1.35 mycroft if (!mntopts && !options)
353 1.35 mycroft catopt(&optbuf, "rw");
354 1.12 mycroft
355 1.35 mycroft if (!strcmp(name, "/"))
356 1.12 mycroft flags |= MNT_UPDATE;
357 1.22 cgd else if (skipmounted) {
358 1.36 drochner if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
359 1.36 drochner warn("getmntinfo");
360 1.21 cgd return (1);
361 1.21 cgd }
362 1.36 drochner for(i = 0; i < numfs; i++) {
363 1.53 enami /*
364 1.53 enami * XXX can't check f_mntfromname,
365 1.53 enami * thanks to mfs, union, etc.
366 1.53 enami */
367 1.36 drochner if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
368 1.36 drochner strncmp(vfstype, sfp[i].f_fstypename,
369 1.53 enami MFSNAMELEN) == 0) {
370 1.36 drochner if (verbose)
371 1.53 enami (void)printf("%s on %s type %.*s: "
372 1.53 enami "%s\n",
373 1.53 enami sfp[i].f_mntfromname,
374 1.53 enami sfp[i].f_mntonname,
375 1.53 enami MFSNAMELEN,
376 1.53 enami sfp[i].f_fstypename,
377 1.53 enami "already mounted");
378 1.36 drochner return (0);
379 1.36 drochner }
380 1.21 cgd }
381 1.21 cgd }
382 1.12 mycroft if (flags & MNT_FORCE)
383 1.35 mycroft catopt(&optbuf, "force");
384 1.12 mycroft if (flags & MNT_RDONLY)
385 1.35 mycroft catopt(&optbuf, "ro");
386 1.33 christos
387 1.30 christos if (flags & MNT_UPDATE) {
388 1.35 mycroft catopt(&optbuf, "update");
389 1.30 christos /* Figure out the fstype only if we defaulted to ffs */
390 1.69 christos if (vfstype == ffs_fstype && statvfs(name, &sf) != -1)
391 1.30 christos vfstype = sf.f_fstypename;
392 1.30 christos }
393 1.12 mycroft
394 1.35 mycroft maxargc = 64;
395 1.35 mycroft argv = malloc(sizeof(char *) * maxargc);
396 1.35 mycroft
397 1.33 christos (void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
398 1.12 mycroft argc = 0;
399 1.33 christos argv[argc++] = execbase;
400 1.35 mycroft if (optbuf)
401 1.35 mycroft mangle(optbuf, &argc, &argv, &maxargc);
402 1.12 mycroft argv[argc++] = spec;
403 1.12 mycroft argv[argc++] = name;
404 1.12 mycroft argv[argc] = NULL;
405 1.12 mycroft
406 1.61 christos if (verbose && buf == NULL) {
407 1.35 mycroft (void)printf("exec:");
408 1.35 mycroft for (i = 0; i < argc; i++)
409 1.12 mycroft (void)printf(" %s", argv[i]);
410 1.12 mycroft (void)printf("\n");
411 1.12 mycroft }
412 1.5 mycroft
413 1.61 christos if (buf) {
414 1.61 christos if (pipe(pfd) == -1)
415 1.61 christos warn("Cannot create pipe");
416 1.61 christos }
417 1.61 christos
418 1.12 mycroft switch (pid = vfork()) {
419 1.12 mycroft case -1: /* Error. */
420 1.12 mycroft warn("vfork");
421 1.35 mycroft if (optbuf)
422 1.35 mycroft free(optbuf);
423 1.12 mycroft return (1);
424 1.35 mycroft
425 1.12 mycroft case 0: /* Child. */
426 1.34 christos if (debug)
427 1.34 christos _exit(0);
428 1.34 christos
429 1.61 christos if (buf) {
430 1.61 christos (void)close(pfd[0]);
431 1.61 christos (void)close(STDOUT_FILENO);
432 1.61 christos if (dup2(pfd[1], STDOUT_FILENO) == -1)
433 1.61 christos warn("Cannot open fd to mount program");
434 1.61 christos }
435 1.61 christos
436 1.12 mycroft /* Go find an executable. */
437 1.12 mycroft edir = edirs;
438 1.12 mycroft do {
439 1.12 mycroft (void)snprintf(execname,
440 1.33 christos sizeof(execname), "%s/%s", *edir, execbase);
441 1.40 cgd (void)execv(execname, (char * const *)argv);
442 1.12 mycroft if (errno != ENOENT)
443 1.12 mycroft warn("exec %s for %s", execname, name);
444 1.12 mycroft } while (*++edir != NULL);
445 1.1 cgd
446 1.12 mycroft if (errno == ENOENT)
447 1.33 christos warnx("%s not found for %s", execbase, name);
448 1.34 christos _exit(1);
449 1.12 mycroft /* NOTREACHED */
450 1.35 mycroft
451 1.12 mycroft default: /* Parent. */
452 1.35 mycroft if (optbuf)
453 1.35 mycroft free(optbuf);
454 1.1 cgd
455 1.64 enami if (buf || (options != NULL &&
456 1.64 enami strstr(options, "getargs") != NULL)) {
457 1.61 christos char tbuf[1024], *ptr;
458 1.61 christos int nread;
459 1.64 enami
460 1.61 christos if (buf == NULL) {
461 1.61 christos ptr = tbuf;
462 1.61 christos buflen = sizeof(tbuf) - 1;
463 1.61 christos } else {
464 1.61 christos ptr = buf;
465 1.61 christos buflen--;
466 1.61 christos }
467 1.61 christos (void)close(pfd[1]);
468 1.61 christos (void)signal(SIGPIPE, SIG_IGN);
469 1.61 christos while ((nread = read(pfd[0], ptr, buflen)) > 0) {
470 1.61 christos buflen -= nread;
471 1.61 christos ptr += nread;
472 1.61 christos }
473 1.61 christos *ptr = '\0';
474 1.61 christos if (buflen == 0) {
475 1.61 christos while (read(pfd[0], &nread, sizeof(nread)) > 0)
476 1.61 christos continue;
477 1.61 christos }
478 1.61 christos if (buf == NULL)
479 1.64 enami (void)fprintf(stdout, "%s", tbuf);
480 1.61 christos }
481 1.61 christos
482 1.12 mycroft if (waitpid(pid, &status, 0) < 0) {
483 1.12 mycroft warn("waitpid");
484 1.12 mycroft return (1);
485 1.1 cgd }
486 1.12 mycroft
487 1.12 mycroft if (WIFEXITED(status)) {
488 1.12 mycroft if (WEXITSTATUS(status) != 0)
489 1.12 mycroft return (WEXITSTATUS(status));
490 1.12 mycroft } else if (WIFSIGNALED(status)) {
491 1.17 jtc warnx("%s: %s", name, strsignal(WTERMSIG(status)));
492 1.12 mycroft return (1);
493 1.1 cgd }
494 1.12 mycroft
495 1.61 christos if (buf == NULL) {
496 1.61 christos if (verbose) {
497 1.69 christos if (statvfs(name, &sf) < 0) {
498 1.69 christos warn("statvfs %s", name);
499 1.61 christos return (1);
500 1.61 christos }
501 1.61 christos prmount(&sf);
502 1.12 mycroft }
503 1.1 cgd }
504 1.12 mycroft break;
505 1.1 cgd }
506 1.12 mycroft
507 1.12 mycroft return (0);
508 1.1 cgd }
509 1.1 cgd
510 1.52 jdolecek static void
511 1.32 lukem prmount(sfp)
512 1.69 christos struct statvfs *sfp;
513 1.13 mycroft {
514 1.12 mycroft int flags;
515 1.52 jdolecek const struct opt *o;
516 1.32 lukem struct passwd *pw;
517 1.12 mycroft int f;
518 1.1 cgd
519 1.53 enami (void)printf("%s on %s type %.*s", sfp->f_mntfromname,
520 1.53 enami sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
521 1.1 cgd
522 1.69 christos flags = sfp->f_flag & MNT_VISFLAGMASK;
523 1.58 christos for (f = 0, o = optnames; flags && o <
524 1.58 christos &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
525 1.12 mycroft if (flags & o->o_opt) {
526 1.58 christos if (!o->o_silent || verbose)
527 1.24 cgd (void)printf("%s%s", !f++ ? " (" : ", ",
528 1.24 cgd o->o_name);
529 1.12 mycroft flags &= ~o->o_opt;
530 1.12 mycroft }
531 1.24 cgd if (flags)
532 1.27 pk (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
533 1.24 cgd flags & (flags - 1) ? "s" : "", flags);
534 1.32 lukem if (sfp->f_owner) {
535 1.32 lukem (void)printf("%smounted by ", !f++ ? " (" : ", ");
536 1.32 lukem if ((pw = getpwuid(sfp->f_owner)) != NULL)
537 1.32 lukem (void)printf("%s", pw->pw_name);
538 1.32 lukem else
539 1.32 lukem (void)printf("%d", sfp->f_owner);
540 1.32 lukem }
541 1.61 christos if (verbose) {
542 1.69 christos (void)printf("%s", !f++ ? " (" : ", ");
543 1.70 hannken (void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
544 1.69 christos sfp->f_syncreads, sfp->f_asyncreads);
545 1.70 hannken (void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
546 1.69 christos sfp->f_syncwrites, sfp->f_asyncwrites);
547 1.61 christos if (verbose > 1) {
548 1.61 christos char buf[2048];
549 1.63 enami
550 1.61 christos if (getmntargs(sfp, buf, sizeof(buf)))
551 1.63 enami printf(", [%s: %s]", sfp->f_fstypename, buf);
552 1.61 christos }
553 1.63 enami printf(")\n");
554 1.61 christos } else
555 1.50 is (void)printf("%s", f ? ")\n" : "\n");
556 1.61 christos }
557 1.61 christos
558 1.61 christos static int
559 1.61 christos getmntargs(sfs, buf, buflen)
560 1.69 christos struct statvfs *sfs;
561 1.61 christos char *buf;
562 1.61 christos size_t buflen;
563 1.61 christos {
564 1.64 enami
565 1.61 christos if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
566 1.61 christos "getargs", NULL, 0, buf, buflen))
567 1.64 enami return (0);
568 1.61 christos else {
569 1.61 christos if (*buf == '\0')
570 1.64 enami return (0);
571 1.61 christos if ((buf = strchr(buf, '\n')) != NULL)
572 1.61 christos *buf = '\0';
573 1.64 enami return (1);
574 1.61 christos }
575 1.1 cgd }
576 1.1 cgd
577 1.69 christos static struct statvfs *
578 1.1 cgd getmntpt(name)
579 1.12 mycroft const char *name;
580 1.1 cgd {
581 1.69 christos struct statvfs *mntbuf;
582 1.12 mycroft int i, mntsize;
583 1.1 cgd
584 1.1 cgd mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
585 1.12 mycroft for (i = 0; i < mntsize; i++)
586 1.12 mycroft if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
587 1.12 mycroft strcmp(mntbuf[i].f_mntonname, name) == 0)
588 1.1 cgd return (&mntbuf[i]);
589 1.12 mycroft return (NULL);
590 1.1 cgd }
591 1.1 cgd
592 1.52 jdolecek static void
593 1.35 mycroft catopt(sp, o)
594 1.35 mycroft char **sp;
595 1.35 mycroft const char *o;
596 1.12 mycroft {
597 1.67 itojun char *s, *n;
598 1.12 mycroft
599 1.35 mycroft s = *sp;
600 1.35 mycroft if (s) {
601 1.67 itojun if (asprintf(&n, "%s,%s", s, o) < 0)
602 1.67 itojun err(1, "asprintf");
603 1.67 itojun free(s);
604 1.67 itojun s = n;
605 1.12 mycroft } else
606 1.35 mycroft s = strdup(o);
607 1.35 mycroft *sp = s;
608 1.1 cgd }
609 1.1 cgd
610 1.35 mycroft static void
611 1.35 mycroft mangle(options, argcp, argvp, maxargcp)
612 1.12 mycroft char *options;
613 1.35 mycroft int *argcp, *maxargcp;
614 1.35 mycroft const char ***argvp;
615 1.1 cgd {
616 1.12 mycroft char *p, *s;
617 1.35 mycroft int argc, maxargc;
618 1.67 itojun const char **argv, **nargv;
619 1.1 cgd
620 1.12 mycroft argc = *argcp;
621 1.35 mycroft argv = *argvp;
622 1.35 mycroft maxargc = *maxargcp;
623 1.35 mycroft
624 1.35 mycroft for (s = options; (p = strsep(&s, ",")) != NULL;) {
625 1.35 mycroft /* Always leave space for one more argument and the NULL. */
626 1.35 mycroft if (argc >= maxargc - 4) {
627 1.67 itojun nargv = realloc(argv, (maxargc << 1) * sizeof(char *));
628 1.67 itojun if (!nargv)
629 1.67 itojun err(1, "realloc");
630 1.67 itojun argv = nargv;
631 1.35 mycroft maxargc <<= 1;
632 1.35 mycroft }
633 1.44 ross if (*p != '\0') {
634 1.12 mycroft if (*p == '-') {
635 1.12 mycroft argv[argc++] = p;
636 1.12 mycroft p = strchr(p, '=');
637 1.12 mycroft if (p) {
638 1.12 mycroft *p = '\0';
639 1.12 mycroft argv[argc++] = p+1;
640 1.1 cgd }
641 1.12 mycroft } else if (strcmp(p, "rw") != 0) {
642 1.12 mycroft argv[argc++] = "-o";
643 1.12 mycroft argv[argc++] = p;
644 1.1 cgd }
645 1.44 ross }
646 1.35 mycroft }
647 1.12 mycroft
648 1.12 mycroft *argcp = argc;
649 1.35 mycroft *argvp = argv;
650 1.35 mycroft *maxargcp = maxargc;
651 1.48 abs }
652 1.48 abs
653 1.53 enami /* Deduce the filesystem type from the disk label. */
654 1.53 enami static const char *
655 1.48 abs getfslab(str)
656 1.48 abs const char *str;
657 1.48 abs {
658 1.48 abs struct disklabel dl;
659 1.48 abs int fd;
660 1.48 abs int part;
661 1.48 abs const char *vfstype;
662 1.48 abs u_char fstype;
663 1.53 enami char buf[MAXPATHLEN + 1];
664 1.51 abs char *sp, *ep;
665 1.48 abs
666 1.51 abs if ((fd = open(str, O_RDONLY)) == -1) {
667 1.51 abs /*
668 1.51 abs * Iff we get EBUSY try the raw device. Since mount always uses
669 1.51 abs * the block device we know we are never passed a raw device.
670 1.51 abs */
671 1.51 abs if (errno != EBUSY)
672 1.51 abs err(1, "cannot open `%s'", str);
673 1.51 abs strlcpy(buf, str, MAXPATHLEN);
674 1.51 abs if ((sp = strrchr(buf, '/')) != NULL)
675 1.51 abs ++sp;
676 1.51 abs else
677 1.51 abs sp = buf;
678 1.53 enami for (ep = sp + strlen(sp) + 1; ep > sp; ep--)
679 1.53 enami *ep = *(ep - 1);
680 1.51 abs *sp = 'r';
681 1.51 abs
682 1.51 abs /* Silently fail here - mount call can display error */
683 1.51 abs if ((fd = open(buf, O_RDONLY)) == -1)
684 1.53 enami return (NULL);
685 1.51 abs }
686 1.49 abs
687 1.51 abs if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
688 1.54 enami (void) close(fd);
689 1.53 enami return (NULL);
690 1.49 abs }
691 1.48 abs
692 1.48 abs (void) close(fd);
693 1.48 abs
694 1.48 abs part = str[strlen(str) - 1] - 'a';
695 1.48 abs
696 1.48 abs if (part < 0 || part >= dl.d_npartitions)
697 1.59 nathanw return (NULL);
698 1.48 abs
699 1.48 abs /* Return NULL for unknown types - caller can fall back to ffs */
700 1.48 abs if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
701 1.48 abs vfstype = NULL;
702 1.48 abs else
703 1.48 abs vfstype = mountnames[fstype];
704 1.48 abs
705 1.53 enami return (vfstype);
706 1.1 cgd }
707 1.1 cgd
708 1.52 jdolecek static void
709 1.12 mycroft usage()
710 1.1 cgd {
711 1.1 cgd
712 1.12 mycroft (void)fprintf(stderr,
713 1.53 enami "usage: mount %s\n mount %s\n mount %s\n",
714 1.57 soren "[-Aadfruvw] [-t type]",
715 1.57 soren "[-dfruvw] special | node",
716 1.57 soren "[-dfruvw] [-o options] [-t type] special node");
717 1.12 mycroft exit(1);
718 1.40 cgd /* NOTREACHED */
719 1.1 cgd }
720