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