nlookup.9 - document nlookup_init_root
[dragonfly.git] / sbin / fsck / preen.c
blob466903f9dfe2c1e381b79844990cdc602f6e3860
1 /*
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
31 * SUCH DAMAGE.
33 * @(#)preen.c 8.5 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/fsck/preen.c,v 1.16 1999/12/30 16:32:40 peter Exp $
35 * $DragonFly: src/sbin/fsck/preen.c,v 1.10 2006/10/19 21:22:13 pavalos Exp $
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/wait.h>
42 #include <vfs/ufs/dinode.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <string.h>
49 #include "fsck.h"
51 struct 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;
58 struct 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;
65 int nrun, ndisks;
67 static void addpart(char *name, char *fsname, long auxdata);
68 static struct disk *finddisk(char *name);
69 static int startdisk(struct disk *dk,
70 int (*checkit)(char *, char *, long, int));
72 int
73 checkfstab(int do_preen, int maxrun, int (*docheck)(struct fstab *),
74 int (*chkit)(char *, char *, long, int))
76 struct fstab *fsp;
77 struct disk *dk, *nextdisk;
78 struct part *pt;
79 int ret, pid, retcode, passno, sumstatus, status;
80 long auxdata;
81 char *name;
83 sumstatus = 0;
84 for (passno = 1; passno <= 2; passno++) {
85 if (setfsent() == 0) {
86 fprintf(stderr, "Can't open checklist file: %s\n",
87 _PATH_FSTAB);
88 return (8);
90 while ((fsp = getfsent()) != 0) {
91 if ((auxdata = (*docheck)(fsp)) == 0)
92 continue;
93 if (do_preen == 0 ||
94 (passno == 1 && fsp->fs_passno == 1)) {
95 if ((name = blockcheck(fsp->fs_spec)) != 0) {
96 if ((sumstatus = (*chkit)(name,
97 fsp->fs_file, auxdata, 0)) != 0)
98 return (sumstatus);
99 } else if (do_preen)
100 return (8);
101 } else if (passno == 2 && fsp->fs_passno > 1) {
102 if ((name = blockcheck(fsp->fs_spec)) == NULL) {
103 fprintf(stderr, "BAD DISK NAME %s\n",
104 fsp->fs_spec);
105 sumstatus |= 8;
106 continue;
108 addpart(name, fsp->fs_file, auxdata);
111 if (do_preen == 0)
112 return (0);
114 if (do_preen) {
115 if (maxrun == 0)
116 maxrun = ndisks;
117 if (maxrun > ndisks)
118 maxrun = ndisks;
119 nextdisk = disks;
120 for (passno = 0; passno < maxrun; ++passno) {
121 while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
122 sleep(10);
123 if (ret)
124 return (ret);
125 nextdisk = nextdisk->next;
127 while ((pid = wait(&status)) != -1) {
128 for (dk = disks; dk; dk = dk->next)
129 if (dk->pid == pid)
130 break;
131 if (dk == 0) {
132 printf("Unknown pid %d\n", pid);
133 continue;
135 if (WIFEXITED(status))
136 retcode = WEXITSTATUS(status);
137 else
138 retcode = 0;
139 if (WIFSIGNALED(status)) {
140 printf("%s (%s): EXITED WITH SIGNAL %d\n",
141 dk->part->name, dk->part->fsname,
142 WTERMSIG(status));
143 retcode = 8;
145 if (retcode != 0) {
146 sumstatus |= retcode;
147 *badnext = dk->part;
148 badnext = &dk->part->next;
149 dk->part = dk->part->next;
150 *badnext = NULL;
151 } else
152 dk->part = dk->part->next;
153 dk->pid = 0;
154 nrun--;
155 if (dk->part == NULL)
156 ndisks--;
158 if (nextdisk == NULL) {
159 if (dk->part) {
160 while ((ret = startdisk(dk, chkit)) &&
161 nrun > 0)
162 sleep(10);
163 if (ret)
164 return (ret);
166 } else if (nrun < maxrun && nrun < ndisks) {
167 for ( ;; ) {
168 if ((nextdisk = nextdisk->next) == NULL)
169 nextdisk = disks;
170 if (nextdisk->part != NULL &&
171 nextdisk->pid == 0)
172 break;
174 while ((ret = startdisk(nextdisk, chkit)) &&
175 nrun > 0)
176 sleep(10);
177 if (ret)
178 return (ret);
182 if (sumstatus) {
183 if (badlist == 0)
184 return (sumstatus);
185 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
186 badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
187 for (pt = badlist; pt; pt = pt->next)
188 fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
189 pt->next ? ", " : "\n");
190 return (sumstatus);
192 endfsent();
193 return (0);
196 static struct disk *
197 finddisk(char *name)
199 struct disk *dk, **dkp;
200 char *p;
201 size_t len;
203 p = strrchr(name, '/');
204 p = p == NULL ? name : p + 1;
205 while (*p != '\0' && !isdigit((u_char)*p))
206 p++;
207 while (isdigit((u_char)*p))
208 p++;
209 len = (size_t)(p - name);
210 for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
211 if (strncmp(dk->name, name, len) == 0 &&
212 dk->name[len] == 0)
213 return (dk);
215 if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
216 fprintf(stderr, "out of memory");
217 exit (8);
219 dk = *dkp;
220 if ((dk->name = malloc(len + 1)) == NULL) {
221 fprintf(stderr, "out of memory");
222 exit (8);
224 strncpy(dk->name, name, len);
225 dk->name[len] = '\0';
226 dk->part = NULL;
227 dk->next = NULL;
228 dk->pid = 0;
229 ndisks++;
230 return (dk);
233 static void
234 addpart(char *name, char *fsname, long auxdata)
236 struct disk *dk = finddisk(name);
237 struct part *pt, **ppt = &dk->part;
239 for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
240 if (strcmp(pt->name, name) == 0) {
241 printf("%s in fstab more than once!\n", name);
242 return;
244 if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
245 fprintf(stderr, "out of memory");
246 exit (8);
248 pt = *ppt;
249 if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
250 fprintf(stderr, "out of memory");
251 exit (8);
253 strcpy(pt->name, name);
254 if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
255 fprintf(stderr, "out of memory");
256 exit (8);
258 strcpy(pt->fsname, fsname);
259 pt->next = NULL;
260 pt->auxdata = auxdata;
263 static int
264 startdisk(struct disk *dk, int (*checkit)(char *, char *, long ,int))
266 struct part *pt = dk->part;
268 dk->pid = fork();
269 if (dk->pid < 0) {
270 perror("fork");
271 return (8);
273 if (dk->pid == 0)
274 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
275 nrun++;
276 return (0);
279 char *
280 blockcheck(char *origname)
282 struct stat stblock;
283 char *newname;
284 struct fstab *fsinfo;
285 int retried = 0, len;
287 newname = origname;
288 retry:
289 if (stat(newname, &stblock) < 0) {
290 newname = getdevpath(newname, 0);
291 if (stat(newname, &stblock) < 0) {
292 printf("Can't stat %s: %s\n", newname, strerror(errno));
293 return (origname);
296 switch(stblock.st_mode & S_IFMT) {
297 case S_IFCHR:
298 case S_IFBLK:
299 return(newname);
300 case S_IFREG:
301 return(newname);
302 case S_IFDIR:
303 if (retried)
304 break;
306 len = strlen(origname) - 1;
307 if (len > 0 && origname[len] == '/')
308 /* remove trailing slash */
309 origname[len] = '\0';
310 if ((fsinfo = getfsfile(origname)) == NULL) {
311 printf("Can't resolve %s to character special device",
312 origname);
313 return (0);
315 newname = fsinfo->fs_spec;
316 retried++;
317 goto retry;
320 * Not a block or character device, just return name and
321 * let the user decide whether to use it.
323 return (origname);