Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sbin / fsck / fsutil.c
1 /*      $NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $       */
2
3 /*
4  * Copyright (c) 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $FreeBSD: src/sbin/fsck/fsutil.c,v 1.2.2.1 2001/08/01 05:47:55 obrien Exp $
36  */
37
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $");
41 #endif /* not lint */
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #if __STDC__
47 #include <stdarg.h>
48 #else
49 #include <varargs.h>
50 #endif
51 #include <errno.h>
52 #include <fstab.h>
53 #include <err.h>
54 #include <paths.h>
55
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <sys/mount.h>
59
60 #include "fsutil.h"
61
62 static const char *dev = NULL;
63 static int hot = 0;
64 static int preen = 0;
65
66 extern char *__progname;
67
68 static void vmsg __P((int, const char *, va_list));
69
70 void
71 setcdevname(cd, pr)
72         const char *cd;
73         int pr;
74 {
75         dev = cd;
76         preen = pr;
77 }
78
79 const char *
80 cdevname()
81 {
82         return dev;
83 }
84
85 int
86 hotroot()
87 {
88         return hot;
89 }
90
91 /*VARARGS*/
92 void
93 #if __STDC__
94 errexit(const char *fmt, ...)
95 #else
96 errexit(va_alist)
97         va_dcl
98 #endif
99 {
100         va_list ap;
101
102 #if __STDC__
103         va_start(ap, fmt);
104 #else
105         const char *fmt;
106
107         va_start(ap);
108         fmt = va_arg(ap, const char *);
109 #endif
110         (void) vfprintf(stderr, fmt, ap);
111         va_end(ap);
112         exit(8);
113 }
114
115 static void
116 vmsg(fatal, fmt, ap)
117         int fatal;
118         const char *fmt;
119         va_list ap;
120 {
121         if (!fatal && preen)
122                 (void) printf("%s: ", dev);
123
124         (void) vprintf(fmt, ap);
125
126         if (fatal && preen)
127                 (void) printf("\n");
128
129         if (fatal && preen) {
130                 (void) printf(
131                     "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
132                     dev, __progname);
133                 exit(8);
134         }
135 }
136
137 /*VARARGS*/
138 void
139 #if __STDC__
140 pfatal(const char *fmt, ...)
141 #else
142 pfatal(va_alist)
143         va_dcl
144 #endif
145 {
146         va_list ap;
147
148 #if __STDC__
149         va_start(ap, fmt);
150 #else
151         const char *fmt;
152
153         va_start(ap);
154         fmt = va_arg(ap, const char *);
155 #endif
156         vmsg(1, fmt, ap);
157         va_end(ap);
158 }
159
160 /*VARARGS*/
161 void
162 #if __STDC__
163 pwarn(const char *fmt, ...)
164 #else
165 pwarn(va_alist)
166         va_dcl
167 #endif
168 {
169         va_list ap;
170 #if __STDC__
171         va_start(ap, fmt);
172 #else
173         const char *fmt;
174
175         va_start(ap);
176         fmt = va_arg(ap, const char *);
177 #endif
178         vmsg(0, fmt, ap);
179         va_end(ap);
180 }
181
182 void
183 perror(s)
184         const char *s;
185 {
186         pfatal("%s (%s)", s, strerror(errno));
187 }
188
189 void
190 #if __STDC__
191 panic(const char *fmt, ...)
192 #else
193 panic(va_alist)
194         va_dcl
195 #endif
196 {
197         va_list ap;
198
199 #if __STDC__
200         va_start(ap, fmt);
201 #else
202         const char *fmt;
203
204         va_start(ap);
205         fmt = va_arg(ap, const char *);
206 #endif
207         vmsg(1, fmt, ap);
208         va_end(ap);
209         exit(8);
210 }
211
212 const char *
213 unrawname(name)
214         const char *name;
215 {
216         static char unrawbuf[32];
217         const char *dp;
218         struct stat stb;
219
220         if ((dp = strrchr(name, '/')) == 0)
221                 return (name);
222         if (stat(name, &stb) < 0)
223                 return (name);
224         if (!S_ISCHR(stb.st_mode))
225                 return (name);
226         if (dp[1] != 'r')
227                 return (name);
228         (void)snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
229         return (unrawbuf);
230 }
231
232 const char *
233 rawname(name)
234         const char *name;
235 {
236         static char rawbuf[32];
237         const char *dp;
238
239         if ((dp = strrchr(name, '/')) == 0)
240                 return (0);
241         (void)snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
242         return (rawbuf);
243 }
244
245 const char *
246 devcheck(origname)
247         const char *origname;
248 {
249         struct stat stslash, stchar;
250
251         if (stat("/", &stslash) < 0) {
252                 perror("/");
253                 printf("Can't stat root\n");
254                 return (origname);
255         }
256         if (stat(origname, &stchar) < 0) {
257                 perror(origname);
258                 printf("Can't stat %s\n", origname);
259                 return (origname);
260         }
261         if (!S_ISCHR(stchar.st_mode)) {
262                 perror(origname);
263                 printf("%s is not a char device\n", origname);
264         }
265         return (origname);
266 }
267
268 /*
269  * Get the mount point information for name.
270  */
271 struct statfs *
272 getmntpt(name)
273         const char *name;
274 {
275         struct stat devstat, mntdevstat;
276         char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
277         char *devname;
278         struct statfs *mntbuf, *statfsp;
279         int i, mntsize, isdev;
280
281         if (stat(name, &devstat) != 0)
282                 return (NULL);
283         if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
284                 isdev = 1;
285         else
286                 isdev = 0;
287         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
288         for (i = 0; i < mntsize; i++) {
289                 statfsp = &mntbuf[i];
290                 devname = statfsp->f_mntfromname;
291                 if (*devname != '/') {
292                         strcpy(device, _PATH_DEV);
293                         strcat(device, devname);
294                         strcpy(statfsp->f_mntfromname, device);
295                 }
296                 if (isdev == 0) {
297                         if (strcmp(name, statfsp->f_mntonname))
298                                 continue;
299                         return (statfsp);
300                 }
301                 if (stat(devname, &mntdevstat) == 0 &&
302                     mntdevstat.st_rdev == devstat.st_rdev)
303                         return (statfsp);
304         }
305         statfsp = NULL;
306         return (statfsp);
307 }
308
309 #if 0
310 /*
311  * XXX this code is from NetBSD, but fails in FreeBSD because we
312  * don't have blockdevs. I don't think its needed.
313  */
314 const char *
315 blockcheck(origname)
316         const char *origname;
317 {
318         struct stat stslash, stblock, stchar;
319         const char *newname, *raw;
320         struct fstab *fsp;
321         int retried = 0;
322
323         hot = 0;
324         if (stat("/", &stslash) < 0) {
325                 perror("/");
326                 printf("Can't stat root\n");
327                 return (origname);
328         }
329         newname = origname;
330 retry:
331         if (stat(newname, &stblock) < 0) {
332                 perror(newname);
333                 printf("Can't stat %s\n", newname);
334                 return (origname);
335         }
336         if (S_ISBLK(stblock.st_mode)) {
337                 if (stslash.st_dev == stblock.st_rdev)
338                         hot++;
339                 raw = rawname(newname);
340                 if (stat(raw, &stchar) < 0) {
341                         perror(raw);
342                         printf("Can't stat %s\n", raw);
343                         return (origname);
344                 }
345                 if (S_ISCHR(stchar.st_mode)) {
346                         return (raw);
347                 } else {
348                         printf("%s is not a character device\n", raw);
349                         return (origname);
350                 }
351         } else if (S_ISCHR(stblock.st_mode) && !retried) {
352                 newname = unrawname(newname);
353                 retried++;
354                 goto retry;
355         } else if ((fsp = getfsfile(newname)) != 0 && !retried) {
356                 newname = fsp->fs_spec;
357                 retried++;
358                 goto retry;
359         }
360         /*
361          * Not a block or character device, just return name and
362          * let the user decide whether to use it.
363          */
364         return (origname);
365 }
366 #endif
367
368
369 void *
370 emalloc(s)
371         size_t s;
372 {
373         void *p;
374
375         p = malloc(s);
376         if (p == NULL)
377                 err(1, "malloc failed");
378         return (p);
379 }
380
381
382 void *
383 erealloc(p, s)
384         void *p;
385         size_t s;
386 {
387         void *q;
388
389         q = realloc(p, s);
390         if (q == NULL)
391                 err(1, "realloc failed");
392         return (q);
393 }
394
395
396 char *
397 estrdup(s)
398         const char *s;
399 {
400         char *p;
401
402         p = strdup(s);
403         if (p == NULL)
404                 err(1, "strdup failed");
405         return (p);
406 }