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