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