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