xref: /netbsd-src/sbin/fsck/fsutil.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: fsutil.c,v 1.26 2015/06/21 04:01:40 dholland 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 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsutil.c,v 1.26 2015/06/21 04:01:40 dholland Exp $");
35 #endif /* not lint */
36 
37 /*
38  * used by sbin/fsck
39  * used by sbin/fsck_ext2fs
40  * used by sbin/fsck_ffs
41  * used by sbin/fsck_lfs
42  * used by sbin/fsck_msdos
43  * used by sbin/fsck_v7fs
44  * used by sbin/fsdb
45  * used by usr.sbin/quotacheck
46  */
47 
48 #include <sys/param.h>
49 
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <stdarg.h>
54 #include <errno.h>
55 #include <fstab.h>
56 #include <fcntl.h>
57 #include <unistd.h>
58 #include <err.h>
59 #include <util.h>
60 
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 
64 #include "fsutil.h"
65 #include "exitvalues.h"
66 
67 volatile sig_atomic_t returntosingle;
68 
69 static const char *dev = NULL;
70 static int hot = 0;
71 static int preen = 0;
72 int quiet;
73 #define F_ERROR	0x80000000
74 
75 void
76 setcdevname(const char *cd, int pr)
77 {
78 
79 	dev = cd;
80 	preen = pr;
81 }
82 
83 const char *
84 cdevname(void)
85 {
86 
87 	return dev;
88 }
89 
90 int
91 hotroot(void)
92 {
93 
94 	return hot;
95 }
96 
97 /*VARARGS*/
98 void
99 errexit(const char *fmt, ...)
100 {
101 	va_list ap;
102 
103 	va_start(ap, fmt);
104 	(void) vfprintf(stderr, fmt, ap);
105 	va_end(ap);
106 	(void)fprintf(stderr, "\n");
107 	exit(FSCK_EXIT_CHECK_FAILED);
108 }
109 
110 void
111 vmsg(int fatal, const char *fmt, va_list ap)
112 {
113 	int serr = fatal & F_ERROR;
114 	int serrno = errno;
115 	fatal &= ~F_ERROR;
116 
117 	if (!fatal && preen)
118 		(void)printf("%s: ", dev);
119 	if (quiet && !preen) {
120 		(void)printf("** %s (vmsg)\n", dev);
121 		quiet = 0;
122 	}
123 
124 	(void) vprintf(fmt, ap);
125 	if (serr)
126 		printf(" (%s)", strerror(serrno));
127 
128 	if (fatal && preen)
129 		(void) printf("\n");
130 
131 	if (fatal && preen) {
132 		(void) printf(
133 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
134 		    dev, getprogname());
135 		exit(FSCK_EXIT_CHECK_FAILED);
136 	}
137 }
138 
139 /*VARARGS*/
140 void
141 pfatal(const char *fmt, ...)
142 {
143 	va_list ap;
144 
145 	va_start(ap, fmt);
146 	vmsg(1, fmt, ap);
147 	va_end(ap);
148 }
149 
150 /*VARARGS*/
151 void
152 pwarn(const char *fmt, ...)
153 {
154 	va_list ap;
155 
156 	va_start(ap, fmt);
157 	vmsg(0, fmt, ap);
158 	va_end(ap);
159 }
160 
161 void
162 perr(const char *fmt, ...)
163 {
164 	va_list ap;
165 
166 	va_start(ap, fmt);
167 	vmsg(1 | F_ERROR, fmt, ap);
168 	va_end(ap);
169 }
170 
171 void
172 panic(const char *fmt, ...)
173 {
174 	va_list ap;
175 
176 	va_start(ap, fmt);
177 	vmsg(1, fmt, ap);
178 	va_end(ap);
179 	exit(FSCK_EXIT_CHECK_FAILED);
180 }
181 
182 const char *
183 blockcheck(const char *origname)
184 {
185 	struct stat stslash, stblock, stchar;
186 	const char *newname, *raw, *cooked;
187 	struct fstab *fsp;
188 	int retried = 0;
189 	ssize_t len;
190 	char cbuf[MAXPATHLEN];
191 	static char buf[MAXPATHLEN];
192 
193 	hot = 0;
194 	if (stat("/", &stslash) < 0) {
195 		perr("Can't stat `/'");
196 		return (origname);
197 	}
198 	len = readlink(origname, cbuf, sizeof(cbuf)-1);
199 	if (len == -1) {
200 		newname = origname;
201 	} else {
202 		cbuf[len] = '\0';
203 		newname = cbuf;
204 	}
205 retry:
206 	if (stat(newname, &stblock) < 0) {
207 		perr("Can't stat `%s'", newname);
208 		return origname;
209 	}
210 	if (S_ISBLK(stblock.st_mode)) {
211 		if (stslash.st_dev == stblock.st_rdev)
212 			hot++;
213 		raw = getdiskrawname(buf, sizeof(buf), newname);
214 		if (raw == NULL) {
215 			perr("Can't convert to raw `%s'", newname);
216 			return origname;
217 		}
218 		if (stat(raw, &stchar) < 0) {
219 			perr("Can't stat `%s'", raw);
220 			return origname;
221 		}
222 		if (S_ISCHR(stchar.st_mode)) {
223 			return raw;
224 		} else {
225 			perr("%s is not a character device\n", raw);
226 			return origname;
227 		}
228 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
229 		cooked = getdiskcookedname(cbuf, sizeof(cbuf), newname);
230 		if (cooked == NULL) {
231 			perr("Can't convert to cooked `%s'", newname);
232 			return origname;
233 		} else
234 			newname = cooked;
235 		retried++;
236 		goto retry;
237 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
238 		newname = getfsspecname(cbuf, sizeof(cbuf), fsp->fs_spec);
239 		if (newname == NULL)
240 			perr("%s", buf);
241 		retried++;
242 		goto retry;
243 	}
244 	/*
245 	 * Not a block or character device, just return name and
246 	 * let the user decide whether to use it.
247 	 */
248 	return origname;
249 }
250 
251 const char *
252 print_mtime(time_t t)
253 {
254 	static char b[128];
255 	char *p = ctime(&t);
256 	if (p != NULL)
257 		(void)snprintf(b, sizeof(b), "%12.12s %4.4s ", &p[4], &p[20]);
258 	else
259 		(void)snprintf(b, sizeof(b), "%lld ", (long long)t);
260 	return b;
261 }
262 
263 
264 void
265 catch(int n)
266 {
267 	if (ckfinish) (*ckfinish)(0);
268 	_exit(FSCK_EXIT_SIGNALLED);
269 }
270 
271 /*
272  * When preening, allow a single quit to signal
273  * a special exit after filesystem checks complete
274  * so that reboot sequence may be interrupted.
275  */
276 void
277 catchquit(int n)
278 {
279 	static const char msg[] =
280 	    "returning to single-user after filesystem check\n";
281 	int serrno = errno;
282 
283 	(void)write(STDOUT_FILENO, msg, sizeof(msg) - 1);
284 	returntosingle = 1;
285 	(void)signal(SIGQUIT, SIG_DFL);
286 	errno = serrno;
287 }
288 
289 /*
290  * Ignore a single quit signal; wait and flush just in case.
291  * Used by child processes in preen.
292  */
293 void
294 voidquit(int n)
295 {
296 	int serrno = errno;
297 
298 	sleep(1);
299 	(void)signal(SIGQUIT, SIG_IGN);
300 	(void)signal(SIGQUIT, SIG_DFL);
301 	errno = serrno;
302 }
303