1 /* $NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft 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 * $FreeBSD$ 36 */ 37 38 #include <sys/cdefs.h> 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95"; 42 #else 43 __RCSID("$NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $"); 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/stat.h> 49 #include <sys/wait.h> 50 #include <sys/queue.h> 51 52 #include <err.h> 53 #include <ctype.h> 54 #include <fstab.h> 55 #include <string.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 60 #include "fsutil.h" 61 62 struct partentry { 63 TAILQ_ENTRY(partentry) p_entries; 64 char *p_devname; /* device name */ 65 char *p_mntpt; /* mount point */ 66 char *p_type; /* filesystem type */ 67 void *p_auxarg; /* auxiliary argument */ 68 }; 69 70 TAILQ_HEAD(part, partentry) badh; 71 72 struct diskentry { 73 TAILQ_ENTRY(diskentry) d_entries; 74 char *d_name; /* disk base name */ 75 TAILQ_HEAD(prt, partentry) d_part; /* list of partitions on disk */ 76 int d_pid; /* 0 or pid of fsck proc */ 77 }; 78 79 TAILQ_HEAD(disk, diskentry) diskh; 80 81 static int nrun = 0, ndisks = 0; 82 83 static struct diskentry *finddisk __P((const char *)); 84 static void addpart __P((const char *, const char *, const char *, void *)); 85 static int startdisk __P((struct diskentry *, 86 int (*)(const char *, const char *, const char *, void *, pid_t *))); 87 static void printpart __P((void)); 88 89 int 90 checkfstab(flags, maxrun, docheck, checkit) 91 int flags, maxrun; 92 void *(*docheck) __P((struct fstab *)); 93 int (*checkit) __P((const char *, const char *, const char *, void *, 94 pid_t *)); 95 { 96 struct fstab *fs; 97 struct diskentry *d, *nextdisk; 98 struct partentry *p; 99 int ret, pid, retcode, passno, sumstatus, status; 100 void *auxarg; 101 const char *name; 102 103 TAILQ_INIT(&badh); 104 TAILQ_INIT(&diskh); 105 106 sumstatus = 0; 107 108 for (passno = 1; passno <= 2; passno++) { 109 if (setfsent() == 0) { 110 warnx("Can't open checklist file: %s\n", _PATH_FSTAB); 111 return (8); 112 } 113 while ((fs = getfsent()) != 0) { 114 if ((auxarg = (*docheck)(fs)) == NULL) 115 continue; 116 117 /* XXX We don't need to search for blockdevs .. */ 118 /* name = blockcheck(fs->fs_spec); */ 119 name = fs->fs_spec; 120 if (flags & CHECK_DEBUG) 121 printf("pass %d, name %s\n", passno, name); 122 123 if ((flags & CHECK_PREEN) == 0 || 124 (passno == 1 && fs->fs_passno == 1)) { 125 if (name == NULL) { 126 if (flags & CHECK_PREEN) 127 return 8; 128 else 129 continue; 130 } 131 sumstatus = (*checkit)(fs->fs_vfstype, 132 name, fs->fs_file, auxarg, NULL); 133 134 if (sumstatus) 135 return (sumstatus); 136 } else if (passno == 2 && fs->fs_passno > 1) { 137 if (name == NULL) { 138 (void) fprintf(stderr, 139 "BAD DISK NAME %s\n", fs->fs_spec); 140 sumstatus |= 8; 141 continue; 142 } 143 addpart(fs->fs_vfstype, name, fs->fs_file, 144 auxarg); 145 } 146 } 147 if ((flags & CHECK_PREEN) == 0) 148 return 0; 149 } 150 151 if (flags & CHECK_DEBUG) 152 printpart(); 153 154 if (flags & CHECK_PREEN) { 155 if (maxrun == 0) 156 maxrun = ndisks; 157 if (maxrun > ndisks) 158 maxrun = ndisks; 159 nextdisk = TAILQ_FIRST(&diskh); 160 for (passno = 0; passno < maxrun; ++passno) { 161 if ((ret = startdisk(nextdisk, checkit)) != 0) 162 return ret; 163 nextdisk = TAILQ_NEXT(nextdisk, d_entries); 164 } 165 166 while ((pid = wait(&status)) != -1) { 167 TAILQ_FOREACH(d, &diskh, d_entries) 168 if (d->d_pid == pid) 169 break; 170 171 if (d == NULL) { 172 warnx("Unknown pid %d\n", pid); 173 continue; 174 } 175 176 177 if (WIFEXITED(status)) 178 retcode = WEXITSTATUS(status); 179 else 180 retcode = 0; 181 182 p = TAILQ_FIRST(&d->d_part); 183 184 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) 185 (void) printf("done %s: %s (%s) = 0x%x\n", 186 p->p_type, p->p_devname, p->p_mntpt, 187 status); 188 189 if (WIFSIGNALED(status)) { 190 (void) fprintf(stderr, 191 "%s: %s (%s): EXITED WITH SIGNAL %d\n", 192 p->p_type, p->p_devname, p->p_mntpt, 193 WTERMSIG(status)); 194 retcode = 8; 195 } 196 197 TAILQ_REMOVE(&d->d_part, p, p_entries); 198 199 if (retcode != 0) { 200 TAILQ_INSERT_TAIL(&badh, p, p_entries); 201 sumstatus |= retcode; 202 } else { 203 free(p->p_type); 204 free(p->p_devname); 205 free(p); 206 } 207 d->d_pid = 0; 208 nrun--; 209 210 if (TAILQ_EMPTY(&d->d_part)) 211 ndisks--; 212 213 if (nextdisk == NULL) { 214 if (!TAILQ_EMPTY(&d->d_part)) { 215 if ((ret = startdisk(d, checkit)) != 0) 216 return ret; 217 } 218 } else if (nrun < maxrun && nrun < ndisks) { 219 for ( ;; ) { 220 nextdisk = TAILQ_NEXT(nextdisk, d_entries); 221 if (nextdisk == NULL) 222 nextdisk = TAILQ_FIRST(&diskh); 223 if (!TAILQ_EMPTY(&nextdisk->d_part) 224 && nextdisk->d_pid == 0) 225 break; 226 } 227 if ((ret = startdisk(nextdisk, checkit)) != 0) 228 return ret; 229 } 230 } 231 } 232 if (sumstatus) { 233 p = TAILQ_FIRST(&badh); 234 if (p == NULL) 235 return (sumstatus); 236 237 (void) fprintf(stderr, 238 "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t", 239 TAILQ_NEXT(p, p_entries) ? "S" : "", 240 "UNEXPECTED INCONSISTENCY:"); 241 242 for (; p; p = TAILQ_NEXT(p, p_entries)) 243 (void) fprintf(stderr, 244 "%s: %s (%s)%s", p->p_type, p->p_devname, 245 p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n"); 246 247 return sumstatus; 248 } 249 (void) endfsent(); 250 return (0); 251 } 252 253 254 static struct diskentry * 255 finddisk(name) 256 const char *name; 257 { 258 const char *p; 259 size_t len = 0; 260 struct diskentry *d; 261 262 p = strrchr(name, '/'); 263 if (p == NULL) 264 p = name; 265 else 266 p++; 267 for (; *p && !isdigit(*p); p++) 268 continue; 269 for (; *p && isdigit(*p); p++) 270 continue; 271 len = p - name; 272 if (len == 0) 273 len = strlen(name); 274 275 TAILQ_FOREACH(d, &diskh, d_entries) 276 if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0) 277 return d; 278 279 d = emalloc(sizeof(*d)); 280 d->d_name = estrdup(name); 281 d->d_name[len] = '\0'; 282 TAILQ_INIT(&d->d_part); 283 d->d_pid = 0; 284 285 TAILQ_INSERT_TAIL(&diskh, d, d_entries); 286 ndisks++; 287 288 return d; 289 } 290 291 292 static void 293 printpart() 294 { 295 struct diskentry *d; 296 struct partentry *p; 297 298 TAILQ_FOREACH(d, &diskh, d_entries) { 299 (void) printf("disk %s: ", d->d_name); 300 TAILQ_FOREACH(p, &d->d_part, p_entries) 301 (void) printf("%s ", p->p_devname); 302 (void) printf("\n"); 303 } 304 } 305 306 307 static void 308 addpart(type, devname, mntpt, auxarg) 309 const char *type, *devname, *mntpt; 310 void *auxarg; 311 { 312 struct diskentry *d = finddisk(devname); 313 struct partentry *p; 314 315 TAILQ_FOREACH(p, &d->d_part, p_entries) 316 if (strcmp(p->p_devname, devname) == 0) { 317 warnx("%s in fstab more than once!\n", devname); 318 return; 319 } 320 321 p = emalloc(sizeof(*p)); 322 p->p_devname = estrdup(devname); 323 p->p_mntpt = estrdup(mntpt); 324 p->p_type = estrdup(type); 325 p->p_auxarg = auxarg; 326 327 TAILQ_INSERT_TAIL(&d->d_part, p, p_entries); 328 } 329 330 331 static int 332 startdisk(d, checkit) 333 struct diskentry *d; 334 int (*checkit) __P((const char *, const char *, const char *, void *, 335 pid_t *)); 336 { 337 struct partentry *p = TAILQ_FIRST(&d->d_part); 338 int rv; 339 340 while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt, 341 p->p_auxarg, &d->d_pid)) != 0 && nrun > 0) 342 sleep(10); 343 344 if (rv == 0) 345 nrun++; 346 347 return rv; 348 } 349