fsck.c revision 1.30
1/* $OpenBSD: fsck.c,v 1.30 2014/07/13 17:28:13 jmc 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/param.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 <util.h> 53 54#include "pathnames.h" 55#include "fsutil.h" 56 57static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 58static enum { NONET_FILTER, NET_FILTER } filter = NONET_FILTER; 59 60TAILQ_HEAD(fstypelist, entry) opthead, selhead; 61 62struct entry { 63 char *type; 64 char *options; 65 TAILQ_ENTRY(entry) entries; 66}; 67 68static int maxrun; 69static char *options; 70static int flags; 71 72int main(int, char *[]); 73 74static int checkfs(const char *, const char *, const char *, void *, pid_t *); 75static int selected(const char *); 76static void addoption(char *); 77static const char *getoptions(const char *); 78static void addentry(struct fstypelist *, const char *, const char *); 79static void maketypelist(char *); 80static char *catopt(char *, const char *, int); 81static void mangle(char *, int *, const char ***, int *); 82static void usage(void); 83static void *isok(struct fstab *); 84static int hasopt(const char *, const char *); 85 86 87int 88main(int argc, char *argv[]) 89{ 90 struct fstab *fs; 91 int i, rval = 0; 92 char *vfstype = NULL; 93 char *p, globopt[3]; 94 struct rlimit rl; 95 96 /* Increase our data size to the max */ 97 if (getrlimit(RLIMIT_DATA, &rl) == 0) { 98 if (geteuid() == 0) 99 rl.rlim_cur = rl.rlim_max = RLIM_INFINITY; 100 else 101 rl.rlim_cur = rl.rlim_max; 102 if (setrlimit(RLIMIT_DATA, &rl) < 0) 103 warn("Can't get resource limit to max data size"); 104 } else 105 warn("Can't get resource limit for data size"); 106 107 globopt[0] = '-'; 108 globopt[2] = '\0'; 109 110 TAILQ_INIT(&selhead); 111 TAILQ_INIT(&opthead); 112 113 while ((i = getopt(argc, argv, "b:dfl:nNpT:t:vy")) != -1) 114 switch (i) { 115 case 'd': 116 flags |= CHECK_DEBUG; 117 break; 118 119 case 'v': 120 flags |= CHECK_VERBOSE; 121 break; 122 123 case 'p': 124 flags |= CHECK_PREEN; 125 /*FALLTHROUGH*/ 126 case 'n': 127 case 'f': 128 case 'y': 129 globopt[1] = i; 130 options = catopt(options, globopt, 1); 131 break; 132 133 case 'b': 134 if (asprintf(&p, "-b %s", optarg) == -1) 135 err(1, "malloc failed"); 136 options = catopt(options, p, 1); 137 free(p); 138 break; 139 140 case 'l': 141 maxrun = atoi(optarg); 142 break; 143 144 case 'T': 145 if (*optarg) 146 addoption(optarg); 147 break; 148 149 case 't': 150 if (!TAILQ_EMPTY(&selhead)) 151 errx(1, "only one -t option may be specified."); 152 153 maketypelist(optarg); 154 vfstype = optarg; 155 break; 156 157 case 'N': 158 filter = NET_FILTER; 159 break; 160 161 case '?': 162 default: 163 usage(); 164 /* NOTREACHED */ 165 } 166 167 argc -= optind; 168 argv += optind; 169 170 if (argc == 0) 171 return checkfstab(flags, maxrun, isok, checkfs); 172 173#define BADTYPE(type) \ 174 (strcmp(type, FSTAB_RO) && \ 175 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 176 177 178 for (; argc--; argv++) { 179 char *spec, *type; 180 181 if ((strncmp(*argv, "/dev/", 5) == 0 || isduid(*argv, 0)) && 182 (type = readlabelfs(*argv, 0))) { 183 spec = *argv; 184 } else if ((fs = getfsfile(*argv)) == NULL && 185 (fs = getfsspec(*argv)) == NULL) { 186 if (vfstype == NULL) 187 errx(1, 188 "%s: unknown special file or file system.", 189 *argv); 190 spec = *argv; 191 type = vfstype; 192 } else { 193 spec = fs->fs_spec; 194 type = fs->fs_vfstype; 195 if (BADTYPE(fs->fs_type)) 196 errx(1, "%s has unknown file system type.", 197 *argv); 198 } 199 200 rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL); 201 } 202 203 return rval; 204} 205 206 207static void * 208isok(struct fstab *fs) 209{ 210 if (fs->fs_passno == 0) 211 return NULL; 212 213 if (BADTYPE(fs->fs_type)) 214 return NULL; 215 216 switch (filter) { 217 case NET_FILTER: 218 if (!hasopt(fs->fs_mntops, "net")) 219 return NULL; 220 break; 221 case NONET_FILTER: 222 if (hasopt(fs->fs_mntops, "net")) 223 return NULL; 224 break; 225 } 226 if (!selected(fs->fs_vfstype)) 227 return NULL; 228 229 return fs; 230} 231 232 233static int 234checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg, 235 pid_t *pidp) 236{ 237 /* List of directories containing fsck_xxx subcommands. */ 238 static const char *edirs[] = { 239 _PATH_SBIN, 240 _PATH_USRSBIN, 241 NULL 242 }; 243 const char **argv, **edir; 244 pid_t pid; 245 int argc, i, status, maxargc; 246 char *optbuf = NULL, fsname[MAXPATHLEN], execname[MAXPATHLEN]; 247 const char *extra = getoptions(vfstype); 248 249 if (strcmp(vfstype, "ufs") == 0) 250 vfstype = MOUNT_UFS; 251 252 maxargc = 100; 253 argv = emalloc(sizeof(char *) * maxargc); 254 255 argc = 0; 256 (void)snprintf(fsname, sizeof(fsname), "fsck_%s", vfstype); 257 argv[argc++] = fsname; 258 259 if (options) { 260 if (extra != NULL) 261 optbuf = catopt(options, extra, 0); 262 else 263 optbuf = estrdup(options); 264 } 265 else if (extra) 266 optbuf = estrdup(extra); 267 268 if (optbuf) 269 mangle(optbuf, &argc, &argv, &maxargc); 270 271 argv[argc++] = spec; 272 argv[argc] = NULL; 273 274 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 275 (void)printf("start %s %swait %s", mntpt, 276 pidp ? "no" : "", fsname); 277 for (i = 1; i < argc; i++) 278 (void)printf(" %s", argv[i]); 279 (void)printf("\n"); 280 } 281 282 switch (pid = fork()) { 283 case -1: /* Error. */ 284 warn("fork"); 285 if (optbuf) 286 free(optbuf); 287 free(argv); 288 return (1); 289 290 case 0: /* Child. */ 291 if (flags & CHECK_DEBUG) 292 _exit(0); 293 294 /* Go find an executable. */ 295 edir = edirs; 296 do { 297 (void)snprintf(execname, 298 sizeof(execname), "%s/fsck_%s", *edir, vfstype); 299 execv(execname, (char * const *)argv); 300 if (errno != ENOENT) { 301 if (spec) 302 warn("exec %s for %s", execname, spec); 303 else 304 warn("exec %s", execname); 305 } 306 } while (*++edir != NULL); 307 308 if (errno == ENOENT) { 309 if (spec) 310 warn("exec %s for %s", execname, spec); 311 else 312 warn("exec %s", execname); 313 } 314 exit(1); 315 /* NOTREACHED */ 316 317 default: /* Parent. */ 318 if (optbuf) 319 free(optbuf); 320 free(argv); 321 322 if (pidp) { 323 *pidp = pid; 324 return 0; 325 } 326 327 if (waitpid(pid, &status, 0) < 0) { 328 warn("waitpid"); 329 return (1); 330 } 331 332 if (WIFEXITED(status)) { 333 if (WEXITSTATUS(status) != 0) 334 return (WEXITSTATUS(status)); 335 } 336 else if (WIFSIGNALED(status)) { 337 warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 338 return (1); 339 } 340 break; 341 } 342 343 return (0); 344} 345 346 347static int 348selected(const char *type) 349{ 350 struct entry *e; 351 352 /* If no type specified, it's always selected. */ 353 TAILQ_FOREACH(e, &selhead, entries) 354 if (!strncmp(e->type, type, MFSNAMELEN)) 355 return which == IN_LIST ? 1 : 0; 356 357 return which == IN_LIST ? 0 : 1; 358} 359 360 361static const char * 362getoptions(const char *type) 363{ 364 struct entry *e; 365 366 TAILQ_FOREACH(e, &opthead, entries) 367 if (!strncmp(e->type, type, MFSNAMELEN)) 368 return e->options; 369 return ""; 370} 371 372 373static void 374addoption(char *optstr) 375{ 376 char *newoptions; 377 struct entry *e; 378 379 if ((newoptions = strchr(optstr, ':')) == NULL) 380 errx(1, "Invalid option string"); 381 382 *newoptions++ = '\0'; 383 384 TAILQ_FOREACH(e, &opthead, entries) 385 if (!strncmp(e->type, optstr, MFSNAMELEN)) { 386 e->options = catopt(e->options, newoptions, 1); 387 return; 388 } 389 addentry(&opthead, optstr, newoptions); 390} 391 392 393static void 394addentry(struct fstypelist *list, const char *type, const char *opts) 395{ 396 struct entry *e; 397 398 e = emalloc(sizeof(struct entry)); 399 e->type = estrdup(type); 400 e->options = estrdup(opts); 401 TAILQ_INSERT_TAIL(list, e, entries); 402} 403 404 405static void 406maketypelist(char *fslist) 407{ 408 char *ptr; 409 410 if ((fslist == NULL) || (fslist[0] == '\0')) 411 errx(1, "empty type list"); 412 413 if (fslist[0] == 'n' && fslist[1] == 'o') { 414 fslist += 2; 415 which = NOT_IN_LIST; 416 } 417 else 418 which = IN_LIST; 419 420 while ((ptr = strsep(&fslist, ",")) != NULL) 421 addentry(&selhead, ptr, ""); 422 423} 424 425 426static char * 427catopt(char *s0, const char *s1, int fr) 428{ 429 char *cp; 430 431 if (s0 && *s0) { 432 if (asprintf(&cp, "%s,%s", s0, s1) == -1) 433 err(1, "malloc failed"); 434 } else 435 cp = estrdup(s1); 436 437 if (s0 && fr) 438 free(s0); 439 return (cp); 440} 441 442 443static void 444mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp) 445{ 446 char *p, *s; 447 int argc = *argcp, maxargc = *maxargcp; 448 const char **argv = *argvp; 449 450 argc = *argcp; 451 maxargc = *maxargcp; 452 453 for (s = opts; (p = strsep(&s, ",")) != NULL;) { 454 /* always leave space for one more argument and the NULL */ 455 if (argc >= maxargc - 3) { 456 int newmaxargc = maxargc + 50; 457 458 argv = erealloc(argv, newmaxargc * sizeof(char *)); 459 maxargc = newmaxargc; 460 } 461 if (*p != '\0') { 462 if (*p == '-') { 463 argv[argc++] = p; 464 p = strchr(p, '='); 465 if (p) { 466 *p = '\0'; 467 argv[argc++] = p+1; 468 } 469 } 470 else { 471 argv[argc++] = "-o"; 472 argv[argc++] = p; 473 } 474 } 475 } 476 477 *argcp = argc; 478 *argvp = argv; 479 *maxargcp = maxargc; 480} 481 482static int 483hasopt(const char *mntopts, const char *option) 484{ 485 int found; 486 char *opt, *optbuf; 487 488 if (mntopts == NULL) 489 return (0); 490 optbuf = strdup(mntopts); 491 found = 0; 492 for (opt = optbuf; !found && opt != NULL; strsep(&opt, ",")) 493 found = !strncmp(opt, option, strlen(option)); 494 free(optbuf); 495 return (found); 496} 497 498 499static void 500usage(void) 501{ 502 extern char *__progname; 503 504 fprintf(stderr, "usage: %s " 505 "[-dfNnpvy] [-b block#] [-l maxparallel] [-T fstype:fsoptions]\n" 506 " [-t fstype] [special | node ...]\n", __progname); 507 exit(1); 508} 509