mount.c revision 1.89 1 1.89 yamt /* $NetBSD: mount.c,v 1.89 2009/05/04 11:41:48 yamt 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.85 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\
35 1.85 lukem The Regents of the University of California. All rights reserved.");
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.89 yamt __RCSID("$NetBSD: mount.c,v 1.89 2009/05/04 11:41:48 yamt 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.73 thorpej #include <sys/disk.h>
63 1.48 abs #include <sys/disklabel.h>
64 1.48 abs #include <sys/ioctl.h>
65 1.48 abs
66 1.1 cgd #include "pathnames.h"
67 1.86 pooka #include "mountprog.h"
68 1.1 cgd
69 1.52 jdolecek static int debug, verbose;
70 1.1 cgd
71 1.76 xtraeme static void catopt(char **, const char *);
72 1.53 enami static const char *
73 1.76 xtraeme getfslab(const char *str);
74 1.69 christos static struct statvfs *
75 1.76 xtraeme getmntpt(const char *);
76 1.76 xtraeme static int getmntargs(struct statvfs *, char *, size_t);
77 1.76 xtraeme static int hasopt(const char *, const char *);
78 1.82 christos static void mangle(char *, int *, const char ** volatile *, int *);
79 1.76 xtraeme static int mountfs(const char *, const char *, const char *,
80 1.76 xtraeme int, const char *, const char *, int, char *, size_t);
81 1.76 xtraeme static void prmount(struct statvfs *);
82 1.76 xtraeme static void usage(void);
83 1.53 enami
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.76 xtraeme main(int argc, char *argv[])
98 1.1 cgd {
99 1.32 lukem const char *mntfromname, *mntonname, **vfslist, *vfstype;
100 1.12 mycroft struct fstab *fs;
101 1.69 christos struct statvfs *mntbuf;
102 1.89 yamt #if 0
103 1.12 mycroft FILE *mountdfp;
104 1.89 yamt #endif
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.75 he char canonical_path_buf[MAXPATHLEN];
109 1.75 he char *canonical_path;
110 1.52 jdolecek
111 1.52 jdolecek /* started as "mount" */
112 1.22 cgd all = forceall = init_flags = 0;
113 1.12 mycroft options = NULL;
114 1.32 lukem vfslist = NULL;
115 1.40 cgd vfstype = ffs_fstype;
116 1.31 lukem while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
117 1.12 mycroft switch (ch) {
118 1.22 cgd case 'A':
119 1.22 cgd all = forceall = 1;
120 1.22 cgd break;
121 1.1 cgd case 'a':
122 1.1 cgd all = 1;
123 1.1 cgd break;
124 1.12 mycroft case 'd':
125 1.12 mycroft debug = 1;
126 1.12 mycroft break;
127 1.1 cgd case 'f':
128 1.12 mycroft init_flags |= MNT_FORCE;
129 1.12 mycroft break;
130 1.12 mycroft case 'o':
131 1.12 mycroft if (*optarg)
132 1.35 mycroft catopt(&options, optarg);
133 1.1 cgd break;
134 1.1 cgd case 'r':
135 1.12 mycroft init_flags |= MNT_RDONLY;
136 1.12 mycroft break;
137 1.12 mycroft case 't':
138 1.32 lukem if (vfslist != NULL)
139 1.53 enami errx(1,
140 1.53 enami "only one -t option may be specified.");
141 1.32 lukem vfslist = makevfslist(optarg);
142 1.12 mycroft vfstype = optarg;
143 1.1 cgd break;
144 1.1 cgd case 'u':
145 1.12 mycroft init_flags |= MNT_UPDATE;
146 1.1 cgd break;
147 1.1 cgd case 'v':
148 1.61 christos verbose++;
149 1.1 cgd break;
150 1.1 cgd case 'w':
151 1.12 mycroft init_flags &= ~MNT_RDONLY;
152 1.1 cgd break;
153 1.1 cgd case '?':
154 1.1 cgd default:
155 1.1 cgd usage();
156 1.1 cgd /* NOTREACHED */
157 1.1 cgd }
158 1.1 cgd argc -= optind;
159 1.1 cgd argv += optind;
160 1.1 cgd
161 1.12 mycroft #define BADTYPE(type) \
162 1.12 mycroft (strcmp(type, FSTAB_RO) && \
163 1.12 mycroft strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
164 1.12 mycroft
165 1.12 mycroft rval = 0;
166 1.12 mycroft switch (argc) {
167 1.12 mycroft case 0:
168 1.12 mycroft if (all)
169 1.12 mycroft while ((fs = getfsent()) != NULL) {
170 1.12 mycroft if (BADTYPE(fs->fs_type))
171 1.12 mycroft continue;
172 1.32 lukem if (checkvfsname(fs->fs_vfstype, vfslist))
173 1.12 mycroft continue;
174 1.12 mycroft if (hasopt(fs->fs_mntops, "noauto"))
175 1.12 mycroft continue;
176 1.68 cgd if (strcmp(fs->fs_spec, "from_mount") == 0) {
177 1.68 cgd if ((mntbuf = getmntpt(fs->fs_file)) == NULL)
178 1.68 cgd errx(1,
179 1.68 cgd "unknown file system %s.",
180 1.68 cgd fs->fs_file);
181 1.68 cgd mntfromname = mntbuf->f_mntfromname;
182 1.68 cgd } else
183 1.68 cgd mntfromname = fs->fs_spec;
184 1.68 cgd if (mountfs(fs->fs_vfstype, mntfromname,
185 1.12 mycroft fs->fs_file, init_flags, options,
186 1.61 christos fs->fs_mntops, !forceall, NULL, 0))
187 1.12 mycroft rval = 1;
188 1.12 mycroft }
189 1.12 mycroft else {
190 1.12 mycroft if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
191 1.12 mycroft err(1, "getmntinfo");
192 1.12 mycroft for (i = 0; i < mntsize; i++) {
193 1.32 lukem if (checkvfsname(mntbuf[i].f_fstypename,
194 1.32 lukem vfslist))
195 1.12 mycroft continue;
196 1.13 mycroft prmount(&mntbuf[i]);
197 1.12 mycroft }
198 1.1 cgd }
199 1.1 cgd exit(rval);
200 1.40 cgd /* NOTREACHED */
201 1.12 mycroft case 1:
202 1.40 cgd if (vfslist != NULL) {
203 1.1 cgd usage();
204 1.40 cgd /* NOTREACHED */
205 1.40 cgd }
206 1.1 cgd
207 1.74 erh /*
208 1.74 erh * Create a canonical version of the device or mount path
209 1.74 erh * passed to us. It's ok for this to fail. It's also ok
210 1.74 erh * for the result to be exactly the same as the original.
211 1.74 erh */
212 1.74 erh canonical_path = realpath(*argv, canonical_path_buf);
213 1.74 erh
214 1.12 mycroft if (init_flags & MNT_UPDATE) {
215 1.74 erh /*
216 1.77 lukem * Try looking up the canonical path first,
217 1.74 erh * then try exactly what the user entered.
218 1.74 erh */
219 1.74 erh if ((canonical_path == NULL ||
220 1.74 erh (mntbuf = getmntpt(canonical_path)) == NULL) &&
221 1.74 erh (mntbuf = getmntpt(*argv)) == NULL
222 1.74 erh )
223 1.74 erh {
224 1.12 mycroft errx(1,
225 1.12 mycroft "unknown special file or file system %s.",
226 1.12 mycroft *argv);
227 1.74 erh }
228 1.68 cgd mntfromname = mntbuf->f_mntfromname;
229 1.42 ross if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
230 1.68 cgd if (strcmp(fs->fs_spec, "from_mount") != 0)
231 1.68 cgd mntfromname = fs->fs_spec;
232 1.42 ross /* ignore the fstab file options. */
233 1.42 ross fs->fs_mntops = NULL;
234 1.68 cgd }
235 1.42 ross mntonname = mntbuf->f_mntonname;
236 1.42 ross fstypename = mntbuf->f_fstypename;
237 1.42 ross mountopts = NULL;
238 1.12 mycroft } else {
239 1.74 erh /*
240 1.77 lukem * Try looking up the canonical path first,
241 1.74 erh * then try exactly what the user entered.
242 1.74 erh */
243 1.77 lukem if (canonical_path == NULL ||
244 1.77 lukem ((fs = getfsfile(canonical_path)) == NULL &&
245 1.77 lukem (fs = getfsspec(canonical_path)) == NULL))
246 1.74 erh {
247 1.74 erh if ((fs = getfsfile(*argv)) == NULL &&
248 1.74 erh (fs = getfsspec(*argv)) == NULL)
249 1.74 erh {
250 1.74 erh errx(1,
251 1.74 erh "%s: unknown special file or file system.",
252 1.74 erh *argv);
253 1.74 erh }
254 1.74 erh }
255 1.12 mycroft if (BADTYPE(fs->fs_type))
256 1.12 mycroft errx(1, "%s has unknown file system type.",
257 1.12 mycroft *argv);
258 1.68 cgd if (strcmp(fs->fs_spec, "from_mount") == 0) {
259 1.74 erh if ((canonical_path == NULL ||
260 1.74 erh (mntbuf = getmntpt(canonical_path)) == NULL) &&
261 1.74 erh (mntbuf = getmntpt(*argv)) == NULL
262 1.74 erh )
263 1.74 erh {
264 1.68 cgd errx(1,
265 1.68 cgd "unknown special file or file system %s.",
266 1.68 cgd *argv);
267 1.74 erh }
268 1.68 cgd mntfromname = mntbuf->f_mntfromname;
269 1.68 cgd } else
270 1.68 cgd mntfromname = fs->fs_spec;
271 1.42 ross mntonname = fs->fs_file;
272 1.42 ross fstypename = fs->fs_vfstype;
273 1.42 ross mountopts = fs->fs_mntops;
274 1.12 mycroft }
275 1.42 ross rval = mountfs(fstypename, mntfromname,
276 1.61 christos mntonname, init_flags, options, mountopts, 0, NULL, 0);
277 1.12 mycroft break;
278 1.12 mycroft case 2:
279 1.1 cgd /*
280 1.12 mycroft * If -t flag has not been specified, and spec contains either
281 1.12 mycroft * a ':' or a '@' then assume that an NFS filesystem is being
282 1.12 mycroft * specified ala Sun.
283 1.1 cgd */
284 1.48 abs if (vfslist == NULL) {
285 1.87 pooka if (strpbrk(argv[0], ":@") != NULL) {
286 1.87 pooka fprintf(stderr, "WARNING: autoselecting nfs "
287 1.87 pooka "based on : or @ in the device name is "
288 1.87 pooka "deprecated!\n"
289 1.87 pooka "WARNING: This behaviour will be removed "
290 1.87 pooka "in a future release\n");
291 1.48 abs vfstype = "nfs";
292 1.87 pooka } else {
293 1.48 abs vfstype = getfslab(argv[0]);
294 1.48 abs if (vfstype == NULL)
295 1.48 abs vfstype = ffs_fstype;
296 1.48 abs }
297 1.48 abs }
298 1.12 mycroft rval = mountfs(vfstype,
299 1.61 christos argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
300 1.12 mycroft break;
301 1.12 mycroft default:
302 1.12 mycroft usage();
303 1.12 mycroft /* NOTREACHED */
304 1.1 cgd }
305 1.1 cgd
306 1.89 yamt #if 0 /* disabled because it interferes the service. */
307 1.12 mycroft /*
308 1.12 mycroft * If the mount was successfully, and done by root, tell mountd the
309 1.12 mycroft * good news. Pid checks are probably unnecessary, but don't hurt.
310 1.12 mycroft */
311 1.12 mycroft if (rval == 0 && getuid() == 0 &&
312 1.12 mycroft (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
313 1.29 christos int pid;
314 1.29 christos
315 1.29 christos if (fscanf(mountdfp, "%d", &pid) == 1 &&
316 1.53 enami pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
317 1.12 mycroft err(1, "signal mountd");
318 1.12 mycroft (void)fclose(mountdfp);
319 1.1 cgd }
320 1.89 yamt #endif
321 1.1 cgd
322 1.12 mycroft exit(rval);
323 1.40 cgd /* NOTREACHED */
324 1.1 cgd }
325 1.1 cgd
326 1.12 mycroft int
327 1.76 xtraeme hasopt(const char *mntopts, const char *option)
328 1.1 cgd {
329 1.12 mycroft int negative, found;
330 1.12 mycroft char *opt, *optbuf;
331 1.1 cgd
332 1.12 mycroft if (option[0] == 'n' && option[1] == 'o') {
333 1.12 mycroft negative = 1;
334 1.12 mycroft option += 2;
335 1.12 mycroft } else
336 1.12 mycroft negative = 0;
337 1.12 mycroft optbuf = strdup(mntopts);
338 1.12 mycroft found = 0;
339 1.12 mycroft for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
340 1.12 mycroft if (opt[0] == 'n' && opt[1] == 'o') {
341 1.12 mycroft if (!strcasecmp(opt + 2, option))
342 1.12 mycroft found = negative;
343 1.12 mycroft } else if (!strcasecmp(opt, option))
344 1.12 mycroft found = !negative;
345 1.12 mycroft }
346 1.12 mycroft free(optbuf);
347 1.12 mycroft return (found);
348 1.1 cgd }
349 1.1 cgd
350 1.52 jdolecek static int
351 1.76 xtraeme mountfs(const char *vfstype, const char *spec, const char *name,
352 1.76 xtraeme int flags, const char *options, const char *mntopts,
353 1.76 xtraeme int skipmounted, char *buf, size_t buflen)
354 1.1 cgd {
355 1.12 mycroft /* List of directories containing mount_xxx subcommands. */
356 1.12 mycroft static const char *edirs[] = {
357 1.72 christos #ifdef RESCUEDIR
358 1.72 christos RESCUEDIR,
359 1.60 lukem #endif
360 1.12 mycroft _PATH_SBIN,
361 1.12 mycroft _PATH_USRSBIN,
362 1.12 mycroft NULL
363 1.12 mycroft };
364 1.82 christos const char ** volatile argv, **edir;
365 1.69 christos struct statvfs *sfp, sf;
366 1.12 mycroft pid_t pid;
367 1.61 christos int pfd[2];
368 1.36 drochner int argc, numfs, i, status, maxargc;
369 1.33 christos char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
370 1.33 christos mntpath[MAXPATHLEN];
371 1.82 christos volatile int getargs;
372 1.1 cgd
373 1.12 mycroft if (realpath(name, mntpath) == NULL) {
374 1.20 ghudson warn("realpath %s", name);
375 1.12 mycroft return (1);
376 1.12 mycroft }
377 1.1 cgd
378 1.12 mycroft name = mntpath;
379 1.1 cgd
380 1.35 mycroft optbuf = NULL;
381 1.35 mycroft if (mntopts)
382 1.35 mycroft catopt(&optbuf, mntopts);
383 1.81 christos
384 1.81 christos if (options) {
385 1.35 mycroft catopt(&optbuf, options);
386 1.81 christos getargs = strstr(options, "getargs") != NULL;
387 1.81 christos } else
388 1.81 christos getargs = 0;
389 1.81 christos
390 1.35 mycroft if (!mntopts && !options)
391 1.35 mycroft catopt(&optbuf, "rw");
392 1.12 mycroft
393 1.81 christos if (getargs == 0 && strcmp(name, "/") == 0)
394 1.12 mycroft flags |= MNT_UPDATE;
395 1.22 cgd else if (skipmounted) {
396 1.36 drochner if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
397 1.36 drochner warn("getmntinfo");
398 1.21 cgd return (1);
399 1.21 cgd }
400 1.36 drochner for(i = 0; i < numfs; i++) {
401 1.53 enami /*
402 1.53 enami * XXX can't check f_mntfromname,
403 1.53 enami * thanks to mfs, union, etc.
404 1.53 enami */
405 1.36 drochner if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
406 1.36 drochner strncmp(vfstype, sfp[i].f_fstypename,
407 1.83 christos sizeof(sfp[i].f_fstypename)) == 0) {
408 1.36 drochner if (verbose)
409 1.53 enami (void)printf("%s on %s type %.*s: "
410 1.53 enami "%s\n",
411 1.53 enami sfp[i].f_mntfromname,
412 1.53 enami sfp[i].f_mntonname,
413 1.84 christos (int)sizeof(sfp[i].f_fstypename),
414 1.53 enami sfp[i].f_fstypename,
415 1.53 enami "already mounted");
416 1.36 drochner return (0);
417 1.36 drochner }
418 1.21 cgd }
419 1.21 cgd }
420 1.12 mycroft if (flags & MNT_FORCE)
421 1.35 mycroft catopt(&optbuf, "force");
422 1.12 mycroft if (flags & MNT_RDONLY)
423 1.35 mycroft catopt(&optbuf, "ro");
424 1.33 christos
425 1.30 christos if (flags & MNT_UPDATE) {
426 1.35 mycroft catopt(&optbuf, "update");
427 1.30 christos /* Figure out the fstype only if we defaulted to ffs */
428 1.69 christos if (vfstype == ffs_fstype && statvfs(name, &sf) != -1)
429 1.30 christos vfstype = sf.f_fstypename;
430 1.30 christos }
431 1.12 mycroft
432 1.35 mycroft maxargc = 64;
433 1.35 mycroft argv = malloc(sizeof(char *) * maxargc);
434 1.79 rumble if (argv == NULL)
435 1.79 rumble err(1, "malloc");
436 1.35 mycroft
437 1.88 pooka if (hasopt(optbuf, "rump"))
438 1.88 pooka (void)snprintf(execbase, sizeof(execbase), "rump_%s", vfstype);
439 1.88 pooka else
440 1.88 pooka (void)snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
441 1.12 mycroft argc = 0;
442 1.33 christos argv[argc++] = execbase;
443 1.35 mycroft if (optbuf)
444 1.35 mycroft mangle(optbuf, &argc, &argv, &maxargc);
445 1.12 mycroft argv[argc++] = spec;
446 1.12 mycroft argv[argc++] = name;
447 1.12 mycroft argv[argc] = NULL;
448 1.12 mycroft
449 1.81 christos if ((verbose && buf == NULL) || debug) {
450 1.35 mycroft (void)printf("exec:");
451 1.35 mycroft for (i = 0; i < argc; i++)
452 1.12 mycroft (void)printf(" %s", argv[i]);
453 1.12 mycroft (void)printf("\n");
454 1.12 mycroft }
455 1.5 mycroft
456 1.61 christos if (buf) {
457 1.61 christos if (pipe(pfd) == -1)
458 1.61 christos warn("Cannot create pipe");
459 1.61 christos }
460 1.61 christos
461 1.12 mycroft switch (pid = vfork()) {
462 1.12 mycroft case -1: /* Error. */
463 1.12 mycroft warn("vfork");
464 1.35 mycroft if (optbuf)
465 1.35 mycroft free(optbuf);
466 1.80 christos free(argv);
467 1.12 mycroft return (1);
468 1.35 mycroft
469 1.12 mycroft case 0: /* Child. */
470 1.34 christos if (debug)
471 1.34 christos _exit(0);
472 1.34 christos
473 1.61 christos if (buf) {
474 1.61 christos (void)close(pfd[0]);
475 1.61 christos (void)close(STDOUT_FILENO);
476 1.61 christos if (dup2(pfd[1], STDOUT_FILENO) == -1)
477 1.61 christos warn("Cannot open fd to mount program");
478 1.61 christos }
479 1.61 christos
480 1.12 mycroft /* Go find an executable. */
481 1.12 mycroft edir = edirs;
482 1.12 mycroft do {
483 1.12 mycroft (void)snprintf(execname,
484 1.33 christos sizeof(execname), "%s/%s", *edir, execbase);
485 1.78 christos (void)execv(execname, __UNCONST(argv));
486 1.12 mycroft if (errno != ENOENT)
487 1.12 mycroft warn("exec %s for %s", execname, name);
488 1.12 mycroft } while (*++edir != NULL);
489 1.1 cgd
490 1.12 mycroft if (errno == ENOENT)
491 1.33 christos warnx("%s not found for %s", execbase, name);
492 1.34 christos _exit(1);
493 1.12 mycroft /* NOTREACHED */
494 1.35 mycroft
495 1.12 mycroft default: /* Parent. */
496 1.35 mycroft if (optbuf)
497 1.35 mycroft free(optbuf);
498 1.80 christos free(argv);
499 1.1 cgd
500 1.81 christos if (buf || getargs) {
501 1.61 christos char tbuf[1024], *ptr;
502 1.61 christos int nread;
503 1.64 enami
504 1.61 christos if (buf == NULL) {
505 1.61 christos ptr = tbuf;
506 1.61 christos buflen = sizeof(tbuf) - 1;
507 1.61 christos } else {
508 1.61 christos ptr = buf;
509 1.61 christos buflen--;
510 1.61 christos }
511 1.61 christos (void)close(pfd[1]);
512 1.61 christos (void)signal(SIGPIPE, SIG_IGN);
513 1.61 christos while ((nread = read(pfd[0], ptr, buflen)) > 0) {
514 1.61 christos buflen -= nread;
515 1.61 christos ptr += nread;
516 1.61 christos }
517 1.61 christos *ptr = '\0';
518 1.61 christos if (buflen == 0) {
519 1.61 christos while (read(pfd[0], &nread, sizeof(nread)) > 0)
520 1.61 christos continue;
521 1.61 christos }
522 1.61 christos if (buf == NULL)
523 1.64 enami (void)fprintf(stdout, "%s", tbuf);
524 1.61 christos }
525 1.61 christos
526 1.12 mycroft if (waitpid(pid, &status, 0) < 0) {
527 1.12 mycroft warn("waitpid");
528 1.12 mycroft return (1);
529 1.1 cgd }
530 1.12 mycroft
531 1.12 mycroft if (WIFEXITED(status)) {
532 1.12 mycroft if (WEXITSTATUS(status) != 0)
533 1.12 mycroft return (WEXITSTATUS(status));
534 1.12 mycroft } else if (WIFSIGNALED(status)) {
535 1.17 jtc warnx("%s: %s", name, strsignal(WTERMSIG(status)));
536 1.12 mycroft return (1);
537 1.1 cgd }
538 1.12 mycroft
539 1.61 christos if (buf == NULL) {
540 1.61 christos if (verbose) {
541 1.69 christos if (statvfs(name, &sf) < 0) {
542 1.69 christos warn("statvfs %s", name);
543 1.61 christos return (1);
544 1.61 christos }
545 1.61 christos prmount(&sf);
546 1.12 mycroft }
547 1.1 cgd }
548 1.12 mycroft break;
549 1.1 cgd }
550 1.12 mycroft
551 1.12 mycroft return (0);
552 1.1 cgd }
553 1.1 cgd
554 1.52 jdolecek static void
555 1.76 xtraeme prmount(struct statvfs *sfp)
556 1.13 mycroft {
557 1.12 mycroft int flags;
558 1.52 jdolecek const struct opt *o;
559 1.32 lukem struct passwd *pw;
560 1.12 mycroft int f;
561 1.1 cgd
562 1.53 enami (void)printf("%s on %s type %.*s", sfp->f_mntfromname,
563 1.84 christos sfp->f_mntonname, (int)sizeof(sfp->f_fstypename),
564 1.84 christos sfp->f_fstypename);
565 1.1 cgd
566 1.69 christos flags = sfp->f_flag & MNT_VISFLAGMASK;
567 1.58 christos for (f = 0, o = optnames; flags && o <
568 1.58 christos &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
569 1.12 mycroft if (flags & o->o_opt) {
570 1.58 christos if (!o->o_silent || verbose)
571 1.24 cgd (void)printf("%s%s", !f++ ? " (" : ", ",
572 1.24 cgd o->o_name);
573 1.12 mycroft flags &= ~o->o_opt;
574 1.12 mycroft }
575 1.24 cgd if (flags)
576 1.27 pk (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
577 1.24 cgd flags & (flags - 1) ? "s" : "", flags);
578 1.32 lukem if (sfp->f_owner) {
579 1.32 lukem (void)printf("%smounted by ", !f++ ? " (" : ", ");
580 1.32 lukem if ((pw = getpwuid(sfp->f_owner)) != NULL)
581 1.32 lukem (void)printf("%s", pw->pw_name);
582 1.32 lukem else
583 1.32 lukem (void)printf("%d", sfp->f_owner);
584 1.32 lukem }
585 1.71 enami if (verbose)
586 1.71 enami (void)printf("%sfsid: 0x%x/0x%x",
587 1.71 enami !f++ ? " (" /* ) */: ", ",
588 1.71 enami sfp->f_fsidx.__fsid_val[0], sfp->f_fsidx.__fsid_val[1]);
589 1.71 enami
590 1.61 christos if (verbose) {
591 1.69 christos (void)printf("%s", !f++ ? " (" : ", ");
592 1.70 hannken (void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
593 1.69 christos sfp->f_syncreads, sfp->f_asyncreads);
594 1.70 hannken (void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
595 1.69 christos sfp->f_syncwrites, sfp->f_asyncwrites);
596 1.61 christos if (verbose > 1) {
597 1.61 christos char buf[2048];
598 1.63 enami
599 1.61 christos if (getmntargs(sfp, buf, sizeof(buf)))
600 1.63 enami printf(", [%s: %s]", sfp->f_fstypename, buf);
601 1.61 christos }
602 1.63 enami printf(")\n");
603 1.61 christos } else
604 1.50 is (void)printf("%s", f ? ")\n" : "\n");
605 1.61 christos }
606 1.61 christos
607 1.61 christos static int
608 1.76 xtraeme getmntargs(struct statvfs *sfs, char *buf, size_t buflen)
609 1.61 christos {
610 1.64 enami
611 1.61 christos if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
612 1.61 christos "getargs", NULL, 0, buf, buflen))
613 1.64 enami return (0);
614 1.61 christos else {
615 1.61 christos if (*buf == '\0')
616 1.64 enami return (0);
617 1.61 christos if ((buf = strchr(buf, '\n')) != NULL)
618 1.61 christos *buf = '\0';
619 1.64 enami return (1);
620 1.61 christos }
621 1.1 cgd }
622 1.1 cgd
623 1.69 christos static struct statvfs *
624 1.76 xtraeme getmntpt(const char *name)
625 1.1 cgd {
626 1.69 christos struct statvfs *mntbuf;
627 1.12 mycroft int i, mntsize;
628 1.1 cgd
629 1.1 cgd mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
630 1.12 mycroft for (i = 0; i < mntsize; i++)
631 1.12 mycroft if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
632 1.12 mycroft strcmp(mntbuf[i].f_mntonname, name) == 0)
633 1.1 cgd return (&mntbuf[i]);
634 1.12 mycroft return (NULL);
635 1.1 cgd }
636 1.1 cgd
637 1.52 jdolecek static void
638 1.76 xtraeme catopt(char **sp, const char *o)
639 1.12 mycroft {
640 1.67 itojun char *s, *n;
641 1.12 mycroft
642 1.35 mycroft s = *sp;
643 1.35 mycroft if (s) {
644 1.67 itojun if (asprintf(&n, "%s,%s", s, o) < 0)
645 1.67 itojun err(1, "asprintf");
646 1.67 itojun free(s);
647 1.67 itojun s = n;
648 1.12 mycroft } else
649 1.35 mycroft s = strdup(o);
650 1.35 mycroft *sp = s;
651 1.1 cgd }
652 1.1 cgd
653 1.35 mycroft static void
654 1.82 christos mangle(char *options, int *argcp, const char ** volatile *argvp, int *maxargcp)
655 1.1 cgd {
656 1.12 mycroft char *p, *s;
657 1.35 mycroft int argc, maxargc;
658 1.67 itojun const char **argv, **nargv;
659 1.1 cgd
660 1.12 mycroft argc = *argcp;
661 1.35 mycroft argv = *argvp;
662 1.35 mycroft maxargc = *maxargcp;
663 1.35 mycroft
664 1.35 mycroft for (s = options; (p = strsep(&s, ",")) != NULL;) {
665 1.35 mycroft /* Always leave space for one more argument and the NULL. */
666 1.35 mycroft if (argc >= maxargc - 4) {
667 1.67 itojun nargv = realloc(argv, (maxargc << 1) * sizeof(char *));
668 1.67 itojun if (!nargv)
669 1.67 itojun err(1, "realloc");
670 1.67 itojun argv = nargv;
671 1.35 mycroft maxargc <<= 1;
672 1.35 mycroft }
673 1.44 ross if (*p != '\0') {
674 1.12 mycroft if (*p == '-') {
675 1.12 mycroft argv[argc++] = p;
676 1.12 mycroft p = strchr(p, '=');
677 1.12 mycroft if (p) {
678 1.12 mycroft *p = '\0';
679 1.12 mycroft argv[argc++] = p+1;
680 1.1 cgd }
681 1.12 mycroft } else if (strcmp(p, "rw") != 0) {
682 1.12 mycroft argv[argc++] = "-o";
683 1.12 mycroft argv[argc++] = p;
684 1.1 cgd }
685 1.44 ross }
686 1.35 mycroft }
687 1.12 mycroft
688 1.12 mycroft *argcp = argc;
689 1.35 mycroft *argvp = argv;
690 1.35 mycroft *maxargcp = maxargc;
691 1.48 abs }
692 1.48 abs
693 1.53 enami /* Deduce the filesystem type from the disk label. */
694 1.53 enami static const char *
695 1.76 xtraeme getfslab(const char *str)
696 1.48 abs {
697 1.73 thorpej static struct dkwedge_info dkw;
698 1.48 abs struct disklabel dl;
699 1.48 abs int fd;
700 1.48 abs int part;
701 1.48 abs const char *vfstype;
702 1.48 abs u_char fstype;
703 1.53 enami char buf[MAXPATHLEN + 1];
704 1.51 abs char *sp, *ep;
705 1.48 abs
706 1.51 abs if ((fd = open(str, O_RDONLY)) == -1) {
707 1.51 abs /*
708 1.51 abs * Iff we get EBUSY try the raw device. Since mount always uses
709 1.51 abs * the block device we know we are never passed a raw device.
710 1.51 abs */
711 1.51 abs if (errno != EBUSY)
712 1.51 abs err(1, "cannot open `%s'", str);
713 1.51 abs strlcpy(buf, str, MAXPATHLEN);
714 1.51 abs if ((sp = strrchr(buf, '/')) != NULL)
715 1.51 abs ++sp;
716 1.51 abs else
717 1.51 abs sp = buf;
718 1.53 enami for (ep = sp + strlen(sp) + 1; ep > sp; ep--)
719 1.53 enami *ep = *(ep - 1);
720 1.51 abs *sp = 'r';
721 1.51 abs
722 1.51 abs /* Silently fail here - mount call can display error */
723 1.51 abs if ((fd = open(buf, O_RDONLY)) == -1)
724 1.53 enami return (NULL);
725 1.51 abs }
726 1.49 abs
727 1.73 thorpej /* Check to see if this is a wedge. */
728 1.73 thorpej if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
729 1.73 thorpej /* Yup, this is easy. */
730 1.73 thorpej (void) close(fd);
731 1.73 thorpej return (dkw.dkw_ptype);
732 1.73 thorpej }
733 1.73 thorpej
734 1.51 abs if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
735 1.54 enami (void) close(fd);
736 1.53 enami return (NULL);
737 1.49 abs }
738 1.48 abs
739 1.48 abs (void) close(fd);
740 1.48 abs
741 1.48 abs part = str[strlen(str) - 1] - 'a';
742 1.48 abs
743 1.48 abs if (part < 0 || part >= dl.d_npartitions)
744 1.59 nathanw return (NULL);
745 1.48 abs
746 1.48 abs /* Return NULL for unknown types - caller can fall back to ffs */
747 1.48 abs if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
748 1.48 abs vfstype = NULL;
749 1.48 abs else
750 1.48 abs vfstype = mountnames[fstype];
751 1.48 abs
752 1.53 enami return (vfstype);
753 1.1 cgd }
754 1.1 cgd
755 1.52 jdolecek static void
756 1.76 xtraeme usage(void)
757 1.1 cgd {
758 1.1 cgd
759 1.12 mycroft (void)fprintf(stderr,
760 1.53 enami "usage: mount %s\n mount %s\n mount %s\n",
761 1.57 soren "[-Aadfruvw] [-t type]",
762 1.57 soren "[-dfruvw] special | node",
763 1.57 soren "[-dfruvw] [-o options] [-t type] special node");
764 1.12 mycroft exit(1);
765 1.40 cgd /* NOTREACHED */
766 1.1 cgd }
767