Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.32
      1 /*	$NetBSD: fsck.c,v 1.32 2004/01/05 23:23:32 jmmv 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.32 2004/01/05 23:23:32 jmmv 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, "dfl:npqT:t:vy")) != -1) {
    143 		switch (i) {
    144 		case 'd':
    145 			flags |= CHECK_DEBUG;
    146 			continue;
    147 
    148 		case 'f':
    149 			flags |= CHECK_FORCE;
    150 			break;
    151 
    152 		case 'n':
    153 			break;
    154 
    155 		case 'p':
    156 			flags |= CHECK_PREEN;
    157 			break;
    158 
    159 		case 'q':
    160 			break;
    161 
    162 		case 'l':
    163 			maxrun = atoi(optarg);
    164 			continue;
    165 
    166 		case 'T':
    167 			if (*optarg)
    168 				addoption(optarg);
    169 			continue;
    170 
    171 		case 't':
    172 			if (TAILQ_FIRST(&selhead) != NULL)
    173 				errx(1, "only one -t option may be specified.");
    174 
    175 			maketypelist(optarg);
    176 			vfstype = optarg;
    177 			continue;
    178 
    179 		case 'v':
    180 			flags |= CHECK_VERBOSE;
    181 			continue;
    182 
    183 		case 'y':
    184 			break;
    185 
    186 		case '?':
    187 		default:
    188 			usage();
    189 			/* NOTREACHED */
    190 		}
    191 
    192 		/* Pass option to fsck_xxxfs */
    193 		globopt[1] = i;
    194 		catopt(&options, globopt);
    195 	}
    196 
    197 	argc -= optind;
    198 	argv += optind;
    199 
    200 	if (argc == 0)
    201 		return checkfstab(flags, maxrun, isok, checkfs);
    202 
    203 #define	BADTYPE(type)							\
    204 	(strcmp(type, FSTAB_RO) &&					\
    205 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    206 
    207 
    208 	for (; argc--; argv++) {
    209 		const char *spec, *type, *cp;
    210 		char	device[MAXPATHLEN];
    211 
    212 		spec = *argv;
    213 		cp = strrchr(spec, '/');
    214 		if (cp == 0) {
    215 			(void)snprintf(device, sizeof(device), "%s%s",
    216 				_PATH_DEV, spec);
    217 			spec = device;
    218 		}
    219 		if ((fs = getfsfile(spec)) == NULL &&
    220 		    (fs = getfsspec(spec)) == NULL) {
    221 			if (vfstype == NULL)
    222 				vfstype = getfslab(spec);
    223 			type = vfstype;
    224 		}
    225 		else {
    226 			spec = fs->fs_spec;
    227 			type = fs->fs_vfstype;
    228 			if (BADTYPE(fs->fs_type))
    229 				errx(1, "%s has unknown file system type.",
    230 				    spec);
    231 		}
    232 
    233 		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
    234 	}
    235 
    236 	return rval;
    237 }
    238 
    239 
    240 static void *
    241 isok(struct fstab *fs)
    242 {
    243 
    244 	if (fs->fs_passno == 0)
    245 		return NULL;
    246 
    247 	if (BADTYPE(fs->fs_type))
    248 		return NULL;
    249 
    250 	if (!selected(fs->fs_vfstype))
    251 		return NULL;
    252 
    253 	return fs;
    254 }
    255 
    256 
    257 static int
    258 checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
    259     pid_t *pidp)
    260 {
    261 	/* List of directories containing fsck_xxx subcommands. */
    262 	static const char *edirs[] = {
    263 #ifdef _PATH_RESCUE
    264 		_PATH_RESCUE,
    265 #endif
    266 		_PATH_SBIN,
    267 		_PATH_USRSBIN,
    268 		NULL
    269 	};
    270 	const char **argv, **edir;
    271 	pid_t pid;
    272 	int argc, i, status, maxargc;
    273 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
    274 	const char *extra = getoptions(vfstype);
    275 
    276 #ifdef __GNUC__
    277 	/* Avoid vfork clobbering */
    278 	(void) &optbuf;
    279 	(void) &vfstype;
    280 #endif
    281 
    282 	if (!strcmp(vfstype, "ufs"))
    283 		vfstype = MOUNT_UFS;
    284 
    285 	optbuf = NULL;
    286 	if (options)
    287 		catopt(&optbuf, options);
    288 	if (extra)
    289 		catopt(&optbuf, extra);
    290 
    291 	maxargc = 64;
    292 	argv = emalloc(sizeof(char *) * maxargc);
    293 
    294 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
    295 	argc = 0;
    296 	argv[argc++] = execbase;
    297 	if (optbuf)
    298 		mangle(optbuf, &argc, &argv, &maxargc);
    299 	argv[argc++] = spec;
    300 	argv[argc] = NULL;
    301 
    302 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
    303 		(void)printf("start %s %swait", mntpt,
    304 			pidp ? "no" : "");
    305 		for (i = 0; i < argc; i++)
    306 			(void)printf(" %s", argv[i]);
    307 		(void)printf("\n");
    308 	}
    309 
    310 	switch (pid = vfork()) {
    311 	case -1:				/* Error. */
    312 		warn("vfork");
    313 		if (optbuf)
    314 			free(optbuf);
    315 		return (1);
    316 
    317 	case 0:					/* Child. */
    318 		if ((flags & CHECK_FORCE) == 0) {
    319 			struct statfs	sfs;
    320 
    321 				/*
    322 				 * if mntpt is a mountpoint of a mounted file
    323 				 * system and it's mounted read-write, skip it
    324 				 * unless -f is given.
    325 				 */
    326 			if ((statfs(mntpt, &sfs) == 0) &&
    327 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
    328 			    ((sfs.f_flags & MNT_RDONLY) == 0)) {
    329 				printf(
    330 		"%s: file system is mounted read-write on %s; not checking\n",
    331 				    spec, mntpt);
    332 				if ((flags & CHECK_PREEN) && auxarg != NULL)
    333 					_exit(0);	/* fsck -p */
    334 				else
    335 					_exit(1);	/* fsck [[-p] ...] */
    336 			}
    337 		}
    338 
    339 		if (flags & CHECK_DEBUG)
    340 			_exit(0);
    341 
    342 		/* Go find an executable. */
    343 		edir = edirs;
    344 		do {
    345 			(void)snprintf(execname,
    346 			    sizeof(execname), "%s/%s", *edir, execbase);
    347 			execv(execname, (char * const *)argv);
    348 			if (errno != ENOENT) {
    349 				if (spec)
    350 					warn("exec %s for %s", execname, spec);
    351 				else
    352 					warn("exec %s", execname);
    353 			}
    354 		} while (*++edir != NULL);
    355 
    356 		if (errno == ENOENT) {
    357 			if (spec)
    358 				warn("exec %s for %s", execname, spec);
    359 			else
    360 				warn("exec %s", execname);
    361 		}
    362 		_exit(1);
    363 		/* NOTREACHED */
    364 
    365 	default:				/* Parent. */
    366 		if (optbuf)
    367 			free(optbuf);
    368 
    369 		if (pidp) {
    370 			*pidp = pid;
    371 			return 0;
    372 		}
    373 
    374 		if (waitpid(pid, &status, 0) < 0) {
    375 			warn("waitpid");
    376 			return (1);
    377 		}
    378 
    379 		if (WIFEXITED(status)) {
    380 			if (WEXITSTATUS(status) != 0)
    381 				return (WEXITSTATUS(status));
    382 		}
    383 		else if (WIFSIGNALED(status)) {
    384 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    385 			return (1);
    386 		}
    387 		break;
    388 	}
    389 
    390 	return (0);
    391 }
    392 
    393 
    394 static int
    395 selected(const char *type)
    396 {
    397 	struct entry *e;
    398 
    399 	/* If no type specified, it's always selected. */
    400 	TAILQ_FOREACH(e, &selhead, entries)
    401 		if (!strncmp(e->type, type, MFSNAMELEN))
    402 			return which == IN_LIST ? 1 : 0;
    403 
    404 	return which == IN_LIST ? 0 : 1;
    405 }
    406 
    407 
    408 static const char *
    409 getoptions(const char *type)
    410 {
    411 	struct entry *e;
    412 
    413 	TAILQ_FOREACH(e, &opthead, entries)
    414 		if (!strncmp(e->type, type, MFSNAMELEN))
    415 			return e->options;
    416 	return "";
    417 }
    418 
    419 
    420 static void
    421 addoption(char *optstr)
    422 {
    423 	char *newoptions;
    424 	struct entry *e;
    425 
    426 	if ((newoptions = strchr(optstr, ':')) == NULL)
    427 		errx(1, "Invalid option string");
    428 
    429 	*newoptions++ = '\0';
    430 
    431 	TAILQ_FOREACH(e, &opthead, entries)
    432 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    433 			catopt(&e->options, newoptions);
    434 			return;
    435 		}
    436 	addentry(&opthead, optstr, newoptions);
    437 }
    438 
    439 
    440 static void
    441 addentry(struct fstypelist *list, const char *type, const char *opts)
    442 {
    443 	struct entry *e;
    444 
    445 	e = emalloc(sizeof(struct entry));
    446 	e->type = estrdup(type);
    447 	e->options = estrdup(opts);
    448 	TAILQ_INSERT_TAIL(list, e, entries);
    449 }
    450 
    451 
    452 static void
    453 maketypelist(char *fslist)
    454 {
    455 	char *ptr;
    456 
    457 	if ((fslist == NULL) || (fslist[0] == '\0'))
    458 		errx(1, "empty type list");
    459 
    460 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    461 		fslist += 2;
    462 		which = NOT_IN_LIST;
    463 	}
    464 	else
    465 		which = IN_LIST;
    466 
    467 	while ((ptr = strsep(&fslist, ",")) != NULL)
    468 		addentry(&selhead, ptr, "");
    469 
    470 }
    471 
    472 
    473 static void
    474 catopt(char **sp, const char *o)
    475 {
    476 	char *s;
    477 	size_t i, j;
    478 
    479 	s = *sp;
    480 	if (s) {
    481 		i = strlen(s);
    482 		j = i + 1 + strlen(o) + 1;
    483 		s = erealloc(s, j);
    484 		(void)snprintf(s + i, j, ",%s", o);
    485 	} else
    486 		s = estrdup(o);
    487 	*sp = s;
    488 }
    489 
    490 
    491 static void
    492 mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
    493 {
    494 	char *p, *s;
    495 	int argc, maxargc;
    496 	const char **argv;
    497 
    498 	argc = *argcp;
    499 	argv = *argvp;
    500 	maxargc = *maxargcp;
    501 
    502 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    503 		/* Always leave space for one more argument and the NULL. */
    504 		if (argc >= maxargc - 3) {
    505 			maxargc <<= 1;
    506 			argv = erealloc(argv, maxargc * sizeof(char *));
    507 		}
    508 		if (*p != '\0')  {
    509 			if (*p == '-') {
    510 				argv[argc++] = p;
    511 				p = strchr(p, '=');
    512 				if (p) {
    513 					*p = '\0';
    514 					argv[argc++] = p+1;
    515 				}
    516 			} else {
    517 				argv[argc++] = "-o";
    518 				argv[argc++] = p;
    519 			}
    520 		}
    521 	}
    522 
    523 	*argcp = argc;
    524 	*argvp = argv;
    525 	*maxargcp = maxargc;
    526 }
    527 
    528 
    529 const static char *
    530 getfslab(const char *str)
    531 {
    532 	struct disklabel dl;
    533 	int fd;
    534 	char p;
    535 	const char *vfstype;
    536 	u_char t;
    537 
    538 	/* deduce the file system type from the disk label */
    539 	if ((fd = open(str, O_RDONLY)) == -1)
    540 		err(1, "cannot open `%s'", str);
    541 
    542 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
    543 		err(1, "cannot get disklabel for `%s'", str);
    544 
    545 	(void) close(fd);
    546 
    547 	p = str[strlen(str) - 1];
    548 
    549 	if ((p - 'a') >= dl.d_npartitions)
    550 		errx(1, "partition `%s' is not defined on disk", str);
    551 
    552 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
    553 		errx(1, "partition `%s' is not of a legal vfstype",
    554 		    str);
    555 
    556 	if ((vfstype = fscknames[t]) == NULL)
    557 		errx(1, "vfstype `%s' on partition `%s' is not supported",
    558 		    fstypenames[t], str);
    559 
    560 	return vfstype;
    561 }
    562 
    563 
    564 static void
    565 usage(void)
    566 {
    567 	static const char common[] =
    568 	    "[-dfnpqvy] [-T fstype:fsoptions] [-t fstype]";
    569 
    570 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
    571 	    getprogname(), common);
    572 	exit(1);
    573 }
    574