1272343Sngie/*	$NetBSD: isqemu.h,v 1.3 2013/04/14 12:46:29 martin Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2013 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Christos Zoulas.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie * 3. Neither the name of The NetBSD Foundation nor the names of its
19272343Sngie *    contributors may be used to endorse or promote products derived
20272343Sngie *    from this software without specific prior written permission.
21272343Sngie *
22272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32272343Sngie * POSSIBILITY OF SUCH DAMAGE.
33272343Sngie */
34272343Sngie#include <sys/param.h>
35272343Sngie#include <sys/sysctl.h>
36272343Sngie#include <stdbool.h>
37272343Sngie#include <string.h>
38272343Sngie#include <errno.h>
39272343Sngie#include <err.h>
40272343Sngie
41272343Sngiestatic __inline bool
42272343SngieisQEMU(void) {
43272343Sngie#if defined(__i386__) || defined(__x86_64__)
44272343Sngie	char name[1024];
45272343Sngie	size_t len = sizeof(name);
46272343Sngie
47272343Sngie	if (sysctlbyname("machdep.cpu_brand", name, &len, NULL, 0) == -1) {
48272343Sngie		if (errno == ENOENT)
49272343Sngie			return false;
50272343Sngie		err(EXIT_FAILURE, "sysctl");
51272343Sngie	}
52272343Sngie	return strstr(name, "QEMU") != NULL;
53272343Sngie#else
54272343Sngie	return false;
55272343Sngie#endif
56272343Sngie}
57272343Sngie
58272343Sngie#ifdef TEST
59272343Sngieint
60272343Sngiemain(void) {
61272343Sngie	return isQEMU();
62272343Sngie}
63272343Sngie#endif
64