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