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