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 <fstab.h> 46 #include <paths.h> 47 #include <stdarg.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 52 #include "fsutil.h" 53 54 static const char *dev = NULL; 55 static int preen = 0; 56 57 static void vmsg(int, const char *, va_list) __printflike(2, 0); 58 59 /* 60 * The getfsopt() function checks whether an option is present in 61 * an fstab(5) fs_mntops entry. There are six possible cases: 62 * 63 * fs_mntops getfsopt result 64 * rw,foo foo true 65 * rw,nofoo nofoo true 66 * rw,nofoo foo false 67 * rw,foo nofoo false 68 * rw foo false 69 * rw nofoo false 70 * 71 * This function should be part of and documented in getfsent(3). 72 */ 73 int 74 getfsopt(struct fstab *fs, const char *option) 75 { 76 int negative, found; 77 char *opt, *optbuf; 78 79 if (option[0] == 'n' && option[1] == 'o') { 80 negative = 1; 81 option += 2; 82 } else 83 negative = 0; 84 optbuf = strdup(fs->fs_mntops); 85 found = 0; 86 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 87 if (opt[0] == 'n' && opt[1] == 'o') { 88 if (!strcasecmp(opt + 2, option)) 89 found = negative; 90 } else if (!strcasecmp(opt, option)) 91 found = !negative; 92 } 93 free(optbuf); 94 return (found); 95 } 96 97 void 98 setcdevname(const char *cd, int pr) 99 { 100 dev = cd; 101 preen = pr; 102 } 103 104 const char * 105 cdevname(void) 106 { 107 return dev; 108 } 109 110 static void 111 vmsg(int fatal, const char *fmt, va_list ap) 112 { 113 if (!fatal && preen) 114 (void) printf("%s: ", dev); 115 116 (void) vprintf(fmt, ap); 117 118 if (fatal && preen) 119 (void) printf("\n"); 120 121 if (fatal && preen) { 122 (void) printf( 123 "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n", 124 dev, getprogname()); 125 exit(8); 126 } 127 } 128 129 /*VARARGS*/ 130 void 131 pfatal(const char *fmt, ...) 132 { 133 va_list ap; 134 135 va_start(ap, fmt); 136 vmsg(1, fmt, ap); 137 va_end(ap); 138 } 139 140 /*VARARGS*/ 141 void 142 pwarn(const char *fmt, ...) 143 { 144 va_list ap; 145 146 va_start(ap, fmt); 147 vmsg(0, fmt, ap); 148 va_end(ap); 149 } 150 151 void 152 perr(const char *fmt, ...) 153 { 154 va_list ap; 155 156 va_start(ap, fmt); 157 vmsg(1, fmt, ap); 158 va_end(ap); 159 } 160 161 void 162 panic(const char *fmt, ...) 163 { 164 va_list ap; 165 166 va_start(ap, fmt); 167 vmsg(1, fmt, ap); 168 va_end(ap); 169 exit(8); 170 } 171 172 const char * 173 devcheck(const char *origname) 174 { 175 struct stat stslash, stchar; 176 177 if (stat("/", &stslash) < 0) { 178 perr("Can't stat `/'"); 179 return (origname); 180 } 181 if (stat(origname, &stchar) < 0) { 182 perr("Can't stat %s\n", origname); 183 return (origname); 184 } 185 if (!S_ISCHR(stchar.st_mode)) { 186 perr("%s is not a char device\n", origname); 187 } 188 return (origname); 189 } 190 191 /* 192 * Get the mount point information for name. 193 */ 194 struct statfs * 195 getmntpt(const char *name) 196 { 197 struct stat devstat, mntdevstat; 198 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 199 char *dev_name; 200 struct statfs *mntbuf, *statfsp; 201 int i, mntsize, isdev; 202 203 if (stat(name, &devstat) != 0) 204 return (NULL); 205 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 206 isdev = 1; 207 else 208 isdev = 0; 209 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 210 for (i = 0; i < mntsize; i++) { 211 statfsp = &mntbuf[i]; 212 dev_name = statfsp->f_mntfromname; 213 if (*dev_name != '/') { 214 if (strlen(_PATH_DEV) + strlen(dev_name) + 1 > 215 sizeof(statfsp->f_mntfromname)) 216 continue; 217 strcpy(device, _PATH_DEV); 218 strcat(device, dev_name); 219 strcpy(statfsp->f_mntfromname, device); 220 } 221 if (isdev == 0) { 222 if (strcmp(name, statfsp->f_mntonname)) 223 continue; 224 return (statfsp); 225 } 226 if (stat(dev_name, &mntdevstat) == 0 && 227 mntdevstat.st_rdev == devstat.st_rdev) 228 return (statfsp); 229 } 230 statfsp = NULL; 231 return (statfsp); 232 } 233 234 235 void * 236 emalloc(size_t s) 237 { 238 void *p; 239 240 p = malloc(s); 241 if (p == NULL) 242 err(1, "malloc failed"); 243 return (p); 244 } 245 246 247 void * 248 erealloc(void *p, size_t s) 249 { 250 void *q; 251 252 q = realloc(p, s); 253 if (q == NULL) 254 err(1, "realloc failed"); 255 return (q); 256 } 257 258 259 char * 260 estrdup(const char *s) 261 { 262 char *p; 263 264 p = strdup(s); 265 if (p == NULL) 266 err(1, "strdup failed"); 267 return (p); 268 } 269