1 /* $OpenBSD: fsck.c,v 1.23 2005/11/21 14:27:55 millert 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 static const char rcsid[] = "$OpenBSD: fsck.c,v 1.23 2005/11/21 14:27:55 millert Exp $"; 39 40 #include <sys/param.h> 41 #include <sys/mount.h> 42 #include <sys/queue.h> 43 #include <sys/resource.h> 44 #include <sys/wait.h> 45 46 #include <err.h> 47 #include <errno.h> 48 #include <fstab.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <util.h> 55 56 #include "pathnames.h" 57 #include "fsutil.h" 58 59 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 60 61 TAILQ_HEAD(fstypelist, entry) opthead, selhead; 62 63 struct entry { 64 char *type; 65 char *options; 66 TAILQ_ENTRY(entry) entries; 67 }; 68 69 static int maxrun = 0; 70 static char *options = NULL; 71 static int flags = 0; 72 73 int main(int, char *[]); 74 75 static int checkfs(const char *, const char *, const char *, void *, pid_t *); 76 static int selected(const char *); 77 static void addoption(char *); 78 static const char *getoptions(const char *); 79 static void addentry(struct fstypelist *, const char *, const char *); 80 static void maketypelist(char *); 81 static char *catopt(char *, const char *, int); 82 static void mangle(char *, int *, const char ***, int *); 83 static void usage(void); 84 static void *isok(struct fstab *); 85 86 87 int 88 main(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, "dvpfnyb:l:T:t:")) != -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 '?': 158 default: 159 usage(); 160 /* NOTREACHED */ 161 } 162 163 argc -= optind; 164 argv += optind; 165 166 if (argc == 0) 167 return checkfstab(flags, maxrun, isok, checkfs); 168 169 #define BADTYPE(type) \ 170 (strcmp(type, FSTAB_RO) && \ 171 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 172 173 174 for (; argc--; argv++) { 175 char *spec, *type; 176 177 if (strncmp(*argv, "/dev/", 5) == 0 && 178 (type = readlabelfs(*argv, 0))) { 179 spec = *argv; 180 } else if ((fs = getfsfile(*argv)) == NULL && 181 (fs = getfsspec(*argv)) == NULL) { 182 if (vfstype == NULL) 183 errx(1, 184 "%s: unknown special file or file system.", 185 *argv); 186 spec = *argv; 187 type = vfstype; 188 } else { 189 spec = fs->fs_spec; 190 type = fs->fs_vfstype; 191 if (BADTYPE(fs->fs_type)) 192 errx(1, "%s has unknown file system type.", 193 *argv); 194 } 195 196 rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL); 197 } 198 199 return rval; 200 } 201 202 203 static void * 204 isok(struct fstab *fs) 205 { 206 if (fs->fs_passno == 0) 207 return NULL; 208 209 if (BADTYPE(fs->fs_type)) 210 return NULL; 211 212 if (!selected(fs->fs_vfstype)) 213 return NULL; 214 215 return fs; 216 } 217 218 219 static int 220 checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg, 221 pid_t *pidp) 222 { 223 /* List of directories containing fsck_xxx subcommands. */ 224 static const char *edirs[] = { 225 _PATH_SBIN, 226 _PATH_USRSBIN, 227 NULL 228 }; 229 const char **argv, **edir; 230 pid_t pid; 231 int argc, i, status, maxargc; 232 char *optbuf = NULL, fsname[MAXPATHLEN], execname[MAXPATHLEN]; 233 const char *extra = getoptions(vfstype); 234 235 if (strcmp(vfstype, "ufs") == 0) 236 vfstype = MOUNT_UFS; 237 238 maxargc = 100; 239 argv = emalloc(sizeof(char *) * maxargc); 240 241 argc = 0; 242 (void)snprintf(fsname, sizeof(fsname), "fsck_%s", vfstype); 243 argv[argc++] = fsname; 244 245 if (options) { 246 if (extra != NULL) 247 optbuf = catopt(options, extra, 0); 248 else 249 optbuf = estrdup(options); 250 } 251 else if (extra) 252 optbuf = estrdup(extra); 253 254 if (optbuf) 255 mangle(optbuf, &argc, &argv, &maxargc); 256 257 argv[argc++] = spec; 258 argv[argc] = NULL; 259 260 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 261 (void)printf("start %s %swait %s", mntpt, 262 pidp ? "no" : "", fsname); 263 for (i = 1; i < argc; i++) 264 (void)printf(" %s", argv[i]); 265 (void)printf("\n"); 266 } 267 268 switch (pid = fork()) { 269 case -1: /* Error. */ 270 warn("fork"); 271 if (optbuf) 272 free(optbuf); 273 return (1); 274 275 case 0: /* Child. */ 276 if (flags & CHECK_DEBUG) 277 _exit(0); 278 279 /* Go find an executable. */ 280 edir = edirs; 281 do { 282 (void)snprintf(execname, 283 sizeof(execname), "%s/fsck_%s", *edir, vfstype); 284 execv(execname, (char * const *)argv); 285 if (errno != ENOENT) { 286 if (spec) 287 warn("exec %s for %s", execname, spec); 288 else 289 warn("exec %s", execname); 290 } 291 } while (*++edir != NULL); 292 293 if (errno == ENOENT) { 294 if (spec) 295 warn("exec %s for %s", execname, spec); 296 else 297 warn("exec %s", execname); 298 } 299 exit(1); 300 /* NOTREACHED */ 301 302 default: /* Parent. */ 303 if (optbuf) 304 free(optbuf); 305 306 if (pidp) { 307 *pidp = pid; 308 return 0; 309 } 310 311 if (waitpid(pid, &status, 0) < 0) { 312 warn("waitpid"); 313 return (1); 314 } 315 316 if (WIFEXITED(status)) { 317 if (WEXITSTATUS(status) != 0) 318 return (WEXITSTATUS(status)); 319 } 320 else if (WIFSIGNALED(status)) { 321 warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 322 return (1); 323 } 324 break; 325 } 326 327 return (0); 328 } 329 330 331 static int 332 selected(const char *type) 333 { 334 struct entry *e; 335 336 /* If no type specified, it's always selected. */ 337 TAILQ_FOREACH(e, &selhead, entries) 338 if (!strncmp(e->type, type, MFSNAMELEN)) 339 return which == IN_LIST ? 1 : 0; 340 341 return which == IN_LIST ? 0 : 1; 342 } 343 344 345 static const char * 346 getoptions(const char *type) 347 { 348 struct entry *e; 349 350 TAILQ_FOREACH(e, &opthead, entries) 351 if (!strncmp(e->type, type, MFSNAMELEN)) 352 return e->options; 353 return ""; 354 } 355 356 357 static void 358 addoption(char *optstr) 359 { 360 char *newoptions; 361 struct entry *e; 362 363 if ((newoptions = strchr(optstr, ':')) == NULL) 364 errx(1, "Invalid option string"); 365 366 *newoptions++ = '\0'; 367 368 TAILQ_FOREACH(e, &opthead, entries) 369 if (!strncmp(e->type, optstr, MFSNAMELEN)) { 370 e->options = catopt(e->options, newoptions, 1); 371 return; 372 } 373 addentry(&opthead, optstr, newoptions); 374 } 375 376 377 static void 378 addentry(struct fstypelist *list, const char *type, const char *opts) 379 { 380 struct entry *e; 381 382 e = emalloc(sizeof(struct entry)); 383 e->type = estrdup(type); 384 e->options = estrdup(opts); 385 TAILQ_INSERT_TAIL(list, e, entries); 386 } 387 388 389 static void 390 maketypelist(char *fslist) 391 { 392 char *ptr; 393 394 if ((fslist == NULL) || (fslist[0] == '\0')) 395 errx(1, "empty type list"); 396 397 if (fslist[0] == 'n' && fslist[1] == 'o') { 398 fslist += 2; 399 which = NOT_IN_LIST; 400 } 401 else 402 which = IN_LIST; 403 404 while ((ptr = strsep(&fslist, ",")) != NULL) 405 addentry(&selhead, ptr, ""); 406 407 } 408 409 410 static char * 411 catopt(char *s0, const char *s1, int fr) 412 { 413 char *cp; 414 415 if (s0 && *s0) { 416 if (asprintf(&cp, "%s,%s", s0, s1) == -1) 417 err(1, "malloc failed"); 418 } else 419 cp = estrdup(s1); 420 421 if (s0 && fr) 422 free(s0); 423 return (cp); 424 } 425 426 427 static void 428 mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp) 429 { 430 char *p, *s; 431 int argc = *argcp, maxargc = *maxargcp; 432 const char **argv = *argvp; 433 434 argc = *argcp; 435 maxargc = *maxargcp; 436 437 for (s = opts; (p = strsep(&s, ",")) != NULL;) { 438 /* always leave space for one more argument and the NULL */ 439 if (argc >= maxargc - 3) { 440 int newmaxargc = maxargc + 50; 441 442 argv = erealloc(argv, newmaxargc * sizeof(char *)); 443 maxargc = newmaxargc; 444 } 445 if (*p != '\0') { 446 if (*p == '-') { 447 argv[argc++] = p; 448 p = strchr(p, '='); 449 if (p) { 450 *p = '\0'; 451 argv[argc++] = p+1; 452 } 453 } 454 else { 455 argv[argc++] = "-o"; 456 argv[argc++] = p; 457 } 458 } 459 } 460 461 *argcp = argc; 462 *argvp = argv; 463 *maxargcp = maxargc; 464 } 465 466 467 static void 468 usage(void) 469 { 470 extern char *__progname; 471 472 fprintf(stderr, "usage: %s [-dfnpvy] [-l maxparallel] " 473 "[-T fstype:fsoptions] [-t fstype] [special | node ...]\n", 474 __progname); 475 exit(1); 476 } 477