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