1 /* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 1996 Christos Zoulas. All rights reserved. 7 * Copyright (c) 1980, 1989, 1993, 1994 8 * The Regents of the University of California. All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 34 * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $ 35 */ 36 37 #include <sys/cdefs.h> 38 #include <sys/param.h> 39 #include <sys/mount.h> 40 #include <sys/queue.h> 41 #include <sys/wait.h> 42 #include <sys/disk.h> 43 #include <sys/ioctl.h> 44 45 #include <ctype.h> 46 #include <err.h> 47 #include <fstab.h> 48 #include <fcntl.h> 49 #include <mntopts.h> 50 #include <paths.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "fsutil.h" 58 59 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 60 61 static TAILQ_HEAD(fstypelist, entry) opthead, selhead; 62 63 struct entry { 64 char *type; 65 char *options; 66 TAILQ_ENTRY(entry) entries; 67 }; 68 69 static char *options = NULL; 70 static int flags = 0; 71 static int forceflag = 0; 72 73 static int checkfs(const char *, const char *, const char *, const char *, pid_t *); 74 static int selected(const char *); 75 static void addoption(char *); 76 static const char *getoptions(const char *); 77 static void addentry(struct fstypelist *, const char *, const char *); 78 static void maketypelist(char *); 79 static void catopt(char **, const char *); 80 static void mangle(char *, int *, const char ** volatile *, int *); 81 static const char *getfstype(const char *); 82 static void usage(void) __dead2; 83 static int isok(struct fstab *); 84 85 static struct { 86 const char *ptype; 87 const char *name; 88 } ptype_map[] = { 89 { "ufs", "ffs" }, 90 { "ffs", "ffs" }, 91 { "fat", "msdosfs" }, 92 { "efi", "msdosfs" }, 93 { NULL, NULL }, 94 }; 95 96 int 97 main(int argc, char *argv[]) 98 { 99 struct fstab *fs; 100 int i, rval = 0; 101 const char *vfstype = NULL; 102 char globopt[3]; 103 const char *etc_fstab; 104 105 globopt[0] = '-'; 106 globopt[2] = '\0'; 107 108 TAILQ_INIT(&selhead); 109 TAILQ_INIT(&opthead); 110 111 etc_fstab = NULL; 112 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1) 113 switch (i) { 114 case 'B': 115 if (flags & CHECK_BACKGRD) 116 errx(1, "Cannot specify -B and -F."); 117 flags |= DO_BACKGRD; 118 break; 119 120 case 'd': 121 flags |= CHECK_DEBUG; 122 break; 123 124 case 'v': 125 flags |= CHECK_VERBOSE; 126 break; 127 128 case 'F': 129 if (flags & DO_BACKGRD) 130 errx(1, "Cannot specify -B and -F."); 131 flags |= CHECK_BACKGRD; 132 break; 133 134 case 'p': 135 flags |= CHECK_PREEN; 136 /*FALLTHROUGH*/ 137 case 'C': 138 flags |= CHECK_CLEAN; 139 /*FALLTHROUGH*/ 140 case 'n': 141 case 'y': 142 globopt[1] = i; 143 catopt(&options, globopt); 144 break; 145 146 case 'f': 147 forceflag = 1; 148 globopt[1] = i; 149 catopt(&options, globopt); 150 break; 151 152 case 'l': 153 warnx("Ignoring obsolete -l option\n"); 154 break; 155 156 case 'T': 157 if (*optarg) 158 addoption(optarg); 159 break; 160 161 case 't': 162 if (!TAILQ_EMPTY(&selhead)) 163 errx(1, "only one -t option may be specified."); 164 165 maketypelist(optarg); 166 vfstype = optarg; 167 break; 168 169 case 'c': 170 etc_fstab = optarg; 171 break; 172 173 case '?': 174 default: 175 usage(); 176 /* NOTREACHED */ 177 } 178 179 argc -= optind; 180 argv += optind; 181 182 if (etc_fstab != NULL) 183 setfstab(etc_fstab); 184 185 if (argc == 0) 186 return checkfstab(flags, isok, checkfs); 187 188 #define BADTYPE(type) \ 189 (strcmp(type, FSTAB_RO) && \ 190 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 191 192 193 for (; argc--; argv++) { 194 const char *spec, *mntpt, *type, *cp; 195 char device[MAXPATHLEN]; 196 struct statfs *mntp; 197 198 mntpt = NULL; 199 spec = *argv; 200 cp = strrchr(spec, '/'); 201 if (cp == NULL) { 202 (void)snprintf(device, sizeof(device), "%s%s", 203 _PATH_DEV, spec); 204 spec = device; 205 } 206 mntp = getmntpoint(spec); 207 if (mntp != NULL) { 208 spec = mntp->f_mntfromname; 209 mntpt = mntp->f_mntonname; 210 } 211 if ((fs = getfsfile(spec)) == NULL && 212 (fs = getfsspec(spec)) == NULL) { 213 if (vfstype == NULL) 214 vfstype = getfstype(spec); 215 if (vfstype == NULL) 216 vfstype = "ufs"; 217 type = vfstype; 218 devcheck(spec); 219 } else { 220 spec = fs->fs_spec; 221 type = fs->fs_vfstype; 222 mntpt = fs->fs_file; 223 if (BADTYPE(fs->fs_type)) 224 errx(1, "%s has unknown file system type.", 225 spec); 226 } 227 if ((flags & CHECK_BACKGRD) && 228 checkfs(type, spec, mntpt, "-F", NULL) == 0) { 229 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv); 230 continue; 231 } 232 if ((flags & DO_BACKGRD) && forceflag == 0 && 233 checkfs(type, spec, mntpt, "-F", NULL) != 0) 234 continue; 235 236 rval |= checkfs(type, spec, mntpt, NULL, NULL); 237 } 238 239 return rval; 240 } 241 242 243 static int 244 isok(struct fstab *fs) 245 { 246 int i; 247 248 if (fs->fs_passno == 0) 249 return (0); 250 if (BADTYPE(fs->fs_type)) 251 return (0); 252 if (!selected(fs->fs_vfstype)) 253 return (0); 254 /* If failok, always check now */ 255 if (getfsopt(fs, "failok")) 256 return (1); 257 /* 258 * If the -B flag has been given, then process the needed 259 * background checks. Background checks cannot be run on 260 * file systems that will be mounted read-only or that were 261 * not mounted at boot time (typically those marked `noauto'). 262 * If these basic tests are passed, check with the file system 263 * itself to see if it is willing to do background checking 264 * by invoking its check program with the -F flag. 265 */ 266 if (flags & DO_BACKGRD) { 267 if (!strcmp(fs->fs_type, FSTAB_RO)) 268 return (0); 269 if (getmntpoint(fs->fs_spec) == NULL) 270 return (0); 271 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0)) 272 return (0); 273 return (1); 274 } 275 /* 276 * If the -F flag has been given, then consider deferring the 277 * check to background. Background checks cannot be run on 278 * file systems that will be mounted read-only or that will 279 * not be mounted at boot time (e.g., marked `noauto'). If 280 * these basic tests are passed, check with the file system 281 * itself to see if it is willing to defer to background 282 * checking by invoking its check program with the -F flag. 283 */ 284 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO)) 285 return (1); 286 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--) 287 if (!strncmp(&fs->fs_mntops[i], "noauto", 6)) 288 break; 289 if (i >= 0) 290 return (1); 291 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0) 292 return (1); 293 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec); 294 return (0); 295 } 296 297 298 static int 299 checkfs(const char *pvfstype, const char *spec, const char *mntpt, 300 const char *auxopt, pid_t *pidp) 301 { 302 const char ** volatile argv; 303 pid_t pid; 304 int argc, i, status, maxargc; 305 char *optbuf, execbase[MAXPATHLEN]; 306 char *vfstype = NULL; 307 const char *extra = NULL; 308 309 #ifdef __GNUC__ 310 /* Avoid vfork clobbering */ 311 (void) &optbuf; 312 (void) &vfstype; 313 #endif 314 /* 315 * We convert the vfstype to lowercase and any spaces to underscores 316 * to not confuse the issue 317 * 318 * XXX This is a kludge to make automatic filesystem type guessing 319 * from the disklabel work for "4.2BSD" filesystems. It does a 320 * very limited subset of transliteration to a normalised form of 321 * filesystem name, and we do not seem to enforce a filesystem 322 * name character set. 323 */ 324 vfstype = strdup(pvfstype); 325 if (vfstype == NULL) 326 perr("strdup(pvfstype)"); 327 for (i = 0; i < (int)strlen(vfstype); i++) { 328 vfstype[i] = tolower(vfstype[i]); 329 if (vfstype[i] == ' ') 330 vfstype[i] = '_'; 331 } 332 333 extra = getoptions(vfstype); 334 optbuf = NULL; 335 if (options) 336 catopt(&optbuf, options); 337 if (extra) 338 catopt(&optbuf, extra); 339 if (auxopt) 340 catopt(&optbuf, auxopt); 341 else if (flags & DO_BACKGRD) 342 catopt(&optbuf, "-B"); 343 344 maxargc = 64; 345 argv = emalloc(sizeof(char *) * maxargc); 346 347 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype); 348 argc = 0; 349 argv[argc++] = execbase; 350 if (optbuf) 351 mangle(optbuf, &argc, &argv, &maxargc); 352 argv[argc++] = spec; 353 argv[argc] = NULL; 354 355 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 356 (void)printf("start %s %swait", mntpt, 357 pidp ? "no" : ""); 358 for (i = 0; i < argc; i++) 359 (void)printf(" %s", argv[i]); 360 (void)printf("\n"); 361 } 362 363 switch (pid = vfork()) { 364 case -1: /* Error. */ 365 warn("vfork"); 366 if (optbuf) 367 free(optbuf); 368 free(vfstype); 369 return (1); 370 371 case 0: /* Child. */ 372 if ((flags & CHECK_DEBUG) && auxopt == NULL) 373 _exit(0); 374 375 /* Go find an executable. */ 376 execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv)); 377 if (spec) 378 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH); 379 else 380 warn("exec %s in %s", execbase, _PATH_SYSPATH); 381 _exit(1); 382 /* NOTREACHED */ 383 384 default: /* Parent. */ 385 if (optbuf) 386 free(optbuf); 387 388 free(vfstype); 389 390 if (pidp) { 391 *pidp = pid; 392 return 0; 393 } 394 395 if (waitpid(pid, &status, 0) < 0) { 396 warn("waitpid"); 397 return (1); 398 } 399 400 if (WIFEXITED(status)) { 401 if (WEXITSTATUS(status) != 0) 402 return (WEXITSTATUS(status)); 403 } 404 else if (WIFSIGNALED(status)) { 405 warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 406 return (1); 407 } 408 break; 409 } 410 411 return (0); 412 } 413 414 415 static int 416 selected(const char *type) 417 { 418 struct entry *e; 419 420 /* If no type specified, it's always selected. */ 421 TAILQ_FOREACH(e, &selhead, entries) 422 if (!strncmp(e->type, type, MFSNAMELEN)) 423 return which == IN_LIST ? 1 : 0; 424 425 return which == IN_LIST ? 0 : 1; 426 } 427 428 429 static const char * 430 getoptions(const char *type) 431 { 432 struct entry *e; 433 434 TAILQ_FOREACH(e, &opthead, entries) 435 if (!strncmp(e->type, type, MFSNAMELEN)) 436 return e->options; 437 return ""; 438 } 439 440 441 static void 442 addoption(char *optstr) 443 { 444 char *newoptions; 445 struct entry *e; 446 447 if ((newoptions = strchr(optstr, ':')) == NULL) 448 errx(1, "Invalid option string"); 449 450 *newoptions++ = '\0'; 451 452 TAILQ_FOREACH(e, &opthead, entries) 453 if (!strncmp(e->type, optstr, MFSNAMELEN)) { 454 catopt(&e->options, newoptions); 455 return; 456 } 457 addentry(&opthead, optstr, newoptions); 458 } 459 460 461 static void 462 addentry(struct fstypelist *list, const char *type, const char *opts) 463 { 464 struct entry *e; 465 466 e = emalloc(sizeof(struct entry)); 467 e->type = estrdup(type); 468 e->options = estrdup(opts); 469 TAILQ_INSERT_TAIL(list, e, entries); 470 } 471 472 473 static void 474 maketypelist(char *fslist) 475 { 476 char *ptr; 477 478 if ((fslist == NULL) || (fslist[0] == '\0')) 479 errx(1, "empty type list"); 480 481 if (fslist[0] == 'n' && fslist[1] == 'o') { 482 fslist += 2; 483 which = NOT_IN_LIST; 484 } 485 else 486 which = IN_LIST; 487 488 while ((ptr = strsep(&fslist, ",")) != NULL) 489 addentry(&selhead, ptr, ""); 490 491 } 492 493 494 static void 495 catopt(char **sp, const char *o) 496 { 497 char *s; 498 size_t i, j; 499 500 s = *sp; 501 if (s) { 502 i = strlen(s); 503 j = i + 1 + strlen(o) + 1; 504 s = erealloc(s, j); 505 (void)snprintf(s + i, j, ",%s", o); 506 } else 507 s = estrdup(o); 508 *sp = s; 509 } 510 511 512 static void 513 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp) 514 { 515 char *p, *s; 516 int argc, maxargc; 517 const char **argv; 518 519 argc = *argcp; 520 argv = *argvp; 521 maxargc = *maxargcp; 522 523 for (s = opts; (p = strsep(&s, ",")) != NULL;) { 524 /* Always leave space for one more argument and the NULL. */ 525 if (argc >= maxargc - 3) { 526 maxargc <<= 1; 527 argv = erealloc(argv, maxargc * sizeof(char *)); 528 } 529 if (*p != '\0') { 530 if (*p == '-') { 531 argv[argc++] = p; 532 p = strchr(p, '='); 533 if (p) { 534 *p = '\0'; 535 argv[argc++] = p+1; 536 } 537 } else { 538 argv[argc++] = "-o"; 539 argv[argc++] = p; 540 } 541 } 542 } 543 544 *argcp = argc; 545 *argvp = argv; 546 *maxargcp = maxargc; 547 } 548 549 static const char * 550 getfstype(const char *str) 551 { 552 struct diocgattr_arg attr; 553 int fd, i; 554 555 if ((fd = open(str, O_RDONLY)) == -1) 556 err(1, "cannot open `%s'", str); 557 558 strncpy(attr.name, "PART::type", sizeof(attr.name)); 559 memset(&attr.value, 0, sizeof(attr.value)); 560 attr.len = sizeof(attr.value); 561 if (ioctl(fd, DIOCGATTR, &attr) == -1) { 562 (void) close(fd); 563 return(NULL); 564 } 565 (void) close(fd); 566 for (i = 0; ptype_map[i].ptype != NULL; i++) 567 if (strstr(attr.value.str, ptype_map[i].ptype) != NULL) 568 return (ptype_map[i].name); 569 return (NULL); 570 } 571 572 573 static void 574 usage(void) 575 { 576 static const char common[] = 577 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]"; 578 579 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n", 580 getprogname(), common); 581 exit(1); 582 } 583