xref: /minix/sbin/fsck/fsck.c (revision 0a6a1f1d)
1 /*	$NetBSD: fsck.c,v 1.52 2014/10/25 22:00:19 mlelstv 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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
33  * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
34  *
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: fsck.c,v 1.52 2014/10/25 22:00:19 mlelstv Exp $");
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/queue.h>
45 #include <sys/wait.h>
46 #define FSTYPENAMES
47 #define FSCKNAMES
48 #include <sys/disk.h>
49 #include <sys/disklabel.h>
50 #include <sys/ioctl.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <fstab.h>
55 #include <fcntl.h>
56 #include <paths.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <util.h>
63 
64 #include "pathnames.h"
65 #include "fsutil.h"
66 #include "exitvalues.h"
67 
68 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
69 
TAILQ_HEAD(fstypelist,entry)70 TAILQ_HEAD(fstypelist, entry) opthead, selhead, omhead;
71 
72 struct entry {
73 	char *type;
74 	char *options;
75 	TAILQ_ENTRY(entry) entries;
76 };
77 
78 static int maxrun = 0;
79 static char *options = NULL;
80 static int flags = 0;
81 
82 static int checkfs(const char *, const char *, const char *, void *, pid_t *);
83 static int selected(const char *);
84 static int omitted(const char *);
85 static void addoption(char *);
86 static const char *getoptions(const char *);
87 static void addentry(struct fstypelist *, const char *, const char *);
88 static void maketypelist(char *);
89 static void catopt(char **, const char *);
90 static void mangle(char *, int *, const char ** volatile *, int *);
91 static const char *getfslab(const char *);
92 __dead static void usage(void);
93 static void *isok(struct fstab *);
94 
95 int
main(int argc,char * argv[])96 main(int argc, char *argv[])
97 {
98 	struct fstab *fs;
99 	int i, rval;
100 	const char *vfstype = NULL;
101 	char globopt[3];
102 	int ret = FSCK_EXIT_OK;
103 	char buf[MAXPATHLEN];
104 
105 	globopt[0] = '-';
106 	globopt[2] = '\0';
107 
108 	TAILQ_INIT(&selhead);
109 	TAILQ_INIT(&opthead);
110 	TAILQ_INIT(&omhead);
111 
112 	while ((i = getopt(argc, argv, "dfl:nPpqT:t:vx:y")) != -1) {
113 		switch (i) {
114 		case 'd':
115 			flags |= CHECK_DEBUG;
116 			continue;
117 
118 		case 'f':
119 			flags |= CHECK_FORCE;
120 			break;
121 
122 		case 'n':
123 			flags |= CHECK_NOFIX;
124 			break;
125 
126 		case 'p':
127 			flags |= CHECK_PREEN;
128 			break;
129 
130 		case 'P':
131 			flags |= CHECK_PROGRESS;
132 			break;
133 
134 		case 'q':
135 			break;
136 
137 		case 'l':
138 			maxrun = atoi(optarg);
139 			continue;
140 
141 		case 'T':
142 			if (*optarg)
143 				addoption(optarg);
144 			continue;
145 
146 		case 't':
147 			if (TAILQ_FIRST(&selhead) != NULL)
148 				errx(1, "only one -t option may be specified.");
149 
150 			maketypelist(optarg);
151 			vfstype = optarg;
152 			continue;
153 
154 		case 'v':
155 			flags |= CHECK_VERBOSE;
156 			continue;
157 
158 		case 'x':
159 			addentry(&omhead, optarg, "");
160 			continue;
161 
162 		case 'y':
163 			break;
164 
165 		case '?':
166 		default:
167 			usage();
168 			/* NOTREACHED */
169 		}
170 
171 		/* Pass option to fsck_xxxfs */
172 		globopt[1] = i;
173 		catopt(&options, globopt);
174 	}
175 
176 	/* Don't do progress meters if we're debugging. */
177 	if (flags & CHECK_DEBUG)
178 		flags &= ~CHECK_PROGRESS;
179 
180 	/*
181 	 * If progress meters are being used, force max parallel to 1
182 	 * so the progress meter outputs don't interfere with one another.
183 	 */
184 	if (flags & CHECK_PROGRESS)
185 		maxrun = 1;
186 
187 #if defined(__minix)
188 	/* parallel checking heuristic doesn't work for minix currently */
189 	maxrun = 1;
190 #endif /* !defined(__minix) */
191 	argc -= optind;
192 	argv += optind;
193 
194 	if (argc == 0)
195 		return checkfstab(flags, maxrun, isok, checkfs);
196 
197 #define	BADTYPE(type)							\
198 	(strcmp(type, FSTAB_RO) &&					\
199 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
200 
201 
202 	for (; argc--; argv++) {
203 		const char *spec, *spec2, *mntpt, *type, *cp;
204 		char	device[MAXPATHLEN];
205 
206 		spec = mntpt = *argv;
207 		spec2 = getfsspecname(buf, sizeof(buf), spec);
208 		if (spec2 == NULL)
209 			spec2 = spec;
210 
211 		cp = strrchr(spec2, '/');
212 		if (cp == 0) {
213 			(void)snprintf(device, sizeof(device), "%s%s",
214 				_PATH_DEV, spec2);
215 			spec2 = device;
216 		}
217 
218 		fs = getfsfile(spec);
219 		if (fs == NULL)
220 		    fs = getfsspec(spec);
221 		if (fs == NULL && spec != spec2) {
222 		    fs = getfsspec(spec2);
223 		    spec = spec2;
224 		}
225 
226 		if (fs) {
227 			spec = getfsspecname(buf, sizeof(buf), fs->fs_spec);
228 			if (spec == NULL)
229 				err(FSCK_EXIT_CHECK_FAILED, "%s", buf);
230 			type = fs->fs_vfstype;
231 			if (BADTYPE(fs->fs_type))
232 				errx(FSCK_EXIT_CHECK_FAILED,
233 				    "%s has unknown file system type.",
234 				    spec);
235 		} else {
236 			if (vfstype == NULL)
237 				vfstype = getfslab(spec);
238 			type = vfstype;
239 		}
240 
241 		rval = checkfs(type, blockcheck(spec), *argv, NULL, NULL);
242 		if (rval > ret)
243 			ret = rval;
244 	}
245 
246 	return ret;
247 }
248 
249 
250 static void *
isok(struct fstab * fs)251 isok(struct fstab *fs)
252 {
253 
254 	if (fs->fs_passno == 0)
255 		return NULL;
256 
257 	if (BADTYPE(fs->fs_type))
258 		return NULL;
259 
260 	if (!selected(fs->fs_vfstype))
261 		return NULL;
262 
263 	if (omitted(fs->fs_file))
264 		return NULL;
265 
266 	return fs;
267 }
268 
269 
270 static int
checkfs(const char * vfst,const char * spec,const char * mntpt,void * auxarg,pid_t * pidp)271 checkfs(const char *vfst, const char *spec, const char *mntpt, void *auxarg,
272     pid_t *pidp)
273 {
274 	/* List of directories containing fsck_xxx subcommands. */
275 	static const char *edirs[] = {
276 #ifdef RESCUEDIR
277 		RESCUEDIR,
278 #endif
279 		_PATH_SBIN,
280 		_PATH_USRSBIN,
281 #if defined(__minix)
282 		"/usr/pkg/sbin/",
283 #endif /* defined(__minix) */
284 		NULL
285 	};
286 	const char ** volatile argv, **edir;
287 	const char * volatile vfstype = vfst;
288 	pid_t pid;
289 	int argc, i, status, maxargc;
290 	char *optb;
291 	char *volatile optbuf;
292 	char execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
293 	const char *extra = getoptions(vfstype);
294 
295 	if (!strcmp(vfstype, "ufs"))
296 		vfstype = MOUNT_UFS;
297 
298 	optb = NULL;
299 	if (options)
300 		catopt(&optb, options);
301 	if (extra)
302 		catopt(&optb, extra);
303 	optbuf = optb;
304 
305 	maxargc = 64;
306 	argv = emalloc(sizeof(char *) * maxargc);
307 
308 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
309 	argc = 0;
310 	argv[argc++] = execbase;
311 	if (optbuf)
312 		mangle(optbuf, &argc, &argv, &maxargc);
313 	argv[argc++] = spec;
314 	argv[argc] = NULL;
315 
316 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
317 		(void)printf("start %s %swait", mntpt,
318 			pidp ? "no" : "");
319 		for (i = 0; i < argc; i++)
320 			(void)printf(" %s", argv[i]);
321 		(void)printf("\n");
322 	}
323 
324 	switch (pid = vfork()) {
325 	case -1:				/* Error. */
326 		warn("vfork");
327 		if (optbuf)
328 			free(optbuf);
329 		free(argv);
330 		return FSCK_EXIT_CHECK_FAILED;
331 
332 	case 0:					/* Child. */
333 		if ((flags & CHECK_FORCE) == 0) {
334 			struct statvfs	sfs;
335 
336 				/*
337 				 * if mntpt is a mountpoint of a mounted file
338 				 * system and it's mounted read-write, skip it
339 				 * unless -f is given.
340 				 */
341 			if ((statvfs(mntpt, &sfs) == 0) &&
342 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
343 			    ((sfs.f_flag & MNT_RDONLY) == 0)) {
344 				printf(
345 		"%s: file system is mounted read-write on %s; not checking\n",
346 				    spec, mntpt);
347 				if ((flags & CHECK_PREEN) && auxarg != NULL)
348 					_exit(FSCK_EXIT_OK);	/* fsck -p */
349 				else
350 					_exit(FSCK_EXIT_CHECK_FAILED);	/* fsck [[-p] ...] */
351 			}
352 		}
353 
354 		if (flags & CHECK_DEBUG)
355 			_exit(FSCK_EXIT_OK);
356 
357 		/* Go find an executable. */
358 		edir = edirs;
359 		do {
360 			(void)snprintf(execname,
361 			    sizeof(execname), "%s/%s", *edir, execbase);
362 			execv(execname, (char * const *)__UNCONST(argv));
363 			if (errno != ENOENT) {
364 				if (spec)
365 					warn("exec %s for %s", execname, spec);
366 				else
367 					warn("exec %s", execname);
368 			}
369 		} while (*++edir != NULL);
370 
371 		if (errno == ENOENT) {
372 			if (spec)
373 				warn("exec %s for %s", execname, spec);
374 			else
375 				warn("exec %s", execname);
376 		}
377 		_exit(FSCK_EXIT_CHECK_FAILED);
378 		/* NOTREACHED */
379 
380 	default:				/* Parent. */
381 		if (optbuf)
382 			free(optbuf);
383 		free(argv);
384 
385 		if (pidp) {
386 			*pidp = pid;
387 			return FSCK_EXIT_OK;
388 		}
389 
390 		if (waitpid(pid, &status, 0) < 0) {
391 			warn("waitpid");
392 			return FSCK_EXIT_CHECK_FAILED;
393 		}
394 
395 		if (WIFEXITED(status)) {
396 			if (WEXITSTATUS(status) != 0)
397 				return WEXITSTATUS(status);
398 		}
399 		else if (WIFSIGNALED(status)) {
400 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
401 			return FSCK_EXIT_CHECK_FAILED;
402 		}
403 		break;
404 	}
405 
406 	return FSCK_EXIT_OK;
407 }
408 
409 
410 static int
selected(const char * type)411 selected(const char *type)
412 {
413 	struct entry *e;
414 
415 	/* If no type specified, it's always selected. */
416 	TAILQ_FOREACH(e, &selhead, entries)
417 		if (!strcmp(e->type, type))
418 			return which == IN_LIST ? 1 : 0;
419 
420 	return which == IN_LIST ? 0 : 1;
421 }
422 
423 
424 static int
omitted(const char * mountedon)425 omitted(const char *mountedon)
426 {
427 	struct entry *e;
428 
429 	/* If no type specified, it's always selected. */
430 	TAILQ_FOREACH(e, &omhead, entries)
431 		if (!strcmp(e->type, mountedon))
432 			return 1;
433 
434 	return 0;
435 }
436 
437 
438 static const char *
getoptions(const char * type)439 getoptions(const char *type)
440 {
441 	struct entry *e;
442 
443 	TAILQ_FOREACH(e, &opthead, entries)
444 		if (!strcmp(e->type, type))
445 			return e->options;
446 	return "";
447 }
448 
449 
450 static void
addoption(char * optstr)451 addoption(char *optstr)
452 {
453 	char *newoptions;
454 	struct entry *e;
455 
456 	if ((newoptions = strchr(optstr, ':')) == NULL)
457 		errx(1, "Invalid option string");
458 
459 	*newoptions++ = '\0';
460 
461 	TAILQ_FOREACH(e, &opthead, entries)
462 		if (!strcmp(e->type, optstr)) {
463 			catopt(&e->options, newoptions);
464 			return;
465 		}
466 	addentry(&opthead, optstr, newoptions);
467 }
468 
469 
470 static void
addentry(struct fstypelist * list,const char * type,const char * opts)471 addentry(struct fstypelist *list, const char *type, const char *opts)
472 {
473 	struct entry *e;
474 
475 	e = emalloc(sizeof(struct entry));
476 	e->type = estrdup(type);
477 	e->options = estrdup(opts);
478 	TAILQ_INSERT_TAIL(list, e, entries);
479 }
480 
481 
482 static void
maketypelist(char * fslist)483 maketypelist(char *fslist)
484 {
485 	char *ptr;
486 
487 	if ((fslist == NULL) || (fslist[0] == '\0'))
488 		errx(1, "empty type list");
489 
490 	if (fslist[0] == 'n' && fslist[1] == 'o') {
491 		fslist += 2;
492 		which = NOT_IN_LIST;
493 	}
494 	else
495 		which = IN_LIST;
496 
497 	while ((ptr = strsep(&fslist, ",")) != NULL)
498 		addentry(&selhead, ptr, "");
499 
500 }
501 
502 
503 static void
catopt(char ** sp,const char * o)504 catopt(char **sp, const char *o)
505 {
506 	char *s;
507 	size_t i, j;
508 
509 	s = *sp;
510 	if (s) {
511 		i = strlen(s);
512 		j = i + 1 + strlen(o) + 1;
513 		s = erealloc(s, j);
514 		(void)snprintf(s + i, j, ",%s", o);
515 	} else
516 		s = estrdup(o);
517 	*sp = s;
518 }
519 
520 
521 static void
mangle(char * opts,int * argcp,const char ** volatile * argvp,int * maxargcp)522 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
523 {
524 	char *p, *s;
525 	int argc, maxargc;
526 	const char **argv;
527 
528 	argc = *argcp;
529 	argv = *argvp;
530 	maxargc = *maxargcp;
531 
532 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
533 		/* Always leave space for one more argument and the NULL. */
534 		if (argc >= maxargc - 3) {
535 			maxargc <<= 1;
536 			argv = erealloc(argv, maxargc * sizeof(char *));
537 		}
538 		if (*p != '\0')  {
539 			if (*p == '-') {
540 				argv[argc++] = p;
541 				p = strchr(p, '=');
542 				if (p) {
543 					*p = '\0';
544 					argv[argc++] = p+1;
545 				}
546 			} else {
547 				argv[argc++] = "-o";
548 				argv[argc++] = p;
549 			}
550 		}
551 	}
552 
553 	*argcp = argc;
554 	*argvp = argv;
555 	*maxargcp = maxargc;
556 }
557 
558 static const char *
getfslab(const char * str)559 getfslab(const char *str)
560 {
561 #if defined(__minix)
562 	errx(1, "cannot determine vfstype under minix");
563 #else
564 	static struct dkwedge_info dkw;
565 	struct disklabel dl;
566 	int fd;
567 	char p;
568 	const char *vfstype;
569 	u_char t;
570 
571 	/* deduce the file system type from the disk label */
572 	if ((fd = open(str, O_RDONLY)) == -1)
573 		err(1, "cannot open `%s'", str);
574 
575 	/* First check to see if it's a wedge. */
576 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
577 		/* Yup, this is easy. */
578 		(void) close(fd);
579 		return (dkw.dkw_ptype);
580 	}
581 
582 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
583 		err(1, "cannot get disklabel for `%s'", str);
584 
585 	(void) close(fd);
586 
587 	p = str[strlen(str) - 1];
588 
589 	if ((p - 'a') >= dl.d_npartitions)
590 		errx(1, "partition `%s' is not defined on disk", str);
591 
592 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
593 		errx(1, "partition `%s' is not of a legal vfstype",
594 		    str);
595 
596 	if ((vfstype = fscknames[t]) == NULL)
597 		errx(1, "vfstype `%s' on partition `%s' is not supported",
598 		    fstypenames[t], str);
599 
600 	return vfstype;
601 #endif /* defined(__minix) */
602 }
603 
604 
605 static void
usage(void)606 usage(void)
607 {
608 	static const char common[] =
609 	    "[-dfnPpqvy] [-x excludemount] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]";
610 
611 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
612 	    getprogname(), common);
613 	exit(FSCK_EXIT_USAGE);
614 }
615