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