Home | History | Annotate | Line # | Download | only in fsck
fsutil.c revision 1.21
      1  1.21  christos /*	$NetBSD: fsutil.c,v 1.21 2012/04/07 04:52:20 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (c) 1990, 1993
      5   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * Redistribution and use in source and binary forms, with or without
      8   1.1  christos  * modification, are permitted provided that the following conditions
      9   1.1  christos  * are met:
     10   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1  christos  *    may be used to endorse or promote products derived from this software
     17   1.1  christos  *    without specific prior written permission.
     18   1.1  christos  *
     19   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1  christos  * SUCH DAMAGE.
     30   1.1  christos  */
     31   1.4     lukem 
     32   1.4     lukem #include <sys/cdefs.h>
     33   1.1  christos #ifndef lint
     34  1.21  christos __RCSID("$NetBSD: fsutil.c,v 1.21 2012/04/07 04:52:20 christos Exp $");
     35   1.1  christos #endif /* not lint */
     36   1.1  christos 
     37  1.11     lukem #include <sys/param.h>
     38  1.11     lukem 
     39   1.1  christos #include <stdio.h>
     40   1.1  christos #include <string.h>
     41   1.1  christos #include <stdlib.h>
     42   1.1  christos #include <stdarg.h>
     43   1.1  christos #include <errno.h>
     44   1.1  christos #include <fstab.h>
     45  1.20  christos #include <fcntl.h>
     46  1.20  christos #include <unistd.h>
     47   1.1  christos #include <err.h>
     48  1.21  christos #include <util.h>
     49   1.1  christos 
     50   1.1  christos #include <sys/types.h>
     51   1.1  christos #include <sys/stat.h>
     52   1.1  christos 
     53   1.1  christos #include "fsutil.h"
     54  1.17  christos #include "exitvalues.h"
     55   1.1  christos 
     56   1.1  christos static const char *dev = NULL;
     57   1.1  christos static int hot = 0;
     58   1.1  christos static int preen = 0;
     59  1.14       dsl int quiet;
     60  1.15  christos #define F_ERROR	0x80000000
     61   1.1  christos 
     62   1.1  christos void
     63  1.10     lukem setcdevname(const char *cd, int pr)
     64   1.1  christos {
     65  1.10     lukem 
     66   1.1  christos 	dev = cd;
     67   1.1  christos 	preen = pr;
     68   1.1  christos }
     69   1.1  christos 
     70   1.1  christos const char *
     71  1.10     lukem cdevname(void)
     72   1.1  christos {
     73  1.10     lukem 
     74   1.1  christos 	return dev;
     75   1.1  christos }
     76   1.1  christos 
     77   1.1  christos int
     78  1.10     lukem hotroot(void)
     79   1.1  christos {
     80  1.10     lukem 
     81   1.1  christos 	return hot;
     82   1.1  christos }
     83   1.1  christos 
     84   1.1  christos /*VARARGS*/
     85   1.1  christos void
     86   1.1  christos errexit(const char *fmt, ...)
     87   1.1  christos {
     88   1.1  christos 	va_list ap;
     89   1.3  christos 
     90   1.1  christos 	va_start(ap, fmt);
     91   1.1  christos 	(void) vfprintf(stderr, fmt, ap);
     92   1.1  christos 	va_end(ap);
     93  1.18     lukem 	(void)fprintf(stderr, "\n");
     94  1.17  christos 	exit(FSCK_EXIT_CHECK_FAILED);
     95   1.1  christos }
     96   1.1  christos 
     97  1.12  perseant void
     98  1.10     lukem vmsg(int fatal, const char *fmt, va_list ap)
     99   1.1  christos {
    100  1.15  christos 	int serr = fatal & F_ERROR;
    101  1.15  christos 	int serrno = errno;
    102  1.15  christos 	fatal &= ~F_ERROR;
    103  1.10     lukem 
    104   1.1  christos 	if (!fatal && preen)
    105  1.14       dsl 		(void)printf("%s: ", dev);
    106  1.14       dsl 	if (quiet && !preen) {
    107  1.14       dsl 		(void)printf("** %s (vmsg)\n", dev);
    108  1.14       dsl 		quiet = 0;
    109  1.14       dsl 	}
    110   1.1  christos 
    111   1.1  christos 	(void) vprintf(fmt, ap);
    112  1.15  christos 	if (serr)
    113  1.15  christos 		printf(" (%s)", strerror(serrno));
    114   1.1  christos 
    115   1.1  christos 	if (fatal && preen)
    116   1.1  christos 		(void) printf("\n");
    117   1.1  christos 
    118   1.1  christos 	if (fatal && preen) {
    119   1.1  christos 		(void) printf(
    120   1.1  christos 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
    121   1.9       cgd 		    dev, getprogname());
    122  1.17  christos 		exit(FSCK_EXIT_CHECK_FAILED);
    123   1.1  christos 	}
    124   1.1  christos }
    125   1.1  christos 
    126   1.1  christos /*VARARGS*/
    127   1.1  christos void
    128   1.1  christos pfatal(const char *fmt, ...)
    129   1.1  christos {
    130   1.1  christos 	va_list ap;
    131   1.3  christos 
    132   1.1  christos 	va_start(ap, fmt);
    133   1.1  christos 	vmsg(1, fmt, ap);
    134   1.1  christos 	va_end(ap);
    135   1.1  christos }
    136   1.1  christos 
    137   1.1  christos /*VARARGS*/
    138   1.1  christos void
    139   1.1  christos pwarn(const char *fmt, ...)
    140   1.1  christos {
    141   1.1  christos 	va_list ap;
    142  1.10     lukem 
    143   1.1  christos 	va_start(ap, fmt);
    144   1.1  christos 	vmsg(0, fmt, ap);
    145   1.1  christos 	va_end(ap);
    146   1.1  christos }
    147   1.1  christos 
    148   1.1  christos void
    149  1.15  christos perr(const char *fmt, ...)
    150   1.1  christos {
    151  1.15  christos 	va_list ap;
    152  1.10     lukem 
    153  1.15  christos 	va_start(ap, fmt);
    154  1.15  christos 	vmsg(1 | F_ERROR, fmt, ap);
    155  1.15  christos 	va_end(ap);
    156   1.1  christos }
    157   1.1  christos 
    158   1.1  christos void
    159   1.1  christos panic(const char *fmt, ...)
    160   1.1  christos {
    161   1.1  christos 	va_list ap;
    162   1.3  christos 
    163   1.1  christos 	va_start(ap, fmt);
    164   1.1  christos 	vmsg(1, fmt, ap);
    165   1.1  christos 	va_end(ap);
    166  1.17  christos 	exit(FSCK_EXIT_CHECK_FAILED);
    167   1.1  christos }
    168   1.1  christos 
    169   1.6   mycroft const char *
    170  1.10     lukem unrawname(const char *name)
    171   1.1  christos {
    172  1.11     lukem 	static char unrawbuf[MAXPATHLEN];
    173   1.6   mycroft 	const char *dp;
    174   1.1  christos 	struct stat stb;
    175   1.1  christos 
    176   1.1  christos 	if ((dp = strrchr(name, '/')) == 0)
    177   1.1  christos 		return (name);
    178   1.1  christos 	if (stat(name, &stb) < 0)
    179   1.1  christos 		return (name);
    180   1.1  christos 	if (!S_ISCHR(stb.st_mode))
    181   1.1  christos 		return (name);
    182   1.1  christos 	if (dp[1] != 'r')
    183   1.1  christos 		return (name);
    184  1.11     lukem 	(void)snprintf(unrawbuf, sizeof(unrawbuf), "%.*s/%s",
    185  1.11     lukem 	    (int)(dp - name), name, dp + 2);
    186   1.6   mycroft 	return (unrawbuf);
    187   1.1  christos }
    188   1.1  christos 
    189   1.6   mycroft const char *
    190  1.10     lukem rawname(const char *name)
    191   1.1  christos {
    192  1.11     lukem 	static char rawbuf[MAXPATHLEN];
    193   1.6   mycroft 	const char *dp;
    194   1.1  christos 
    195   1.1  christos 	if ((dp = strrchr(name, '/')) == 0)
    196   1.1  christos 		return (0);
    197  1.11     lukem 	(void)snprintf(rawbuf, sizeof(rawbuf), "%.*s/r%s",
    198  1.11     lukem 	    (int)(dp - name), name, dp + 1);
    199   1.1  christos 	return (rawbuf);
    200   1.1  christos }
    201   1.1  christos 
    202   1.6   mycroft const char *
    203  1.10     lukem blockcheck(const char *origname)
    204   1.1  christos {
    205   1.1  christos 	struct stat stslash, stblock, stchar;
    206   1.6   mycroft 	const char *newname, *raw;
    207   1.1  christos 	struct fstab *fsp;
    208   1.1  christos 	int retried = 0;
    209   1.1  christos 
    210   1.1  christos 	hot = 0;
    211   1.1  christos 	if (stat("/", &stslash) < 0) {
    212  1.15  christos 		perr("Can't stat `/'");
    213   1.1  christos 		return (origname);
    214   1.1  christos 	}
    215   1.1  christos 	newname = origname;
    216   1.1  christos retry:
    217   1.1  christos 	if (stat(newname, &stblock) < 0) {
    218  1.15  christos 		perr("Can't stat `%s'", newname);
    219   1.1  christos 		return (origname);
    220   1.1  christos 	}
    221   1.1  christos 	if (S_ISBLK(stblock.st_mode)) {
    222   1.1  christos 		if (stslash.st_dev == stblock.st_rdev)
    223   1.1  christos 			hot++;
    224   1.1  christos 		raw = rawname(newname);
    225   1.1  christos 		if (stat(raw, &stchar) < 0) {
    226  1.15  christos 			perr("Can't stat `%s'", raw);
    227   1.1  christos 			return (origname);
    228   1.1  christos 		}
    229   1.1  christos 		if (S_ISCHR(stchar.st_mode)) {
    230   1.1  christos 			return (raw);
    231   1.1  christos 		} else {
    232   1.1  christos 			printf("%s is not a character device\n", raw);
    233   1.1  christos 			return (origname);
    234   1.1  christos 		}
    235   1.1  christos 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
    236   1.1  christos 		newname = unrawname(newname);
    237   1.1  christos 		retried++;
    238   1.1  christos 		goto retry;
    239   1.1  christos 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
    240  1.21  christos 		char buf[MAXPATHLEN];
    241  1.21  christos 		newname = getfsspecname(buf, sizeof(buf), fsp->fs_spec);
    242  1.21  christos 		if (newname == NULL)
    243  1.21  christos 			perr("%s", buf);
    244   1.1  christos 		retried++;
    245   1.1  christos 		goto retry;
    246   1.1  christos 	}
    247   1.1  christos 	/*
    248   1.1  christos 	 * Not a block or character device, just return name and
    249   1.1  christos 	 * let the user decide whether to use it.
    250   1.1  christos 	 */
    251   1.1  christos 	return (origname);
    252   1.1  christos }
    253  1.19  christos 
    254  1.19  christos const char *
    255  1.19  christos print_mtime(time_t t)
    256  1.19  christos {
    257  1.19  christos 	static char b[128];
    258  1.19  christos 	char *p = ctime(&t);
    259  1.19  christos 	if (p != NULL)
    260  1.19  christos 		(void)snprintf(b, sizeof(b), "%12.12s %4.4s ", &p[4], &p[20]);
    261  1.19  christos 	else
    262  1.19  christos 		(void)snprintf(b, sizeof(b), "%lld ", (long long)t);
    263  1.19  christos 	return b;
    264  1.19  christos }
    265  1.20  christos 
    266  1.20  christos 
    267  1.20  christos void
    268  1.20  christos catch(int n)
    269  1.20  christos {
    270  1.20  christos 	if (ckfinish) (*ckfinish)(0);
    271  1.20  christos 	_exit(FSCK_EXIT_SIGNALLED);
    272  1.20  christos }
    273  1.20  christos 
    274  1.20  christos /*
    275  1.20  christos  * When preening, allow a single quit to signal
    276  1.20  christos  * a special exit after filesystem checks complete
    277  1.20  christos  * so that reboot sequence may be interrupted.
    278  1.20  christos  */
    279  1.20  christos void
    280  1.20  christos catchquit(int n)
    281  1.20  christos {
    282  1.20  christos 	static const char msg[] =
    283  1.20  christos 	    "returning to single-user after filesystem check\n";
    284  1.20  christos 	int serrno = errno;
    285  1.20  christos 
    286  1.20  christos 	(void)write(STDOUT_FILENO, msg, sizeof(msg) - 1);
    287  1.20  christos 	returntosingle = 1;
    288  1.20  christos 	(void)signal(SIGQUIT, SIG_DFL);
    289  1.20  christos 	errno = serrno;
    290  1.20  christos }
    291  1.20  christos 
    292  1.20  christos /*
    293  1.20  christos  * Ignore a single quit signal; wait and flush just in case.
    294  1.20  christos  * Used by child processes in preen.
    295  1.20  christos  */
    296  1.20  christos void
    297  1.20  christos voidquit(int n)
    298  1.20  christos {
    299  1.20  christos 	int serrno = errno;
    300  1.20  christos 
    301  1.20  christos 	sleep(1);
    302  1.20  christos 	(void)signal(SIGQUIT, SIG_IGN);
    303  1.20  christos 	(void)signal(SIGQUIT, SIG_DFL);
    304  1.20  christos 	errno = serrno;
    305  1.20  christos }
    306