blob: e9d8c8b1f109e575052877652ef3b2711b72518c [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +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.
vapierd13d74b2006-02-11 07:24:00 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
vapierd13d74b2006-02-11 07:24:00 +000019 * TEST IDENTIFIER : prctl02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
vapierd13d74b2006-02-11 07:24:00 +000021 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
vapierd13d74b2006-02-11 07:24:00 +000023 * TEST TITLE : Tests for error conditions
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
vapierd13d74b2006-02-11 07:24:00 +000025 * TEST CASE TOTAL : 2
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
vapierd13d74b2006-02-11 07:24:00 +000027 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
vapierd13d74b2006-02-11 07:24:00 +000029 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
33 * DESCRIPTION
34 * Verify that
35 * 1) prctl() fails with errno, EINVAL when an invalid value is given for
36 * option
37 * 2) prctl() fails with errno, EINVAL when option is PR_SET_PDEATHSIG
38 * & arg2 is not zero or a valid signal number
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
vapierd13d74b2006-02-11 07:24:00 +000040 * Setup:
41 * Setup signal handling.
42 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
vapierd13d74b2006-02-11 07:24:00 +000044 * Test:
45 * Loop if the proper options are given.
46 * fork a child
47 *
48 * CHILD:
49 * call prctl() with proper arguments
50 * If call fails with expected errno,
51 * exit with 0
52 * else
53 * exit with 1
54 * PARENT:
55 * wait() for child.
56 * If child exits with exit value 0,
57 * Test passed
58 * else
59 * Test Failed
Garrett Cooper2c282152010-12-16 00:55:50 -080060 *
vapierd13d74b2006-02-11 07:24:00 +000061 * Cleanup:
62 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000063 *
vapierd13d74b2006-02-11 07:24:00 +000064 * USAGE: <for command-line>
65 * prctl02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
66 * where, -c n : Run n copies concurrently.
67 * -e : Turn on errno logging.
68 * -h : Show help screen
69 * -f : Turn off functional testing
70 * -i n : Execute test n times.
71 * -I x : Execute test for x seconds.
72 * -p : Pause for SIGUSR1 before starting
73 * -P x : Pause for x seconds between iterations.
74 * -t : Turn on syscall timing.
75 *
76 ****************************************************************/
77
78#include <errno.h>
79#include <signal.h>
80#include <sys/prctl.h>
81#include <sys/wait.h>
82
83#include "test.h"
vapierd13d74b2006-02-11 07:24:00 +000084
85#define OPTION_INVALID 999
86#define INVALID_ARG 999
87
88static void setup(void);
89static void cleanup(void);
90
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091char *TCID = "prctl02";
vapierd13d74b2006-02-11 07:24:00 +000092
93struct test_cases_t {
94 int option;
95 unsigned long arg2;
96 int exp_errno;
97} test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000098 {
99 OPTION_INVALID, 0, EINVAL}, {
100 PR_SET_PDEATHSIG, INVALID_ARG, EINVAL}
vapierd13d74b2006-02-11 07:24:00 +0000101};
subrata_modakbdbaec52009-02-26 12:14:51 +0000102
vapierd13d74b2006-02-11 07:24:00 +0000103int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +0000106{
107
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200108 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200109 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +0000110 pid_t child_pid;
111 int status;
112
Garrett Coopera9e49f12010-12-16 10:54:03 -0800113 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800114 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +0000115
vapierd13d74b2006-02-11 07:24:00 +0000116 setup();
117
vapierd13d74b2006-02-11 07:24:00 +0000118 for (lc = 0; TEST_LOOPING(lc); lc++) {
119
Caspar Zhangd59a6592013-03-07 14:59:12 +0800120 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +0000121
122 for (i = 0; i < TST_TOTAL; ++i) {
123
mridgeed06ca62006-02-14 16:55:09 +0000124 switch (child_pid = FORK_OR_VFORK()) {
vapierd13d74b2006-02-11 07:24:00 +0000125
126 case -1:
127 /* fork() failed */
128 tst_resm(TFAIL, "fork() failed");
129 continue;
130
131 case 0:
132 /* Child */
133
134 TEST(prctl(test_cases[i].option,
135 test_cases[i].arg2));
136 if ((TEST_RETURN == -1) && (TEST_ERRNO ==
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 test_cases
138 [i].exp_errno)) {
vapierd13d74b2006-02-11 07:24:00 +0000139 exit(TEST_ERRNO);
140 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_resm(TWARN | TTERRNO,
142 "prctl() returned %ld",
subrata_modak923b23f2009-11-02 13:57:16 +0000143 TEST_RETURN);
vapierd13d74b2006-02-11 07:24:00 +0000144 exit(TEST_ERRNO);
145 }
146
147 default:
148 /* Parent */
149 if ((waitpid(child_pid, &status, 0)) < 0) {
150 tst_resm(TFAIL, "waitpid() failed");
151 continue;
152 }
153
154 if ((WIFEXITED(status)) && (WEXITSTATUS(status)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 ==
156 test_cases
157 [i].exp_errno)) {
vapierd13d74b2006-02-11 07:24:00 +0000158 tst_resm(TPASS, "Test Passed");
159 } else {
160 tst_resm(TFAIL, "Test Failed");
161 }
vapierd13d74b2006-02-11 07:24:00 +0000162
163 }
164 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800165 }
vapierd13d74b2006-02-11 07:24:00 +0000166
167 /* cleanup and exit */
168 cleanup();
169
Garrett Cooper53740502010-12-16 00:04:01 -0800170 tst_exit();
vapierd13d74b2006-02-11 07:24:00 +0000171
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
vapierd13d74b2006-02-11 07:24:00 +0000173
174/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000176{
177
vapierd13d74b2006-02-11 07:24:00 +0000178 tst_sig(FORK, DEF_HANDLER, cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000179
vapierd13d74b2006-02-11 07:24:00 +0000180 TEST_PAUSE;
181
Garrett Cooper2c282152010-12-16 00:55:50 -0800182}
vapierd13d74b2006-02-11 07:24:00 +0000183
184/*
185 *cleanup() - performs all ONE TIME cleanup for this test at
186 * completion or premature exit.
187 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400188void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000189{
190
Chris Dearmanec6edca2012-10-17 19:54:01 -0700191}