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