xref: /illumos-gate/usr/src/lib/libc/port/gen/getvfsent.c (revision 6a634c9d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297257d1b4Sraf #include	"lint.h"
307c478bd9Sstevel@tonic-gate #include	<mtlib.h>
317c478bd9Sstevel@tonic-gate #include	<stdio.h>
327c478bd9Sstevel@tonic-gate #include	<stdlib.h>
337c478bd9Sstevel@tonic-gate #include	<errno.h>
347c478bd9Sstevel@tonic-gate #include	<sys/types.h>
357c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
367c478bd9Sstevel@tonic-gate #include	<sys/vfstab.h>
377c478bd9Sstevel@tonic-gate #include	<string.h>
387c478bd9Sstevel@tonic-gate #include	<thread.h>
397c478bd9Sstevel@tonic-gate #include	<synch.h>
407c478bd9Sstevel@tonic-gate #include	<strings.h>
417c478bd9Sstevel@tonic-gate #include	<libc.h>
427c478bd9Sstevel@tonic-gate #include	"tsd.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	GETTOK_R(xx, ll, tmp)\
467c478bd9Sstevel@tonic-gate 	if ((vp->xx = (char *)strtok_r(ll, sepstr, tmp)) == NULL)\
477c478bd9Sstevel@tonic-gate 		return (VFS_TOOFEW);\
487c478bd9Sstevel@tonic-gate 	if (strcmp(vp->xx, dash) == 0)\
497c478bd9Sstevel@tonic-gate 		vp->xx = NULL
507c478bd9Sstevel@tonic-gate #define	GETTOK(xx, ll)\
517c478bd9Sstevel@tonic-gate 	if ((vp->xx = strtok(ll, sepstr)) == NULL)\
527c478bd9Sstevel@tonic-gate 		return (VFS_TOOFEW);\
537c478bd9Sstevel@tonic-gate 	if (strcmp(vp->xx, dash) == 0)\
547c478bd9Sstevel@tonic-gate 		vp->xx = NULL
557c478bd9Sstevel@tonic-gate #define	DIFF(xx)\
567c478bd9Sstevel@tonic-gate 	(vrefp->xx != NULL && (vgetp->xx == NULL ||\
577c478bd9Sstevel@tonic-gate 	    strcmp(vrefp->xx, vgetp->xx) != 0))
587c478bd9Sstevel@tonic-gate #define	SDIFF(xx, typem, typer)\
597c478bd9Sstevel@tonic-gate 	(vgetp->xx == NULL || stat64(vgetp->xx, &statb) == -1 ||\
607c478bd9Sstevel@tonic-gate 	(statb.st_mode & S_IFMT) != typem ||\
617c478bd9Sstevel@tonic-gate 	    statb.st_rdev != typer)
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static const char	sepstr[] = " \t\n";
647c478bd9Sstevel@tonic-gate static const char	dash[] = "-";
657c478bd9Sstevel@tonic-gate 
66*23a1cceaSRoger A. Faulkner static int	getaline(char *, FILE *);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
getvfsspec(FILE * fd,struct vfstab * vgetp,char * special)697c478bd9Sstevel@tonic-gate getvfsspec(FILE *fd, struct vfstab *vgetp, char *special)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	int	ret, bstat;
727c478bd9Sstevel@tonic-gate 	mode_t	bmode;
737c478bd9Sstevel@tonic-gate 	dev_t	brdev;
747c478bd9Sstevel@tonic-gate 	struct stat64	statb;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (special && stat64(special, &statb) == 0 &&
78*23a1cceaSRoger A. Faulkner 	    ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
79*23a1cceaSRoger A. Faulkner 	    bmode == S_IFCHR)) {
807c478bd9Sstevel@tonic-gate 		bstat = 1;
817c478bd9Sstevel@tonic-gate 		brdev = statb.st_rdev;
827c478bd9Sstevel@tonic-gate 	} else
837c478bd9Sstevel@tonic-gate 		bstat = 0;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	while ((ret = getvfsent(fd, vgetp)) == 0 &&
867c478bd9Sstevel@tonic-gate 	    ((bstat == 0 &&
877c478bd9Sstevel@tonic-gate 	    (special != NULL && (vgetp->vfs_special == NULL ||
887c478bd9Sstevel@tonic-gate 	    strcmp(special, vgetp->vfs_special) != 0))) ||
897c478bd9Sstevel@tonic-gate 	    (bstat == 1 &&
907c478bd9Sstevel@tonic-gate 	    (vgetp->vfs_special == NULL ||
917c478bd9Sstevel@tonic-gate 	    stat64(vgetp->vfs_special, &statb) == -1 ||
927c478bd9Sstevel@tonic-gate 	    (statb.st_mode & S_IFMT) != bmode ||
937c478bd9Sstevel@tonic-gate 	    statb.st_rdev != brdev))))
947c478bd9Sstevel@tonic-gate 		;
957c478bd9Sstevel@tonic-gate 	return (ret);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate int
getvfsfile(FILE * fd,struct vfstab * vp,char * mountp)997c478bd9Sstevel@tonic-gate getvfsfile(FILE *fd, struct vfstab *vp, char *mountp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	struct vfstab	vv;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	bzero(&vv, (size_t)sizeof (vv));
1047c478bd9Sstevel@tonic-gate 	vv.vfs_mountp = mountp;
1057c478bd9Sstevel@tonic-gate 	return (getvfsany(fd, vp, &vv));
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int
getvfsany(FILE * fd,struct vfstab * vgetp,struct vfstab * vrefp)1097c478bd9Sstevel@tonic-gate getvfsany(FILE *fd, struct vfstab *vgetp, struct vfstab *vrefp)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	int	ret, bstat, cstat;
1127c478bd9Sstevel@tonic-gate 	mode_t	bmode, cmode;
1137c478bd9Sstevel@tonic-gate 	dev_t	brdev, crdev;
1147c478bd9Sstevel@tonic-gate 	struct stat64	statb;
1157c478bd9Sstevel@tonic-gate 	off64_t start = ftello64(fd);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* Match by straight strcmp */
1187c478bd9Sstevel@tonic-gate 	while ((ret = getvfsent(fd, vgetp)) == 0 &&
119*23a1cceaSRoger A. Faulkner 	    (DIFF(vfs_special) || DIFF(vfs_fsckdev) ||
120*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_mountp) ||
121*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_fstype) ||
122*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_fsckpass) ||
123*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_automnt) ||
124*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_mntopts)))
1257c478bd9Sstevel@tonic-gate 		;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* If something other than EOF, return it */
1287c478bd9Sstevel@tonic-gate 	if (ret != -1)
1297c478bd9Sstevel@tonic-gate 		return (ret);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Go back to the original location in the file and try to
1337c478bd9Sstevel@tonic-gate 	 * match the devices by doing stat's (retains compatibility
1347c478bd9Sstevel@tonic-gate 	 * with original getvfsany).
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	(void) fseeko64(fd, start, SEEK_SET);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (vrefp->vfs_special && stat64(vrefp->vfs_special, &statb) == 0 &&
139*23a1cceaSRoger A. Faulkner 	    ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
140*23a1cceaSRoger A. Faulkner 	    bmode == S_IFCHR)) {
1417c478bd9Sstevel@tonic-gate 		bstat = 1;
1427c478bd9Sstevel@tonic-gate 		brdev = statb.st_rdev;
1437c478bd9Sstevel@tonic-gate 	} else
1447c478bd9Sstevel@tonic-gate 		bstat = 0;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (vrefp->vfs_fsckdev && stat64(vrefp->vfs_fsckdev, &statb) == 0 &&
147*23a1cceaSRoger A. Faulkner 	    ((cmode = (statb.st_mode & S_IFMT)) == S_IFBLK ||
148*23a1cceaSRoger A. Faulkner 	    cmode == S_IFCHR)) {
1497c478bd9Sstevel@tonic-gate 		cstat = 1;
1507c478bd9Sstevel@tonic-gate 		crdev = statb.st_rdev;
1517c478bd9Sstevel@tonic-gate 	} else
1527c478bd9Sstevel@tonic-gate 		cstat = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	while ((ret = getvfsent(fd, vgetp)) == 0 &&
155*23a1cceaSRoger A. Faulkner 	    ((bstat == 0 && DIFF(vfs_special)) ||
156*23a1cceaSRoger A. Faulkner 	    (bstat == 1 && SDIFF(vfs_special, bmode, brdev)) ||
157*23a1cceaSRoger A. Faulkner 	    (cstat == 0 && DIFF(vfs_fsckdev)) ||
158*23a1cceaSRoger A. Faulkner 	    (cstat == 1 && SDIFF(vfs_fsckdev, cmode, crdev)) ||
159*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_mountp) ||
160*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_fstype) ||
161*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_fsckpass) ||
162*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_automnt) ||
163*23a1cceaSRoger A. Faulkner 	    DIFF(vfs_mntopts)))
1647c478bd9Sstevel@tonic-gate 		;
1657c478bd9Sstevel@tonic-gate 	return (ret);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
getvfsent(FILE * fd,struct vfstab * vp)1697c478bd9Sstevel@tonic-gate getvfsent(FILE *fd, struct vfstab *vp)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	int	ret;
1727c478bd9Sstevel@tonic-gate 	char	*tmp, *line;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	line = tsdalloc(_T_GETVFSENT, VFS_LINE_MAX, NULL);
1757c478bd9Sstevel@tonic-gate 	if (line == NULL)
1767c478bd9Sstevel@tonic-gate 		return (0);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* skip leading spaces and comments */
179*23a1cceaSRoger A. Faulkner 	if ((ret = getaline(line, fd)) != 0)
1807c478bd9Sstevel@tonic-gate 		return (ret);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* split up each field */
1837c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_special, line, &tmp);
1847c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_fsckdev, NULL, &tmp);
1857c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_mountp, NULL, &tmp);
1867c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_fstype, NULL, &tmp);
1877c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_fsckpass, NULL, &tmp);
1887c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_automnt, NULL, &tmp);
1897c478bd9Sstevel@tonic-gate 	GETTOK_R(vfs_mntopts, NULL, &tmp);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* check for too many fields */
1927c478bd9Sstevel@tonic-gate 	if (strtok_r(NULL, sepstr, &tmp) != NULL)
1937c478bd9Sstevel@tonic-gate 		return (VFS_TOOMANY);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return (0);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static int
getaline(char * lp,FILE * fd)199*23a1cceaSRoger A. Faulkner getaline(char *lp, FILE *fd)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	char	*cp;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	while ((lp = fgets(lp, VFS_LINE_MAX, fd)) != NULL) {
2047c478bd9Sstevel@tonic-gate 		if (strlen(lp) == VFS_LINE_MAX-1 && lp[VFS_LINE_MAX-2] != '\n')
2057c478bd9Sstevel@tonic-gate 			return (VFS_TOOLONG);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		for (cp = lp; *cp == ' ' || *cp == '\t'; cp++)
2087c478bd9Sstevel@tonic-gate 			;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (*cp != '#' && *cp != '\n')
2117c478bd9Sstevel@tonic-gate 			return (0);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	return (-1);
2147c478bd9Sstevel@tonic-gate }
215