xref: /freebsd/sbin/quotacheck/preen.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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
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.
20  *
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.
32  */
33 
34 #if 0
35 #ifndef lint
36 static const char sccsid[] = "@(#)preen.c	8.5 (Berkeley) 4/28/95";
37 #endif /* not lint */
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45 
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ffs/fs.h>
48 
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fstab.h>
52 #include <string.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 
57 char *blockcheck(char *origname);
58 
59 
60 struct part {
61 	struct	part *next;		/* forward link of partitions on disk */
62 	char	*name;			/* device name */
63 	char	*fsname;		/* mounted file system name */
64 	long	auxdata;		/* auxiliary data for application */
65 } *badlist, **badnext = &badlist;
66 
67 struct disk {
68 	char	*name;			/* disk base name */
69 	struct	disk *next;		/* forward link for list of disks */
70 	struct	part *part;		/* head of list of partitions on disk */
71 	int	pid;			/* If != 0, pid of proc working on */
72 } *disks;
73 
74 int	nrun, ndisks;
75 
76 static void addpart(char *name, char *fsname, long auxdata);
77 static struct disk *finddisk(char *name);
78 static int startdisk(struct disk *dk,int (*checkit)(char *, char *, long, int));
79 
80 int
81 checkfstab(int preen, int maxrun, int (*docheck)(struct fstab *),
82     int (*chkit)(char *, char *, long, int))
83 {
84 	struct fstab *fsp;
85 	struct disk *dk, *nextdisk;
86 	struct part *pt;
87 	int ret, pid, retcode, passno, sumstatus, status;
88 	long auxdata;
89 	char *name;
90 
91 	sumstatus = 0;
92 	for (passno = 1; passno <= 2; passno++) {
93 		if (setfsent() == 0) {
94 			fprintf(stderr, "Can't open checklist file: %s\n",
95 			    _PATH_FSTAB);
96 			return (8);
97 		}
98 		while ((fsp = getfsent()) != 0) {
99 			if ((auxdata = (*docheck)(fsp)) == 0)
100 				continue;
101 			if (preen == 0 ||
102 			    (passno == 1 && fsp->fs_passno == 1)) {
103 				if ((name = blockcheck(fsp->fs_spec)) != 0) {
104 					if ((sumstatus = (*chkit)(name,
105 					    fsp->fs_file, auxdata, 0)) != 0)
106 						return (sumstatus);
107 				} else if (preen)
108 					return (8);
109 			} else if (passno == 2 && fsp->fs_passno > 1) {
110 				if ((name = blockcheck(fsp->fs_spec)) == NULL) {
111 					fprintf(stderr, "BAD DISK NAME %s\n",
112 						fsp->fs_spec);
113 					sumstatus |= 8;
114 					continue;
115 				}
116 				addpart(name, fsp->fs_file, auxdata);
117 			}
118 		}
119 		if (preen == 0)
120 			return (0);
121 	}
122 	if (preen) {
123 		if (maxrun == 0)
124 			maxrun = ndisks;
125 		if (maxrun > ndisks)
126 			maxrun = ndisks;
127 		nextdisk = disks;
128 		for (passno = 0; passno < maxrun; ++passno) {
129 			while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
130 				sleep(10);
131 			if (ret)
132 				return (ret);
133 			nextdisk = nextdisk->next;
134 		}
135 		while ((pid = wait(&status)) != -1) {
136 			for (dk = disks; dk; dk = dk->next)
137 				if (dk->pid == pid)
138 					break;
139 			if (dk == 0) {
140 				printf("Unknown pid %d\n", pid);
141 				continue;
142 			}
143 			if (WIFEXITED(status))
144 				retcode = WEXITSTATUS(status);
145 			else
146 				retcode = 0;
147 			if (WIFSIGNALED(status)) {
148 				printf("%s (%s): EXITED WITH SIGNAL %d\n",
149 					dk->part->name, dk->part->fsname,
150 					WTERMSIG(status));
151 				retcode = 8;
152 			}
153 			if (retcode != 0) {
154 				sumstatus |= retcode;
155 				*badnext = dk->part;
156 				badnext = &dk->part->next;
157 				dk->part = dk->part->next;
158 				*badnext = NULL;
159 			} else
160 				dk->part = dk->part->next;
161 			dk->pid = 0;
162 			nrun--;
163 			if (dk->part == NULL)
164 				ndisks--;
165 
166 			if (nextdisk == NULL) {
167 				if (dk->part) {
168 					while ((ret = startdisk(dk, chkit)) &&
169 					    nrun > 0)
170 						sleep(10);
171 					if (ret)
172 						return (ret);
173 				}
174 			} else if (nrun < maxrun && nrun < ndisks) {
175 				for ( ;; ) {
176 					if ((nextdisk = nextdisk->next) == NULL)
177 						nextdisk = disks;
178 					if (nextdisk->part != NULL &&
179 					    nextdisk->pid == 0)
180 						break;
181 				}
182 				while ((ret = startdisk(nextdisk, chkit)) &&
183 				    nrun > 0)
184 					sleep(10);
185 				if (ret)
186 					return (ret);
187 			}
188 		}
189 	}
190 	if (sumstatus) {
191 		if (badlist == 0)
192 			return (sumstatus);
193 		fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
194 			badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
195 		for (pt = badlist; pt; pt = pt->next)
196 			fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
197 			    pt->next ? ", " : "\n");
198 		return (sumstatus);
199 	}
200 	(void)endfsent();
201 	return (0);
202 }
203 
204 static struct disk *
205 finddisk(char *name)
206 {
207 	struct disk *dk, **dkp;
208 	char *p;
209 	size_t len;
210 
211 	p = strrchr(name, '/');
212 	p = p == NULL ? name : p + 1;
213 	while (*p != '\0' && !isdigit((u_char)*p))
214 		p++;
215 	while (isdigit((u_char)*p))
216 		p++;
217 	len = (size_t)(p - name);
218 	for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
219 		if (strncmp(dk->name, name, len) == 0 &&
220 		    dk->name[len] == 0)
221 			return (dk);
222 	}
223 	if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
224 		fprintf(stderr, "out of memory");
225 		exit (8);
226 	}
227 	dk = *dkp;
228 	if ((dk->name = malloc(len + 1)) == NULL) {
229 		fprintf(stderr, "out of memory");
230 		exit (8);
231 	}
232 	(void)strncpy(dk->name, name, len);
233 	dk->name[len] = '\0';
234 	dk->part = NULL;
235 	dk->next = NULL;
236 	dk->pid = 0;
237 	ndisks++;
238 	return (dk);
239 }
240 
241 static void
242 addpart(char *name, char *fsname, long auxdata)
243 {
244 	struct disk *dk = finddisk(name);
245 	struct part *pt, **ppt = &dk->part;
246 
247 	for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
248 		if (strcmp(pt->name, name) == 0) {
249 			printf("%s in fstab more than once!\n", name);
250 			return;
251 		}
252 	if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
253 		fprintf(stderr, "out of memory");
254 		exit (8);
255 	}
256 	pt = *ppt;
257 	if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
258 		fprintf(stderr, "out of memory");
259 		exit (8);
260 	}
261 	(void)strcpy(pt->name, name);
262 	if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
263 		fprintf(stderr, "out of memory");
264 		exit (8);
265 	}
266 	(void)strcpy(pt->fsname, fsname);
267 	pt->next = NULL;
268 	pt->auxdata = auxdata;
269 }
270 
271 static int
272 startdisk(struct disk *dk, int (*checkit)(char *, char *, long, int))
273 {
274 	struct part *pt = dk->part;
275 
276 	dk->pid = fork();
277 	if (dk->pid < 0) {
278 		perror("fork");
279 		return (8);
280 	}
281 	if (dk->pid == 0)
282 		exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
283 	nrun++;
284 	return (0);
285 }
286 
287