xref: /freebsd/sbin/fsck/fsck.c (revision 32e86a82f54826f14ea381affa6674db3aa3b5ae)
16cf357bcSUlrich Spörlein /*	$NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $	*/
2da7e7114SAdrian Chadd 
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
6da7e7114SAdrian Chadd  * Copyright (c) 1996 Christos Zoulas. All rights reserved.
7da7e7114SAdrian Chadd  * Copyright (c) 1980, 1989, 1993, 1994
8da7e7114SAdrian Chadd  *	The Regents of the University of California.  All rights reserved.
9da7e7114SAdrian Chadd  *
10da7e7114SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
11da7e7114SAdrian Chadd  * modification, are permitted provided that the following conditions
12da7e7114SAdrian Chadd  * are met:
13da7e7114SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
14da7e7114SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
15da7e7114SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
16da7e7114SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
17da7e7114SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
186cf357bcSUlrich Spörlein  * 3. Neither the name of the University nor the names of its contributors
19da7e7114SAdrian Chadd  *    may be used to endorse or promote products derived from this software
20da7e7114SAdrian Chadd  *    without specific prior written permission.
21da7e7114SAdrian Chadd  *
22da7e7114SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23da7e7114SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24da7e7114SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25da7e7114SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26da7e7114SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27da7e7114SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28da7e7114SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29da7e7114SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30da7e7114SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31da7e7114SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32da7e7114SAdrian Chadd  * SUCH DAMAGE.
33d4b552a9SDavid E. O'Brien  * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
346cf357bcSUlrich Spörlein  * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $
35da7e7114SAdrian Chadd  */
36da7e7114SAdrian Chadd 
37da7e7114SAdrian Chadd #include <sys/param.h>
38da7e7114SAdrian Chadd #include <sys/mount.h>
39da7e7114SAdrian Chadd #include <sys/queue.h>
40da7e7114SAdrian Chadd #include <sys/wait.h>
41009ad003SWarner Losh #include <sys/disk.h>
42da7e7114SAdrian Chadd #include <sys/ioctl.h>
43da7e7114SAdrian Chadd 
44b813a714SMark Murray #include <ctype.h>
45da7e7114SAdrian Chadd #include <err.h>
46da7e7114SAdrian Chadd #include <fstab.h>
47da7e7114SAdrian Chadd #include <fcntl.h>
48*906c312bSKirk McKusick #include <mntopts.h>
49da7e7114SAdrian Chadd #include <paths.h>
50da7e7114SAdrian Chadd #include <signal.h>
51da7e7114SAdrian Chadd #include <stdio.h>
52da7e7114SAdrian Chadd #include <stdlib.h>
53da7e7114SAdrian Chadd #include <string.h>
54da7e7114SAdrian Chadd #include <unistd.h>
55da7e7114SAdrian Chadd 
56da7e7114SAdrian Chadd #include "fsutil.h"
57da7e7114SAdrian Chadd 
58da7e7114SAdrian Chadd static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
59da7e7114SAdrian Chadd 
TAILQ_HEAD(fstypelist,entry)601efe3c6bSEd Schouten static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
61da7e7114SAdrian Chadd 
62da7e7114SAdrian Chadd struct entry {
63da7e7114SAdrian Chadd 	char *type;
64da7e7114SAdrian Chadd 	char *options;
65da7e7114SAdrian Chadd 	TAILQ_ENTRY(entry) entries;
66da7e7114SAdrian Chadd };
67da7e7114SAdrian Chadd 
68da7e7114SAdrian Chadd static char *options = NULL;
69da7e7114SAdrian Chadd static int flags = 0;
70a02a0079SKirk McKusick static int forceflag = 0;
71da7e7114SAdrian Chadd 
723bbc4438SUlrich Spörlein static int checkfs(const char *, const char *, const char *, const char *, pid_t *);
73b70cd7eeSWarner Losh static int selected(const char *);
74b70cd7eeSWarner Losh static void addoption(char *);
75b70cd7eeSWarner Losh static const char *getoptions(const char *);
76b70cd7eeSWarner Losh static void addentry(struct fstypelist *, const char *, const char *);
77b70cd7eeSWarner Losh static void maketypelist(char *);
78b70cd7eeSWarner Losh static void catopt(char **, const char *);
793bbc4438SUlrich Spörlein static void mangle(char *, int *, const char ** volatile *, int *);
80009ad003SWarner Losh static const char *getfstype(const char *);
81b70cd7eeSWarner Losh static void usage(void) __dead2;
82b70cd7eeSWarner Losh static int isok(struct fstab *);
83da7e7114SAdrian Chadd 
84009ad003SWarner Losh static struct {
85009ad003SWarner Losh 	const char *ptype;
86009ad003SWarner Losh 	const char *name;
87009ad003SWarner Losh } ptype_map[] = {
88009ad003SWarner Losh 	{ "ufs",	"ffs" },
89009ad003SWarner Losh 	{ "ffs",	"ffs" },
90009ad003SWarner Losh 	{ "fat",	"msdosfs" },
91009ad003SWarner Losh 	{ "efi",	"msdosfs" },
92009ad003SWarner Losh 	{ NULL,		NULL },
93009ad003SWarner Losh };
94009ad003SWarner Losh 
95da7e7114SAdrian Chadd int
main(int argc,char * argv[])96b70cd7eeSWarner Losh main(int argc, char *argv[])
97da7e7114SAdrian Chadd {
98da7e7114SAdrian Chadd 	struct fstab *fs;
99da7e7114SAdrian Chadd 	int i, rval = 0;
100da7e7114SAdrian Chadd 	const char *vfstype = NULL;
101da7e7114SAdrian Chadd 	char globopt[3];
102f2104fc0SMaxim Sobolev 	const char *etc_fstab;
103da7e7114SAdrian Chadd 
104da7e7114SAdrian Chadd 	globopt[0] = '-';
105da7e7114SAdrian Chadd 	globopt[2] = '\0';
106da7e7114SAdrian Chadd 
107da7e7114SAdrian Chadd 	TAILQ_INIT(&selhead);
108da7e7114SAdrian Chadd 	TAILQ_INIT(&opthead);
109da7e7114SAdrian Chadd 
110f2104fc0SMaxim Sobolev 	etc_fstab = NULL;
111f2104fc0SMaxim Sobolev 	while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
112da7e7114SAdrian Chadd 		switch (i) {
113a02a0079SKirk McKusick 		case 'B':
114a02a0079SKirk McKusick 			if (flags & CHECK_BACKGRD)
115a02a0079SKirk McKusick 				errx(1, "Cannot specify -B and -F.");
116a02a0079SKirk McKusick 			flags |= DO_BACKGRD;
117a02a0079SKirk McKusick 			break;
118a02a0079SKirk McKusick 
119da7e7114SAdrian Chadd 		case 'd':
120da7e7114SAdrian Chadd 			flags |= CHECK_DEBUG;
121da7e7114SAdrian Chadd 			break;
122da7e7114SAdrian Chadd 
123da7e7114SAdrian Chadd 		case 'v':
124da7e7114SAdrian Chadd 			flags |= CHECK_VERBOSE;
125da7e7114SAdrian Chadd 			break;
126da7e7114SAdrian Chadd 
127a02a0079SKirk McKusick 		case 'F':
128a02a0079SKirk McKusick 			if (flags & DO_BACKGRD)
129a02a0079SKirk McKusick 				errx(1, "Cannot specify -B and -F.");
130a02a0079SKirk McKusick 			flags |= CHECK_BACKGRD;
131a02a0079SKirk McKusick 			break;
132a02a0079SKirk McKusick 
133da7e7114SAdrian Chadd 		case 'p':
134da7e7114SAdrian Chadd 			flags |= CHECK_PREEN;
135da7e7114SAdrian Chadd 			/*FALLTHROUGH*/
136111a5220SDavid E. O'Brien 		case 'C':
137111a5220SDavid E. O'Brien 			flags |= CHECK_CLEAN;
138111a5220SDavid E. O'Brien 			/*FALLTHROUGH*/
139da7e7114SAdrian Chadd 		case 'n':
140da7e7114SAdrian Chadd 		case 'y':
141da7e7114SAdrian Chadd 			globopt[1] = i;
142da7e7114SAdrian Chadd 			catopt(&options, globopt);
143da7e7114SAdrian Chadd 			break;
144da7e7114SAdrian Chadd 
145a02a0079SKirk McKusick 		case 'f':
146a02a0079SKirk McKusick 			forceflag = 1;
147a02a0079SKirk McKusick 			globopt[1] = i;
148a02a0079SKirk McKusick 			catopt(&options, globopt);
149a02a0079SKirk McKusick 			break;
150a02a0079SKirk McKusick 
151da7e7114SAdrian Chadd 		case 'l':
1520af7bca2SPoul-Henning Kamp 			warnx("Ignoring obsolete -l option\n");
153da7e7114SAdrian Chadd 			break;
154da7e7114SAdrian Chadd 
155da7e7114SAdrian Chadd 		case 'T':
156da7e7114SAdrian Chadd 			if (*optarg)
157da7e7114SAdrian Chadd 				addoption(optarg);
158da7e7114SAdrian Chadd 			break;
159da7e7114SAdrian Chadd 
160da7e7114SAdrian Chadd 		case 't':
16132ff2662SPoul-Henning Kamp 			if (!TAILQ_EMPTY(&selhead))
162da7e7114SAdrian Chadd 				errx(1, "only one -t option may be specified.");
163da7e7114SAdrian Chadd 
164da7e7114SAdrian Chadd 			maketypelist(optarg);
165da7e7114SAdrian Chadd 			vfstype = optarg;
166da7e7114SAdrian Chadd 			break;
167da7e7114SAdrian Chadd 
168f2104fc0SMaxim Sobolev 		case 'c':
169f2104fc0SMaxim Sobolev 			etc_fstab = optarg;
170f2104fc0SMaxim Sobolev 			break;
171f2104fc0SMaxim Sobolev 
172da7e7114SAdrian Chadd 		case '?':
173da7e7114SAdrian Chadd 		default:
174da7e7114SAdrian Chadd 			usage();
175da7e7114SAdrian Chadd 			/* NOTREACHED */
176da7e7114SAdrian Chadd 		}
177da7e7114SAdrian Chadd 
178da7e7114SAdrian Chadd 	argc -= optind;
179da7e7114SAdrian Chadd 	argv += optind;
180da7e7114SAdrian Chadd 
181f2104fc0SMaxim Sobolev 	if (etc_fstab != NULL)
182f2104fc0SMaxim Sobolev 		setfstab(etc_fstab);
183f2104fc0SMaxim Sobolev 
184da7e7114SAdrian Chadd 	if (argc == 0)
1850af7bca2SPoul-Henning Kamp 		return checkfstab(flags, isok, checkfs);
186da7e7114SAdrian Chadd 
187da7e7114SAdrian Chadd #define	BADTYPE(type)							\
188da7e7114SAdrian Chadd 	(strcmp(type, FSTAB_RO) &&					\
189da7e7114SAdrian Chadd 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
190da7e7114SAdrian Chadd 
191da7e7114SAdrian Chadd 
192da7e7114SAdrian Chadd 	for (; argc--; argv++) {
193a02a0079SKirk McKusick 		const char *spec, *mntpt, *type, *cp;
194da7e7114SAdrian Chadd 		char device[MAXPATHLEN];
195a02a0079SKirk McKusick 		struct statfs *mntp;
196da7e7114SAdrian Chadd 
1973bbc4438SUlrich Spörlein 		mntpt = NULL;
198da7e7114SAdrian Chadd 		spec = *argv;
199da7e7114SAdrian Chadd 		cp = strrchr(spec, '/');
200cd5f6a0cSMarcelo Araujo 		if (cp == NULL) {
201da7e7114SAdrian Chadd 			(void)snprintf(device, sizeof(device), "%s%s",
202da7e7114SAdrian Chadd 				_PATH_DEV, spec);
203da7e7114SAdrian Chadd 			spec = device;
204da7e7114SAdrian Chadd 		}
205*906c312bSKirk McKusick 		mntp = getmntpoint(spec);
206a02a0079SKirk McKusick 		if (mntp != NULL) {
207a02a0079SKirk McKusick 			spec = mntp->f_mntfromname;
208a02a0079SKirk McKusick 			mntpt = mntp->f_mntonname;
209a02a0079SKirk McKusick 		}
210da7e7114SAdrian Chadd 		if ((fs = getfsfile(spec)) == NULL &&
211da7e7114SAdrian Chadd 		    (fs = getfsspec(spec)) == NULL) {
212da7e7114SAdrian Chadd 			if (vfstype == NULL)
213009ad003SWarner Losh 				vfstype = getfstype(spec);
2145c63c8ddSPoul-Henning Kamp 			if (vfstype == NULL)
2156a27a9f6SEdward Tomasz Napierala 				vfstype = "ufs";
216da7e7114SAdrian Chadd 			type = vfstype;
217a02a0079SKirk McKusick 			devcheck(spec);
218a02a0079SKirk McKusick 		} else {
219da7e7114SAdrian Chadd 			spec = fs->fs_spec;
220da7e7114SAdrian Chadd 			type = fs->fs_vfstype;
221a02a0079SKirk McKusick 			mntpt = fs->fs_file;
222da7e7114SAdrian Chadd 			if (BADTYPE(fs->fs_type))
223da7e7114SAdrian Chadd 				errx(1, "%s has unknown file system type.",
224da7e7114SAdrian Chadd 				    spec);
225da7e7114SAdrian Chadd 		}
226a02a0079SKirk McKusick 		if ((flags & CHECK_BACKGRD) &&
227a02a0079SKirk McKusick 		    checkfs(type, spec, mntpt, "-F", NULL) == 0) {
228a02a0079SKirk McKusick 			printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
229a02a0079SKirk McKusick 			continue;
230a02a0079SKirk McKusick 		}
231a02a0079SKirk McKusick 		if ((flags & DO_BACKGRD) && forceflag == 0 &&
232a02a0079SKirk McKusick 		    checkfs(type, spec, mntpt, "-F", NULL) != 0)
233a02a0079SKirk McKusick 			continue;
234da7e7114SAdrian Chadd 
235a02a0079SKirk McKusick 		rval |= checkfs(type, spec, mntpt, NULL, NULL);
236da7e7114SAdrian Chadd 	}
237da7e7114SAdrian Chadd 
238da7e7114SAdrian Chadd 	return rval;
239da7e7114SAdrian Chadd }
240da7e7114SAdrian Chadd 
241da7e7114SAdrian Chadd 
242a02a0079SKirk McKusick static int
isok(struct fstab * fs)243b70cd7eeSWarner Losh isok(struct fstab *fs)
244da7e7114SAdrian Chadd {
245a02a0079SKirk McKusick 	int i;
246a02a0079SKirk McKusick 
247da7e7114SAdrian Chadd 	if (fs->fs_passno == 0)
248a02a0079SKirk McKusick 		return (0);
249da7e7114SAdrian Chadd 	if (BADTYPE(fs->fs_type))
250a02a0079SKirk McKusick 		return (0);
251da7e7114SAdrian Chadd 	if (!selected(fs->fs_vfstype))
252a02a0079SKirk McKusick 		return (0);
253c72372c6SKirk McKusick 	/* If failok, always check now */
254c72372c6SKirk McKusick 	if (getfsopt(fs, "failok"))
255c72372c6SKirk McKusick 		return (1);
256a02a0079SKirk McKusick 	/*
257a02a0079SKirk McKusick 	 * If the -B flag has been given, then process the needed
258a02a0079SKirk McKusick 	 * background checks. Background checks cannot be run on
259a02a0079SKirk McKusick 	 * file systems that will be mounted read-only or that were
260a02a0079SKirk McKusick 	 * not mounted at boot time (typically those marked `noauto').
261a02a0079SKirk McKusick 	 * If these basic tests are passed, check with the file system
262a02a0079SKirk McKusick 	 * itself to see if it is willing to do background checking
263a02a0079SKirk McKusick 	 * by invoking its check program with the -F flag.
264a02a0079SKirk McKusick 	 */
265a02a0079SKirk McKusick 	if (flags & DO_BACKGRD) {
266a02a0079SKirk McKusick 		if (!strcmp(fs->fs_type, FSTAB_RO))
267a02a0079SKirk McKusick 			return (0);
268*906c312bSKirk McKusick 		if (getmntpoint(fs->fs_spec) == NULL)
269a02a0079SKirk McKusick 			return (0);
270a02a0079SKirk McKusick 		if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
271a02a0079SKirk McKusick 			return (0);
272a02a0079SKirk McKusick 		return (1);
273a02a0079SKirk McKusick 	}
274a02a0079SKirk McKusick 	/*
275a02a0079SKirk McKusick 	 * If the -F flag has been given, then consider deferring the
276a02a0079SKirk McKusick 	 * check to background. Background checks cannot be run on
277a02a0079SKirk McKusick 	 * file systems that will be mounted read-only or that will
278a02a0079SKirk McKusick 	 * not be mounted at boot time (e.g., marked `noauto'). If
279a02a0079SKirk McKusick 	 * these basic tests are passed, check with the file system
280a02a0079SKirk McKusick 	 * itself to see if it is willing to defer to background
281a02a0079SKirk McKusick 	 * checking by invoking its check program with the -F flag.
282a02a0079SKirk McKusick 	 */
283a02a0079SKirk McKusick 	if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
284a02a0079SKirk McKusick 		return (1);
285a02a0079SKirk McKusick 	for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
286a02a0079SKirk McKusick 		if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
287a02a0079SKirk McKusick 			break;
288a02a0079SKirk McKusick 	if (i >= 0)
289a02a0079SKirk McKusick 		return (1);
290a02a0079SKirk McKusick 	if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
291a02a0079SKirk McKusick 		return (1);
292a02a0079SKirk McKusick 	printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
293a02a0079SKirk McKusick 	return (0);
294da7e7114SAdrian Chadd }
295da7e7114SAdrian Chadd 
296da7e7114SAdrian Chadd 
297da7e7114SAdrian Chadd static int
checkfs(const char * pvfstype,const char * spec,const char * mntpt,const char * auxopt,pid_t * pidp)2988235d79aSJuli Mallett checkfs(const char *pvfstype, const char *spec, const char *mntpt,
2993bbc4438SUlrich Spörlein     const char *auxopt, pid_t *pidp)
300da7e7114SAdrian Chadd {
3013bbc4438SUlrich Spörlein 	const char ** volatile argv;
302da7e7114SAdrian Chadd 	pid_t pid;
303da7e7114SAdrian Chadd 	int argc, i, status, maxargc;
304a3ba4c65SGordon Tetlow 	char *optbuf, execbase[MAXPATHLEN];
3058235d79aSJuli Mallett 	char *vfstype = NULL;
306da7e7114SAdrian Chadd 	const char *extra = NULL;
307da7e7114SAdrian Chadd 
308da7e7114SAdrian Chadd #ifdef __GNUC__
309da7e7114SAdrian Chadd 	/* Avoid vfork clobbering */
310da7e7114SAdrian Chadd 	(void) &optbuf;
311da7e7114SAdrian Chadd 	(void) &vfstype;
312da7e7114SAdrian Chadd #endif
3138235d79aSJuli Mallett 	/*
3148235d79aSJuli Mallett 	 * We convert the vfstype to lowercase and any spaces to underscores
3158235d79aSJuli Mallett 	 * to not confuse the issue
3168235d79aSJuli Mallett 	 *
3178235d79aSJuli Mallett 	 * XXX This is a kludge to make automatic filesystem type guessing
3188235d79aSJuli Mallett 	 * from the disklabel work for "4.2BSD" filesystems.  It does a
3198235d79aSJuli Mallett 	 * very limited subset of transliteration to a normalised form of
3208235d79aSJuli Mallett 	 * filesystem name, and we do not seem to enforce a filesystem
3218235d79aSJuli Mallett 	 * name character set.
3228235d79aSJuli Mallett 	 */
3238235d79aSJuli Mallett 	vfstype = strdup(pvfstype);
3248235d79aSJuli Mallett 	if (vfstype == NULL)
3256cf357bcSUlrich Spörlein 		perr("strdup(pvfstype)");
3263bbc4438SUlrich Spörlein 	for (i = 0; i < (int)strlen(vfstype); i++) {
3278235d79aSJuli Mallett 		vfstype[i] = tolower(vfstype[i]);
3288235d79aSJuli Mallett 		if (vfstype[i] == ' ')
3298235d79aSJuli Mallett 			vfstype[i] = '_';
3308235d79aSJuli Mallett 	}
331da7e7114SAdrian Chadd 
332da7e7114SAdrian Chadd 	extra = getoptions(vfstype);
333da7e7114SAdrian Chadd 	optbuf = NULL;
334da7e7114SAdrian Chadd 	if (options)
335da7e7114SAdrian Chadd 		catopt(&optbuf, options);
336da7e7114SAdrian Chadd 	if (extra)
337da7e7114SAdrian Chadd 		catopt(&optbuf, extra);
338a02a0079SKirk McKusick 	if (auxopt)
339a02a0079SKirk McKusick 		catopt(&optbuf, auxopt);
340a02a0079SKirk McKusick 	else if (flags & DO_BACKGRD)
341a02a0079SKirk McKusick 		catopt(&optbuf, "-B");
342da7e7114SAdrian Chadd 
343da7e7114SAdrian Chadd 	maxargc = 64;
344da7e7114SAdrian Chadd 	argv = emalloc(sizeof(char *) * maxargc);
345da7e7114SAdrian Chadd 
346da7e7114SAdrian Chadd 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
347da7e7114SAdrian Chadd 	argc = 0;
348da7e7114SAdrian Chadd 	argv[argc++] = execbase;
349da7e7114SAdrian Chadd 	if (optbuf)
350da7e7114SAdrian Chadd 		mangle(optbuf, &argc, &argv, &maxargc);
351da7e7114SAdrian Chadd 	argv[argc++] = spec;
352da7e7114SAdrian Chadd 	argv[argc] = NULL;
353da7e7114SAdrian Chadd 
354da7e7114SAdrian Chadd 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
355da7e7114SAdrian Chadd 		(void)printf("start %s %swait", mntpt,
356da7e7114SAdrian Chadd 			pidp ? "no" : "");
357da7e7114SAdrian Chadd 		for (i = 0; i < argc; i++)
358da7e7114SAdrian Chadd 			(void)printf(" %s", argv[i]);
359da7e7114SAdrian Chadd 		(void)printf("\n");
360da7e7114SAdrian Chadd 	}
361da7e7114SAdrian Chadd 
362da7e7114SAdrian Chadd 	switch (pid = vfork()) {
363da7e7114SAdrian Chadd 	case -1:				/* Error. */
364da7e7114SAdrian Chadd 		warn("vfork");
365da7e7114SAdrian Chadd 		if (optbuf)
366da7e7114SAdrian Chadd 			free(optbuf);
3678235d79aSJuli Mallett 		free(vfstype);
368da7e7114SAdrian Chadd 		return (1);
369da7e7114SAdrian Chadd 
370da7e7114SAdrian Chadd 	case 0:					/* Child. */
371a02a0079SKirk McKusick 		if ((flags & CHECK_DEBUG) && auxopt == NULL)
372da7e7114SAdrian Chadd 			_exit(0);
373da7e7114SAdrian Chadd 
374da7e7114SAdrian Chadd 		/* Go find an executable. */
3753bbc4438SUlrich Spörlein 		execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv));
376da7e7114SAdrian Chadd 		if (spec)
377a3ba4c65SGordon Tetlow 			warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
378da7e7114SAdrian Chadd 		else
379a3ba4c65SGordon Tetlow 			warn("exec %s in %s", execbase, _PATH_SYSPATH);
380da7e7114SAdrian Chadd 		_exit(1);
381da7e7114SAdrian Chadd 		/* NOTREACHED */
382da7e7114SAdrian Chadd 
383da7e7114SAdrian Chadd 	default:				/* Parent. */
384da7e7114SAdrian Chadd 		if (optbuf)
385da7e7114SAdrian Chadd 			free(optbuf);
386da7e7114SAdrian Chadd 
3878235d79aSJuli Mallett 		free(vfstype);
3888235d79aSJuli Mallett 
389da7e7114SAdrian Chadd 		if (pidp) {
390da7e7114SAdrian Chadd 			*pidp = pid;
391da7e7114SAdrian Chadd 			return 0;
392da7e7114SAdrian Chadd 		}
393da7e7114SAdrian Chadd 
394da7e7114SAdrian Chadd 		if (waitpid(pid, &status, 0) < 0) {
395da7e7114SAdrian Chadd 			warn("waitpid");
396da7e7114SAdrian Chadd 			return (1);
397da7e7114SAdrian Chadd 		}
398da7e7114SAdrian Chadd 
399da7e7114SAdrian Chadd 		if (WIFEXITED(status)) {
400da7e7114SAdrian Chadd 			if (WEXITSTATUS(status) != 0)
401da7e7114SAdrian Chadd 				return (WEXITSTATUS(status));
402da7e7114SAdrian Chadd 		}
403da7e7114SAdrian Chadd 		else if (WIFSIGNALED(status)) {
404da7e7114SAdrian Chadd 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
405da7e7114SAdrian Chadd 			return (1);
406da7e7114SAdrian Chadd 		}
407da7e7114SAdrian Chadd 		break;
408da7e7114SAdrian Chadd 	}
409da7e7114SAdrian Chadd 
410da7e7114SAdrian Chadd 	return (0);
411da7e7114SAdrian Chadd }
412da7e7114SAdrian Chadd 
413da7e7114SAdrian Chadd 
414da7e7114SAdrian Chadd static int
selected(const char * type)415b70cd7eeSWarner Losh selected(const char *type)
416da7e7114SAdrian Chadd {
417da7e7114SAdrian Chadd 	struct entry *e;
418da7e7114SAdrian Chadd 
419da7e7114SAdrian Chadd 	/* If no type specified, it's always selected. */
42032ff2662SPoul-Henning Kamp 	TAILQ_FOREACH(e, &selhead, entries)
421da7e7114SAdrian Chadd 		if (!strncmp(e->type, type, MFSNAMELEN))
422da7e7114SAdrian Chadd 			return which == IN_LIST ? 1 : 0;
423da7e7114SAdrian Chadd 
424da7e7114SAdrian Chadd 	return which == IN_LIST ? 0 : 1;
425da7e7114SAdrian Chadd }
426da7e7114SAdrian Chadd 
427da7e7114SAdrian Chadd 
428da7e7114SAdrian Chadd static const char *
getoptions(const char * type)429b70cd7eeSWarner Losh getoptions(const char *type)
430da7e7114SAdrian Chadd {
431da7e7114SAdrian Chadd 	struct entry *e;
432da7e7114SAdrian Chadd 
43332ff2662SPoul-Henning Kamp 	TAILQ_FOREACH(e, &opthead, entries)
434da7e7114SAdrian Chadd 		if (!strncmp(e->type, type, MFSNAMELEN))
435da7e7114SAdrian Chadd 			return e->options;
436da7e7114SAdrian Chadd 	return "";
437da7e7114SAdrian Chadd }
438da7e7114SAdrian Chadd 
439da7e7114SAdrian Chadd 
440da7e7114SAdrian Chadd static void
addoption(char * optstr)441b70cd7eeSWarner Losh addoption(char *optstr)
442da7e7114SAdrian Chadd {
443da7e7114SAdrian Chadd 	char *newoptions;
444da7e7114SAdrian Chadd 	struct entry *e;
445da7e7114SAdrian Chadd 
446da7e7114SAdrian Chadd 	if ((newoptions = strchr(optstr, ':')) == NULL)
447da7e7114SAdrian Chadd 		errx(1, "Invalid option string");
448da7e7114SAdrian Chadd 
449da7e7114SAdrian Chadd 	*newoptions++ = '\0';
450da7e7114SAdrian Chadd 
45132ff2662SPoul-Henning Kamp 	TAILQ_FOREACH(e, &opthead, entries)
452da7e7114SAdrian Chadd 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
453da7e7114SAdrian Chadd 			catopt(&e->options, newoptions);
454da7e7114SAdrian Chadd 			return;
455da7e7114SAdrian Chadd 		}
456da7e7114SAdrian Chadd 	addentry(&opthead, optstr, newoptions);
457da7e7114SAdrian Chadd }
458da7e7114SAdrian Chadd 
459da7e7114SAdrian Chadd 
460da7e7114SAdrian Chadd static void
addentry(struct fstypelist * list,const char * type,const char * opts)461b70cd7eeSWarner Losh addentry(struct fstypelist *list, const char *type, const char *opts)
462da7e7114SAdrian Chadd {
463da7e7114SAdrian Chadd 	struct entry *e;
464da7e7114SAdrian Chadd 
465da7e7114SAdrian Chadd 	e = emalloc(sizeof(struct entry));
466da7e7114SAdrian Chadd 	e->type = estrdup(type);
467da7e7114SAdrian Chadd 	e->options = estrdup(opts);
468da7e7114SAdrian Chadd 	TAILQ_INSERT_TAIL(list, e, entries);
469da7e7114SAdrian Chadd }
470da7e7114SAdrian Chadd 
471da7e7114SAdrian Chadd 
472da7e7114SAdrian Chadd static void
maketypelist(char * fslist)473b70cd7eeSWarner Losh maketypelist(char *fslist)
474da7e7114SAdrian Chadd {
475da7e7114SAdrian Chadd 	char *ptr;
476da7e7114SAdrian Chadd 
477da7e7114SAdrian Chadd 	if ((fslist == NULL) || (fslist[0] == '\0'))
478da7e7114SAdrian Chadd 		errx(1, "empty type list");
479da7e7114SAdrian Chadd 
480da7e7114SAdrian Chadd 	if (fslist[0] == 'n' && fslist[1] == 'o') {
481da7e7114SAdrian Chadd 		fslist += 2;
482da7e7114SAdrian Chadd 		which = NOT_IN_LIST;
483da7e7114SAdrian Chadd 	}
484da7e7114SAdrian Chadd 	else
485da7e7114SAdrian Chadd 		which = IN_LIST;
486da7e7114SAdrian Chadd 
487da7e7114SAdrian Chadd 	while ((ptr = strsep(&fslist, ",")) != NULL)
488da7e7114SAdrian Chadd 		addentry(&selhead, ptr, "");
489da7e7114SAdrian Chadd 
490da7e7114SAdrian Chadd }
491da7e7114SAdrian Chadd 
492da7e7114SAdrian Chadd 
493da7e7114SAdrian Chadd static void
catopt(char ** sp,const char * o)494b70cd7eeSWarner Losh catopt(char **sp, const char *o)
495da7e7114SAdrian Chadd {
496da7e7114SAdrian Chadd 	char *s;
497da7e7114SAdrian Chadd 	size_t i, j;
498da7e7114SAdrian Chadd 
499da7e7114SAdrian Chadd 	s = *sp;
500da7e7114SAdrian Chadd 	if (s) {
501da7e7114SAdrian Chadd 		i = strlen(s);
502da7e7114SAdrian Chadd 		j = i + 1 + strlen(o) + 1;
503da7e7114SAdrian Chadd 		s = erealloc(s, j);
504da7e7114SAdrian Chadd 		(void)snprintf(s + i, j, ",%s", o);
505da7e7114SAdrian Chadd 	} else
506da7e7114SAdrian Chadd 		s = estrdup(o);
507da7e7114SAdrian Chadd 	*sp = s;
508da7e7114SAdrian Chadd }
509da7e7114SAdrian Chadd 
510da7e7114SAdrian Chadd 
511da7e7114SAdrian Chadd static void
mangle(char * opts,int * argcp,const char ** volatile * argvp,int * maxargcp)5123bbc4438SUlrich Spörlein mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
513da7e7114SAdrian Chadd {
514da7e7114SAdrian Chadd 	char *p, *s;
515da7e7114SAdrian Chadd 	int argc, maxargc;
516da7e7114SAdrian Chadd 	const char **argv;
517da7e7114SAdrian Chadd 
518da7e7114SAdrian Chadd 	argc = *argcp;
519da7e7114SAdrian Chadd 	argv = *argvp;
520da7e7114SAdrian Chadd 	maxargc = *maxargcp;
521da7e7114SAdrian Chadd 
5226cf357bcSUlrich Spörlein 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
523da7e7114SAdrian Chadd 		/* Always leave space for one more argument and the NULL. */
524da7e7114SAdrian Chadd 		if (argc >= maxargc - 3) {
525da7e7114SAdrian Chadd 			maxargc <<= 1;
526da7e7114SAdrian Chadd 			argv = erealloc(argv, maxargc * sizeof(char *));
527da7e7114SAdrian Chadd 		}
528da7e7114SAdrian Chadd 		if (*p != '\0')  {
529da7e7114SAdrian Chadd 			if (*p == '-') {
530da7e7114SAdrian Chadd 				argv[argc++] = p;
531da7e7114SAdrian Chadd 				p = strchr(p, '=');
532da7e7114SAdrian Chadd 				if (p) {
533da7e7114SAdrian Chadd 					*p = '\0';
534da7e7114SAdrian Chadd 					argv[argc++] = p+1;
535da7e7114SAdrian Chadd 				}
536da7e7114SAdrian Chadd 			} else {
537da7e7114SAdrian Chadd 				argv[argc++] = "-o";
538da7e7114SAdrian Chadd 				argv[argc++] = p;
539da7e7114SAdrian Chadd 			}
540da7e7114SAdrian Chadd 		}
541da7e7114SAdrian Chadd 	}
542da7e7114SAdrian Chadd 
543da7e7114SAdrian Chadd 	*argcp = argc;
544da7e7114SAdrian Chadd 	*argvp = argv;
545da7e7114SAdrian Chadd 	*maxargcp = maxargc;
546da7e7114SAdrian Chadd }
547da7e7114SAdrian Chadd 
548adafc9d6SWarner Losh static const char *
getfstype(const char * str)549009ad003SWarner Losh getfstype(const char *str)
550adafc9d6SWarner Losh {
551009ad003SWarner Losh 	struct diocgattr_arg attr;
552009ad003SWarner Losh 	int fd, i;
553adafc9d6SWarner Losh 
554da7e7114SAdrian Chadd 	if ((fd = open(str, O_RDONLY)) == -1)
555da7e7114SAdrian Chadd 		err(1, "cannot open `%s'", str);
556da7e7114SAdrian Chadd 
557009ad003SWarner Losh 	strncpy(attr.name, "PART::type", sizeof(attr.name));
558009ad003SWarner Losh 	memset(&attr.value, 0, sizeof(attr.value));
559009ad003SWarner Losh 	attr.len = sizeof(attr.value);
560009ad003SWarner Losh 	if (ioctl(fd, DIOCGATTR, &attr) == -1) {
5614b2d15efSAlexander Leidinger 		(void) close(fd);
5625c63c8ddSPoul-Henning Kamp 		return(NULL);
5634b2d15efSAlexander Leidinger 	}
564da7e7114SAdrian Chadd 	(void) close(fd);
565009ad003SWarner Losh 	for (i = 0; ptype_map[i].ptype != NULL; i++)
566009ad003SWarner Losh 		if (strstr(attr.value.str, ptype_map[i].ptype) != NULL)
567009ad003SWarner Losh 			return (ptype_map[i].name);
568009ad003SWarner Losh 	return (NULL);
569da7e7114SAdrian Chadd }
570da7e7114SAdrian Chadd 
571da7e7114SAdrian Chadd 
572da7e7114SAdrian Chadd static void
usage(void)573b70cd7eeSWarner Losh usage(void)
574da7e7114SAdrian Chadd {
575da7e7114SAdrian Chadd 	static const char common[] =
576f2104fc0SMaxim Sobolev 	    "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
577da7e7114SAdrian Chadd 
578d3974088SDag-Erling Smørgrav 	(void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
579b813a714SMark Murray 	    getprogname(), common);
580da7e7114SAdrian Chadd 	exit(1);
581da7e7114SAdrian Chadd }
582