fsck.c revision 1.25
1/*	$OpenBSD: fsck.c,v 1.25 2007/10/20 18:08:57 sobrado Exp $	*/
2/*	$NetBSD: fsck.c,v 1.7 1996/10/03 20:06:30 christos Exp $	*/
3
4/*
5 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
6 * Copyright (c) 1980, 1989, 1993, 1994
7 *	The Regents of the University of California.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. 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 * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
34 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
35 *
36 */
37
38static const char rcsid[] = "$OpenBSD: fsck.c,v 1.25 2007/10/20 18:08:57 sobrado Exp $";
39
40#include <sys/param.h>
41#include <sys/mount.h>
42#include <sys/queue.h>
43#include <sys/resource.h>
44#include <sys/wait.h>
45
46#include <err.h>
47#include <errno.h>
48#include <fstab.h>
49#include <signal.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54#include <util.h>
55
56#include "pathnames.h"
57#include "fsutil.h"
58
59static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
60
61TAILQ_HEAD(fstypelist, entry) opthead, selhead;
62
63struct entry {
64	char *type;
65	char *options;
66	TAILQ_ENTRY(entry) entries;
67};
68
69static int maxrun = 0;
70static char *options = NULL;
71static int flags = 0;
72
73int main(int, char *[]);
74
75static int checkfs(const char *, const char *, const char *, void *, pid_t *);
76static int selected(const char *);
77static void addoption(char *);
78static const char *getoptions(const char *);
79static void addentry(struct fstypelist *, const char *, const char *);
80static void maketypelist(char *);
81static char *catopt(char *, const char *, int);
82static void mangle(char *, int *, const char ***, int *);
83static void usage(void);
84static void *isok(struct fstab *);
85
86
87int
88main(int argc, char *argv[])
89{
90	struct fstab *fs;
91	int i, rval = 0;
92	char *vfstype = NULL;
93	char *p, globopt[3];
94	struct rlimit rl;
95
96	/* Increase our data size to the max */
97	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
98		if (geteuid() == 0)
99			rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
100		else
101			rl.rlim_cur = rl.rlim_max;
102		if (setrlimit(RLIMIT_DATA, &rl) < 0)
103			warn("Can't get resource limit to max data size");
104	} else
105		warn("Can't get resource limit for data size");
106
107	globopt[0] = '-';
108	globopt[2] = '\0';
109
110	TAILQ_INIT(&selhead);
111	TAILQ_INIT(&opthead);
112
113	while ((i = getopt(argc, argv, "dvpfnyb:l:T:t:")) != -1)
114		switch (i) {
115		case 'd':
116			flags |= CHECK_DEBUG;
117			break;
118
119		case 'v':
120			flags |= CHECK_VERBOSE;
121			break;
122
123		case 'p':
124			flags |= CHECK_PREEN;
125			/*FALLTHROUGH*/
126		case 'n':
127		case 'f':
128		case 'y':
129			globopt[1] = i;
130			options = catopt(options, globopt, 1);
131			break;
132
133		case 'b':
134			if (asprintf(&p, "-b %s", optarg) == -1)
135				err(1, "malloc failed");
136			options = catopt(options, p, 1);
137			free(p);
138			break;
139
140		case 'l':
141			maxrun = atoi(optarg);
142			break;
143
144		case 'T':
145			if (*optarg)
146				addoption(optarg);
147			break;
148
149		case 't':
150			if (!TAILQ_EMPTY(&selhead))
151				errx(1, "only one -t option may be specified.");
152
153			maketypelist(optarg);
154			vfstype = optarg;
155			break;
156
157		case '?':
158		default:
159			usage();
160			/* NOTREACHED */
161		}
162
163	argc -= optind;
164	argv += optind;
165
166	if (argc == 0)
167		return checkfstab(flags, maxrun, isok, checkfs);
168
169#define	BADTYPE(type)							\
170	(strcmp(type, FSTAB_RO) &&					\
171	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
172
173
174	for (; argc--; argv++) {
175		char *spec, *type;
176
177		if (strncmp(*argv, "/dev/", 5) == 0 &&
178		    (type = readlabelfs(*argv, 0))) {
179			spec = *argv;
180		} else if ((fs = getfsfile(*argv)) == NULL &&
181		    (fs = getfsspec(*argv)) == NULL) {
182			if (vfstype == NULL)
183				errx(1,
184				    "%s: unknown special file or file system.",
185				    *argv);
186			spec = *argv;
187			type = vfstype;
188		} else {
189			spec = fs->fs_spec;
190			type = fs->fs_vfstype;
191			if (BADTYPE(fs->fs_type))
192				errx(1, "%s has unknown file system type.",
193				    *argv);
194		}
195
196		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
197	}
198
199	return rval;
200}
201
202
203static void *
204isok(struct fstab *fs)
205{
206	if (fs->fs_passno == 0)
207		return NULL;
208
209	if (BADTYPE(fs->fs_type))
210		return NULL;
211
212	if (!selected(fs->fs_vfstype))
213		return NULL;
214
215	return fs;
216}
217
218
219static int
220checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
221    pid_t *pidp)
222{
223	/* List of directories containing fsck_xxx subcommands. */
224	static const char *edirs[] = {
225		_PATH_SBIN,
226		_PATH_USRSBIN,
227		NULL
228	};
229	const char **argv, **edir;
230	pid_t pid;
231	int argc, i, status, maxargc;
232	char *optbuf = NULL, fsname[MAXPATHLEN], execname[MAXPATHLEN];
233	const char *extra = getoptions(vfstype);
234
235	if (strcmp(vfstype, "ufs") == 0)
236		vfstype = MOUNT_UFS;
237
238	maxargc = 100;
239	argv = emalloc(sizeof(char *) * maxargc);
240
241	argc = 0;
242	(void)snprintf(fsname, sizeof(fsname), "fsck_%s", vfstype);
243	argv[argc++] = fsname;
244
245	if (options) {
246		if (extra != NULL)
247			optbuf = catopt(options, extra, 0);
248		else
249			optbuf = estrdup(options);
250	}
251	else if (extra)
252		optbuf = estrdup(extra);
253
254	if (optbuf)
255		mangle(optbuf, &argc, &argv, &maxargc);
256
257	argv[argc++] = spec;
258	argv[argc] = NULL;
259
260	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
261		(void)printf("start %s %swait %s", mntpt,
262			pidp ? "no" : "", fsname);
263		for (i = 1; i < argc; i++)
264			(void)printf(" %s", argv[i]);
265		(void)printf("\n");
266	}
267
268	switch (pid = fork()) {
269	case -1:				/* Error. */
270		warn("fork");
271		if (optbuf)
272			free(optbuf);
273		free(argv);
274		return (1);
275
276	case 0:					/* Child. */
277		if (flags & CHECK_DEBUG)
278			_exit(0);
279
280		/* Go find an executable. */
281		edir = edirs;
282		do {
283			(void)snprintf(execname,
284			    sizeof(execname), "%s/fsck_%s", *edir, vfstype);
285			execv(execname, (char * const *)argv);
286			if (errno != ENOENT) {
287				if (spec)
288					warn("exec %s for %s", execname, spec);
289				else
290					warn("exec %s", execname);
291			}
292		} while (*++edir != NULL);
293
294		if (errno == ENOENT) {
295			if (spec)
296				warn("exec %s for %s", execname, spec);
297			else
298				warn("exec %s", execname);
299		}
300		exit(1);
301		/* NOTREACHED */
302
303	default:				/* Parent. */
304		if (optbuf)
305			free(optbuf);
306		free(argv);
307
308		if (pidp) {
309			*pidp = pid;
310			return 0;
311		}
312
313		if (waitpid(pid, &status, 0) < 0) {
314			warn("waitpid");
315			return (1);
316		}
317
318		if (WIFEXITED(status)) {
319			if (WEXITSTATUS(status) != 0)
320				return (WEXITSTATUS(status));
321		}
322		else if (WIFSIGNALED(status)) {
323			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
324			return (1);
325		}
326		break;
327	}
328
329	return (0);
330}
331
332
333static int
334selected(const char *type)
335{
336	struct entry *e;
337
338	/* If no type specified, it's always selected. */
339	TAILQ_FOREACH(e, &selhead, entries)
340		if (!strncmp(e->type, type, MFSNAMELEN))
341			return which == IN_LIST ? 1 : 0;
342
343	return which == IN_LIST ? 0 : 1;
344}
345
346
347static const char *
348getoptions(const char *type)
349{
350	struct entry *e;
351
352	TAILQ_FOREACH(e, &opthead, entries)
353		if (!strncmp(e->type, type, MFSNAMELEN))
354			return e->options;
355	return "";
356}
357
358
359static void
360addoption(char *optstr)
361{
362	char *newoptions;
363	struct entry *e;
364
365	if ((newoptions = strchr(optstr, ':')) == NULL)
366		errx(1, "Invalid option string");
367
368	*newoptions++ = '\0';
369
370	TAILQ_FOREACH(e, &opthead, entries)
371		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
372			e->options = catopt(e->options, newoptions, 1);
373			return;
374		}
375	addentry(&opthead, optstr, newoptions);
376}
377
378
379static void
380addentry(struct fstypelist *list, const char *type, const char *opts)
381{
382	struct entry *e;
383
384	e = emalloc(sizeof(struct entry));
385	e->type = estrdup(type);
386	e->options = estrdup(opts);
387	TAILQ_INSERT_TAIL(list, e, entries);
388}
389
390
391static void
392maketypelist(char *fslist)
393{
394	char *ptr;
395
396	if ((fslist == NULL) || (fslist[0] == '\0'))
397		errx(1, "empty type list");
398
399	if (fslist[0] == 'n' && fslist[1] == 'o') {
400		fslist += 2;
401		which = NOT_IN_LIST;
402	}
403	else
404		which = IN_LIST;
405
406	while ((ptr = strsep(&fslist, ",")) != NULL)
407		addentry(&selhead, ptr, "");
408
409}
410
411
412static char *
413catopt(char *s0, const char *s1, int fr)
414{
415	char *cp;
416
417	if (s0 && *s0) {
418		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
419			err(1, "malloc failed");
420	} else
421		cp = estrdup(s1);
422
423	if (s0 && fr)
424		free(s0);
425	return (cp);
426}
427
428
429static void
430mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
431{
432	char *p, *s;
433	int argc = *argcp, maxargc = *maxargcp;
434	const char **argv = *argvp;
435
436	argc = *argcp;
437	maxargc = *maxargcp;
438
439	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
440		/* always leave space for one more argument and the NULL */
441		if (argc >= maxargc - 3) {
442			int newmaxargc = maxargc + 50;
443
444			argv = erealloc(argv, newmaxargc * sizeof(char *));
445			maxargc = newmaxargc;
446		}
447		if (*p != '\0') {
448			if (*p == '-') {
449				argv[argc++] = p;
450				p = strchr(p, '=');
451				if (p) {
452					*p = '\0';
453					argv[argc++] = p+1;
454				}
455			}
456			else {
457				argv[argc++] = "-o";
458				argv[argc++] = p;
459			}
460		}
461	}
462
463	*argcp = argc;
464	*argvp = argv;
465	*maxargcp = maxargc;
466}
467
468
469static void
470usage(void)
471{
472	extern char *__progname;
473
474	fprintf(stderr, "usage: %s "
475	    "[-dfnpvy] [-l maxparallel] [-T fstype:fsoptions]\n"
476	    "            [-t fstype] [special | node ...]\n", __progname);
477	exit(1);
478}
479