preen.c revision 1.14 1 1.14 christos /* $NetBSD: preen.c,v 1.14 1996/09/27 22:38:43 christos Exp $ */
2 1.11 cgd
3 1.1 cgd /*
4 1.6 mycroft * Copyright (c) 1990, 1993
5 1.6 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.11 cgd #if 0
38 1.11 cgd static char sccsid[] = "@(#)preen.c 8.3 (Berkeley) 12/6/94";
39 1.11 cgd #else
40 1.14 christos static char rcsid[] = "$NetBSD: preen.c,v 1.14 1996/09/27 22:38:43 christos Exp $";
41 1.11 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/stat.h>
46 1.1 cgd #include <sys/wait.h>
47 1.13 christos #include <sys/queue.h>
48 1.13 christos
49 1.1 cgd #include <fstab.h>
50 1.1 cgd #include <string.h>
51 1.1 cgd #include <stdio.h>
52 1.1 cgd #include <stdlib.h>
53 1.1 cgd #include <ctype.h>
54 1.9 cgd #include <unistd.h>
55 1.13 christos #include <err.h>
56 1.13 christos
57 1.14 christos #include "fsutil.h"
58 1.1 cgd
59 1.13 christos struct partentry {
60 1.13 christos TAILQ_ENTRY(partentry) p_entries;
61 1.14 christos char *p_devname; /* device name */
62 1.14 christos char *p_mntpt; /* mount point */
63 1.13 christos char *p_type; /* filesystem type */
64 1.13 christos };
65 1.13 christos
66 1.13 christos TAILQ_HEAD(part, partentry) badh;
67 1.13 christos
68 1.13 christos struct diskentry {
69 1.13 christos TAILQ_ENTRY(diskentry) d_entries;
70 1.13 christos char *d_name; /* disk base name */
71 1.13 christos TAILQ_HEAD(prt, partentry) d_part; /* list of partitions on disk */
72 1.13 christos int d_pid; /* 0 or pid of fsck proc */
73 1.13 christos };
74 1.13 christos
75 1.13 christos TAILQ_HEAD(disk, diskentry) diskh;
76 1.13 christos
77 1.13 christos static int nrun = 0, ndisks = 0;
78 1.13 christos
79 1.13 christos static struct diskentry *finddisk __P((const char *));
80 1.14 christos static void addpart __P((const char *, const char *, const char *));
81 1.13 christos static int startdisk __P((struct diskentry *,
82 1.14 christos int (*)(const char *, const char *, const char *, void *)));
83 1.13 christos static void printpart __P((void));
84 1.9 cgd
85 1.9 cgd int
86 1.14 christos checkfstab(flags, maxrun, docheck, chkit)
87 1.14 christos int flags, maxrun;
88 1.14 christos void *(*docheck) __P((struct fstab *));
89 1.14 christos int (*chkit) __P((const char *, const char *, const char *, void *));
90 1.1 cgd {
91 1.13 christos struct fstab *fs;
92 1.13 christos struct diskentry *d, *nextdisk;
93 1.13 christos struct partentry *p;
94 1.1 cgd int ret, pid, retcode, passno, sumstatus, status;
95 1.1 cgd char *name;
96 1.1 cgd
97 1.13 christos TAILQ_INIT(&badh);
98 1.13 christos TAILQ_INIT(&diskh);
99 1.13 christos
100 1.1 cgd sumstatus = 0;
101 1.13 christos
102 1.1 cgd for (passno = 1; passno <= 2; passno++) {
103 1.1 cgd if (setfsent() == 0) {
104 1.13 christos warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
105 1.1 cgd return (8);
106 1.1 cgd }
107 1.13 christos while ((fs = getfsent()) != 0) {
108 1.14 christos if ((*docheck)(fs) == NULL)
109 1.1 cgd continue;
110 1.13 christos
111 1.13 christos name = blockcheck(fs->fs_spec);
112 1.14 christos if (flags & CHECK_DEBUG)
113 1.13 christos printf("pass %d, name %s\n", passno, name);
114 1.13 christos
115 1.14 christos if ((flags & CHECK_PREEN) == 0 ||
116 1.14 christos (passno == 1 && fs->fs_passno == 1)) {
117 1.13 christos if (name == NULL) {
118 1.14 christos if (flags & CHECK_PREEN)
119 1.13 christos return 8;
120 1.13 christos else
121 1.13 christos continue;
122 1.13 christos }
123 1.13 christos sumstatus = (*chkit)(fs->fs_vfstype,
124 1.14 christos name, fs->fs_file, NULL);
125 1.13 christos
126 1.13 christos if (sumstatus)
127 1.13 christos return (sumstatus);
128 1.13 christos } else if (passno == 2 && fs->fs_passno > 1) {
129 1.13 christos if (name == NULL) {
130 1.13 christos (void) fprintf(stderr,
131 1.13 christos "BAD DISK NAME %s\n", fs->fs_spec);
132 1.1 cgd sumstatus |= 8;
133 1.1 cgd continue;
134 1.1 cgd }
135 1.14 christos addpart(fs->fs_vfstype, name, fs->fs_file);
136 1.1 cgd }
137 1.1 cgd }
138 1.14 christos if ((flags & CHECK_PREEN) == 0)
139 1.14 christos return 0;
140 1.1 cgd }
141 1.13 christos
142 1.14 christos if (flags & CHECK_DEBUG)
143 1.13 christos printpart();
144 1.13 christos
145 1.14 christos if (flags & CHECK_PREEN) {
146 1.1 cgd if (maxrun == 0)
147 1.1 cgd maxrun = ndisks;
148 1.1 cgd if (maxrun > ndisks)
149 1.1 cgd maxrun = ndisks;
150 1.13 christos nextdisk = diskh.tqh_first;
151 1.1 cgd for (passno = 0; passno < maxrun; ++passno) {
152 1.13 christos if ((ret = startdisk(nextdisk, chkit)) != 0)
153 1.13 christos return ret;
154 1.13 christos nextdisk = nextdisk->d_entries.tqe_next;
155 1.1 cgd }
156 1.13 christos
157 1.1 cgd while ((pid = wait(&status)) != -1) {
158 1.13 christos for (d = diskh.tqh_first; d; d = d->d_entries.tqe_next)
159 1.13 christos if (d->d_pid == pid)
160 1.1 cgd break;
161 1.13 christos
162 1.13 christos if (d == NULL) {
163 1.13 christos warnx("Unknown pid %d\n", pid);
164 1.1 cgd continue;
165 1.1 cgd }
166 1.13 christos
167 1.13 christos
168 1.1 cgd if (WIFEXITED(status))
169 1.1 cgd retcode = WEXITSTATUS(status);
170 1.1 cgd else
171 1.1 cgd retcode = 0;
172 1.13 christos
173 1.13 christos p = d->d_part.tqh_first;
174 1.13 christos
175 1.14 christos if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
176 1.14 christos (void) printf("done %s: %s (%s) = %x\n",
177 1.14 christos p->p_type, p->p_devname, p->p_mntpt,
178 1.14 christos status);
179 1.13 christos
180 1.1 cgd if (WIFSIGNALED(status)) {
181 1.13 christos (void) fprintf(stderr,
182 1.14 christos "%s: %s (%s): EXITED WITH SIGNAL %d\n",
183 1.14 christos p->p_type, p->p_devname, p->p_mntpt,
184 1.14 christos WTERMSIG(status));
185 1.1 cgd retcode = 8;
186 1.1 cgd }
187 1.13 christos
188 1.13 christos TAILQ_REMOVE(&d->d_part, p, p_entries);
189 1.13 christos
190 1.1 cgd if (retcode != 0) {
191 1.13 christos TAILQ_INSERT_TAIL(&badh, p, p_entries);
192 1.1 cgd sumstatus |= retcode;
193 1.13 christos } else {
194 1.13 christos free(p->p_type);
195 1.14 christos free(p->p_devname);
196 1.13 christos free(p);
197 1.13 christos }
198 1.13 christos d->d_pid = 0;
199 1.1 cgd nrun--;
200 1.13 christos
201 1.13 christos if (d->d_part.tqh_first == NULL)
202 1.1 cgd ndisks--;
203 1.1 cgd
204 1.1 cgd if (nextdisk == NULL) {
205 1.13 christos if (d->d_part.tqh_first) {
206 1.13 christos if ((ret = startdisk(d, chkit)) != 0)
207 1.13 christos return ret;
208 1.1 cgd }
209 1.1 cgd } else if (nrun < maxrun && nrun < ndisks) {
210 1.1 cgd for ( ;; ) {
211 1.13 christos nextdisk = nextdisk->d_entries.tqe_next;
212 1.13 christos if (nextdisk == NULL)
213 1.13 christos nextdisk = diskh.tqh_first;
214 1.13 christos if (nextdisk->d_part.tqh_first != NULL
215 1.13 christos && nextdisk->d_pid == 0)
216 1.1 cgd break;
217 1.1 cgd }
218 1.13 christos if ((ret = startdisk(nextdisk, chkit)) != 0)
219 1.13 christos return ret;
220 1.1 cgd }
221 1.1 cgd }
222 1.1 cgd }
223 1.1 cgd if (sumstatus) {
224 1.13 christos p = badh.tqh_first;
225 1.13 christos if (p == NULL)
226 1.1 cgd return (sumstatus);
227 1.13 christos
228 1.13 christos (void) fprintf(stderr,
229 1.13 christos "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
230 1.13 christos p->p_entries.tqe_next ? "S" : "",
231 1.13 christos "UNEXPECTED INCONSISTENCY:");
232 1.13 christos
233 1.13 christos for (; p; p = p->p_entries.tqe_next)
234 1.13 christos (void) fprintf(stderr,
235 1.14 christos "%s: %s (%s)%s", p->p_type, p->p_devname,
236 1.14 christos p->p_mntpt, p->p_entries.tqe_next ? ", " : "\n");
237 1.13 christos
238 1.13 christos return sumstatus;
239 1.1 cgd }
240 1.13 christos (void) endfsent();
241 1.1 cgd return (0);
242 1.1 cgd }
243 1.1 cgd
244 1.13 christos
245 1.13 christos static struct diskentry *
246 1.1 cgd finddisk(name)
247 1.13 christos const char *name;
248 1.1 cgd {
249 1.13 christos const char *p;
250 1.1 cgd size_t len;
251 1.13 christos struct diskentry *d;
252 1.1 cgd
253 1.1 cgd for (p = name + strlen(name) - 1; p >= name; --p)
254 1.1 cgd if (isdigit(*p)) {
255 1.1 cgd len = p - name + 1;
256 1.1 cgd break;
257 1.1 cgd }
258 1.13 christos
259 1.1 cgd if (p < name)
260 1.1 cgd len = strlen(name);
261 1.1 cgd
262 1.13 christos for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next)
263 1.13 christos if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
264 1.13 christos return d;
265 1.13 christos
266 1.13 christos d = emalloc(sizeof(*d));
267 1.13 christos d->d_name = estrdup(name);
268 1.13 christos d->d_name[len] = '\0';
269 1.13 christos TAILQ_INIT(&d->d_part);
270 1.13 christos d->d_pid = 0;
271 1.13 christos
272 1.13 christos TAILQ_INSERT_TAIL(&diskh, d, d_entries);
273 1.1 cgd ndisks++;
274 1.13 christos
275 1.13 christos return d;
276 1.1 cgd }
277 1.1 cgd
278 1.13 christos
279 1.13 christos static void
280 1.13 christos printpart()
281 1.1 cgd {
282 1.13 christos struct diskentry *d;
283 1.13 christos struct partentry *p;
284 1.1 cgd
285 1.13 christos for (d = diskh.tqh_first; d != NULL; d = d->d_entries.tqe_next) {
286 1.13 christos (void) printf("disk %s: ", d->d_name);
287 1.13 christos for (p = d->d_part.tqh_first; p != NULL;
288 1.13 christos p = p->p_entries.tqe_next)
289 1.14 christos (void) printf("%s ", p->p_devname);
290 1.13 christos (void) printf("\n");
291 1.1 cgd }
292 1.1 cgd }
293 1.1 cgd
294 1.13 christos
295 1.13 christos static void
296 1.14 christos addpart(type, devname, mntpt)
297 1.14 christos const char *type, *devname, *mntpt;
298 1.1 cgd {
299 1.14 christos struct diskentry *d = finddisk(devname);
300 1.13 christos struct partentry *p;
301 1.13 christos
302 1.13 christos for (p = d->d_part.tqh_first; p != NULL; p = p->p_entries.tqe_next)
303 1.14 christos if (strcmp(p->p_devname, devname) == 0) {
304 1.14 christos warnx("%s in fstab more than once!\n", devname);
305 1.13 christos return;
306 1.13 christos }
307 1.1 cgd
308 1.13 christos p = emalloc(sizeof(*p));
309 1.14 christos p->p_devname = estrdup(devname);
310 1.14 christos p->p_mntpt = estrdup(mntpt);
311 1.13 christos p->p_type = estrdup(type);
312 1.13 christos
313 1.13 christos TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd
317 1.13 christos static int
318 1.13 christos startdisk(d, checkit)
319 1.13 christos register struct diskentry *d;
320 1.14 christos int (*checkit) __P((const char *, const char *, const char *, void *));
321 1.1 cgd {
322 1.13 christos register struct partentry *p = d->d_part.tqh_first;
323 1.13 christos int rv;
324 1.1 cgd
325 1.14 christos while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
326 1.14 christos &d->d_pid)) != 0 && nrun > 0)
327 1.13 christos sleep(10);
328 1.1 cgd
329 1.13 christos if (rv == 0)
330 1.13 christos nrun++;
331 1.1 cgd
332 1.13 christos return rv;
333 1.1 cgd }
334