xref: /dragonfly/sbin/fsck/fsutil.c (revision 9348a738)
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sbin/fsck/fsutil.c,v 1.2.2.1 2001/08/01 05:47:55 obrien Exp $
32  */
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <errno.h>
39 #include <fstab.h>
40 #include <err.h>
41 #include <paths.h>
42 
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/mount.h>
46 
47 #include "fsutil.h"
48 
49 static const char *dev = NULL;
50 static int hot = 0;
51 static int preen = 0;
52 
53 extern char *__progname;
54 
55 static void vmsg(int, const char *, va_list) __printflike(2, 0);
56 
57 void
58 setcdevname(const char *cd, int pr)
59 {
60 	dev = cd;
61 	preen = pr;
62 }
63 
64 const char *
65 cdevname(void)
66 {
67 	return dev;
68 }
69 
70 int
71 hotroot(void)
72 {
73 	return hot;
74 }
75 
76 /*VARARGS*/
77 void
78 errexit(const char *fmt, ...)
79 {
80 	va_list ap;
81 
82 	va_start(ap, fmt);
83 	vfprintf(stderr, fmt, ap);
84 	va_end(ap);
85 	exit(8);
86 }
87 
88 static void
89 vmsg(int fatal, const char *fmt, va_list ap)
90 {
91 	if (!fatal && preen)
92 		printf("%s: ", dev);
93 
94 	vprintf(fmt, ap);
95 
96 	if (fatal && preen)
97 		printf("\n");
98 
99 	if (fatal && preen) {
100 		printf(
101 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
102 		    dev, __progname);
103 		exit(8);
104 	}
105 }
106 
107 /*VARARGS*/
108 void
109 pfatal(const char *fmt, ...)
110 {
111 	va_list ap;
112 
113 	va_start(ap, fmt);
114 	vmsg(1, fmt, ap);
115 	va_end(ap);
116 }
117 
118 /*VARARGS*/
119 void
120 pwarn(const char *fmt, ...)
121 {
122 	va_list ap;
123 	va_start(ap, fmt);
124 	vmsg(0, fmt, ap);
125 	va_end(ap);
126 }
127 
128 void
129 perror(const char *s)
130 {
131 	pfatal("%s (%s)", s, strerror(errno));
132 }
133 
134 void
135 panic(const char *fmt, ...)
136 {
137 	va_list ap;
138 
139 	va_start(ap, fmt);
140 	vmsg(1, fmt, ap);
141 	va_end(ap);
142 	exit(8);
143 }
144 
145 const char *
146 unrawname(const char *name)
147 {
148 	static char unrawbuf[32];
149 	const char *dp;
150 	struct stat stb;
151 
152 	if ((dp = strrchr(name, '/')) == NULL)
153 		return (name);
154 	if (stat(name, &stb) < 0)
155 		return (name);
156 	if (!S_ISCHR(stb.st_mode))
157 		return (name);
158 	if (dp[1] != 'r')
159 		return (name);
160 	snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
161 	return (unrawbuf);
162 }
163 
164 const char *
165 rawname(const char *name)
166 {
167 	static char rawbuf[32];
168 	const char *dp;
169 
170 	if ((dp = strrchr(name, '/')) == NULL)
171 		return (0);
172 	snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
173 	return (rawbuf);
174 }
175 
176 const char *
177 devcheck(const char *origname)
178 {
179 	struct stat stslash, stchar;
180 
181 	if (stat("/", &stslash) < 0) {
182 		perror("/");
183 		printf("Can't stat root\n");
184 		return (origname);
185 	}
186 	if (stat(origname, &stchar) < 0) {
187 		perror(origname);
188 		printf("Can't stat %s\n", origname);
189 		return (origname);
190 	}
191 	if (!S_ISCHR(stchar.st_mode)) {
192 		perror(origname);
193 		printf("%s is not a char device\n", origname);
194 	}
195 	return (origname);
196 }
197 
198 /*
199  * Get the mount point information for name.
200  */
201 struct statfs *
202 getmntpt(const char *name)
203 {
204 	struct stat devstat, mntdevstat;
205 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
206 	char *devname;
207 	struct statfs *mntbuf, *statfsp;
208 	int i, mntsize, isdev;
209 
210 	if (stat(name, &devstat) != 0)
211 		return (NULL);
212 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
213 		isdev = 1;
214 	else
215 		isdev = 0;
216 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
217 	for (i = 0; i < mntsize; i++) {
218 		statfsp = &mntbuf[i];
219 		devname = statfsp->f_mntfromname;
220 		if (*devname != '/') {
221 			strcpy(device, _PATH_DEV);
222 			strcat(device, devname);
223 			strcpy(statfsp->f_mntfromname, device);
224 		}
225 		if (isdev == 0) {
226 			if (strcmp(name, statfsp->f_mntonname))
227 				continue;
228 			return (statfsp);
229 		}
230 		if (stat(devname, &mntdevstat) == 0 &&
231 		    mntdevstat.st_rdev == devstat.st_rdev)
232 			return (statfsp);
233 	}
234 	statfsp = NULL;
235 	return (statfsp);
236 }
237 
238 #if 0
239 /*
240  * XXX this code is from NetBSD, but fails in FreeBSD because we
241  * don't have blockdevs. I don't think its needed.
242  */
243 const char *
244 blockcheck(const char *origname)
245 {
246 	struct stat stslash, stblock, stchar;
247 	const char *newname, *raw;
248 	struct fstab *fsp;
249 	int retried = 0;
250 
251 	hot = 0;
252 	if (stat("/", &stslash) < 0) {
253 		perror("/");
254 		printf("Can't stat root\n");
255 		return (origname);
256 	}
257 	newname = origname;
258 retry:
259 	if (stat(newname, &stblock) < 0) {
260 		perror(newname);
261 		printf("Can't stat %s\n", newname);
262 		return (origname);
263 	}
264 	if (S_ISBLK(stblock.st_mode)) {
265 		if (stslash.st_dev == stblock.st_rdev)
266 			hot++;
267 		raw = rawname(newname);
268 		if (stat(raw, &stchar) < 0) {
269 			perror(raw);
270 			printf("Can't stat %s\n", raw);
271 			return (origname);
272 		}
273 		if (S_ISCHR(stchar.st_mode)) {
274 			return (raw);
275 		} else {
276 			printf("%s is not a character device\n", raw);
277 			return (origname);
278 		}
279 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
280 		newname = unrawname(newname);
281 		retried++;
282 		goto retry;
283 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
284 		newname = fsp->fs_spec;
285 		retried++;
286 		goto retry;
287 	}
288 	/*
289 	 * Not a block or character device, just return name and
290 	 * let the user decide whether to use it.
291 	 */
292 	return (origname);
293 }
294 #endif
295 
296