Deleted Added
full compact
mount.c (36772) mount.c (37425)
1/*-
2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
1/*-
2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
43#else
44static const char rcsid[] =
45 "$Id: mount.c,v 1.26 1998/05/17 21:57:17 dt Exp $";
46#endif
43#endif
44static const char rcsid[] =
45 "$Id$";
47#endif /* not lint */
48
49#include <sys/param.h>
50#include <sys/mount.h>
51#include <sys/stat.h>
52#include <sys/wait.h>
53
54#include <err.h>
55#include <errno.h>
56#include <fstab.h>
57#include <pwd.h>
58#include <signal.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <unistd.h>
63
64#include "extern.h"
65#include "pathnames.h"
66
67int debug, fstab_style, verbose;
68
69char *catopt __P((char *, const char *));
70struct statfs
71 *getmntpt __P((const char *));
72int hasopt __P((const char *, const char *));
73int ismounted __P((struct fstab *, struct statfs *, int));
74int isremountable __P((const char *));
75void mangle __P((char *, int *, const char **));
76int mountfs __P((const char *, const char *, const char *,
77 int, const char *, const char *));
78void prmount __P((struct statfs *));
79void putfsent __P((const struct statfs *));
80void usage __P((void));
81
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/mount.h>
50#include <sys/stat.h>
51#include <sys/wait.h>
52
53#include <err.h>
54#include <errno.h>
55#include <fstab.h>
56#include <pwd.h>
57#include <signal.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63#include "extern.h"
64#include "pathnames.h"
65
66int debug, fstab_style, verbose;
67
68char *catopt __P((char *, const char *));
69struct statfs
70 *getmntpt __P((const char *));
71int hasopt __P((const char *, const char *));
72int ismounted __P((struct fstab *, struct statfs *, int));
73int isremountable __P((const char *));
74void mangle __P((char *, int *, const char **));
75int mountfs __P((const char *, const char *, const char *,
76 int, const char *, const char *));
77void prmount __P((struct statfs *));
78void putfsent __P((const struct statfs *));
79void usage __P((void));
80
82/* Map from mount otions to printable formats. */
81/* Map from mount options to printable formats. */
83static struct opt {
84 int o_opt;
85 const char *o_name;
86} optnames[] = {
87 { MNT_ASYNC, "asynchronous" },
88 { MNT_EXPORTED, "NFS exported" },
89 { MNT_LOCAL, "local" },
90 { MNT_NOATIME, "noatime" },
91 { MNT_NODEV, "nodev" },
92 { MNT_NOEXEC, "noexec" },
93 { MNT_NOSUID, "nosuid" },
94 { MNT_NOSYMFOLLOW, "nosymfollow" },
95 { MNT_QUOTA, "with quotas" },
96 { MNT_RDONLY, "read-only" },
97 { MNT_SYNCHRONOUS, "synchronous" },
98 { MNT_UNION, "union" },
99 { MNT_NOCLUSTERR, "noclusterr" },
100 { MNT_NOCLUSTERW, "noclusterw" },
101 { MNT_SUIDDIR, "suiddir" },
102 { MNT_SOFTDEP, "soft-updates" },
103 { NULL }
104};
105
106/*
107 * List of VFS types that can be remounted without becoming mounted on top
108 * of each other.
109 * XXX Is this list correct?
110 */
111static const char *
112remountable_fs_names[] = {
113 "ufs", "ffs", "lfs", "ext2fs",
114 0
115};
116
117int
118main(argc, argv)
119 int argc;
120 char * const argv[];
121{
122 const char *mntfromname, **vfslist, *vfstype;
123 struct fstab *fs;
124 struct statfs *mntbuf;
125 FILE *mountdfp;
126 pid_t pid;
127 int all, ch, i, init_flags, mntsize, rval;
128 char *options;
129
130 all = init_flags = 0;
131 options = NULL;
132 vfslist = NULL;
133 vfstype = "ufs";
134 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
135 switch (ch) {
136 case 'a':
137 all = 1;
138 break;
139 case 'd':
140 debug = 1;
141 break;
142 case 'f':
143 init_flags |= MNT_FORCE;
144 break;
145 case 'o':
146 if (*optarg)
147 options = catopt(options, optarg);
148 break;
149 case 'p':
150 fstab_style = 1;
151 verbose = 1;
152 break;
153 case 'r':
154 init_flags |= MNT_RDONLY;
155 break;
156 case 't':
157 if (vfslist != NULL)
82static struct opt {
83 int o_opt;
84 const char *o_name;
85} optnames[] = {
86 { MNT_ASYNC, "asynchronous" },
87 { MNT_EXPORTED, "NFS exported" },
88 { MNT_LOCAL, "local" },
89 { MNT_NOATIME, "noatime" },
90 { MNT_NODEV, "nodev" },
91 { MNT_NOEXEC, "noexec" },
92 { MNT_NOSUID, "nosuid" },
93 { MNT_NOSYMFOLLOW, "nosymfollow" },
94 { MNT_QUOTA, "with quotas" },
95 { MNT_RDONLY, "read-only" },
96 { MNT_SYNCHRONOUS, "synchronous" },
97 { MNT_UNION, "union" },
98 { MNT_NOCLUSTERR, "noclusterr" },
99 { MNT_NOCLUSTERW, "noclusterw" },
100 { MNT_SUIDDIR, "suiddir" },
101 { MNT_SOFTDEP, "soft-updates" },
102 { NULL }
103};
104
105/*
106 * List of VFS types that can be remounted without becoming mounted on top
107 * of each other.
108 * XXX Is this list correct?
109 */
110static const char *
111remountable_fs_names[] = {
112 "ufs", "ffs", "lfs", "ext2fs",
113 0
114};
115
116int
117main(argc, argv)
118 int argc;
119 char * const argv[];
120{
121 const char *mntfromname, **vfslist, *vfstype;
122 struct fstab *fs;
123 struct statfs *mntbuf;
124 FILE *mountdfp;
125 pid_t pid;
126 int all, ch, i, init_flags, mntsize, rval;
127 char *options;
128
129 all = init_flags = 0;
130 options = NULL;
131 vfslist = NULL;
132 vfstype = "ufs";
133 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
134 switch (ch) {
135 case 'a':
136 all = 1;
137 break;
138 case 'd':
139 debug = 1;
140 break;
141 case 'f':
142 init_flags |= MNT_FORCE;
143 break;
144 case 'o':
145 if (*optarg)
146 options = catopt(options, optarg);
147 break;
148 case 'p':
149 fstab_style = 1;
150 verbose = 1;
151 break;
152 case 'r':
153 init_flags |= MNT_RDONLY;
154 break;
155 case 't':
156 if (vfslist != NULL)
158 errx(1, "only one -t option may be specified.");
157 errx(1, "only one -t option may be specified");
159 vfslist = makevfslist(optarg);
160 vfstype = optarg;
161 break;
162 case 'u':
163 init_flags |= MNT_UPDATE;
164 break;
165 case 'v':
166 verbose = 1;
167 break;
168 case 'w':
169 init_flags &= ~MNT_RDONLY;
170 break;
171 case '?':
172 default:
173 usage();
174 /* NOTREACHED */
175 }
176 argc -= optind;
177 argv += optind;
178
179#define BADTYPE(type) \
180 (strcmp(type, FSTAB_RO) && \
181 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
182
183 rval = 0;
184 switch (argc) {
185 case 0:
186 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
187 err(1, "getmntinfo");
188 if (all) {
189 while ((fs = getfsent()) != NULL) {
190 if (BADTYPE(fs->fs_type))
191 continue;
192 if (checkvfsname(fs->fs_vfstype, vfslist))
193 continue;
194 if (hasopt(fs->fs_mntops, "noauto"))
195 continue;
196 if (ismounted(fs, mntbuf, mntsize))
197 continue;
198 if (mountfs(fs->fs_vfstype, fs->fs_spec,
199 fs->fs_file, init_flags, options,
200 fs->fs_mntops))
201 rval = 1;
202 }
203 } else if (fstab_style) {
204 for (i = 0; i < mntsize; i++) {
205 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
206 continue;
207 putfsent(&mntbuf[i]);
208 }
209 } else {
210 for (i = 0; i < mntsize; i++) {
211 if (checkvfsname(mntbuf[i].f_fstypename,
212 vfslist))
213 continue;
214 prmount(&mntbuf[i]);
215 }
216 }
217 exit(rval);
218 case 1:
219 if (vfslist != NULL)
220 usage();
221
222 if (init_flags & MNT_UPDATE) {
223 if ((mntbuf = getmntpt(*argv)) == NULL)
224 errx(1,
158 vfslist = makevfslist(optarg);
159 vfstype = optarg;
160 break;
161 case 'u':
162 init_flags |= MNT_UPDATE;
163 break;
164 case 'v':
165 verbose = 1;
166 break;
167 case 'w':
168 init_flags &= ~MNT_RDONLY;
169 break;
170 case '?':
171 default:
172 usage();
173 /* NOTREACHED */
174 }
175 argc -= optind;
176 argv += optind;
177
178#define BADTYPE(type) \
179 (strcmp(type, FSTAB_RO) && \
180 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
181
182 rval = 0;
183 switch (argc) {
184 case 0:
185 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
186 err(1, "getmntinfo");
187 if (all) {
188 while ((fs = getfsent()) != NULL) {
189 if (BADTYPE(fs->fs_type))
190 continue;
191 if (checkvfsname(fs->fs_vfstype, vfslist))
192 continue;
193 if (hasopt(fs->fs_mntops, "noauto"))
194 continue;
195 if (ismounted(fs, mntbuf, mntsize))
196 continue;
197 if (mountfs(fs->fs_vfstype, fs->fs_spec,
198 fs->fs_file, init_flags, options,
199 fs->fs_mntops))
200 rval = 1;
201 }
202 } else if (fstab_style) {
203 for (i = 0; i < mntsize; i++) {
204 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
205 continue;
206 putfsent(&mntbuf[i]);
207 }
208 } else {
209 for (i = 0; i < mntsize; i++) {
210 if (checkvfsname(mntbuf[i].f_fstypename,
211 vfslist))
212 continue;
213 prmount(&mntbuf[i]);
214 }
215 }
216 exit(rval);
217 case 1:
218 if (vfslist != NULL)
219 usage();
220
221 if (init_flags & MNT_UPDATE) {
222 if ((mntbuf = getmntpt(*argv)) == NULL)
223 errx(1,
225 "unknown special file or file system %s.",
224 "unknown special file or file system %s",
226 *argv);
227 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL)
228 mntfromname = fs->fs_spec;
229 else
230 mntfromname = mntbuf->f_mntfromname;
231 rval = mountfs(mntbuf->f_fstypename, mntfromname,
232 mntbuf->f_mntonname, init_flags, options, 0);
233 break;
234 }
235 if ((fs = getfsfile(*argv)) == NULL &&
236 (fs = getfsspec(*argv)) == NULL)
225 *argv);
226 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL)
227 mntfromname = fs->fs_spec;
228 else
229 mntfromname = mntbuf->f_mntfromname;
230 rval = mountfs(mntbuf->f_fstypename, mntfromname,
231 mntbuf->f_mntonname, init_flags, options, 0);
232 break;
233 }
234 if ((fs = getfsfile(*argv)) == NULL &&
235 (fs = getfsspec(*argv)) == NULL)
237 errx(1, "%s: unknown special file or file system.",
236 errx(1, "%s: unknown special file or file system",
238 *argv);
239 if (BADTYPE(fs->fs_type))
237 *argv);
238 if (BADTYPE(fs->fs_type))
240 errx(1, "%s has unknown file system type.",
239 errx(1, "%s has unknown file system type",
241 *argv);
242 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
243 init_flags, options, fs->fs_mntops);
244 break;
245 case 2:
246 /*
247 * If -t flag has not been specified, and spec contains either
248 * a ':' or a '@' then assume that an NFS filesystem is being
249 * specified ala Sun.
250 */
251 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
252 vfstype = "nfs";
253 rval = mountfs(vfstype,
254 argv[0], argv[1], init_flags, options, NULL);
255 break;
256 default:
257 usage();
258 /* NOTREACHED */
259 }
260
261 /*
262 * If the mount was successfully, and done by root, tell mountd the
263 * good news. Pid checks are probably unnecessary, but don't hurt.
264 */
265 if (rval == 0 && getuid() == 0 &&
266 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
267 if (fscanf(mountdfp, "%d", &pid) == 1 &&
268 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
269 err(1, "signal mountd");
270 (void)fclose(mountdfp);
271 }
272
273 exit(rval);
274}
275
276int
277ismounted(fs, mntbuf, mntsize)
278 struct fstab *fs;
279 struct statfs *mntbuf;
280 int mntsize;
281{
282 int i;
283
284 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
285 /* the root file system can always be remounted */
286 return (0);
287
288 for (i = mntsize - 1; i >= 0; --i)
289 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
290 (!isremountable(fs->fs_vfstype) ||
291 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
292 return (1);
293 return (0);
294}
295
296int
297isremountable(vfsname)
298 const char *vfsname;
299{
300 const char **cp;
301
302 for (cp = remountable_fs_names; *cp; cp++)
303 if (strcmp(*cp, vfsname) == 0)
304 return (1);
305 return (0);
306}
307
308int
309hasopt(mntopts, option)
310 const char *mntopts, *option;
311{
312 int negative, found;
313 char *opt, *optbuf;
314
315 if (option[0] == 'n' && option[1] == 'o') {
316 negative = 1;
317 option += 2;
318 } else
319 negative = 0;
320 optbuf = strdup(mntopts);
321 found = 0;
322 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
323 if (opt[0] == 'n' && opt[1] == 'o') {
324 if (!strcasecmp(opt + 2, option))
325 found = negative;
326 } else if (!strcasecmp(opt, option))
327 found = !negative;
328 }
329 free(optbuf);
330 return (found);
331}
332
333int
334mountfs(vfstype, spec, name, flags, options, mntopts)
335 const char *vfstype, *spec, *name, *options, *mntopts;
336 int flags;
337{
338 /* List of directories containing mount_xxx subcommands. */
339 static const char *edirs[] = {
340 _PATH_SBIN,
341 _PATH_USRSBIN,
342 NULL
343 };
344 const char *argv[100], **edir;
345 struct stat sb;
346 struct statfs sf;
347 pid_t pid;
348 int argc, i, status;
349 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
350
351#if __GNUC__
352 (void)&optbuf;
353 (void)&name;
354#endif
355
356 if (realpath(name, mntpath) != NULL && stat(mntpath, &sb) == 0) {
357 if (!S_ISDIR(sb.st_mode)) {
240 *argv);
241 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
242 init_flags, options, fs->fs_mntops);
243 break;
244 case 2:
245 /*
246 * If -t flag has not been specified, and spec contains either
247 * a ':' or a '@' then assume that an NFS filesystem is being
248 * specified ala Sun.
249 */
250 if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
251 vfstype = "nfs";
252 rval = mountfs(vfstype,
253 argv[0], argv[1], init_flags, options, NULL);
254 break;
255 default:
256 usage();
257 /* NOTREACHED */
258 }
259
260 /*
261 * If the mount was successfully, and done by root, tell mountd the
262 * good news. Pid checks are probably unnecessary, but don't hurt.
263 */
264 if (rval == 0 && getuid() == 0 &&
265 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
266 if (fscanf(mountdfp, "%d", &pid) == 1 &&
267 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
268 err(1, "signal mountd");
269 (void)fclose(mountdfp);
270 }
271
272 exit(rval);
273}
274
275int
276ismounted(fs, mntbuf, mntsize)
277 struct fstab *fs;
278 struct statfs *mntbuf;
279 int mntsize;
280{
281 int i;
282
283 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
284 /* the root file system can always be remounted */
285 return (0);
286
287 for (i = mntsize - 1; i >= 0; --i)
288 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
289 (!isremountable(fs->fs_vfstype) ||
290 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
291 return (1);
292 return (0);
293}
294
295int
296isremountable(vfsname)
297 const char *vfsname;
298{
299 const char **cp;
300
301 for (cp = remountable_fs_names; *cp; cp++)
302 if (strcmp(*cp, vfsname) == 0)
303 return (1);
304 return (0);
305}
306
307int
308hasopt(mntopts, option)
309 const char *mntopts, *option;
310{
311 int negative, found;
312 char *opt, *optbuf;
313
314 if (option[0] == 'n' && option[1] == 'o') {
315 negative = 1;
316 option += 2;
317 } else
318 negative = 0;
319 optbuf = strdup(mntopts);
320 found = 0;
321 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
322 if (opt[0] == 'n' && opt[1] == 'o') {
323 if (!strcasecmp(opt + 2, option))
324 found = negative;
325 } else if (!strcasecmp(opt, option))
326 found = !negative;
327 }
328 free(optbuf);
329 return (found);
330}
331
332int
333mountfs(vfstype, spec, name, flags, options, mntopts)
334 const char *vfstype, *spec, *name, *options, *mntopts;
335 int flags;
336{
337 /* List of directories containing mount_xxx subcommands. */
338 static const char *edirs[] = {
339 _PATH_SBIN,
340 _PATH_USRSBIN,
341 NULL
342 };
343 const char *argv[100], **edir;
344 struct stat sb;
345 struct statfs sf;
346 pid_t pid;
347 int argc, i, status;
348 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
349
350#if __GNUC__
351 (void)&optbuf;
352 (void)&name;
353#endif
354
355 if (realpath(name, mntpath) != NULL && stat(mntpath, &sb) == 0) {
356 if (!S_ISDIR(sb.st_mode)) {
358 warnx("%s: Not a directory", mntpath);
357 warnx("%s: not a directory", mntpath);
359 return (1);
360 }
361 } else {
362 warn("%s", mntpath);
363 return (1);
364 }
365
366 name = mntpath;
367
368 if (mntopts == NULL)
369 mntopts = "";
370 if (options == NULL) {
371 if (*mntopts == '\0') {
372 options = "rw";
373 } else {
374 options = mntopts;
375 mntopts = "";
376 }
377 }
378 optbuf = catopt(strdup(mntopts), options);
379
380 if (strcmp(name, "/") == 0)
381 flags |= MNT_UPDATE;
382 if (flags & MNT_FORCE)
383 optbuf = catopt(optbuf, "force");
384 if (flags & MNT_RDONLY)
385 optbuf = catopt(optbuf, "ro");
386 /*
387 * XXX
388 * The mount_mfs (newfs) command uses -o to select the
358 return (1);
359 }
360 } else {
361 warn("%s", mntpath);
362 return (1);
363 }
364
365 name = mntpath;
366
367 if (mntopts == NULL)
368 mntopts = "";
369 if (options == NULL) {
370 if (*mntopts == '\0') {
371 options = "rw";
372 } else {
373 options = mntopts;
374 mntopts = "";
375 }
376 }
377 optbuf = catopt(strdup(mntopts), options);
378
379 if (strcmp(name, "/") == 0)
380 flags |= MNT_UPDATE;
381 if (flags & MNT_FORCE)
382 optbuf = catopt(optbuf, "force");
383 if (flags & MNT_RDONLY)
384 optbuf = catopt(optbuf, "ro");
385 /*
386 * XXX
387 * The mount_mfs (newfs) command uses -o to select the
389 * optimisation mode. We don't pass the default "-o rw"
388 * optimization mode. We don't pass the default "-o rw"
390 * for that reason.
391 */
392 if (flags & MNT_UPDATE)
393 optbuf = catopt(optbuf, "update");
394
395 argc = 0;
396 argv[argc++] = vfstype;
397 mangle(optbuf, &argc, argv);
398 argv[argc++] = spec;
399 argv[argc++] = name;
400 argv[argc] = NULL;
401
402 if (debug) {
403 (void)printf("exec: mount_%s", vfstype);
404 for (i = 1; i < argc; i++)
405 (void)printf(" %s", argv[i]);
406 (void)printf("\n");
407 return (0);
408 }
409
410 switch (pid = fork()) {
411 case -1: /* Error. */
412 warn("fork");
413 free(optbuf);
414 return (1);
415 case 0: /* Child. */
416 if (strcmp(vfstype, "ufs") == 0)
417 exit(mount_ufs(argc, (char * const *) argv));
418
419 /* Go find an executable. */
420 for (edir = edirs; *edir; edir++) {
421 (void)snprintf(execname,
422 sizeof(execname), "%s/mount_%s", *edir, vfstype);
423 execv(execname, (char * const *)argv);
424 }
425 if (errno == ENOENT) {
426 int len = 0;
427 char *cp;
428 for (edir = edirs; *edir; edir++)
429 len += strlen(*edir) + 2; /* ", " */
389 * for that reason.
390 */
391 if (flags & MNT_UPDATE)
392 optbuf = catopt(optbuf, "update");
393
394 argc = 0;
395 argv[argc++] = vfstype;
396 mangle(optbuf, &argc, argv);
397 argv[argc++] = spec;
398 argv[argc++] = name;
399 argv[argc] = NULL;
400
401 if (debug) {
402 (void)printf("exec: mount_%s", vfstype);
403 for (i = 1; i < argc; i++)
404 (void)printf(" %s", argv[i]);
405 (void)printf("\n");
406 return (0);
407 }
408
409 switch (pid = fork()) {
410 case -1: /* Error. */
411 warn("fork");
412 free(optbuf);
413 return (1);
414 case 0: /* Child. */
415 if (strcmp(vfstype, "ufs") == 0)
416 exit(mount_ufs(argc, (char * const *) argv));
417
418 /* Go find an executable. */
419 for (edir = edirs; *edir; edir++) {
420 (void)snprintf(execname,
421 sizeof(execname), "%s/mount_%s", *edir, vfstype);
422 execv(execname, (char * const *)argv);
423 }
424 if (errno == ENOENT) {
425 int len = 0;
426 char *cp;
427 for (edir = edirs; *edir; edir++)
428 len += strlen(*edir) + 2; /* ", " */
430 if ((cp = malloc(len)) == NULL) {
431 warn(NULL);
432 exit(1);
433 }
429 if ((cp = malloc(len)) == NULL)
430 errx(1, "malloc failed");
434 cp[0] = '\0';
435 for (edir = edirs; *edir; edir++) {
436 strcat(cp, *edir);
437 if (edir[1] != NULL)
438 strcat(cp, ", ");
439 }
440 warn("exec mount_%s not found in %s", vfstype, cp);
441 }
442 exit(1);
443 /* NOTREACHED */
444 default: /* Parent. */
445 free(optbuf);
446
447 if (waitpid(pid, &status, 0) < 0) {
448 warn("waitpid");
449 return (1);
450 }
451
452 if (WIFEXITED(status)) {
453 if (WEXITSTATUS(status) != 0)
454 return (WEXITSTATUS(status));
455 } else if (WIFSIGNALED(status)) {
456 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
457 return (1);
458 }
459
460 if (verbose) {
461 if (statfs(name, &sf) < 0) {
462 warn("statfs %s", name);
463 return (1);
464 }
465 if (fstab_style)
466 putfsent(&sf);
467 else
468 prmount(&sf);
469 }
470 break;
471 }
472
473 return (0);
474}
475
476void
477prmount(sfp)
478 struct statfs *sfp;
479{
480 int flags;
481 struct opt *o;
482 struct passwd *pw;
483 int f;
484
485 (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname);
486
487 flags = sfp->f_flags & MNT_VISFLAGMASK;
488 for (f = 0, o = optnames; flags && o->o_opt; o++)
489 if (flags & o->o_opt) {
490 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
491 flags &= ~o->o_opt;
492 }
493 if (sfp->f_owner) {
494 (void)printf("%smounted by ", !f++ ? " (" : ", ");
495 if ((pw = getpwuid(sfp->f_owner)) != NULL)
496 (void)printf("%s", pw->pw_name);
497 else
498 (void)printf("%d", sfp->f_owner);
499 }
500 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
501 (void)printf("%swrites: sync %ld async %ld",
502 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
503 (void)printf("%s\n", f ? ")" : "");
504}
505
506struct statfs *
507getmntpt(name)
508 const char *name;
509{
510 struct statfs *mntbuf;
511 int i, mntsize;
512
513 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
514 for (i = 0; i < mntsize; i++)
515 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
516 strcmp(mntbuf[i].f_mntonname, name) == 0)
517 return (&mntbuf[i]);
518 return (NULL);
519}
520
521char *
522catopt(s0, s1)
523 char *s0;
524 const char *s1;
525{
526 size_t i;
527 char *cp;
528
529 if (s0 && *s0) {
530 i = strlen(s0) + strlen(s1) + 1 + 1;
531 if ((cp = malloc(i)) == NULL)
431 cp[0] = '\0';
432 for (edir = edirs; *edir; edir++) {
433 strcat(cp, *edir);
434 if (edir[1] != NULL)
435 strcat(cp, ", ");
436 }
437 warn("exec mount_%s not found in %s", vfstype, cp);
438 }
439 exit(1);
440 /* NOTREACHED */
441 default: /* Parent. */
442 free(optbuf);
443
444 if (waitpid(pid, &status, 0) < 0) {
445 warn("waitpid");
446 return (1);
447 }
448
449 if (WIFEXITED(status)) {
450 if (WEXITSTATUS(status) != 0)
451 return (WEXITSTATUS(status));
452 } else if (WIFSIGNALED(status)) {
453 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
454 return (1);
455 }
456
457 if (verbose) {
458 if (statfs(name, &sf) < 0) {
459 warn("statfs %s", name);
460 return (1);
461 }
462 if (fstab_style)
463 putfsent(&sf);
464 else
465 prmount(&sf);
466 }
467 break;
468 }
469
470 return (0);
471}
472
473void
474prmount(sfp)
475 struct statfs *sfp;
476{
477 int flags;
478 struct opt *o;
479 struct passwd *pw;
480 int f;
481
482 (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname);
483
484 flags = sfp->f_flags & MNT_VISFLAGMASK;
485 for (f = 0, o = optnames; flags && o->o_opt; o++)
486 if (flags & o->o_opt) {
487 (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
488 flags &= ~o->o_opt;
489 }
490 if (sfp->f_owner) {
491 (void)printf("%smounted by ", !f++ ? " (" : ", ");
492 if ((pw = getpwuid(sfp->f_owner)) != NULL)
493 (void)printf("%s", pw->pw_name);
494 else
495 (void)printf("%d", sfp->f_owner);
496 }
497 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
498 (void)printf("%swrites: sync %ld async %ld",
499 !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
500 (void)printf("%s\n", f ? ")" : "");
501}
502
503struct statfs *
504getmntpt(name)
505 const char *name;
506{
507 struct statfs *mntbuf;
508 int i, mntsize;
509
510 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
511 for (i = 0; i < mntsize; i++)
512 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
513 strcmp(mntbuf[i].f_mntonname, name) == 0)
514 return (&mntbuf[i]);
515 return (NULL);
516}
517
518char *
519catopt(s0, s1)
520 char *s0;
521 const char *s1;
522{
523 size_t i;
524 char *cp;
525
526 if (s0 && *s0) {
527 i = strlen(s0) + strlen(s1) + 1 + 1;
528 if ((cp = malloc(i)) == NULL)
532 err(1, NULL);
529 errx(1, "malloc failed");
533 (void)snprintf(cp, i, "%s,%s", s0, s1);
534 } else
535 cp = strdup(s1);
536
537 if (s0)
538 free(s0);
539 return (cp);
540}
541
542void
543mangle(options, argcp, argv)
544 char *options;
545 int *argcp;
546 const char **argv;
547{
548 char *p, *s;
549 int argc;
550
551 argc = *argcp;
552 for (s = options; (p = strsep(&s, ",")) != NULL;)
553 if (*p != '\0')
554 if (*p == '-') {
555 argv[argc++] = p;
556 p = strchr(p, '=');
557 if (p) {
558 *p = '\0';
559 argv[argc++] = p+1;
560 }
561 } else if (strcmp(p, "rw") != 0) {
562 argv[argc++] = "-o";
563 argv[argc++] = p;
564 }
565
566 *argcp = argc;
567}
568
569void
570usage()
571{
572
530 (void)snprintf(cp, i, "%s,%s", s0, s1);
531 } else
532 cp = strdup(s1);
533
534 if (s0)
535 free(s0);
536 return (cp);
537}
538
539void
540mangle(options, argcp, argv)
541 char *options;
542 int *argcp;
543 const char **argv;
544{
545 char *p, *s;
546 int argc;
547
548 argc = *argcp;
549 for (s = options; (p = strsep(&s, ",")) != NULL;)
550 if (*p != '\0')
551 if (*p == '-') {
552 argv[argc++] = p;
553 p = strchr(p, '=');
554 if (p) {
555 *p = '\0';
556 argv[argc++] = p+1;
557 }
558 } else if (strcmp(p, "rw") != 0) {
559 argv[argc++] = "-o";
560 argv[argc++] = p;
561 }
562
563 *argcp = argc;
564}
565
566void
567usage()
568{
569
573 (void)fprintf(stderr,
574 "usage: mount %s %s\n mount %s\n mount %s\n",
575 "[-dfpruvw] [-o options] [-t ufs | external_type]",
576 "special node",
577 "[-adfpruvw] [-t ufs | external_type]",
578 "[-dfpruvw] special | node");
570 (void)fprintf(stderr, "%s\n%s\n%s\n",
571"usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
572" mount [-adfpruvw] [-t ufs | external_type]",
573" mount [-dfpruvw] special | node");
579 exit(1);
580}
581
582void
583putfsent(ent)
584 const struct statfs *ent;
585{
586 struct fstab *fst;
587
588 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
589 ent->f_fstypename, (ent->f_flags & MNT_RDONLY) ? "ro" : "rw");
590
591 /* XXX should use optnames[] - put shorter names in it. */
592 if (ent->f_flags & MNT_SYNCHRONOUS)
593 printf(",sync");
594 if (ent->f_flags & MNT_NOEXEC)
595 printf(",noexec");
596 if (ent->f_flags & MNT_NOSUID)
597 printf(",nosuid");
598 if (ent->f_flags & MNT_NODEV)
599 printf(",nodev");
600 if (ent->f_flags & MNT_UNION)
601 printf(",union");
602 if (ent->f_flags & MNT_ASYNC)
603 printf(",async");
604 if (ent->f_flags & MNT_NOATIME)
605 printf(",noatime");
606 if (ent->f_flags & MNT_NOCLUSTERR)
607 printf(",noclusterr");
608 if (ent->f_flags & MNT_NOCLUSTERW)
609 printf(",noclusterw");
610 if (ent->f_flags & MNT_NOSYMFOLLOW)
611 printf (",nosymfollow");
612 if (ent->f_flags & MNT_SUIDDIR)
613 printf(",suiddir");
614
615 if ((fst = getfsspec(ent->f_mntfromname)))
616 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
617 else if ((fst = getfsfile(ent->f_mntonname)))
618 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
619 else if (strcmp(ent->f_fstypename, "ufs") == 0)
620 printf("\t1 1\n");
621 else
622 printf("\t0 0\n");
623}
574 exit(1);
575}
576
577void
578putfsent(ent)
579 const struct statfs *ent;
580{
581 struct fstab *fst;
582
583 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
584 ent->f_fstypename, (ent->f_flags & MNT_RDONLY) ? "ro" : "rw");
585
586 /* XXX should use optnames[] - put shorter names in it. */
587 if (ent->f_flags & MNT_SYNCHRONOUS)
588 printf(",sync");
589 if (ent->f_flags & MNT_NOEXEC)
590 printf(",noexec");
591 if (ent->f_flags & MNT_NOSUID)
592 printf(",nosuid");
593 if (ent->f_flags & MNT_NODEV)
594 printf(",nodev");
595 if (ent->f_flags & MNT_UNION)
596 printf(",union");
597 if (ent->f_flags & MNT_ASYNC)
598 printf(",async");
599 if (ent->f_flags & MNT_NOATIME)
600 printf(",noatime");
601 if (ent->f_flags & MNT_NOCLUSTERR)
602 printf(",noclusterr");
603 if (ent->f_flags & MNT_NOCLUSTERW)
604 printf(",noclusterw");
605 if (ent->f_flags & MNT_NOSYMFOLLOW)
606 printf (",nosymfollow");
607 if (ent->f_flags & MNT_SUIDDIR)
608 printf(",suiddir");
609
610 if ((fst = getfsspec(ent->f_mntfromname)))
611 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
612 else if ((fst = getfsfile(ent->f_mntonname)))
613 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
614 else if (strcmp(ent->f_fstypename, "ufs") == 0)
615 printf("\t1 1\n");
616 else
617 printf("\t0 0\n");
618}