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