16cf357bcSUlrich Spörlein /* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $ */ 2da7e7114SAdrian Chadd 38a16b7a1SPedro F. Giffuni /*- 48a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 58a16b7a1SPedro F. Giffuni * 6da7e7114SAdrian Chadd * Copyright (c) 1996 Christos Zoulas. All rights reserved. 7da7e7114SAdrian Chadd * Copyright (c) 1980, 1989, 1993, 1994 8da7e7114SAdrian Chadd * The Regents of the University of California. All rights reserved. 9da7e7114SAdrian Chadd * 10da7e7114SAdrian Chadd * Redistribution and use in source and binary forms, with or without 11da7e7114SAdrian Chadd * modification, are permitted provided that the following conditions 12da7e7114SAdrian Chadd * are met: 13da7e7114SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 14da7e7114SAdrian Chadd * notice, this list of conditions and the following disclaimer. 15da7e7114SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright 16da7e7114SAdrian Chadd * notice, this list of conditions and the following disclaimer in the 17da7e7114SAdrian Chadd * documentation and/or other materials provided with the distribution. 186cf357bcSUlrich Spörlein * 3. Neither the name of the University nor the names of its contributors 19da7e7114SAdrian Chadd * may be used to endorse or promote products derived from this software 20da7e7114SAdrian Chadd * without specific prior written permission. 21da7e7114SAdrian Chadd * 22da7e7114SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23da7e7114SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24da7e7114SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25da7e7114SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26da7e7114SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27da7e7114SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28da7e7114SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29da7e7114SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30da7e7114SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31da7e7114SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32da7e7114SAdrian Chadd * SUCH DAMAGE. 33da7e7114SAdrian Chadd * 34da7e7114SAdrian Chadd * From: @(#)mount.c 8.19 (Berkeley) 4/19/94 35d4b552a9SDavid E. O'Brien * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 366cf357bcSUlrich Spörlein * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $ 37da7e7114SAdrian Chadd */ 38da7e7114SAdrian Chadd 39da7e7114SAdrian Chadd #include <sys/cdefs.h> 40b813a714SMark Murray __FBSDID("$FreeBSD$"); 41da7e7114SAdrian Chadd 42da7e7114SAdrian Chadd #include <sys/param.h> 43da7e7114SAdrian Chadd #include <sys/mount.h> 44da7e7114SAdrian Chadd #include <sys/queue.h> 45da7e7114SAdrian Chadd #include <sys/wait.h> 46009ad003SWarner Losh #include <sys/disk.h> 47da7e7114SAdrian Chadd #include <sys/ioctl.h> 48da7e7114SAdrian Chadd 49b813a714SMark Murray #include <ctype.h> 50da7e7114SAdrian Chadd #include <err.h> 51da7e7114SAdrian Chadd #include <fstab.h> 52da7e7114SAdrian Chadd #include <fcntl.h> 53*906c312bSKirk McKusick #include <mntopts.h> 54da7e7114SAdrian Chadd #include <paths.h> 55da7e7114SAdrian Chadd #include <signal.h> 56da7e7114SAdrian Chadd #include <stdio.h> 57da7e7114SAdrian Chadd #include <stdlib.h> 58da7e7114SAdrian Chadd #include <string.h> 59da7e7114SAdrian Chadd #include <unistd.h> 60da7e7114SAdrian Chadd 61da7e7114SAdrian Chadd #include "fsutil.h" 62da7e7114SAdrian Chadd 63da7e7114SAdrian Chadd static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 64da7e7114SAdrian Chadd 651efe3c6bSEd Schouten static TAILQ_HEAD(fstypelist, entry) opthead, selhead; 66da7e7114SAdrian Chadd 67da7e7114SAdrian Chadd struct entry { 68da7e7114SAdrian Chadd char *type; 69da7e7114SAdrian Chadd char *options; 70da7e7114SAdrian Chadd TAILQ_ENTRY(entry) entries; 71da7e7114SAdrian Chadd }; 72da7e7114SAdrian Chadd 73da7e7114SAdrian Chadd static char *options = NULL; 74da7e7114SAdrian Chadd static int flags = 0; 75a02a0079SKirk McKusick static int forceflag = 0; 76da7e7114SAdrian Chadd 773bbc4438SUlrich Spörlein static int checkfs(const char *, const char *, const char *, const char *, pid_t *); 78b70cd7eeSWarner Losh static int selected(const char *); 79b70cd7eeSWarner Losh static void addoption(char *); 80b70cd7eeSWarner Losh static const char *getoptions(const char *); 81b70cd7eeSWarner Losh static void addentry(struct fstypelist *, const char *, const char *); 82b70cd7eeSWarner Losh static void maketypelist(char *); 83b70cd7eeSWarner Losh static void catopt(char **, const char *); 843bbc4438SUlrich Spörlein static void mangle(char *, int *, const char ** volatile *, int *); 85009ad003SWarner Losh static const char *getfstype(const char *); 86b70cd7eeSWarner Losh static void usage(void) __dead2; 87b70cd7eeSWarner Losh static int isok(struct fstab *); 88da7e7114SAdrian Chadd 89009ad003SWarner Losh static struct { 90009ad003SWarner Losh const char *ptype; 91009ad003SWarner Losh const char *name; 92009ad003SWarner Losh } ptype_map[] = { 93009ad003SWarner Losh { "ufs", "ffs" }, 94009ad003SWarner Losh { "ffs", "ffs" }, 95009ad003SWarner Losh { "fat", "msdosfs" }, 96009ad003SWarner Losh { "efi", "msdosfs" }, 97009ad003SWarner Losh { NULL, NULL }, 98009ad003SWarner Losh }; 99009ad003SWarner Losh 100da7e7114SAdrian Chadd int 101b70cd7eeSWarner Losh main(int argc, char *argv[]) 102da7e7114SAdrian Chadd { 103da7e7114SAdrian Chadd struct fstab *fs; 104da7e7114SAdrian Chadd int i, rval = 0; 105da7e7114SAdrian Chadd const char *vfstype = NULL; 106da7e7114SAdrian Chadd char globopt[3]; 107f2104fc0SMaxim Sobolev const char *etc_fstab; 108da7e7114SAdrian Chadd 109da7e7114SAdrian Chadd globopt[0] = '-'; 110da7e7114SAdrian Chadd globopt[2] = '\0'; 111da7e7114SAdrian Chadd 112da7e7114SAdrian Chadd TAILQ_INIT(&selhead); 113da7e7114SAdrian Chadd TAILQ_INIT(&opthead); 114da7e7114SAdrian Chadd 115f2104fc0SMaxim Sobolev etc_fstab = NULL; 116f2104fc0SMaxim Sobolev while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1) 117da7e7114SAdrian Chadd switch (i) { 118a02a0079SKirk McKusick case 'B': 119a02a0079SKirk McKusick if (flags & CHECK_BACKGRD) 120a02a0079SKirk McKusick errx(1, "Cannot specify -B and -F."); 121a02a0079SKirk McKusick flags |= DO_BACKGRD; 122a02a0079SKirk McKusick break; 123a02a0079SKirk McKusick 124da7e7114SAdrian Chadd case 'd': 125da7e7114SAdrian Chadd flags |= CHECK_DEBUG; 126da7e7114SAdrian Chadd break; 127da7e7114SAdrian Chadd 128da7e7114SAdrian Chadd case 'v': 129da7e7114SAdrian Chadd flags |= CHECK_VERBOSE; 130da7e7114SAdrian Chadd break; 131da7e7114SAdrian Chadd 132a02a0079SKirk McKusick case 'F': 133a02a0079SKirk McKusick if (flags & DO_BACKGRD) 134a02a0079SKirk McKusick errx(1, "Cannot specify -B and -F."); 135a02a0079SKirk McKusick flags |= CHECK_BACKGRD; 136a02a0079SKirk McKusick break; 137a02a0079SKirk McKusick 138da7e7114SAdrian Chadd case 'p': 139da7e7114SAdrian Chadd flags |= CHECK_PREEN; 140da7e7114SAdrian Chadd /*FALLTHROUGH*/ 141111a5220SDavid E. O'Brien case 'C': 142111a5220SDavid E. O'Brien flags |= CHECK_CLEAN; 143111a5220SDavid E. O'Brien /*FALLTHROUGH*/ 144da7e7114SAdrian Chadd case 'n': 145da7e7114SAdrian Chadd case 'y': 146da7e7114SAdrian Chadd globopt[1] = i; 147da7e7114SAdrian Chadd catopt(&options, globopt); 148da7e7114SAdrian Chadd break; 149da7e7114SAdrian Chadd 150a02a0079SKirk McKusick case 'f': 151a02a0079SKirk McKusick forceflag = 1; 152a02a0079SKirk McKusick globopt[1] = i; 153a02a0079SKirk McKusick catopt(&options, globopt); 154a02a0079SKirk McKusick break; 155a02a0079SKirk McKusick 156da7e7114SAdrian Chadd case 'l': 1570af7bca2SPoul-Henning Kamp warnx("Ignoring obsolete -l option\n"); 158da7e7114SAdrian Chadd break; 159da7e7114SAdrian Chadd 160da7e7114SAdrian Chadd case 'T': 161da7e7114SAdrian Chadd if (*optarg) 162da7e7114SAdrian Chadd addoption(optarg); 163da7e7114SAdrian Chadd break; 164da7e7114SAdrian Chadd 165da7e7114SAdrian Chadd case 't': 16632ff2662SPoul-Henning Kamp if (!TAILQ_EMPTY(&selhead)) 167da7e7114SAdrian Chadd errx(1, "only one -t option may be specified."); 168da7e7114SAdrian Chadd 169da7e7114SAdrian Chadd maketypelist(optarg); 170da7e7114SAdrian Chadd vfstype = optarg; 171da7e7114SAdrian Chadd break; 172da7e7114SAdrian Chadd 173f2104fc0SMaxim Sobolev case 'c': 174f2104fc0SMaxim Sobolev etc_fstab = optarg; 175f2104fc0SMaxim Sobolev break; 176f2104fc0SMaxim Sobolev 177da7e7114SAdrian Chadd case '?': 178da7e7114SAdrian Chadd default: 179da7e7114SAdrian Chadd usage(); 180da7e7114SAdrian Chadd /* NOTREACHED */ 181da7e7114SAdrian Chadd } 182da7e7114SAdrian Chadd 183da7e7114SAdrian Chadd argc -= optind; 184da7e7114SAdrian Chadd argv += optind; 185da7e7114SAdrian Chadd 186f2104fc0SMaxim Sobolev if (etc_fstab != NULL) 187f2104fc0SMaxim Sobolev setfstab(etc_fstab); 188f2104fc0SMaxim Sobolev 189da7e7114SAdrian Chadd if (argc == 0) 1900af7bca2SPoul-Henning Kamp return checkfstab(flags, isok, checkfs); 191da7e7114SAdrian Chadd 192da7e7114SAdrian Chadd #define BADTYPE(type) \ 193da7e7114SAdrian Chadd (strcmp(type, FSTAB_RO) && \ 194da7e7114SAdrian Chadd strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 195da7e7114SAdrian Chadd 196da7e7114SAdrian Chadd 197da7e7114SAdrian Chadd for (; argc--; argv++) { 198a02a0079SKirk McKusick const char *spec, *mntpt, *type, *cp; 199da7e7114SAdrian Chadd char device[MAXPATHLEN]; 200a02a0079SKirk McKusick struct statfs *mntp; 201da7e7114SAdrian Chadd 2023bbc4438SUlrich Spörlein mntpt = NULL; 203da7e7114SAdrian Chadd spec = *argv; 204da7e7114SAdrian Chadd cp = strrchr(spec, '/'); 205cd5f6a0cSMarcelo Araujo if (cp == NULL) { 206da7e7114SAdrian Chadd (void)snprintf(device, sizeof(device), "%s%s", 207da7e7114SAdrian Chadd _PATH_DEV, spec); 208da7e7114SAdrian Chadd spec = device; 209da7e7114SAdrian Chadd } 210*906c312bSKirk McKusick mntp = getmntpoint(spec); 211a02a0079SKirk McKusick if (mntp != NULL) { 212a02a0079SKirk McKusick spec = mntp->f_mntfromname; 213a02a0079SKirk McKusick mntpt = mntp->f_mntonname; 214a02a0079SKirk McKusick } 215da7e7114SAdrian Chadd if ((fs = getfsfile(spec)) == NULL && 216da7e7114SAdrian Chadd (fs = getfsspec(spec)) == NULL) { 217da7e7114SAdrian Chadd if (vfstype == NULL) 218009ad003SWarner Losh vfstype = getfstype(spec); 2195c63c8ddSPoul-Henning Kamp if (vfstype == NULL) 2206a27a9f6SEdward Tomasz Napierala vfstype = "ufs"; 221da7e7114SAdrian Chadd type = vfstype; 222a02a0079SKirk McKusick devcheck(spec); 223a02a0079SKirk McKusick } else { 224da7e7114SAdrian Chadd spec = fs->fs_spec; 225da7e7114SAdrian Chadd type = fs->fs_vfstype; 226a02a0079SKirk McKusick mntpt = fs->fs_file; 227da7e7114SAdrian Chadd if (BADTYPE(fs->fs_type)) 228da7e7114SAdrian Chadd errx(1, "%s has unknown file system type.", 229da7e7114SAdrian Chadd spec); 230da7e7114SAdrian Chadd } 231a02a0079SKirk McKusick if ((flags & CHECK_BACKGRD) && 232a02a0079SKirk McKusick checkfs(type, spec, mntpt, "-F", NULL) == 0) { 233a02a0079SKirk McKusick printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv); 234a02a0079SKirk McKusick continue; 235a02a0079SKirk McKusick } 236a02a0079SKirk McKusick if ((flags & DO_BACKGRD) && forceflag == 0 && 237a02a0079SKirk McKusick checkfs(type, spec, mntpt, "-F", NULL) != 0) 238a02a0079SKirk McKusick continue; 239da7e7114SAdrian Chadd 240a02a0079SKirk McKusick rval |= checkfs(type, spec, mntpt, NULL, NULL); 241da7e7114SAdrian Chadd } 242da7e7114SAdrian Chadd 243da7e7114SAdrian Chadd return rval; 244da7e7114SAdrian Chadd } 245da7e7114SAdrian Chadd 246da7e7114SAdrian Chadd 247a02a0079SKirk McKusick static int 248b70cd7eeSWarner Losh isok(struct fstab *fs) 249da7e7114SAdrian Chadd { 250a02a0079SKirk McKusick int i; 251a02a0079SKirk McKusick 252da7e7114SAdrian Chadd if (fs->fs_passno == 0) 253a02a0079SKirk McKusick return (0); 254da7e7114SAdrian Chadd if (BADTYPE(fs->fs_type)) 255a02a0079SKirk McKusick return (0); 256da7e7114SAdrian Chadd if (!selected(fs->fs_vfstype)) 257a02a0079SKirk McKusick return (0); 258c72372c6SKirk McKusick /* If failok, always check now */ 259c72372c6SKirk McKusick if (getfsopt(fs, "failok")) 260c72372c6SKirk McKusick return (1); 261a02a0079SKirk McKusick /* 262a02a0079SKirk McKusick * If the -B flag has been given, then process the needed 263a02a0079SKirk McKusick * background checks. Background checks cannot be run on 264a02a0079SKirk McKusick * file systems that will be mounted read-only or that were 265a02a0079SKirk McKusick * not mounted at boot time (typically those marked `noauto'). 266a02a0079SKirk McKusick * If these basic tests are passed, check with the file system 267a02a0079SKirk McKusick * itself to see if it is willing to do background checking 268a02a0079SKirk McKusick * by invoking its check program with the -F flag. 269a02a0079SKirk McKusick */ 270a02a0079SKirk McKusick if (flags & DO_BACKGRD) { 271a02a0079SKirk McKusick if (!strcmp(fs->fs_type, FSTAB_RO)) 272a02a0079SKirk McKusick return (0); 273*906c312bSKirk McKusick if (getmntpoint(fs->fs_spec) == NULL) 274a02a0079SKirk McKusick return (0); 275a02a0079SKirk McKusick if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0)) 276a02a0079SKirk McKusick return (0); 277a02a0079SKirk McKusick return (1); 278a02a0079SKirk McKusick } 279a02a0079SKirk McKusick /* 280a02a0079SKirk McKusick * If the -F flag has been given, then consider deferring the 281a02a0079SKirk McKusick * check to background. Background checks cannot be run on 282a02a0079SKirk McKusick * file systems that will be mounted read-only or that will 283a02a0079SKirk McKusick * not be mounted at boot time (e.g., marked `noauto'). If 284a02a0079SKirk McKusick * these basic tests are passed, check with the file system 285a02a0079SKirk McKusick * itself to see if it is willing to defer to background 286a02a0079SKirk McKusick * checking by invoking its check program with the -F flag. 287a02a0079SKirk McKusick */ 288a02a0079SKirk McKusick if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO)) 289a02a0079SKirk McKusick return (1); 290a02a0079SKirk McKusick for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--) 291a02a0079SKirk McKusick if (!strncmp(&fs->fs_mntops[i], "noauto", 6)) 292a02a0079SKirk McKusick break; 293a02a0079SKirk McKusick if (i >= 0) 294a02a0079SKirk McKusick return (1); 295a02a0079SKirk McKusick if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0) 296a02a0079SKirk McKusick return (1); 297a02a0079SKirk McKusick printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec); 298a02a0079SKirk McKusick return (0); 299da7e7114SAdrian Chadd } 300da7e7114SAdrian Chadd 301da7e7114SAdrian Chadd 302da7e7114SAdrian Chadd static int 3038235d79aSJuli Mallett checkfs(const char *pvfstype, const char *spec, const char *mntpt, 3043bbc4438SUlrich Spörlein const char *auxopt, pid_t *pidp) 305da7e7114SAdrian Chadd { 3063bbc4438SUlrich Spörlein const char ** volatile argv; 307da7e7114SAdrian Chadd pid_t pid; 308da7e7114SAdrian Chadd int argc, i, status, maxargc; 309a3ba4c65SGordon Tetlow char *optbuf, execbase[MAXPATHLEN]; 3108235d79aSJuli Mallett char *vfstype = NULL; 311da7e7114SAdrian Chadd const char *extra = NULL; 312da7e7114SAdrian Chadd 313da7e7114SAdrian Chadd #ifdef __GNUC__ 314da7e7114SAdrian Chadd /* Avoid vfork clobbering */ 315da7e7114SAdrian Chadd (void) &optbuf; 316da7e7114SAdrian Chadd (void) &vfstype; 317da7e7114SAdrian Chadd #endif 3188235d79aSJuli Mallett /* 3198235d79aSJuli Mallett * We convert the vfstype to lowercase and any spaces to underscores 3208235d79aSJuli Mallett * to not confuse the issue 3218235d79aSJuli Mallett * 3228235d79aSJuli Mallett * XXX This is a kludge to make automatic filesystem type guessing 3238235d79aSJuli Mallett * from the disklabel work for "4.2BSD" filesystems. It does a 3248235d79aSJuli Mallett * very limited subset of transliteration to a normalised form of 3258235d79aSJuli Mallett * filesystem name, and we do not seem to enforce a filesystem 3268235d79aSJuli Mallett * name character set. 3278235d79aSJuli Mallett */ 3288235d79aSJuli Mallett vfstype = strdup(pvfstype); 3298235d79aSJuli Mallett if (vfstype == NULL) 3306cf357bcSUlrich Spörlein perr("strdup(pvfstype)"); 3313bbc4438SUlrich Spörlein for (i = 0; i < (int)strlen(vfstype); i++) { 3328235d79aSJuli Mallett vfstype[i] = tolower(vfstype[i]); 3338235d79aSJuli Mallett if (vfstype[i] == ' ') 3348235d79aSJuli Mallett vfstype[i] = '_'; 3358235d79aSJuli Mallett } 336da7e7114SAdrian Chadd 337da7e7114SAdrian Chadd extra = getoptions(vfstype); 338da7e7114SAdrian Chadd optbuf = NULL; 339da7e7114SAdrian Chadd if (options) 340da7e7114SAdrian Chadd catopt(&optbuf, options); 341da7e7114SAdrian Chadd if (extra) 342da7e7114SAdrian Chadd catopt(&optbuf, extra); 343a02a0079SKirk McKusick if (auxopt) 344a02a0079SKirk McKusick catopt(&optbuf, auxopt); 345a02a0079SKirk McKusick else if (flags & DO_BACKGRD) 346a02a0079SKirk McKusick catopt(&optbuf, "-B"); 347da7e7114SAdrian Chadd 348da7e7114SAdrian Chadd maxargc = 64; 349da7e7114SAdrian Chadd argv = emalloc(sizeof(char *) * maxargc); 350da7e7114SAdrian Chadd 351da7e7114SAdrian Chadd (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype); 352da7e7114SAdrian Chadd argc = 0; 353da7e7114SAdrian Chadd argv[argc++] = execbase; 354da7e7114SAdrian Chadd if (optbuf) 355da7e7114SAdrian Chadd mangle(optbuf, &argc, &argv, &maxargc); 356da7e7114SAdrian Chadd argv[argc++] = spec; 357da7e7114SAdrian Chadd argv[argc] = NULL; 358da7e7114SAdrian Chadd 359da7e7114SAdrian Chadd if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 360da7e7114SAdrian Chadd (void)printf("start %s %swait", mntpt, 361da7e7114SAdrian Chadd pidp ? "no" : ""); 362da7e7114SAdrian Chadd for (i = 0; i < argc; i++) 363da7e7114SAdrian Chadd (void)printf(" %s", argv[i]); 364da7e7114SAdrian Chadd (void)printf("\n"); 365da7e7114SAdrian Chadd } 366da7e7114SAdrian Chadd 367da7e7114SAdrian Chadd switch (pid = vfork()) { 368da7e7114SAdrian Chadd case -1: /* Error. */ 369da7e7114SAdrian Chadd warn("vfork"); 370da7e7114SAdrian Chadd if (optbuf) 371da7e7114SAdrian Chadd free(optbuf); 3728235d79aSJuli Mallett free(vfstype); 373da7e7114SAdrian Chadd return (1); 374da7e7114SAdrian Chadd 375da7e7114SAdrian Chadd case 0: /* Child. */ 376a02a0079SKirk McKusick if ((flags & CHECK_DEBUG) && auxopt == NULL) 377da7e7114SAdrian Chadd _exit(0); 378da7e7114SAdrian Chadd 379da7e7114SAdrian Chadd /* Go find an executable. */ 3803bbc4438SUlrich Spörlein execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv)); 381da7e7114SAdrian Chadd if (spec) 382a3ba4c65SGordon Tetlow warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH); 383da7e7114SAdrian Chadd else 384a3ba4c65SGordon Tetlow warn("exec %s in %s", execbase, _PATH_SYSPATH); 385da7e7114SAdrian Chadd _exit(1); 386da7e7114SAdrian Chadd /* NOTREACHED */ 387da7e7114SAdrian Chadd 388da7e7114SAdrian Chadd default: /* Parent. */ 389da7e7114SAdrian Chadd if (optbuf) 390da7e7114SAdrian Chadd free(optbuf); 391da7e7114SAdrian Chadd 3928235d79aSJuli Mallett free(vfstype); 3938235d79aSJuli Mallett 394da7e7114SAdrian Chadd if (pidp) { 395da7e7114SAdrian Chadd *pidp = pid; 396da7e7114SAdrian Chadd return 0; 397da7e7114SAdrian Chadd } 398da7e7114SAdrian Chadd 399da7e7114SAdrian Chadd if (waitpid(pid, &status, 0) < 0) { 400da7e7114SAdrian Chadd warn("waitpid"); 401da7e7114SAdrian Chadd return (1); 402da7e7114SAdrian Chadd } 403da7e7114SAdrian Chadd 404da7e7114SAdrian Chadd if (WIFEXITED(status)) { 405da7e7114SAdrian Chadd if (WEXITSTATUS(status) != 0) 406da7e7114SAdrian Chadd return (WEXITSTATUS(status)); 407da7e7114SAdrian Chadd } 408da7e7114SAdrian Chadd else if (WIFSIGNALED(status)) { 409da7e7114SAdrian Chadd warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 410da7e7114SAdrian Chadd return (1); 411da7e7114SAdrian Chadd } 412da7e7114SAdrian Chadd break; 413da7e7114SAdrian Chadd } 414da7e7114SAdrian Chadd 415da7e7114SAdrian Chadd return (0); 416da7e7114SAdrian Chadd } 417da7e7114SAdrian Chadd 418da7e7114SAdrian Chadd 419da7e7114SAdrian Chadd static int 420b70cd7eeSWarner Losh selected(const char *type) 421da7e7114SAdrian Chadd { 422da7e7114SAdrian Chadd struct entry *e; 423da7e7114SAdrian Chadd 424da7e7114SAdrian Chadd /* If no type specified, it's always selected. */ 42532ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &selhead, entries) 426da7e7114SAdrian Chadd if (!strncmp(e->type, type, MFSNAMELEN)) 427da7e7114SAdrian Chadd return which == IN_LIST ? 1 : 0; 428da7e7114SAdrian Chadd 429da7e7114SAdrian Chadd return which == IN_LIST ? 0 : 1; 430da7e7114SAdrian Chadd } 431da7e7114SAdrian Chadd 432da7e7114SAdrian Chadd 433da7e7114SAdrian Chadd static const char * 434b70cd7eeSWarner Losh getoptions(const char *type) 435da7e7114SAdrian Chadd { 436da7e7114SAdrian Chadd struct entry *e; 437da7e7114SAdrian Chadd 43832ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &opthead, entries) 439da7e7114SAdrian Chadd if (!strncmp(e->type, type, MFSNAMELEN)) 440da7e7114SAdrian Chadd return e->options; 441da7e7114SAdrian Chadd return ""; 442da7e7114SAdrian Chadd } 443da7e7114SAdrian Chadd 444da7e7114SAdrian Chadd 445da7e7114SAdrian Chadd static void 446b70cd7eeSWarner Losh addoption(char *optstr) 447da7e7114SAdrian Chadd { 448da7e7114SAdrian Chadd char *newoptions; 449da7e7114SAdrian Chadd struct entry *e; 450da7e7114SAdrian Chadd 451da7e7114SAdrian Chadd if ((newoptions = strchr(optstr, ':')) == NULL) 452da7e7114SAdrian Chadd errx(1, "Invalid option string"); 453da7e7114SAdrian Chadd 454da7e7114SAdrian Chadd *newoptions++ = '\0'; 455da7e7114SAdrian Chadd 45632ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &opthead, entries) 457da7e7114SAdrian Chadd if (!strncmp(e->type, optstr, MFSNAMELEN)) { 458da7e7114SAdrian Chadd catopt(&e->options, newoptions); 459da7e7114SAdrian Chadd return; 460da7e7114SAdrian Chadd } 461da7e7114SAdrian Chadd addentry(&opthead, optstr, newoptions); 462da7e7114SAdrian Chadd } 463da7e7114SAdrian Chadd 464da7e7114SAdrian Chadd 465da7e7114SAdrian Chadd static void 466b70cd7eeSWarner Losh addentry(struct fstypelist *list, const char *type, const char *opts) 467da7e7114SAdrian Chadd { 468da7e7114SAdrian Chadd struct entry *e; 469da7e7114SAdrian Chadd 470da7e7114SAdrian Chadd e = emalloc(sizeof(struct entry)); 471da7e7114SAdrian Chadd e->type = estrdup(type); 472da7e7114SAdrian Chadd e->options = estrdup(opts); 473da7e7114SAdrian Chadd TAILQ_INSERT_TAIL(list, e, entries); 474da7e7114SAdrian Chadd } 475da7e7114SAdrian Chadd 476da7e7114SAdrian Chadd 477da7e7114SAdrian Chadd static void 478b70cd7eeSWarner Losh maketypelist(char *fslist) 479da7e7114SAdrian Chadd { 480da7e7114SAdrian Chadd char *ptr; 481da7e7114SAdrian Chadd 482da7e7114SAdrian Chadd if ((fslist == NULL) || (fslist[0] == '\0')) 483da7e7114SAdrian Chadd errx(1, "empty type list"); 484da7e7114SAdrian Chadd 485da7e7114SAdrian Chadd if (fslist[0] == 'n' && fslist[1] == 'o') { 486da7e7114SAdrian Chadd fslist += 2; 487da7e7114SAdrian Chadd which = NOT_IN_LIST; 488da7e7114SAdrian Chadd } 489da7e7114SAdrian Chadd else 490da7e7114SAdrian Chadd which = IN_LIST; 491da7e7114SAdrian Chadd 492da7e7114SAdrian Chadd while ((ptr = strsep(&fslist, ",")) != NULL) 493da7e7114SAdrian Chadd addentry(&selhead, ptr, ""); 494da7e7114SAdrian Chadd 495da7e7114SAdrian Chadd } 496da7e7114SAdrian Chadd 497da7e7114SAdrian Chadd 498da7e7114SAdrian Chadd static void 499b70cd7eeSWarner Losh catopt(char **sp, const char *o) 500da7e7114SAdrian Chadd { 501da7e7114SAdrian Chadd char *s; 502da7e7114SAdrian Chadd size_t i, j; 503da7e7114SAdrian Chadd 504da7e7114SAdrian Chadd s = *sp; 505da7e7114SAdrian Chadd if (s) { 506da7e7114SAdrian Chadd i = strlen(s); 507da7e7114SAdrian Chadd j = i + 1 + strlen(o) + 1; 508da7e7114SAdrian Chadd s = erealloc(s, j); 509da7e7114SAdrian Chadd (void)snprintf(s + i, j, ",%s", o); 510da7e7114SAdrian Chadd } else 511da7e7114SAdrian Chadd s = estrdup(o); 512da7e7114SAdrian Chadd *sp = s; 513da7e7114SAdrian Chadd } 514da7e7114SAdrian Chadd 515da7e7114SAdrian Chadd 516da7e7114SAdrian Chadd static void 5173bbc4438SUlrich Spörlein mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp) 518da7e7114SAdrian Chadd { 519da7e7114SAdrian Chadd char *p, *s; 520da7e7114SAdrian Chadd int argc, maxargc; 521da7e7114SAdrian Chadd const char **argv; 522da7e7114SAdrian Chadd 523da7e7114SAdrian Chadd argc = *argcp; 524da7e7114SAdrian Chadd argv = *argvp; 525da7e7114SAdrian Chadd maxargc = *maxargcp; 526da7e7114SAdrian Chadd 5276cf357bcSUlrich Spörlein for (s = opts; (p = strsep(&s, ",")) != NULL;) { 528da7e7114SAdrian Chadd /* Always leave space for one more argument and the NULL. */ 529da7e7114SAdrian Chadd if (argc >= maxargc - 3) { 530da7e7114SAdrian Chadd maxargc <<= 1; 531da7e7114SAdrian Chadd argv = erealloc(argv, maxargc * sizeof(char *)); 532da7e7114SAdrian Chadd } 533da7e7114SAdrian Chadd if (*p != '\0') { 534da7e7114SAdrian Chadd if (*p == '-') { 535da7e7114SAdrian Chadd argv[argc++] = p; 536da7e7114SAdrian Chadd p = strchr(p, '='); 537da7e7114SAdrian Chadd if (p) { 538da7e7114SAdrian Chadd *p = '\0'; 539da7e7114SAdrian Chadd argv[argc++] = p+1; 540da7e7114SAdrian Chadd } 541da7e7114SAdrian Chadd } else { 542da7e7114SAdrian Chadd argv[argc++] = "-o"; 543da7e7114SAdrian Chadd argv[argc++] = p; 544da7e7114SAdrian Chadd } 545da7e7114SAdrian Chadd } 546da7e7114SAdrian Chadd } 547da7e7114SAdrian Chadd 548da7e7114SAdrian Chadd *argcp = argc; 549da7e7114SAdrian Chadd *argvp = argv; 550da7e7114SAdrian Chadd *maxargcp = maxargc; 551da7e7114SAdrian Chadd } 552da7e7114SAdrian Chadd 553adafc9d6SWarner Losh static const char * 554009ad003SWarner Losh getfstype(const char *str) 555adafc9d6SWarner Losh { 556009ad003SWarner Losh struct diocgattr_arg attr; 557009ad003SWarner Losh int fd, i; 558adafc9d6SWarner Losh 559da7e7114SAdrian Chadd if ((fd = open(str, O_RDONLY)) == -1) 560da7e7114SAdrian Chadd err(1, "cannot open `%s'", str); 561da7e7114SAdrian Chadd 562009ad003SWarner Losh strncpy(attr.name, "PART::type", sizeof(attr.name)); 563009ad003SWarner Losh memset(&attr.value, 0, sizeof(attr.value)); 564009ad003SWarner Losh attr.len = sizeof(attr.value); 565009ad003SWarner Losh if (ioctl(fd, DIOCGATTR, &attr) == -1) { 5664b2d15efSAlexander Leidinger (void) close(fd); 5675c63c8ddSPoul-Henning Kamp return(NULL); 5684b2d15efSAlexander Leidinger } 569da7e7114SAdrian Chadd (void) close(fd); 570009ad003SWarner Losh for (i = 0; ptype_map[i].ptype != NULL; i++) 571009ad003SWarner Losh if (strstr(attr.value.str, ptype_map[i].ptype) != NULL) 572009ad003SWarner Losh return (ptype_map[i].name); 573009ad003SWarner Losh return (NULL); 574da7e7114SAdrian Chadd } 575da7e7114SAdrian Chadd 576da7e7114SAdrian Chadd 577da7e7114SAdrian Chadd static void 578b70cd7eeSWarner Losh usage(void) 579da7e7114SAdrian Chadd { 580da7e7114SAdrian Chadd static const char common[] = 581f2104fc0SMaxim Sobolev "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]"; 582da7e7114SAdrian Chadd 583d3974088SDag-Erling Smørgrav (void)fprintf(stderr, "usage: %s %s [special | node] ...\n", 584b813a714SMark Murray getprogname(), common); 585da7e7114SAdrian Chadd exit(1); 586da7e7114SAdrian Chadd } 587