preen.c (172193) | preen.c (175678) |
---|---|
1/* $NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $ */ 2 |
|
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 --- 11 unchanged lines hidden (view full) --- 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. | 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 unchanged lines hidden (view full) --- 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. |
30 * 31 * $FreeBSD: head/sbin/quotacheck/preen.c 175678 2008-01-26 12:03:26Z mpp $ |
|
28 */ 29 | 32 */ 33 |
30#if 0 | 34#include <sys/cdefs.h> |
31#ifndef lint | 35#ifndef lint |
32static const char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95"; 33#endif /* not lint */ | 36#if 0 37static char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95"; 38#else 39__RCSID("$NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $"); |
34#endif | 40#endif |
35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/sbin/quotacheck/preen.c 172193 2007-09-16 02:59:32Z mpp $"); | 41#endif /* not lint */ |
37 38#include <sys/param.h> 39#include <sys/stat.h> 40#include <sys/wait.h> | 42 43#include <sys/param.h> 44#include <sys/stat.h> 45#include <sys/wait.h> |
46#include <sys/queue.h> |
|
41 | 47 |
42#include <ufs/ufs/dinode.h> 43#include <ufs/ffs/fs.h> 44 | 48#include <err.h> |
45#include <ctype.h> | 49#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 | 50#include <fstab.h> 51#include <string.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <unistd.h> 55 |
53char *blockcheck(char *origname); | 56#include "quotacheck.h" |
54 | 57 |
58struct partentry { 59 TAILQ_ENTRY(partentry) p_entries; 60 char *p_devname; /* device name */ 61 char *p_mntpt; /* mount point */ 62 char *p_type; /* file system type */ 63 struct quotaname *p_quota; /* quota file info ptr */ 64}; |
|
55 | 65 |
56struct part { 57 struct part *next; /* forward link of partitions on disk */ 58 char *name; /* device name */ 59 char *fsname; /* mounted file system name */ 60 void *auxdata; /* auxiliary data for application */ 61} *badlist, **badnext = &badlist; | 66TAILQ_HEAD(part, partentry) badh; |
62 | 67 |
63struct 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; | 68struct diskentry { 69 TAILQ_ENTRY(diskentry) d_entries; 70 char *d_name; /* disk base name */ 71 TAILQ_HEAD(prt, partentry) d_part; /* list of partitions on disk */ 72 int d_pid; /* 0 or pid of fsck proc */ 73}; |
69 | 74 |
70int nrun, ndisks; | 75TAILQ_HEAD(disk, diskentry) diskh; |
71 | 76 |
72static void addpart(char *name, char *fsname, void *auxdata); 73static struct disk *finddisk(char *name); 74static int startdisk(struct disk *dk, 75 int (*checkit)(char *, char *, void *, int)); | 77static struct diskentry *finddisk(const char *); 78static void addpart(const char *, const char *, const char *, 79 struct quotaname *); 80static int startdisk(struct diskentry *); 81extern void *emalloc(size_t); 82extern char *estrdup(const char *); |
76 77int | 83 84int |
78checkfstab(int preen, int maxrun, void *(*docheck)(struct fstab *), 79 int (*chkit)(char *, char *, void *, int)) | 85checkfstab() |
80{ | 86{ |
81 struct fstab *fsp; 82 struct disk *dk, *nextdisk; 83 struct part *pt; 84 int ret, pid, retcode, passno, sumstatus, status; 85 void *auxdata; | 87 struct fstab *fs; 88 struct diskentry *d, *nextdisk; 89 struct partentry *p; 90 int ret, pid, retcode, passno, sumstatus, status, nextpass; |
86 char *name; | 91 char *name; |
92 struct quotaname *qnp; |
|
87 | 93 |
94 TAILQ_INIT(&badh); 95 TAILQ_INIT(&diskh); 96 |
|
88 sumstatus = 0; | 97 sumstatus = 0; |
89 for (passno = 1; passno <= 2; passno++) { | 98 99 nextpass = 0; 100 for (passno = 1; nextpass != INT_MAX; passno = nextpass) { 101 nextpass = INT_MAX; |
90 if (setfsent() == 0) { | 102 if (setfsent() == 0) { |
91 fprintf(stderr, "Can't open checklist file: %s\n", 92 _PATH_FSTAB); | 103 warnx("Can't open checklist file: %s\n", _PATH_FSTAB); |
93 return (8); 94 } | 104 return (8); 105 } |
95 while ((fsp = getfsent()) != 0) { 96 if ((auxdata = (*docheck)(fsp)) == NULL) | 106 while ((fs = getfsent()) != 0) { 107 name = fs->fs_spec; 108 if (fs->fs_passno > passno && fs->fs_passno < nextpass) 109 nextpass = fs->fs_passno; 110 111 if (passno != fs->fs_passno) |
97 continue; | 112 continue; |
98 if (preen == 0 || 99 (passno == 1 && fsp->fs_passno == 1)) { 100 if ((name = blockcheck(fsp->fs_spec)) != 0) { 101 if ((sumstatus = (*chkit)(name, 102 fsp->fs_file, auxdata, 0)) != 0) 103 return (sumstatus); 104 } else if (preen) 105 return (8); 106 } else if (passno == 2 && fsp->fs_passno > 1) { 107 if ((name = blockcheck(fsp->fs_spec)) == NULL) { 108 fprintf(stderr, "BAD DISK NAME %s\n", 109 fsp->fs_spec); 110 sumstatus |= 8; 111 continue; 112 } 113 addpart(name, fsp->fs_file, auxdata); | 113 114 if ((qnp = needchk(fs)) == NULL) 115 continue; 116 117 if (passno == 1) { 118 sumstatus = chkquota(name, fs->fs_file, qnp); 119 120 if (sumstatus) 121 return (sumstatus); 122 continue; 123 } 124 if (name == NULL) { 125 (void) fprintf(stderr, 126 "BAD DISK NAME %s\n", fs->fs_spec); 127 sumstatus |= 8; 128 continue; |
114 } | 129 } |
130 addpart(fs->fs_vfstype, name, fs->fs_file, qnp); |
|
115 } | 131 } |
116 if (preen == 0) 117 return (0); 118 } 119 if (preen) { 120 if (maxrun == 0) 121 maxrun = ndisks; 122 if (maxrun > ndisks) 123 maxrun = ndisks; 124 nextdisk = disks; 125 for (passno = 0; passno < maxrun; ++passno) { 126 while ((ret = startdisk(nextdisk, chkit)) && nrun > 0) 127 sleep(10); 128 if (ret) 129 return (ret); 130 nextdisk = nextdisk->next; | 132 133 if (passno == 1) 134 continue; 135 136 TAILQ_FOREACH(nextdisk, &diskh, d_entries) { 137 if ((ret = startdisk(nextdisk)) != 0) 138 return ret; |
131 } | 139 } |
140 |
|
132 while ((pid = wait(&status)) != -1) { | 141 while ((pid = wait(&status)) != -1) { |
133 for (dk = disks; dk; dk = dk->next) 134 if (dk->pid == pid) | 142 TAILQ_FOREACH(d, &diskh, d_entries) 143 if (d->d_pid == pid) |
135 break; | 144 break; |
136 if (dk == 0) { 137 printf("Unknown pid %d\n", pid); | 145 146 if (d == NULL) { 147 warnx("Unknown pid %d\n", pid); |
138 continue; 139 } | 148 continue; 149 } |
150 |
|
140 if (WIFEXITED(status)) 141 retcode = WEXITSTATUS(status); 142 else 143 retcode = 0; | 151 if (WIFEXITED(status)) 152 retcode = WEXITSTATUS(status); 153 else 154 retcode = 0; |
155 156 p = TAILQ_FIRST(&d->d_part); 157 |
|
144 if (WIFSIGNALED(status)) { | 158 if (WIFSIGNALED(status)) { |
145 printf("%s (%s): EXITED WITH SIGNAL %d\n", 146 dk->part->name, dk->part->fsname, 147 WTERMSIG(status)); | 159 (void) fprintf(stderr, 160 "%s: %s (%s): EXITED WITH SIGNAL %d\n", 161 p->p_type, p->p_devname, p->p_mntpt, 162 WTERMSIG(status)); |
148 retcode = 8; 149 } | 163 retcode = 8; 164 } |
165 166 TAILQ_REMOVE(&d->d_part, p, p_entries); 167 |
|
150 if (retcode != 0) { | 168 if (retcode != 0) { |
169 TAILQ_INSERT_TAIL(&badh, p, p_entries); |
|
151 sumstatus |= retcode; | 170 sumstatus |= retcode; |
152 *badnext = dk->part; 153 badnext = &dk->part->next; 154 dk->part = dk->part->next; 155 *badnext = NULL; 156 } else 157 dk->part = dk->part->next; 158 dk->pid = 0; 159 nrun--; 160 if (dk->part == NULL) 161 ndisks--; | 171 } else { 172 free(p->p_type); 173 free(p->p_devname); 174 free(p); 175 } 176 d->d_pid = 0; |
162 | 177 |
163 if (nextdisk == NULL) { 164 if (dk->part) { 165 while ((ret = startdisk(dk, chkit)) && 166 nrun > 0) 167 sleep(10); 168 if (ret) 169 return (ret); 170 } 171 } else if (nrun < maxrun && nrun < ndisks) { 172 for ( ;; ) { 173 if ((nextdisk = nextdisk->next) == NULL) 174 nextdisk = disks; 175 if (nextdisk->part != NULL && 176 nextdisk->pid == 0) 177 break; 178 } 179 while ((ret = startdisk(nextdisk, chkit)) && 180 nrun > 0) 181 sleep(10); 182 if (ret) 183 return (ret); | 178 if (TAILQ_EMPTY(&d->d_part)) { 179 TAILQ_REMOVE(&diskh, d, d_entries); 180 } else { 181 if ((ret = startdisk(d)) != 0) 182 return ret; |
184 } 185 } 186 } | 183 } 184 } 185 } |
186 |
|
187 if (sumstatus) { | 187 if (sumstatus) { |
188 if (badlist == 0) | 188 p = TAILQ_FIRST(&badh); 189 if (p == NULL) |
189 return (sumstatus); | 190 return (sumstatus); |
190 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t", 191 badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:"); 192 for (pt = badlist; pt; pt = pt->next) 193 fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname, 194 pt->next ? ", " : "\n"); 195 return (sumstatus); | 191 192 (void) fprintf(stderr, 193 "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t", 194 TAILQ_NEXT(p, p_entries) ? "S" : "", 195 "UNEXPECTED INCONSISTENCY:"); 196 197 for (; p; p = TAILQ_NEXT(p, p_entries)) 198 (void) fprintf(stderr, 199 "%s: %s (%s)%s", p->p_type, p->p_devname, 200 p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n"); 201 202 return sumstatus; |
196 } | 203 } |
197 (void)endfsent(); | 204 (void) endfsent(); |
198 return (0); 199} 200 | 205 return (0); 206} 207 |
201static struct disk * 202finddisk(char *name) | 208 209static struct diskentry * 210finddisk(const char *name) |
203{ | 211{ |
204 struct disk *dk, **dkp; 205 char *p; 206 size_t len; | 212 const char *p; 213 size_t len = 0; 214 struct diskentry *d; |
207 208 p = strrchr(name, '/'); | 215 216 p = strrchr(name, '/'); |
209 p = p == NULL ? name : p + 1; 210 while (*p != '\0' && !isdigit((u_char)*p)) | 217 if (p == NULL) 218 p = name; 219 else |
211 p++; | 220 p++; |
212 while (isdigit((u_char)*p)) 213 p++; 214 len = (size_t)(p - name); 215 for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) { 216 if (strncmp(dk->name, name, len) == 0 && 217 dk->name[len] == 0) 218 return (dk); 219 } 220 if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) { 221 fprintf(stderr, "out of memory"); 222 exit (8); 223 } 224 dk = *dkp; 225 if ((dk->name = malloc(len + 1)) == NULL) { 226 fprintf(stderr, "out of memory"); 227 exit (8); 228 } 229 (void)strncpy(dk->name, name, len); 230 dk->name[len] = '\0'; 231 dk->part = NULL; 232 dk->next = NULL; 233 dk->pid = 0; 234 ndisks++; 235 return (dk); | 221 for (; *p && !isdigit(*p); p++) 222 continue; 223 for (; *p && isdigit(*p); p++) 224 continue; 225 len = p - name; 226 if (len == 0) 227 len = strlen(name); 228 229 TAILQ_FOREACH(d, &diskh, d_entries) 230 if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0) 231 return d; 232 233 d = emalloc(sizeof(*d)); 234 d->d_name = estrdup(name); 235 d->d_name[len] = '\0'; 236 TAILQ_INIT(&d->d_part); 237 d->d_pid = 0; 238 239 TAILQ_INSERT_TAIL(&diskh, d, d_entries); 240 241 return d; |
236} 237 238static void | 242} 243 244static void |
239addpart(char *name, char *fsname, void *auxdata) | 245addpart(const char *type, const char *devname, const char *mntpt, 246 struct quotaname *qnp) |
240{ | 247{ |
241 struct disk *dk = finddisk(name); 242 struct part *pt, **ppt = &dk->part; | 248 struct diskentry *d = finddisk(devname); 249 struct partentry *p; |
243 | 250 |
244 for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next) 245 if (strcmp(pt->name, name) == 0) { 246 printf("%s in fstab more than once!\n", name); | 251 TAILQ_FOREACH(p, &d->d_part, p_entries) 252 if (strcmp(p->p_devname, devname) == 0) { 253 warnx("%s in fstab more than once!\n", devname); |
247 return; 248 } | 254 return; 255 } |
249 if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) { 250 fprintf(stderr, "out of memory"); 251 exit (8); 252 } 253 pt = *ppt; 254 if ((pt->name = malloc(strlen(name) + 1)) == NULL) { 255 fprintf(stderr, "out of memory"); 256 exit (8); 257 } 258 (void)strcpy(pt->name, name); 259 if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) { 260 fprintf(stderr, "out of memory"); 261 exit (8); 262 } 263 (void)strcpy(pt->fsname, fsname); 264 pt->next = NULL; 265 pt->auxdata = auxdata; | 256 257 p = emalloc(sizeof(*p)); 258 p->p_devname = estrdup(devname); 259 p->p_mntpt = estrdup(mntpt); 260 p->p_type = estrdup(type); 261 p->p_quota = qnp; 262 263 TAILQ_INSERT_TAIL(&d->d_part, p, p_entries); |
266} 267 | 264} 265 |
266 |
|
268static int | 267static int |
269startdisk(struct disk *dk, int (*checkit)(char *, char *, void *, int)) | 268startdisk(struct diskentry *d) |
270{ | 269{ |
271 struct part *pt = dk->part; | 270 struct partentry *p = TAILQ_FIRST(&d->d_part); |
272 | 271 |
273 dk->pid = fork(); 274 if (dk->pid < 0) { | 272 d->d_pid = fork(); 273 if (d->d_pid < 0) { |
275 perror("fork"); 276 return (8); 277 } | 274 perror("fork"); 275 return (8); 276 } |
278 if (dk->pid == 0) 279 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1)); 280 nrun++; | 277 if (d->d_pid == 0) 278 exit(chkquota(p->p_devname, p->p_mntpt, p->p_quota)); |
281 return (0); 282} | 279 return (0); 280} |
283 | |