GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/fsck/fsck.c Lines: 80 208 38.5 %
Date: 2017-11-07 Branches: 36 156 23.1 %

Line Branch Exec Source
1
/*	$OpenBSD: fsck.c,v 1.38 2015/11/23 19:19:29 deraadt Exp $	*/
2
/*	$NetBSD: fsck.c,v 1.7 1996/10/03 20:06:30 christos Exp $	*/
3
4
/*
5
 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
6
 * Copyright (c) 1980, 1989, 1993, 1994
7
 *	The Regents of the University of California.  All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. Neither the name of the University nor the names of its contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 *
33
 * From: @(#)mount.c	8.19 (Berkeley) 4/19/94
34
 * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
35
 *
36
 */
37
38
#include <sys/types.h>
39
#include <sys/mount.h>
40
#include <sys/queue.h>
41
#include <sys/resource.h>
42
#include <sys/wait.h>
43
44
#include <err.h>
45
#include <errno.h>
46
#include <fstab.h>
47
#include <signal.h>
48
#include <stdio.h>
49
#include <stdlib.h>
50
#include <string.h>
51
#include <unistd.h>
52
#include <limits.h>
53
#include <util.h>
54
55
#include "pathnames.h"
56
#include "fsutil.h"
57
58
static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
59
static enum { NONET_FILTER, NET_FILTER } filter = NONET_FILTER;
60
61
TAILQ_HEAD(fstypelist, entry) opthead, selhead;
62
63
struct entry {
64
	char *type;
65
	char *options;
66
	TAILQ_ENTRY(entry) entries;
67
};
68
69
static int maxrun;
70
static char *options;
71
static int flags;
72
73
int main(int, char *[]);
74
75
static int checkfs(const char *, const char *, const char *, void *, pid_t *);
76
static int selected(const char *);
77
static void addoption(char *);
78
static const char *getoptions(const char *);
79
static void addentry(struct fstypelist *, const char *, const char *);
80
static void maketypelist(char *);
81
static char *catopt(char *, const char *, int);
82
static void mangle(char *, int *, const char ***, int *);
83
static void usage(void);
84
static void *isok(struct fstab *);
85
static int hasopt(const char *, const char *);
86
87
88
int
89
main(int argc, char *argv[])
90
{
91
32
	const char *errstr;
92
	struct fstab *fs;
93
	int i, rval = 0;
94
	char *vfstype = NULL;
95
16
	char *p, globopt[3];
96
16
	struct rlimit rl;
97
98
	/* Increase our data size to the max */
99
16
	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
100
16
		if (geteuid() == 0)
101
16
			rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
102
		else
103
			rl.rlim_cur = rl.rlim_max;
104
16
		if (setrlimit(RLIMIT_DATA, &rl) < 0)
105
			warn("Can't set resource limit to max data size");
106
	} else
107
		warn("Can't get resource limit for data size");
108
109
16
	if (pledge("stdio rpath wpath disklabel proc exec flock cpath", NULL) == -1)
110
		err(1, "pledge");
111
112
16
	globopt[0] = '-';
113
16
	globopt[2] = '\0';
114
115
16
	TAILQ_INIT(&selhead);
116
16
	TAILQ_INIT(&opthead);
117
118
64
	while ((i = getopt(argc, argv, "b:dfl:nNpT:t:vy")) != -1)
119



32
		switch (i) {
120
		case 'd':
121
			flags |= CHECK_DEBUG;
122
			break;
123
124
		case 'v':
125
			flags |= CHECK_VERBOSE;
126
			break;
127
128
		case 'p':
129
			flags |= CHECK_PREEN;
130
			/*FALLTHROUGH*/
131
		case 'n':
132
		case 'f':
133
		case 'y':
134
32
			globopt[1] = i;
135
32
			options = catopt(options, globopt, 1);
136
32
			break;
137
138
		case 'b':
139
			if (asprintf(&p, "-b %s", optarg) == -1)
140
				err(1, "malloc failed");
141
			options = catopt(options, p, 1);
142
			free(p);
143
			break;
144
145
		case 'l':
146
			maxrun = strtonum(optarg, 0, INT_MAX, &errstr);
147
			if (errstr)
148
				errx(1, "-l %s: %s", optarg, errstr);
149
150
			break;
151
152
		case 'T':
153
			if (*optarg)
154
				addoption(optarg);
155
			break;
156
157
		case 't':
158
			if (!TAILQ_EMPTY(&selhead))
159
				errx(1, "only one -t option may be specified.");
160
161
			maketypelist(optarg);
162
			vfstype = optarg;
163
			break;
164
165
		case 'N':
166
			filter = NET_FILTER;
167
			break;
168
169
		case '?':
170
		default:
171
			usage();
172
			/* NOTREACHED */
173
		}
174
175
16
	argc -= optind;
176
16
	argv += optind;
177
178
16
	if (argc == 0)
179
		return checkfstab(flags, maxrun, isok, checkfs);
180
181
#define	BADTYPE(type)							\
182
	(strcmp(type, FSTAB_RO) &&					\
183
	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
184
185
186
48
	for (; argc--; argv++) {
187
		char *spec, *type;
188
189

32
		if ((strncmp(*argv, "/dev/", 5) == 0 || isduid(*argv, 0)) &&
190
16
		    (type = readlabelfs(*argv, 0))) {
191
16
			spec = *argv;
192

16
		} else if ((fs = getfsfile(*argv)) == NULL &&
193
		    (fs = getfsspec(*argv)) == NULL) {
194
			if (vfstype == NULL)
195
				errx(1,
196
				    "%s: unknown special file or file system.",
197
				    *argv);
198
			spec = *argv;
199
			type = vfstype;
200
		} else {
201
			spec = fs->fs_spec;
202
			type = fs->fs_vfstype;
203
			if (BADTYPE(fs->fs_type))
204
				errx(1, "%s has unknown file system type.",
205
				    *argv);
206
		}
207
208
16
		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
209
	}
210
211
16
	return rval;
212
16
}
213
214
215
static void *
216
isok(struct fstab *fs)
217
{
218
	if (fs->fs_passno == 0)
219
		return NULL;
220
221
	if (BADTYPE(fs->fs_type))
222
		return NULL;
223
224
	switch (filter) {
225
	case NET_FILTER:
226
		if (!hasopt(fs->fs_mntops, "net"))
227
			return NULL;
228
		break;
229
	case NONET_FILTER:
230
		if (hasopt(fs->fs_mntops, "net"))
231
			return NULL;
232
		break;
233
	}
234
	if (!selected(fs->fs_vfstype))
235
		return NULL;
236
237
	return fs;
238
}
239
240
241
static int
242
checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
243
    pid_t *pidp)
244
{
245
	/* List of directories containing fsck_xxx subcommands. */
246
	static const char *edirs[] = {
247
		_PATH_SBIN,
248
		_PATH_USRSBIN,
249
		NULL
250
	};
251
32
	const char **argv, **edir;
252
	pid_t pid;
253
16
	int argc, i, status, maxargc;
254
16
	char *optbuf = NULL, fsname[PATH_MAX], execname[PATH_MAX];
255
16
	const char *extra = getoptions(vfstype);
256
257
16
	if (strcmp(vfstype, "ufs") == 0)
258
		vfstype = MOUNT_UFS;
259
260
16
	maxargc = 100;
261
16
	argv = ereallocarray(NULL, maxargc, sizeof(char *));
262
263
16
	argc = 0;
264
16
	(void)snprintf(fsname, sizeof(fsname), "fsck_%s", vfstype);
265
16
	argv[argc++] = fsname;
266
267
16
	if (options) {
268
16
		if (extra != NULL)
269
16
			optbuf = catopt(options, extra, 0);
270
		else
271
			optbuf = estrdup(options);
272
	}
273
	else if (extra)
274
		optbuf = estrdup(extra);
275
276
16
	if (optbuf)
277
16
		mangle(optbuf, &argc, &argv, &maxargc);
278
279
16
	argv[argc++] = spec;
280
16
	argv[argc] = NULL;
281
282
16
	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
283
		(void)printf("start %s %swait %s", mntpt,
284
			pidp ? "no" : "", fsname);
285
		for (i = 1; i < argc; i++)
286
			(void)printf(" %s", argv[i]);
287
		(void)printf("\n");
288
	}
289
290
16
	switch (pid = fork()) {
291
	case -1:				/* Error. */
292
		warn("fork");
293
		free(optbuf);
294
		free(argv);
295
		return (1);
296
297
	case 0:					/* Child. */
298
		if (flags & CHECK_DEBUG)
299
			_exit(0);
300
301
		/* Go find an executable. */
302
		edir = edirs;
303
		do {
304
			(void)snprintf(execname,
305
			    sizeof(execname), "%s/fsck_%s", *edir, vfstype);
306
			execv(execname, (char * const *)argv);
307
			if (errno != ENOENT) {
308
				if (spec)
309
					warn("exec %s for %s", execname, spec);
310
				else
311
					warn("exec %s", execname);
312
			}
313
		} while (*++edir != NULL);
314
315
		if (errno == ENOENT) {
316
			if (spec)
317
				warn("exec %s for %s", execname, spec);
318
			else
319
				warn("exec %s", execname);
320
		}
321
		exit(1);
322
		/* NOTREACHED */
323
324
	default:				/* Parent. */
325
16
		free(optbuf);
326
16
		free(argv);
327
328
16
		if (pidp) {
329
			*pidp = pid;
330
			return 0;
331
		}
332
333
16
		if (waitpid(pid, &status, 0) < 0) {
334
			warn("waitpid");
335
			return (1);
336
		}
337
338
16
		if (WIFEXITED(status)) {
339
16
			if (WEXITSTATUS(status) != 0)
340
				return (WEXITSTATUS(status));
341
		}
342
		else if (WIFSIGNALED(status)) {
343
			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
344
			return (1);
345
		}
346
		break;
347
	}
348
349
16
	return (0);
350
16
}
351
352
353
static int
354
selected(const char *type)
355
{
356
	struct entry *e;
357
358
	/* If no type specified, it's always selected. */
359
	TAILQ_FOREACH(e, &selhead, entries)
360
		if (!strncmp(e->type, type, MFSNAMELEN))
361
			return which == IN_LIST ? 1 : 0;
362
363
	return which == IN_LIST ? 0 : 1;
364
}
365
366
367
static const char *
368
getoptions(const char *type)
369
{
370
	struct entry *e;
371
372
48
	TAILQ_FOREACH(e, &opthead, entries)
373
		if (!strncmp(e->type, type, MFSNAMELEN))
374
			return e->options;
375
16
	return "";
376
16
}
377
378
379
static void
380
addoption(char *optstr)
381
{
382
	char *newoptions;
383
	struct entry *e;
384
385
	if ((newoptions = strchr(optstr, ':')) == NULL)
386
		errx(1, "Invalid option string");
387
388
	*newoptions++ = '\0';
389
390
	TAILQ_FOREACH(e, &opthead, entries)
391
		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
392
			e->options = catopt(e->options, newoptions, 1);
393
			return;
394
		}
395
	addentry(&opthead, optstr, newoptions);
396
}
397
398
399
static void
400
addentry(struct fstypelist *list, const char *type, const char *opts)
401
{
402
	struct entry *e;
403
404
	e = emalloc(sizeof(struct entry));
405
	e->type = estrdup(type);
406
	e->options = estrdup(opts);
407
	TAILQ_INSERT_TAIL(list, e, entries);
408
}
409
410
411
static void
412
maketypelist(char *fslist)
413
{
414
	char *ptr;
415
416
	if ((fslist == NULL) || (fslist[0] == '\0'))
417
		errx(1, "empty type list");
418
419
	if (fslist[0] == 'n' && fslist[1] == 'o') {
420
		fslist += 2;
421
		which = NOT_IN_LIST;
422
	}
423
	else
424
		which = IN_LIST;
425
426
	while ((ptr = strsep(&fslist, ",")) != NULL)
427
		addentry(&selhead, ptr, "");
428
429
}
430
431
432
static char *
433
catopt(char *s0, const char *s1, int fr)
434
{
435
96
	char *cp;
436
437

80
	if (s0 && *s0) {
438
32
		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
439
			err(1, "malloc failed");
440
	} else
441
16
		cp = estrdup(s1);
442
443
48
	if (fr)
444
32
		free(s0);
445
96
	return (cp);
446
48
}
447
448
449
static void
450
mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
451
{
452
32
	char *p, *s;
453
16
	int argc = *argcp, maxargc = *maxargcp;
454
16
	const char **argv = *argvp;
455
456
80
	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
457
		/* always leave space for one more argument and the NULL */
458
48
		if (argc >= maxargc - 3) {
459
			int newmaxargc = maxargc + 50;
460
461
			argv = ereallocarray(argv, newmaxargc, sizeof(char *));
462
			maxargc = newmaxargc;
463
		}
464
48
		if (*p != '\0') {
465
32
			if (*p == '-') {
466
32
				argv[argc++] = p;
467
32
				p = strchr(p, '=');
468
32
				if (p) {
469
					*p = '\0';
470
					argv[argc++] = p+1;
471
				}
472
			}
473
			else {
474
				argv[argc++] = "-o";
475
				argv[argc++] = p;
476
			}
477
		}
478
	}
479
480
16
	*argcp = argc;
481
16
	*argvp = argv;
482
16
	*maxargcp = maxargc;
483
16
}
484
485
static int
486
hasopt(const char *mntopts, const char *option)
487
{
488
	int found;
489
	char *opt, *optbuf;
490
491
	if (mntopts == NULL)
492
		return (0);
493
	optbuf = strdup(mntopts);
494
	found = 0;
495
	for (opt = optbuf; !found && opt != NULL; strsep(&opt, ","))
496
		found = !strncmp(opt, option, strlen(option));
497
	free(optbuf);
498
	return (found);
499
}
500
501
502
static void
503
usage(void)
504
{
505
	extern char *__progname;
506
507
	fprintf(stderr, "usage: %s "
508
	    "[-dfNnpvy] [-b block#] [-l maxparallel] [-T fstype:fsoptions]\n"
509
	    "            [-t fstype] [special | node ...]\n", __progname);
510
	exit(1);
511
}