1 /* $NetBSD: fsck.c,v 1.51 2012/04/07 04:52:20 christos 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94 33 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 34 * 35 */ 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: fsck.c,v 1.51 2012/04/07 04:52:20 christos Exp $"); 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/mount.h> 44 #include <sys/queue.h> 45 #include <sys/wait.h> 46 #define FSTYPENAMES 47 #define FSCKNAMES 48 #include <sys/disk.h> 49 #include <sys/disklabel.h> 50 #include <sys/ioctl.h> 51 52 #include <err.h> 53 #include <errno.h> 54 #include <fstab.h> 55 #include <fcntl.h> 56 #include <paths.h> 57 #include <signal.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <util.h> 63 64 #include "pathnames.h" 65 #include "fsutil.h" 66 #include "exitvalues.h" 67 68 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 69 70 TAILQ_HEAD(fstypelist, entry) opthead, selhead, omhead; 71 72 struct entry { 73 char *type; 74 char *options; 75 TAILQ_ENTRY(entry) entries; 76 }; 77 78 static int maxrun = 0; 79 static char *options = NULL; 80 static int flags = 0; 81 82 static int checkfs(const char *, const char *, const char *, void *, pid_t *); 83 static int selected(const char *); 84 static int omitted(const char *); 85 static void addoption(char *); 86 static const char *getoptions(const char *); 87 static void addentry(struct fstypelist *, const char *, const char *); 88 static void maketypelist(char *); 89 static void catopt(char **, const char *); 90 static void mangle(char *, int *, const char ** volatile *, int *); 91 static const char *getfslab(const char *); 92 __dead static void usage(void); 93 static void *isok(struct fstab *); 94 95 int 96 main(int argc, char *argv[]) 97 { 98 struct fstab *fs; 99 int i, rval; 100 const char *vfstype = NULL; 101 char globopt[3]; 102 int ret = FSCK_EXIT_OK; 103 char buf[MAXPATHLEN]; 104 105 globopt[0] = '-'; 106 globopt[2] = '\0'; 107 108 TAILQ_INIT(&selhead); 109 TAILQ_INIT(&opthead); 110 TAILQ_INIT(&omhead); 111 112 while ((i = getopt(argc, argv, "dfl:nPpqT:t:vx:y")) != -1) { 113 switch (i) { 114 case 'd': 115 flags |= CHECK_DEBUG; 116 continue; 117 118 case 'f': 119 flags |= CHECK_FORCE; 120 break; 121 122 case 'n': 123 flags |= CHECK_NOFIX; 124 break; 125 126 case 'p': 127 flags |= CHECK_PREEN; 128 break; 129 130 case 'P': 131 flags |= CHECK_PROGRESS; 132 break; 133 134 case 'q': 135 break; 136 137 case 'l': 138 maxrun = atoi(optarg); 139 continue; 140 141 case 'T': 142 if (*optarg) 143 addoption(optarg); 144 continue; 145 146 case 't': 147 if (TAILQ_FIRST(&selhead) != NULL) 148 errx(1, "only one -t option may be specified."); 149 150 maketypelist(optarg); 151 vfstype = optarg; 152 continue; 153 154 case 'v': 155 flags |= CHECK_VERBOSE; 156 continue; 157 158 case 'x': 159 addentry(&omhead, optarg, ""); 160 continue; 161 162 case 'y': 163 break; 164 165 case '?': 166 default: 167 usage(); 168 /* NOTREACHED */ 169 } 170 171 /* Pass option to fsck_xxxfs */ 172 globopt[1] = i; 173 catopt(&options, globopt); 174 } 175 176 /* Don't do progress meters if we're debugging. */ 177 if (flags & CHECK_DEBUG) 178 flags &= ~CHECK_PROGRESS; 179 180 /* 181 * If progress meters are being used, force max parallel to 1 182 * so the progress meter outputs don't interfere with one another. 183 */ 184 if (flags & CHECK_PROGRESS) 185 maxrun = 1; 186 187 #if defined(__minix) 188 /* parallel checking heuristic doesn't work for minix currently */ 189 maxrun = 1; 190 #endif /* !defined(__minix) */ 191 argc -= optind; 192 argv += optind; 193 194 if (argc == 0) 195 return checkfstab(flags, maxrun, isok, checkfs); 196 197 #define BADTYPE(type) \ 198 (strcmp(type, FSTAB_RO) && \ 199 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 200 201 202 for (; argc--; argv++) { 203 const char *spec, *type, *cp; 204 char device[MAXPATHLEN]; 205 206 spec = *argv; 207 cp = strrchr(spec, '/'); 208 if (cp == 0) { 209 (void)snprintf(device, sizeof(device), "%s%s", 210 _PATH_DEV, spec); 211 spec = device; 212 } 213 if ((fs = getfsfile(spec)) == NULL && 214 (fs = getfsspec(spec)) == NULL) { 215 if (vfstype == NULL) 216 vfstype = getfslab(spec); 217 type = vfstype; 218 } 219 else { 220 spec = getfsspecname(buf, sizeof(buf), fs->fs_spec); 221 if (spec == NULL) 222 err(FSCK_EXIT_CHECK_FAILED, "%s", buf); 223 type = fs->fs_vfstype; 224 if (BADTYPE(fs->fs_type)) 225 errx(FSCK_EXIT_CHECK_FAILED, 226 "%s has unknown file system type.", 227 spec); 228 } 229 230 rval = checkfs(type, blockcheck(spec), *argv, NULL, NULL); 231 if (rval > ret) 232 ret = rval; 233 } 234 235 return ret; 236 } 237 238 239 static void * 240 isok(struct fstab *fs) 241 { 242 243 if (fs->fs_passno == 0) 244 return NULL; 245 246 if (BADTYPE(fs->fs_type)) 247 return NULL; 248 249 if (!selected(fs->fs_vfstype)) 250 return NULL; 251 252 if (omitted(fs->fs_file)) 253 return NULL; 254 255 return fs; 256 } 257 258 259 static int 260 checkfs(const char *vfst, const char *spec, const char *mntpt, void *auxarg, 261 pid_t *pidp) 262 { 263 /* List of directories containing fsck_xxx subcommands. */ 264 static const char *edirs[] = { 265 #ifdef RESCUEDIR 266 RESCUEDIR, 267 #endif 268 _PATH_SBIN, 269 _PATH_USRSBIN, 270 #if defined(__minix) 271 "/usr/pkg/sbin/", 272 #endif /* defined(__minix) */ 273 NULL 274 }; 275 const char ** volatile argv, **edir; 276 const char * volatile vfstype = vfst; 277 pid_t pid; 278 int argc, i, status, maxargc; 279 char *optb; 280 char *volatile optbuf; 281 char execname[MAXPATHLEN + 1], execbase[MAXPATHLEN]; 282 const char *extra = getoptions(vfstype); 283 284 if (!strcmp(vfstype, "ufs")) 285 vfstype = MOUNT_UFS; 286 287 optb = NULL; 288 if (options) 289 catopt(&optb, options); 290 if (extra) 291 catopt(&optb, extra); 292 optbuf = optb; 293 294 maxargc = 64; 295 argv = emalloc(sizeof(char *) * maxargc); 296 297 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype); 298 argc = 0; 299 argv[argc++] = execbase; 300 if (optbuf) 301 mangle(optbuf, &argc, &argv, &maxargc); 302 argv[argc++] = spec; 303 argv[argc] = NULL; 304 305 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 306 (void)printf("start %s %swait", mntpt, 307 pidp ? "no" : ""); 308 for (i = 0; i < argc; i++) 309 (void)printf(" %s", argv[i]); 310 (void)printf("\n"); 311 } 312 313 switch (pid = vfork()) { 314 case -1: /* Error. */ 315 warn("vfork"); 316 if (optbuf) 317 free(optbuf); 318 free(argv); 319 return FSCK_EXIT_CHECK_FAILED; 320 321 case 0: /* Child. */ 322 if ((flags & CHECK_FORCE) == 0) { 323 struct statvfs sfs; 324 325 /* 326 * if mntpt is a mountpoint of a mounted file 327 * system and it's mounted read-write, skip it 328 * unless -f is given. 329 */ 330 if ((statvfs(mntpt, &sfs) == 0) && 331 (strcmp(mntpt, sfs.f_mntonname) == 0) && 332 ((sfs.f_flag & MNT_RDONLY) == 0)) { 333 printf( 334 "%s: file system is mounted read-write on %s; not checking\n", 335 spec, mntpt); 336 if ((flags & CHECK_PREEN) && auxarg != NULL) 337 _exit(FSCK_EXIT_OK); /* fsck -p */ 338 else 339 _exit(FSCK_EXIT_CHECK_FAILED); /* fsck [[-p] ...] */ 340 } 341 } 342 343 if (flags & CHECK_DEBUG) 344 _exit(FSCK_EXIT_OK); 345 346 /* Go find an executable. */ 347 edir = edirs; 348 do { 349 (void)snprintf(execname, 350 sizeof(execname), "%s/%s", *edir, execbase); 351 execv(execname, (char * const *)__UNCONST(argv)); 352 if (errno != ENOENT) { 353 if (spec) 354 warn("exec %s for %s", execname, spec); 355 else 356 warn("exec %s", execname); 357 } 358 } while (*++edir != NULL); 359 360 if (errno == ENOENT) { 361 if (spec) 362 warn("exec %s for %s", execname, spec); 363 else 364 warn("exec %s", execname); 365 } 366 _exit(FSCK_EXIT_CHECK_FAILED); 367 /* NOTREACHED */ 368 369 default: /* Parent. */ 370 if (optbuf) 371 free(optbuf); 372 free(argv); 373 374 if (pidp) { 375 *pidp = pid; 376 return FSCK_EXIT_OK; 377 } 378 379 if (waitpid(pid, &status, 0) < 0) { 380 warn("waitpid"); 381 return FSCK_EXIT_CHECK_FAILED; 382 } 383 384 if (WIFEXITED(status)) { 385 if (WEXITSTATUS(status) != 0) 386 return WEXITSTATUS(status); 387 } 388 else if (WIFSIGNALED(status)) { 389 warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 390 return FSCK_EXIT_CHECK_FAILED; 391 } 392 break; 393 } 394 395 return FSCK_EXIT_OK; 396 } 397 398 399 static int 400 selected(const char *type) 401 { 402 struct entry *e; 403 404 /* If no type specified, it's always selected. */ 405 TAILQ_FOREACH(e, &selhead, entries) 406 if (!strcmp(e->type, type)) 407 return which == IN_LIST ? 1 : 0; 408 409 return which == IN_LIST ? 0 : 1; 410 } 411 412 413 static int 414 omitted(const char *mountedon) 415 { 416 struct entry *e; 417 418 /* If no type specified, it's always selected. */ 419 TAILQ_FOREACH(e, &omhead, entries) 420 if (!strcmp(e->type, mountedon)) 421 return 1; 422 423 return 0; 424 } 425 426 427 static const char * 428 getoptions(const char *type) 429 { 430 struct entry *e; 431 432 TAILQ_FOREACH(e, &opthead, entries) 433 if (!strcmp(e->type, type)) 434 return e->options; 435 return ""; 436 } 437 438 439 static void 440 addoption(char *optstr) 441 { 442 char *newoptions; 443 struct entry *e; 444 445 if ((newoptions = strchr(optstr, ':')) == NULL) 446 errx(1, "Invalid option string"); 447 448 *newoptions++ = '\0'; 449 450 TAILQ_FOREACH(e, &opthead, entries) 451 if (!strcmp(e->type, optstr)) { 452 catopt(&e->options, newoptions); 453 return; 454 } 455 addentry(&opthead, optstr, newoptions); 456 } 457 458 459 static void 460 addentry(struct fstypelist *list, const char *type, const char *opts) 461 { 462 struct entry *e; 463 464 e = emalloc(sizeof(struct entry)); 465 e->type = estrdup(type); 466 e->options = estrdup(opts); 467 TAILQ_INSERT_TAIL(list, e, entries); 468 } 469 470 471 static void 472 maketypelist(char *fslist) 473 { 474 char *ptr; 475 476 if ((fslist == NULL) || (fslist[0] == '\0')) 477 errx(1, "empty type list"); 478 479 if (fslist[0] == 'n' && fslist[1] == 'o') { 480 fslist += 2; 481 which = NOT_IN_LIST; 482 } 483 else 484 which = IN_LIST; 485 486 while ((ptr = strsep(&fslist, ",")) != NULL) 487 addentry(&selhead, ptr, ""); 488 489 } 490 491 492 static void 493 catopt(char **sp, const char *o) 494 { 495 char *s; 496 size_t i, j; 497 498 s = *sp; 499 if (s) { 500 i = strlen(s); 501 j = i + 1 + strlen(o) + 1; 502 s = erealloc(s, j); 503 (void)snprintf(s + i, j, ",%s", o); 504 } else 505 s = estrdup(o); 506 *sp = s; 507 } 508 509 510 static void 511 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp) 512 { 513 char *p, *s; 514 int argc, maxargc; 515 const char **argv; 516 517 argc = *argcp; 518 argv = *argvp; 519 maxargc = *maxargcp; 520 521 for (s = opts; (p = strsep(&s, ",")) != NULL;) { 522 /* Always leave space for one more argument and the NULL. */ 523 if (argc >= maxargc - 3) { 524 maxargc <<= 1; 525 argv = erealloc(argv, maxargc * sizeof(char *)); 526 } 527 if (*p != '\0') { 528 if (*p == '-') { 529 argv[argc++] = p; 530 p = strchr(p, '='); 531 if (p) { 532 *p = '\0'; 533 argv[argc++] = p+1; 534 } 535 } else { 536 argv[argc++] = "-o"; 537 argv[argc++] = p; 538 } 539 } 540 } 541 542 *argcp = argc; 543 *argvp = argv; 544 *maxargcp = maxargc; 545 } 546 547 static const char * 548 getfslab(const char *str) 549 { 550 #if defined(__minix) 551 errx(1, "cannot determine vfstype under minix"); 552 #else 553 static struct dkwedge_info dkw; 554 struct disklabel dl; 555 int fd; 556 char p; 557 const char *vfstype; 558 u_char t; 559 560 /* deduce the file system type from the disk label */ 561 if ((fd = open(str, O_RDONLY)) == -1) 562 err(1, "cannot open `%s'", str); 563 564 /* First check to see if it's a wedge. */ 565 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) { 566 /* Yup, this is easy. */ 567 (void) close(fd); 568 return (dkw.dkw_ptype); 569 } 570 571 if (ioctl(fd, DIOCGDINFO, &dl) == -1) 572 err(1, "cannot get disklabel for `%s'", str); 573 574 (void) close(fd); 575 576 p = str[strlen(str) - 1]; 577 578 if ((p - 'a') >= dl.d_npartitions) 579 errx(1, "partition `%s' is not defined on disk", str); 580 581 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES) 582 errx(1, "partition `%s' is not of a legal vfstype", 583 str); 584 585 if ((vfstype = fscknames[t]) == NULL) 586 errx(1, "vfstype `%s' on partition `%s' is not supported", 587 fstypenames[t], str); 588 589 return vfstype; 590 #endif /* defined(__minix) */ 591 } 592 593 594 static void 595 usage(void) 596 { 597 static const char common[] = 598 "[-dfnPpqvy] [-x excludemount] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]"; 599 600 (void)fprintf(stderr, "usage: %s %s [special|node]...\n", 601 getprogname(), common); 602 exit(FSCK_EXIT_USAGE); 603 } 604