xref: /titanic_44/usr/src/lib/libc/port/gen/getvfsent.c (revision 4a6ec905b96eb96a398c346f59e034a90ce8ad37)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #pragma weak getvfsspec = _getvfsspec
34 #pragma weak getvfsfile = _getvfsfile
35 #pragma weak getvfsany = _getvfsany
36 #pragma weak getvfsent = _getvfsent
37 
38 #include	"synonyms.h"
39 #include	<mtlib.h>
40 #include	<stdio.h>
41 #include	<stdlib.h>
42 #include	<errno.h>
43 #include	<sys/types.h>
44 #include	<sys/stat.h>
45 #include	<sys/vfstab.h>
46 #include	<string.h>
47 #include	<thread.h>
48 #include	<synch.h>
49 #include	<strings.h>
50 #include	<libc.h>
51 #include	"tsd.h"
52 
53 
54 #define	GETTOK_R(xx, ll, tmp)\
55 	if ((vp->xx = (char *)strtok_r(ll, sepstr, tmp)) == NULL)\
56 		return (VFS_TOOFEW);\
57 	if (strcmp(vp->xx, dash) == 0)\
58 		vp->xx = NULL
59 #define	GETTOK(xx, ll)\
60 	if ((vp->xx = strtok(ll, sepstr)) == NULL)\
61 		return (VFS_TOOFEW);\
62 	if (strcmp(vp->xx, dash) == 0)\
63 		vp->xx = NULL
64 #define	DIFF(xx)\
65 	(vrefp->xx != NULL && (vgetp->xx == NULL ||\
66 	    strcmp(vrefp->xx, vgetp->xx) != 0))
67 #define	SDIFF(xx, typem, typer)\
68 	(vgetp->xx == NULL || stat64(vgetp->xx, &statb) == -1 ||\
69 	(statb.st_mode & S_IFMT) != typem ||\
70 	    statb.st_rdev != typer)
71 
72 static const char	sepstr[] = " \t\n";
73 static const char	dash[] = "-";
74 
75 static int	getline(char *, FILE *);
76 
77 int
78 getvfsspec(FILE *fd, struct vfstab *vgetp, char *special)
79 {
80 	int	ret, bstat;
81 	mode_t	bmode;
82 	dev_t	brdev;
83 	struct stat64	statb;
84 
85 
86 	if (special && stat64(special, &statb) == 0 &&
87 		((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
88 		bmode == S_IFCHR)) {
89 		bstat = 1;
90 		brdev = statb.st_rdev;
91 	} else
92 		bstat = 0;
93 
94 	while ((ret = getvfsent(fd, vgetp)) == 0 &&
95 	    ((bstat == 0 &&
96 	    (special != NULL && (vgetp->vfs_special == NULL ||
97 	    strcmp(special, vgetp->vfs_special) != 0))) ||
98 	    (bstat == 1 &&
99 	    (vgetp->vfs_special == NULL ||
100 	    stat64(vgetp->vfs_special, &statb) == -1 ||
101 	    (statb.st_mode & S_IFMT) != bmode ||
102 	    statb.st_rdev != brdev))))
103 		;
104 	return (ret);
105 }
106 
107 int
108 getvfsfile(FILE *fd, struct vfstab *vp, char *mountp)
109 {
110 	struct vfstab	vv;
111 
112 	bzero(&vv, (size_t)sizeof (vv));
113 	vv.vfs_mountp = mountp;
114 	return (getvfsany(fd, vp, &vv));
115 }
116 
117 int
118 getvfsany(FILE *fd, struct vfstab *vgetp, struct vfstab *vrefp)
119 {
120 	int	ret, bstat, cstat;
121 	mode_t	bmode, cmode;
122 	dev_t	brdev, crdev;
123 	struct stat64	statb;
124 	off64_t start = ftello64(fd);
125 
126 	/* Match by straight strcmp */
127 	while ((ret = getvfsent(fd, vgetp)) == 0 &&
128 		(DIFF(vfs_special) || DIFF(vfs_fsckdev) ||
129 		DIFF(vfs_mountp) ||
130 		DIFF(vfs_fstype) ||
131 		DIFF(vfs_fsckpass) ||
132 		DIFF(vfs_automnt) ||
133 		DIFF(vfs_mntopts)))
134 		;
135 
136 	/* If something other than EOF, return it */
137 	if (ret != -1)
138 		return (ret);
139 
140 	/*
141 	 * Go back to the original location in the file and try to
142 	 * match the devices by doing stat's (retains compatibility
143 	 * with original getvfsany).
144 	 */
145 	(void) fseeko64(fd, start, SEEK_SET);
146 
147 	if (vrefp->vfs_special && stat64(vrefp->vfs_special, &statb) == 0 &&
148 		((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
149 		bmode == S_IFCHR)) {
150 		bstat = 1;
151 		brdev = statb.st_rdev;
152 	} else
153 		bstat = 0;
154 
155 	if (vrefp->vfs_fsckdev && stat64(vrefp->vfs_fsckdev, &statb) == 0 &&
156 		((cmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
157 		cmode == S_IFCHR)) {
158 		cstat = 1;
159 		crdev = statb.st_rdev;
160 	} else
161 		cstat = 0;
162 
163 	while ((ret = getvfsent(fd, vgetp)) == 0 &&
164 		((bstat == 0 && DIFF(vfs_special)) ||
165 		(bstat == 1 && SDIFF(vfs_special, bmode, brdev)) ||
166 		(cstat == 0 && DIFF(vfs_fsckdev)) ||
167 		(cstat == 1 && SDIFF(vfs_fsckdev, cmode, crdev)) ||
168 		DIFF(vfs_mountp) ||
169 		DIFF(vfs_fstype) ||
170 		DIFF(vfs_fsckpass) ||
171 		DIFF(vfs_automnt) ||
172 		DIFF(vfs_mntopts)))
173 		;
174 	return (ret);
175 }
176 
177 int
178 getvfsent(FILE *fd, struct vfstab *vp)
179 {
180 	int	ret;
181 	char	*tmp, *line;
182 
183 	line = tsdalloc(_T_GETVFSENT, VFS_LINE_MAX, NULL);
184 	if (line == NULL)
185 		return (0);
186 
187 	/* skip leading spaces and comments */
188 	if ((ret = getline(line, fd)) != 0)
189 		return (ret);
190 
191 	/* split up each field */
192 	GETTOK_R(vfs_special, line, &tmp);
193 	GETTOK_R(vfs_fsckdev, NULL, &tmp);
194 	GETTOK_R(vfs_mountp, NULL, &tmp);
195 	GETTOK_R(vfs_fstype, NULL, &tmp);
196 	GETTOK_R(vfs_fsckpass, NULL, &tmp);
197 	GETTOK_R(vfs_automnt, NULL, &tmp);
198 	GETTOK_R(vfs_mntopts, NULL, &tmp);
199 
200 	/* check for too many fields */
201 	if (strtok_r(NULL, sepstr, &tmp) != NULL)
202 		return (VFS_TOOMANY);
203 
204 	return (0);
205 }
206 
207 static int
208 getline(char *lp, FILE *fd)
209 {
210 	char	*cp;
211 
212 	while ((lp = fgets(lp, VFS_LINE_MAX, fd)) != NULL) {
213 		if (strlen(lp) == VFS_LINE_MAX-1 && lp[VFS_LINE_MAX-2] != '\n')
214 			return (VFS_TOOLONG);
215 
216 		for (cp = lp; *cp == ' ' || *cp == '\t'; cp++)
217 			;
218 
219 		if (*cp != '#' && *cp != '\n')
220 			return (0);
221 	}
222 	return (-1);
223 }
224