1da7e7114SAdrian Chadd /* $NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs 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. 16da7e7114SAdrian Chadd * 3. All advertising materials mentioning features or use of this software 17da7e7114SAdrian Chadd * must display the following acknowledgement: 18da7e7114SAdrian Chadd * This product includes software developed by the University of 19da7e7114SAdrian Chadd * California, Berkeley and its contributors. 20da7e7114SAdrian Chadd * 4. Neither the name of the University nor the names of its contributors 21da7e7114SAdrian Chadd * may be used to endorse or promote products derived from this software 22da7e7114SAdrian Chadd * without specific prior written permission. 23da7e7114SAdrian Chadd * 24da7e7114SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25da7e7114SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26da7e7114SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27da7e7114SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28da7e7114SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29da7e7114SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30da7e7114SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31da7e7114SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32da7e7114SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33da7e7114SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34da7e7114SAdrian Chadd * SUCH DAMAGE. 35da7e7114SAdrian Chadd * 36da7e7114SAdrian Chadd * From: @(#)mount.c 8.19 (Berkeley) 4/19/94 37d4b552a9SDavid E. O'Brien * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 38d4b552a9SDavid E. O'Brien * $NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs Exp $ 39da7e7114SAdrian Chadd */ 40da7e7114SAdrian Chadd 41da7e7114SAdrian Chadd #include <sys/cdefs.h> 42b813a714SMark Murray __FBSDID("$FreeBSD$"); 43da7e7114SAdrian Chadd 44da7e7114SAdrian Chadd #include <sys/param.h> 45da7e7114SAdrian Chadd #include <sys/mount.h> 46da7e7114SAdrian Chadd #include <sys/queue.h> 47da7e7114SAdrian Chadd #include <sys/wait.h> 48da7e7114SAdrian Chadd #define FSTYPENAMES 49da7e7114SAdrian Chadd #include <sys/disklabel.h> 50da7e7114SAdrian Chadd #include <sys/ioctl.h> 51da7e7114SAdrian Chadd 52b813a714SMark Murray #include <ctype.h> 53da7e7114SAdrian Chadd #include <err.h> 54da7e7114SAdrian Chadd #include <errno.h> 55da7e7114SAdrian Chadd #include <fstab.h> 56da7e7114SAdrian Chadd #include <fcntl.h> 57da7e7114SAdrian Chadd #include <paths.h> 58da7e7114SAdrian Chadd #include <signal.h> 59da7e7114SAdrian Chadd #include <stdio.h> 60da7e7114SAdrian Chadd #include <stdlib.h> 61da7e7114SAdrian Chadd #include <string.h> 62da7e7114SAdrian Chadd #include <unistd.h> 63da7e7114SAdrian Chadd 64da7e7114SAdrian Chadd #include "fsutil.h" 65da7e7114SAdrian Chadd 66da7e7114SAdrian Chadd static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 67da7e7114SAdrian Chadd 68da7e7114SAdrian Chadd TAILQ_HEAD(fstypelist, entry) opthead, selhead; 69da7e7114SAdrian Chadd 70da7e7114SAdrian Chadd struct entry { 71da7e7114SAdrian Chadd char *type; 72da7e7114SAdrian Chadd char *options; 73da7e7114SAdrian Chadd TAILQ_ENTRY(entry) entries; 74da7e7114SAdrian Chadd }; 75da7e7114SAdrian Chadd 76da7e7114SAdrian Chadd static char *options = NULL; 77da7e7114SAdrian Chadd static int flags = 0; 78a02a0079SKirk McKusick static int forceflag = 0; 79da7e7114SAdrian Chadd 80b70cd7eeSWarner Losh static int checkfs(const char *, const char *, const char *, char *, pid_t *); 81b70cd7eeSWarner Losh static int selected(const char *); 82b70cd7eeSWarner Losh static void addoption(char *); 83b70cd7eeSWarner Losh static const char *getoptions(const char *); 84b70cd7eeSWarner Losh static void addentry(struct fstypelist *, const char *, const char *); 85b70cd7eeSWarner Losh static void maketypelist(char *); 86b70cd7eeSWarner Losh static void catopt(char **, const char *); 87b70cd7eeSWarner Losh static void mangle(char *, int *, const char ***, int *); 88b70cd7eeSWarner Losh static const char *getfslab(const char *); 89b70cd7eeSWarner Losh static void usage(void) __dead2; 90b70cd7eeSWarner Losh static int isok(struct fstab *); 91da7e7114SAdrian Chadd 92da7e7114SAdrian Chadd int 93b70cd7eeSWarner Losh main(int argc, char *argv[]) 94da7e7114SAdrian Chadd { 95da7e7114SAdrian Chadd struct fstab *fs; 96da7e7114SAdrian Chadd int i, rval = 0; 97da7e7114SAdrian Chadd const char *vfstype = NULL; 98da7e7114SAdrian Chadd char globopt[3]; 99da7e7114SAdrian Chadd 100da7e7114SAdrian Chadd globopt[0] = '-'; 101da7e7114SAdrian Chadd globopt[2] = '\0'; 102da7e7114SAdrian Chadd 103da7e7114SAdrian Chadd TAILQ_INIT(&selhead); 104da7e7114SAdrian Chadd TAILQ_INIT(&opthead); 105da7e7114SAdrian Chadd 106111a5220SDavid E. O'Brien while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:")) != -1) 107da7e7114SAdrian Chadd switch (i) { 108a02a0079SKirk McKusick case 'B': 109a02a0079SKirk McKusick if (flags & CHECK_BACKGRD) 110a02a0079SKirk McKusick errx(1, "Cannot specify -B and -F."); 111a02a0079SKirk McKusick flags |= DO_BACKGRD; 112a02a0079SKirk McKusick break; 113a02a0079SKirk McKusick 114da7e7114SAdrian Chadd case 'd': 115da7e7114SAdrian Chadd flags |= CHECK_DEBUG; 116da7e7114SAdrian Chadd break; 117da7e7114SAdrian Chadd 118da7e7114SAdrian Chadd case 'v': 119da7e7114SAdrian Chadd flags |= CHECK_VERBOSE; 120da7e7114SAdrian Chadd break; 121da7e7114SAdrian Chadd 122a02a0079SKirk McKusick case 'F': 123a02a0079SKirk McKusick if (flags & DO_BACKGRD) 124a02a0079SKirk McKusick errx(1, "Cannot specify -B and -F."); 125a02a0079SKirk McKusick flags |= CHECK_BACKGRD; 126a02a0079SKirk McKusick break; 127a02a0079SKirk McKusick 128da7e7114SAdrian Chadd case 'p': 129da7e7114SAdrian Chadd flags |= CHECK_PREEN; 130da7e7114SAdrian Chadd /*FALLTHROUGH*/ 131111a5220SDavid E. O'Brien case 'C': 132111a5220SDavid E. O'Brien flags |= CHECK_CLEAN; 133111a5220SDavid E. O'Brien /*FALLTHROUGH*/ 134da7e7114SAdrian Chadd case 'n': 135da7e7114SAdrian Chadd case 'y': 136da7e7114SAdrian Chadd globopt[1] = i; 137da7e7114SAdrian Chadd catopt(&options, globopt); 138da7e7114SAdrian Chadd break; 139da7e7114SAdrian Chadd 140a02a0079SKirk McKusick case 'f': 141a02a0079SKirk McKusick forceflag = 1; 142a02a0079SKirk McKusick globopt[1] = i; 143a02a0079SKirk McKusick catopt(&options, globopt); 144a02a0079SKirk McKusick break; 145a02a0079SKirk McKusick 146da7e7114SAdrian Chadd case 'l': 1470af7bca2SPoul-Henning Kamp warnx("Ignoring obsolete -l option\n"); 148da7e7114SAdrian Chadd break; 149da7e7114SAdrian Chadd 150da7e7114SAdrian Chadd case 'T': 151da7e7114SAdrian Chadd if (*optarg) 152da7e7114SAdrian Chadd addoption(optarg); 153da7e7114SAdrian Chadd break; 154da7e7114SAdrian Chadd 155da7e7114SAdrian Chadd case 't': 15632ff2662SPoul-Henning Kamp if (!TAILQ_EMPTY(&selhead)) 157da7e7114SAdrian Chadd errx(1, "only one -t option may be specified."); 158da7e7114SAdrian Chadd 159da7e7114SAdrian Chadd maketypelist(optarg); 160da7e7114SAdrian Chadd vfstype = optarg; 161da7e7114SAdrian Chadd break; 162da7e7114SAdrian Chadd 163da7e7114SAdrian Chadd case '?': 164da7e7114SAdrian Chadd default: 165da7e7114SAdrian Chadd usage(); 166da7e7114SAdrian Chadd /* NOTREACHED */ 167da7e7114SAdrian Chadd } 168da7e7114SAdrian Chadd 169da7e7114SAdrian Chadd argc -= optind; 170da7e7114SAdrian Chadd argv += optind; 171da7e7114SAdrian Chadd 172da7e7114SAdrian Chadd if (argc == 0) 1730af7bca2SPoul-Henning Kamp return checkfstab(flags, isok, checkfs); 174da7e7114SAdrian Chadd 175da7e7114SAdrian Chadd #define BADTYPE(type) \ 176da7e7114SAdrian Chadd (strcmp(type, FSTAB_RO) && \ 177da7e7114SAdrian Chadd strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 178da7e7114SAdrian Chadd 179da7e7114SAdrian Chadd 180da7e7114SAdrian Chadd for (; argc--; argv++) { 181a02a0079SKirk McKusick const char *spec, *mntpt, *type, *cp; 182da7e7114SAdrian Chadd char device[MAXPATHLEN]; 183a02a0079SKirk McKusick struct statfs *mntp; 184da7e7114SAdrian Chadd 185da7e7114SAdrian Chadd spec = *argv; 186da7e7114SAdrian Chadd cp = strrchr(spec, '/'); 187da7e7114SAdrian Chadd if (cp == 0) { 188da7e7114SAdrian Chadd (void)snprintf(device, sizeof(device), "%s%s", 189da7e7114SAdrian Chadd _PATH_DEV, spec); 190da7e7114SAdrian Chadd spec = device; 191da7e7114SAdrian Chadd } 192a02a0079SKirk McKusick mntp = getmntpt(spec); 193a02a0079SKirk McKusick if (mntp != NULL) { 194a02a0079SKirk McKusick spec = mntp->f_mntfromname; 195a02a0079SKirk McKusick mntpt = mntp->f_mntonname; 196a02a0079SKirk McKusick } 197da7e7114SAdrian Chadd if ((fs = getfsfile(spec)) == NULL && 198da7e7114SAdrian Chadd (fs = getfsspec(spec)) == NULL) { 199da7e7114SAdrian Chadd if (vfstype == NULL) 200da7e7114SAdrian Chadd vfstype = getfslab(spec); 2015c63c8ddSPoul-Henning Kamp if (vfstype == NULL) 2025c63c8ddSPoul-Henning Kamp errx(1, "Could not determine filesystem type"); 203da7e7114SAdrian Chadd type = vfstype; 204a02a0079SKirk McKusick devcheck(spec); 205a02a0079SKirk McKusick } else { 206da7e7114SAdrian Chadd spec = fs->fs_spec; 207da7e7114SAdrian Chadd type = fs->fs_vfstype; 208a02a0079SKirk McKusick mntpt = fs->fs_file; 209da7e7114SAdrian Chadd if (BADTYPE(fs->fs_type)) 210da7e7114SAdrian Chadd errx(1, "%s has unknown file system type.", 211da7e7114SAdrian Chadd spec); 212da7e7114SAdrian Chadd } 213a02a0079SKirk McKusick if ((flags & CHECK_BACKGRD) && 214a02a0079SKirk McKusick checkfs(type, spec, mntpt, "-F", NULL) == 0) { 215a02a0079SKirk McKusick printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv); 216a02a0079SKirk McKusick continue; 217a02a0079SKirk McKusick } 218a02a0079SKirk McKusick if ((flags & DO_BACKGRD) && forceflag == 0 && 219a02a0079SKirk McKusick checkfs(type, spec, mntpt, "-F", NULL) != 0) 220a02a0079SKirk McKusick continue; 221da7e7114SAdrian Chadd 222a02a0079SKirk McKusick rval |= checkfs(type, spec, mntpt, NULL, NULL); 223da7e7114SAdrian Chadd } 224da7e7114SAdrian Chadd 225da7e7114SAdrian Chadd return rval; 226da7e7114SAdrian Chadd } 227da7e7114SAdrian Chadd 228da7e7114SAdrian Chadd 229a02a0079SKirk McKusick static int 230b70cd7eeSWarner Losh isok(struct fstab *fs) 231da7e7114SAdrian Chadd { 232a02a0079SKirk McKusick int i; 233a02a0079SKirk McKusick 234da7e7114SAdrian Chadd if (fs->fs_passno == 0) 235a02a0079SKirk McKusick return (0); 236da7e7114SAdrian Chadd if (BADTYPE(fs->fs_type)) 237a02a0079SKirk McKusick return (0); 238da7e7114SAdrian Chadd if (!selected(fs->fs_vfstype)) 239a02a0079SKirk McKusick return (0); 240a02a0079SKirk McKusick /* 241a02a0079SKirk McKusick * If the -B flag has been given, then process the needed 242a02a0079SKirk McKusick * background checks. Background checks cannot be run on 243a02a0079SKirk McKusick * file systems that will be mounted read-only or that were 244a02a0079SKirk McKusick * not mounted at boot time (typically those marked `noauto'). 245a02a0079SKirk McKusick * If these basic tests are passed, check with the file system 246a02a0079SKirk McKusick * itself to see if it is willing to do background checking 247a02a0079SKirk McKusick * by invoking its check program with the -F flag. 248a02a0079SKirk McKusick */ 249a02a0079SKirk McKusick if (flags & DO_BACKGRD) { 250a02a0079SKirk McKusick if (!strcmp(fs->fs_type, FSTAB_RO)) 251a02a0079SKirk McKusick return (0); 252a02a0079SKirk McKusick if (getmntpt(fs->fs_spec) == NULL) 253a02a0079SKirk McKusick return (0); 254a02a0079SKirk McKusick if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0)) 255a02a0079SKirk McKusick return (0); 256a02a0079SKirk McKusick return (1); 257a02a0079SKirk McKusick } 258a02a0079SKirk McKusick /* 259a02a0079SKirk McKusick * If the -F flag has been given, then consider deferring the 260a02a0079SKirk McKusick * check to background. Background checks cannot be run on 261a02a0079SKirk McKusick * file systems that will be mounted read-only or that will 262a02a0079SKirk McKusick * not be mounted at boot time (e.g., marked `noauto'). If 263a02a0079SKirk McKusick * these basic tests are passed, check with the file system 264a02a0079SKirk McKusick * itself to see if it is willing to defer to background 265a02a0079SKirk McKusick * checking by invoking its check program with the -F flag. 266a02a0079SKirk McKusick */ 267a02a0079SKirk McKusick if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO)) 268a02a0079SKirk McKusick return (1); 269a02a0079SKirk McKusick for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--) 270a02a0079SKirk McKusick if (!strncmp(&fs->fs_mntops[i], "noauto", 6)) 271a02a0079SKirk McKusick break; 272a02a0079SKirk McKusick if (i >= 0) 273a02a0079SKirk McKusick return (1); 274a02a0079SKirk McKusick if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0) 275a02a0079SKirk McKusick return (1); 276a02a0079SKirk McKusick printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec); 277a02a0079SKirk McKusick return (0); 278da7e7114SAdrian Chadd } 279da7e7114SAdrian Chadd 280da7e7114SAdrian Chadd 281da7e7114SAdrian Chadd static int 2828235d79aSJuli Mallett checkfs(const char *pvfstype, const char *spec, const char *mntpt, 283b70cd7eeSWarner Losh char *auxopt, pid_t *pidp) 284da7e7114SAdrian Chadd { 285a3ba4c65SGordon Tetlow const char **argv; 286da7e7114SAdrian Chadd pid_t pid; 287da7e7114SAdrian Chadd int argc, i, status, maxargc; 288a3ba4c65SGordon Tetlow char *optbuf, execbase[MAXPATHLEN]; 2898235d79aSJuli Mallett char *vfstype = NULL; 290da7e7114SAdrian Chadd const char *extra = NULL; 291da7e7114SAdrian Chadd 292da7e7114SAdrian Chadd #ifdef __GNUC__ 293da7e7114SAdrian Chadd /* Avoid vfork clobbering */ 294da7e7114SAdrian Chadd (void) &optbuf; 295da7e7114SAdrian Chadd (void) &vfstype; 296da7e7114SAdrian Chadd #endif 2978235d79aSJuli Mallett /* 2988235d79aSJuli Mallett * We convert the vfstype to lowercase and any spaces to underscores 2998235d79aSJuli Mallett * to not confuse the issue 3008235d79aSJuli Mallett * 3018235d79aSJuli Mallett * XXX This is a kludge to make automatic filesystem type guessing 3028235d79aSJuli Mallett * from the disklabel work for "4.2BSD" filesystems. It does a 3038235d79aSJuli Mallett * very limited subset of transliteration to a normalised form of 3048235d79aSJuli Mallett * filesystem name, and we do not seem to enforce a filesystem 3058235d79aSJuli Mallett * name character set. 3068235d79aSJuli Mallett */ 3078235d79aSJuli Mallett vfstype = strdup(pvfstype); 3088235d79aSJuli Mallett if (vfstype == NULL) 3098235d79aSJuli Mallett perror("strdup(pvfstype)"); 3108235d79aSJuli Mallett for (i = 0; i < strlen(vfstype); i++) { 3118235d79aSJuli Mallett vfstype[i] = tolower(vfstype[i]); 3128235d79aSJuli Mallett if (vfstype[i] == ' ') 3138235d79aSJuli Mallett vfstype[i] = '_'; 3148235d79aSJuli Mallett } 315da7e7114SAdrian Chadd 316da7e7114SAdrian Chadd extra = getoptions(vfstype); 317da7e7114SAdrian Chadd optbuf = NULL; 318da7e7114SAdrian Chadd if (options) 319da7e7114SAdrian Chadd catopt(&optbuf, options); 320da7e7114SAdrian Chadd if (extra) 321da7e7114SAdrian Chadd catopt(&optbuf, extra); 322a02a0079SKirk McKusick if (auxopt) 323a02a0079SKirk McKusick catopt(&optbuf, auxopt); 324a02a0079SKirk McKusick else if (flags & DO_BACKGRD) 325a02a0079SKirk McKusick catopt(&optbuf, "-B"); 326da7e7114SAdrian Chadd 327da7e7114SAdrian Chadd maxargc = 64; 328da7e7114SAdrian Chadd argv = emalloc(sizeof(char *) * maxargc); 329da7e7114SAdrian Chadd 330da7e7114SAdrian Chadd (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype); 331da7e7114SAdrian Chadd argc = 0; 332da7e7114SAdrian Chadd argv[argc++] = execbase; 333da7e7114SAdrian Chadd if (optbuf) 334da7e7114SAdrian Chadd mangle(optbuf, &argc, &argv, &maxargc); 335da7e7114SAdrian Chadd argv[argc++] = spec; 336da7e7114SAdrian Chadd argv[argc] = NULL; 337da7e7114SAdrian Chadd 338da7e7114SAdrian Chadd if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 339da7e7114SAdrian Chadd (void)printf("start %s %swait", mntpt, 340da7e7114SAdrian Chadd pidp ? "no" : ""); 341da7e7114SAdrian Chadd for (i = 0; i < argc; i++) 342da7e7114SAdrian Chadd (void)printf(" %s", argv[i]); 343da7e7114SAdrian Chadd (void)printf("\n"); 344da7e7114SAdrian Chadd } 345da7e7114SAdrian Chadd 346da7e7114SAdrian Chadd switch (pid = vfork()) { 347da7e7114SAdrian Chadd case -1: /* Error. */ 348da7e7114SAdrian Chadd warn("vfork"); 349da7e7114SAdrian Chadd if (optbuf) 350da7e7114SAdrian Chadd free(optbuf); 3518235d79aSJuli Mallett free(vfstype); 352da7e7114SAdrian Chadd return (1); 353da7e7114SAdrian Chadd 354da7e7114SAdrian Chadd case 0: /* Child. */ 355a02a0079SKirk McKusick if ((flags & CHECK_DEBUG) && auxopt == NULL) 356da7e7114SAdrian Chadd _exit(0); 357da7e7114SAdrian Chadd 358da7e7114SAdrian Chadd /* Go find an executable. */ 359a3ba4c65SGordon Tetlow execvP(execbase, _PATH_SYSPATH, (char * const *)argv); 360da7e7114SAdrian Chadd if (spec) 361a3ba4c65SGordon Tetlow warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH); 362da7e7114SAdrian Chadd else 363a3ba4c65SGordon Tetlow warn("exec %s in %s", execbase, _PATH_SYSPATH); 364da7e7114SAdrian Chadd _exit(1); 365da7e7114SAdrian Chadd /* NOTREACHED */ 366da7e7114SAdrian Chadd 367da7e7114SAdrian Chadd default: /* Parent. */ 368da7e7114SAdrian Chadd if (optbuf) 369da7e7114SAdrian Chadd free(optbuf); 370da7e7114SAdrian Chadd 3718235d79aSJuli Mallett free(vfstype); 3728235d79aSJuli Mallett 373da7e7114SAdrian Chadd if (pidp) { 374da7e7114SAdrian Chadd *pidp = pid; 375da7e7114SAdrian Chadd return 0; 376da7e7114SAdrian Chadd } 377da7e7114SAdrian Chadd 378da7e7114SAdrian Chadd if (waitpid(pid, &status, 0) < 0) { 379da7e7114SAdrian Chadd warn("waitpid"); 380da7e7114SAdrian Chadd return (1); 381da7e7114SAdrian Chadd } 382da7e7114SAdrian Chadd 383da7e7114SAdrian Chadd if (WIFEXITED(status)) { 384da7e7114SAdrian Chadd if (WEXITSTATUS(status) != 0) 385da7e7114SAdrian Chadd return (WEXITSTATUS(status)); 386da7e7114SAdrian Chadd } 387da7e7114SAdrian Chadd else if (WIFSIGNALED(status)) { 388da7e7114SAdrian Chadd warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 389da7e7114SAdrian Chadd return (1); 390da7e7114SAdrian Chadd } 391da7e7114SAdrian Chadd break; 392da7e7114SAdrian Chadd } 393da7e7114SAdrian Chadd 394da7e7114SAdrian Chadd return (0); 395da7e7114SAdrian Chadd } 396da7e7114SAdrian Chadd 397da7e7114SAdrian Chadd 398da7e7114SAdrian Chadd static int 399b70cd7eeSWarner Losh selected(const char *type) 400da7e7114SAdrian Chadd { 401da7e7114SAdrian Chadd struct entry *e; 402da7e7114SAdrian Chadd 403da7e7114SAdrian Chadd /* If no type specified, it's always selected. */ 40432ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &selhead, entries) 405da7e7114SAdrian Chadd if (!strncmp(e->type, type, MFSNAMELEN)) 406da7e7114SAdrian Chadd return which == IN_LIST ? 1 : 0; 407da7e7114SAdrian Chadd 408da7e7114SAdrian Chadd return which == IN_LIST ? 0 : 1; 409da7e7114SAdrian Chadd } 410da7e7114SAdrian Chadd 411da7e7114SAdrian Chadd 412da7e7114SAdrian Chadd static const char * 413b70cd7eeSWarner Losh getoptions(const char *type) 414da7e7114SAdrian Chadd { 415da7e7114SAdrian Chadd struct entry *e; 416da7e7114SAdrian Chadd 41732ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &opthead, entries) 418da7e7114SAdrian Chadd if (!strncmp(e->type, type, MFSNAMELEN)) 419da7e7114SAdrian Chadd return e->options; 420da7e7114SAdrian Chadd return ""; 421da7e7114SAdrian Chadd } 422da7e7114SAdrian Chadd 423da7e7114SAdrian Chadd 424da7e7114SAdrian Chadd static void 425b70cd7eeSWarner Losh addoption(char *optstr) 426da7e7114SAdrian Chadd { 427da7e7114SAdrian Chadd char *newoptions; 428da7e7114SAdrian Chadd struct entry *e; 429da7e7114SAdrian Chadd 430da7e7114SAdrian Chadd if ((newoptions = strchr(optstr, ':')) == NULL) 431da7e7114SAdrian Chadd errx(1, "Invalid option string"); 432da7e7114SAdrian Chadd 433da7e7114SAdrian Chadd *newoptions++ = '\0'; 434da7e7114SAdrian Chadd 43532ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &opthead, entries) 436da7e7114SAdrian Chadd if (!strncmp(e->type, optstr, MFSNAMELEN)) { 437da7e7114SAdrian Chadd catopt(&e->options, newoptions); 438da7e7114SAdrian Chadd return; 439da7e7114SAdrian Chadd } 440da7e7114SAdrian Chadd addentry(&opthead, optstr, newoptions); 441da7e7114SAdrian Chadd } 442da7e7114SAdrian Chadd 443da7e7114SAdrian Chadd 444da7e7114SAdrian Chadd static void 445b70cd7eeSWarner Losh addentry(struct fstypelist *list, const char *type, const char *opts) 446da7e7114SAdrian Chadd { 447da7e7114SAdrian Chadd struct entry *e; 448da7e7114SAdrian Chadd 449da7e7114SAdrian Chadd e = emalloc(sizeof(struct entry)); 450da7e7114SAdrian Chadd e->type = estrdup(type); 451da7e7114SAdrian Chadd e->options = estrdup(opts); 452da7e7114SAdrian Chadd TAILQ_INSERT_TAIL(list, e, entries); 453da7e7114SAdrian Chadd } 454da7e7114SAdrian Chadd 455da7e7114SAdrian Chadd 456da7e7114SAdrian Chadd static void 457b70cd7eeSWarner Losh maketypelist(char *fslist) 458da7e7114SAdrian Chadd { 459da7e7114SAdrian Chadd char *ptr; 460da7e7114SAdrian Chadd 461da7e7114SAdrian Chadd if ((fslist == NULL) || (fslist[0] == '\0')) 462da7e7114SAdrian Chadd errx(1, "empty type list"); 463da7e7114SAdrian Chadd 464da7e7114SAdrian Chadd if (fslist[0] == 'n' && fslist[1] == 'o') { 465da7e7114SAdrian Chadd fslist += 2; 466da7e7114SAdrian Chadd which = NOT_IN_LIST; 467da7e7114SAdrian Chadd } 468da7e7114SAdrian Chadd else 469da7e7114SAdrian Chadd which = IN_LIST; 470da7e7114SAdrian Chadd 471da7e7114SAdrian Chadd while ((ptr = strsep(&fslist, ",")) != NULL) 472da7e7114SAdrian Chadd addentry(&selhead, ptr, ""); 473da7e7114SAdrian Chadd 474da7e7114SAdrian Chadd } 475da7e7114SAdrian Chadd 476da7e7114SAdrian Chadd 477da7e7114SAdrian Chadd static void 478b70cd7eeSWarner Losh catopt(char **sp, const char *o) 479da7e7114SAdrian Chadd { 480da7e7114SAdrian Chadd char *s; 481da7e7114SAdrian Chadd size_t i, j; 482da7e7114SAdrian Chadd 483da7e7114SAdrian Chadd s = *sp; 484da7e7114SAdrian Chadd if (s) { 485da7e7114SAdrian Chadd i = strlen(s); 486da7e7114SAdrian Chadd j = i + 1 + strlen(o) + 1; 487da7e7114SAdrian Chadd s = erealloc(s, j); 488da7e7114SAdrian Chadd (void)snprintf(s + i, j, ",%s", o); 489da7e7114SAdrian Chadd } else 490da7e7114SAdrian Chadd s = estrdup(o); 491da7e7114SAdrian Chadd *sp = s; 492da7e7114SAdrian Chadd } 493da7e7114SAdrian Chadd 494da7e7114SAdrian Chadd 495da7e7114SAdrian Chadd static void 496b70cd7eeSWarner Losh mangle(char *options, int *argcp, const char ***argvp, int *maxargcp) 497da7e7114SAdrian Chadd { 498da7e7114SAdrian Chadd char *p, *s; 499da7e7114SAdrian Chadd int argc, maxargc; 500da7e7114SAdrian Chadd const char **argv; 501da7e7114SAdrian Chadd 502da7e7114SAdrian Chadd argc = *argcp; 503da7e7114SAdrian Chadd argv = *argvp; 504da7e7114SAdrian Chadd maxargc = *maxargcp; 505da7e7114SAdrian Chadd 506da7e7114SAdrian Chadd for (s = options; (p = strsep(&s, ",")) != NULL;) { 507da7e7114SAdrian Chadd /* Always leave space for one more argument and the NULL. */ 508da7e7114SAdrian Chadd if (argc >= maxargc - 3) { 509da7e7114SAdrian Chadd maxargc <<= 1; 510da7e7114SAdrian Chadd argv = erealloc(argv, maxargc * sizeof(char *)); 511da7e7114SAdrian Chadd } 512da7e7114SAdrian Chadd if (*p != '\0') { 513da7e7114SAdrian Chadd if (*p == '-') { 514da7e7114SAdrian Chadd argv[argc++] = p; 515da7e7114SAdrian Chadd p = strchr(p, '='); 516da7e7114SAdrian Chadd if (p) { 517da7e7114SAdrian Chadd *p = '\0'; 518da7e7114SAdrian Chadd argv[argc++] = p+1; 519da7e7114SAdrian Chadd } 520da7e7114SAdrian Chadd } else { 521da7e7114SAdrian Chadd argv[argc++] = "-o"; 522da7e7114SAdrian Chadd argv[argc++] = p; 523da7e7114SAdrian Chadd } 524da7e7114SAdrian Chadd } 525da7e7114SAdrian Chadd } 526da7e7114SAdrian Chadd 527da7e7114SAdrian Chadd *argcp = argc; 528da7e7114SAdrian Chadd *argvp = argv; 529da7e7114SAdrian Chadd *maxargcp = maxargc; 530da7e7114SAdrian Chadd } 531da7e7114SAdrian Chadd 532da7e7114SAdrian Chadd 533da7e7114SAdrian Chadd const static char * 534b70cd7eeSWarner Losh getfslab(const char *str) 535da7e7114SAdrian Chadd { 536da7e7114SAdrian Chadd struct disklabel dl; 537da7e7114SAdrian Chadd int fd; 538da7e7114SAdrian Chadd char p; 539da7e7114SAdrian Chadd const char *vfstype; 540da7e7114SAdrian Chadd u_char t; 541da7e7114SAdrian Chadd 542da7e7114SAdrian Chadd /* deduce the file system type from the disk label */ 543da7e7114SAdrian Chadd if ((fd = open(str, O_RDONLY)) == -1) 544da7e7114SAdrian Chadd err(1, "cannot open `%s'", str); 545da7e7114SAdrian Chadd 546da7e7114SAdrian Chadd if (ioctl(fd, DIOCGDINFO, &dl) == -1) 5475c63c8ddSPoul-Henning Kamp return(NULL); 548da7e7114SAdrian Chadd 549da7e7114SAdrian Chadd (void) close(fd); 550da7e7114SAdrian Chadd 551da7e7114SAdrian Chadd p = str[strlen(str) - 1]; 552da7e7114SAdrian Chadd 553da7e7114SAdrian Chadd if ((p - 'a') >= dl.d_npartitions) 554da7e7114SAdrian Chadd errx(1, "partition `%s' is not defined on disk", str); 555da7e7114SAdrian Chadd 556da7e7114SAdrian Chadd if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES) 557da7e7114SAdrian Chadd errx(1, "partition `%s' is not of a legal vfstype", 558da7e7114SAdrian Chadd str); 559da7e7114SAdrian Chadd 560da7e7114SAdrian Chadd if ((vfstype = fstypenames[t]) == NULL) 561da7e7114SAdrian Chadd errx(1, "vfstype `%s' on partition `%s' is not supported", 562da7e7114SAdrian Chadd fstypenames[t], str); 563da7e7114SAdrian Chadd 564da7e7114SAdrian Chadd return vfstype; 565da7e7114SAdrian Chadd } 566da7e7114SAdrian Chadd 567da7e7114SAdrian Chadd 568da7e7114SAdrian Chadd static void 569b70cd7eeSWarner Losh usage(void) 570da7e7114SAdrian Chadd { 571da7e7114SAdrian Chadd static const char common[] = 572111a5220SDavid E. O'Brien "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype]"; 573da7e7114SAdrian Chadd 574d3974088SDag-Erling Smørgrav (void)fprintf(stderr, "usage: %s %s [special | node] ...\n", 575b813a714SMark Murray getprogname(), common); 576da7e7114SAdrian Chadd exit(1); 577da7e7114SAdrian Chadd } 578