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$");
33175220Sjhb
34175220Sjhb#include <sys/types.h>
35175220Sjhb#include <sys/sysctl.h>
36175220Sjhb#include <stdio.h>
37175220Sjhb#include <stdlib.h>
38175220Sjhb
39175220Sjhb/*
40175220Sjhb * Returns true if the named feature is present in the currently
41175220Sjhb * running kernel.  A feature's presence is indicated by an integer
42175220Sjhb * sysctl node called kern.feature.<feature> that is non-zero.
43175220Sjhb */
44175220Sjhbint
45175220Sjhbfeature_present(const char *feature)
46175220Sjhb{
47175220Sjhb	char *mib;
48175220Sjhb	size_t len;
49175220Sjhb	int i;
50175220Sjhb
51175220Sjhb	if (asprintf(&mib, "kern.features.%s", feature) < 0)
52175220Sjhb		return (0);
53175220Sjhb	len = sizeof(i);
54175220Sjhb	if (sysctlbyname(mib, &i, &len, NULL, 0) < 0) {
55175220Sjhb		free(mib);
56175220Sjhb		return (0);
57175220Sjhb	}
58175220Sjhb	free(mib);
59175220Sjhb	if (len != sizeof(i))
60175220Sjhb		return (0);
61175220Sjhb	return (i != 0);
62175220Sjhb}
63