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