• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  * Copyright (c) 2018 Xiao Yang <yangx.jy@cn.fujitsu.com>
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  */
15 
16 /*
17  * DESCRIPTION:
18  * Testcase for testing the basic functionality of sysctl(2) system call.
19  * This testcase attempts to read the kernel parameters by using
20  * sysctl({CTL_KERN, KERN_* }, ...) and compares it with the known values.
21  */
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <linux/version.h>
27 #include <sys/utsname.h>
28 #include <linux/unistd.h>
29 #include <linux/sysctl.h>
30 
31 #include "tst_test.h"
32 #include "lapi/syscalls.h"
33 
34 static struct utsname buf;
35 
36 static struct tcase {
37 	char *desc;
38 	int name[2];
39 	char *cmp_str;
40 } tcases[] = {
41 	{"KERN_OSTYPE", {CTL_KERN, KERN_OSTYPE}, buf.sysname},
42 	{"KERN_OSRELEASE", {CTL_KERN, KERN_OSRELEASE}, buf.release},
43 	{"KERN_VERSION", {CTL_KERN, KERN_VERSION}, buf.version},
44 };
45 
verify_sysctl(unsigned int n)46 static void verify_sysctl(unsigned int n)
47 {
48 	char osname[BUFSIZ];
49 	size_t length = BUFSIZ;
50 	struct tcase *tc = &tcases[n];
51 
52 	memset(osname, 0, BUFSIZ);
53 
54 	struct __sysctl_args args = {
55 		.name = tc->name,
56 		.nlen = ARRAY_SIZE(tc->name),
57 		.oldval = osname,
58 		.oldlenp = &length,
59 	};
60 
61 	TEST(tst_syscall(__NR__sysctl, &args));
62 	if (TST_RET != 0) {
63 		tst_res(TFAIL | TTERRNO, "sysctl() failed unexpectedly");
64 		return;
65 	}
66 
67 	if (strcmp(osname, tc->cmp_str)) {
68 		tst_res(TFAIL, "Strings don't match %s : %s",
69 			osname, tc->cmp_str);
70 	} else {
71 		tst_res(TPASS, "Test for %s is correct", tc->desc);
72 	}
73 }
74 
setup(void)75 static void setup(void)
76 {
77 	/* get kernel name and information */
78 	if (uname(&buf) == -1)
79 		tst_brk(TBROK | TERRNO, "uname() failed");
80 }
81 
82 static struct tst_test test = {
83 	.setup = setup,
84 	.tcnt = ARRAY_SIZE(tcases),
85 	.test = verify_sysctl,
86 };
87