Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.7
      1 /*	$NetBSD: fsck.c,v 1.7 1996/10/03 20:06:30 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. 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 static char rcsid[] = "$NetBSD: fsck.c,v 1.7 1996/10/03 20:06:30 christos Exp $";
     42 
     43 #include <sys/param.h>
     44 #include <sys/mount.h>
     45 #include <sys/queue.h>
     46 #include <sys/wait.h>
     47 
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <fstab.h>
     51 #include <signal.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 #include "pathnames.h"
     58 #include "fsutil.h"
     59 
     60 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
     61 
     62 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
     63 
     64 struct entry {
     65 	char *type;
     66 	char *options;
     67 	TAILQ_ENTRY(entry) entries;
     68 };
     69 
     70 static int maxrun = 0;
     71 static char *options = NULL;
     72 static int flags = 0;
     73 
     74 int main __P((int, char *[]));
     75 
     76 static int checkfs __P((const char *, const char *, const char *, void *,
     77     pid_t *));
     78 static int selected __P((const char *));
     79 static void addoption __P((char *));
     80 static const char *getoptions __P((const char *));
     81 static void addentry __P((struct fstypelist *, const char *, const char *));
     82 static void maketypelist __P((char *));
     83 static char *catopt __P((char *, const char *, int));
     84 static void mangle __P((char *, int *, const char ***, int *));
     85 static void usage __P((void));
     86 static void *isok __P((struct fstab *));
     87 
     88 
     89 int
     90 main(argc, argv)
     91 	int argc;
     92 	char *argv[];
     93 {
     94 	struct fstab *fs;
     95 	int i, rval = 0;
     96 	char *vfstype = NULL;
     97 	char globopt[3];
     98 
     99 	globopt[0] = '-';
    100 	globopt[2] = '\0';
    101 
    102 	TAILQ_INIT(&selhead);
    103 	TAILQ_INIT(&opthead);
    104 
    105 	while ((i = getopt(argc, argv, "dvpfnyl:t:T:")) != -1)
    106 		switch (i) {
    107 		case 'd':
    108 			flags |= CHECK_DEBUG;
    109 			break;
    110 
    111 		case 'v':
    112 			flags |= CHECK_VERBOSE;
    113 			break;
    114 
    115 		case 'p':
    116 			flags |= CHECK_PREEN;
    117 			/*FALLTHROUGH*/
    118 		case 'n':
    119 		case 'f':
    120 		case 'y':
    121 			globopt[1] = i;
    122 			options = catopt(options, globopt, 1);
    123 			break;
    124 
    125 		case 'l':
    126 			maxrun = atoi(optarg);
    127 			break;
    128 
    129 		case 'T':
    130 			if (*optarg)
    131 				addoption(optarg);
    132 			break;
    133 
    134 		case 't':
    135 			if (selhead.tqh_first != NULL)
    136 				errx(1, "only one -t option may be specified.");
    137 
    138 			maketypelist(optarg);
    139 			vfstype = optarg;
    140 			break;
    141 
    142 		case '?':
    143 		default:
    144 			usage();
    145 			/* NOTREACHED */
    146 		}
    147 
    148 	argc -= optind;
    149 	argv += optind;
    150 
    151 	if (argc == 0)
    152 		return checkfstab(flags, maxrun, isok, checkfs);
    153 
    154 #define	BADTYPE(type)							\
    155 	(strcmp(type, FSTAB_RO) &&					\
    156 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    157 
    158 
    159 	for (; argc--; argv++) {
    160 		char *spec, *type;
    161 
    162 		if ((fs = getfsfile(*argv)) == NULL &&
    163 		    (fs = getfsspec(*argv)) == NULL) {
    164 			if (vfstype == NULL)
    165 				errx(1,
    166 				    "%s: unknown special file or file system.",
    167 				    *argv);
    168 			spec = *argv;
    169 			type = vfstype;
    170 		}
    171 		else {
    172 			spec = fs->fs_spec;
    173 			type = fs->fs_vfstype;
    174 			if (BADTYPE(fs->fs_type))
    175 				errx(1, "%s has unknown file system type.",
    176 				    *argv);
    177 		}
    178 
    179 		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
    180 	}
    181 
    182 	return rval;
    183 }
    184 
    185 
    186 static void *
    187 isok(fs)
    188 	struct fstab *fs;
    189 {
    190 	if (fs->fs_passno == 0)
    191 		return NULL;
    192 
    193 	if (BADTYPE(fs->fs_type))
    194 		return NULL;
    195 
    196 	if (!selected(fs->fs_vfstype))
    197 		return NULL;
    198 
    199 	return fs;
    200 }
    201 
    202 
    203 static int
    204 checkfs(vfstype, spec, mntpt, auxarg, pidp)
    205 	const char *vfstype, *spec, *mntpt;
    206 	void *auxarg;
    207 	pid_t *pidp;
    208 {
    209 	/* List of directories containing fsck_xxx subcommands. */
    210 	static const char *edirs[] = {
    211 		_PATH_SBIN,
    212 		_PATH_USRSBIN,
    213 		NULL
    214 	};
    215 	const char **argv, **edir;
    216 	pid_t pid;
    217 	int argc, i, status, maxargc;
    218 	char *optbuf = NULL, execname[MAXPATHLEN + 1];
    219 	const char *extra = getoptions(vfstype);
    220 
    221 #ifdef __GNUC__
    222 	/* Avoid vfork clobbering */
    223 	(void) &optbuf;
    224 #endif
    225 
    226 	if (strcmp(vfstype, "ufs") == 0)
    227 		vfstype = MOUNT_UFS;
    228 
    229 	maxargc = 100;
    230 	argv = emalloc(sizeof(char *) * maxargc);
    231 
    232 	argc = 0;
    233 	argv[argc++] = vfstype;
    234 
    235 	if (options) {
    236 		if (extra != NULL)
    237 			optbuf = catopt(options, extra, 0);
    238 		else
    239 			optbuf = estrdup(options);
    240 	}
    241 	else if (extra)
    242 		optbuf = estrdup(extra);
    243 
    244 	if (optbuf)
    245 		mangle(optbuf, &argc, &argv, &maxargc);
    246 
    247 	argv[argc++] = spec;
    248 	argv[argc] = NULL;
    249 
    250 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
    251 		(void)printf("start %s %swait fsck_%s", mntpt,
    252 			pidp ? "no" : "", vfstype);
    253 		for (i = 1; i < argc; i++)
    254 			(void)printf(" %s", argv[i]);
    255 		(void)printf("\n");
    256 	}
    257 
    258 	switch (pid = vfork()) {
    259 	case -1:				/* Error. */
    260 		warn("vfork");
    261 		if (optbuf)
    262 			free(optbuf);
    263 		return (1);
    264 
    265 	case 0:					/* Child. */
    266 		if (flags & CHECK_DEBUG)
    267 			_exit(0);
    268 
    269 		/* Go find an executable. */
    270 		edir = edirs;
    271 		do {
    272 			(void)snprintf(execname,
    273 			    sizeof(execname), "%s/fsck_%s", *edir, vfstype);
    274 			execv(execname, (char * const *)argv);
    275 			if (errno != ENOENT)
    276 				if (spec)
    277 					warn("exec %s for %s", execname, spec);
    278 				else
    279 					warn("exec %s", execname);
    280 		} while (*++edir != NULL);
    281 
    282 		if (errno == ENOENT)
    283 			if (spec)
    284 				warn("exec %s for %s", execname, spec);
    285 			else
    286 				warn("exec %s", execname);
    287 		exit(1);
    288 		/* NOTREACHED */
    289 
    290 	default:				/* Parent. */
    291 		if (optbuf)
    292 			free(optbuf);
    293 
    294 		if (pidp) {
    295 			*pidp = pid;
    296 			return 0;
    297 		}
    298 
    299 		if (waitpid(pid, &status, 0) < 0) {
    300 			warn("waitpid");
    301 			return (1);
    302 		}
    303 
    304 		if (WIFEXITED(status)) {
    305 			if (WEXITSTATUS(status) != 0)
    306 				return (WEXITSTATUS(status));
    307 		}
    308 		else if (WIFSIGNALED(status)) {
    309 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    310 			return (1);
    311 		}
    312 		break;
    313 	}
    314 
    315 	return (0);
    316 }
    317 
    318 
    319 static int
    320 selected(type)
    321 	const char *type;
    322 {
    323 	struct entry *e;
    324 
    325 	/* If no type specified, it's always selected. */
    326 	for (e = selhead.tqh_first; e != NULL; e = e->entries.tqe_next)
    327 		if (!strncmp(e->type, type, MFSNAMELEN))
    328 			return which == IN_LIST ? 1 : 0;
    329 
    330 	return which == IN_LIST ? 0 : 1;
    331 }
    332 
    333 
    334 static const char *
    335 getoptions(type)
    336 	const char *type;
    337 {
    338 	struct entry *e;
    339 
    340 	for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next)
    341 		if (!strncmp(e->type, type, MFSNAMELEN))
    342 			return e->options;
    343 	return "";
    344 }
    345 
    346 
    347 static void
    348 addoption(optstr)
    349 	char *optstr;
    350 {
    351 	char *newoptions;
    352 	struct entry *e;
    353 
    354 	if ((newoptions = strchr(optstr, ':')) == NULL)
    355 		errx(1, "Invalid option string");
    356 
    357 	*newoptions++ = '\0';
    358 
    359 	for (e = opthead.tqh_first; e != NULL; e = e->entries.tqe_next)
    360 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    361 			e->options = catopt(e->options, newoptions, 1);
    362 			return;
    363 		}
    364 	addentry(&opthead, optstr, newoptions);
    365 }
    366 
    367 
    368 static void
    369 addentry(list, type, opts)
    370 	struct fstypelist *list;
    371 	const char *type;
    372 	const char *opts;
    373 {
    374 	struct entry *e;
    375 
    376 	e = emalloc(sizeof(struct entry));
    377 	e->type = estrdup(type);
    378 	e->options = estrdup(opts);
    379 	TAILQ_INSERT_TAIL(list, e, entries);
    380 }
    381 
    382 
    383 static void
    384 maketypelist(fslist)
    385 	char *fslist;
    386 {
    387 	char *ptr;
    388 
    389 	if ((fslist == NULL) || (fslist[0] == '\0'))
    390 		errx(1, "empty type list");
    391 
    392 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    393 		fslist += 2;
    394 		which = NOT_IN_LIST;
    395 	}
    396 	else
    397 		which = IN_LIST;
    398 
    399 	while ((ptr = strsep(&fslist, ",")) != NULL)
    400 		addentry(&selhead, ptr, "");
    401 
    402 }
    403 
    404 
    405 static char *
    406 catopt(s0, s1, fr)
    407 	char *s0;
    408 	const char *s1;
    409 	int fr;
    410 {
    411 	size_t i;
    412 	char *cp;
    413 
    414 	if (s0 && *s0) {
    415 		i = strlen(s0) + strlen(s1) + 1 + 1;
    416 		cp = emalloc(i);
    417 		(void)snprintf(cp, i, "%s,%s", s0, s1);
    418 	}
    419 	else
    420 		cp = estrdup(s1);
    421 
    422 	if (s0 && fr)
    423 		free(s0);
    424 	return (cp);
    425 }
    426 
    427 
    428 static void
    429 mangle(opts, argcp, argvp, maxargcp)
    430 	char *opts;
    431 	int *argcp;
    432 	const char ***argvp;
    433 	int *maxargcp;
    434 {
    435 	char *p, *s;
    436 	int argc = *argcp, maxargc = *maxargcp;
    437 	const char **argv = *argvp;
    438 
    439 	argc = *argcp;
    440 	maxargc = *maxargcp;
    441 
    442 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    443 		/* always leave space for one more argument and the NULL */
    444 		if (argc >= maxargc - 3) {
    445 			maxargc += 50;
    446 			argv = erealloc(argv, maxargc * sizeof(char *));
    447 		}
    448 		if (*p != '\0')
    449 			if (*p == '-') {
    450 				argv[argc++] = p;
    451 				p = strchr(p, '=');
    452 				if (p) {
    453 					*p = '\0';
    454 					argv[argc++] = p+1;
    455 				}
    456 			}
    457 			else {
    458 				argv[argc++] = "-o";
    459 				argv[argc++] = p;
    460 			}
    461 	}
    462 
    463 	*argcp = argc;
    464 	*argvp = argv;
    465 	*maxargcp = maxargc;
    466 }
    467 
    468 
    469 static void
    470 usage()
    471 {
    472 	extern char *__progname;
    473 	static const char common[] =
    474 	    "[-dpvlyn] [-T fstype:fsoptions] [-t fstype]";
    475 
    476 	(void)fprintf(stderr, "Usage: %s %s [special|node]...\n",
    477 	    __progname, common);
    478 	exit(1);
    479 }
    480