blob: b8900ad3fdeacdaa96daccbb1127a8dba6d287d1 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * sysctl01.c
23 *
24 * DESCRIPTION
25 * Testcase for testing the basic functionality of sysctl(2) system call.
26 * This testcase attempts to read the kernel parameters using
27 * sysctl({CTL_KERN, KERN_* }, ...) and compares it with the known
28 * values.
29 *
30 * USAGE: <for command-line>
31 * sysctl01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
32 * where, -c n : Run n copies concurrently.
33 * -f : Turn off functionality Testing.
34 * -i n : Execute test n times.
35 * -I x : Execute test for x seconds.
36 * -P x : Pause for x seconds between iterations.
37 * -t : Turn on syscall timing.
38 *
39 * HISTORY
40 * 07/2001 Ported by Wayne Boyer
41 *
42 * RESTRICTIONS
43 * None
44 */
plars865695b2001-08-27 22:15:12 +000045#include "test.h"
robbiew9a66de12003-03-27 22:16:14 +000046#include <stdio.h>
47#include <errno.h>
48#include <unistd.h>
49#include <linux/version.h>
50#include <sys/utsname.h>
51#include <linux/unistd.h>
52#include <linux/sysctl.h>
plars865695b2001-08-27 22:15:12 +000053
54char *TCID = "sysctl01";
Mike Frysinger75201f12013-02-08 17:12:41 -050055
56/* This is an older/deprecated syscall that newer arches are omitting */
57#ifdef __NR_sysctl
58
plars865695b2001-08-27 22:15:12 +000059int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000060
Wanlong Gao64aa7222011-11-14 08:37:39 +080061static int sysctl(int *name, int nlen, void *oldval, size_t * oldlenp,
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 void *newval, size_t newlen)
robbiew9a66de12003-03-27 22:16:14 +000063{
subrata_modak56207ce2009-03-23 13:35:39 +000064 struct __sysctl_args args =
65 { name, nlen, oldval, oldlenp, newval, newlen };
vapiere24e35c2006-08-06 00:51:02 +000066 return syscall(__NR__sysctl, &args);
robbiew9a66de12003-03-27 22:16:14 +000067}
68
plars865695b2001-08-27 22:15:12 +000069#define SIZE(x) sizeof(x)/sizeof(x[0])
70
71struct utsname buf;
72char osname[BUFSIZ];
plars74948ad2002-11-14 16:16:14 +000073size_t osnamelth;
plars865695b2001-08-27 22:15:12 +000074
75void setup(void);
76void cleanup(void);
77
78struct test_case_t {
79 char *desc;
80 int name[2];
81 int size;
Garrett Cooper63dc4bd2010-12-19 08:25:51 -080082 char *oldval;
plars74948ad2002-11-14 16:16:14 +000083 size_t *oldlen;
plars865695b2001-08-27 22:15:12 +000084 void *newval;
85 int newlen;
subrata_modak56207ce2009-03-23 13:35:39 +000086 int (*cleanup) ();
plars865695b2001-08-27 22:15:12 +000087 int exp_retval;
88} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000089 {
90 "Test for KERN_OSTYPE", {
91 CTL_KERN, KERN_OSTYPE}, 2, osname, &osnamelth, NULL, 0, NULL, 0}, {
92 "Test for KERN_OSRELEASE", {
93 CTL_KERN, KERN_OSRELEASE}, 2,
94 osname, &osnamelth, NULL, 0, NULL, 0}, {
95 "Test for KERN_VERSION", {
96 CTL_KERN, KERN_VERSION}, 2, osname, &osnamelth, NULL, 0, NULL, 0}
plars865695b2001-08-27 22:15:12 +000097};
98
plars74948ad2002-11-14 16:16:14 +000099int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000100{
101 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200102 const char *msg;
plars865695b2001-08-27 22:15:12 +0000103 int i;
plars865695b2001-08-27 22:15:12 +0000104 char *comp_string;
105
Garrett Cooper63dc4bd2010-12-19 08:25:51 -0800106 comp_string = NULL;
107
Garrett Cooper63dc4bd2010-12-19 08:25:51 -0800108 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800109 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000110
111 setup();
112
plars865695b2001-08-27 22:15:12 +0000113 for (lc = 0; TEST_LOOPING(lc); lc++) {
114
Caspar Zhangd59a6592013-03-07 14:59:12 +0800115 /* reset tst_count in case we are looping */
116 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000117
118 for (i = 0; i < TST_TOTAL; ++i) {
119
120 osnamelth = SIZE(osname);
121
122 switch (i) {
123 case 0:
124 comp_string = buf.sysname;
125 break;
126 case 1:
127 comp_string = buf.release;
128 break;
129 case 2:
130 comp_string = buf.version;
131 break;
132 }
133
134 TEST(sysctl(TC[i].name, TC[i].size, TC[i].oldval,
subrata_modak56207ce2009-03-23 13:35:39 +0000135 TC[i].oldlen, TC[i].newval, TC[i].newlen));
plars865695b2001-08-27 22:15:12 +0000136
137 if (TEST_RETURN != 0) {
Wanlong Gao64aa7222011-11-14 08:37:39 +0800138 if (TEST_ERRNO == ENOSYS) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TCONF,
140 "You may need to make CONFIG_SYSCTL_SYSCALL=y"
141 " to your kernel config.");
Wanlong Gao64aa7222011-11-14 08:37:39 +0800142 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 tst_resm(TFAIL,
144 "sysctl(2) failed unexpectedly "
145 "errno:%d", TEST_ERRNO);
Wanlong Gao64aa7222011-11-14 08:37:39 +0800146 }
plars865695b2001-08-27 22:15:12 +0000147 continue;
148 }
149
plars865695b2001-08-27 22:15:12 +0000150 if (strcmp(TC[i].oldval, comp_string) != 0) {
151 tst_resm(TFAIL, "strings don't match - %s : %s",
152 TC[i].oldval, comp_string);
153 } else {
154 tst_resm(TPASS, "%s is correct", TC[i].desc);
155 }
156 if (TC[i].cleanup) {
157 (void)TC[i].cleanup();
158 }
159 }
160 }
plars865695b2001-08-27 22:15:12 +0000161
Cyril Hrubise38b9612014-06-02 17:20:57 +0200162 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800163 tst_exit();
plars865695b2001-08-27 22:15:12 +0000164}
165
166/*
167 * setup() - performs all ONE TIME setup for this test.
168 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400169void setup(void)
plars865695b2001-08-27 22:15:12 +0000170{
Garrett Cooper2c282152010-12-16 00:55:50 -0800171
plars865695b2001-08-27 22:15:12 +0000172 tst_sig(NOFORK, DEF_HANDLER, cleanup);
173
plars865695b2001-08-27 22:15:12 +0000174 TEST_PAUSE;
175
176 /* get kernel name and information */
177 if (uname(&buf) == -1) {
178 tst_brkm(TBROK, cleanup, "uname() failed");
179 }
180}
181
182/*
183 * cleanup() - performs all ONE TIME cleanup for this test at
184 * completion or premature exit.
185 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400186void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000187{
plars865695b2001-08-27 22:15:12 +0000188
Wanlong Gao64aa7222011-11-14 08:37:39 +0800189}
Mike Frysinger75201f12013-02-08 17:12:41 -0500190
191#else
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200192int TST_TOTAL = 0;
Mike Frysinger75201f12013-02-08 17:12:41 -0500193
Mike Frysingerc57fba52014-04-09 18:56:30 -0400194int main(void)
Mike Frysinger75201f12013-02-08 17:12:41 -0500195{
196
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100197 tst_brkm(TCONF, NULL,
198 "This test needs a kernel that has sysctl syscall.");
Mike Frysinger75201f12013-02-08 17:12:41 -0500199}
200#endif