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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95"; 33 #endif /* not lint */ 34 #endif 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 #include <sys/wait.h> 41 42 #include <ufs/ufs/dinode.h> 43 #include <ufs/ffs/fs.h> 44 45 #include <ctype.h> 46 #include <errno.h> 47 #include <fstab.h> 48 #include <string.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <unistd.h> 52 53 char *blockcheck(char *origname); 54 55 56 struct part { 57 struct part *next; /* forward link of partitions on disk */ 58 char *name; /* device name */ 59 char *fsname; /* mounted file system name */ 60 long auxdata; /* auxiliary data for application */ 61 } *badlist, **badnext = &badlist; 62 63 struct disk { 64 char *name; /* disk base name */ 65 struct disk *next; /* forward link for list of disks */ 66 struct part *part; /* head of list of partitions on disk */ 67 int pid; /* If != 0, pid of proc working on */ 68 } *disks; 69 70 int nrun, ndisks; 71 72 static void addpart(char *name, char *fsname, long auxdata); 73 static struct disk *finddisk(char *name); 74 static int startdisk(struct disk *dk,int (*checkit)(char *, char *, long, int)); 75 76 int 77 checkfstab(int preen, int maxrun, int (*docheck)(struct fstab *), 78 int (*chkit)(char *, char *, long, int)) 79 { 80 struct fstab *fsp; 81 struct disk *dk, *nextdisk; 82 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 || 98 (passno == 1 && fsp->fs_passno == 1)) { 99 if ((name = blockcheck(fsp->fs_spec)) != 0) { 100 if ((sumstatus = (*chkit)(name, 101 fsp->fs_file, auxdata, 0)) != 0) 102 return (sumstatus); 103 } else if (preen) 104 return (8); 105 } else if (passno == 2 && fsp->fs_passno > 1) { 106 if ((name = blockcheck(fsp->fs_spec)) == NULL) { 107 fprintf(stderr, "BAD DISK NAME %s\n", 108 fsp->fs_spec); 109 sumstatus |= 8; 110 continue; 111 } 112 addpart(name, fsp->fs_file, auxdata); 113 } 114 } 115 if (preen == 0) 116 return (0); 117 } 118 if (preen) { 119 if (maxrun == 0) 120 maxrun = ndisks; 121 if (maxrun > ndisks) 122 maxrun = ndisks; 123 nextdisk = disks; 124 for (passno = 0; passno < maxrun; ++passno) { 125 while ((ret = startdisk(nextdisk, chkit)) && nrun > 0) 126 sleep(10); 127 if (ret) 128 return (ret); 129 nextdisk = nextdisk->next; 130 } 131 while ((pid = wait(&status)) != -1) { 132 for (dk = disks; dk; dk = dk->next) 133 if (dk->pid == pid) 134 break; 135 if (dk == 0) { 136 printf("Unknown pid %d\n", pid); 137 continue; 138 } 139 if (WIFEXITED(status)) 140 retcode = WEXITSTATUS(status); 141 else 142 retcode = 0; 143 if (WIFSIGNALED(status)) { 144 printf("%s (%s): EXITED WITH SIGNAL %d\n", 145 dk->part->name, dk->part->fsname, 146 WTERMSIG(status)); 147 retcode = 8; 148 } 149 if (retcode != 0) { 150 sumstatus |= retcode; 151 *badnext = dk->part; 152 badnext = &dk->part->next; 153 dk->part = dk->part->next; 154 *badnext = NULL; 155 } else 156 dk->part = dk->part->next; 157 dk->pid = 0; 158 nrun--; 159 if (dk->part == NULL) 160 ndisks--; 161 162 if (nextdisk == NULL) { 163 if (dk->part) { 164 while ((ret = startdisk(dk, chkit)) && 165 nrun > 0) 166 sleep(10); 167 if (ret) 168 return (ret); 169 } 170 } else if (nrun < maxrun && nrun < ndisks) { 171 for ( ;; ) { 172 if ((nextdisk = nextdisk->next) == NULL) 173 nextdisk = disks; 174 if (nextdisk->part != NULL && 175 nextdisk->pid == 0) 176 break; 177 } 178 while ((ret = startdisk(nextdisk, chkit)) && 179 nrun > 0) 180 sleep(10); 181 if (ret) 182 return (ret); 183 } 184 } 185 } 186 if (sumstatus) { 187 if (badlist == 0) 188 return (sumstatus); 189 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t", 190 badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:"); 191 for (pt = badlist; pt; pt = pt->next) 192 fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname, 193 pt->next ? ", " : "\n"); 194 return (sumstatus); 195 } 196 (void)endfsent(); 197 return (0); 198 } 199 200 static struct disk * 201 finddisk(char *name) 202 { 203 struct disk *dk, **dkp; 204 char *p; 205 size_t len; 206 207 p = strrchr(name, '/'); 208 p = p == NULL ? name : p + 1; 209 while (*p != '\0' && !isdigit((u_char)*p)) 210 p++; 211 while (isdigit((u_char)*p)) 212 p++; 213 len = (size_t)(p - name); 214 for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) { 215 if (strncmp(dk->name, name, len) == 0 && 216 dk->name[len] == 0) 217 return (dk); 218 } 219 if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) { 220 fprintf(stderr, "out of memory"); 221 exit (8); 222 } 223 dk = *dkp; 224 if ((dk->name = malloc(len + 1)) == NULL) { 225 fprintf(stderr, "out of memory"); 226 exit (8); 227 } 228 (void)strncpy(dk->name, name, len); 229 dk->name[len] = '\0'; 230 dk->part = NULL; 231 dk->next = NULL; 232 dk->pid = 0; 233 ndisks++; 234 return (dk); 235 } 236 237 static void 238 addpart(char *name, char *fsname, long auxdata) 239 { 240 struct disk *dk = finddisk(name); 241 struct part *pt, **ppt = &dk->part; 242 243 for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next) 244 if (strcmp(pt->name, name) == 0) { 245 printf("%s in fstab more than once!\n", name); 246 return; 247 } 248 if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) { 249 fprintf(stderr, "out of memory"); 250 exit (8); 251 } 252 pt = *ppt; 253 if ((pt->name = malloc(strlen(name) + 1)) == NULL) { 254 fprintf(stderr, "out of memory"); 255 exit (8); 256 } 257 (void)strcpy(pt->name, name); 258 if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) { 259 fprintf(stderr, "out of memory"); 260 exit (8); 261 } 262 (void)strcpy(pt->fsname, fsname); 263 pt->next = NULL; 264 pt->auxdata = auxdata; 265 } 266 267 static int 268 startdisk(struct disk *dk, int (*checkit)(char *, char *, long, int)) 269 { 270 struct part *pt = dk->part; 271 272 dk->pid = fork(); 273 if (dk->pid < 0) { 274 perror("fork"); 275 return (8); 276 } 277 if (dk->pid == 0) 278 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1)); 279 nrun++; 280 return (0); 281 } 282 283