1175220Sjhb/*-
2175220Sjhb * Copyright (c) 2008 Yahoo!, Inc.
3175220Sjhb * All rights reserved.
4175220Sjhb * Written by: John Baldwin <jhb@FreeBSD.org>
5175220Sjhb *
6175220Sjhb * Redistribution and use in source and binary forms, with or without
7175220Sjhb * modification, are permitted provided that the following conditions
8175220Sjhb * are met:
9175220Sjhb * 1. Redistributions of source code must retain the above copyright
10175220Sjhb *    notice, this list of conditions and the following disclaimer.
11175220Sjhb * 2. Redistributions in binary form must reproduce the above copyright
12175220Sjhb *    notice, this list of conditions and the following disclaimer in the
13175220Sjhb *    documentation and/or other materials provided with the distribution.
14175220Sjhb * 3. Neither the name of the author nor the names of any co-contributors
15175220Sjhb *    may be used to endorse or promote products derived from this software
16175220Sjhb *    without specific prior written permission.
17175220Sjhb *
18175220Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19175220Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20175220Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21175220Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22175220Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23175220Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24175220Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25175220Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26175220Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27175220Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28175220Sjhb * SUCH DAMAGE.
29175220Sjhb */
30175220Sjhb
31175220Sjhb#include <sys/cdefs.h>
32175220Sjhb__FBSDID("$FreeBSD: releng/11.0/lib/libc/gen/feature_present.c 288007 2015-09-20 03:56:57Z rodrigc $");
33175220Sjhb
34175220Sjhb#include <sys/types.h>
35175220Sjhb#include <sys/sysctl.h>
36175220Sjhb#include <stdio.h>
37175220Sjhb#include <stdlib.h>
38288007Srodrigc#include <unistd.h>
39175220Sjhb
40175220Sjhb/*
41175220Sjhb * Returns true if the named feature is present in the currently
42175220Sjhb * running kernel.  A feature's presence is indicated by an integer
43175220Sjhb * sysctl node called kern.feature.<feature> that is non-zero.
44175220Sjhb */
45175220Sjhbint
46175220Sjhbfeature_present(const char *feature)
47175220Sjhb{
48175220Sjhb	char *mib;
49175220Sjhb	size_t len;
50175220Sjhb	int i;
51175220Sjhb
52175220Sjhb	if (asprintf(&mib, "kern.features.%s", feature) < 0)
53175220Sjhb		return (0);
54175220Sjhb	len = sizeof(i);
55175220Sjhb	if (sysctlbyname(mib, &i, &len, NULL, 0) < 0) {
56175220Sjhb		free(mib);
57175220Sjhb		return (0);
58175220Sjhb	}
59175220Sjhb	free(mib);
60175220Sjhb	if (len != sizeof(i))
61175220Sjhb		return (0);
62175220Sjhb	return (i != 0);
63175220Sjhb}
64