xref: /dragonfly/sbin/fsck/fsutil.c (revision 77b0c609)
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 <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <fstab.h>
44 #include <err.h>
45 #include <paths.h>
46 
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <sys/mount.h>
50 
51 #include "fsutil.h"
52 
53 static const char *dev = NULL;
54 static int hot = 0;
55 static int preen = 0;
56 
57 extern char *__progname;
58 
59 static void vmsg(int, const char *, va_list) __printflike(2, 0);
60 
61 void
62 setcdevname(const char *cd, int pr)
63 {
64 	dev = cd;
65 	preen = pr;
66 }
67 
68 const char *
69 cdevname(void)
70 {
71 	return dev;
72 }
73 
74 int
75 hotroot(void)
76 {
77 	return hot;
78 }
79 
80 /*VARARGS*/
81 void
82 errexit(const char *fmt, ...)
83 {
84 	va_list ap;
85 
86 	va_start(ap, fmt);
87 	vfprintf(stderr, fmt, ap);
88 	va_end(ap);
89 	exit(8);
90 }
91 
92 static void
93 vmsg(int fatal, const char *fmt, va_list ap)
94 {
95 	if (!fatal && preen)
96 		printf("%s: ", dev);
97 
98 	vprintf(fmt, ap);
99 
100 	if (fatal && preen)
101 		printf("\n");
102 
103 	if (fatal && preen) {
104 		printf(
105 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
106 		    dev, __progname);
107 		exit(8);
108 	}
109 }
110 
111 /*VARARGS*/
112 void
113 pfatal(const char *fmt, ...)
114 {
115 	va_list ap;
116 
117 	va_start(ap, fmt);
118 	vmsg(1, fmt, ap);
119 	va_end(ap);
120 }
121 
122 /*VARARGS*/
123 void
124 pwarn(const char *fmt, ...)
125 {
126 	va_list ap;
127 	va_start(ap, fmt);
128 	vmsg(0, fmt, ap);
129 	va_end(ap);
130 }
131 
132 void
133 perror(const char *s)
134 {
135 	pfatal("%s (%s)", s, strerror(errno));
136 }
137 
138 void
139 panic(const char *fmt, ...)
140 {
141 	va_list ap;
142 
143 	va_start(ap, fmt);
144 	vmsg(1, fmt, ap);
145 	va_end(ap);
146 	exit(8);
147 }
148 
149 const char *
150 unrawname(const char *name)
151 {
152 	static char unrawbuf[32];
153 	const char *dp;
154 	struct stat stb;
155 
156 	if ((dp = strrchr(name, '/')) == NULL)
157 		return (name);
158 	if (stat(name, &stb) < 0)
159 		return (name);
160 	if (!S_ISCHR(stb.st_mode))
161 		return (name);
162 	if (dp[1] != 'r')
163 		return (name);
164 	snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2);
165 	return (unrawbuf);
166 }
167 
168 const char *
169 rawname(const char *name)
170 {
171 	static char rawbuf[32];
172 	const char *dp;
173 
174 	if ((dp = strrchr(name, '/')) == NULL)
175 		return (0);
176 	snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1);
177 	return (rawbuf);
178 }
179 
180 const char *
181 devcheck(const char *origname)
182 {
183 	struct stat stslash, stchar;
184 
185 	if (stat("/", &stslash) < 0) {
186 		perror("/");
187 		printf("Can't stat root\n");
188 		return (origname);
189 	}
190 	if (stat(origname, &stchar) < 0) {
191 		perror(origname);
192 		printf("Can't stat %s\n", origname);
193 		return (origname);
194 	}
195 	if (!S_ISCHR(stchar.st_mode)) {
196 		perror(origname);
197 		printf("%s is not a char device\n", origname);
198 	}
199 	return (origname);
200 }
201 
202 /*
203  * Get the mount point information for name.
204  */
205 struct statfs *
206 getmntpt(const char *name)
207 {
208 	struct stat devstat, mntdevstat;
209 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
210 	char *devname;
211 	struct statfs *mntbuf, *statfsp;
212 	int i, mntsize, isdev;
213 
214 	if (stat(name, &devstat) != 0)
215 		return (NULL);
216 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
217 		isdev = 1;
218 	else
219 		isdev = 0;
220 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
221 	for (i = 0; i < mntsize; i++) {
222 		statfsp = &mntbuf[i];
223 		devname = statfsp->f_mntfromname;
224 		if (*devname != '/') {
225 			strcpy(device, _PATH_DEV);
226 			strcat(device, devname);
227 			strcpy(statfsp->f_mntfromname, device);
228 		}
229 		if (isdev == 0) {
230 			if (strcmp(name, statfsp->f_mntonname))
231 				continue;
232 			return (statfsp);
233 		}
234 		if (stat(devname, &mntdevstat) == 0 &&
235 		    mntdevstat.st_rdev == devstat.st_rdev)
236 			return (statfsp);
237 	}
238 	statfsp = NULL;
239 	return (statfsp);
240 }
241 
242 #if 0
243 /*
244  * XXX this code is from NetBSD, but fails in FreeBSD because we
245  * don't have blockdevs. I don't think its needed.
246  */
247 const char *
248 blockcheck(const char *origname)
249 {
250 	struct stat stslash, stblock, stchar;
251 	const char *newname, *raw;
252 	struct fstab *fsp;
253 	int retried = 0;
254 
255 	hot = 0;
256 	if (stat("/", &stslash) < 0) {
257 		perror("/");
258 		printf("Can't stat root\n");
259 		return (origname);
260 	}
261 	newname = origname;
262 retry:
263 	if (stat(newname, &stblock) < 0) {
264 		perror(newname);
265 		printf("Can't stat %s\n", newname);
266 		return (origname);
267 	}
268 	if (S_ISBLK(stblock.st_mode)) {
269 		if (stslash.st_dev == stblock.st_rdev)
270 			hot++;
271 		raw = rawname(newname);
272 		if (stat(raw, &stchar) < 0) {
273 			perror(raw);
274 			printf("Can't stat %s\n", raw);
275 			return (origname);
276 		}
277 		if (S_ISCHR(stchar.st_mode)) {
278 			return (raw);
279 		} else {
280 			printf("%s is not a character device\n", raw);
281 			return (origname);
282 		}
283 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
284 		newname = unrawname(newname);
285 		retried++;
286 		goto retry;
287 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
288 		newname = fsp->fs_spec;
289 		retried++;
290 		goto retry;
291 	}
292 	/*
293 	 * Not a block or character device, just return name and
294 	 * let the user decide whether to use it.
295 	 */
296 	return (origname);
297 }
298 #endif
299 
300