1 /* $OpenBSD: fsck.c,v 1.7 2001/07/07 18:26:11 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. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94 38 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 39 * 40 */ 41 42 static char rcsid[] = "$NetBSD: fsck.c,v 1.7 1996/10/03 20:06:30 christos Exp $"; 43 44 #include <sys/param.h> 45 #include <sys/mount.h> 46 #include <sys/queue.h> 47 #include <sys/resource.h> 48 #include <sys/wait.h> 49 50 #include <err.h> 51 #include <errno.h> 52 #include <fstab.h> 53 #include <signal.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 #include <util.h> 59 60 #include "pathnames.h" 61 #include "fsutil.h" 62 63 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 64 65 TAILQ_HEAD(fstypelist, entry) opthead, selhead; 66 67 struct entry { 68 char *type; 69 char *options; 70 TAILQ_ENTRY(entry) entries; 71 }; 72 73 static int maxrun = 0; 74 static char *options = NULL; 75 static int flags = 0; 76 77 int main __P((int, char *[])); 78 79 static int checkfs __P((const char *, const char *, const char *, void *, 80 pid_t *)); 81 static int selected __P((const char *)); 82 static void addoption __P((char *)); 83 static const char *getoptions __P((const char *)); 84 static void addentry __P((struct fstypelist *, const char *, const char *)); 85 static void maketypelist __P((char *)); 86 static char *catopt __P((char *, const char *, int)); 87 static void mangle __P((char *, int *, const char ***, int *)); 88 static void usage __P((void)); 89 static void *isok __P((struct fstab *)); 90 91 92 int 93 main(argc, argv) 94 int argc; 95 char *argv[]; 96 { 97 struct fstab *fs; 98 int i, rval = 0; 99 char *vfstype = NULL; 100 char globopt[3]; 101 struct rlimit rl; 102 103 /* Increase our data size to the max */ 104 if (getrlimit(RLIMIT_DATA, &rl) == 0) { 105 rl.rlim_cur = rl.rlim_max; 106 if (setrlimit(RLIMIT_DATA, &rl) < 0) 107 warn("Can't get resource limit to max data size"); 108 } else 109 warn("Can't get resource limit for data size"); 110 111 globopt[0] = '-'; 112 globopt[2] = '\0'; 113 114 TAILQ_INIT(&selhead); 115 TAILQ_INIT(&opthead); 116 117 while ((i = getopt(argc, argv, "dvpfnyl:t:T:")) != -1) 118 switch (i) { 119 case 'd': 120 flags |= CHECK_DEBUG; 121 break; 122 123 case 'v': 124 flags |= CHECK_VERBOSE; 125 break; 126 127 case 'p': 128 flags |= CHECK_PREEN; 129 /*FALLTHROUGH*/ 130 case 'n': 131 case 'f': 132 case 'y': 133 globopt[1] = i; 134 options = catopt(options, globopt, 1); 135 break; 136 137 case 'l': 138 maxrun = atoi(optarg); 139 break; 140 141 case 'T': 142 if (*optarg) 143 addoption(optarg); 144 break; 145 146 case 't': 147 if (selhead.tqh_first != NULL) 148 errx(1, "only one -t option may be specified."); 149 150 maketypelist(optarg); 151 vfstype = optarg; 152 break; 153 154 case '?': 155 default: 156 usage(); 157 /* NOTREACHED */ 158 } 159 160 argc -= optind; 161 argv += optind; 162 163 if (argc == 0) 164 return checkfstab(flags, maxrun, isok, checkfs); 165 166 #define BADTYPE(type) \ 167 (strcmp(type, FSTAB_RO) && \ 168 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 169 170 171 for (; argc--; argv++) { 172 char *spec, *type; 173 174 if (strncmp(*argv, "/dev/", 5) == 0 && 175 (type = readlabelfs(*argv, 0))) { 176 spec = *argv; 177 } else if ((fs = getfsfile(*argv)) == NULL && 178 (fs = getfsspec(*argv)) == NULL) { 179 if (vfstype == NULL) 180 errx(1, 181 "%s: unknown special file or file system.", 182 *argv); 183 spec = *argv; 184 type = vfstype; 185 } else { 186 spec = fs->fs_spec; 187 type = fs->fs_vfstype; 188 if (BADTYPE(fs->fs_type)) 189 errx(1, "%s has unknown file system type.", 190 *argv); 191 } 192 193 rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL); 194 } 195 196 return rval; 197 } 198 199 200 static void * 201 isok(fs) 202 struct fstab *fs; 203 { 204 if (fs->fs_passno == 0) 205 return NULL; 206 207 if (BADTYPE(fs->fs_type)) 208 return NULL; 209 210 if (!selected(fs->fs_vfstype)) 211 return NULL; 212 213 return fs; 214 } 215 216 217 static int 218 checkfs(vfstype, spec, mntpt, auxarg, pidp) 219 const char *vfstype, *spec, *mntpt; 220 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(type) 333 const char *type; 334 { 335 struct entry *e; 336 337 /* If no type specified, it's always selected. */ 338 for (e = selhead.tqh_first; e != NULL; e = e->entries.tqe_next) 339 if (!strncmp(e->type, type, MFSNAMELEN)) 340 return which == IN_LIST ? 1 : 0; 341 342 return which == IN_LIST ? 0 : 1; 343 } 344 345 346 static const char * 347 getoptions(type) 348 const char *type; 349 { 350 struct entry *e; 351 352 for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next) 353 if (!strncmp(e->type, type, MFSNAMELEN)) 354 return e->options; 355 return ""; 356 } 357 358 359 static void 360 addoption(optstr) 361 char *optstr; 362 { 363 char *newoptions; 364 struct entry *e; 365 366 if ((newoptions = strchr(optstr, ':')) == NULL) 367 errx(1, "Invalid option string"); 368 369 *newoptions++ = '\0'; 370 371 for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next) 372 if (!strncmp(e->type, optstr, MFSNAMELEN)) { 373 e->options = catopt(e->options, newoptions, 1); 374 return; 375 } 376 addentry(&opthead, optstr, newoptions); 377 } 378 379 380 static void 381 addentry(list, type, opts) 382 struct fstypelist *list; 383 const char *type; 384 const char *opts; 385 { 386 struct entry *e; 387 388 e = emalloc(sizeof(struct entry)); 389 e->type = estrdup(type); 390 e->options = estrdup(opts); 391 TAILQ_INSERT_TAIL(list, e, entries); 392 } 393 394 395 static void 396 maketypelist(fslist) 397 char *fslist; 398 { 399 char *ptr; 400 401 if ((fslist == NULL) || (fslist[0] == '\0')) 402 errx(1, "empty type list"); 403 404 if (fslist[0] == 'n' && fslist[1] == 'o') { 405 fslist += 2; 406 which = NOT_IN_LIST; 407 } 408 else 409 which = IN_LIST; 410 411 while ((ptr = strsep(&fslist, ",")) != NULL) 412 addentry(&selhead, ptr, ""); 413 414 } 415 416 417 static char * 418 catopt(s0, s1, fr) 419 char *s0; 420 const char *s1; 421 int fr; 422 { 423 size_t i; 424 char *cp; 425 426 if (s0 && *s0) { 427 i = strlen(s0) + strlen(s1) + 1 + 1; 428 cp = emalloc(i); 429 (void)snprintf(cp, i, "%s,%s", s0, s1); 430 } 431 else 432 cp = estrdup(s1); 433 434 if (s0 && fr) 435 free(s0); 436 return (cp); 437 } 438 439 440 static void 441 mangle(opts, argcp, argvp, maxargcp) 442 char *opts; 443 int *argcp; 444 const char ***argvp; 445 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 maxargc += 50; 458 argv = erealloc(argv, maxargc * sizeof(char *)); 459 } 460 if (*p != '\0') { 461 if (*p == '-') { 462 argv[argc++] = p; 463 p = strchr(p, '='); 464 if (p) { 465 *p = '\0'; 466 argv[argc++] = p+1; 467 } 468 } 469 else { 470 argv[argc++] = "-o"; 471 argv[argc++] = p; 472 } 473 } 474 } 475 476 *argcp = argc; 477 *argvp = argv; 478 *maxargcp = maxargc; 479 } 480 481 482 static void 483 usage() 484 { 485 extern char *__progname; 486 static const char common[] = 487 "[-dpvlyn] [-T fstype:fsoptions] [-t fstype]"; 488 489 (void)fprintf(stderr, "Usage: %s %s [special|node]...\n", 490 __progname, common); 491 exit(1); 492 } 493