blob: 07e0d2aa3172ca9d17be772dcacadaa4b07cc6ef [file] [log] [blame]
robbiew9a55a782002-12-26 20:45:16 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiew9a55a782002-12-26 20:45:16 +000015 *
16 */
17/**************************************************************************
vapier238755b2007-03-22 06:50:54 +000018 *
robbiew9a55a782002-12-26 20:45:16 +000019 * TEST IDENTIFIER : sysfs(2)
20 *
vapier238755b2007-03-22 06:50:54 +000021 *
robbiew9a55a782002-12-26 20:45:16 +000022 * EXECUTED BY : anyone
vapier238755b2007-03-22 06:50:54 +000023 *
robbiew9a55a782002-12-26 20:45:16 +000024 * TEST TITLE : Test checking for basic error conditions
subrata_modak56207ce2009-03-23 13:35:39 +000025 * for sysfs(2)
vapier238755b2007-03-22 06:50:54 +000026 *
27 * TEST CASE TOTAL : 3
28 *
robbiew9a55a782002-12-26 20:45:16 +000029 * AUTHOR : Aniruddha Marathe <aniruddha.marathe@wipro.com>
vapier238755b2007-03-22 06:50:54 +000030 *
robbiew9a55a782002-12-26 20:45:16 +000031 * SIGNALS
subrata_modak56207ce2009-03-23 13:35:39 +000032 * Uses SIGUSR1 to pause before test if option set.
33 * (See the parse_opts(3) man page).
robbiew9a55a782002-12-26 20:45:16 +000034 *
35 * DESCRIPTION
vapier238755b2007-03-22 06:50:54 +000036 * This test case checks whether sysfs(2) system call returns
robbiew9a55a782002-12-26 20:45:16 +000037 * appropriate error number for invalid
vapier238755b2007-03-22 06:50:54 +000038 * option and for invalid filesystem index and when
robbiew9a55a782002-12-26 20:45:16 +000039 * buffer is out of address space
vapier238755b2007-03-22 06:50:54 +000040 *
subrata_modak56207ce2009-03-23 13:35:39 +000041 * Setup:
robbiew9a55a782002-12-26 20:45:16 +000042 * Setup signal handling.
43 * Pause for SIGUSR1 if option specified.
vapier238755b2007-03-22 06:50:54 +000044 *
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Test:
robbiew9a55a782002-12-26 20:45:16 +000046 * Loop if the proper options are given.
vapier238755b2007-03-22 06:50:54 +000047 * Execute system call with invaid option parameter and for
robbiew9a55a782002-12-26 20:45:16 +000048 * invalid filesystem index
49 * Check return code, if system call fails with errno == expected errno
50 * Issue syscall passed with expected errno
vapier238755b2007-03-22 06:50:54 +000051 * Otherwise,
robbiew9a55a782002-12-26 20:45:16 +000052 * Issue syscall failed to produce expected errno
vapier238755b2007-03-22 06:50:54 +000053 *
subrata_modak56207ce2009-03-23 13:35:39 +000054 * Cleanup:
55 * Do cleanup for the test.
vapier238755b2007-03-22 06:50:54 +000056 *
robbiew9a55a782002-12-26 20:45:16 +000057 * USAGE: <for command-line>
58 * sysfs06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
59 * where:
subrata_modak56207ce2009-03-23 13:35:39 +000060 * -c n : Run n copies simultaneously
robbiew9a55a782002-12-26 20:45:16 +000061 * -e : Turn on errno logging.
62 * -i n : Execute test n times.
63 * -I x : Execute test for x seconds.
64 * -p : Pause for SIGUSR1 before starting
65 * -P x : Pause for x seconds between iterations.
66 * -t : Turn on syscall timing.
vapier238755b2007-03-22 06:50:54 +000067 *
robbiew9a55a782002-12-26 20:45:16 +000068 *RESTRICTIONS:
69 *There is no libc or glibc support
70 *****************************************************************************/
71
72#include <errno.h>
73#include <syscall.h>
robbiewde7e9422003-03-05 16:58:25 +000074#include <sys/mman.h>
robbiew9a55a782002-12-26 20:45:16 +000075#include "test.h"
robbiew9a55a782002-12-26 20:45:16 +000076
77static void setup();
78static void cleanup();
79
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020080char *TCID = "sysfs06";
vapier238755b2007-03-22 06:50:54 +000081static int option[3] = { 2, 4, 2 }; /* valid and invalid option */
82static int fsindex[3] = { 10000, 0, 1 }; /*invalid and valid fsindex */
robbiew9a55a782002-12-26 20:45:16 +000083
84static struct test_case_t {
85 char *err_desc; /*error description */
vapier238755b2007-03-22 06:50:54 +000086 int exp_errno; /* expected error number */
87 char *exp_errval; /*Expected errorvalue string */
robbiew9a55a782002-12-26 20:45:16 +000088} testcase[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000089 {
90 "Invalid option", EINVAL, "EINVAL"}, {
91 "fs_index is out of bounds", EINVAL, "EINVAL"}, {
92 "buf is outside your accessible address space", EFAULT, "EFAULT"}
robbiew9a55a782002-12-26 20:45:16 +000093};
Wanlong Gao354ebb42012-12-07 10:10:04 +080094
Cyril Hrubisb863a0b2014-09-24 13:15:29 +020095int TST_TOTAL = ARRAY_SIZE(testcase);
robbiew9a55a782002-12-26 20:45:16 +000096
vapier238755b2007-03-22 06:50:54 +000097char *bad_addr = 0;
robbiewde7e9422003-03-05 16:58:25 +000098
vapier238755b2007-03-22 06:50:54 +000099int main(int ac, char **av)
robbiew9a55a782002-12-26 20:45:16 +0000100{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200101 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200102 const char *msg;
robbiew9a55a782002-12-26 20:45:16 +0000103
Garrett Cooper45e285d2010-11-22 12:19:25 -0800104 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800105 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew9a55a782002-12-26 20:45:16 +0000106
robbiew9a55a782002-12-26 20:45:16 +0000107 setup();
108
subrata_modak5e278252008-04-23 07:21:00 +0000109#ifdef __NR_sysfs
Garrett Cooper2c282152010-12-16 00:55:50 -0800110
robbiew9a55a782002-12-26 20:45:16 +0000111 for (lc = 0; TEST_LOOPING(lc); lc++) {
112
vapier238755b2007-03-22 06:50:54 +0000113 for (i = 0; i < TST_TOTAL; i++) {
robbiew9a55a782002-12-26 20:45:16 +0000114
Caspar Zhangd59a6592013-03-07 14:59:12 +0800115 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000116 TEST(syscall
117 (__NR_sysfs, option[i], fsindex[i], bad_addr));
vapier238755b2007-03-22 06:50:54 +0000118
robbiew9a55a782002-12-26 20:45:16 +0000119 /* check return code */
subrata_modak56207ce2009-03-23 13:35:39 +0000120 if ((TEST_RETURN == -1)
121 && (TEST_ERRNO == testcase[i].exp_errno)) {
122 tst_resm(TPASS,
123 "sysfs(2) expected failure;"
vapier238755b2007-03-22 06:50:54 +0000124 " Got errno - %s : %s",
125 testcase[i].exp_errval,
126 testcase[i].err_desc);
robbiew9a55a782002-12-26 20:45:16 +0000127 } else {
128 tst_resm(TFAIL, "sysfs(2) failed to produce"
vapier238755b2007-03-22 06:50:54 +0000129 " expected error; %d, errno"
130 ": %s and got %d",
131 testcase[i].exp_errno,
132 testcase[i].exp_errval, TEST_ERRNO);
robbiew9a55a782002-12-26 20:45:16 +0000133 }
vapier238755b2007-03-22 06:50:54 +0000134 } /*End of TEST LOOPS */
Garrett Cooper2c282152010-12-16 00:55:50 -0800135 }
subrata_modak5e278252008-04-23 07:21:00 +0000136#else
subrata_modak56207ce2009-03-23 13:35:39 +0000137 tst_resm(TWARN,
138 "This test can only run on kernels that support the sysfs system call");
subrata_modak5e278252008-04-23 07:21:00 +0000139#endif
robbiew9a55a782002-12-26 20:45:16 +0000140
vapier238755b2007-03-22 06:50:54 +0000141 /*Clean up and exit */
robbiew9a55a782002-12-26 20:45:16 +0000142 cleanup();
143
Garrett Cooper2c282152010-12-16 00:55:50 -0800144 tst_exit();
vapier238755b2007-03-22 06:50:54 +0000145} /*End of main */
robbiew9a55a782002-12-26 20:45:16 +0000146
147/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400148void setup(void)
robbiew9a55a782002-12-26 20:45:16 +0000149{
Garrett Cooper2c282152010-12-16 00:55:50 -0800150
robbiew9a55a782002-12-26 20:45:16 +0000151 tst_sig(NOFORK, DEF_HANDLER, cleanup);
152
robbiew9a55a782002-12-26 20:45:16 +0000153 TEST_PAUSE;
154
subrata_modak56207ce2009-03-23 13:35:39 +0000155 bad_addr =
156 mmap(0, 1, PROT_NONE, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0,
157 0);
vapier238755b2007-03-22 06:50:54 +0000158 if (bad_addr == MAP_FAILED)
159 tst_brkm(TBROK, cleanup, "mmap failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800160}
robbiew9a55a782002-12-26 20:45:16 +0000161
162/*
163* cleanup() - Performs one time cleanup for this test at
164* completion or premature exit
165*/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400166void cleanup(void)
robbiew9a55a782002-12-26 20:45:16 +0000167{
robbiew9a55a782002-12-26 20:45:16 +0000168
Chris Dearmanec6edca2012-10-17 19:54:01 -0700169}