1 /* $NetBSD: fsck.c,v 1.49 2010/02/24 13:56:07 hannken 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.49 2010/02/24 13:56:07 hannken 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 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 104 globopt[0] = '-'; 105 globopt[2] = '\0'; 106 107 TAILQ_INIT(&selhead); 108 TAILQ_INIT(&opthead); 109 TAILQ_INIT(&omhead); 110 111 while ((i = getopt(argc, argv, "dfl:nPpqT:t:vx:y")) != -1) { 112 switch (i) { 113 case 'd': 114 flags |= CHECK_DEBUG; 115 continue; 116 117 case 'f': 118 flags |= CHECK_FORCE; 119 break; 120 121 case 'n': 122 flags |= CHECK_NOFIX; 123 break; 124 125 case 'p': 126 flags |= CHECK_PREEN; 127 break; 128 129 case 'P': 130 flags |= CHECK_PROGRESS; 131 break; 132 133 case 'q': 134 break; 135 136 case 'l': 137 maxrun = atoi(optarg); 138 continue; 139 140 case 'T': 141 if (*optarg) 142 addoption(optarg); 143 continue; 144 145 case 't': 146 if (TAILQ_FIRST(&selhead) != NULL) 147 errx(1, "only one -t option may be specified."); 148 149 maketypelist(optarg); 150 vfstype = optarg; 151 continue; 152 153 case 'v': 154 flags |= CHECK_VERBOSE; 155 continue; 156 157 case 'x': 158 addentry(&omhead, optarg, ""); 159 continue; 160 161 case 'y': 162 break; 163 164 case '?': 165 default: 166 usage(); 167 /* NOTREACHED */ 168 } 169 170 /* Pass option to fsck_xxxfs */ 171 globopt[1] = i; 172 catopt(&options, globopt); 173 } 174 175 /* Don't do progress meters if we're debugging. */ 176 if (flags & CHECK_DEBUG) 177 flags &= ~CHECK_PROGRESS; 178 179 /* 180 * If progress meters are being used, force max parallel to 1 181 * so the progress meter outputs don't interfere with one another. 182 */ 183 if (flags & CHECK_PROGRESS) 184 maxrun = 1; 185 186 argc -= optind; 187 argv += optind; 188 189 if (argc == 0) 190 return checkfstab(flags, maxrun, isok, checkfs); 191 192 #define BADTYPE(type) \ 193 (strcmp(type, FSTAB_RO) && \ 194 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 195 196 197 for (; argc--; argv++) { 198 const char *spec, *type, *cp; 199 char device[MAXPATHLEN]; 200 201 spec = *argv; 202 cp = strrchr(spec, '/'); 203 if (cp == 0) { 204 (void)snprintf(device, sizeof(device), "%s%s", 205 _PATH_DEV, spec); 206 spec = device; 207 } 208 if ((fs = getfsfile(spec)) == NULL && 209 (fs = getfsspec(spec)) == NULL) { 210 if (vfstype == NULL) 211 vfstype = getfslab(spec); 212 type = vfstype; 213 } 214 else { 215 spec = fs->fs_spec; 216 type = fs->fs_vfstype; 217 if (BADTYPE(fs->fs_type)) 218 errx(FSCK_EXIT_CHECK_FAILED, 219 "%s has unknown file system type.", 220 spec); 221 } 222 223 rval = checkfs(type, blockcheck(spec), *argv, NULL, NULL); 224 if (rval > ret) 225 ret = rval; 226 } 227 228 return ret; 229 } 230 231 232 static void * 233 isok(struct fstab *fs) 234 { 235 236 if (fs->fs_passno == 0) 237 return NULL; 238 239 if (BADTYPE(fs->fs_type)) 240 return NULL; 241 242 if (!selected(fs->fs_vfstype)) 243 return NULL; 244 245 if (omitted(fs->fs_file)) 246 return NULL; 247 248 return fs; 249 } 250 251 252 static int 253 checkfs(const char *vfst, const char *spec, const char *mntpt, void *auxarg, 254 pid_t *pidp) 255 { 256 /* List of directories containing fsck_xxx subcommands. */ 257 static const char *edirs[] = { 258 #ifdef RESCUEDIR 259 RESCUEDIR, 260 #endif 261 _PATH_SBIN, 262 _PATH_USRSBIN, 263 NULL 264 }; 265 const char ** volatile argv, **edir; 266 const char * volatile vfstype = vfst; 267 pid_t pid; 268 int argc, i, status, maxargc; 269 char *optb; 270 char *volatile optbuf; 271 char execname[MAXPATHLEN + 1], execbase[MAXPATHLEN]; 272 const char *extra = getoptions(vfstype); 273 274 if (!strcmp(vfstype, "ufs")) 275 vfstype = MOUNT_UFS; 276 277 optb = NULL; 278 if (options) 279 catopt(&optb, options); 280 if (extra) 281 catopt(&optb, extra); 282 optbuf = optb; 283 284 maxargc = 64; 285 argv = emalloc(sizeof(char *) * maxargc); 286 287 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype); 288 argc = 0; 289 argv[argc++] = execbase; 290 if (optbuf) 291 mangle(optbuf, &argc, &argv, &maxargc); 292 argv[argc++] = spec; 293 argv[argc] = NULL; 294 295 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 296 (void)printf("start %s %swait", mntpt, 297 pidp ? "no" : ""); 298 for (i = 0; i < argc; i++) 299 (void)printf(" %s", argv[i]); 300 (void)printf("\n"); 301 } 302 303 switch (pid = vfork()) { 304 case -1: /* Error. */ 305 warn("vfork"); 306 if (optbuf) 307 free(optbuf); 308 free(argv); 309 return FSCK_EXIT_CHECK_FAILED; 310 311 case 0: /* Child. */ 312 if ((flags & CHECK_FORCE) == 0) { 313 struct statvfs sfs; 314 315 /* 316 * if mntpt is a mountpoint of a mounted file 317 * system and it's mounted read-write, skip it 318 * unless -f is given. 319 */ 320 if ((statvfs(mntpt, &sfs) == 0) && 321 (strcmp(mntpt, sfs.f_mntonname) == 0) && 322 ((sfs.f_flag & MNT_RDONLY) == 0)) { 323 printf( 324 "%s: file system is mounted read-write on %s; not checking\n", 325 spec, mntpt); 326 if ((flags & CHECK_PREEN) && auxarg != NULL) 327 _exit(FSCK_EXIT_OK); /* fsck -p */ 328 else 329 _exit(FSCK_EXIT_CHECK_FAILED); /* fsck [[-p] ...] */ 330 } 331 } 332 333 if (flags & CHECK_DEBUG) 334 _exit(FSCK_EXIT_OK); 335 336 /* Go find an executable. */ 337 edir = edirs; 338 do { 339 (void)snprintf(execname, 340 sizeof(execname), "%s/%s", *edir, execbase); 341 execv(execname, (char * const *)__UNCONST(argv)); 342 if (errno != ENOENT) { 343 if (spec) 344 warn("exec %s for %s", execname, spec); 345 else 346 warn("exec %s", execname); 347 } 348 } while (*++edir != NULL); 349 350 if (errno == ENOENT) { 351 if (spec) 352 warn("exec %s for %s", execname, spec); 353 else 354 warn("exec %s", execname); 355 } 356 _exit(FSCK_EXIT_CHECK_FAILED); 357 /* NOTREACHED */ 358 359 default: /* Parent. */ 360 if (optbuf) 361 free(optbuf); 362 free(argv); 363 364 if (pidp) { 365 *pidp = pid; 366 return FSCK_EXIT_OK; 367 } 368 369 if (waitpid(pid, &status, 0) < 0) { 370 warn("waitpid"); 371 return FSCK_EXIT_CHECK_FAILED; 372 } 373 374 if (WIFEXITED(status)) { 375 if (WEXITSTATUS(status) != 0) 376 return WEXITSTATUS(status); 377 } 378 else if (WIFSIGNALED(status)) { 379 warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 380 return FSCK_EXIT_CHECK_FAILED; 381 } 382 break; 383 } 384 385 return FSCK_EXIT_OK; 386 } 387 388 389 static int 390 selected(const char *type) 391 { 392 struct entry *e; 393 394 /* If no type specified, it's always selected. */ 395 TAILQ_FOREACH(e, &selhead, entries) 396 if (!strcmp(e->type, type)) 397 return which == IN_LIST ? 1 : 0; 398 399 return which == IN_LIST ? 0 : 1; 400 } 401 402 403 static int 404 omitted(const char *mountedon) 405 { 406 struct entry *e; 407 408 /* If no type specified, it's always selected. */ 409 TAILQ_FOREACH(e, &omhead, entries) 410 if (!strcmp(e->type, mountedon)) 411 return 1; 412 413 return 0; 414 } 415 416 417 static const char * 418 getoptions(const char *type) 419 { 420 struct entry *e; 421 422 TAILQ_FOREACH(e, &opthead, entries) 423 if (!strcmp(e->type, type)) 424 return e->options; 425 return ""; 426 } 427 428 429 static void 430 addoption(char *optstr) 431 { 432 char *newoptions; 433 struct entry *e; 434 435 if ((newoptions = strchr(optstr, ':')) == NULL) 436 errx(1, "Invalid option string"); 437 438 *newoptions++ = '\0'; 439 440 TAILQ_FOREACH(e, &opthead, entries) 441 if (!strcmp(e->type, optstr)) { 442 catopt(&e->options, newoptions); 443 return; 444 } 445 addentry(&opthead, optstr, newoptions); 446 } 447 448 449 static void 450 addentry(struct fstypelist *list, const char *type, const char *opts) 451 { 452 struct entry *e; 453 454 e = emalloc(sizeof(struct entry)); 455 e->type = estrdup(type); 456 e->options = estrdup(opts); 457 TAILQ_INSERT_TAIL(list, e, entries); 458 } 459 460 461 static void 462 maketypelist(char *fslist) 463 { 464 char *ptr; 465 466 if ((fslist == NULL) || (fslist[0] == '\0')) 467 errx(1, "empty type list"); 468 469 if (fslist[0] == 'n' && fslist[1] == 'o') { 470 fslist += 2; 471 which = NOT_IN_LIST; 472 } 473 else 474 which = IN_LIST; 475 476 while ((ptr = strsep(&fslist, ",")) != NULL) 477 addentry(&selhead, ptr, ""); 478 479 } 480 481 482 static void 483 catopt(char **sp, const char *o) 484 { 485 char *s; 486 size_t i, j; 487 488 s = *sp; 489 if (s) { 490 i = strlen(s); 491 j = i + 1 + strlen(o) + 1; 492 s = erealloc(s, j); 493 (void)snprintf(s + i, j, ",%s", o); 494 } else 495 s = estrdup(o); 496 *sp = s; 497 } 498 499 500 static void 501 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp) 502 { 503 char *p, *s; 504 int argc, maxargc; 505 const char **argv; 506 507 argc = *argcp; 508 argv = *argvp; 509 maxargc = *maxargcp; 510 511 for (s = opts; (p = strsep(&s, ",")) != NULL;) { 512 /* Always leave space for one more argument and the NULL. */ 513 if (argc >= maxargc - 3) { 514 maxargc <<= 1; 515 argv = erealloc(argv, maxargc * sizeof(char *)); 516 } 517 if (*p != '\0') { 518 if (*p == '-') { 519 argv[argc++] = p; 520 p = strchr(p, '='); 521 if (p) { 522 *p = '\0'; 523 argv[argc++] = p+1; 524 } 525 } else { 526 argv[argc++] = "-o"; 527 argv[argc++] = p; 528 } 529 } 530 } 531 532 *argcp = argc; 533 *argvp = argv; 534 *maxargcp = maxargc; 535 } 536 537 static const char * 538 getfslab(const char *str) 539 { 540 static struct dkwedge_info dkw; 541 struct disklabel dl; 542 int fd; 543 char p; 544 const char *vfstype; 545 u_char t; 546 547 /* deduce the file system type from the disk label */ 548 if ((fd = open(str, O_RDONLY)) == -1) 549 err(1, "cannot open `%s'", str); 550 551 /* First check to see if it's a wedge. */ 552 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) { 553 /* Yup, this is easy. */ 554 (void) close(fd); 555 return (dkw.dkw_ptype); 556 } 557 558 if (ioctl(fd, DIOCGDINFO, &dl) == -1) 559 err(1, "cannot get disklabel for `%s'", str); 560 561 (void) close(fd); 562 563 p = str[strlen(str) - 1]; 564 565 if ((p - 'a') >= dl.d_npartitions) 566 errx(1, "partition `%s' is not defined on disk", str); 567 568 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES) 569 errx(1, "partition `%s' is not of a legal vfstype", 570 str); 571 572 if ((vfstype = fscknames[t]) == NULL) 573 errx(1, "vfstype `%s' on partition `%s' is not supported", 574 fstypenames[t], str); 575 576 return vfstype; 577 } 578 579 580 static void 581 usage(void) 582 { 583 static const char common[] = 584 "[-dfnPpqvy] [-x excludemount] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]"; 585 586 (void)fprintf(stderr, "usage: %s %s [special|node]...\n", 587 getprogname(), common); 588 exit(FSCK_EXIT_USAGE); 589 } 590