fsutil.c revision 330897
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: stable/11/sbin/fsck/fsutil.c 330897 2018-03-14 03:19:51Z eadler $"); 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#include <fstab.h> 47#include <paths.h> 48#include <stdarg.h> 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52 53#include "fsutil.h" 54 55static const char *dev = NULL; 56static int preen = 0; 57 58static void vmsg(int, const char *, va_list) __printflike(2, 0); 59 60void 61setcdevname(const char *cd, int pr) 62{ 63 dev = cd; 64 preen = pr; 65} 66 67const char * 68cdevname(void) 69{ 70 return dev; 71} 72 73static void 74vmsg(int fatal, const char *fmt, va_list ap) 75{ 76 if (!fatal && preen) 77 (void) printf("%s: ", dev); 78 79 (void) vprintf(fmt, ap); 80 81 if (fatal && preen) 82 (void) printf("\n"); 83 84 if (fatal && preen) { 85 (void) printf( 86 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", 87 dev, getprogname()); 88 exit(8); 89 } 90} 91 92/*VARARGS*/ 93void 94pfatal(const char *fmt, ...) 95{ 96 va_list ap; 97 98 va_start(ap, fmt); 99 vmsg(1, fmt, ap); 100 va_end(ap); 101} 102 103/*VARARGS*/ 104void 105pwarn(const char *fmt, ...) 106{ 107 va_list ap; 108 109 va_start(ap, fmt); 110 vmsg(0, fmt, ap); 111 va_end(ap); 112} 113 114void 115perr(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 124void 125panic(const char *fmt, ...) 126{ 127 va_list ap; 128 129 va_start(ap, fmt); 130 vmsg(1, fmt, ap); 131 va_end(ap); 132 exit(8); 133} 134 135const char * 136devcheck(const char *origname) 137{ 138 struct stat stslash, stchar; 139 140 if (stat("/", &stslash) < 0) { 141 perr("Can't stat `/'"); 142 return (origname); 143 } 144 if (stat(origname, &stchar) < 0) { 145 perr("Can't stat %s\n", origname); 146 return (origname); 147 } 148 if (!S_ISCHR(stchar.st_mode)) { 149 perr("%s is not a char device\n", origname); 150 } 151 return (origname); 152} 153 154/* 155 * Get the mount point information for name. 156 */ 157struct statfs * 158getmntpt(const char *name) 159{ 160 struct stat devstat, mntdevstat; 161 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 162 char *dev_name; 163 struct statfs *mntbuf, *statfsp; 164 int i, mntsize, isdev; 165 166 if (stat(name, &devstat) != 0) 167 return (NULL); 168 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 169 isdev = 1; 170 else 171 isdev = 0; 172 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 173 for (i = 0; i < mntsize; i++) { 174 statfsp = &mntbuf[i]; 175 dev_name = statfsp->f_mntfromname; 176 if (*dev_name != '/') { 177 strcpy(device, _PATH_DEV); 178 strcat(device, dev_name); 179 strcpy(statfsp->f_mntfromname, device); 180 } 181 if (isdev == 0) { 182 if (strcmp(name, statfsp->f_mntonname)) 183 continue; 184 return (statfsp); 185 } 186 if (stat(dev_name, &mntdevstat) == 0 && 187 mntdevstat.st_rdev == devstat.st_rdev) 188 return (statfsp); 189 } 190 statfsp = NULL; 191 return (statfsp); 192} 193 194 195void * 196emalloc(size_t s) 197{ 198 void *p; 199 200 p = malloc(s); 201 if (p == NULL) 202 err(1, "malloc failed"); 203 return (p); 204} 205 206 207void * 208erealloc(void *p, size_t s) 209{ 210 void *q; 211 212 q = realloc(p, s); 213 if (q == NULL) 214 err(1, "realloc failed"); 215 return (q); 216} 217 218 219char * 220estrdup(const char *s) 221{ 222 char *p; 223 224 p = strdup(s); 225 if (p == NULL) 226 err(1, "strdup failed"); 227 return (p); 228} 229