summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/getvfsent.c
blob: 13ce57d19f2464222439ee90edcb4509888ba732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

#include	"lint.h"
#include	<mtlib.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<errno.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<sys/vfstab.h>
#include	<string.h>
#include	<thread.h>
#include	<synch.h>
#include	<strings.h>
#include	<libc.h>
#include	"tsd.h"


#define	GETTOK_R(xx, ll, tmp)\
	if ((vp->xx = (char *)strtok_r(ll, sepstr, tmp)) == NULL)\
		return (VFS_TOOFEW);\
	if (strcmp(vp->xx, dash) == 0)\
		vp->xx = NULL
#define	GETTOK(xx, ll)\
	if ((vp->xx = strtok(ll, sepstr)) == NULL)\
		return (VFS_TOOFEW);\
	if (strcmp(vp->xx, dash) == 0)\
		vp->xx = NULL
#define	DIFF(xx)\
	(vrefp->xx != NULL && (vgetp->xx == NULL ||\
	    strcmp(vrefp->xx, vgetp->xx) != 0))
#define	SDIFF(xx, typem, typer)\
	(vgetp->xx == NULL || stat64(vgetp->xx, &statb) == -1 ||\
	(statb.st_mode & S_IFMT) != typem ||\
	    statb.st_rdev != typer)

static const char	sepstr[] = " \t\n";
static const char	dash[] = "-";

static int	getaline(char *, FILE *);

int
getvfsspec(FILE *fd, struct vfstab *vgetp, char *special)
{
	int	ret, bstat;
	mode_t	bmode;
	dev_t	brdev;
	struct stat64	statb;


	if (special && stat64(special, &statb) == 0 &&
	    ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
	    bmode == S_IFCHR)) {
		bstat = 1;
		brdev = statb.st_rdev;
	} else
		bstat = 0;

	while ((ret = getvfsent(fd, vgetp)) == 0 &&
	    ((bstat == 0 &&
	    (special != NULL && (vgetp->vfs_special == NULL ||
	    strcmp(special, vgetp->vfs_special) != 0))) ||
	    (bstat == 1 &&
	    (vgetp->vfs_special == NULL ||
	    stat64(vgetp->vfs_special, &statb) == -1 ||
	    (statb.st_mode & S_IFMT) != bmode ||
	    statb.st_rdev != brdev))))
		;
	return (ret);
}

int
getvfsfile(FILE *fd, struct vfstab *vp, char *mountp)
{
	struct vfstab	vv;

	bzero(&vv, (size_t)sizeof (vv));
	vv.vfs_mountp = mountp;
	return (getvfsany(fd, vp, &vv));
}

int
getvfsany(FILE *fd, struct vfstab *vgetp, struct vfstab *vrefp)
{
	int	ret, bstat, cstat;
	mode_t	bmode, cmode;
	dev_t	brdev, crdev;
	struct stat64	statb;
	off64_t start = ftello64(fd);

	/* Match by straight strcmp */
	while ((ret = getvfsent(fd, vgetp)) == 0 &&
	    (DIFF(vfs_special) || DIFF(vfs_fsckdev) ||
	    DIFF(vfs_mountp) ||
	    DIFF(vfs_fstype) ||
	    DIFF(vfs_fsckpass) ||
	    DIFF(vfs_automnt) ||
	    DIFF(vfs_mntopts)))
		;

	/* If something other than EOF, return it */
	if (ret != -1)
		return (ret);

	/*
	 * Go back to the original location in the file and try to
	 * match the devices by doing stat's (retains compatibility
	 * with original getvfsany).
	 */
	(void) fseeko64(fd, start, SEEK_SET);

	if (vrefp->vfs_special && stat64(vrefp->vfs_special, &statb) == 0 &&
	    ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
	    bmode == S_IFCHR)) {
		bstat = 1;
		brdev = statb.st_rdev;
	} else
		bstat = 0;

	if (vrefp->vfs_fsckdev && stat64(vrefp->vfs_fsckdev, &statb) == 0 &&
	    ((cmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
	    cmode == S_IFCHR)) {
		cstat = 1;
		crdev = statb.st_rdev;
	} else
		cstat = 0;

	while ((ret = getvfsent(fd, vgetp)) == 0 &&
	    ((bstat == 0 && DIFF(vfs_special)) ||
	    (bstat == 1 && SDIFF(vfs_special, bmode, brdev)) ||
	    (cstat == 0 && DIFF(vfs_fsckdev)) ||
	    (cstat == 1 && SDIFF(vfs_fsckdev, cmode, crdev)) ||
	    DIFF(vfs_mountp) ||
	    DIFF(vfs_fstype) ||
	    DIFF(vfs_fsckpass) ||
	    DIFF(vfs_automnt) ||
	    DIFF(vfs_mntopts)))
		;
	return (ret);
}

int
getvfsent(FILE *fd, struct vfstab *vp)
{
	int	ret;
	char	*tmp, *line;

	line = tsdalloc(_T_GETVFSENT, VFS_LINE_MAX, NULL);
	if (line == NULL)
		return (0);

	/* skip leading spaces and comments */
	if ((ret = getaline(line, fd)) != 0)
		return (ret);

	/* split up each field */
	GETTOK_R(vfs_special, line, &tmp);
	GETTOK_R(vfs_fsckdev, NULL, &tmp);
	GETTOK_R(vfs_mountp, NULL, &tmp);
	GETTOK_R(vfs_fstype, NULL, &tmp);
	GETTOK_R(vfs_fsckpass, NULL, &tmp);
	GETTOK_R(vfs_automnt, NULL, &tmp);
	GETTOK_R(vfs_mntopts, NULL, &tmp);

	/* check for too many fields */
	if (strtok_r(NULL, sepstr, &tmp) != NULL)
		return (VFS_TOOMANY);

	return (0);
}

static int
getaline(char *lp, FILE *fd)
{
	char	*cp;

	while ((lp = fgets(lp, VFS_LINE_MAX, fd)) != NULL) {
		if (strlen(lp) == VFS_LINE_MAX-1 && lp[VFS_LINE_MAX-2] != '\n')
			return (VFS_TOOLONG);

		for (cp = lp; *cp == ' ' || *cp == '\t'; cp++)
			;

		if (*cp != '#' && *cp != '\n')
			return (0);
	}
	return (-1);
}