1// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
2//
3// UNSUPPORTED: linux, target={{.*solaris.*}}
4
5#include <sys/param.h>
6#include <sys/types.h>
7
8#include <sys/sysctl.h>
9
10#include <assert.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14#ifndef __arraycount
15#define __arraycount(a) (sizeof(a) / sizeof(a[0]))
16#endif
17
18void test_sysctl() {
19 char buf[100];
20 size_t len = sizeof(buf);
21 int mib[] = {CTL_KERN, KERN_OSTYPE};
22 int rv = sysctl(mib, __arraycount(mib), buf, &len, NULL, 0);
23 assert(!rv);
24
25 printf(format: "sysctl: '%s' size: '%zu'\n", buf, len);
26}
27
28void test_sysctlbyname() {
29 char buf[100];
30 size_t len = sizeof(buf);
31 int rv = sysctlbyname("kern.ostype", buf, &len, NULL, 0);
32 assert(!rv);
33
34 printf(format: "sysctlbyname: '%s' size: '%zu'\n", buf, len);
35}
36
37void test_sysctlnametomib() {
38 int mib[CTL_MAXNAME];
39 size_t mib_len = __arraycount(mib);
40 int rv = sysctlnametomib("kern.ostype", &mib[0], &mib_len);
41 assert(!rv);
42
43 char buf[100];
44 size_t len = sizeof(buf);
45 rv = sysctl(mib, mib_len, buf, &len, NULL, 0);
46 assert(!rv);
47
48 printf(format: "sysctlnametomib: '%s' size: '%zu'\n", buf, len);
49}
50
51int main(void) {
52 printf(format: "sysctl\n");
53
54 test_sysctl();
55 test_sysctlbyname();
56 test_sysctlnametomib();
57
58 // CHECK: sysctl
59 // CHECK: sysctl: '{{.*}}' size: '{{.*}}'
60 // CHECK: sysctlbyname: '{{.*}}' size: '{{.*}}'
61 // CHECK: sysctlnametomib: '{{.*}}' size: '{{.*}}'
62
63 return 0;
64}
65

source code of compiler-rt/test/sanitizer_common/TestCases/Posix/sysctl.cpp