fsck.c revision 1.33
1/* $OpenBSD: fsck.c,v 1.33 2015/01/16 06:39:57 deraadt Exp $ */ 2/* $NetBSD: fsck.c,v 1.7 1996/10/03 20:06:30 christos Exp $ */ 3 4/* 5 * Copyright (c) 1996 Christos Zoulas. All rights reserved. 6 * Copyright (c) 1980, 1989, 1993, 1994 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94 34 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 35 * 36 */ 37 38#include <sys/types.h> 39#include <sys/mount.h> 40#include <sys/queue.h> 41#include <sys/resource.h> 42#include <sys/wait.h> 43 44#include <err.h> 45#include <errno.h> 46#include <fstab.h> 47#include <signal.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> 52#include <limits.h> 53#include <util.h> 54 55#include "pathnames.h" 56#include "fsutil.h" 57 58static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 59static enum { NONET_FILTER, NET_FILTER } filter = NONET_FILTER; 60 61TAILQ_HEAD(fstypelist, entry) opthead, selhead; 62 63struct entry { 64 char *type; 65 char *options; 66 TAILQ_ENTRY(entry) entries; 67}; 68 69static int maxrun; 70static char *options; 71static int flags; 72 73int main(int, char *[]); 74 75static int checkfs(const char *, const char *, const char *, void *, pid_t *); 76static int selected(const char *); 77static void addoption(char *); 78static const char *getoptions(const char *); 79static void addentry(struct fstypelist *, const char *, const char *); 80static void maketypelist(char *); 81static char *catopt(char *, const char *, int); 82static void mangle(char *, int *, const char ***, int *); 83static void usage(void); 84static void *isok(struct fstab *); 85static int hasopt(const char *, const char *); 86 87 88int 89main(int argc, char *argv[]) 90{ 91 struct fstab *fs; 92 int i, rval = 0; 93 char *vfstype = NULL; 94 char *p, globopt[3]; 95 struct rlimit rl; 96 97 /* Increase our data size to the max */ 98 if (getrlimit(RLIMIT_DATA, &rl) == 0) { 99 if (geteuid() == 0) 100 rl.rlim_cur = rl.rlim_max = RLIM_INFINITY; 101 else 102 rl.rlim_cur = rl.rlim_max; 103 if (setrlimit(RLIMIT_DATA, &rl) < 0) 104 warn("Can't get resource limit to max data size"); 105 } else 106 warn("Can't get resource limit for data size"); 107 108 globopt[0] = '-'; 109 globopt[2] = '\0'; 110 111 TAILQ_INIT(&selhead); 112 TAILQ_INIT(&opthead); 113 114 while ((i = getopt(argc, argv, "b:dfl:nNpT:t:vy")) != -1) 115 switch (i) { 116 case 'd': 117 flags |= CHECK_DEBUG; 118 break; 119 120 case 'v': 121 flags |= CHECK_VERBOSE; 122 break; 123 124 case 'p': 125 flags |= CHECK_PREEN; 126 /*FALLTHROUGH*/ 127 case 'n': 128 case 'f': 129 case 'y': 130 globopt[1] = i; 131 options = catopt(options, globopt, 1); 132 break; 133 134 case 'b': 135 if (asprintf(&p, "-b %s", optarg) == -1) 136 err(1, "malloc failed"); 137 options = catopt(options, p, 1); 138 free(p); 139 break; 140 141 case 'l': 142 maxrun = atoi(optarg); 143 break; 144 145 case 'T': 146 if (*optarg) 147 addoption(optarg); 148 break; 149 150 case 't': 151 if (!TAILQ_EMPTY(&selhead)) 152 errx(1, "only one -t option may be specified."); 153 154 maketypelist(optarg); 155 vfstype = optarg; 156 break; 157 158 case 'N': 159 filter = NET_FILTER; 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, maxrun, 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 char *spec, *type; 181 182 if ((strncmp(*argv, "/dev/", 5) == 0 || isduid(*argv, 0)) && 183 (type = readlabelfs(*argv, 0))) { 184 spec = *argv; 185 } else if ((fs = getfsfile(*argv)) == NULL && 186 (fs = getfsspec(*argv)) == NULL) { 187 if (vfstype == NULL) 188 errx(1, 189 "%s: unknown special file or file system.", 190 *argv); 191 spec = *argv; 192 type = vfstype; 193 } else { 194 spec = fs->fs_spec; 195 type = fs->fs_vfstype; 196 if (BADTYPE(fs->fs_type)) 197 errx(1, "%s has unknown file system type.", 198 *argv); 199 } 200 201 rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL); 202 } 203 204 return rval; 205} 206 207 208static void * 209isok(struct fstab *fs) 210{ 211 if (fs->fs_passno == 0) 212 return NULL; 213 214 if (BADTYPE(fs->fs_type)) 215 return NULL; 216 217 switch (filter) { 218 case NET_FILTER: 219 if (!hasopt(fs->fs_mntops, "net")) 220 return NULL; 221 break; 222 case NONET_FILTER: 223 if (hasopt(fs->fs_mntops, "net")) 224 return NULL; 225 break; 226 } 227 if (!selected(fs->fs_vfstype)) 228 return NULL; 229 230 return fs; 231} 232 233 234static int 235checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg, 236 pid_t *pidp) 237{ 238 /* List of directories containing fsck_xxx subcommands. */ 239 static const char *edirs[] = { 240 _PATH_SBIN, 241 _PATH_USRSBIN, 242 NULL 243 }; 244 const char **argv, **edir; 245 pid_t pid; 246 int argc, i, status, maxargc; 247 char *optbuf = NULL, fsname[PATH_MAX], execname[PATH_MAX]; 248 const char *extra = getoptions(vfstype); 249 250 if (strcmp(vfstype, "ufs") == 0) 251 vfstype = MOUNT_UFS; 252 253 maxargc = 100; 254 argv = ereallocarray(NULL, maxargc, sizeof(char *)); 255 256 argc = 0; 257 (void)snprintf(fsname, sizeof(fsname), "fsck_%s", vfstype); 258 argv[argc++] = fsname; 259 260 if (options) { 261 if (extra != NULL) 262 optbuf = catopt(options, extra, 0); 263 else 264 optbuf = estrdup(options); 265 } 266 else if (extra) 267 optbuf = estrdup(extra); 268 269 if (optbuf) 270 mangle(optbuf, &argc, &argv, &maxargc); 271 272 argv[argc++] = spec; 273 argv[argc] = NULL; 274 275 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 276 (void)printf("start %s %swait %s", mntpt, 277 pidp ? "no" : "", fsname); 278 for (i = 1; i < argc; i++) 279 (void)printf(" %s", argv[i]); 280 (void)printf("\n"); 281 } 282 283 switch (pid = fork()) { 284 case -1: /* Error. */ 285 warn("fork"); 286 if (optbuf) 287 free(optbuf); 288 free(argv); 289 return (1); 290 291 case 0: /* Child. */ 292 if (flags & CHECK_DEBUG) 293 _exit(0); 294 295 /* Go find an executable. */ 296 edir = edirs; 297 do { 298 (void)snprintf(execname, 299 sizeof(execname), "%s/fsck_%s", *edir, vfstype); 300 execv(execname, (char * const *)argv); 301 if (errno != ENOENT) { 302 if (spec) 303 warn("exec %s for %s", execname, spec); 304 else 305 warn("exec %s", execname); 306 } 307 } while (*++edir != NULL); 308 309 if (errno == ENOENT) { 310 if (spec) 311 warn("exec %s for %s", execname, spec); 312 else 313 warn("exec %s", execname); 314 } 315 exit(1); 316 /* NOTREACHED */ 317 318 default: /* Parent. */ 319 if (optbuf) 320 free(optbuf); 321 free(argv); 322 323 if (pidp) { 324 *pidp = pid; 325 return 0; 326 } 327 328 if (waitpid(pid, &status, 0) < 0) { 329 warn("waitpid"); 330 return (1); 331 } 332 333 if (WIFEXITED(status)) { 334 if (WEXITSTATUS(status) != 0) 335 return (WEXITSTATUS(status)); 336 } 337 else if (WIFSIGNALED(status)) { 338 warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 339 return (1); 340 } 341 break; 342 } 343 344 return (0); 345} 346 347 348static int 349selected(const char *type) 350{ 351 struct entry *e; 352 353 /* If no type specified, it's always selected. */ 354 TAILQ_FOREACH(e, &selhead, entries) 355 if (!strncmp(e->type, type, MFSNAMELEN)) 356 return which == IN_LIST ? 1 : 0; 357 358 return which == IN_LIST ? 0 : 1; 359} 360 361 362static const char * 363getoptions(const char *type) 364{ 365 struct entry *e; 366 367 TAILQ_FOREACH(e, &opthead, entries) 368 if (!strncmp(e->type, type, MFSNAMELEN)) 369 return e->options; 370 return ""; 371} 372 373 374static void 375addoption(char *optstr) 376{ 377 char *newoptions; 378 struct entry *e; 379 380 if ((newoptions = strchr(optstr, ':')) == NULL) 381 errx(1, "Invalid option string"); 382 383 *newoptions++ = '\0'; 384 385 TAILQ_FOREACH(e, &opthead, entries) 386 if (!strncmp(e->type, optstr, MFSNAMELEN)) { 387 e->options = catopt(e->options, newoptions, 1); 388 return; 389 } 390 addentry(&opthead, optstr, newoptions); 391} 392 393 394static void 395addentry(struct fstypelist *list, const char *type, const char *opts) 396{ 397 struct entry *e; 398 399 e = emalloc(sizeof(struct entry)); 400 e->type = estrdup(type); 401 e->options = estrdup(opts); 402 TAILQ_INSERT_TAIL(list, e, entries); 403} 404 405 406static void 407maketypelist(char *fslist) 408{ 409 char *ptr; 410 411 if ((fslist == NULL) || (fslist[0] == '\0')) 412 errx(1, "empty type list"); 413 414 if (fslist[0] == 'n' && fslist[1] == 'o') { 415 fslist += 2; 416 which = NOT_IN_LIST; 417 } 418 else 419 which = IN_LIST; 420 421 while ((ptr = strsep(&fslist, ",")) != NULL) 422 addentry(&selhead, ptr, ""); 423 424} 425 426 427static char * 428catopt(char *s0, const char *s1, int fr) 429{ 430 char *cp; 431 432 if (s0 && *s0) { 433 if (asprintf(&cp, "%s,%s", s0, s1) == -1) 434 err(1, "malloc failed"); 435 } else 436 cp = estrdup(s1); 437 438 if (s0 && fr) 439 free(s0); 440 return (cp); 441} 442 443 444static void 445mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp) 446{ 447 char *p, *s; 448 int argc = *argcp, maxargc = *maxargcp; 449 const char **argv = *argvp; 450 451 argc = *argcp; 452 maxargc = *maxargcp; 453 454 for (s = opts; (p = strsep(&s, ",")) != NULL;) { 455 /* always leave space for one more argument and the NULL */ 456 if (argc >= maxargc - 3) { 457 int newmaxargc = maxargc + 50; 458 459 argv = ereallocarray(argv, newmaxargc, sizeof(char *)); 460 maxargc = newmaxargc; 461 } 462 if (*p != '\0') { 463 if (*p == '-') { 464 argv[argc++] = p; 465 p = strchr(p, '='); 466 if (p) { 467 *p = '\0'; 468 argv[argc++] = p+1; 469 } 470 } 471 else { 472 argv[argc++] = "-o"; 473 argv[argc++] = p; 474 } 475 } 476 } 477 478 *argcp = argc; 479 *argvp = argv; 480 *maxargcp = maxargc; 481} 482 483static int 484hasopt(const char *mntopts, const char *option) 485{ 486 int found; 487 char *opt, *optbuf; 488 489 if (mntopts == NULL) 490 return (0); 491 optbuf = strdup(mntopts); 492 found = 0; 493 for (opt = optbuf; !found && opt != NULL; strsep(&opt, ",")) 494 found = !strncmp(opt, option, strlen(option)); 495 free(optbuf); 496 return (found); 497} 498 499 500static void 501usage(void) 502{ 503 extern char *__progname; 504 505 fprintf(stderr, "usage: %s " 506 "[-dfNnpvy] [-b block#] [-l maxparallel] [-T fstype:fsoptions]\n" 507 " [-t fstype] [special | node ...]\n", __progname); 508 exit(1); 509} 510