xref: /freebsd/lib/libc/gen/fstab.c (revision 63f537551380d2dab29fa402ad1269feae17e594)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1988, 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)fstab.c	8.1 (Berkeley) 6/4/93");
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 
39 #include <errno.h>
40 #include <fstab.h>
41 #include <paths.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <vis.h>
47 #include "un-namespace.h"
48 
49 static FILE *_fs_fp;
50 static struct fstab _fs_fstab;
51 static int LineNo = 0;
52 static char *path_fstab;
53 static char fstab_path[PATH_MAX];
54 static int fsp_set = 0;
55 
56 static void error(int);
57 static void fixfsfile(void);
58 static int fstabscan(void);
59 
60 void
61 setfstab(const char *file)
62 {
63 
64 	if (file == NULL) {
65 		path_fstab = _PATH_FSTAB;
66 	} else {
67 		strncpy(fstab_path, file, PATH_MAX);
68 		fstab_path[PATH_MAX - 1] = '\0';
69 		path_fstab = fstab_path;
70 	}
71 	fsp_set = 1;
72 
73 	return;
74 }
75 
76 const char *
77 getfstab(void)
78 {
79 
80 	if (fsp_set)
81 		return (path_fstab);
82 	else
83 		return (_PATH_FSTAB);
84 }
85 
86 static void
87 fixfsfile(void)
88 {
89 	static char buf[sizeof(_PATH_DEV) + MNAMELEN];
90 	struct stat sb;
91 	struct statfs sf;
92 
93 	if (_fs_fstab.fs_file != NULL && strcmp(_fs_fstab.fs_file, "/") != 0)
94 		return;
95 	if (statfs("/", &sf) != 0)
96 		return;
97 	if (sf.f_mntfromname[0] == '/')
98 		buf[0] = '\0';
99 	else
100 		strcpy(buf, _PATH_DEV);
101 	strcat(buf, sf.f_mntfromname);
102 	if (stat(buf, &sb) != 0 ||
103 	    (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)))
104 		return;
105 	_fs_fstab.fs_spec = buf;
106 }
107 
108 static int
109 fstabscan(void)
110 {
111 	char *cp, *p;
112 #define	MAXLINELENGTH	1024
113 	static char line[MAXLINELENGTH];
114 	char subline[MAXLINELENGTH];
115 	int typexx;
116 
117 	for (;;) {
118 
119 		if (!(p = fgets(line, sizeof(line), _fs_fp)))
120 			return (0);
121 /* OLD_STYLE_FSTAB */
122 		++LineNo;
123 		if (*line == '#' || *line == '\n')
124 			continue;
125 		if (!strpbrk(p, " \t")) {
126 			_fs_fstab.fs_spec = strsep(&p, ":\n");
127 			_fs_fstab.fs_file = strsep(&p, ":\n");
128 			fixfsfile();
129 			_fs_fstab.fs_type = strsep(&p, ":\n");
130 			if (_fs_fstab.fs_type) {
131 				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
132 					continue;
133 				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
134 				_fs_fstab.fs_vfstype =
135 				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
136 				    "ufs" : "swap";
137 				if ((cp = strsep(&p, ":\n")) != NULL) {
138 					_fs_fstab.fs_freq = atoi(cp);
139 					if ((cp = strsep(&p, ":\n")) != NULL) {
140 						_fs_fstab.fs_passno = atoi(cp);
141 						return (1);
142 					}
143 				}
144 			}
145 			goto bad;
146 		}
147 /* OLD_STYLE_FSTAB */
148 		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
149 			;
150 		_fs_fstab.fs_spec = cp;
151 		if (_fs_fstab.fs_spec == NULL || *_fs_fstab.fs_spec == '#')
152 			continue;
153 		if (strunvis(_fs_fstab.fs_spec, _fs_fstab.fs_spec) < 0)
154 			goto bad;
155 		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
156 			;
157 		_fs_fstab.fs_file = cp;
158 		if (_fs_fstab.fs_file == NULL)
159 			goto bad;
160 		if (strunvis(_fs_fstab.fs_file, _fs_fstab.fs_file) < 0)
161 			goto bad;
162 		fixfsfile();
163 		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
164 			;
165 		_fs_fstab.fs_vfstype = cp;
166 		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
167 			;
168 		_fs_fstab.fs_mntops = cp;
169 		if (_fs_fstab.fs_mntops == NULL)
170 			goto bad;
171 		_fs_fstab.fs_freq = 0;
172 		_fs_fstab.fs_passno = 0;
173 		while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
174 			;
175 		if (cp != NULL) {
176 			_fs_fstab.fs_freq = atoi(cp);
177 			while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
178 				;
179 			if (cp != NULL)
180 				_fs_fstab.fs_passno = atoi(cp);
181 		}
182 		(void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline));
183 		p = subline;
184 		for (typexx = 0, cp = strsep(&p, ","); cp;
185 		     cp = strsep(&p, ",")) {
186 			if (strlen(cp) != 2)
187 				continue;
188 			if (!strcmp(cp, FSTAB_RW)) {
189 				_fs_fstab.fs_type = FSTAB_RW;
190 				break;
191 			}
192 			if (!strcmp(cp, FSTAB_RQ)) {
193 				_fs_fstab.fs_type = FSTAB_RQ;
194 				break;
195 			}
196 			if (!strcmp(cp, FSTAB_RO)) {
197 				_fs_fstab.fs_type = FSTAB_RO;
198 				break;
199 			}
200 			if (!strcmp(cp, FSTAB_SW)) {
201 				_fs_fstab.fs_type = FSTAB_SW;
202 				break;
203 			}
204 			if (!strcmp(cp, FSTAB_XX)) {
205 				_fs_fstab.fs_type = FSTAB_XX;
206 				typexx++;
207 				break;
208 			}
209 		}
210 		if (typexx)
211 			continue;
212 		if (cp != NULL)
213 			return (1);
214 
215 bad:		/* no way to distinguish between EOF and syntax error */
216 		error(EFTYPE);
217 	}
218 	/* NOTREACHED */
219 }
220 
221 struct fstab *
222 getfsent(void)
223 {
224 
225 	if ((!_fs_fp && !setfsent()) || !fstabscan())
226 		return (NULL);
227 	return (&_fs_fstab);
228 }
229 
230 struct fstab *
231 getfsspec(const char *name)
232 {
233 
234 	if (setfsent())
235 		while (fstabscan())
236 			if (!strcmp(_fs_fstab.fs_spec, name))
237 				return (&_fs_fstab);
238 	return (NULL);
239 }
240 
241 struct fstab *
242 getfsfile(const char *name)
243 {
244 
245 	if (setfsent())
246 		while (fstabscan())
247 			if (!strcmp(_fs_fstab.fs_file, name))
248 				return (&_fs_fstab);
249 	return (NULL);
250 }
251 
252 int
253 setfsent(void)
254 {
255 	if (_fs_fp) {
256 		rewind(_fs_fp);
257 		LineNo = 0;
258 		return (1);
259 	}
260 	if (fsp_set == 0)
261 		setfstab(secure_getenv("PATH_FSTAB"));
262 	if ((_fs_fp = fopen(path_fstab, "re")) != NULL) {
263 		LineNo = 0;
264 		return (1);
265 	}
266 	error(errno);
267 	return (0);
268 }
269 
270 void
271 endfsent(void)
272 {
273 
274 	if (_fs_fp) {
275 		(void)fclose(_fs_fp);
276 		_fs_fp = NULL;
277 	}
278 
279 	fsp_set = 0;
280 }
281 
282 static void
283 error(int err)
284 {
285 	char *p;
286 	char num[30];
287 
288 	(void)_write(STDERR_FILENO, "fstab: ", 7);
289 	(void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
290 	(void)_write(STDERR_FILENO, ":", 1);
291 	sprintf(num, "%d: ", LineNo);
292 	(void)_write(STDERR_FILENO, num, strlen(num));
293 	p = strerror(err);
294 	(void)_write(STDERR_FILENO, p, strlen(p));
295 	(void)_write(STDERR_FILENO, "\n", 1);
296 }
297