xref: /freebsd/sbin/fsck/fsck.c (revision 3047fefe49f57a673de8df152c199de12ec2c6d3)
1 /*	$NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Christos Zoulas. All rights reserved.
5  * Copyright (c) 1980, 1989, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
37  * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
38  * $NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs Exp $
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/queue.h>
47 #include <sys/wait.h>
48 #define FSTYPENAMES
49 #define DKTYPENAMES
50 #include <sys/disklabel.h>
51 #include <sys/ioctl.h>
52 
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <fstab.h>
57 #include <fcntl.h>
58 #include <paths.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 #include "pathnames.h"
66 #include "fsutil.h"
67 
68 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
69 
70 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
71 
72 struct entry {
73 	char *type;
74 	char *options;
75 	TAILQ_ENTRY(entry) entries;
76 };
77 
78 static char *options = NULL;
79 static int flags = 0;
80 static int forceflag = 0;
81 
82 static int checkfs(const char *, const char *, const char *, char *, pid_t *);
83 static int selected(const char *);
84 static void addoption(char *);
85 static const char *getoptions(const char *);
86 static void addentry(struct fstypelist *, const char *, const char *);
87 static void maketypelist(char *);
88 static void catopt(char **, const char *);
89 static void mangle(char *, int *, const char ***, int *);
90 static const char *getfslab(const char *);
91 static void usage(void) __dead2;
92 static int isok(struct fstab *);
93 
94 int
95 main(int argc, char *argv[])
96 {
97 	struct fstab *fs;
98 	int i, rval = 0;
99 	const char *vfstype = NULL;
100 	char globopt[3];
101 
102 	globopt[0] = '-';
103 	globopt[2] = '\0';
104 
105 	TAILQ_INIT(&selhead);
106 	TAILQ_INIT(&opthead);
107 
108 	while ((i = getopt(argc, argv, "BdvpfFnyl:t:T:")) != -1)
109 		switch (i) {
110 		case 'B':
111 			if (flags & CHECK_BACKGRD)
112 				errx(1, "Cannot specify -B and -F.");
113 			flags |= DO_BACKGRD;
114 			break;
115 
116 		case 'd':
117 			flags |= CHECK_DEBUG;
118 			break;
119 
120 		case 'v':
121 			flags |= CHECK_VERBOSE;
122 			break;
123 
124 		case 'F':
125 			if (flags & DO_BACKGRD)
126 				errx(1, "Cannot specify -B and -F.");
127 			flags |= CHECK_BACKGRD;
128 			break;
129 
130 		case 'p':
131 			flags |= CHECK_PREEN;
132 			/*FALLTHROUGH*/
133 		case 'n':
134 		case 'y':
135 			globopt[1] = i;
136 			catopt(&options, globopt);
137 			break;
138 
139 		case 'f':
140 			forceflag = 1;
141 			globopt[1] = i;
142 			catopt(&options, globopt);
143 			break;
144 
145 		case 'l':
146 			warnx("Ignoring obsolete -l option\n");
147 			break;
148 
149 		case 'T':
150 			if (*optarg)
151 				addoption(optarg);
152 			break;
153 
154 		case 't':
155 			if (!TAILQ_EMPTY(&selhead))
156 				errx(1, "only one -t option may be specified.");
157 
158 			maketypelist(optarg);
159 			vfstype = optarg;
160 			break;
161 
162 		case '?':
163 		default:
164 			usage();
165 			/* NOTREACHED */
166 		}
167 
168 	argc -= optind;
169 	argv += optind;
170 
171 	if (argc == 0)
172 		return checkfstab(flags, isok, checkfs);
173 
174 #define	BADTYPE(type)							\
175 	(strcmp(type, FSTAB_RO) &&					\
176 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
177 
178 
179 	for (; argc--; argv++) {
180 		const char *spec, *mntpt, *type, *cp;
181 		char device[MAXPATHLEN];
182 		struct statfs *mntp;
183 
184 		spec = *argv;
185 		cp = strrchr(spec, '/');
186 		if (cp == 0) {
187 			(void)snprintf(device, sizeof(device), "%s%s",
188 				_PATH_DEV, spec);
189 			spec = device;
190 		}
191 		mntp = getmntpt(spec);
192 		if (mntp != NULL) {
193 			spec = mntp->f_mntfromname;
194 			mntpt = mntp->f_mntonname;
195 		}
196 		if ((fs = getfsfile(spec)) == NULL &&
197 		    (fs = getfsspec(spec)) == NULL) {
198 			if (vfstype == NULL)
199 				vfstype = getfslab(spec);
200 			type = vfstype;
201 			devcheck(spec);
202 		} else {
203 			spec = fs->fs_spec;
204 			type = fs->fs_vfstype;
205 			mntpt = fs->fs_file;
206 			if (BADTYPE(fs->fs_type))
207 				errx(1, "%s has unknown file system type.",
208 				    spec);
209 		}
210 		if ((flags & CHECK_BACKGRD) &&
211 		    checkfs(type, spec, mntpt, "-F", NULL) == 0) {
212 			printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
213 			continue;
214 		}
215 		if ((flags & DO_BACKGRD) && forceflag == 0 &&
216 		    checkfs(type, spec, mntpt, "-F", NULL) != 0)
217 			continue;
218 
219 		rval |= checkfs(type, spec, mntpt, NULL, NULL);
220 	}
221 
222 	return rval;
223 }
224 
225 
226 static int
227 isok(struct fstab *fs)
228 {
229 	int i;
230 
231 	if (fs->fs_passno == 0)
232 		return (0);
233 	if (BADTYPE(fs->fs_type))
234 		return (0);
235 	if (!selected(fs->fs_vfstype))
236 		return (0);
237 	/*
238 	 * If the -B flag has been given, then process the needed
239 	 * background checks. Background checks cannot be run on
240 	 * file systems that will be mounted read-only or that were
241 	 * not mounted at boot time (typically those marked `noauto').
242 	 * If these basic tests are passed, check with the file system
243 	 * itself to see if it is willing to do background checking
244 	 * by invoking its check program with the -F flag.
245 	 */
246 	if (flags & DO_BACKGRD) {
247 		if (!strcmp(fs->fs_type, FSTAB_RO))
248 			return (0);
249 		if (getmntpt(fs->fs_spec) == NULL)
250 			return (0);
251 		if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
252 			return (0);
253 		return (1);
254 	}
255 	/*
256 	 * If the -F flag has been given, then consider deferring the
257 	 * check to background. Background checks cannot be run on
258 	 * file systems that will be mounted read-only or that will
259 	 * not be mounted at boot time (e.g., marked `noauto'). If
260 	 * these basic tests are passed, check with the file system
261 	 * itself to see if it is willing to defer to background
262 	 * checking by invoking its check program with the -F flag.
263 	 */
264 	if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
265 		return (1);
266 	for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
267 		if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
268 			break;
269 	if (i >= 0)
270 		return (1);
271 	if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
272 		return (1);
273 	printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
274 	return (0);
275 }
276 
277 
278 static int
279 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
280     char *auxopt, pid_t *pidp)
281 {
282 	/* List of directories containing fsck_xxx subcommands. */
283 	static const char *edirs[] = {
284 		_PATH_SBIN,
285 		_PATH_USRSBIN,
286 		NULL
287 	};
288 	const char **argv, **edir;
289 	pid_t pid;
290 	int argc, i, status, maxargc;
291 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
292 	char *vfstype = NULL;
293 	const char *extra = NULL;
294 
295 #ifdef __GNUC__
296 	/* Avoid vfork clobbering */
297 	(void) &optbuf;
298 	(void) &vfstype;
299 #endif
300 	/*
301 	 * We convert the vfstype to lowercase and any spaces to underscores
302 	 * to not confuse the issue
303 	 */
304 	vfstype = strdup(pvfstype);
305 	if (vfstype == NULL)
306 		perror("strdup(pvfstype)");
307 	for (i = 0; i < strlen(vfstype); i++) {
308 		vfstype[i] = tolower(vfstype[i]);
309 		if (vfstype[i] == ' ')
310 			vfstype[i] = '_';
311 	}
312 
313 	extra = getoptions(vfstype);
314 	optbuf = NULL;
315 	if (options)
316 		catopt(&optbuf, options);
317 	if (extra)
318 		catopt(&optbuf, extra);
319 	if (auxopt)
320 		catopt(&optbuf, auxopt);
321 	else if (flags & DO_BACKGRD)
322 		catopt(&optbuf, "-B");
323 
324 	maxargc = 64;
325 	argv = emalloc(sizeof(char *) * maxargc);
326 
327 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
328 	argc = 0;
329 	argv[argc++] = execbase;
330 	if (optbuf)
331 		mangle(optbuf, &argc, &argv, &maxargc);
332 	argv[argc++] = spec;
333 	argv[argc] = NULL;
334 
335 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
336 		(void)printf("start %s %swait", mntpt,
337 			pidp ? "no" : "");
338 		for (i = 0; i < argc; i++)
339 			(void)printf(" %s", argv[i]);
340 		(void)printf("\n");
341 	}
342 
343 	switch (pid = vfork()) {
344 	case -1:				/* Error. */
345 		warn("vfork");
346 		if (optbuf)
347 			free(optbuf);
348 		free(vfstype);
349 		return (1);
350 
351 	case 0:					/* Child. */
352 		if ((flags & CHECK_DEBUG) && auxopt == NULL)
353 			_exit(0);
354 
355 		/* Go find an executable. */
356 		edir = edirs;
357 		do {
358 			(void)snprintf(execname,
359 			    sizeof(execname), "%s/%s", *edir, execbase);
360 			execv(execname, (char * const *)argv);
361 			if (errno != ENOENT) {
362 				if (spec)
363 					warn("exec %s for %s", execname, spec);
364 				else
365 					warn("exec %s", execname);
366 			}
367 		} while (*++edir != NULL);
368 
369 		if (errno == ENOENT) {
370 			if (spec)
371 				warn("exec %s for %s", execname, spec);
372 			else
373 				warn("exec %s", execname);
374 		}
375 		_exit(1);
376 		/* NOTREACHED */
377 
378 	default:				/* Parent. */
379 		if (optbuf)
380 			free(optbuf);
381 
382 		free(vfstype);
383 
384 		if (pidp) {
385 			*pidp = pid;
386 			return 0;
387 		}
388 
389 		if (waitpid(pid, &status, 0) < 0) {
390 			warn("waitpid");
391 			return (1);
392 		}
393 
394 		if (WIFEXITED(status)) {
395 			if (WEXITSTATUS(status) != 0)
396 				return (WEXITSTATUS(status));
397 		}
398 		else if (WIFSIGNALED(status)) {
399 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
400 			return (1);
401 		}
402 		break;
403 	}
404 
405 	return (0);
406 }
407 
408 
409 static int
410 selected(const char *type)
411 {
412 	struct entry *e;
413 
414 	/* If no type specified, it's always selected. */
415 	TAILQ_FOREACH(e, &selhead, entries)
416 		if (!strncmp(e->type, type, MFSNAMELEN))
417 			return which == IN_LIST ? 1 : 0;
418 
419 	return which == IN_LIST ? 0 : 1;
420 }
421 
422 
423 static const char *
424 getoptions(const char *type)
425 {
426 	struct entry *e;
427 
428 	TAILQ_FOREACH(e, &opthead, entries)
429 		if (!strncmp(e->type, type, MFSNAMELEN))
430 			return e->options;
431 	return "";
432 }
433 
434 
435 static void
436 addoption(char *optstr)
437 {
438 	char *newoptions;
439 	struct entry *e;
440 
441 	if ((newoptions = strchr(optstr, ':')) == NULL)
442 		errx(1, "Invalid option string");
443 
444 	*newoptions++ = '\0';
445 
446 	TAILQ_FOREACH(e, &opthead, entries)
447 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
448 			catopt(&e->options, newoptions);
449 			return;
450 		}
451 	addentry(&opthead, optstr, newoptions);
452 }
453 
454 
455 static void
456 addentry(struct fstypelist *list, const char *type, const char *opts)
457 {
458 	struct entry *e;
459 
460 	e = emalloc(sizeof(struct entry));
461 	e->type = estrdup(type);
462 	e->options = estrdup(opts);
463 	TAILQ_INSERT_TAIL(list, e, entries);
464 }
465 
466 
467 static void
468 maketypelist(char *fslist)
469 {
470 	char *ptr;
471 
472 	if ((fslist == NULL) || (fslist[0] == '\0'))
473 		errx(1, "empty type list");
474 
475 	if (fslist[0] == 'n' && fslist[1] == 'o') {
476 		fslist += 2;
477 		which = NOT_IN_LIST;
478 	}
479 	else
480 		which = IN_LIST;
481 
482 	while ((ptr = strsep(&fslist, ",")) != NULL)
483 		addentry(&selhead, ptr, "");
484 
485 }
486 
487 
488 static void
489 catopt(char **sp, const char *o)
490 {
491 	char *s;
492 	size_t i, j;
493 
494 	s = *sp;
495 	if (s) {
496 		i = strlen(s);
497 		j = i + 1 + strlen(o) + 1;
498 		s = erealloc(s, j);
499 		(void)snprintf(s + i, j, ",%s", o);
500 	} else
501 		s = estrdup(o);
502 	*sp = s;
503 }
504 
505 
506 static void
507 mangle(char *options, int *argcp, const char ***argvp, int *maxargcp)
508 {
509 	char *p, *s;
510 	int argc, maxargc;
511 	const char **argv;
512 
513 	argc = *argcp;
514 	argv = *argvp;
515 	maxargc = *maxargcp;
516 
517 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
518 		/* Always leave space for one more argument and the NULL. */
519 		if (argc >= maxargc - 3) {
520 			maxargc <<= 1;
521 			argv = erealloc(argv, maxargc * sizeof(char *));
522 		}
523 		if (*p != '\0')  {
524 			if (*p == '-') {
525 				argv[argc++] = p;
526 				p = strchr(p, '=');
527 				if (p) {
528 					*p = '\0';
529 					argv[argc++] = p+1;
530 				}
531 			} else {
532 				argv[argc++] = "-o";
533 				argv[argc++] = p;
534 			}
535 		}
536 	}
537 
538 	*argcp = argc;
539 	*argvp = argv;
540 	*maxargcp = maxargc;
541 }
542 
543 
544 const static char *
545 getfslab(const char *str)
546 {
547 	struct disklabel dl;
548 	int fd;
549 	char p;
550 	const char *vfstype;
551 	u_char t;
552 
553 	/* deduce the file system type from the disk label */
554 	if ((fd = open(str, O_RDONLY)) == -1)
555 		err(1, "cannot open `%s'", str);
556 
557 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
558 		err(1, "cannot get disklabel for `%s'", str);
559 
560 	(void) close(fd);
561 
562 	p = str[strlen(str) - 1];
563 
564 	if ((p - 'a') >= dl.d_npartitions)
565 		errx(1, "partition `%s' is not defined on disk", str);
566 
567 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
568 		errx(1, "partition `%s' is not of a legal vfstype",
569 		    str);
570 
571 	if ((vfstype = fstypenames[t]) == NULL)
572 		errx(1, "vfstype `%s' on partition `%s' is not supported",
573 		    fstypenames[t], str);
574 
575 	return vfstype;
576 }
577 
578 
579 static void
580 usage(void)
581 {
582 	static const char common[] =
583 	    "[-BFdpvlyn] [-T fstype:fsoptions] [-t fstype]";
584 
585 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
586 	    getprogname(), common);
587 	exit(1);
588 }
589