Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.30
      1 /*	$NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
     32  * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
     33  *
     34  */
     35 
     36 /*
     37  * Copyright (c) 1996 Christos Zoulas. All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *	This product includes software developed by the University of
     50  *	California, Berkeley and its contributors.
     51  * 4. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
     68  * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
     69  *
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 #ifndef lint
     74 __RCSID("$NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $");
     75 #endif /* not lint */
     76 
     77 #include <sys/param.h>
     78 #include <sys/mount.h>
     79 #include <sys/queue.h>
     80 #include <sys/wait.h>
     81 #define FSTYPENAMES
     82 #define FSCKNAMES
     83 #include <sys/disklabel.h>
     84 #include <sys/ioctl.h>
     85 
     86 #include <err.h>
     87 #include <errno.h>
     88 #include <fstab.h>
     89 #include <fcntl.h>
     90 #include <paths.h>
     91 #include <signal.h>
     92 #include <stdio.h>
     93 #include <stdlib.h>
     94 #include <string.h>
     95 #include <unistd.h>
     96 
     97 #include "pathnames.h"
     98 #include "fsutil.h"
     99 
    100 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
    101 
    102 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
    103 
    104 struct entry {
    105 	char *type;
    106 	char *options;
    107 	TAILQ_ENTRY(entry) entries;
    108 };
    109 
    110 static int maxrun = 0;
    111 static char *options = NULL;
    112 static int flags = 0;
    113 
    114 int main(int, char *[]);
    115 
    116 static int checkfs(const char *, const char *, const char *, void *, pid_t *);
    117 static int selected(const char *);
    118 static void addoption(char *);
    119 static const char *getoptions(const char *);
    120 static void addentry(struct fstypelist *, const char *, const char *);
    121 static void maketypelist(char *);
    122 static void catopt(char **, const char *);
    123 static void mangle(char *, int *, const char ***, int *);
    124 static const char *getfslab(const char *);
    125 static void usage(void);
    126 static void *isok(struct fstab *);
    127 
    128 int
    129 main(int argc, char *argv[])
    130 {
    131 	struct fstab *fs;
    132 	int i, rval = 0;
    133 	const char *vfstype = NULL;
    134 	char globopt[3];
    135 
    136 	globopt[0] = '-';
    137 	globopt[2] = '\0';
    138 
    139 	TAILQ_INIT(&selhead);
    140 	TAILQ_INIT(&opthead);
    141 
    142 	while ((i = getopt(argc, argv, "dvpfnyl:t:T:")) != -1)
    143 		switch (i) {
    144 		case 'd':
    145 			flags |= CHECK_DEBUG;
    146 			break;
    147 
    148 		case 'v':
    149 			flags |= CHECK_VERBOSE;
    150 			break;
    151 
    152 		case 'p':
    153 		case 'f':
    154 			if (i == 'p')
    155 				flags |= CHECK_PREEN;
    156 			else
    157 				flags |= CHECK_FORCE;
    158 			/*FALLTHROUGH*/
    159 		case 'n':
    160 		case 'y':
    161 			globopt[1] = i;
    162 			catopt(&options, globopt);
    163 			break;
    164 
    165 		case 'l':
    166 			maxrun = atoi(optarg);
    167 			break;
    168 
    169 		case 'T':
    170 			if (*optarg)
    171 				addoption(optarg);
    172 			break;
    173 
    174 		case 't':
    175 			if (TAILQ_FIRST(&selhead) != NULL)
    176 				errx(1, "only one -t option may be specified.");
    177 
    178 			maketypelist(optarg);
    179 			vfstype = optarg;
    180 			break;
    181 
    182 		case '?':
    183 		default:
    184 			usage();
    185 			/* NOTREACHED */
    186 		}
    187 
    188 	argc -= optind;
    189 	argv += optind;
    190 
    191 	if (argc == 0)
    192 		return checkfstab(flags, maxrun, isok, checkfs);
    193 
    194 #define	BADTYPE(type)							\
    195 	(strcmp(type, FSTAB_RO) &&					\
    196 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    197 
    198 
    199 	for (; argc--; argv++) {
    200 		const char *spec, *type, *cp;
    201 		char	device[MAXPATHLEN];
    202 
    203 		spec = *argv;
    204 		cp = strrchr(spec, '/');
    205 		if (cp == 0) {
    206 			(void)snprintf(device, sizeof(device), "%s%s",
    207 				_PATH_DEV, spec);
    208 			spec = device;
    209 		}
    210 		if ((fs = getfsfile(spec)) == NULL &&
    211 		    (fs = getfsspec(spec)) == NULL) {
    212 			if (vfstype == NULL)
    213 				vfstype = getfslab(spec);
    214 			type = vfstype;
    215 		}
    216 		else {
    217 			spec = fs->fs_spec;
    218 			type = fs->fs_vfstype;
    219 			if (BADTYPE(fs->fs_type))
    220 				errx(1, "%s has unknown file system type.",
    221 				    spec);
    222 		}
    223 
    224 		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
    225 	}
    226 
    227 	return rval;
    228 }
    229 
    230 
    231 static void *
    232 isok(struct fstab *fs)
    233 {
    234 
    235 	if (fs->fs_passno == 0)
    236 		return NULL;
    237 
    238 	if (BADTYPE(fs->fs_type))
    239 		return NULL;
    240 
    241 	if (!selected(fs->fs_vfstype))
    242 		return NULL;
    243 
    244 	return fs;
    245 }
    246 
    247 
    248 static int
    249 checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
    250     pid_t *pidp)
    251 {
    252 	/* List of directories containing fsck_xxx subcommands. */
    253 	static const char *edirs[] = {
    254 #ifdef _PATH_RESCUE
    255 		_PATH_RESCUE,
    256 #endif
    257 		_PATH_SBIN,
    258 		_PATH_USRSBIN,
    259 		NULL
    260 	};
    261 	const char **argv, **edir;
    262 	pid_t pid;
    263 	int argc, i, status, maxargc;
    264 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
    265 	const char *extra = getoptions(vfstype);
    266 
    267 #ifdef __GNUC__
    268 	/* Avoid vfork clobbering */
    269 	(void) &optbuf;
    270 	(void) &vfstype;
    271 #endif
    272 
    273 	if (!strcmp(vfstype, "ufs"))
    274 		vfstype = MOUNT_UFS;
    275 
    276 	optbuf = NULL;
    277 	if (options)
    278 		catopt(&optbuf, options);
    279 	if (extra)
    280 		catopt(&optbuf, extra);
    281 
    282 	maxargc = 64;
    283 	argv = emalloc(sizeof(char *) * maxargc);
    284 
    285 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
    286 	argc = 0;
    287 	argv[argc++] = execbase;
    288 	if (optbuf)
    289 		mangle(optbuf, &argc, &argv, &maxargc);
    290 	argv[argc++] = spec;
    291 	argv[argc] = NULL;
    292 
    293 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
    294 		(void)printf("start %s %swait", mntpt,
    295 			pidp ? "no" : "");
    296 		for (i = 0; i < argc; i++)
    297 			(void)printf(" %s", argv[i]);
    298 		(void)printf("\n");
    299 	}
    300 
    301 	switch (pid = vfork()) {
    302 	case -1:				/* Error. */
    303 		warn("vfork");
    304 		if (optbuf)
    305 			free(optbuf);
    306 		return (1);
    307 
    308 	case 0:					/* Child. */
    309 		if ((flags & CHECK_FORCE) == 0) {
    310 			struct statfs	sfs;
    311 
    312 				/*
    313 				 * if mntpt is a mountpoint of a mounted file
    314 				 * system and it's mounted read-write, skip it
    315 				 * unless -f is given.
    316 				 */
    317 			if ((statfs(mntpt, &sfs) == 0) &&
    318 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
    319 			    ((sfs.f_flags & MNT_RDONLY) == 0)) {
    320 				printf(
    321 		"%s: file system is mounted read-write on %s; not checking\n",
    322 				    spec, mntpt);
    323 				if ((flags & CHECK_PREEN) && auxarg != NULL)
    324 					_exit(0);	/* fsck -p */
    325 				else
    326 					_exit(1);	/* fsck [[-p] ...] */
    327 			}
    328 		}
    329 
    330 		if (flags & CHECK_DEBUG)
    331 			_exit(0);
    332 
    333 		/* Go find an executable. */
    334 		edir = edirs;
    335 		do {
    336 			(void)snprintf(execname,
    337 			    sizeof(execname), "%s/%s", *edir, execbase);
    338 			execv(execname, (char * const *)argv);
    339 			if (errno != ENOENT) {
    340 				if (spec)
    341 					warn("exec %s for %s", execname, spec);
    342 				else
    343 					warn("exec %s", execname);
    344 			}
    345 		} while (*++edir != NULL);
    346 
    347 		if (errno == ENOENT) {
    348 			if (spec)
    349 				warn("exec %s for %s", execname, spec);
    350 			else
    351 				warn("exec %s", execname);
    352 		}
    353 		_exit(1);
    354 		/* NOTREACHED */
    355 
    356 	default:				/* Parent. */
    357 		if (optbuf)
    358 			free(optbuf);
    359 
    360 		if (pidp) {
    361 			*pidp = pid;
    362 			return 0;
    363 		}
    364 
    365 		if (waitpid(pid, &status, 0) < 0) {
    366 			warn("waitpid");
    367 			return (1);
    368 		}
    369 
    370 		if (WIFEXITED(status)) {
    371 			if (WEXITSTATUS(status) != 0)
    372 				return (WEXITSTATUS(status));
    373 		}
    374 		else if (WIFSIGNALED(status)) {
    375 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    376 			return (1);
    377 		}
    378 		break;
    379 	}
    380 
    381 	return (0);
    382 }
    383 
    384 
    385 static int
    386 selected(const char *type)
    387 {
    388 	struct entry *e;
    389 
    390 	/* If no type specified, it's always selected. */
    391 	TAILQ_FOREACH(e, &selhead, entries)
    392 		if (!strncmp(e->type, type, MFSNAMELEN))
    393 			return which == IN_LIST ? 1 : 0;
    394 
    395 	return which == IN_LIST ? 0 : 1;
    396 }
    397 
    398 
    399 static const char *
    400 getoptions(const char *type)
    401 {
    402 	struct entry *e;
    403 
    404 	TAILQ_FOREACH(e, &opthead, entries)
    405 		if (!strncmp(e->type, type, MFSNAMELEN))
    406 			return e->options;
    407 	return "";
    408 }
    409 
    410 
    411 static void
    412 addoption(char *optstr)
    413 {
    414 	char *newoptions;
    415 	struct entry *e;
    416 
    417 	if ((newoptions = strchr(optstr, ':')) == NULL)
    418 		errx(1, "Invalid option string");
    419 
    420 	*newoptions++ = '\0';
    421 
    422 	TAILQ_FOREACH(e, &opthead, entries)
    423 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    424 			catopt(&e->options, newoptions);
    425 			return;
    426 		}
    427 	addentry(&opthead, optstr, newoptions);
    428 }
    429 
    430 
    431 static void
    432 addentry(struct fstypelist *list, const char *type, const char *opts)
    433 {
    434 	struct entry *e;
    435 
    436 	e = emalloc(sizeof(struct entry));
    437 	e->type = estrdup(type);
    438 	e->options = estrdup(opts);
    439 	TAILQ_INSERT_TAIL(list, e, entries);
    440 }
    441 
    442 
    443 static void
    444 maketypelist(char *fslist)
    445 {
    446 	char *ptr;
    447 
    448 	if ((fslist == NULL) || (fslist[0] == '\0'))
    449 		errx(1, "empty type list");
    450 
    451 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    452 		fslist += 2;
    453 		which = NOT_IN_LIST;
    454 	}
    455 	else
    456 		which = IN_LIST;
    457 
    458 	while ((ptr = strsep(&fslist, ",")) != NULL)
    459 		addentry(&selhead, ptr, "");
    460 
    461 }
    462 
    463 
    464 static void
    465 catopt(char **sp, const char *o)
    466 {
    467 	char *s;
    468 	size_t i, j;
    469 
    470 	s = *sp;
    471 	if (s) {
    472 		i = strlen(s);
    473 		j = i + 1 + strlen(o) + 1;
    474 		s = erealloc(s, j);
    475 		(void)snprintf(s + i, j, ",%s", o);
    476 	} else
    477 		s = estrdup(o);
    478 	*sp = s;
    479 }
    480 
    481 
    482 static void
    483 mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
    484 {
    485 	char *p, *s;
    486 	int argc, maxargc;
    487 	const char **argv;
    488 
    489 	argc = *argcp;
    490 	argv = *argvp;
    491 	maxargc = *maxargcp;
    492 
    493 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    494 		/* Always leave space for one more argument and the NULL. */
    495 		if (argc >= maxargc - 3) {
    496 			maxargc <<= 1;
    497 			argv = erealloc(argv, maxargc * sizeof(char *));
    498 		}
    499 		if (*p != '\0')  {
    500 			if (*p == '-') {
    501 				argv[argc++] = p;
    502 				p = strchr(p, '=');
    503 				if (p) {
    504 					*p = '\0';
    505 					argv[argc++] = p+1;
    506 				}
    507 			} else {
    508 				argv[argc++] = "-o";
    509 				argv[argc++] = p;
    510 			}
    511 		}
    512 	}
    513 
    514 	*argcp = argc;
    515 	*argvp = argv;
    516 	*maxargcp = maxargc;
    517 }
    518 
    519 
    520 const static char *
    521 getfslab(const char *str)
    522 {
    523 	struct disklabel dl;
    524 	int fd;
    525 	char p;
    526 	const char *vfstype;
    527 	u_char t;
    528 
    529 	/* deduce the file system type from the disk label */
    530 	if ((fd = open(str, O_RDONLY)) == -1)
    531 		err(1, "cannot open `%s'", str);
    532 
    533 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
    534 		err(1, "cannot get disklabel for `%s'", str);
    535 
    536 	(void) close(fd);
    537 
    538 	p = str[strlen(str) - 1];
    539 
    540 	if ((p - 'a') >= dl.d_npartitions)
    541 		errx(1, "partition `%s' is not defined on disk", str);
    542 
    543 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
    544 		errx(1, "partition `%s' is not of a legal vfstype",
    545 		    str);
    546 
    547 	if ((vfstype = fscknames[t]) == NULL)
    548 		errx(1, "vfstype `%s' on partition `%s' is not supported",
    549 		    fstypenames[t], str);
    550 
    551 	return vfstype;
    552 }
    553 
    554 
    555 static void
    556 usage(void)
    557 {
    558 	static const char common[] =
    559 	    "[-dfnpvy] [-T fstype:fsoptions] [-t fstype]";
    560 
    561 	(void)fprintf(stderr, "Usage: %s %s [special|node]...\n",
    562 	    getprogname(), common);
    563 	exit(1);
    564 }
    565