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>
62 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
64 static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
69 TAILQ_ENTRY(entry) entries;
72 static char *options = NULL;
74 static int forceflag = 0;
76 static int checkfs(const char *, const char *, const char *, const char *, pid_t *);
77 static int selected(const char *);
78 static void addoption(char *);
79 static const char *getoptions(const char *);
80 static void addentry(struct fstypelist *, const char *, const char *);
81 static void maketypelist(char *);
82 static void catopt(char **, const char *);
83 static void mangle(char *, int *, const char ** volatile *, int *);
84 static const char *getfstype(const char *);
85 static void usage(void) __dead2;
86 static int isok(struct fstab *);
100 main(int argc, char *argv[])
104 const char *vfstype = NULL;
106 const char *etc_fstab;
111 TAILQ_INIT(&selhead);
112 TAILQ_INIT(&opthead);
115 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
118 if (flags & CHECK_BACKGRD)
119 errx(1, "Cannot specify -B and -F.");
124 flags |= CHECK_DEBUG;
128 flags |= CHECK_VERBOSE;
132 if (flags & DO_BACKGRD)
133 errx(1, "Cannot specify -B and -F.");
134 flags |= CHECK_BACKGRD;
138 flags |= CHECK_PREEN;
141 flags |= CHECK_CLEAN;
146 catopt(&options, globopt);
152 catopt(&options, globopt);
156 warnx("Ignoring obsolete -l option\n");
165 if (!TAILQ_EMPTY(&selhead))
166 errx(1, "only one -t option may be specified.");
168 maketypelist(optarg);
185 if (etc_fstab != NULL)
189 return checkfstab(flags, isok, checkfs);
191 #define BADTYPE(type) \
192 (strcmp(type, FSTAB_RO) && \
193 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
196 for (; argc--; argv++) {
197 const char *spec, *mntpt, *type, *cp;
198 char device[MAXPATHLEN];
203 cp = strrchr(spec, '/');
205 (void)snprintf(device, sizeof(device), "%s%s",
209 mntp = getmntpt(spec);
211 spec = mntp->f_mntfromname;
212 mntpt = mntp->f_mntonname;
214 if ((fs = getfsfile(spec)) == NULL &&
215 (fs = getfsspec(spec)) == NULL) {
217 vfstype = getfstype(spec);
224 type = fs->fs_vfstype;
226 if (BADTYPE(fs->fs_type))
227 errx(1, "%s has unknown file system type.",
230 if ((flags & CHECK_BACKGRD) &&
231 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
232 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
235 if ((flags & DO_BACKGRD) && forceflag == 0 &&
236 checkfs(type, spec, mntpt, "-F", NULL) != 0)
239 rval |= checkfs(type, spec, mntpt, NULL, NULL);
247 isok(struct fstab *fs)
251 if (fs->fs_passno == 0)
253 if (BADTYPE(fs->fs_type))
255 if (!selected(fs->fs_vfstype))
258 * If the -B flag has been given, then process the needed
259 * background checks. Background checks cannot be run on
260 * file systems that will be mounted read-only or that were
261 * not mounted at boot time (typically those marked `noauto').
262 * If these basic tests are passed, check with the file system
263 * itself to see if it is willing to do background checking
264 * by invoking its check program with the -F flag.
266 if (flags & DO_BACKGRD) {
267 if (!strcmp(fs->fs_type, FSTAB_RO))
269 if (getmntpt(fs->fs_spec) == NULL)
271 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
276 * If the -F flag has been given, then consider deferring the
277 * check to background. Background checks cannot be run on
278 * file systems that will be mounted read-only or that will
279 * not be mounted at boot time (e.g., marked `noauto'). If
280 * these basic tests are passed, check with the file system
281 * itself to see if it is willing to defer to background
282 * checking by invoking its check program with the -F flag.
284 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
286 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
287 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
291 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
293 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
299 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
300 const char *auxopt, pid_t *pidp)
302 const char ** volatile argv;
304 int argc, i, status, maxargc;
305 char *optbuf, execbase[MAXPATHLEN];
306 char *vfstype = NULL;
307 const char *extra = NULL;
310 /* Avoid vfork clobbering */
315 * We convert the vfstype to lowercase and any spaces to underscores
316 * to not confuse the issue
318 * XXX This is a kludge to make automatic filesystem type guessing
319 * from the disklabel work for "4.2BSD" filesystems. It does a
320 * very limited subset of transliteration to a normalised form of
321 * filesystem name, and we do not seem to enforce a filesystem
322 * name character set.
324 vfstype = strdup(pvfstype);
326 perr("strdup(pvfstype)");
327 for (i = 0; i < (int)strlen(vfstype); i++) {
328 vfstype[i] = tolower(vfstype[i]);
329 if (vfstype[i] == ' ')
333 extra = getoptions(vfstype);
336 catopt(&optbuf, options);
338 catopt(&optbuf, extra);
340 catopt(&optbuf, auxopt);
341 else if (flags & DO_BACKGRD)
342 catopt(&optbuf, "-B");
345 argv = emalloc(sizeof(char *) * maxargc);
347 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
349 argv[argc++] = execbase;
351 mangle(optbuf, &argc, &argv, &maxargc);
355 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
356 (void)printf("start %s %swait", mntpt,
358 for (i = 0; i < argc; i++)
359 (void)printf(" %s", argv[i]);
363 switch (pid = vfork()) {
364 case -1: /* Error. */
372 if ((flags & CHECK_DEBUG) && auxopt == NULL)
375 /* Go find an executable. */
376 execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv));
378 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
380 warn("exec %s in %s", execbase, _PATH_SYSPATH);
384 default: /* Parent. */
395 if (waitpid(pid, &status, 0) < 0) {
400 if (WIFEXITED(status)) {
401 if (WEXITSTATUS(status) != 0)
402 return (WEXITSTATUS(status));
404 else if (WIFSIGNALED(status)) {
405 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
416 selected(const char *type)
420 /* If no type specified, it's always selected. */
421 TAILQ_FOREACH(e, &selhead, entries)
422 if (!strncmp(e->type, type, MFSNAMELEN))
423 return which == IN_LIST ? 1 : 0;
425 return which == IN_LIST ? 0 : 1;
430 getoptions(const char *type)
434 TAILQ_FOREACH(e, &opthead, entries)
435 if (!strncmp(e->type, type, MFSNAMELEN))
442 addoption(char *optstr)
447 if ((newoptions = strchr(optstr, ':')) == NULL)
448 errx(1, "Invalid option string");
450 *newoptions++ = '\0';
452 TAILQ_FOREACH(e, &opthead, entries)
453 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
454 catopt(&e->options, newoptions);
457 addentry(&opthead, optstr, newoptions);
462 addentry(struct fstypelist *list, const char *type, const char *opts)
466 e = emalloc(sizeof(struct entry));
467 e->type = estrdup(type);
468 e->options = estrdup(opts);
469 TAILQ_INSERT_TAIL(list, e, entries);
474 maketypelist(char *fslist)
478 if ((fslist == NULL) || (fslist[0] == '\0'))
479 errx(1, "empty type list");
481 if (fslist[0] == 'n' && fslist[1] == 'o') {
488 while ((ptr = strsep(&fslist, ",")) != NULL)
489 addentry(&selhead, ptr, "");
495 catopt(char **sp, const char *o)
503 j = i + 1 + strlen(o) + 1;
505 (void)snprintf(s + i, j, ",%s", o);
513 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
523 for (s = opts; (p = strsep(&s, ",")) != NULL;) {
524 /* Always leave space for one more argument and the NULL. */
525 if (argc >= maxargc - 3) {
527 argv = erealloc(argv, maxargc * sizeof(char *));
550 getfstype(const char *str)
552 struct diocgattr_arg attr;
555 if ((fd = open(str, O_RDONLY)) == -1)
556 err(1, "cannot open `%s'", str);
558 strncpy(attr.name, "PART::type", sizeof(attr.name));
559 memset(&attr.value, 0, sizeof(attr.value));
560 attr.len = sizeof(attr.value);
561 if (ioctl(fd, DIOCGATTR, &attr) == -1) {
566 for (i = 0; ptype_map[i].ptype != NULL; i++)
567 if (strstr(attr.value.str, ptype_map[i].ptype) != NULL)
568 return (ptype_map[i].name);
576 static const char common[] =
577 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
579 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
580 getprogname(), common);