xref: /freebsd/lib/libc/gen/feature_present.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1ce309a2fSJohn Baldwin /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ce309a2fSJohn Baldwin  * Copyright (c) 2008 Yahoo!, Inc.
5ce309a2fSJohn Baldwin  * All rights reserved.
6ce309a2fSJohn Baldwin  * Written by: John Baldwin <jhb@FreeBSD.org>
7ce309a2fSJohn Baldwin  *
8ce309a2fSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
9ce309a2fSJohn Baldwin  * modification, are permitted provided that the following conditions
10ce309a2fSJohn Baldwin  * are met:
11ce309a2fSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
12ce309a2fSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
13ce309a2fSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
14ce309a2fSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
15ce309a2fSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
16ce309a2fSJohn Baldwin  * 3. Neither the name of the author nor the names of any co-contributors
17ce309a2fSJohn Baldwin  *    may be used to endorse or promote products derived from this software
18ce309a2fSJohn Baldwin  *    without specific prior written permission.
19ce309a2fSJohn Baldwin  *
20ce309a2fSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21ce309a2fSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22ce309a2fSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ce309a2fSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24ce309a2fSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25ce309a2fSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26ce309a2fSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27ce309a2fSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28ce309a2fSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29ce309a2fSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30ce309a2fSJohn Baldwin  * SUCH DAMAGE.
31ce309a2fSJohn Baldwin  */
32ce309a2fSJohn Baldwin 
33ce309a2fSJohn Baldwin #include <sys/types.h>
34ce309a2fSJohn Baldwin #include <sys/sysctl.h>
35ce309a2fSJohn Baldwin #include <stdio.h>
36ce309a2fSJohn Baldwin #include <stdlib.h>
3797f3e1c2SCraig Rodrigues #include <unistd.h>
38ce309a2fSJohn Baldwin 
39ce309a2fSJohn Baldwin /*
40ce309a2fSJohn Baldwin  * Returns true if the named feature is present in the currently
41ce309a2fSJohn Baldwin  * running kernel.  A feature's presence is indicated by an integer
42ce309a2fSJohn Baldwin  * sysctl node called kern.feature.<feature> that is non-zero.
43ce309a2fSJohn Baldwin  */
44ce309a2fSJohn Baldwin int
feature_present(const char * feature)45ce309a2fSJohn Baldwin feature_present(const char *feature)
46ce309a2fSJohn Baldwin {
47ce309a2fSJohn Baldwin 	char *mib;
48ce309a2fSJohn Baldwin 	size_t len;
49ce309a2fSJohn Baldwin 	int i;
50ce309a2fSJohn Baldwin 
51ce309a2fSJohn Baldwin 	if (asprintf(&mib, "kern.features.%s", feature) < 0)
52ce309a2fSJohn Baldwin 		return (0);
53ce309a2fSJohn Baldwin 	len = sizeof(i);
54ce309a2fSJohn Baldwin 	if (sysctlbyname(mib, &i, &len, NULL, 0) < 0) {
55ce309a2fSJohn Baldwin 		free(mib);
56ce309a2fSJohn Baldwin 		return (0);
57ce309a2fSJohn Baldwin 	}
58ce309a2fSJohn Baldwin 	free(mib);
59ce309a2fSJohn Baldwin 	if (len != sizeof(i))
60ce309a2fSJohn Baldwin 		return (0);
61ce309a2fSJohn Baldwin 	return (i != 0);
62ce309a2fSJohn Baldwin }
63