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