xref: /freebsd/lib/libc/gen/getvfsbyname.c (revision 088cc7d221bb0743fc5ec12de983559b812366bd)
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 
3208f68ac6SBruce Evans #include <sys/param.h>
3308f68ac6SBruce Evans #include <sys/mount.h>
3408f68ac6SBruce Evans #include <sys/sysctl.h>
3508f68ac6SBruce Evans #include <errno.h>
3673400123SAlan Somers #include <stdbool.h>
375965373eSMaxime Henrion #include <stdlib.h>
385965373eSMaxime Henrion #include <string.h>
3908f68ac6SBruce Evans 
4008f68ac6SBruce Evans /*
41*088cc7d2SAlexander Ziaee  * fusefs(4) file systems may have a "subtype" which gets appended to
4273400123SAlan Somers  * statfs(2)'s f_fstypename field on a per-mount basis.  Allow getvfsbyname to
4373400123SAlan Somers  * match either the full "fusefs.foobar" or the more general "fusefs".
4473400123SAlan Somers  */
4573400123SAlan Somers static bool
are_fusefs(const char * fsname,const char * vfc_name)4673400123SAlan Somers are_fusefs(const char *fsname, const char *vfc_name)
4773400123SAlan Somers {
48d5fce87dSAlan Somers 	const static char fusefs[] = "fusefs";
49d5fce87dSAlan Somers 	const static char fusefs_dot[] = "fusefs.";
5073400123SAlan Somers 
51d5fce87dSAlan Somers 	return (strncmp(fsname, fusefs_dot, sizeof(fusefs_dot) - 1) == 0 &&
5273400123SAlan Somers 	    strcmp(fusefs, vfc_name) == 0);
5373400123SAlan Somers }
5473400123SAlan Somers 
5573400123SAlan Somers /*
5608f68ac6SBruce Evans  * Given a filesystem name, determine if it is resident in the kernel,
575965373eSMaxime Henrion  * and if it is resident, return its xvfsconf structure.
5808f68ac6SBruce Evans  */
595965373eSMaxime Henrion int
getvfsbyname(const char * fsname,struct xvfsconf * vfcp)6055b6b759SCraig Rodrigues getvfsbyname(const char *fsname, struct xvfsconf *vfcp)
6108f68ac6SBruce Evans {
625965373eSMaxime Henrion 	struct xvfsconf *xvfsp;
6308f68ac6SBruce Evans 	size_t buflen;
645965373eSMaxime Henrion 	int cnt, i;
6508f68ac6SBruce Evans 
665965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0)
6708f68ac6SBruce Evans 		return (-1);
685965373eSMaxime Henrion 	xvfsp = malloc(buflen);
695965373eSMaxime Henrion 	if (xvfsp == NULL)
7008f68ac6SBruce Evans 		return (-1);
715965373eSMaxime Henrion 	if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
725965373eSMaxime Henrion 		free(xvfsp);
735965373eSMaxime Henrion 		return (-1);
7408f68ac6SBruce Evans 	}
755965373eSMaxime Henrion 	cnt = buflen / sizeof(struct xvfsconf);
765965373eSMaxime Henrion 	for (i = 0; i < cnt; i++) {
7773400123SAlan Somers 		if (strcmp(fsname, xvfsp[i].vfc_name) == 0 ||
78822f5b1dSAlan Somers 		    are_fusefs(fsname, xvfsp[i].vfc_name)) {
795965373eSMaxime Henrion 			memcpy(vfcp, xvfsp + i, sizeof(struct xvfsconf));
805965373eSMaxime Henrion 			free(xvfsp);
8108f68ac6SBruce Evans 			return (0);
8208f68ac6SBruce Evans 		}
835965373eSMaxime Henrion 	}
845965373eSMaxime Henrion 	free(xvfsp);
8508f68ac6SBruce Evans 	errno = ENOENT;
8608f68ac6SBruce Evans 	return (-1);
8708f68ac6SBruce Evans }
88