blob: 861b0214aebfcd8d70c0b8e29525cc1f83018451 [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 * sysctl04.c
23 *
24 * DESCRIPTION
25 * Testcase to check that sysctl(2) sets errno to ENOTDIR
26 *
27 * ALGORITHM
28 * 1. Call sysctl(2) with sc_nlen set to 0, and expect ENOTDIR to be set.
29 * 2. Call sysctl(2) with sc_nlen greater than CTL_MAXNAME, and expect
30 * ENOTDIR to be set in the errno.
31 *
32 * USAGE: <for command-line>
33 * sysctl04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
34 * where, -c n : Run n copies concurrently.
35 * -e : Turn on errno logging.
36 * -i n : Execute test n times.
37 * -I x : Execute test for x seconds.
38 * -P x : Pause for x seconds between iterations.
39 * -t : Turn on syscall timing.
40 *
41 * HISTORY
42 * 07/2001 Ported by Wayne Boyer
43 *
44 * RESTRICTIONS
45 * None
46 */
47
robbiew9a66de12003-03-27 22:16:14 +000048#include "test.h"
plars865695b2001-08-27 22:15:12 +000049#include <stdio.h>
50#include <errno.h>
plars74948ad2002-11-14 16:16:14 +000051#include <unistd.h>
52#include <linux/unistd.h>
robbiew9a66de12003-03-27 22:16:14 +000053#include <linux/sysctl.h>
plars865695b2001-08-27 22:15:12 +000054
55char *TCID = "sysctl04";
Mike Frysinger75201f12013-02-08 17:12:41 -050056
57/* This is an older/deprecated syscall that newer arches are omitting */
58#ifdef __NR_sysctl
59
plars865695b2001-08-27 22:15:12 +000060int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000061
subrata_modak56207ce2009-03-23 13:35:39 +000062int sysctl(int *name, int nlen, void *oldval, size_t * oldlenp,
63 void *newval, size_t newlen)
robbiew9a66de12003-03-27 22:16:14 +000064{
subrata_modak56207ce2009-03-23 13:35:39 +000065 struct __sysctl_args args =
66 { name, nlen, oldval, oldlenp, newval, newlen };
vapiere24e35c2006-08-06 00:51:02 +000067 return syscall(__NR__sysctl, &args);
robbiew9a66de12003-03-27 22:16:14 +000068}
69
plars865695b2001-08-27 22:15:12 +000070#define SIZE(x) sizeof(x)/sizeof(x[0])
71#define OSNAMESZ 100
72
plars865695b2001-08-27 22:15:12 +000073void setup(void);
74void cleanup(void);
75
76struct test_case_t {
77 int size;
78 int error;
79} TC[] = {
80 /* comment goes here */
subrata_modak56207ce2009-03-23 13:35:39 +000081 {
82 0, ENOTDIR},
83 /* comment goes here */
84 {
85 CTL_MAXNAME + 1, ENOTDIR}
plars865695b2001-08-27 22:15:12 +000086};
87
plars74948ad2002-11-14 16:16:14 +000088int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000089{
90 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020091 const char *msg;
plars865695b2001-08-27 22:15:12 +000092
93 char osname[OSNAMESZ];
plars74948ad2002-11-14 16:16:14 +000094 int i;
95 size_t osnamelth;
plars865695b2001-08-27 22:15:12 +000096 int name[] = { CTL_KERN, KERN_OSREV };
97
Garrett Cooper45e285d2010-11-22 12:19:25 -080098 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000100 }
101
102 setup();
103
104 osnamelth = SIZE(osname);
105
plars865695b2001-08-27 22:15:12 +0000106 for (lc = 0; TEST_LOOPING(lc); lc++) {
107
Caspar Zhangd59a6592013-03-07 14:59:12 +0800108 /* reset tst_count in case we are looping */
109 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000110
111 /* loop through the test cases */
112 for (i = 0; i < TST_TOTAL; i++) {
113
114 TEST(sysctl(name, 0, osname, &osnamelth, 0, 0));
115
116 if (TEST_RETURN != -1) {
117 tst_resm(TFAIL, "call succeeded unexpectedly");
118 continue;
119 }
120
plars865695b2001-08-27 22:15:12 +0000121 if (TEST_ERRNO == TC[i].error) {
122 tst_resm(TPASS, "expected failure - "
123 "errno = %d : %s", TEST_ERRNO,
124 strerror(TEST_ERRNO));
Wanlong Gao64aa7222011-11-14 08:37:39 +0800125 } else if (TEST_ERRNO == ENOSYS) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_resm(TCONF,
127 "You may need to make CONFIG_SYSCTL_SYSCALL=y"
128 " to your kernel config.");
plars865695b2001-08-27 22:15:12 +0000129 } else {
130 tst_resm(TFAIL, "unexpected error - %d : %s - "
131 "expected %d", TEST_ERRNO,
132 strerror(TEST_ERRNO), TC[i].error);
133 }
134 }
135 }
136 cleanup();
137
Garrett Cooper53740502010-12-16 00:04:01 -0800138 tst_exit();
plars865695b2001-08-27 22:15:12 +0000139}
140
141/*
142 * setup() - performs all ONE TIME setup for this test.
143 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void setup(void)
plars865695b2001-08-27 22:15:12 +0000145{
Garrett Cooper2c282152010-12-16 00:55:50 -0800146
plars865695b2001-08-27 22:15:12 +0000147 tst_sig(NOFORK, DEF_HANDLER, cleanup);
148
plars865695b2001-08-27 22:15:12 +0000149 TEST_PAUSE;
150}
151
152/*
153 * cleanup() - performs all ONE TIME cleanup for this test at
154 * completion or premature exit.
155 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400156void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000157{
plars865695b2001-08-27 22:15:12 +0000158
Wanlong Gao64aa7222011-11-14 08:37:39 +0800159}
Mike Frysinger75201f12013-02-08 17:12:41 -0500160
161#else
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200162int TST_TOTAL = 0;
Mike Frysinger75201f12013-02-08 17:12:41 -0500163
Mike Frysingerc57fba52014-04-09 18:56:30 -0400164int main(void)
Mike Frysinger75201f12013-02-08 17:12:41 -0500165{
166
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100167 tst_brkm(TCONF, NULL,
168 "This test needs a kernel that has sysctl syscall.");
Mike Frysinger75201f12013-02-08 17:12:41 -0500169}
170#endif