xref: /netbsd/sbin/fsck/preen.c (revision fdf6ea6c)
1 /*	$NetBSD: preen.c,v 1.13 1996/09/23 16:11:35 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)preen.c	8.3 (Berkeley) 12/6/94";
39 #else
40 static char rcsid[] = "$NetBSD: preen.c,v 1.13 1996/09/23 16:11:35 christos Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/wait.h>
47 #include <sys/queue.h>
48 
49 #include <fstab.h>
50 #include <string.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <ctype.h>
54 #include <unistd.h>
55 #include <err.h>
56 
57 #include "util.h"
58 #include "extern.h"
59 
60 struct partentry {
61 	TAILQ_ENTRY(partentry)	 p_entries;
62 	char		  	*p_name;	/* device name */
63 	char		  	*p_type;	/* filesystem type */
64 };
65 
66 TAILQ_HEAD(part, partentry) badh;
67 
68 struct diskentry {
69 	TAILQ_ENTRY(diskentry) 	    d_entries;
70 	char		       	   *d_name;	/* disk base name */
71 	TAILQ_HEAD(prt, partentry)  d_part;	/* list of partitions on disk */
72 	int			    d_pid;	/* 0 or pid of fsck proc */
73 };
74 
75 TAILQ_HEAD(disk, diskentry) diskh;
76 
77 static int nrun = 0, ndisks = 0;
78 
79 static struct diskentry *finddisk __P((const char *));
80 static void addpart __P((const char *, const char *));
81 static int startdisk __P((struct diskentry *,
82     int (*)(const char *, const char *, pid_t *)));
83 #ifdef DEBUG
84 static void printpart __P((void));
85 #endif
86 
87 int
88 checkfstab(preen, maxrun, docheck, chkit)
89 	int preen, maxrun;
90 	int (*docheck) __P((struct fstab *));
91 	int (*chkit) __P((const char *, const char *, pid_t *));
92 {
93 	struct fstab *fs;
94 	struct diskentry *d, *nextdisk;
95 	struct partentry *p;
96 	int ret, pid, retcode, passno, sumstatus, status;
97 	char *name;
98 
99 	TAILQ_INIT(&badh);
100 	TAILQ_INIT(&diskh);
101 
102 	sumstatus = 0;
103 
104 	for (passno = 1; passno <= 2; passno++) {
105 		if (setfsent() == 0) {
106 			warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
107 			return (8);
108 		}
109 		while ((fs = getfsent()) != 0) {
110 			if ((*docheck)(fs) == 0)
111 				continue;
112 
113 			name = blockcheck(fs->fs_spec);
114 #ifdef DEBUG
115 			if (debug > 1)
116 				printf("pass %d, name %s\n", passno, name);
117 #endif
118 
119 			if (preen == 0 || (passno == 1 && fs->fs_passno == 1)) {
120 				if (name == NULL) {
121 					if (preen)
122 						return 8;
123 					else
124 						continue;
125 				}
126 				sumstatus = (*chkit)(fs->fs_vfstype,
127 				    name, NULL);
128 
129 				if (sumstatus)
130 					return (sumstatus);
131 			} else if (passno == 2 && fs->fs_passno > 1) {
132 				if (name == NULL) {
133 					(void) fprintf(stderr,
134 					    "BAD DISK NAME %s\n", fs->fs_spec);
135 					sumstatus |= 8;
136 					continue;
137 				}
138 				addpart(fs->fs_vfstype, name);
139 			}
140 		}
141 		if (preen == 0)
142 			return (0);
143 	}
144 
145 #ifdef DEBUG
146 	if (debug > 1)
147 		printpart();
148 #endif
149 
150 	if (preen) {
151 		if (maxrun == 0)
152 			maxrun = ndisks;
153 		if (maxrun > ndisks)
154 			maxrun = ndisks;
155 		nextdisk = diskh.tqh_first;
156 		for (passno = 0; passno < maxrun; ++passno) {
157 			if ((ret = startdisk(nextdisk, chkit)) != 0)
158 				return ret;
159 			nextdisk = nextdisk->d_entries.tqe_next;
160 		}
161 
162 		while ((pid = wait(&status)) != -1) {
163 			for (d = diskh.tqh_first; d; d = d->d_entries.tqe_next)
164 				if (d->d_pid == pid)
165 					break;
166 
167 			if (d == NULL) {
168 				warnx("Unknown pid %d\n", pid);
169 				continue;
170 			}
171 
172 
173 			if (WIFEXITED(status))
174 				retcode = WEXITSTATUS(status);
175 			else
176 				retcode = 0;
177 
178 			p = d->d_part.tqh_first;
179 
180 			if (debug || verbose)
181 				(void) printf("done %s(%s) = %x\n", p->p_name,
182 				    p->p_type, status);
183 
184 			if (WIFSIGNALED(status)) {
185 				(void) fprintf(stderr,
186 				    "%s (%s): EXITED WITH SIGNAL %d\n",
187 				    p->p_name, p->p_type, WTERMSIG(status));
188 				retcode = 8;
189 			}
190 
191 			TAILQ_REMOVE(&d->d_part, p, p_entries);
192 
193 			if (retcode != 0) {
194 				TAILQ_INSERT_TAIL(&badh, p, p_entries);
195 				sumstatus |= retcode;
196 			} else {
197 				free(p->p_type);
198 				free(p->p_name);
199 				free(p);
200 			}
201 			d->d_pid = 0;
202 			nrun--;
203 
204 			if (d->d_part.tqh_first == NULL)
205 				ndisks--;
206 
207 			if (nextdisk == NULL) {
208 				if (d->d_part.tqh_first) {
209 					if ((ret = startdisk(d, chkit)) != 0)
210 						return ret;
211 				}
212 			} else if (nrun < maxrun && nrun < ndisks) {
213 				for ( ;; ) {
214 					nextdisk = nextdisk->d_entries.tqe_next;
215 					if (nextdisk == NULL)
216 						nextdisk = diskh.tqh_first;
217 					if (nextdisk->d_part.tqh_first != NULL
218 					    && nextdisk->d_pid == 0)
219 						break;
220 				}
221 				if ((ret = startdisk(nextdisk, chkit)) != 0)
222 					return ret;
223 			}
224 		}
225 	}
226 	if (sumstatus) {
227 		p = badh.tqh_first;
228 		if (p == NULL)
229 			return (sumstatus);
230 
231 		(void) fprintf(stderr,
232 			"THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
233 			p->p_entries.tqe_next ? "S" : "",
234 			"UNEXPECTED INCONSISTENCY:");
235 
236 		for (; p; p = p->p_entries.tqe_next)
237 			(void) fprintf(stderr,
238 			    "%s (%s)%s", p->p_name, p->p_type,
239 			    p->p_entries.tqe_next ? ", " : "\n");
240 
241 		return sumstatus;
242 	}
243 	(void) endfsent();
244 	return (0);
245 }
246 
247 
248 static struct diskentry *
249 finddisk(name)
250 	const char *name;
251 {
252 	const char *p;
253 	size_t len;
254 	struct diskentry *d;
255 
256 	for (p = name + strlen(name) - 1; p >= name; --p)
257 		if (isdigit(*p)) {
258 			len = p - name + 1;
259 			break;
260 		}
261 
262 	if (p < name)
263 		len = strlen(name);
264 
265 	for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next)
266 		if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
267 			return d;
268 
269 	d = emalloc(sizeof(*d));
270 	d->d_name = estrdup(name);
271 	d->d_name[len] = '\0';
272 	TAILQ_INIT(&d->d_part);
273 	d->d_pid = 0;
274 
275 	TAILQ_INSERT_TAIL(&diskh, d, d_entries);
276 	ndisks++;
277 
278 	return d;
279 }
280 
281 
282 #ifdef DEBUG
283 static void
284 printpart()
285 {
286 	struct diskentry *d;
287 	struct partentry *p;
288 
289 	for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next) {
290 		(void) printf("disk %s: ", d->d_name);
291 		for (p = d->d_part.tqh_first; p != NULL;
292 		    p = p->p_entries.tqe_next)
293 			(void) printf("%s ", p->p_name);
294 		(void) printf("\n");
295 	}
296 }
297 #endif
298 
299 
300 static void
301 addpart(type, name)
302 	const char *type, *name;
303 {
304 	struct diskentry *d = finddisk(name);
305 	struct partentry *p;
306 
307 	for (p = d->d_part.tqh_first; p != NULL; p = p->p_entries.tqe_next)
308 		if (strcmp(p->p_name, name) == 0) {
309 			warnx("%s in fstab more than once!\n", name);
310 			return;
311 		}
312 
313 	p = emalloc(sizeof(*p));
314 	p->p_name = estrdup(name);
315 	p->p_type = estrdup(type);
316 
317 	TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
318 }
319 
320 
321 static int
322 startdisk(d, checkit)
323 	register struct diskentry *d;
324 	int (*checkit) __P((const char *, const char *, pid_t *));
325 {
326 	register struct partentry *p = d->d_part.tqh_first;
327 	int rv;
328 
329 	while ((rv = (*checkit)(p->p_type, p->p_name, &d->d_pid)) != 0 &&
330 	    nrun > 0)
331 		sleep(10);
332 
333 	if (rv == 0)
334 		nrun++;
335 
336 	return rv;
337 }
338