fsutil.c (4975B)
1 /* $NetBSD: fsutil.c,v 1.27 2020/04/03 19:36:32 joerg 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 #ifndef lint 33 __RCSID("$NetBSD: fsutil.c,v 1.27 2020/04/03 19:36:32 joerg Exp $"); 34 #endif /* not lint */ 35 36 /* 37 * used by sbin/fsck 38 * used by sbin/fsck_ext2fs 39 * used by sbin/fsck_ffs 40 * used by sbin/fsck_lfs 41 * used by sbin/fsck_msdos 42 * used by sbin/fsck_v7fs 43 * used by sbin/fsdb 44 * used by usr.sbin/quotacheck 45 */ 46 47 #include <sys/param.h> 48 49 #include <stdio.h> 50 #include <time.h> 51 #include <string.h> 52 #include <stdlib.h> 53 #include <stdarg.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <unistd.h> 57 #include <err.h> 58 59 #include <sys/types.h> 60 #include <sys/stat.h> 61 62 #include "fsutil.h" 63 #include "exitvalues.h" 64 65 extern int unresolved; 66 67 volatile sig_atomic_t returntosingle; 68 69 void (*ckfinish)(int); 70 71 static const char *dev = NULL; 72 static int hot = 0; 73 static int preen = 0; 74 int quiet; /* don't report clean filesystems */ 75 #define F_ERROR 0x80000000 76 77 void 78 setcdevname(const char *cd, int pr) 79 { 80 dev = cd; 81 preen = pr; 82 } 83 84 const char * 85 cdevname(void) 86 { 87 return dev; 88 } 89 90 int 91 hotroot(void) 92 { 93 return hot; 94 } 95 96 /*VARARGS*/ 97 void 98 errexit(const char *fmt, ...) 99 { 100 va_list ap; 101 102 va_start(ap, fmt); 103 (void)vfprintf(stderr, fmt, ap); 104 va_end(ap); 105 (void)fprintf(stderr, "\n"); 106 exit(FSCK_EXIT_CHECK_FAILED); 107 } 108 109 void 110 vmsg(int fatal, const char *fmt, va_list ap) 111 { 112 int serr = fatal & F_ERROR; 113 int serrno = errno; 114 fatal &= ~F_ERROR; 115 116 if (!fatal && preen) 117 (void)printf("%s: ", dev); 118 if (quiet && !preen) { 119 (void)printf("** %s (vmsg)\n", dev); 120 quiet = 0; 121 } 122 123 (void)vprintf(fmt, ap); 124 if (serr) 125 printf(" (%s)", strerror(serrno)); 126 127 if (fatal && preen) 128 (void)printf("\n"); 129 130 if (fatal && preen) { 131 (void)printf("%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", dev, getprogname()); 132 exit(FSCK_EXIT_CHECK_FAILED); 133 } 134 } 135 136 /*VARARGS*/ 137 void 138 pfatal(const char *fmt, ...) 139 { 140 va_list ap; 141 142 unresolved = 1; 143 144 va_start(ap, fmt); 145 vmsg(1, fmt, ap); 146 va_end(ap); 147 } 148 149 /*VARARGS*/ 150 void 151 pwarn(const char *fmt, ...) 152 { 153 va_list ap; 154 155 va_start(ap, fmt); 156 vmsg(0, fmt, ap); 157 va_end(ap); 158 } 159 160 void 161 perr(const char *fmt, ...) 162 { 163 va_list ap; 164 165 va_start(ap, fmt); 166 vmsg(1 | F_ERROR, fmt, ap); 167 va_end(ap); 168 } 169 170 void 171 panic(const char *fmt, ...) 172 { 173 va_list ap; 174 175 va_start(ap, fmt); 176 vmsg(1, fmt, ap); 177 va_end(ap); 178 exit(FSCK_EXIT_CHECK_FAILED); 179 } 180 181 const char * 182 print_mtime(time_t t) 183 { 184 static char b[128]; 185 char *p = ctime(&t); 186 if (p != NULL) 187 (void)snprintf(b, sizeof(b), "%12.12s %4.4s ", &p[4], &p[20]); 188 else 189 (void)snprintf(b, sizeof(b), "%lld ", (long long)t); 190 return b; 191 } 192 193 void catch (int n) 194 { 195 if (ckfinish) 196 (*ckfinish)(0); 197 _exit(FSCK_EXIT_SIGNALLED); 198 } 199 200 /* 201 * When preening, allow a single quit to signal 202 * a special exit after filesystem checks complete 203 * so that reboot sequence may be interrupted. 204 */ 205 void 206 catchquit(int n) 207 { 208 static const char msg[] = "returning to single-user after filesystem check\n"; 209 int serrno = errno; 210 211 (void)write(STDOUT_FILENO, msg, sizeof(msg) - 1); 212 returntosingle = 1; 213 (void)signal(SIGQUIT, SIG_DFL); 214 errno = serrno; 215 } 216 217 /* 218 * Ignore a single quit signal; wait and flush just in case. 219 * Used by child processes in preen. 220 */ 221 void 222 voidquit(int n) 223 { 224 int serrno = errno; 225 226 sleep(1); 227 (void)signal(SIGQUIT, SIG_IGN); 228 (void)signal(SIGQUIT, SIG_DFL); 229 errno = serrno; 230 }