1*6cf357bcSUlrich 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. 16*6cf357bcSUlrich 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 34*6cf357bcSUlrich 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> 44da7e7114SAdrian Chadd #define FSTYPENAMES 45da7e7114SAdrian Chadd #include <sys/disklabel.h> 46da7e7114SAdrian Chadd #include <sys/ioctl.h> 47da7e7114SAdrian Chadd 48b813a714SMark Murray #include <ctype.h> 49da7e7114SAdrian Chadd #include <err.h> 50da7e7114SAdrian Chadd #include <errno.h> 51da7e7114SAdrian Chadd #include <fstab.h> 52da7e7114SAdrian Chadd #include <fcntl.h> 53da7e7114SAdrian Chadd #include <paths.h> 54da7e7114SAdrian Chadd #include <signal.h> 55da7e7114SAdrian Chadd #include <stdio.h> 56da7e7114SAdrian Chadd #include <stdlib.h> 57da7e7114SAdrian Chadd #include <string.h> 58da7e7114SAdrian Chadd #include <unistd.h> 59da7e7114SAdrian Chadd 60da7e7114SAdrian Chadd #include "fsutil.h" 61da7e7114SAdrian Chadd 62da7e7114SAdrian Chadd static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST; 63da7e7114SAdrian Chadd 641efe3c6bSEd Schouten static TAILQ_HEAD(fstypelist, entry) opthead, selhead; 65da7e7114SAdrian Chadd 66da7e7114SAdrian Chadd struct entry { 67da7e7114SAdrian Chadd char *type; 68da7e7114SAdrian Chadd char *options; 69da7e7114SAdrian Chadd TAILQ_ENTRY(entry) entries; 70da7e7114SAdrian Chadd }; 71da7e7114SAdrian Chadd 72da7e7114SAdrian Chadd static char *options = NULL; 73da7e7114SAdrian Chadd static int flags = 0; 74a02a0079SKirk McKusick static int forceflag = 0; 75da7e7114SAdrian Chadd 76b70cd7eeSWarner Losh static int checkfs(const char *, const char *, const char *, char *, pid_t *); 77b70cd7eeSWarner Losh static int selected(const char *); 78b70cd7eeSWarner Losh static void addoption(char *); 79b70cd7eeSWarner Losh static const char *getoptions(const char *); 80b70cd7eeSWarner Losh static void addentry(struct fstypelist *, const char *, const char *); 81b70cd7eeSWarner Losh static void maketypelist(char *); 82b70cd7eeSWarner Losh static void catopt(char **, const char *); 83b70cd7eeSWarner Losh static void mangle(char *, int *, const char ***, int *); 84b70cd7eeSWarner Losh static const char *getfslab(const char *); 85b70cd7eeSWarner Losh static void usage(void) __dead2; 86b70cd7eeSWarner Losh static int isok(struct fstab *); 87da7e7114SAdrian Chadd 88da7e7114SAdrian Chadd int 89b70cd7eeSWarner Losh main(int argc, char *argv[]) 90da7e7114SAdrian Chadd { 91da7e7114SAdrian Chadd struct fstab *fs; 92da7e7114SAdrian Chadd int i, rval = 0; 93da7e7114SAdrian Chadd const char *vfstype = NULL; 94da7e7114SAdrian Chadd char globopt[3]; 95f2104fc0SMaxim Sobolev const char *etc_fstab; 96da7e7114SAdrian Chadd 97da7e7114SAdrian Chadd globopt[0] = '-'; 98da7e7114SAdrian Chadd globopt[2] = '\0'; 99da7e7114SAdrian Chadd 100da7e7114SAdrian Chadd TAILQ_INIT(&selhead); 101da7e7114SAdrian Chadd TAILQ_INIT(&opthead); 102da7e7114SAdrian Chadd 103f2104fc0SMaxim Sobolev etc_fstab = NULL; 104f2104fc0SMaxim Sobolev while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1) 105da7e7114SAdrian Chadd switch (i) { 106a02a0079SKirk McKusick case 'B': 107a02a0079SKirk McKusick if (flags & CHECK_BACKGRD) 108a02a0079SKirk McKusick errx(1, "Cannot specify -B and -F."); 109a02a0079SKirk McKusick flags |= DO_BACKGRD; 110a02a0079SKirk McKusick break; 111a02a0079SKirk McKusick 112da7e7114SAdrian Chadd case 'd': 113da7e7114SAdrian Chadd flags |= CHECK_DEBUG; 114da7e7114SAdrian Chadd break; 115da7e7114SAdrian Chadd 116da7e7114SAdrian Chadd case 'v': 117da7e7114SAdrian Chadd flags |= CHECK_VERBOSE; 118da7e7114SAdrian Chadd break; 119da7e7114SAdrian Chadd 120a02a0079SKirk McKusick case 'F': 121a02a0079SKirk McKusick if (flags & DO_BACKGRD) 122a02a0079SKirk McKusick errx(1, "Cannot specify -B and -F."); 123a02a0079SKirk McKusick flags |= CHECK_BACKGRD; 124a02a0079SKirk McKusick break; 125a02a0079SKirk McKusick 126da7e7114SAdrian Chadd case 'p': 127da7e7114SAdrian Chadd flags |= CHECK_PREEN; 128da7e7114SAdrian Chadd /*FALLTHROUGH*/ 129111a5220SDavid E. O'Brien case 'C': 130111a5220SDavid E. O'Brien flags |= CHECK_CLEAN; 131111a5220SDavid E. O'Brien /*FALLTHROUGH*/ 132da7e7114SAdrian Chadd case 'n': 133da7e7114SAdrian Chadd case 'y': 134da7e7114SAdrian Chadd globopt[1] = i; 135da7e7114SAdrian Chadd catopt(&options, globopt); 136da7e7114SAdrian Chadd break; 137da7e7114SAdrian Chadd 138a02a0079SKirk McKusick case 'f': 139a02a0079SKirk McKusick forceflag = 1; 140a02a0079SKirk McKusick globopt[1] = i; 141a02a0079SKirk McKusick catopt(&options, globopt); 142a02a0079SKirk McKusick break; 143a02a0079SKirk McKusick 144da7e7114SAdrian Chadd case 'l': 1450af7bca2SPoul-Henning Kamp warnx("Ignoring obsolete -l option\n"); 146da7e7114SAdrian Chadd break; 147da7e7114SAdrian Chadd 148da7e7114SAdrian Chadd case 'T': 149da7e7114SAdrian Chadd if (*optarg) 150da7e7114SAdrian Chadd addoption(optarg); 151da7e7114SAdrian Chadd break; 152da7e7114SAdrian Chadd 153da7e7114SAdrian Chadd case 't': 15432ff2662SPoul-Henning Kamp if (!TAILQ_EMPTY(&selhead)) 155da7e7114SAdrian Chadd errx(1, "only one -t option may be specified."); 156da7e7114SAdrian Chadd 157da7e7114SAdrian Chadd maketypelist(optarg); 158da7e7114SAdrian Chadd vfstype = optarg; 159da7e7114SAdrian Chadd break; 160da7e7114SAdrian Chadd 161f2104fc0SMaxim Sobolev case 'c': 162f2104fc0SMaxim Sobolev etc_fstab = optarg; 163f2104fc0SMaxim Sobolev break; 164f2104fc0SMaxim Sobolev 165da7e7114SAdrian Chadd case '?': 166da7e7114SAdrian Chadd default: 167da7e7114SAdrian Chadd usage(); 168da7e7114SAdrian Chadd /* NOTREACHED */ 169da7e7114SAdrian Chadd } 170da7e7114SAdrian Chadd 171da7e7114SAdrian Chadd argc -= optind; 172da7e7114SAdrian Chadd argv += optind; 173da7e7114SAdrian Chadd 174f2104fc0SMaxim Sobolev if (etc_fstab != NULL) 175f2104fc0SMaxim Sobolev setfstab(etc_fstab); 176f2104fc0SMaxim Sobolev 177da7e7114SAdrian Chadd if (argc == 0) 1780af7bca2SPoul-Henning Kamp return checkfstab(flags, isok, checkfs); 179da7e7114SAdrian Chadd 180da7e7114SAdrian Chadd #define BADTYPE(type) \ 181da7e7114SAdrian Chadd (strcmp(type, FSTAB_RO) && \ 182da7e7114SAdrian Chadd strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 183da7e7114SAdrian Chadd 184da7e7114SAdrian Chadd 185da7e7114SAdrian Chadd for (; argc--; argv++) { 186a02a0079SKirk McKusick const char *spec, *mntpt, *type, *cp; 187da7e7114SAdrian Chadd char device[MAXPATHLEN]; 188a02a0079SKirk McKusick struct statfs *mntp; 189da7e7114SAdrian Chadd 190da7e7114SAdrian Chadd spec = *argv; 191da7e7114SAdrian Chadd cp = strrchr(spec, '/'); 192da7e7114SAdrian Chadd if (cp == 0) { 193da7e7114SAdrian Chadd (void)snprintf(device, sizeof(device), "%s%s", 194da7e7114SAdrian Chadd _PATH_DEV, spec); 195da7e7114SAdrian Chadd spec = device; 196da7e7114SAdrian Chadd } 197a02a0079SKirk McKusick mntp = getmntpt(spec); 198a02a0079SKirk McKusick if (mntp != NULL) { 199a02a0079SKirk McKusick spec = mntp->f_mntfromname; 200a02a0079SKirk McKusick mntpt = mntp->f_mntonname; 201a02a0079SKirk McKusick } 202da7e7114SAdrian Chadd if ((fs = getfsfile(spec)) == NULL && 203da7e7114SAdrian Chadd (fs = getfsspec(spec)) == NULL) { 204da7e7114SAdrian Chadd if (vfstype == NULL) 205da7e7114SAdrian Chadd vfstype = getfslab(spec); 2065c63c8ddSPoul-Henning Kamp if (vfstype == NULL) 2075c63c8ddSPoul-Henning Kamp errx(1, "Could not determine filesystem type"); 208da7e7114SAdrian Chadd type = vfstype; 209a02a0079SKirk McKusick devcheck(spec); 210a02a0079SKirk McKusick } else { 211da7e7114SAdrian Chadd spec = fs->fs_spec; 212da7e7114SAdrian Chadd type = fs->fs_vfstype; 213a02a0079SKirk McKusick mntpt = fs->fs_file; 214da7e7114SAdrian Chadd if (BADTYPE(fs->fs_type)) 215da7e7114SAdrian Chadd errx(1, "%s has unknown file system type.", 216da7e7114SAdrian Chadd spec); 217da7e7114SAdrian Chadd } 218a02a0079SKirk McKusick if ((flags & CHECK_BACKGRD) && 219a02a0079SKirk McKusick checkfs(type, spec, mntpt, "-F", NULL) == 0) { 220a02a0079SKirk McKusick printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv); 221a02a0079SKirk McKusick continue; 222a02a0079SKirk McKusick } 223a02a0079SKirk McKusick if ((flags & DO_BACKGRD) && forceflag == 0 && 224a02a0079SKirk McKusick checkfs(type, spec, mntpt, "-F", NULL) != 0) 225a02a0079SKirk McKusick continue; 226da7e7114SAdrian Chadd 227a02a0079SKirk McKusick rval |= checkfs(type, spec, mntpt, NULL, NULL); 228da7e7114SAdrian Chadd } 229da7e7114SAdrian Chadd 230da7e7114SAdrian Chadd return rval; 231da7e7114SAdrian Chadd } 232da7e7114SAdrian Chadd 233da7e7114SAdrian Chadd 234a02a0079SKirk McKusick static int 235b70cd7eeSWarner Losh isok(struct fstab *fs) 236da7e7114SAdrian Chadd { 237a02a0079SKirk McKusick int i; 238a02a0079SKirk McKusick 239da7e7114SAdrian Chadd if (fs->fs_passno == 0) 240a02a0079SKirk McKusick return (0); 241da7e7114SAdrian Chadd if (BADTYPE(fs->fs_type)) 242a02a0079SKirk McKusick return (0); 243da7e7114SAdrian Chadd if (!selected(fs->fs_vfstype)) 244a02a0079SKirk McKusick return (0); 245a02a0079SKirk McKusick /* 246a02a0079SKirk McKusick * If the -B flag has been given, then process the needed 247a02a0079SKirk McKusick * background checks. Background checks cannot be run on 248a02a0079SKirk McKusick * file systems that will be mounted read-only or that were 249a02a0079SKirk McKusick * not mounted at boot time (typically those marked `noauto'). 250a02a0079SKirk McKusick * If these basic tests are passed, check with the file system 251a02a0079SKirk McKusick * itself to see if it is willing to do background checking 252a02a0079SKirk McKusick * by invoking its check program with the -F flag. 253a02a0079SKirk McKusick */ 254a02a0079SKirk McKusick if (flags & DO_BACKGRD) { 255a02a0079SKirk McKusick if (!strcmp(fs->fs_type, FSTAB_RO)) 256a02a0079SKirk McKusick return (0); 257a02a0079SKirk McKusick if (getmntpt(fs->fs_spec) == NULL) 258a02a0079SKirk McKusick return (0); 259a02a0079SKirk McKusick if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0)) 260a02a0079SKirk McKusick return (0); 261a02a0079SKirk McKusick return (1); 262a02a0079SKirk McKusick } 263a02a0079SKirk McKusick /* 264a02a0079SKirk McKusick * If the -F flag has been given, then consider deferring the 265a02a0079SKirk McKusick * check to background. Background checks cannot be run on 266a02a0079SKirk McKusick * file systems that will be mounted read-only or that will 267a02a0079SKirk McKusick * not be mounted at boot time (e.g., marked `noauto'). If 268a02a0079SKirk McKusick * these basic tests are passed, check with the file system 269a02a0079SKirk McKusick * itself to see if it is willing to defer to background 270a02a0079SKirk McKusick * checking by invoking its check program with the -F flag. 271a02a0079SKirk McKusick */ 272a02a0079SKirk McKusick if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO)) 273a02a0079SKirk McKusick return (1); 274a02a0079SKirk McKusick for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--) 275a02a0079SKirk McKusick if (!strncmp(&fs->fs_mntops[i], "noauto", 6)) 276a02a0079SKirk McKusick break; 277a02a0079SKirk McKusick if (i >= 0) 278a02a0079SKirk McKusick return (1); 279a02a0079SKirk McKusick if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0) 280a02a0079SKirk McKusick return (1); 281a02a0079SKirk McKusick printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec); 282a02a0079SKirk McKusick return (0); 283da7e7114SAdrian Chadd } 284da7e7114SAdrian Chadd 285da7e7114SAdrian Chadd 286da7e7114SAdrian Chadd static int 2878235d79aSJuli Mallett checkfs(const char *pvfstype, const char *spec, const char *mntpt, 288b70cd7eeSWarner Losh char *auxopt, pid_t *pidp) 289da7e7114SAdrian Chadd { 290a3ba4c65SGordon Tetlow const char **argv; 291da7e7114SAdrian Chadd pid_t pid; 292da7e7114SAdrian Chadd int argc, i, status, maxargc; 293a3ba4c65SGordon Tetlow char *optbuf, execbase[MAXPATHLEN]; 2948235d79aSJuli Mallett char *vfstype = NULL; 295da7e7114SAdrian Chadd const char *extra = NULL; 296da7e7114SAdrian Chadd 297da7e7114SAdrian Chadd #ifdef __GNUC__ 298da7e7114SAdrian Chadd /* Avoid vfork clobbering */ 299da7e7114SAdrian Chadd (void) &optbuf; 300da7e7114SAdrian Chadd (void) &vfstype; 301da7e7114SAdrian Chadd #endif 3028235d79aSJuli Mallett /* 3038235d79aSJuli Mallett * We convert the vfstype to lowercase and any spaces to underscores 3048235d79aSJuli Mallett * to not confuse the issue 3058235d79aSJuli Mallett * 3068235d79aSJuli Mallett * XXX This is a kludge to make automatic filesystem type guessing 3078235d79aSJuli Mallett * from the disklabel work for "4.2BSD" filesystems. It does a 3088235d79aSJuli Mallett * very limited subset of transliteration to a normalised form of 3098235d79aSJuli Mallett * filesystem name, and we do not seem to enforce a filesystem 3108235d79aSJuli Mallett * name character set. 3118235d79aSJuli Mallett */ 3128235d79aSJuli Mallett vfstype = strdup(pvfstype); 3138235d79aSJuli Mallett if (vfstype == NULL) 314*6cf357bcSUlrich Spörlein perr("strdup(pvfstype)"); 3158235d79aSJuli Mallett for (i = 0; i < strlen(vfstype); i++) { 3168235d79aSJuli Mallett vfstype[i] = tolower(vfstype[i]); 3178235d79aSJuli Mallett if (vfstype[i] == ' ') 3188235d79aSJuli Mallett vfstype[i] = '_'; 3198235d79aSJuli Mallett } 320da7e7114SAdrian Chadd 321da7e7114SAdrian Chadd extra = getoptions(vfstype); 322da7e7114SAdrian Chadd optbuf = NULL; 323da7e7114SAdrian Chadd if (options) 324da7e7114SAdrian Chadd catopt(&optbuf, options); 325da7e7114SAdrian Chadd if (extra) 326da7e7114SAdrian Chadd catopt(&optbuf, extra); 327a02a0079SKirk McKusick if (auxopt) 328a02a0079SKirk McKusick catopt(&optbuf, auxopt); 329a02a0079SKirk McKusick else if (flags & DO_BACKGRD) 330a02a0079SKirk McKusick catopt(&optbuf, "-B"); 331da7e7114SAdrian Chadd 332da7e7114SAdrian Chadd maxargc = 64; 333da7e7114SAdrian Chadd argv = emalloc(sizeof(char *) * maxargc); 334da7e7114SAdrian Chadd 335da7e7114SAdrian Chadd (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype); 336da7e7114SAdrian Chadd argc = 0; 337da7e7114SAdrian Chadd argv[argc++] = execbase; 338da7e7114SAdrian Chadd if (optbuf) 339da7e7114SAdrian Chadd mangle(optbuf, &argc, &argv, &maxargc); 340da7e7114SAdrian Chadd argv[argc++] = spec; 341da7e7114SAdrian Chadd argv[argc] = NULL; 342da7e7114SAdrian Chadd 343da7e7114SAdrian Chadd if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) { 344da7e7114SAdrian Chadd (void)printf("start %s %swait", mntpt, 345da7e7114SAdrian Chadd pidp ? "no" : ""); 346da7e7114SAdrian Chadd for (i = 0; i < argc; i++) 347da7e7114SAdrian Chadd (void)printf(" %s", argv[i]); 348da7e7114SAdrian Chadd (void)printf("\n"); 349da7e7114SAdrian Chadd } 350da7e7114SAdrian Chadd 351da7e7114SAdrian Chadd switch (pid = vfork()) { 352da7e7114SAdrian Chadd case -1: /* Error. */ 353da7e7114SAdrian Chadd warn("vfork"); 354da7e7114SAdrian Chadd if (optbuf) 355da7e7114SAdrian Chadd free(optbuf); 3568235d79aSJuli Mallett free(vfstype); 357da7e7114SAdrian Chadd return (1); 358da7e7114SAdrian Chadd 359da7e7114SAdrian Chadd case 0: /* Child. */ 360a02a0079SKirk McKusick if ((flags & CHECK_DEBUG) && auxopt == NULL) 361da7e7114SAdrian Chadd _exit(0); 362da7e7114SAdrian Chadd 363da7e7114SAdrian Chadd /* Go find an executable. */ 364a3ba4c65SGordon Tetlow execvP(execbase, _PATH_SYSPATH, (char * const *)argv); 365da7e7114SAdrian Chadd if (spec) 366a3ba4c65SGordon Tetlow warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH); 367da7e7114SAdrian Chadd else 368a3ba4c65SGordon Tetlow warn("exec %s in %s", execbase, _PATH_SYSPATH); 369da7e7114SAdrian Chadd _exit(1); 370da7e7114SAdrian Chadd /* NOTREACHED */ 371da7e7114SAdrian Chadd 372da7e7114SAdrian Chadd default: /* Parent. */ 373da7e7114SAdrian Chadd if (optbuf) 374da7e7114SAdrian Chadd free(optbuf); 375da7e7114SAdrian Chadd 3768235d79aSJuli Mallett free(vfstype); 3778235d79aSJuli Mallett 378da7e7114SAdrian Chadd if (pidp) { 379da7e7114SAdrian Chadd *pidp = pid; 380da7e7114SAdrian Chadd return 0; 381da7e7114SAdrian Chadd } 382da7e7114SAdrian Chadd 383da7e7114SAdrian Chadd if (waitpid(pid, &status, 0) < 0) { 384da7e7114SAdrian Chadd warn("waitpid"); 385da7e7114SAdrian Chadd return (1); 386da7e7114SAdrian Chadd } 387da7e7114SAdrian Chadd 388da7e7114SAdrian Chadd if (WIFEXITED(status)) { 389da7e7114SAdrian Chadd if (WEXITSTATUS(status) != 0) 390da7e7114SAdrian Chadd return (WEXITSTATUS(status)); 391da7e7114SAdrian Chadd } 392da7e7114SAdrian Chadd else if (WIFSIGNALED(status)) { 393da7e7114SAdrian Chadd warnx("%s: %s", spec, strsignal(WTERMSIG(status))); 394da7e7114SAdrian Chadd return (1); 395da7e7114SAdrian Chadd } 396da7e7114SAdrian Chadd break; 397da7e7114SAdrian Chadd } 398da7e7114SAdrian Chadd 399da7e7114SAdrian Chadd return (0); 400da7e7114SAdrian Chadd } 401da7e7114SAdrian Chadd 402da7e7114SAdrian Chadd 403da7e7114SAdrian Chadd static int 404b70cd7eeSWarner Losh selected(const char *type) 405da7e7114SAdrian Chadd { 406da7e7114SAdrian Chadd struct entry *e; 407da7e7114SAdrian Chadd 408da7e7114SAdrian Chadd /* If no type specified, it's always selected. */ 40932ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &selhead, entries) 410da7e7114SAdrian Chadd if (!strncmp(e->type, type, MFSNAMELEN)) 411da7e7114SAdrian Chadd return which == IN_LIST ? 1 : 0; 412da7e7114SAdrian Chadd 413da7e7114SAdrian Chadd return which == IN_LIST ? 0 : 1; 414da7e7114SAdrian Chadd } 415da7e7114SAdrian Chadd 416da7e7114SAdrian Chadd 417da7e7114SAdrian Chadd static const char * 418b70cd7eeSWarner Losh getoptions(const char *type) 419da7e7114SAdrian Chadd { 420da7e7114SAdrian Chadd struct entry *e; 421da7e7114SAdrian Chadd 42232ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &opthead, entries) 423da7e7114SAdrian Chadd if (!strncmp(e->type, type, MFSNAMELEN)) 424da7e7114SAdrian Chadd return e->options; 425da7e7114SAdrian Chadd return ""; 426da7e7114SAdrian Chadd } 427da7e7114SAdrian Chadd 428da7e7114SAdrian Chadd 429da7e7114SAdrian Chadd static void 430b70cd7eeSWarner Losh addoption(char *optstr) 431da7e7114SAdrian Chadd { 432da7e7114SAdrian Chadd char *newoptions; 433da7e7114SAdrian Chadd struct entry *e; 434da7e7114SAdrian Chadd 435da7e7114SAdrian Chadd if ((newoptions = strchr(optstr, ':')) == NULL) 436da7e7114SAdrian Chadd errx(1, "Invalid option string"); 437da7e7114SAdrian Chadd 438da7e7114SAdrian Chadd *newoptions++ = '\0'; 439da7e7114SAdrian Chadd 44032ff2662SPoul-Henning Kamp TAILQ_FOREACH(e, &opthead, entries) 441da7e7114SAdrian Chadd if (!strncmp(e->type, optstr, MFSNAMELEN)) { 442da7e7114SAdrian Chadd catopt(&e->options, newoptions); 443da7e7114SAdrian Chadd return; 444da7e7114SAdrian Chadd } 445da7e7114SAdrian Chadd addentry(&opthead, optstr, newoptions); 446da7e7114SAdrian Chadd } 447da7e7114SAdrian Chadd 448da7e7114SAdrian Chadd 449da7e7114SAdrian Chadd static void 450b70cd7eeSWarner Losh addentry(struct fstypelist *list, const char *type, const char *opts) 451da7e7114SAdrian Chadd { 452da7e7114SAdrian Chadd struct entry *e; 453da7e7114SAdrian Chadd 454da7e7114SAdrian Chadd e = emalloc(sizeof(struct entry)); 455da7e7114SAdrian Chadd e->type = estrdup(type); 456da7e7114SAdrian Chadd e->options = estrdup(opts); 457da7e7114SAdrian Chadd TAILQ_INSERT_TAIL(list, e, entries); 458da7e7114SAdrian Chadd } 459da7e7114SAdrian Chadd 460da7e7114SAdrian Chadd 461da7e7114SAdrian Chadd static void 462b70cd7eeSWarner Losh maketypelist(char *fslist) 463da7e7114SAdrian Chadd { 464da7e7114SAdrian Chadd char *ptr; 465da7e7114SAdrian Chadd 466da7e7114SAdrian Chadd if ((fslist == NULL) || (fslist[0] == '\0')) 467da7e7114SAdrian Chadd errx(1, "empty type list"); 468da7e7114SAdrian Chadd 469da7e7114SAdrian Chadd if (fslist[0] == 'n' && fslist[1] == 'o') { 470da7e7114SAdrian Chadd fslist += 2; 471da7e7114SAdrian Chadd which = NOT_IN_LIST; 472da7e7114SAdrian Chadd } 473da7e7114SAdrian Chadd else 474da7e7114SAdrian Chadd which = IN_LIST; 475da7e7114SAdrian Chadd 476da7e7114SAdrian Chadd while ((ptr = strsep(&fslist, ",")) != NULL) 477da7e7114SAdrian Chadd addentry(&selhead, ptr, ""); 478da7e7114SAdrian Chadd 479da7e7114SAdrian Chadd } 480da7e7114SAdrian Chadd 481da7e7114SAdrian Chadd 482da7e7114SAdrian Chadd static void 483b70cd7eeSWarner Losh catopt(char **sp, const char *o) 484da7e7114SAdrian Chadd { 485da7e7114SAdrian Chadd char *s; 486da7e7114SAdrian Chadd size_t i, j; 487da7e7114SAdrian Chadd 488da7e7114SAdrian Chadd s = *sp; 489da7e7114SAdrian Chadd if (s) { 490da7e7114SAdrian Chadd i = strlen(s); 491da7e7114SAdrian Chadd j = i + 1 + strlen(o) + 1; 492da7e7114SAdrian Chadd s = erealloc(s, j); 493da7e7114SAdrian Chadd (void)snprintf(s + i, j, ",%s", o); 494da7e7114SAdrian Chadd } else 495da7e7114SAdrian Chadd s = estrdup(o); 496da7e7114SAdrian Chadd *sp = s; 497da7e7114SAdrian Chadd } 498da7e7114SAdrian Chadd 499da7e7114SAdrian Chadd 500da7e7114SAdrian Chadd static void 501*6cf357bcSUlrich Spörlein mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp) 502da7e7114SAdrian Chadd { 503da7e7114SAdrian Chadd char *p, *s; 504da7e7114SAdrian Chadd int argc, maxargc; 505da7e7114SAdrian Chadd const char **argv; 506da7e7114SAdrian Chadd 507da7e7114SAdrian Chadd argc = *argcp; 508da7e7114SAdrian Chadd argv = *argvp; 509da7e7114SAdrian Chadd maxargc = *maxargcp; 510da7e7114SAdrian Chadd 511*6cf357bcSUlrich Spörlein for (s = opts; (p = strsep(&s, ",")) != NULL;) { 512da7e7114SAdrian Chadd /* Always leave space for one more argument and the NULL. */ 513da7e7114SAdrian Chadd if (argc >= maxargc - 3) { 514da7e7114SAdrian Chadd maxargc <<= 1; 515da7e7114SAdrian Chadd argv = erealloc(argv, maxargc * sizeof(char *)); 516da7e7114SAdrian Chadd } 517da7e7114SAdrian Chadd if (*p != '\0') { 518da7e7114SAdrian Chadd if (*p == '-') { 519da7e7114SAdrian Chadd argv[argc++] = p; 520da7e7114SAdrian Chadd p = strchr(p, '='); 521da7e7114SAdrian Chadd if (p) { 522da7e7114SAdrian Chadd *p = '\0'; 523da7e7114SAdrian Chadd argv[argc++] = p+1; 524da7e7114SAdrian Chadd } 525da7e7114SAdrian Chadd } else { 526da7e7114SAdrian Chadd argv[argc++] = "-o"; 527da7e7114SAdrian Chadd argv[argc++] = p; 528da7e7114SAdrian Chadd } 529da7e7114SAdrian Chadd } 530da7e7114SAdrian Chadd } 531da7e7114SAdrian Chadd 532da7e7114SAdrian Chadd *argcp = argc; 533da7e7114SAdrian Chadd *argvp = argv; 534da7e7114SAdrian Chadd *maxargcp = maxargc; 535da7e7114SAdrian Chadd } 536da7e7114SAdrian Chadd 537da7e7114SAdrian Chadd 538da7e7114SAdrian Chadd const static char * 539b70cd7eeSWarner Losh getfslab(const char *str) 540da7e7114SAdrian Chadd { 541da7e7114SAdrian Chadd struct disklabel dl; 542da7e7114SAdrian Chadd int fd; 543da7e7114SAdrian Chadd char p; 544da7e7114SAdrian Chadd const char *vfstype; 545da7e7114SAdrian Chadd u_char t; 546da7e7114SAdrian Chadd 547da7e7114SAdrian Chadd /* deduce the file system type from the disk label */ 548da7e7114SAdrian Chadd if ((fd = open(str, O_RDONLY)) == -1) 549da7e7114SAdrian Chadd err(1, "cannot open `%s'", str); 550da7e7114SAdrian Chadd 5514b2d15efSAlexander Leidinger if (ioctl(fd, DIOCGDINFO, &dl) == -1) { 5524b2d15efSAlexander Leidinger (void) close(fd); 5535c63c8ddSPoul-Henning Kamp return(NULL); 5544b2d15efSAlexander Leidinger } 555da7e7114SAdrian Chadd 556da7e7114SAdrian Chadd (void) close(fd); 557da7e7114SAdrian Chadd 558da7e7114SAdrian Chadd p = str[strlen(str) - 1]; 559da7e7114SAdrian Chadd 560da7e7114SAdrian Chadd if ((p - 'a') >= dl.d_npartitions) 561da7e7114SAdrian Chadd errx(1, "partition `%s' is not defined on disk", str); 562da7e7114SAdrian Chadd 563da7e7114SAdrian Chadd if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES) 564da7e7114SAdrian Chadd errx(1, "partition `%s' is not of a legal vfstype", 565da7e7114SAdrian Chadd str); 566da7e7114SAdrian Chadd 567da7e7114SAdrian Chadd if ((vfstype = fstypenames[t]) == NULL) 568da7e7114SAdrian Chadd errx(1, "vfstype `%s' on partition `%s' is not supported", 569da7e7114SAdrian Chadd fstypenames[t], str); 570da7e7114SAdrian Chadd 571da7e7114SAdrian Chadd return vfstype; 572da7e7114SAdrian Chadd } 573da7e7114SAdrian Chadd 574da7e7114SAdrian Chadd 575da7e7114SAdrian Chadd static void 576b70cd7eeSWarner Losh usage(void) 577da7e7114SAdrian Chadd { 578da7e7114SAdrian Chadd static const char common[] = 579f2104fc0SMaxim Sobolev "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]"; 580da7e7114SAdrian Chadd 581d3974088SDag-Erling Smørgrav (void)fprintf(stderr, "usage: %s %s [special | node] ...\n", 582b813a714SMark Murray getprogname(), common); 583da7e7114SAdrian Chadd exit(1); 584da7e7114SAdrian Chadd } 585