]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/disklib/preen.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #include <sys/param.h>
67 #include <TargetConditionals.h>
70 struct part
*next
; /* forward link of partitions on disk */
71 char *name
; /* device name */
72 char *fsname
; /* mounted filesystem name */
73 long auxdata
; /* auxillary data for application */
74 } *badlist
, **badnext
= &badlist
;
77 char *name
; /* disk base name */
78 struct disk
*next
; /* forward link for list of disks */
79 struct part
*part
; /* head of list of partitions on disk */
80 int pid
; /* If != 0, pid of proc working on */
86 static void addpart (char *name
, char *fsname
, long auxdata
);
87 static struct disk
*finddisk (char *name
);
88 static char *rawname (char *name
);
89 static int startdisk (struct disk
*dk
,
90 int (*checkit
)(char *, char *, long, int));
91 static char *unrawname (char *name
);
92 char* blockcheck (char *name
);
95 checkfstab(preen
, maxrun
, docheck
, chkit
)
98 int (*docheck
)(struct fstab
*);
99 int (*chkit
)(char *, char *, long, int);
101 register struct fstab
*fsp
;
102 register struct disk
*dk
, *nextdisk
;
103 register struct part
*pt
;
104 int ret
, pid
, retcode
, passno
, sumstatus
, status
;
109 for (passno
= 1; passno
<= 2; passno
++) {
110 if (setfsent() == 0) {
111 fprintf(stderr
, "Can't open checklist file: %s\n",
115 while ((fsp
= getfsent()) != 0) {
116 if ((auxdata
= (*docheck
)(fsp
)) == 0)
119 (passno
== 1 && fsp
->fs_passno
== 1)) {
120 if ((name
= blockcheck(fsp
->fs_spec
)) != 0) {
121 if ((sumstatus
= (*chkit
)(name
,
122 fsp
->fs_file
, auxdata
, 0)) != 0)
126 } else if (passno
== 2 && fsp
->fs_passno
> 1) {
127 if ((name
= blockcheck(fsp
->fs_spec
)) == NULL
) {
128 fprintf(stderr
, "BAD DISK NAME %s\n",
133 addpart(name
, fsp
->fs_file
, auxdata
);
145 for (passno
= 0; passno
< maxrun
; ++passno
) {
146 while ((ret
= startdisk(nextdisk
, chkit
)) && nrun
> 0)
150 nextdisk
= nextdisk
->next
;
152 while ((pid
= wait(&status
)) != -1) {
153 for (dk
= disks
; dk
; dk
= dk
->next
)
157 printf("Unknown pid %d\n", pid
);
160 if (WIFEXITED(status
))
161 retcode
= WEXITSTATUS(status
);
164 if (WIFSIGNALED(status
)) {
165 printf("%s (%s): EXITED WITH SIGNAL %d\n",
166 dk
->part
->name
, dk
->part
->fsname
,
171 sumstatus
|= retcode
;
173 badnext
= &dk
->part
->next
;
174 dk
->part
= dk
->part
->next
;
177 dk
->part
= dk
->part
->next
;
180 if (dk
->part
== NULL
)
183 if (nextdisk
== NULL
) {
185 while ((ret
= startdisk(dk
, chkit
)) &&
191 } else if (nrun
< maxrun
&& nrun
< ndisks
) {
193 if ((nextdisk
= nextdisk
->next
) == NULL
)
195 if (nextdisk
->part
!= NULL
&&
199 while ((ret
= startdisk(nextdisk
, chkit
)) &&
210 fprintf(stderr
, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
211 badlist
->next
? "S" : "", "UNEXPECTED INCONSISTENCY:");
212 for (pt
= badlist
; pt
; pt
= pt
->next
)
213 fprintf(stderr
, "%s (%s)%s", pt
->name
, pt
->fsname
,
214 pt
->next
? ", " : "\n");
225 register struct disk
*dk
, **dkp
;
229 for (len
= strlen(name
), p
= name
+ len
- 1; p
>= name
; --p
)
235 for (dk
= disks
, dkp
= &disks
; dk
; dkp
= &dk
->next
, dk
= dk
->next
) {
236 if (strncmp(dk
->name
, name
, len
) == 0 &&
240 if ((*dkp
= (struct disk
*)malloc(sizeof(struct disk
))) == NULL
) {
241 fprintf(stderr
, "out of memory");
245 if ((dk
->name
= malloc(len
+ 1)) == NULL
) {
246 fprintf(stderr
, "out of memory");
249 (void)strncpy(dk
->name
, name
, len
);
250 dk
->name
[len
] = '\0';
259 addpart(name
, fsname
, auxdata
)
263 struct disk
*dk
= finddisk(name
);
264 register struct part
*pt
, **ppt
= &dk
->part
;
266 for (pt
= dk
->part
; pt
; ppt
= &pt
->next
, pt
= pt
->next
)
267 if (strcmp(pt
->name
, name
) == 0) {
268 printf("%s in fstab more than once!\n", name
);
271 if ((*ppt
= (struct part
*)malloc(sizeof(struct part
))) == NULL
) {
272 fprintf(stderr
, "out of memory");
276 if ((pt
->name
= strdup(name
)) == NULL
) {
277 fprintf(stderr
, "out of memory");
280 if ((pt
->fsname
= strdup(fsname
)) == NULL
) {
281 fprintf(stderr
, "out of memory");
285 pt
->auxdata
= auxdata
;
289 startdisk(dk
, checkit
)
290 register struct disk
*dk
;
291 int (*checkit
)(char *, char *, long, int);
293 register struct part
*pt
= dk
->part
;
301 exit((*checkit
)(pt
->name
, pt
->fsname
, pt
->auxdata
, 1));
307 blockcheck(char* origname
) {
308 struct stat stslash
, stblock
, stchar
;
313 if (stat("/", &stslash
) < 0) {
315 printf("Can't stat root\n");
320 if (stat(newname
, &stblock
) < 0) {
322 printf("Can't stat %s\n", newname
);
325 if ((stblock
.st_mode
& S_IFMT
) == S_IFBLK
) {
326 if (stslash
.st_dev
== stblock
.st_rdev
)
328 raw
= rawname(newname
);
329 if (stat(raw
, &stchar
) < 0) {
331 printf("Can't stat %s\n", raw
);
334 if ((stchar
.st_mode
& S_IFMT
) == S_IFCHR
) {
337 printf("%s is not a character device\n", raw
);
340 } else if ((stblock
.st_mode
& S_IFMT
) == S_IFCHR
&& !retried
) {
341 newname
= unrawname(newname
);
346 * Not a block or character device, just return name and
347 * let the user decide whether to use it.
360 if ((dp
= strrchr(name
, '/')) == 0)
362 if (stat(name
, &stb
) < 0)
364 if ((stb
.st_mode
& S_IFMT
) != S_IFCHR
)
368 dp_len
= strlen(&dp
[2]) + 1;
369 (void)memmove(&dp
[1], &dp
[2], dp_len
);
377 static char rawbuf
[32];
380 if ((dp
= strrchr(name
, '/')) == 0)
383 (void)strncpy(rawbuf
, name
, sizeof(rawbuf
));
385 (void)strlcat(rawbuf
, "/r", sizeof(rawbuf
));
386 (void)strlcat(rawbuf
, &dp
[1], sizeof(rawbuf
));