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