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