mount.c revision 1.66 1 1.66 agc /* $NetBSD: mount.c,v 1.66 2003/08/07 10:04:26 agc 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.66 agc __RCSID("$NetBSD: mount.c,v 1.66 2003/08/07 10:04:26 agc 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.53 enami static struct statfs *
74 1.53 enami getmntpt __P((const char *));
75 1.61 christos static int getmntargs __P((struct statfs *, 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.52 jdolecek static void prmount __P((struct statfs *));
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.9 cgd struct statfs *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.12 mycroft if (mountfs(fs->fs_vfstype, fs->fs_spec,
175 1.12 mycroft fs->fs_file, init_flags, options,
176 1.61 christos fs->fs_mntops, !forceall, NULL, 0))
177 1.12 mycroft rval = 1;
178 1.12 mycroft }
179 1.12 mycroft else {
180 1.12 mycroft if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
181 1.12 mycroft err(1, "getmntinfo");
182 1.12 mycroft for (i = 0; i < mntsize; i++) {
183 1.32 lukem if (checkvfsname(mntbuf[i].f_fstypename,
184 1.32 lukem vfslist))
185 1.12 mycroft continue;
186 1.13 mycroft prmount(&mntbuf[i]);
187 1.12 mycroft }
188 1.1 cgd }
189 1.1 cgd exit(rval);
190 1.40 cgd /* NOTREACHED */
191 1.12 mycroft case 1:
192 1.40 cgd if (vfslist != NULL) {
193 1.1 cgd usage();
194 1.40 cgd /* NOTREACHED */
195 1.40 cgd }
196 1.1 cgd
197 1.12 mycroft if (init_flags & MNT_UPDATE) {
198 1.12 mycroft if ((mntbuf = getmntpt(*argv)) == NULL)
199 1.12 mycroft errx(1,
200 1.12 mycroft "unknown special file or file system %s.",
201 1.12 mycroft *argv);
202 1.42 ross if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
203 1.32 lukem mntfromname = fs->fs_spec;
204 1.42 ross /* ignore the fstab file options. */
205 1.42 ross fs->fs_mntops = NULL;
206 1.42 ross } else
207 1.32 lukem mntfromname = mntbuf->f_mntfromname;
208 1.42 ross mntonname = mntbuf->f_mntonname;
209 1.42 ross fstypename = mntbuf->f_fstypename;
210 1.42 ross mountopts = NULL;
211 1.12 mycroft } else {
212 1.12 mycroft if ((fs = getfsfile(*argv)) == NULL &&
213 1.12 mycroft (fs = getfsspec(*argv)) == NULL)
214 1.12 mycroft errx(1,
215 1.12 mycroft "%s: unknown special file or file system.",
216 1.12 mycroft *argv);
217 1.12 mycroft if (BADTYPE(fs->fs_type))
218 1.12 mycroft errx(1, "%s has unknown file system type.",
219 1.12 mycroft *argv);
220 1.32 lukem mntfromname = fs->fs_spec;
221 1.42 ross mntonname = fs->fs_file;
222 1.42 ross fstypename = fs->fs_vfstype;
223 1.42 ross mountopts = fs->fs_mntops;
224 1.12 mycroft }
225 1.42 ross rval = mountfs(fstypename, mntfromname,
226 1.61 christos mntonname, init_flags, options, mountopts, 0, NULL, 0);
227 1.12 mycroft break;
228 1.12 mycroft case 2:
229 1.1 cgd /*
230 1.12 mycroft * If -t flag has not been specified, and spec contains either
231 1.12 mycroft * a ':' or a '@' then assume that an NFS filesystem is being
232 1.12 mycroft * specified ala Sun.
233 1.1 cgd */
234 1.48 abs if (vfslist == NULL) {
235 1.48 abs if (strpbrk(argv[0], ":@") != NULL)
236 1.48 abs vfstype = "nfs";
237 1.48 abs else {
238 1.48 abs vfstype = getfslab(argv[0]);
239 1.48 abs if (vfstype == NULL)
240 1.48 abs vfstype = ffs_fstype;
241 1.48 abs }
242 1.48 abs }
243 1.12 mycroft rval = mountfs(vfstype,
244 1.61 christos argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
245 1.12 mycroft break;
246 1.12 mycroft default:
247 1.12 mycroft usage();
248 1.12 mycroft /* NOTREACHED */
249 1.1 cgd }
250 1.1 cgd
251 1.12 mycroft /*
252 1.12 mycroft * If the mount was successfully, and done by root, tell mountd the
253 1.12 mycroft * good news. Pid checks are probably unnecessary, but don't hurt.
254 1.12 mycroft */
255 1.12 mycroft if (rval == 0 && getuid() == 0 &&
256 1.12 mycroft (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
257 1.29 christos int pid;
258 1.29 christos
259 1.29 christos if (fscanf(mountdfp, "%d", &pid) == 1 &&
260 1.53 enami pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
261 1.12 mycroft err(1, "signal mountd");
262 1.12 mycroft (void)fclose(mountdfp);
263 1.1 cgd }
264 1.1 cgd
265 1.12 mycroft exit(rval);
266 1.40 cgd /* NOTREACHED */
267 1.1 cgd }
268 1.1 cgd
269 1.12 mycroft int
270 1.12 mycroft hasopt(mntopts, option)
271 1.12 mycroft const char *mntopts, *option;
272 1.1 cgd {
273 1.12 mycroft int negative, found;
274 1.12 mycroft char *opt, *optbuf;
275 1.1 cgd
276 1.12 mycroft if (option[0] == 'n' && option[1] == 'o') {
277 1.12 mycroft negative = 1;
278 1.12 mycroft option += 2;
279 1.12 mycroft } else
280 1.12 mycroft negative = 0;
281 1.12 mycroft optbuf = strdup(mntopts);
282 1.12 mycroft found = 0;
283 1.12 mycroft for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
284 1.12 mycroft if (opt[0] == 'n' && opt[1] == 'o') {
285 1.12 mycroft if (!strcasecmp(opt + 2, option))
286 1.12 mycroft found = negative;
287 1.12 mycroft } else if (!strcasecmp(opt, option))
288 1.12 mycroft found = !negative;
289 1.12 mycroft }
290 1.12 mycroft free(optbuf);
291 1.12 mycroft return (found);
292 1.1 cgd }
293 1.1 cgd
294 1.52 jdolecek static int
295 1.61 christos mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted, buf, buflen)
296 1.12 mycroft const char *vfstype, *spec, *name, *options, *mntopts;
297 1.22 cgd int flags, skipmounted;
298 1.61 christos char *buf;
299 1.61 christos size_t buflen;
300 1.1 cgd {
301 1.12 mycroft /* List of directories containing mount_xxx subcommands. */
302 1.12 mycroft static const char *edirs[] = {
303 1.60 lukem #ifdef _PATH_RESCUE
304 1.60 lukem _PATH_RESCUE,
305 1.60 lukem #endif
306 1.12 mycroft _PATH_SBIN,
307 1.12 mycroft _PATH_USRSBIN,
308 1.12 mycroft NULL
309 1.12 mycroft };
310 1.35 mycroft const char **argv, **edir;
311 1.36 drochner struct statfs *sfp, sf;
312 1.12 mycroft pid_t pid;
313 1.61 christos int pfd[2];
314 1.36 drochner int argc, numfs, i, status, maxargc;
315 1.33 christos char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
316 1.33 christos mntpath[MAXPATHLEN];
317 1.35 mycroft
318 1.29 christos #ifdef __GNUC__
319 1.29 christos (void) &name;
320 1.29 christos (void) &optbuf;
321 1.30 christos (void) &vfstype;
322 1.29 christos #endif
323 1.1 cgd
324 1.12 mycroft if (realpath(name, mntpath) == NULL) {
325 1.20 ghudson warn("realpath %s", name);
326 1.12 mycroft return (1);
327 1.12 mycroft }
328 1.1 cgd
329 1.12 mycroft name = mntpath;
330 1.1 cgd
331 1.35 mycroft optbuf = NULL;
332 1.35 mycroft if (mntopts)
333 1.35 mycroft catopt(&optbuf, mntopts);
334 1.35 mycroft if (options)
335 1.35 mycroft catopt(&optbuf, options);
336 1.35 mycroft if (!mntopts && !options)
337 1.35 mycroft catopt(&optbuf, "rw");
338 1.12 mycroft
339 1.35 mycroft if (!strcmp(name, "/"))
340 1.12 mycroft flags |= MNT_UPDATE;
341 1.22 cgd else if (skipmounted) {
342 1.36 drochner if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
343 1.36 drochner warn("getmntinfo");
344 1.21 cgd return (1);
345 1.21 cgd }
346 1.36 drochner for(i = 0; i < numfs; i++) {
347 1.53 enami /*
348 1.53 enami * XXX can't check f_mntfromname,
349 1.53 enami * thanks to mfs, union, etc.
350 1.53 enami */
351 1.36 drochner if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
352 1.36 drochner strncmp(vfstype, sfp[i].f_fstypename,
353 1.53 enami MFSNAMELEN) == 0) {
354 1.36 drochner if (verbose)
355 1.53 enami (void)printf("%s on %s type %.*s: "
356 1.53 enami "%s\n",
357 1.53 enami sfp[i].f_mntfromname,
358 1.53 enami sfp[i].f_mntonname,
359 1.53 enami MFSNAMELEN,
360 1.53 enami sfp[i].f_fstypename,
361 1.53 enami "already mounted");
362 1.36 drochner return (0);
363 1.36 drochner }
364 1.21 cgd }
365 1.21 cgd }
366 1.12 mycroft if (flags & MNT_FORCE)
367 1.35 mycroft catopt(&optbuf, "force");
368 1.12 mycroft if (flags & MNT_RDONLY)
369 1.35 mycroft catopt(&optbuf, "ro");
370 1.33 christos
371 1.30 christos if (flags & MNT_UPDATE) {
372 1.35 mycroft catopt(&optbuf, "update");
373 1.30 christos /* Figure out the fstype only if we defaulted to ffs */
374 1.40 cgd if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
375 1.30 christos vfstype = sf.f_fstypename;
376 1.30 christos }
377 1.12 mycroft
378 1.35 mycroft maxargc = 64;
379 1.35 mycroft argv = malloc(sizeof(char *) * maxargc);
380 1.35 mycroft
381 1.33 christos (void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
382 1.12 mycroft argc = 0;
383 1.33 christos argv[argc++] = execbase;
384 1.35 mycroft if (optbuf)
385 1.35 mycroft mangle(optbuf, &argc, &argv, &maxargc);
386 1.12 mycroft argv[argc++] = spec;
387 1.12 mycroft argv[argc++] = name;
388 1.12 mycroft argv[argc] = NULL;
389 1.12 mycroft
390 1.61 christos if (verbose && buf == NULL) {
391 1.35 mycroft (void)printf("exec:");
392 1.35 mycroft for (i = 0; i < argc; i++)
393 1.12 mycroft (void)printf(" %s", argv[i]);
394 1.12 mycroft (void)printf("\n");
395 1.12 mycroft }
396 1.5 mycroft
397 1.61 christos if (buf) {
398 1.61 christos if (pipe(pfd) == -1)
399 1.61 christos warn("Cannot create pipe");
400 1.61 christos }
401 1.61 christos
402 1.12 mycroft switch (pid = vfork()) {
403 1.12 mycroft case -1: /* Error. */
404 1.12 mycroft warn("vfork");
405 1.35 mycroft if (optbuf)
406 1.35 mycroft free(optbuf);
407 1.12 mycroft return (1);
408 1.35 mycroft
409 1.12 mycroft case 0: /* Child. */
410 1.34 christos if (debug)
411 1.34 christos _exit(0);
412 1.34 christos
413 1.61 christos if (buf) {
414 1.61 christos (void)close(pfd[0]);
415 1.61 christos (void)close(STDOUT_FILENO);
416 1.61 christos if (dup2(pfd[1], STDOUT_FILENO) == -1)
417 1.61 christos warn("Cannot open fd to mount program");
418 1.61 christos }
419 1.61 christos
420 1.12 mycroft /* Go find an executable. */
421 1.12 mycroft edir = edirs;
422 1.12 mycroft do {
423 1.12 mycroft (void)snprintf(execname,
424 1.33 christos sizeof(execname), "%s/%s", *edir, execbase);
425 1.40 cgd (void)execv(execname, (char * const *)argv);
426 1.12 mycroft if (errno != ENOENT)
427 1.12 mycroft warn("exec %s for %s", execname, name);
428 1.12 mycroft } while (*++edir != NULL);
429 1.1 cgd
430 1.12 mycroft if (errno == ENOENT)
431 1.33 christos warnx("%s not found for %s", execbase, name);
432 1.34 christos _exit(1);
433 1.12 mycroft /* NOTREACHED */
434 1.35 mycroft
435 1.12 mycroft default: /* Parent. */
436 1.35 mycroft if (optbuf)
437 1.35 mycroft free(optbuf);
438 1.1 cgd
439 1.64 enami if (buf || (options != NULL &&
440 1.64 enami strstr(options, "getargs") != NULL)) {
441 1.61 christos char tbuf[1024], *ptr;
442 1.61 christos int nread;
443 1.64 enami
444 1.61 christos if (buf == NULL) {
445 1.61 christos ptr = tbuf;
446 1.61 christos buflen = sizeof(tbuf) - 1;
447 1.61 christos } else {
448 1.61 christos ptr = buf;
449 1.61 christos buflen--;
450 1.61 christos }
451 1.61 christos (void)close(pfd[1]);
452 1.61 christos (void)signal(SIGPIPE, SIG_IGN);
453 1.61 christos while ((nread = read(pfd[0], ptr, buflen)) > 0) {
454 1.61 christos buflen -= nread;
455 1.61 christos ptr += nread;
456 1.61 christos }
457 1.61 christos *ptr = '\0';
458 1.61 christos if (buflen == 0) {
459 1.61 christos while (read(pfd[0], &nread, sizeof(nread)) > 0)
460 1.61 christos continue;
461 1.61 christos }
462 1.61 christos if (buf == NULL)
463 1.64 enami (void)fprintf(stdout, "%s", tbuf);
464 1.61 christos }
465 1.61 christos
466 1.12 mycroft if (waitpid(pid, &status, 0) < 0) {
467 1.12 mycroft warn("waitpid");
468 1.12 mycroft return (1);
469 1.1 cgd }
470 1.12 mycroft
471 1.12 mycroft if (WIFEXITED(status)) {
472 1.12 mycroft if (WEXITSTATUS(status) != 0)
473 1.12 mycroft return (WEXITSTATUS(status));
474 1.12 mycroft } else if (WIFSIGNALED(status)) {
475 1.17 jtc warnx("%s: %s", name, strsignal(WTERMSIG(status)));
476 1.12 mycroft return (1);
477 1.1 cgd }
478 1.12 mycroft
479 1.61 christos if (buf == NULL) {
480 1.61 christos if (verbose) {
481 1.61 christos if (statfs(name, &sf) < 0) {
482 1.61 christos warn("statfs %s", name);
483 1.61 christos return (1);
484 1.61 christos }
485 1.61 christos prmount(&sf);
486 1.12 mycroft }
487 1.1 cgd }
488 1.12 mycroft break;
489 1.1 cgd }
490 1.12 mycroft
491 1.12 mycroft return (0);
492 1.1 cgd }
493 1.1 cgd
494 1.52 jdolecek static void
495 1.32 lukem prmount(sfp)
496 1.32 lukem struct statfs *sfp;
497 1.13 mycroft {
498 1.12 mycroft int flags;
499 1.52 jdolecek const struct opt *o;
500 1.32 lukem struct passwd *pw;
501 1.12 mycroft int f;
502 1.1 cgd
503 1.53 enami (void)printf("%s on %s type %.*s", sfp->f_mntfromname,
504 1.53 enami sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
505 1.1 cgd
506 1.32 lukem flags = sfp->f_flags & MNT_VISFLAGMASK;
507 1.58 christos for (f = 0, o = optnames; flags && o <
508 1.58 christos &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
509 1.12 mycroft if (flags & o->o_opt) {
510 1.58 christos if (!o->o_silent || verbose)
511 1.24 cgd (void)printf("%s%s", !f++ ? " (" : ", ",
512 1.24 cgd o->o_name);
513 1.12 mycroft flags &= ~o->o_opt;
514 1.12 mycroft }
515 1.24 cgd if (flags)
516 1.27 pk (void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
517 1.24 cgd flags & (flags - 1) ? "s" : "", flags);
518 1.32 lukem if (sfp->f_owner) {
519 1.32 lukem (void)printf("%smounted by ", !f++ ? " (" : ", ");
520 1.32 lukem if ((pw = getpwuid(sfp->f_owner)) != NULL)
521 1.32 lukem (void)printf("%s", pw->pw_name);
522 1.32 lukem else
523 1.32 lukem (void)printf("%d", sfp->f_owner);
524 1.32 lukem }
525 1.61 christos if (verbose) {
526 1.61 christos (void)printf("%swrites: sync %ld async %ld",
527 1.46 fvdl !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
528 1.61 christos if (verbose > 1) {
529 1.61 christos char buf[2048];
530 1.63 enami
531 1.61 christos if (getmntargs(sfp, buf, sizeof(buf)))
532 1.63 enami printf(", [%s: %s]", sfp->f_fstypename, buf);
533 1.61 christos }
534 1.63 enami printf(")\n");
535 1.61 christos } else
536 1.50 is (void)printf("%s", f ? ")\n" : "\n");
537 1.61 christos }
538 1.61 christos
539 1.61 christos static int
540 1.61 christos getmntargs(sfs, buf, buflen)
541 1.61 christos struct statfs *sfs;
542 1.61 christos char *buf;
543 1.61 christos size_t buflen;
544 1.61 christos {
545 1.64 enami
546 1.61 christos if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
547 1.61 christos "getargs", NULL, 0, buf, buflen))
548 1.64 enami return (0);
549 1.61 christos else {
550 1.61 christos if (*buf == '\0')
551 1.64 enami return (0);
552 1.61 christos if ((buf = strchr(buf, '\n')) != NULL)
553 1.61 christos *buf = '\0';
554 1.64 enami return (1);
555 1.61 christos }
556 1.1 cgd }
557 1.1 cgd
558 1.52 jdolecek static struct statfs *
559 1.1 cgd getmntpt(name)
560 1.12 mycroft const char *name;
561 1.1 cgd {
562 1.1 cgd struct statfs *mntbuf;
563 1.12 mycroft int i, mntsize;
564 1.1 cgd
565 1.1 cgd mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
566 1.12 mycroft for (i = 0; i < mntsize; i++)
567 1.12 mycroft if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
568 1.12 mycroft strcmp(mntbuf[i].f_mntonname, name) == 0)
569 1.1 cgd return (&mntbuf[i]);
570 1.12 mycroft return (NULL);
571 1.1 cgd }
572 1.1 cgd
573 1.52 jdolecek static void
574 1.35 mycroft catopt(sp, o)
575 1.35 mycroft char **sp;
576 1.35 mycroft const char *o;
577 1.12 mycroft {
578 1.35 mycroft char *s;
579 1.35 mycroft size_t i, j;
580 1.12 mycroft
581 1.35 mycroft s = *sp;
582 1.35 mycroft if (s) {
583 1.35 mycroft i = strlen(s);
584 1.35 mycroft j = i + 1 + strlen(o) + 1;
585 1.35 mycroft s = realloc(s, j);
586 1.35 mycroft (void)snprintf(s + i, j, ",%s", o);
587 1.12 mycroft } else
588 1.35 mycroft s = strdup(o);
589 1.35 mycroft *sp = s;
590 1.1 cgd }
591 1.1 cgd
592 1.35 mycroft static void
593 1.35 mycroft mangle(options, argcp, argvp, maxargcp)
594 1.12 mycroft char *options;
595 1.35 mycroft int *argcp, *maxargcp;
596 1.35 mycroft const char ***argvp;
597 1.1 cgd {
598 1.12 mycroft char *p, *s;
599 1.35 mycroft int argc, maxargc;
600 1.35 mycroft const char **argv;
601 1.1 cgd
602 1.12 mycroft argc = *argcp;
603 1.35 mycroft argv = *argvp;
604 1.35 mycroft maxargc = *maxargcp;
605 1.35 mycroft
606 1.35 mycroft for (s = options; (p = strsep(&s, ",")) != NULL;) {
607 1.35 mycroft /* Always leave space for one more argument and the NULL. */
608 1.35 mycroft if (argc >= maxargc - 4) {
609 1.35 mycroft maxargc <<= 1;
610 1.35 mycroft argv = realloc(argv, maxargc * sizeof(char *));
611 1.35 mycroft }
612 1.44 ross if (*p != '\0') {
613 1.12 mycroft if (*p == '-') {
614 1.12 mycroft argv[argc++] = p;
615 1.12 mycroft p = strchr(p, '=');
616 1.12 mycroft if (p) {
617 1.12 mycroft *p = '\0';
618 1.12 mycroft argv[argc++] = p+1;
619 1.1 cgd }
620 1.12 mycroft } else if (strcmp(p, "rw") != 0) {
621 1.12 mycroft argv[argc++] = "-o";
622 1.12 mycroft argv[argc++] = p;
623 1.1 cgd }
624 1.44 ross }
625 1.35 mycroft }
626 1.12 mycroft
627 1.12 mycroft *argcp = argc;
628 1.35 mycroft *argvp = argv;
629 1.35 mycroft *maxargcp = maxargc;
630 1.48 abs }
631 1.48 abs
632 1.53 enami /* Deduce the filesystem type from the disk label. */
633 1.53 enami static const char *
634 1.48 abs getfslab(str)
635 1.48 abs const char *str;
636 1.48 abs {
637 1.48 abs struct disklabel dl;
638 1.48 abs int fd;
639 1.48 abs int part;
640 1.48 abs const char *vfstype;
641 1.48 abs u_char fstype;
642 1.53 enami char buf[MAXPATHLEN + 1];
643 1.51 abs char *sp, *ep;
644 1.48 abs
645 1.51 abs if ((fd = open(str, O_RDONLY)) == -1) {
646 1.51 abs /*
647 1.51 abs * Iff we get EBUSY try the raw device. Since mount always uses
648 1.51 abs * the block device we know we are never passed a raw device.
649 1.51 abs */
650 1.51 abs if (errno != EBUSY)
651 1.51 abs err(1, "cannot open `%s'", str);
652 1.51 abs strlcpy(buf, str, MAXPATHLEN);
653 1.51 abs if ((sp = strrchr(buf, '/')) != NULL)
654 1.51 abs ++sp;
655 1.51 abs else
656 1.51 abs sp = buf;
657 1.53 enami for (ep = sp + strlen(sp) + 1; ep > sp; ep--)
658 1.53 enami *ep = *(ep - 1);
659 1.51 abs *sp = 'r';
660 1.51 abs
661 1.51 abs /* Silently fail here - mount call can display error */
662 1.51 abs if ((fd = open(buf, O_RDONLY)) == -1)
663 1.53 enami return (NULL);
664 1.51 abs }
665 1.49 abs
666 1.51 abs if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
667 1.54 enami (void) close(fd);
668 1.53 enami return (NULL);
669 1.49 abs }
670 1.48 abs
671 1.48 abs (void) close(fd);
672 1.48 abs
673 1.48 abs part = str[strlen(str) - 1] - 'a';
674 1.48 abs
675 1.48 abs if (part < 0 || part >= dl.d_npartitions)
676 1.59 nathanw return (NULL);
677 1.48 abs
678 1.48 abs /* Return NULL for unknown types - caller can fall back to ffs */
679 1.48 abs if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
680 1.48 abs vfstype = NULL;
681 1.48 abs else
682 1.48 abs vfstype = mountnames[fstype];
683 1.48 abs
684 1.53 enami return (vfstype);
685 1.1 cgd }
686 1.1 cgd
687 1.52 jdolecek static void
688 1.12 mycroft usage()
689 1.1 cgd {
690 1.1 cgd
691 1.12 mycroft (void)fprintf(stderr,
692 1.53 enami "usage: mount %s\n mount %s\n mount %s\n",
693 1.57 soren "[-Aadfruvw] [-t type]",
694 1.57 soren "[-dfruvw] special | node",
695 1.57 soren "[-dfruvw] [-o options] [-t type] special node");
696 1.12 mycroft exit(1);
697 1.40 cgd /* NOTREACHED */
698 1.1 cgd }
699