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