1 /* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
7 * Copyright (c) 1980, 1989, 1993, 1994
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
35 * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
36 * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/queue.h>
47 #include <sys/ioctl.h>
63 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
65 static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
70 TAILQ_ENTRY(entry) entries;
73 static char *options = NULL;
75 static int forceflag = 0;
77 static int checkfs(const char *, const char *, const char *, const char *, pid_t *);
78 static int selected(const char *);
79 static void addoption(char *);
80 static const char *getoptions(const char *);
81 static void addentry(struct fstypelist *, const char *, const char *);
82 static void maketypelist(char *);
83 static void catopt(char **, const char *);
84 static void mangle(char *, int *, const char ** volatile *, int *);
85 static const char *getfstype(const char *);
86 static void usage(void) __dead2;
87 static int isok(struct fstab *);
101 main(int argc, char *argv[])
105 const char *vfstype = NULL;
107 const char *etc_fstab;
112 TAILQ_INIT(&selhead);
113 TAILQ_INIT(&opthead);
116 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
119 if (flags & CHECK_BACKGRD)
120 errx(1, "Cannot specify -B and -F.");
125 flags |= CHECK_DEBUG;
129 flags |= CHECK_VERBOSE;
133 if (flags & DO_BACKGRD)
134 errx(1, "Cannot specify -B and -F.");
135 flags |= CHECK_BACKGRD;
139 flags |= CHECK_PREEN;
142 flags |= CHECK_CLEAN;
147 catopt(&options, globopt);
153 catopt(&options, globopt);
157 warnx("Ignoring obsolete -l option\n");
166 if (!TAILQ_EMPTY(&selhead))
167 errx(1, "only one -t option may be specified.");
169 maketypelist(optarg);
186 if (etc_fstab != NULL)
190 return checkfstab(flags, isok, checkfs);
192 #define BADTYPE(type) \
193 (strcmp(type, FSTAB_RO) && \
194 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
197 for (; argc--; argv++) {
198 const char *spec, *mntpt, *type, *cp;
199 char device[MAXPATHLEN];
204 cp = strrchr(spec, '/');
206 (void)snprintf(device, sizeof(device), "%s%s",
210 mntp = getmntpt(spec);
212 spec = mntp->f_mntfromname;
213 mntpt = mntp->f_mntonname;
215 if ((fs = getfsfile(spec)) == NULL &&
216 (fs = getfsspec(spec)) == NULL) {
218 vfstype = getfstype(spec);
225 type = fs->fs_vfstype;
227 if (BADTYPE(fs->fs_type))
228 errx(1, "%s has unknown file system type.",
231 if ((flags & CHECK_BACKGRD) &&
232 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
233 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
236 if ((flags & DO_BACKGRD) && forceflag == 0 &&
237 checkfs(type, spec, mntpt, "-F", NULL) != 0)
240 rval |= checkfs(type, spec, mntpt, NULL, NULL);
248 isok(struct fstab *fs)
252 if (fs->fs_passno == 0)
254 if (BADTYPE(fs->fs_type))
256 if (!selected(fs->fs_vfstype))
259 * If the -B flag has been given, then process the needed
260 * background checks. Background checks cannot be run on
261 * file systems that will be mounted read-only or that were
262 * not mounted at boot time (typically those marked `noauto').
263 * If these basic tests are passed, check with the file system
264 * itself to see if it is willing to do background checking
265 * by invoking its check program with the -F flag.
267 if (flags & DO_BACKGRD) {
268 if (!strcmp(fs->fs_type, FSTAB_RO))
270 if (getmntpt(fs->fs_spec) == NULL)
272 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
277 * If the -F flag has been given, then consider deferring the
278 * check to background. Background checks cannot be run on
279 * file systems that will be mounted read-only or that will
280 * not be mounted at boot time (e.g., marked `noauto'). If
281 * these basic tests are passed, check with the file system
282 * itself to see if it is willing to defer to background
283 * checking by invoking its check program with the -F flag.
285 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
287 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
288 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
292 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
294 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
300 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
301 const char *auxopt, pid_t *pidp)
303 const char ** volatile argv;
305 int argc, i, status, maxargc;
306 char *optbuf, execbase[MAXPATHLEN];
307 char *vfstype = NULL;
308 const char *extra = NULL;
311 /* Avoid vfork clobbering */
316 * We convert the vfstype to lowercase and any spaces to underscores
317 * to not confuse the issue
319 * XXX This is a kludge to make automatic filesystem type guessing
320 * from the disklabel work for "4.2BSD" filesystems. It does a
321 * very limited subset of transliteration to a normalised form of
322 * filesystem name, and we do not seem to enforce a filesystem
323 * name character set.
325 vfstype = strdup(pvfstype);
327 perr("strdup(pvfstype)");
328 for (i = 0; i < (int)strlen(vfstype); i++) {
329 vfstype[i] = tolower(vfstype[i]);
330 if (vfstype[i] == ' ')
334 extra = getoptions(vfstype);
337 catopt(&optbuf, options);
339 catopt(&optbuf, extra);
341 catopt(&optbuf, auxopt);
342 else if (flags & DO_BACKGRD)
343 catopt(&optbuf, "-B");
346 argv = emalloc(sizeof(char *) * maxargc);
348 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
350 argv[argc++] = execbase;
352 mangle(optbuf, &argc, &argv, &maxargc);
356 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
357 (void)printf("start %s %swait", mntpt,
359 for (i = 0; i < argc; i++)
360 (void)printf(" %s", argv[i]);
364 switch (pid = vfork()) {
365 case -1: /* Error. */
373 if ((flags & CHECK_DEBUG) && auxopt == NULL)
376 /* Go find an executable. */
377 execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv));
379 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
381 warn("exec %s in %s", execbase, _PATH_SYSPATH);
385 default: /* Parent. */
396 if (waitpid(pid, &status, 0) < 0) {
401 if (WIFEXITED(status)) {
402 if (WEXITSTATUS(status) != 0)
403 return (WEXITSTATUS(status));
405 else if (WIFSIGNALED(status)) {
406 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
417 selected(const char *type)
421 /* If no type specified, it's always selected. */
422 TAILQ_FOREACH(e, &selhead, entries)
423 if (!strncmp(e->type, type, MFSNAMELEN))
424 return which == IN_LIST ? 1 : 0;
426 return which == IN_LIST ? 0 : 1;
431 getoptions(const char *type)
435 TAILQ_FOREACH(e, &opthead, entries)
436 if (!strncmp(e->type, type, MFSNAMELEN))
443 addoption(char *optstr)
448 if ((newoptions = strchr(optstr, ':')) == NULL)
449 errx(1, "Invalid option string");
451 *newoptions++ = '\0';
453 TAILQ_FOREACH(e, &opthead, entries)
454 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
455 catopt(&e->options, newoptions);
458 addentry(&opthead, optstr, newoptions);
463 addentry(struct fstypelist *list, const char *type, const char *opts)
467 e = emalloc(sizeof(struct entry));
468 e->type = estrdup(type);
469 e->options = estrdup(opts);
470 TAILQ_INSERT_TAIL(list, e, entries);
475 maketypelist(char *fslist)
479 if ((fslist == NULL) || (fslist[0] == '\0'))
480 errx(1, "empty type list");
482 if (fslist[0] == 'n' && fslist[1] == 'o') {
489 while ((ptr = strsep(&fslist, ",")) != NULL)
490 addentry(&selhead, ptr, "");
496 catopt(char **sp, const char *o)
504 j = i + 1 + strlen(o) + 1;
506 (void)snprintf(s + i, j, ",%s", o);
514 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
524 for (s = opts; (p = strsep(&s, ",")) != NULL;) {
525 /* Always leave space for one more argument and the NULL. */
526 if (argc >= maxargc - 3) {
528 argv = erealloc(argv, maxargc * sizeof(char *));
551 getfstype(const char *str)
553 struct diocgattr_arg attr;
556 if ((fd = open(str, O_RDONLY)) == -1)
557 err(1, "cannot open `%s'", str);
559 strncpy(attr.name, "PART::type", sizeof(attr.name));
560 memset(&attr.value, 0, sizeof(attr.value));
561 attr.len = sizeof(attr.value);
562 if (ioctl(fd, DIOCGATTR, &attr) == -1) {
567 for (i = 0; ptype_map[i].ptype != NULL; i++)
568 if (strstr(attr.value.str, ptype_map[i].ptype) != NULL)
569 return (ptype_map[i].name);
577 static const char common[] =
578 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
580 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
581 getprogname(), common);