preen.c (19212) | preen.c (23675) |
---|---|
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 --- 18 unchanged lines hidden (view full) --- 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 | 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 --- 18 unchanged lines hidden (view full) --- 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 |
35static const char sccsid[] = "@(#)preen.c 8.1 (Berkeley) 6/5/93"; | 35static const char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95"; |
36#endif /* not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <sys/wait.h> 41#include <ufs/ufs/dinode.h> 42#include <fstab.h> 43#include <string.h> 44#include <stdio.h> 45#include <unistd.h> 46#include <stdlib.h> 47#include <ctype.h> | 36#endif /* not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <sys/wait.h> 41#include <ufs/ufs/dinode.h> 42#include <fstab.h> 43#include <string.h> 44#include <stdio.h> 45#include <unistd.h> 46#include <stdlib.h> 47#include <ctype.h> |
48#include <fstab.h> | 48 |
49#include "fsck.h" 50 51struct part { 52 struct part *next; /* forward link of partitions on disk */ 53 char *name; /* device name */ 54 char *fsname; /* mounted filesystem name */ 55 long auxdata; /* auxillary data for application */ 56} *badlist, **badnext = &badlist; 57 58struct disk { 59 char *name; /* disk base name */ 60 struct disk *next; /* forward link for list of disks */ 61 struct part *part; /* head of list of partitions on disk */ 62 int pid; /* If != 0, pid of proc working on */ 63} *disks; 64 | 49#include "fsck.h" 50 51struct part { 52 struct part *next; /* forward link of partitions on disk */ 53 char *name; /* device name */ 54 char *fsname; /* mounted filesystem name */ 55 long auxdata; /* auxillary data for application */ 56} *badlist, **badnext = &badlist; 57 58struct disk { 59 char *name; /* disk base name */ 60 struct disk *next; /* forward link for list of disks */ 61 struct part *part; /* head of list of partitions on disk */ 62 int pid; /* If != 0, pid of proc working on */ 63} *disks; 64 |
65static void addpart __P((char *name, char *fsname, long auxdata)); 66static int startdisk __P((struct disk *dk, int (*checkit)())); 67static struct disk *finddisk __P((char *name)); 68static char *unrawname __P((char *name)); 69static char *rawname __P((char *name)); 70 | |
71int nrun, ndisks; 72char hotroot; 73 | 65int nrun, ndisks; 66char hotroot; 67 |
68static void addpart __P((char *name, char *fsname, long auxdata)); 69static struct disk *finddisk __P((char *name)); 70static char *rawname __P((char *name)); 71static int startdisk __P((struct disk *dk, 72 int (*checkit)(char *, char *, long, int))); 73static char *unrawname __P((char *name)); 74 |
|
74int 75checkfstab(preen, maxrun, docheck, chkit) | 75int 76checkfstab(preen, maxrun, docheck, chkit) |
76 int preen, maxrun; 77 int (*docheck)(), (*chkit)(); | 77 int preen; 78 int maxrun; 79 int (*docheck)(struct fstab *); 80 int (*chkit)(char *, char *, long, int); |
78{ 79 register struct fstab *fsp; 80 register struct disk *dk, *nextdisk; 81 register struct part *pt; 82 int ret, pid, retcode, passno, sumstatus, status; 83 long auxdata; 84 char *name; 85 86 sumstatus = 0; 87 for (passno = 1; passno <= 2; passno++) { 88 if (setfsent() == 0) { 89 fprintf(stderr, "Can't open checklist file: %s\n", 90 _PATH_FSTAB); 91 return (8); 92 } 93 while ((fsp = getfsent()) != 0) { 94 if ((auxdata = (*docheck)(fsp)) == 0) 95 continue; | 81{ 82 register struct fstab *fsp; 83 register struct disk *dk, *nextdisk; 84 register struct part *pt; 85 int ret, pid, retcode, passno, sumstatus, status; 86 long auxdata; 87 char *name; 88 89 sumstatus = 0; 90 for (passno = 1; passno <= 2; passno++) { 91 if (setfsent() == 0) { 92 fprintf(stderr, "Can't open checklist file: %s\n", 93 _PATH_FSTAB); 94 return (8); 95 } 96 while ((fsp = getfsent()) != 0) { 97 if ((auxdata = (*docheck)(fsp)) == 0) 98 continue; |
96 if (!preen || (passno == 1 && fsp->fs_passno == 1)) { 97 name = blockcheck(fsp->fs_spec); 98 if (name) { 99 sumstatus = (*chkit)(name, 100 fsp->fs_file, auxdata, 0); 101 if (sumstatus) | 99 if (preen == 0 || 100 (passno == 1 && fsp->fs_passno == 1)) { 101 if ((name = blockcheck(fsp->fs_spec)) != 0) { 102 if ((sumstatus = (*chkit)(name, 103 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; --- 83 unchanged lines hidden (view full) --- 193 fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname, 194 pt->next ? ", " : "\n"); 195 return (sumstatus); 196 } 197 (void)endfsent(); 198 return (0); 199} 200 | 104 return (sumstatus); 105 } else if (preen) 106 return (8); 107 } else if (passno == 2 && fsp->fs_passno > 1) { 108 if ((name = blockcheck(fsp->fs_spec)) == NULL) { 109 fprintf(stderr, "BAD DISK NAME %s\n", 110 fsp->fs_spec); 111 sumstatus |= 8; --- 83 unchanged lines hidden (view full) --- 195 fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname, 196 pt->next ? ", " : "\n"); 197 return (sumstatus); 198 } 199 (void)endfsent(); 200 return (0); 201} 202 |
201struct disk * | 203static struct disk * |
202finddisk(name) 203 char *name; 204{ 205 register struct disk *dk, **dkp; 206 register char *p; 207 size_t len = 0; 208 | 204finddisk(name) 205 char *name; 206{ 207 register struct disk *dk, **dkp; 208 register char *p; 209 size_t len = 0; 210 |
209 for (p = name + strlen(name) - 1; p >= name; --p) | 211 for (len = strlen(name), p = name + len - 1; p >= name; --p) |
210 if (isdigit(*p)) { 211 len = p - name + 1; 212 break; 213 } | 212 if (isdigit(*p)) { 213 len = p - name + 1; 214 break; 215 } |
214 if (p < name) 215 len = strlen(name); | |
216 217 for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) { 218 if (strncmp(dk->name, name, len) == 0 && 219 dk->name[len] == 0) 220 return (dk); 221 } 222 if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) { 223 fprintf(stderr, "out of memory"); --- 8 unchanged lines hidden (view full) --- 232 dk->name[len] = '\0'; 233 dk->part = NULL; 234 dk->next = NULL; 235 dk->pid = 0; 236 ndisks++; 237 return (dk); 238} 239 | 216 217 for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) { 218 if (strncmp(dk->name, name, len) == 0 && 219 dk->name[len] == 0) 220 return (dk); 221 } 222 if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) { 223 fprintf(stderr, "out of memory"); --- 8 unchanged lines hidden (view full) --- 232 dk->name[len] = '\0'; 233 dk->part = NULL; 234 dk->next = NULL; 235 dk->pid = 0; 236 ndisks++; 237 return (dk); 238} 239 |
240void | 240static void |
241addpart(name, fsname, auxdata) 242 char *name, *fsname; 243 long auxdata; 244{ 245 struct disk *dk = finddisk(name); 246 register struct part *pt, **ppt = &dk->part; 247 248 for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next) --- 15 unchanged lines hidden (view full) --- 264 fprintf(stderr, "out of memory"); 265 exit (8); 266 } 267 (void)strcpy(pt->fsname, fsname); 268 pt->next = NULL; 269 pt->auxdata = auxdata; 270} 271 | 241addpart(name, fsname, auxdata) 242 char *name, *fsname; 243 long auxdata; 244{ 245 struct disk *dk = finddisk(name); 246 register struct part *pt, **ppt = &dk->part; 247 248 for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next) --- 15 unchanged lines hidden (view full) --- 264 fprintf(stderr, "out of memory"); 265 exit (8); 266 } 267 (void)strcpy(pt->fsname, fsname); 268 pt->next = NULL; 269 pt->auxdata = auxdata; 270} 271 |
272int | 272static int |
273startdisk(dk, checkit) 274 register struct disk *dk; | 273startdisk(dk, checkit) 274 register struct disk *dk; |
275 int (*checkit)(); | 275 int (*checkit)(char *, char *, long, int); |
276{ 277 register struct part *pt = dk->part; 278 279 dk->pid = fork(); 280 if (dk->pid < 0) { 281 perror("fork"); 282 return (8); 283 } 284 if (dk->pid == 0) 285 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1)); 286 nrun++; 287 return (0); 288} 289 290char * | 276{ 277 register struct part *pt = dk->part; 278 279 dk->pid = fork(); 280 if (dk->pid < 0) { 281 perror("fork"); 282 return (8); 283 } 284 if (dk->pid == 0) 285 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1)); 286 nrun++; 287 return (0); 288} 289 290char * |
291blockcheck(name) 292 char *name; | 291blockcheck(origname) 292 char *origname; |
293{ 294 struct stat stslash, stblock, stchar; | 293{ 294 struct stat stslash, stblock, stchar; |
295 char *raw; | 295 char *newname, *raw; |
296 struct fstab *fsinfo; 297 int retried = 0, l; 298 299 hotroot = 0; 300 if (stat("/", &stslash) < 0) { 301 perror("/"); 302 printf("Can't stat root\n"); | 296 struct fstab *fsinfo; 297 int retried = 0, l; 298 299 hotroot = 0; 300 if (stat("/", &stslash) < 0) { 301 perror("/"); 302 printf("Can't stat root\n"); |
303 return (0); | 303 return (origname); |
304 } | 304 } |
305 newname = origname; |
|
305retry: | 306retry: |
306 if (stat(name, &stblock) < 0) { 307 perror(name); 308 printf("Can't stat %s\n", name); 309 return (0); | 307 if (stat(newname, &stblock) < 0) { 308 perror(newname); 309 printf("Can't stat %s\n", newname); 310 return (origname); |
310 } 311 if ((stblock.st_mode & S_IFMT) == S_IFBLK) { 312 if (stslash.st_dev == stblock.st_rdev) 313 hotroot++; | 311 } 312 if ((stblock.st_mode & S_IFMT) == S_IFBLK) { 313 if (stslash.st_dev == stblock.st_rdev) 314 hotroot++; |
314 raw = rawname(name); | 315 raw = rawname(newname); |
315 if (stat(raw, &stchar) < 0) { 316 perror(raw); 317 printf("Can't stat %s\n", raw); | 316 if (stat(raw, &stchar) < 0) { 317 perror(raw); 318 printf("Can't stat %s\n", raw); |
318 return (name); | 319 return (origname); |
319 } 320 if ((stchar.st_mode & S_IFMT) == S_IFCHR) { 321 return (raw); 322 } else { 323 printf("%s is not a character device\n", raw); | 320 } 321 if ((stchar.st_mode & S_IFMT) == S_IFCHR) { 322 return (raw); 323 } else { 324 printf("%s is not a character device\n", raw); |
324 return (name); | 325 return (origname); |
325 } 326 } else if ((stblock.st_mode & S_IFMT) == S_IFCHR && !retried) { | 326 } 327 } else if ((stblock.st_mode & S_IFMT) == S_IFCHR && !retried) { |
327 name = unrawname(name); | 328 newname = unrawname(origname); |
328 retried++; 329 goto retry; 330 } else if ((stblock.st_mode & S_IFMT) == S_IFDIR && !retried) { | 329 retried++; 330 goto retry; 331 } else if ((stblock.st_mode & S_IFMT) == S_IFDIR && !retried) { |
331 l = strlen(name) - 1; 332 if (l > 0 && name[l] == '/') | 332 l = strlen(origname) - 1; 333 if (l > 0 && origname[l] == '/') |
333 /* remove trailing slash */ | 334 /* remove trailing slash */ |
334 name[l] = '\0'; 335 if(!(fsinfo=getfsfile(name))) { | 335 origname[l] = '\0'; 336 if(!(fsinfo=getfsfile(origname))) { |
336 printf("Can't resolve %s to character special device", | 337 printf("Can't resolve %s to character special device", |
337 name); | 338 origname); |
338 return (0); 339 } | 339 return (0); 340 } |
340 name = fsinfo->fs_spec; | 341 newname = fsinfo->fs_spec; |
341 retried++; 342 goto retry; 343 } | 342 retried++; 343 goto retry; 344 } |
344 printf("Warning: Can't find blockdevice corresponding to name %s\n", 345 name); 346 return (name); | 345 /* 346 * Not a block or character device, just return name and 347 * let the user decide whether to use it. 348 */ 349 return (origname); |
347} 348 | 350} 351 |
349char * | 352static char * |
350unrawname(name) 351 char *name; 352{ 353 char *dp; 354 struct stat stb; 355 | 353unrawname(name) 354 char *name; 355{ 356 char *dp; 357 struct stat stb; 358 |
356 if ((dp = rindex(name, '/')) == 0) | 359 if ((dp = strrchr(name, '/')) == 0) |
357 return (name); 358 if (stat(name, &stb) < 0) 359 return (name); 360 if ((stb.st_mode & S_IFMT) != S_IFCHR) 361 return (name); 362 if (dp[1] != 'r') 363 return (name); 364 (void)strcpy(&dp[1], &dp[2]); 365 return (name); 366} 367 | 360 return (name); 361 if (stat(name, &stb) < 0) 362 return (name); 363 if ((stb.st_mode & S_IFMT) != S_IFCHR) 364 return (name); 365 if (dp[1] != 'r') 366 return (name); 367 (void)strcpy(&dp[1], &dp[2]); 368 return (name); 369} 370 |
368char * | 371static char * |
369rawname(name) 370 char *name; 371{ 372 static char rawbuf[32]; 373 char *dp; 374 | 372rawname(name) 373 char *name; 374{ 375 static char rawbuf[32]; 376 char *dp; 377 |
375 if ((dp = rindex(name, '/')) == 0) | 378 if ((dp = strrchr(name, '/')) == 0) |
376 return (0); 377 *dp = 0; 378 (void)strcpy(rawbuf, name); 379 *dp = '/'; 380 (void)strcat(rawbuf, "/r"); 381 (void)strcat(rawbuf, &dp[1]); 382 return (rawbuf); 383} | 379 return (0); 380 *dp = 0; 381 (void)strcpy(rawbuf, name); 382 *dp = '/'; 383 (void)strcat(rawbuf, "/r"); 384 (void)strcat(rawbuf, &dp[1]); 385 return (rawbuf); 386} |