xref: /freebsd/lib/libc/gen/getvfsbyname.c (revision 5965373e69ca208cedd8ede9716ef0d58e6c3434)
108f68ac6SBruce Evans /*
208f68ac6SBruce Evans  * Copyright (c) 1995
308f68ac6SBruce Evans  *	The Regents of the University of California.  All rights reserved.
408f68ac6SBruce Evans  *
508f68ac6SBruce Evans  * Redistribution and use in source and binary forms, with or without
608f68ac6SBruce Evans  * modification, are permitted provided that the following conditions
708f68ac6SBruce Evans  * are met:
808f68ac6SBruce Evans  * 1. Redistributions of source code must retain the above copyright
908f68ac6SBruce Evans  *    notice, this list of conditions and the following disclaimer.
1008f68ac6SBruce Evans  * 2. Redistributions in binary form must reproduce the above copyright
1108f68ac6SBruce Evans  *    notice, this list of conditions and the following disclaimer in the
1208f68ac6SBruce Evans  *    documentation and/or other materials provided with the distribution.
1308f68ac6SBruce Evans  * 3. All advertising materials mentioning features or use of this software
1408f68ac6SBruce Evans  *    must display the following acknowledgement:
1508f68ac6SBruce Evans  *	This product includes software developed by the University of
1608f68ac6SBruce Evans  *	California, Berkeley and its contributors.
1708f68ac6SBruce Evans  * 4. Neither the name of the University nor the names of its contributors
1808f68ac6SBruce Evans  *    may be used to endorse or promote products derived from this software
1908f68ac6SBruce Evans  *    without specific prior written permission.
2008f68ac6SBruce Evans  *
2108f68ac6SBruce Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2208f68ac6SBruce Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2308f68ac6SBruce Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2408f68ac6SBruce Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2508f68ac6SBruce Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2608f68ac6SBruce Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2708f68ac6SBruce Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2808f68ac6SBruce Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2908f68ac6SBruce Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3008f68ac6SBruce Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3108f68ac6SBruce Evans  * SUCH DAMAGE.
3208f68ac6SBruce Evans  */
3308f68ac6SBruce Evans 
34ea8d448aSDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
3508f68ac6SBruce Evans static char sccsid[] = "@(#)kvm_getvfsbyname.c	8.1 (Berkeley) 4/3/95";
36ea8d448aSDavid E. O'Brien #endif /* LIBC_SCCS and not lint */
37ea8d448aSDavid E. O'Brien #include <sys/cdefs.h>
38ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$");
3908f68ac6SBruce Evans 
4008f68ac6SBruce Evans #include <sys/param.h>
4108f68ac6SBruce Evans #include <sys/mount.h>
4208f68ac6SBruce Evans #include <sys/sysctl.h>
4308f68ac6SBruce Evans #include <errno.h>
445965373eSMaxime Henrion #include <stdlib.h>
455965373eSMaxime Henrion #include <string.h>
4608f68ac6SBruce Evans 
4708f68ac6SBruce Evans /*
4808f68ac6SBruce Evans  * Given a filesystem name, determine if it is resident in the kernel,
495965373eSMaxime Henrion  * and if it is resident, return its xvfsconf structure.
5008f68ac6SBruce Evans  */
515965373eSMaxime Henrion int
5208f68ac6SBruce Evans getvfsbyname(fsname, vfcp)
5308f68ac6SBruce Evans 	const char *fsname;
545965373eSMaxime Henrion 	struct xvfsconf *vfcp;
5508f68ac6SBruce Evans {
56ee51c92bSJohn Birrell #ifdef	__NETBSD_SYSCALLS
57ee51c92bSJohn Birrell 	errno = ENOSYS;
58ee51c92bSJohn Birrell #else
595965373eSMaxime Henrion 	struct xvfsconf *xvfsp;
6008f68ac6SBruce Evans 	size_t buflen;
615965373eSMaxime Henrion 	int cnt, i;
6208f68ac6SBruce Evans 
635965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0)
6408f68ac6SBruce Evans 		return (-1);
655965373eSMaxime Henrion 	xvfsp = malloc(buflen);
665965373eSMaxime Henrion 	if (xvfsp == NULL)
6708f68ac6SBruce Evans 		return (-1);
685965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
695965373eSMaxime Henrion 		free(xvfsp);
705965373eSMaxime Henrion 		return (-1);
7108f68ac6SBruce Evans 	}
725965373eSMaxime Henrion 	cnt = buflen / sizeof(struct xvfsconf);
735965373eSMaxime Henrion 	for (i = 0; i < cnt; i++) {
745965373eSMaxime Henrion 		if (strcmp(fsname, xvfsp[i].vfc_name) == 0) {
755965373eSMaxime Henrion 			memcpy(vfcp, xvfsp + i, sizeof(struct xvfsconf));
765965373eSMaxime Henrion 			free(xvfsp);
7708f68ac6SBruce Evans 			return (0);
7808f68ac6SBruce Evans 		}
795965373eSMaxime Henrion 	}
805965373eSMaxime Henrion 	free(xvfsp);
8108f68ac6SBruce Evans 	errno = ENOENT;
82ee51c92bSJohn Birrell #endif
8308f68ac6SBruce Evans 	return (-1);
8408f68ac6SBruce Evans }
85