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