1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) 2018 Xiao Yang <yangx.jy@cn.fujitsu.com>
5 * Copyright (c) 2019 SUSE. All Rights Reserved.
6 */
7
8 /*
9 * DESCRIPTION:
10 * Testcase for testing the basic functionality of sysctl(2) system call.
11 * This testcase attempts to read the kernel parameters by using
12 * sysctl({CTL_KERN, KERN_* }, ...) and compares it with the known values.
13 */
14
15 #include <errno.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <linux/version.h>
19 #include <sys/utsname.h>
20 #include <linux/unistd.h>
21 #include <linux/sysctl.h>
22
23 #include "tst_test.h"
24 #include "lapi/syscalls.h"
25
26 static struct utsname buf;
27
28 static struct tcase {
29 char *desc;
30 int name[2];
31 char *cmp_str;
32 } tcases[] = {
33 {"KERN_OSTYPE", {CTL_KERN, KERN_OSTYPE}, buf.sysname},
34 {"KERN_OSRELEASE", {CTL_KERN, KERN_OSRELEASE}, buf.release},
35 {"KERN_VERSION", {CTL_KERN, KERN_VERSION}, buf.version},
36 };
37
verify_sysctl(unsigned int n)38 static void verify_sysctl(unsigned int n)
39 {
40 char osname[BUFSIZ];
41 size_t length = BUFSIZ;
42 struct tcase *tc = &tcases[n];
43
44 memset(osname, 0, BUFSIZ);
45
46 struct __sysctl_args args = {
47 .name = tc->name,
48 .nlen = ARRAY_SIZE(tc->name),
49 .oldval = osname,
50 .oldlenp = &length,
51 };
52
53 TEST(tst_syscall(__NR__sysctl, &args));
54 if (TST_RET != 0) {
55 tst_res(TFAIL | TTERRNO, "sysctl() failed unexpectedly");
56 return;
57 }
58
59 if (strcmp(osname, tc->cmp_str)) {
60 tst_res(TFAIL, "Strings don't match %s : %s",
61 osname, tc->cmp_str);
62 } else {
63 tst_res(TPASS, "Test for %s is correct", tc->desc);
64 }
65 }
66
setup(void)67 static void setup(void)
68 {
69 /* get kernel name and information */
70 if (uname(&buf) == -1)
71 tst_brk(TBROK | TERRNO, "uname() failed");
72
73 /* revert uname change in case of kGraft/livepatch */
74 char *klp_tag;
75 char *right_brace;
76
77 klp_tag = strstr(buf.version, "/kGraft-");
78 if (!klp_tag)
79 klp_tag = strstr(buf.version, "/lp-");
80 if (klp_tag) {
81 right_brace = strchr(klp_tag, ')');
82 if (right_brace)
83 memmove(klp_tag, right_brace, strlen(right_brace)+1);
84 }
85 }
86
87 static struct tst_test test = {
88 .setup = setup,
89 .tcnt = ARRAY_SIZE(tcases),
90 .test = verify_sysctl,
91 };
92