xref: /openbsd-src/sbin/fsck/preen.c (revision b9fc9a728fce9c4289b7e9a992665e28d5629a54)
1*b9fc9a72Sderaadt /*	$OpenBSD: preen.c,v 1.20 2015/01/16 06:39:57 deraadt Exp $	*/
287304b87Stholo /*	$NetBSD: preen.c,v 1.15 1996/09/28 19:21:42 christos Exp $	*/
387304b87Stholo 
487304b87Stholo /*
587304b87Stholo  * Copyright (c) 1990, 1993
687304b87Stholo  *	The Regents of the University of California.  All rights reserved.
787304b87Stholo  *
887304b87Stholo  * Redistribution and use in source and binary forms, with or without
987304b87Stholo  * modification, are permitted provided that the following conditions
1087304b87Stholo  * are met:
1187304b87Stholo  * 1. Redistributions of source code must retain the above copyright
1287304b87Stholo  *    notice, this list of conditions and the following disclaimer.
1387304b87Stholo  * 2. Redistributions in binary form must reproduce the above copyright
1487304b87Stholo  *    notice, this list of conditions and the following disclaimer in the
1587304b87Stholo  *    documentation and/or other materials provided with the distribution.
161ef0d710Smillert  * 3. Neither the name of the University nor the names of its contributors
1787304b87Stholo  *    may be used to endorse or promote products derived from this software
1887304b87Stholo  *    without specific prior written permission.
1987304b87Stholo  *
2087304b87Stholo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2187304b87Stholo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2287304b87Stholo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2387304b87Stholo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2487304b87Stholo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2587304b87Stholo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2687304b87Stholo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2787304b87Stholo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2887304b87Stholo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2987304b87Stholo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3087304b87Stholo  * SUCH DAMAGE.
3187304b87Stholo  */
3287304b87Stholo 
3387304b87Stholo #include <sys/stat.h>
3487304b87Stholo #include <sys/wait.h>
3587304b87Stholo #include <sys/queue.h>
3687304b87Stholo 
3787304b87Stholo #include <fstab.h>
3887304b87Stholo #include <string.h>
3987304b87Stholo #include <stdio.h>
4087304b87Stholo #include <stdlib.h>
4187304b87Stholo #include <ctype.h>
4287304b87Stholo #include <unistd.h>
4387304b87Stholo #include <err.h>
4487304b87Stholo 
4587304b87Stholo #include "fsutil.h"
4687304b87Stholo 
4787304b87Stholo struct partentry {
4887304b87Stholo 	TAILQ_ENTRY(partentry)	 p_entries;
4987304b87Stholo 	char		  	*p_devname;	/* device name */
5087304b87Stholo 	char			*p_mntpt;	/* mount point */
5187304b87Stholo 	char		  	*p_type;	/* filesystem type */
5287304b87Stholo 	void			*p_auxarg;	/* auxiliary argument */
5387304b87Stholo };
5487304b87Stholo 
5587304b87Stholo TAILQ_HEAD(part, partentry) badh;
5687304b87Stholo 
5787304b87Stholo struct diskentry {
5887304b87Stholo 	TAILQ_ENTRY(diskentry) 	    d_entries;
5987304b87Stholo 	char		       	   *d_name;	/* disk base name */
6087304b87Stholo 	TAILQ_HEAD(prt, partentry)  d_part;	/* list of partitions on disk */
61baa95fbdSniklas 	pid_t			    d_pid;	/* 0 or pid of fsck proc */
6287304b87Stholo };
6387304b87Stholo 
6487304b87Stholo TAILQ_HEAD(disk, diskentry) diskh;
6587304b87Stholo 
6687304b87Stholo static int nrun = 0, ndisks = 0;
6787304b87Stholo 
68c72b5b24Smillert static struct diskentry *finddisk(const char *);
69c72b5b24Smillert static void addpart(const char *, const char *, const char *, void *);
70f3c3a9c6Smillert static int startdisk(struct diskentry *,
71f3c3a9c6Smillert     int (*)(const char *, const char *, const char *, void *, pid_t *));
72c72b5b24Smillert static void printpart(void);
7387304b87Stholo 
7487304b87Stholo int
checkfstab(int flags,int maxrun,void * (* docheck)(struct fstab *),int (* checkit)(const char *,const char *,const char *,void *,pid_t *))75bc52e260Sderaadt checkfstab(int flags, int maxrun, void *(*docheck)(struct fstab *),
76bc52e260Sderaadt     int (*checkit)(const char *, const char *, const char *, void *, pid_t *))
7787304b87Stholo {
7887304b87Stholo 	struct fstab *fs;
7987304b87Stholo 	struct diskentry *d, *nextdisk;
8087304b87Stholo 	struct partentry *p;
819fe2e33fSckuethe 	int ret, retcode, passno, sumstatus, status, maxp;
8287304b87Stholo 	void *auxarg;
8387304b87Stholo 	char *name;
8413ee8a54Sderaadt 	pid_t pid;
8587304b87Stholo 
8687304b87Stholo 	TAILQ_INIT(&badh);
8787304b87Stholo 	TAILQ_INIT(&diskh);
8887304b87Stholo 
8987304b87Stholo 	sumstatus = 0;
909fe2e33fSckuethe 	maxp = 2;
9187304b87Stholo 
929fe2e33fSckuethe 	for (passno = 1; passno <= maxp; passno++) {
9387304b87Stholo 		if (setfsent() == 0) {
9446347781Smpech 			warnx("Can't open checklist file: %s", _PATH_FSTAB);
9587304b87Stholo 			return (8);
9687304b87Stholo 		}
9787304b87Stholo 		while ((fs = getfsent()) != 0) {
9887304b87Stholo 			if ((auxarg = (*docheck)(fs)) == NULL)
9987304b87Stholo 				continue;
10087304b87Stholo 
10187304b87Stholo 			name = blockcheck(fs->fs_spec);
10287304b87Stholo 			if (flags & CHECK_DEBUG)
10387304b87Stholo 				printf("pass %d, name %s\n", passno, name);
1049fe2e33fSckuethe 			maxp = (fs->fs_passno > maxp) ? fs->fs_passno : maxp;
10587304b87Stholo 
10687304b87Stholo 			if ((flags & CHECK_PREEN) == 0 ||
10787304b87Stholo 			    (passno == 1 && fs->fs_passno == 1)) {
10887304b87Stholo 				if (name == NULL) {
10987304b87Stholo 					if (flags & CHECK_PREEN)
11087304b87Stholo 						return 8;
11187304b87Stholo 					else
11287304b87Stholo 						continue;
11387304b87Stholo 				}
11487304b87Stholo 				sumstatus = (*checkit)(fs->fs_vfstype,
11587304b87Stholo 				    name, fs->fs_file, auxarg, NULL);
11687304b87Stholo 
11787304b87Stholo 				if (sumstatus)
11887304b87Stholo 					return (sumstatus);
1199fe2e33fSckuethe 			} else  {
12087304b87Stholo 				if (name == NULL) {
12187304b87Stholo 					(void) fprintf(stderr,
12287304b87Stholo 					    "BAD DISK NAME %s\n", fs->fs_spec);
12387304b87Stholo 					sumstatus |= 8;
12487304b87Stholo 					continue;
12587304b87Stholo 				}
1269fe2e33fSckuethe 				if (passno == fs->fs_passno)
1279fe2e33fSckuethe 					addpart(fs->fs_vfstype, name,
1289fe2e33fSckuethe 					    fs->fs_file, auxarg);
12987304b87Stholo 			}
13087304b87Stholo 		}
13187304b87Stholo 		if ((flags & CHECK_PREEN) == 0)
13287304b87Stholo 			return 0;
13387304b87Stholo 	}
13487304b87Stholo 
13587304b87Stholo 	if (flags & CHECK_DEBUG)
13687304b87Stholo 		printpart();
13787304b87Stholo 
13887304b87Stholo 	if (flags & CHECK_PREEN) {
13987304b87Stholo 		if (maxrun == 0)
14087304b87Stholo 			maxrun = ndisks;
14187304b87Stholo 		if (maxrun > ndisks)
14287304b87Stholo 			maxrun = ndisks;
143441476a3Sotto 		nextdisk = TAILQ_FIRST(&diskh);
14487304b87Stholo 		for (passno = 0; passno < maxrun; ++passno) {
14587304b87Stholo 			if ((ret = startdisk(nextdisk, checkit)) != 0)
14687304b87Stholo 				return ret;
147441476a3Sotto 			nextdisk = TAILQ_NEXT(nextdisk, d_entries);
14887304b87Stholo 		}
14987304b87Stholo 
15087304b87Stholo 		while ((pid = wait(&status)) != -1) {
151441476a3Sotto 			TAILQ_FOREACH(d, &diskh, d_entries)
15287304b87Stholo 				if (d->d_pid == pid)
15387304b87Stholo 					break;
15487304b87Stholo 
15587304b87Stholo 			if (d == NULL) {
15613ee8a54Sderaadt 				warnx("Unknown pid %ld", (long)pid);
15787304b87Stholo 				continue;
15887304b87Stholo 			}
15987304b87Stholo 
16087304b87Stholo 
16187304b87Stholo 			if (WIFEXITED(status))
16287304b87Stholo 				retcode = WEXITSTATUS(status);
16387304b87Stholo 			else
16487304b87Stholo 				retcode = 0;
16587304b87Stholo 
166441476a3Sotto 			p = TAILQ_FIRST(&d->d_part);
16787304b87Stholo 
16887304b87Stholo 			if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
16987304b87Stholo 				(void) printf("done %s: %s (%s) = %x\n",
17087304b87Stholo 				    p->p_type, p->p_devname, p->p_mntpt,
17187304b87Stholo 				    status);
17287304b87Stholo 
17387304b87Stholo 			if (WIFSIGNALED(status)) {
17487304b87Stholo 				(void) fprintf(stderr,
17587304b87Stholo 				    "%s: %s (%s): EXITED WITH SIGNAL %d\n",
17687304b87Stholo 				    p->p_type, p->p_devname, p->p_mntpt,
17787304b87Stholo 				    WTERMSIG(status));
17887304b87Stholo 				retcode = 8;
17987304b87Stholo 			}
18087304b87Stholo 
18187304b87Stholo 			TAILQ_REMOVE(&d->d_part, p, p_entries);
18287304b87Stholo 
18387304b87Stholo 			if (retcode != 0) {
18487304b87Stholo 				TAILQ_INSERT_TAIL(&badh, p, p_entries);
18587304b87Stholo 				sumstatus |= retcode;
18687304b87Stholo 			} else {
18787304b87Stholo 				free(p->p_type);
18887304b87Stholo 				free(p->p_devname);
18987304b87Stholo 				free(p);
19087304b87Stholo 			}
19187304b87Stholo 			d->d_pid = 0;
19287304b87Stholo 			nrun--;
19387304b87Stholo 
19474c87c28Sotto 			if (TAILQ_EMPTY(&d->d_part))
19587304b87Stholo 				ndisks--;
19687304b87Stholo 
19787304b87Stholo 			if (nextdisk == NULL) {
19874c87c28Sotto 				if (!TAILQ_EMPTY(&d->d_part)) {
19987304b87Stholo 					if ((ret = startdisk(d, checkit)) != 0)
20087304b87Stholo 						return ret;
20187304b87Stholo 				}
20287304b87Stholo 			} else if (nrun < maxrun && nrun < ndisks) {
20387304b87Stholo 				for ( ;; ) {
204441476a3Sotto 					nextdisk = TAILQ_NEXT(nextdisk,
205441476a3Sotto 					    d_entries);
20687304b87Stholo 					if (nextdisk == NULL)
207441476a3Sotto 						nextdisk = TAILQ_FIRST(&diskh);
20874c87c28Sotto 					if (!TAILQ_EMPTY(&nextdisk->d_part) &&
20974c87c28Sotto 					    nextdisk->d_pid == 0)
21087304b87Stholo 						break;
21187304b87Stholo 				}
21287304b87Stholo 				if ((ret = startdisk(nextdisk, checkit)) != 0)
21387304b87Stholo 					return ret;
21487304b87Stholo 			}
21587304b87Stholo 		}
21687304b87Stholo 	}
21787304b87Stholo 	if (sumstatus) {
218441476a3Sotto 		p = TAILQ_FIRST(&badh);
21987304b87Stholo 		if (p == NULL)
22087304b87Stholo 			return (sumstatus);
22187304b87Stholo 
22287304b87Stholo 		(void) fprintf(stderr,
22387304b87Stholo 			"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
224441476a3Sotto 			TAILQ_NEXT(p, p_entries) ? "S" : "",
22587304b87Stholo 			"UNEXPECTED INCONSISTENCY:");
22687304b87Stholo 
227441476a3Sotto 		for (; p; p = TAILQ_NEXT(p, p_entries))
22887304b87Stholo 			(void) fprintf(stderr,
22987304b87Stholo 			    "%s: %s (%s)%s", p->p_type, p->p_devname,
230441476a3Sotto 			    p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n");
23187304b87Stholo 
23287304b87Stholo 		return sumstatus;
23387304b87Stholo 	}
23487304b87Stholo 	(void) endfsent();
23587304b87Stholo 	return (0);
23687304b87Stholo }
23787304b87Stholo 
23887304b87Stholo 
23987304b87Stholo static struct diskentry *
finddisk(const char * name)240bc52e260Sderaadt finddisk(const char *name)
24187304b87Stholo {
24287304b87Stholo 	const char *p;
2434158ce8dSderaadt 	size_t len = 0;
24487304b87Stholo 	struct diskentry *d;
24587304b87Stholo 
24687304b87Stholo 	for (p = name + strlen(name) - 1; p >= name; --p)
247025f5691Sderaadt 		if (isdigit((unsigned char)*p)) {
24887304b87Stholo 			len = p - name + 1;
24987304b87Stholo 			break;
25087304b87Stholo 		}
25187304b87Stholo 
25287304b87Stholo 	if (p < name)
25387304b87Stholo 		len = strlen(name);
25487304b87Stholo 
255441476a3Sotto 	TAILQ_FOREACH(d, &diskh, d_entries)
25687304b87Stholo 		if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
25787304b87Stholo 			return d;
25887304b87Stholo 
25987304b87Stholo 	d = emalloc(sizeof(*d));
26087304b87Stholo 	d->d_name = estrdup(name);
26187304b87Stholo 	d->d_name[len] = '\0';
26287304b87Stholo 	TAILQ_INIT(&d->d_part);
26387304b87Stholo 	d->d_pid = 0;
26487304b87Stholo 
26587304b87Stholo 	TAILQ_INSERT_TAIL(&diskh, d, d_entries);
26687304b87Stholo 	ndisks++;
26787304b87Stholo 
26887304b87Stholo 	return d;
26987304b87Stholo }
27087304b87Stholo 
27187304b87Stholo 
27287304b87Stholo static void
printpart(void)273bc52e260Sderaadt printpart(void)
27487304b87Stholo {
27587304b87Stholo 	struct diskentry *d;
27687304b87Stholo 	struct partentry *p;
27787304b87Stholo 
278441476a3Sotto 	TAILQ_FOREACH(d, &diskh, d_entries) {
27987304b87Stholo 		(void) printf("disk %s: ", d->d_name);
280441476a3Sotto 		TAILQ_FOREACH(p, &d->d_part, p_entries)
28187304b87Stholo 			(void) printf("%s ", p->p_devname);
28287304b87Stholo 		(void) printf("\n");
28387304b87Stholo 	}
28487304b87Stholo }
28587304b87Stholo 
28687304b87Stholo 
28787304b87Stholo static void
addpart(const char * type,const char * devname,const char * mntpt,void * auxarg)288bc52e260Sderaadt addpart(const char *type, const char *devname, const char *mntpt, void *auxarg)
28987304b87Stholo {
29087304b87Stholo 	struct diskentry *d = finddisk(devname);
29187304b87Stholo 	struct partentry *p;
29287304b87Stholo 
293441476a3Sotto 	TAILQ_FOREACH(p, &d->d_part, p_entries)
29487304b87Stholo 		if (strcmp(p->p_devname, devname) == 0) {
29546347781Smpech 			warnx("%s in fstab more than once!", devname);
29687304b87Stholo 			return;
29787304b87Stholo 		}
29887304b87Stholo 
29987304b87Stholo 	p = emalloc(sizeof(*p));
30087304b87Stholo 	p->p_devname = estrdup(devname);
30187304b87Stholo 	p->p_mntpt = estrdup(mntpt);
30287304b87Stholo 	p->p_type = estrdup(type);
30387304b87Stholo 	p->p_auxarg = auxarg;
30487304b87Stholo 
30587304b87Stholo 	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
30687304b87Stholo }
30787304b87Stholo 
30887304b87Stholo 
30987304b87Stholo static int
startdisk(struct diskentry * d,int (* checkit)(const char *,const char *,const char *,void *,pid_t *))310bc52e260Sderaadt startdisk(struct diskentry *d,
311bc52e260Sderaadt     int (*checkit)(const char *, const char *, const char *, void *, pid_t *))
31287304b87Stholo {
313441476a3Sotto 	struct partentry *p = TAILQ_FIRST(&d->d_part);
31487304b87Stholo 	int rv;
31587304b87Stholo 
31687304b87Stholo 	while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
31787304b87Stholo 	    p->p_auxarg, &d->d_pid)) != 0 && nrun > 0)
31887304b87Stholo 		sleep(10);
31987304b87Stholo 
32087304b87Stholo 	if (rv == 0)
32187304b87Stholo 		nrun++;
32287304b87Stholo 
32387304b87Stholo 	return rv;
32487304b87Stholo }
325