Jeff Sharkey | 74d9e58 | 2018-09-13 11:15:24 -0600 | [diff] [blame] | 1 | /* $NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $ */ |
| 2 | |
| 3 | /*- |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | * Copyright (c) 1990, 1993 |
| 7 | * The Regents of the University of California. All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <sys/cdefs.h> |
| 35 | #ifndef lint |
| 36 | __RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $"); |
| 37 | #endif /* not lint */ |
| 38 | __FBSDID("$FreeBSD$"); |
| 39 | |
| 40 | #include <sys/param.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <sys/mount.h> |
| 43 | |
| 44 | #include <err.h> |
| 45 | #include <errno.h> |
| 46 | #ifndef __ANDROID__ |
| 47 | #include <fstab.h> |
| 48 | #endif |
| 49 | #include <paths.h> |
| 50 | #include <stdarg.h> |
| 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
| 54 | |
| 55 | #include "fsutil.h" |
| 56 | |
| 57 | static const char *dev = NULL; |
| 58 | static int preen = 0; |
| 59 | |
| 60 | static void vmsg(int, const char *, va_list) __printflike(2, 0); |
| 61 | |
| 62 | void |
| 63 | setcdevname(const char *cd, int pr) |
| 64 | { |
| 65 | dev = cd; |
| 66 | preen = pr; |
| 67 | } |
| 68 | |
| 69 | const char * |
| 70 | cdevname(void) |
| 71 | { |
| 72 | return dev; |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | vmsg(int fatal, const char *fmt, va_list ap) |
| 77 | { |
| 78 | if (!fatal && preen) |
| 79 | (void) printf("%s: ", dev); |
| 80 | |
| 81 | (void) vprintf(fmt, ap); |
| 82 | |
| 83 | if (fatal && preen) |
| 84 | (void) printf("\n"); |
| 85 | |
| 86 | if (fatal && preen) { |
| 87 | (void) printf( |
| 88 | "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", |
| 89 | dev, getprogname()); |
| 90 | exit(8); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /*VARARGS*/ |
| 95 | void |
| 96 | pfatal(const char *fmt, ...) |
| 97 | { |
| 98 | va_list ap; |
| 99 | |
| 100 | va_start(ap, fmt); |
| 101 | vmsg(1, fmt, ap); |
| 102 | va_end(ap); |
| 103 | } |
| 104 | |
| 105 | /*VARARGS*/ |
| 106 | void |
| 107 | pwarn(const char *fmt, ...) |
| 108 | { |
| 109 | va_list ap; |
| 110 | |
| 111 | va_start(ap, fmt); |
| 112 | vmsg(0, fmt, ap); |
| 113 | va_end(ap); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | perr(const char *fmt, ...) |
| 118 | { |
| 119 | va_list ap; |
| 120 | |
| 121 | va_start(ap, fmt); |
| 122 | vmsg(1, fmt, ap); |
| 123 | va_end(ap); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | panic(const char *fmt, ...) |
| 128 | { |
| 129 | va_list ap; |
| 130 | |
| 131 | va_start(ap, fmt); |
| 132 | vmsg(1, fmt, ap); |
| 133 | va_end(ap); |
| 134 | exit(8); |
| 135 | } |
| 136 | |
| 137 | const char * |
| 138 | devcheck(const char *origname) |
| 139 | { |
| 140 | struct stat stslash, stchar; |
| 141 | |
| 142 | if (stat("/", &stslash) < 0) { |
| 143 | perr("Can't stat `/'"); |
| 144 | return (origname); |
| 145 | } |
| 146 | if (stat(origname, &stchar) < 0) { |
| 147 | perr("Can't stat %s\n", origname); |
| 148 | return (origname); |
| 149 | } |
| 150 | if (!S_ISCHR(stchar.st_mode)) { |
| 151 | perr("%s is not a char device\n", origname); |
| 152 | } |
| 153 | return (origname); |
| 154 | } |
| 155 | |
| 156 | #ifndef __ANDROID__ |
| 157 | /* |
| 158 | * Get the mount point information for name. |
| 159 | */ |
| 160 | struct statfs * |
| 161 | getmntpt(const char *name) |
| 162 | { |
| 163 | struct stat devstat, mntdevstat; |
| 164 | char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; |
| 165 | char *dev_name; |
| 166 | struct statfs *mntbuf, *statfsp; |
| 167 | int i, mntsize, isdev; |
| 168 | |
| 169 | if (stat(name, &devstat) != 0) |
| 170 | return (NULL); |
| 171 | if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) |
| 172 | isdev = 1; |
| 173 | else |
| 174 | isdev = 0; |
| 175 | mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); |
| 176 | for (i = 0; i < mntsize; i++) { |
| 177 | statfsp = &mntbuf[i]; |
| 178 | dev_name = statfsp->f_mntfromname; |
| 179 | if (*dev_name != '/') { |
| 180 | if (strlen(_PATH_DEV) + strlen(dev_name) + 1 > |
| 181 | sizeof(statfsp->f_mntfromname)) |
| 182 | continue; |
| 183 | strcpy(device, _PATH_DEV); |
| 184 | strcat(device, dev_name); |
| 185 | strcpy(statfsp->f_mntfromname, device); |
| 186 | } |
| 187 | if (isdev == 0) { |
| 188 | if (strcmp(name, statfsp->f_mntonname)) |
| 189 | continue; |
| 190 | return (statfsp); |
| 191 | } |
| 192 | if (stat(dev_name, &mntdevstat) == 0 && |
| 193 | mntdevstat.st_rdev == devstat.st_rdev) |
| 194 | return (statfsp); |
| 195 | } |
| 196 | statfsp = NULL; |
| 197 | return (statfsp); |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | void * |
| 202 | emalloc(size_t s) |
| 203 | { |
| 204 | void *p; |
| 205 | |
| 206 | p = malloc(s); |
| 207 | if (p == NULL) |
| 208 | err(1, "malloc failed"); |
| 209 | return (p); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void * |
| 214 | erealloc(void *p, size_t s) |
| 215 | { |
| 216 | void *q; |
| 217 | |
| 218 | q = realloc(p, s); |
| 219 | if (q == NULL) |
| 220 | err(1, "realloc failed"); |
| 221 | return (q); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | char * |
| 226 | estrdup(const char *s) |
| 227 | { |
| 228 | char *p; |
| 229 | |
| 230 | p = strdup(s); |
| 231 | if (p == NULL) |
| 232 | err(1, "strdup failed"); |
| 233 | return (p); |
| 234 | } |