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