- Test m_pkthdr.fw_flags against DUMMYNET_MBUF_TAGGED before trying to locate
[dragonfly/netmp.git] / sbin / fsck / fsutil.c
blob54941b54d314c9e55385160041d7a01852e4a15e
1 /* $NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $ */
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
35 * $FreeBSD: src/sbin/fsck/fsutil.c,v 1.2.2.1 2001/08/01 05:47:55 obrien Exp $
36 * $DragonFly: src/sbin/fsck/fsutil.c,v 1.6 2006/10/12 04:04:03 dillon Exp $
38 * $NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $
41 #include <sys/cdefs.h>
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>
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <sys/mount.h>
60 #include "fsutil.h"
62 static const char *dev = NULL;
63 static int hot = 0;
64 static int preen = 0;
66 extern char *__progname;
68 static void vmsg(int, const char *, va_list);
70 void
71 setcdevname(const char *cd, int pr)
73 dev = cd;
74 preen = pr;
77 const char *
78 cdevname(void)
80 return dev;
83 int
84 hotroot(void)
86 return hot;
89 /*VARARGS*/
90 void
91 errexit(const char *fmt, ...)
93 va_list ap;
95 va_start(ap, fmt);
96 vfprintf(stderr, fmt, ap);
97 va_end(ap);
98 exit(8);
101 static void
102 vmsg(int fatal, const char *fmt, va_list ap)
104 if (!fatal && preen)
105 printf("%s: ", dev);
107 vprintf(fmt, ap);
109 if (fatal && preen)
110 printf("\n");
112 if (fatal && preen) {
113 printf(
114 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
115 dev, __progname);
116 exit(8);
120 /*VARARGS*/
121 void
122 pfatal(const char *fmt, ...)
124 va_list ap;
126 va_start(ap, fmt);
127 vmsg(1, fmt, ap);
128 va_end(ap);
131 /*VARARGS*/
132 void
133 pwarn(const char *fmt, ...)
135 va_list ap;
136 va_start(ap, fmt);
137 vmsg(0, fmt, ap);
138 va_end(ap);
141 void
142 perror(const char *s)
144 pfatal("%s (%s)", s, strerror(errno));
147 void
148 panic(const char *fmt, ...)
150 va_list ap;
152 va_start(ap, fmt);
153 vmsg(1, fmt, ap);
154 va_end(ap);
155 exit(8);
158 const char *
159 unrawname(const char *name)
161 static char unrawbuf[32];
162 const char *dp;
163 struct stat stb;
165 if ((dp = strrchr(name, '/')) == 0)
166 return (name);
167 if (stat(name, &stb) < 0)
168 return (name);
169 if (!S_ISCHR(stb.st_mode))
170 return (name);
171 if (dp[1] != 'r')
172 return (name);
173 snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
174 return (unrawbuf);
177 const char *
178 rawname(const char *name)
180 static char rawbuf[32];
181 const char *dp;
183 if ((dp = strrchr(name, '/')) == 0)
184 return (0);
185 snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
186 return (rawbuf);
189 const char *
190 devcheck(const char *origname)
192 struct stat stslash, stchar;
194 if (stat("/", &stslash) < 0) {
195 perror("/");
196 printf("Can't stat root\n");
197 return (origname);
199 if (stat(origname, &stchar) < 0) {
200 perror(origname);
201 printf("Can't stat %s\n", origname);
202 return (origname);
204 if (!S_ISCHR(stchar.st_mode)) {
205 perror(origname);
206 printf("%s is not a char device\n", origname);
208 return (origname);
212 * Get the mount point information for name.
214 struct statfs *
215 getmntpt(const char *name)
217 struct stat devstat, mntdevstat;
218 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
219 char *devname;
220 struct statfs *mntbuf, *statfsp;
221 int i, mntsize, isdev;
223 if (stat(name, &devstat) != 0)
224 return (NULL);
225 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
226 isdev = 1;
227 else
228 isdev = 0;
229 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
230 for (i = 0; i < mntsize; i++) {
231 statfsp = &mntbuf[i];
232 devname = statfsp->f_mntfromname;
233 if (*devname != '/') {
234 strcpy(device, _PATH_DEV);
235 strcat(device, devname);
236 strcpy(statfsp->f_mntfromname, device);
238 if (isdev == 0) {
239 if (strcmp(name, statfsp->f_mntonname))
240 continue;
241 return (statfsp);
243 if (stat(devname, &mntdevstat) == 0 &&
244 mntdevstat.st_rdev == devstat.st_rdev)
245 return (statfsp);
247 statfsp = NULL;
248 return (statfsp);
251 #if 0
253 * XXX this code is from NetBSD, but fails in FreeBSD because we
254 * don't have blockdevs. I don't think its needed.
256 const char *
257 blockcheck(const char *origname)
259 struct stat stslash, stblock, stchar;
260 const char *newname, *raw;
261 struct fstab *fsp;
262 int retried = 0;
264 hot = 0;
265 if (stat("/", &stslash) < 0) {
266 perror("/");
267 printf("Can't stat root\n");
268 return (origname);
270 newname = origname;
271 retry:
272 if (stat(newname, &stblock) < 0) {
273 perror(newname);
274 printf("Can't stat %s\n", newname);
275 return (origname);
277 if (S_ISBLK(stblock.st_mode)) {
278 if (stslash.st_dev == stblock.st_rdev)
279 hot++;
280 raw = rawname(newname);
281 if (stat(raw, &stchar) < 0) {
282 perror(raw);
283 printf("Can't stat %s\n", raw);
284 return (origname);
286 if (S_ISCHR(stchar.st_mode)) {
287 return (raw);
288 } else {
289 printf("%s is not a character device\n", raw);
290 return (origname);
292 } else if (S_ISCHR(stblock.st_mode) && !retried) {
293 newname = unrawname(newname);
294 retried++;
295 goto retry;
296 } else if ((fsp = getfsfile(newname)) != 0 && !retried) {
297 newname = fsp->fs_spec;
298 retried++;
299 goto retry;
302 * Not a block or character device, just return name and
303 * let the user decide whether to use it.
305 return (origname);
307 #endif