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