1 /* $NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1990, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
36 #include <sys/cdefs.h>
39 static char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95";
41 __RCSID("$NetBSD: preen.c,v 1.18 1998/07/26 20:02:36 mycroft Exp $");
45 #include <sys/param.h>
48 #include <sys/queue.h>
61 TAILQ_ENTRY(partentry) p_entries;
62 char *p_devname; /* device name */
63 char *p_mntpt; /* mount point */
64 char *p_type; /* file system type */
65 int p_failok; /* failok option set */
68 static TAILQ_HEAD(part, partentry) badh;
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 */
77 static TAILQ_HEAD(disk, diskentry) diskh;
79 static int nrun = 0, ndisks = 0;
81 static struct diskentry *finddisk(const char *);
82 static void addpart(const char *, const char *, const char *, const int);
83 static int startdisk(struct diskentry *,
84 int (*)(const char *, const char *, const char *, const char *, pid_t *));
85 static void printpart(void);
88 checkfstab(int flags, int (*docheck)(struct fstab *),
89 int (*checkit)(const char *, const char *, const char *, const char *, pid_t *))
92 struct diskentry *d, *nextdisk;
94 int ret, pid, retcode, passno, sumstatus, status, nextpass;
103 for (passno = 1; nextpass != INT_MAX; passno = nextpass) {
104 if (flags & CHECK_DEBUG)
105 printf("pass %d\n", passno);
108 if (setfsent() == 0) {
109 warnx("Can't open checklist file: %s\n", _PATH_FSTAB);
112 while ((fs = getfsent()) != NULL) {
114 if (fs->fs_passno > passno && fs->fs_passno < nextpass)
115 nextpass = fs->fs_passno;
117 if (passno != fs->fs_passno)
120 if ((*docheck)(fs) == 0)
123 if (flags & CHECK_DEBUG)
124 printf("pass %d, name %s\n", passno, name);
126 if ((flags & CHECK_PREEN) == 0 || passno == 1 ||
127 (flags & DO_BACKGRD) != 0) {
129 if (flags & CHECK_PREEN)
134 sumstatus = (*checkit)(fs->fs_vfstype,
135 name, fs->fs_file, NULL, NULL);
141 (void) fprintf(stderr,
142 "BAD DISK NAME %s\n", fs->fs_spec);
146 addpart(fs->fs_vfstype, name, fs->fs_file,
147 getfsopt(fs, "failok"));
150 if ((flags & CHECK_PREEN) == 0 || passno == 1 ||
151 (flags & DO_BACKGRD) != 0)
154 if (flags & CHECK_DEBUG) {
155 printf("Parallel start\n");
159 TAILQ_FOREACH(nextdisk, &diskh, d_entries) {
160 if ((ret = startdisk(nextdisk, checkit)) != 0)
164 if (flags & CHECK_DEBUG)
165 printf("Parallel wait\n");
166 while ((pid = wait(&status)) != -1) {
167 TAILQ_FOREACH(d, &diskh, d_entries)
172 warnx("Unknown pid %d\n", pid);
176 p = TAILQ_FIRST(&d->d_part);
178 if (WIFEXITED(status) == 0) {
180 } else if (p->p_failok == 0) {
181 retcode = WEXITSTATUS(status);
184 fprintf(stderr, "%s: failok SPECIFIED, FSCK "
185 "ERROR(S) IGNORED\n", p->p_devname);
188 if (flags & (CHECK_DEBUG|CHECK_VERBOSE))
189 (void) printf("done %s: %s (%s) = 0x%x\n",
190 p->p_type, p->p_devname, p->p_mntpt,
193 if (WIFSIGNALED(status)) {
194 (void) fprintf(stderr,
195 "%s: %s (%s): EXITED WITH SIGNAL %d\n",
196 p->p_type, p->p_devname, p->p_mntpt,
201 TAILQ_REMOVE(&d->d_part, p, p_entries);
204 TAILQ_INSERT_TAIL(&badh, p, p_entries);
205 sumstatus |= retcode;
214 if (TAILQ_EMPTY(&d->d_part)) {
215 TAILQ_REMOVE(&diskh, d, d_entries);
218 if ((ret = startdisk(d, checkit)) != 0)
222 if (flags & CHECK_DEBUG) {
223 printf("Parallel end\n");
228 if (!(flags & CHECK_PREEN))
232 p = TAILQ_FIRST(&badh);
236 (void) fprintf(stderr,
237 "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
238 TAILQ_NEXT(p, p_entries) ? "S" : "",
239 "UNEXPECTED INCONSISTENCY:");
241 for (; p; p = TAILQ_NEXT(p, p_entries))
242 (void) fprintf(stderr,
243 "%s: %s (%s)%s", p->p_type, p->p_devname,
244 p->p_mntpt, TAILQ_NEXT(p, p_entries) ? ", " : "\n");
252 static struct diskentry *
253 finddisk(const char *name)
259 p = strrchr(name, '/');
264 for (; *p && !isdigit(*p); p++)
266 for (; *p && isdigit(*p); p++)
272 TAILQ_FOREACH(d, &diskh, d_entries)
273 if (strncmp(d->d_name, name, len) == 0 && d->d_name[len] == 0)
276 d = emalloc(sizeof(*d));
277 d->d_name = estrdup(name);
278 d->d_name[len] = '\0';
279 TAILQ_INIT(&d->d_part);
282 TAILQ_INSERT_TAIL(&diskh, d, d_entries);
295 TAILQ_FOREACH(d, &diskh, d_entries) {
296 (void) printf("disk %s: ", d->d_name);
297 TAILQ_FOREACH(p, &d->d_part, p_entries)
298 (void) printf("%s ", p->p_devname);
305 addpart(const char *type, const char *dev, const char *mntpt, const int failok)
307 struct diskentry *d = finddisk(dev);
310 TAILQ_FOREACH(p, &d->d_part, p_entries)
311 if (strcmp(p->p_devname, dev) == 0) {
312 warnx("%s in fstab more than once!\n", dev);
316 p = emalloc(sizeof(*p));
317 p->p_devname = estrdup(dev);
318 p->p_mntpt = estrdup(mntpt);
319 p->p_type = estrdup(type);
320 p->p_failok = failok;
322 TAILQ_INSERT_TAIL(&d->d_part, p, p_entries);
327 startdisk(struct diskentry *d, int (*checkit)(const char *, const char *,
328 const char *, const char *, pid_t *))
330 struct partentry *p = TAILQ_FIRST(&d->d_part);
333 while ((rv = (*checkit)(p->p_type, p->p_devname, p->p_mntpt,
334 NULL, &d->d_pid)) != 0 && nrun > 0)