xref: /netbsd-src/sbin/fsck/preen.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: preen.c,v 1.11 1995/03/18 14:55:59 cgd 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.11 1995/03/18 14:55:59 cgd 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 <fstab.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <ctype.h>
52 #include <unistd.h>
53 
54 struct part {
55 	struct	part *next;		/* forward link of partitions on disk */
56 	char	*name;			/* device name */
57 	char	*fsname;		/* mounted filesystem name */
58 	long	auxdata;		/* auxillary data for application */
59 } *badlist, **badnext = &badlist;
60 
61 struct disk {
62 	char	*name;			/* disk base name */
63 	struct	disk *next;		/* forward link for list of disks */
64 	struct	part *part;		/* head of list of partitions on disk */
65 	int	pid;			/* If != 0, pid of proc working on */
66 } *disks;
67 
68 int	nrun, ndisks;
69 char	hotroot;
70 
71 char	*rawname(), *unrawname(), *blockcheck();
72 void addpart __P((char *, char *, long));
73 int startdisk __P((struct disk *, int (*)() ));
74 
75 int
76 checkfstab(preen, maxrun, docheck, chkit)
77 	int preen, maxrun;
78 	int (*docheck)(), (*chkit)();
79 {
80 	register struct fstab *fsp;
81 	register struct disk *dk, *nextdisk;
82 	register struct part *pt;
83 	int ret, pid, retcode, passno, sumstatus, status;
84 	long auxdata;
85 	char *name;
86 
87 	sumstatus = 0;
88 	for (passno = 1; passno <= 2; passno++) {
89 		if (setfsent() == 0) {
90 			fprintf(stderr, "Can't open checklist file: %s\n",
91 			    _PATH_FSTAB);
92 			return (8);
93 		}
94 		while ((fsp = getfsent()) != 0) {
95 			if ((auxdata = (*docheck)(fsp)) == 0)
96 				continue;
97 			if (preen == 0 || passno == 1 && fsp->fs_passno == 1) {
98 				if (name = blockcheck(fsp->fs_spec)) {
99 					if (sumstatus = (*chkit)(name,
100 					    fsp->fs_file, auxdata, 0))
101 						return (sumstatus);
102 				} else if (preen)
103 					return (8);
104 			} else if (passno == 2 && fsp->fs_passno > 1) {
105 				if ((name = blockcheck(fsp->fs_spec)) == NULL) {
106 					fprintf(stderr, "BAD DISK NAME %s\n",
107 						fsp->fs_spec);
108 					sumstatus |= 8;
109 					continue;
110 				}
111 				addpart(name, fsp->fs_file, auxdata);
112 			}
113 		}
114 		if (preen == 0)
115 			return (0);
116 	}
117 	if (preen) {
118 		if (maxrun == 0)
119 			maxrun = ndisks;
120 		if (maxrun > ndisks)
121 			maxrun = ndisks;
122 		nextdisk = disks;
123 		for (passno = 0; passno < maxrun; ++passno) {
124 			while (ret = startdisk(nextdisk, chkit) && nrun > 0)
125 				sleep(10);
126 			if (ret)
127 				return (ret);
128 			nextdisk = nextdisk->next;
129 		}
130 		while ((pid = wait(&status)) != -1) {
131 			for (dk = disks; dk; dk = dk->next)
132 				if (dk->pid == pid)
133 					break;
134 			if (dk == 0) {
135 				printf("Unknown pid %d\n", pid);
136 				continue;
137 			}
138 			if (WIFEXITED(status))
139 				retcode = WEXITSTATUS(status);
140 			else
141 				retcode = 0;
142 			if (WIFSIGNALED(status)) {
143 				printf("%s (%s): EXITED WITH SIGNAL %d\n",
144 					dk->part->name, dk->part->fsname,
145 					WTERMSIG(status));
146 				retcode = 8;
147 			}
148 			if (retcode != 0) {
149 				sumstatus |= retcode;
150 				*badnext = dk->part;
151 				badnext = &dk->part->next;
152 				dk->part = dk->part->next;
153 				*badnext = NULL;
154 			} else
155 				dk->part = dk->part->next;
156 			dk->pid = 0;
157 			nrun--;
158 			if (dk->part == NULL)
159 				ndisks--;
160 
161 			if (nextdisk == NULL) {
162 				if (dk->part) {
163 					while (ret = startdisk(dk, chkit) &&
164 					    nrun > 0)
165 						sleep(10);
166 					if (ret)
167 						return (ret);
168 				}
169 			} else if (nrun < maxrun && nrun < ndisks) {
170 				for ( ;; ) {
171 					if ((nextdisk = nextdisk->next) == NULL)
172 						nextdisk = disks;
173 					if (nextdisk->part != NULL &&
174 					    nextdisk->pid == 0)
175 						break;
176 				}
177 				while (ret = startdisk(nextdisk, chkit) &&
178 				    nrun > 0)
179 					sleep(10);
180 				if (ret)
181 					return (ret);
182 			}
183 		}
184 	}
185 	if (sumstatus) {
186 		if (badlist == 0)
187 			return (sumstatus);
188 		fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
189 			badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
190 		for (pt = badlist; pt; pt = pt->next)
191 			fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
192 			    pt->next ? ", " : "\n");
193 		return (sumstatus);
194 	}
195 	(void)endfsent();
196 	return (0);
197 }
198 
199 struct disk *
200 finddisk(name)
201 	char *name;
202 {
203 	register struct disk *dk, **dkp;
204 	register char *p;
205 	size_t len;
206 
207 	for (p = name + strlen(name) - 1; p >= name; --p)
208 		if (isdigit(*p)) {
209 			len = p - name + 1;
210 			break;
211 		}
212 	if (p < name)
213 		len = strlen(name);
214 
215 	for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
216 		if (strncmp(dk->name, name, len) == 0 &&
217 		    dk->name[len] == 0)
218 			return (dk);
219 	}
220 	if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
221 		fprintf(stderr, "out of memory");
222 		exit (8);
223 	}
224 	dk = *dkp;
225 	if ((dk->name = malloc(len + 1)) == NULL) {
226 		fprintf(stderr, "out of memory");
227 		exit (8);
228 	}
229 	(void)strncpy(dk->name, name, len);
230 	dk->name[len] = '\0';
231 	dk->part = NULL;
232 	dk->next = NULL;
233 	dk->pid = 0;
234 	ndisks++;
235 	return (dk);
236 }
237 
238 void
239 addpart(name, fsname, auxdata)
240 	char *name, *fsname;
241 	long auxdata;
242 {
243 	struct disk *dk = finddisk(name);
244 	register struct part *pt, **ppt = &dk->part;
245 
246 	for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
247 		if (strcmp(pt->name, name) == 0) {
248 			printf("%s in fstab more than once!\n", name);
249 			return;
250 		}
251 	if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
252 		fprintf(stderr, "out of memory");
253 		exit (8);
254 	}
255 	pt = *ppt;
256 	if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
257 		fprintf(stderr, "out of memory");
258 		exit (8);
259 	}
260 	(void)strcpy(pt->name, name);
261 	if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
262 		fprintf(stderr, "out of memory");
263 		exit (8);
264 	}
265 	(void)strcpy(pt->fsname, fsname);
266 	pt->next = NULL;
267 	pt->auxdata = auxdata;
268 }
269 
270 int
271 startdisk(dk, checkit)
272 	register struct disk *dk;
273 	int (*checkit)();
274 {
275 	register struct part *pt = dk->part;
276 
277 	dk->pid = fork();
278 	if (dk->pid < 0) {
279 		perror("fork");
280 		return (8);
281 	}
282 	if (dk->pid == 0)
283 		exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
284 	nrun++;
285 	return (0);
286 }
287 
288 char *
289 blockcheck(origname)
290 	char *origname;
291 {
292 	struct stat stslash, stblock, stchar;
293 	char *newname, *raw;
294 	int retried = 0;
295 
296 	hotroot = 0;
297 	if (stat("/", &stslash) < 0) {
298 		perror("/");
299 		printf("Can't stat root\n");
300 		return (origname);
301 	}
302 	newname = origname;
303 retry:
304 	if (stat(newname, &stblock) < 0) {
305 		perror(newname);
306 		printf("Can't stat %s\n", newname);
307 		return (origname);
308 	}
309 	if (S_ISBLK(stblock.st_mode)) {
310 		if (stslash.st_dev == stblock.st_rdev)
311 			hotroot++;
312 		raw = rawname(newname);
313 		if (stat(raw, &stchar) < 0) {
314 			perror(raw);
315 			printf("Can't stat %s\n", raw);
316 			return (origname);
317 		}
318 		if (S_ISCHR(stchar.st_mode)) {
319 			return (raw);
320 		} else {
321 			printf("%s is not a character device\n", raw);
322 			return (origname);
323 		}
324 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
325 		newname = unrawname(newname);
326 		retried++;
327 		goto retry;
328 	}
329 	/*
330 	 * Not a block or character device, just return name and
331 	 * let the user decide whether to use it.
332 	 */
333 	return (origname);
334 }
335 
336 char *
337 unrawname(name)
338 	char *name;
339 {
340 	char *dp;
341 	struct stat stb;
342 
343 	if ((dp = strrchr(name, '/')) == 0)
344 		return (name);
345 	if (stat(name, &stb) < 0)
346 		return (name);
347 	if (!S_ISCHR(stb.st_mode))
348 		return (name);
349 	if (dp[1] != 'r')
350 		return (name);
351 	(void)strcpy(&dp[1], &dp[2]);
352 	return (name);
353 }
354 
355 char *
356 rawname(name)
357 	char *name;
358 {
359 	static char rawbuf[32];
360 	char *dp;
361 
362 	if ((dp = strrchr(name, '/')) == 0)
363 		return (0);
364 	*dp = 0;
365 	(void)strcpy(rawbuf, name);
366 	*dp = '/';
367 	(void)strcat(rawbuf, "/r");
368 	(void)strcat(rawbuf, &dp[1]);
369 	return (rawbuf);
370 }
371