]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/quotacheck/preen.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sbin / quotacheck / preen.c
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 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)preen.c       8.5 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45
46 #include <ufs/ufs/dinode.h>
47
48 #include <ctype.h>
49 #include <errno.h>
50 #include <fstab.h>
51 #include <string.h>
52
53 #include "fsck.h"
54
55 struct part {
56         struct  part *next;             /* forward link of partitions on disk */
57         char    *name;                  /* device name */
58         char    *fsname;                /* mounted filesystem name */
59         long    auxdata;                /* auxillary data for application */
60 } *badlist, **badnext = &badlist;
61
62 struct disk {
63         char    *name;                  /* disk base name */
64         struct  disk *next;             /* forward link for list of disks */
65         struct  part *part;             /* head of list of partitions on disk */
66         int     pid;                    /* If != 0, pid of proc working on */
67 } *disks;
68
69 int     nrun, ndisks;
70
71 static void addpart __P((char *name, char *fsname, long auxdata));
72 static struct disk *finddisk __P((char *name));
73 static int startdisk __P((struct disk *dk,
74                 int (*checkit)(char *, char *, long, int)));
75
76 int
77 checkfstab(preen, maxrun, docheck, chkit)
78         int preen;
79         int maxrun;
80         int (*docheck)(struct fstab *);
81         int (*chkit)(char *, char *, long, int);
82 {
83         register struct fstab *fsp;
84         register struct disk *dk, *nextdisk;
85         register struct part *pt;
86         int ret, pid, retcode, passno, sumstatus, status;
87         long auxdata;
88         char *name;
89
90         sumstatus = 0;
91         for (passno = 1; passno <= 2; passno++) {
92                 if (setfsent() == 0) {
93                         fprintf(stderr, "Can't open checklist file: %s\n",
94                             _PATH_FSTAB);
95                         return (8);
96                 }
97                 while ((fsp = getfsent()) != 0) {
98                         if ((auxdata = (*docheck)(fsp)) == 0)
99                                 continue;
100                         if (preen == 0 ||
101                             (passno == 1 && fsp->fs_passno == 1)) {
102                                 if ((name = blockcheck(fsp->fs_spec)) != 0) {
103                                         if ((sumstatus = (*chkit)(name,
104                                             fsp->fs_file, auxdata, 0)) != 0)
105                                                 return (sumstatus);
106                                 } else if (preen)
107                                         return (8);
108                         } else if (passno == 2 && fsp->fs_passno > 1) {
109                                 if ((name = blockcheck(fsp->fs_spec)) == NULL) {
110                                         fprintf(stderr, "BAD DISK NAME %s\n",
111                                                 fsp->fs_spec);
112                                         sumstatus |= 8;
113                                         continue;
114                                 }
115                                 addpart(name, fsp->fs_file, auxdata);
116                         }
117                 }
118                 if (preen == 0)
119                         return (0);
120         }
121         if (preen) {
122                 if (maxrun == 0)
123                         maxrun = ndisks;
124                 if (maxrun > ndisks)
125                         maxrun = ndisks;
126                 nextdisk = disks;
127                 for (passno = 0; passno < maxrun; ++passno) {
128                         while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
129                                 sleep(10);
130                         if (ret)
131                                 return (ret);
132                         nextdisk = nextdisk->next;
133                 }
134                 while ((pid = wait(&status)) != -1) {
135                         for (dk = disks; dk; dk = dk->next)
136                                 if (dk->pid == pid)
137                                         break;
138                         if (dk == 0) {
139                                 printf("Unknown pid %d\n", pid);
140                                 continue;
141                         }
142                         if (WIFEXITED(status))
143                                 retcode = WEXITSTATUS(status);
144                         else
145                                 retcode = 0;
146                         if (WIFSIGNALED(status)) {
147                                 printf("%s (%s): EXITED WITH SIGNAL %d\n",
148                                         dk->part->name, dk->part->fsname,
149                                         WTERMSIG(status));
150                                 retcode = 8;
151                         }
152                         if (retcode != 0) {
153                                 sumstatus |= retcode;
154                                 *badnext = dk->part;
155                                 badnext = &dk->part->next;
156                                 dk->part = dk->part->next;
157                                 *badnext = NULL;
158                         } else
159                                 dk->part = dk->part->next;
160                         dk->pid = 0;
161                         nrun--;
162                         if (dk->part == NULL)
163                                 ndisks--;
164
165                         if (nextdisk == NULL) {
166                                 if (dk->part) {
167                                         while ((ret = startdisk(dk, chkit)) &&
168                                             nrun > 0)
169                                                 sleep(10);
170                                         if (ret)
171                                                 return (ret);
172                                 }
173                         } else if (nrun < maxrun && nrun < ndisks) {
174                                 for ( ;; ) {
175                                         if ((nextdisk = nextdisk->next) == NULL)
176                                                 nextdisk = disks;
177                                         if (nextdisk->part != NULL &&
178                                             nextdisk->pid == 0)
179                                                 break;
180                                 }
181                                 while ((ret = startdisk(nextdisk, chkit)) &&
182                                     nrun > 0)
183                                         sleep(10);
184                                 if (ret)
185                                         return (ret);
186                         }
187                 }
188         }
189         if (sumstatus) {
190                 if (badlist == 0)
191                         return (sumstatus);
192                 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
193                         badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
194                 for (pt = badlist; pt; pt = pt->next)
195                         fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
196                             pt->next ? ", " : "\n");
197                 return (sumstatus);
198         }
199         (void)endfsent();
200         return (0);
201 }
202
203 static struct disk *
204 finddisk(name)
205         char *name;
206 {
207         register struct disk *dk, **dkp;
208         register 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(name, fsname, auxdata)
243         char *name, *fsname;
244         long auxdata;
245 {
246         struct disk *dk = finddisk(name);
247         register struct part *pt, **ppt = &dk->part;
248
249         for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
250                 if (strcmp(pt->name, name) == 0) {
251                         printf("%s in fstab more than once!\n", name);
252                         return;
253                 }
254         if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
255                 fprintf(stderr, "out of memory");
256                 exit (8);
257         }
258         pt = *ppt;
259         if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
260                 fprintf(stderr, "out of memory");
261                 exit (8);
262         }
263         (void)strcpy(pt->name, name);
264         if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
265                 fprintf(stderr, "out of memory");
266                 exit (8);
267         }
268         (void)strcpy(pt->fsname, fsname);
269         pt->next = NULL;
270         pt->auxdata = auxdata;
271 }
272
273 static int
274 startdisk(dk, checkit)
275         register struct disk *dk;
276         int (*checkit)(char *, char *, long, int);
277 {
278         register struct part *pt = dk->part;
279
280         dk->pid = fork();
281         if (dk->pid < 0) {
282                 perror("fork");
283                 return (8);
284         }
285         if (dk->pid == 0)
286                 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
287         nrun++;
288         return (0);
289 }
290
291 char *
292 blockcheck(origname)
293         char *origname;
294 {
295         struct stat stslash, stblock, stchar;
296         char *newname, *raw;
297         struct fstab *fsinfo;
298         int retried = 0, len;
299
300         newname = origname;
301 retry:
302         if (stat(newname, &stblock) < 0) {
303                 printf("Can't stat %s: %s\n", newname, strerror(errno));
304                 return (origname);
305         }
306         switch(stblock.st_mode & S_IFMT) {
307         case S_IFCHR:
308         case S_IFBLK:
309                 return(newname);
310         case S_IFDIR:
311                 if (retried)
312                         break;
313                 
314                 len = strlen(origname) - 1;
315                 if (len > 0 && origname[len] == '/')
316                         /* remove trailing slash */
317                         origname[len] = '\0';
318                 if ((fsinfo = getfsfile(origname)) == NULL) {
319                         printf("Can't resolve %s to character special device",
320                             origname);
321                         return (0);
322                 }
323                 newname = fsinfo->fs_spec;
324                 retried++;
325                 goto retry;
326         }
327         /*
328          * Not a block or character device, just return name and
329          * let the user decide whether to use it.
330          */
331         return (origname);
332 }