xref: /freebsd/lib/libc/gen/getvfsbyname.c (revision 7340012337c572dfce6c28992c4badbde78d9749)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
408f68ac6SBruce Evans  * Copyright (c) 1995
508f68ac6SBruce Evans  *	The Regents of the University of California.  All rights reserved.
608f68ac6SBruce Evans  *
708f68ac6SBruce Evans  * Redistribution and use in source and binary forms, with or without
808f68ac6SBruce Evans  * modification, are permitted provided that the following conditions
908f68ac6SBruce Evans  * are met:
1008f68ac6SBruce Evans  * 1. Redistributions of source code must retain the above copyright
1108f68ac6SBruce Evans  *    notice, this list of conditions and the following disclaimer.
1208f68ac6SBruce Evans  * 2. Redistributions in binary form must reproduce the above copyright
1308f68ac6SBruce Evans  *    notice, this list of conditions and the following disclaimer in the
1408f68ac6SBruce Evans  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1608f68ac6SBruce Evans  *    may be used to endorse or promote products derived from this software
1708f68ac6SBruce Evans  *    without specific prior written permission.
1808f68ac6SBruce Evans  *
1908f68ac6SBruce Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2008f68ac6SBruce Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2108f68ac6SBruce Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2208f68ac6SBruce Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2308f68ac6SBruce Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2408f68ac6SBruce Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2508f68ac6SBruce Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2608f68ac6SBruce Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2708f68ac6SBruce Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2808f68ac6SBruce Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2908f68ac6SBruce Evans  * SUCH DAMAGE.
3008f68ac6SBruce Evans  */
3108f68ac6SBruce Evans 
32ea8d448aSDavid E. O'Brien #include <sys/cdefs.h>
33c1920558SJohn Baldwin __SCCSID("@(#)kvm_getvfsbyname.c	8.1 (Berkeley) 4/3/95");
34ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$");
3508f68ac6SBruce Evans 
3608f68ac6SBruce Evans #include <sys/param.h>
3708f68ac6SBruce Evans #include <sys/mount.h>
3808f68ac6SBruce Evans #include <sys/sysctl.h>
3908f68ac6SBruce Evans #include <errno.h>
40*73400123SAlan Somers #include <stdbool.h>
415965373eSMaxime Henrion #include <stdlib.h>
425965373eSMaxime Henrion #include <string.h>
4308f68ac6SBruce Evans 
4408f68ac6SBruce Evans /*
45*73400123SAlan Somers  * fusefs(5) file systems may have a "subtype" which gets appended to
46*73400123SAlan Somers  * statfs(2)'s f_fstypename field on a per-mount basis.  Allow getvfsbyname to
47*73400123SAlan Somers  * match either the full "fusefs.foobar" or the more general "fusefs".
48*73400123SAlan Somers  */
49*73400123SAlan Somers static bool
50*73400123SAlan Somers are_fusefs(const char *fsname, const char *vfc_name)
51*73400123SAlan Somers {
52*73400123SAlan Somers 	const char fusefs[] = "fusefs";
53*73400123SAlan Somers 	const char fusefs_dot[] = "fusefs.";
54*73400123SAlan Somers 
55*73400123SAlan Somers 	return (strncmp(fsname, fusefs_dot, strlen(fusefs_dot)) == 0 &&
56*73400123SAlan Somers 	    strcmp(fusefs, vfc_name) == 0);
57*73400123SAlan Somers }
58*73400123SAlan Somers 
59*73400123SAlan Somers /*
6008f68ac6SBruce Evans  * Given a filesystem name, determine if it is resident in the kernel,
615965373eSMaxime Henrion  * and if it is resident, return its xvfsconf structure.
6208f68ac6SBruce Evans  */
635965373eSMaxime Henrion int
6455b6b759SCraig Rodrigues getvfsbyname(const char *fsname, struct xvfsconf *vfcp)
6508f68ac6SBruce Evans {
665965373eSMaxime Henrion 	struct xvfsconf *xvfsp;
6708f68ac6SBruce Evans 	size_t buflen;
685965373eSMaxime Henrion 	int cnt, i;
6908f68ac6SBruce Evans 
705965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0)
7108f68ac6SBruce Evans 		return (-1);
725965373eSMaxime Henrion 	xvfsp = malloc(buflen);
735965373eSMaxime Henrion 	if (xvfsp == NULL)
7408f68ac6SBruce Evans 		return (-1);
755965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
765965373eSMaxime Henrion 		free(xvfsp);
775965373eSMaxime Henrion 		return (-1);
7808f68ac6SBruce Evans 	}
795965373eSMaxime Henrion 	cnt = buflen / sizeof(struct xvfsconf);
805965373eSMaxime Henrion 	for (i = 0; i < cnt; i++) {
81*73400123SAlan Somers 		if (strcmp(fsname, xvfsp[i].vfc_name) == 0 ||
82*73400123SAlan Somers 		    are_fusefs(fsname, xvfsp[i].vfc_name))
83*73400123SAlan Somers 		{
845965373eSMaxime Henrion 			memcpy(vfcp, xvfsp + i, sizeof(struct xvfsconf));
855965373eSMaxime Henrion 			free(xvfsp);
8608f68ac6SBruce Evans 			return (0);
8708f68ac6SBruce Evans 		}
885965373eSMaxime Henrion 	}
895965373eSMaxime Henrion 	free(xvfsp);
9008f68ac6SBruce Evans 	errno = ENOENT;
9108f68ac6SBruce Evans 	return (-1);
9208f68ac6SBruce Evans }
93