xref: /minix3/tests/lib/libc/gen/isqemu.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: isqemu.h,v 1.4 2015/01/03 14:21:05 gson Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2013 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Christos Zoulas.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  * 3. Neither the name of The NetBSD Foundation nor the names of its
1911be35a1SLionel Sambuc  *    contributors may be used to endorse or promote products derived
2011be35a1SLionel Sambuc  *    from this software without specific prior written permission.
2111be35a1SLionel Sambuc  *
2211be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2311be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2411be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2511be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2611be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2711be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2811be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2911be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3011be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3111be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3211be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3311be35a1SLionel Sambuc  */
3411be35a1SLionel Sambuc #include <sys/param.h>
3511be35a1SLionel Sambuc #include <sys/sysctl.h>
3611be35a1SLionel Sambuc #include <stdbool.h>
37*0a6a1f1dSLionel Sambuc #include <stdlib.h>
3811be35a1SLionel Sambuc #include <string.h>
3911be35a1SLionel Sambuc #include <errno.h>
4011be35a1SLionel Sambuc #include <err.h>
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc static __inline bool
isQEMU(void)4311be35a1SLionel Sambuc isQEMU(void) {
4411be35a1SLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
4511be35a1SLionel Sambuc 	char name[1024];
4611be35a1SLionel Sambuc 	size_t len = sizeof(name);
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc 	if (sysctlbyname("machdep.cpu_brand", name, &len, NULL, 0) == -1) {
4911be35a1SLionel Sambuc 		if (errno == ENOENT)
5011be35a1SLionel Sambuc 			return false;
5111be35a1SLionel Sambuc 		err(EXIT_FAILURE, "sysctl");
5211be35a1SLionel Sambuc 	}
5311be35a1SLionel Sambuc 	return strstr(name, "QEMU") != NULL;
5411be35a1SLionel Sambuc #else
5511be35a1SLionel Sambuc 	return false;
5611be35a1SLionel Sambuc #endif
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc #ifdef TEST
6011be35a1SLionel Sambuc int
main(void)6111be35a1SLionel Sambuc main(void) {
6211be35a1SLionel Sambuc 	return isQEMU();
6311be35a1SLionel Sambuc }
6411be35a1SLionel Sambuc #endif
65