1 /* $NetBSD: fsutil.c,v 1.7 1998/07/30 17:41:03 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD: src/sbin/fsck/fsutil.c,v 1.2.2.1 2001/08/01 05:47:55 obrien Exp $ 36 */ 37 38 #include <sys/cdefs.h> 39 40 #include <stdio.h> 41 #include <string.h> 42 #include <stdlib.h> 43 #include <stdarg.h> 44 #include <errno.h> 45 #include <fstab.h> 46 #include <err.h> 47 #include <paths.h> 48 49 #include <sys/param.h> 50 #include <sys/stat.h> 51 #include <sys/mount.h> 52 53 #include "fsutil.h" 54 55 static const char *dev = NULL; 56 static int hot = 0; 57 static int preen = 0; 58 59 extern char *__progname; 60 61 static void vmsg(int, const char *, va_list) __printflike(2, 0); 62 63 void 64 setcdevname(const char *cd, int pr) 65 { 66 dev = cd; 67 preen = pr; 68 } 69 70 const char * 71 cdevname(void) 72 { 73 return dev; 74 } 75 76 int 77 hotroot(void) 78 { 79 return hot; 80 } 81 82 /*VARARGS*/ 83 void 84 errexit(const char *fmt, ...) 85 { 86 va_list ap; 87 88 va_start(ap, fmt); 89 vfprintf(stderr, fmt, ap); 90 va_end(ap); 91 exit(8); 92 } 93 94 static void 95 vmsg(int fatal, const char *fmt, va_list ap) 96 { 97 if (!fatal && preen) 98 printf("%s: ", dev); 99 100 vprintf(fmt, ap); 101 102 if (fatal && preen) 103 printf("\n"); 104 105 if (fatal && preen) { 106 printf( 107 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", 108 dev, __progname); 109 exit(8); 110 } 111 } 112 113 /*VARARGS*/ 114 void 115 pfatal(const char *fmt, ...) 116 { 117 va_list ap; 118 119 va_start(ap, fmt); 120 vmsg(1, fmt, ap); 121 va_end(ap); 122 } 123 124 /*VARARGS*/ 125 void 126 pwarn(const char *fmt, ...) 127 { 128 va_list ap; 129 va_start(ap, fmt); 130 vmsg(0, fmt, ap); 131 va_end(ap); 132 } 133 134 void 135 perror(const char *s) 136 { 137 pfatal("%s (%s)", s, strerror(errno)); 138 } 139 140 void 141 panic(const char *fmt, ...) 142 { 143 va_list ap; 144 145 va_start(ap, fmt); 146 vmsg(1, fmt, ap); 147 va_end(ap); 148 exit(8); 149 } 150 151 const char * 152 unrawname(const char *name) 153 { 154 static char unrawbuf[32]; 155 const char *dp; 156 struct stat stb; 157 158 if ((dp = strrchr(name, '/')) == 0) 159 return (name); 160 if (stat(name, &stb) < 0) 161 return (name); 162 if (!S_ISCHR(stb.st_mode)) 163 return (name); 164 if (dp[1] != 'r') 165 return (name); 166 snprintf(unrawbuf, 32, "%.*s/%s", (int)(dp - name), name, dp + 2); 167 return (unrawbuf); 168 } 169 170 const char * 171 rawname(const char *name) 172 { 173 static char rawbuf[32]; 174 const char *dp; 175 176 if ((dp = strrchr(name, '/')) == 0) 177 return (0); 178 snprintf(rawbuf, 32, "%.*s/r%s", (int)(dp - name), name, dp + 1); 179 return (rawbuf); 180 } 181 182 const char * 183 devcheck(const char *origname) 184 { 185 struct stat stslash, stchar; 186 187 if (stat("/", &stslash) < 0) { 188 perror("/"); 189 printf("Can't stat root\n"); 190 return (origname); 191 } 192 if (stat(origname, &stchar) < 0) { 193 perror(origname); 194 printf("Can't stat %s\n", origname); 195 return (origname); 196 } 197 if (!S_ISCHR(stchar.st_mode)) { 198 perror(origname); 199 printf("%s is not a char device\n", origname); 200 } 201 return (origname); 202 } 203 204 /* 205 * Get the mount point information for name. 206 */ 207 struct statfs * 208 getmntpt(const char *name) 209 { 210 struct stat devstat, mntdevstat; 211 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 212 char *devname; 213 struct statfs *mntbuf, *statfsp; 214 int i, mntsize, isdev; 215 216 if (stat(name, &devstat) != 0) 217 return (NULL); 218 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 219 isdev = 1; 220 else 221 isdev = 0; 222 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 223 for (i = 0; i < mntsize; i++) { 224 statfsp = &mntbuf[i]; 225 devname = statfsp->f_mntfromname; 226 if (*devname != '/') { 227 strcpy(device, _PATH_DEV); 228 strcat(device, devname); 229 strcpy(statfsp->f_mntfromname, device); 230 } 231 if (isdev == 0) { 232 if (strcmp(name, statfsp->f_mntonname)) 233 continue; 234 return (statfsp); 235 } 236 if (stat(devname, &mntdevstat) == 0 && 237 mntdevstat.st_rdev == devstat.st_rdev) 238 return (statfsp); 239 } 240 statfsp = NULL; 241 return (statfsp); 242 } 243 244 #if 0 245 /* 246 * XXX this code is from NetBSD, but fails in FreeBSD because we 247 * don't have blockdevs. I don't think its needed. 248 */ 249 const char * 250 blockcheck(const char *origname) 251 { 252 struct stat stslash, stblock, stchar; 253 const char *newname, *raw; 254 struct fstab *fsp; 255 int retried = 0; 256 257 hot = 0; 258 if (stat("/", &stslash) < 0) { 259 perror("/"); 260 printf("Can't stat root\n"); 261 return (origname); 262 } 263 newname = origname; 264 retry: 265 if (stat(newname, &stblock) < 0) { 266 perror(newname); 267 printf("Can't stat %s\n", newname); 268 return (origname); 269 } 270 if (S_ISBLK(stblock.st_mode)) { 271 if (stslash.st_dev == stblock.st_rdev) 272 hot++; 273 raw = rawname(newname); 274 if (stat(raw, &stchar) < 0) { 275 perror(raw); 276 printf("Can't stat %s\n", raw); 277 return (origname); 278 } 279 if (S_ISCHR(stchar.st_mode)) { 280 return (raw); 281 } else { 282 printf("%s is not a character device\n", raw); 283 return (origname); 284 } 285 } else if (S_ISCHR(stblock.st_mode) && !retried) { 286 newname = unrawname(newname); 287 retried++; 288 goto retry; 289 } else if ((fsp = getfsfile(newname)) != 0 && !retried) { 290 newname = fsp->fs_spec; 291 retried++; 292 goto retry; 293 } 294 /* 295 * Not a block or character device, just return name and 296 * let the user decide whether to use it. 297 */ 298 return (origname); 299 } 300 #endif 301 302