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