blob: 1dbfcf050b151af41da4914ea9c58ee9ca5c5196 [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 : ptrace02
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 : functionality test for ptrace(2)
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 * This test case tests the functionality of ptrace() for
35 * PTRACE_TRACEME & PTRACE_CONT requests.
36 * Here, we fork a child & the child does ptrace(PTRACE_TRACEME, ...).
37 * Then a signal is delivered to the child & verified that parent
38 * is notified via wait(). then parent does ptrace(PTRACE_CONT, ..)
39 * to make the child to continue. Again parent wait() for child to finish.
40 * If child finished normally, test passes.
41 * We test two cases
42 * 1) By telling child to ignore SIGUSR2 signal
43 * 2) By installing a signal handler for child for SIGUSR2
44 * In both cases, child should stop & notify parent on reception
45 * of SIGUSR2
subrata_modak4bb656a2009-02-26 12:02:09 +000046 *
vapierd13d74b2006-02-11 07:24:00 +000047 * Setup:
48 * Setup signal handling.
49 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000050 *
vapierd13d74b2006-02-11 07:24:00 +000051 * Test:
52 * Loop if the proper options are given.
53 * setup signal handler for SIGUSR2 signal
54 * fork a child
55 *
56 * CHILD:
57 * setup signal handler for SIGUSR2 signal
58 * call ptrace() with PTRACE_TRACEME request
59 * send SIGUSR2 signal to self
60 * PARENT:
61 * wait() for child.
62 * if parent is notified when child gets a signal through wait(),
63 * then
64 * do ptrace(PTRACE_CONT, ..) on child
65 * wait() for child to finish,
66 * if child exited normaly,
67 * TEST passed
68 * else
69 * TEST failed
70 * else
71 * TEST failed
Garrett Cooper2c282152010-12-16 00:55:50 -080072 *
vapierd13d74b2006-02-11 07:24:00 +000073 * Cleanup:
74 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000075 *
vapierd13d74b2006-02-11 07:24:00 +000076 * USAGE: <for command-line>
77 * ptrace02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
78 * where, -c n : Run n copies concurrently.
79 * -e : Turn on errno logging.
80 * -h : Show help screen
81 * -f : Turn off functional testing
82 * -i n : Execute test n times.
83 * -I x : Execute test for x seconds.
84 * -p : Pause for SIGUSR1 before starting
85 * -P x : Pause for x seconds between iterations.
86 * -t : Turn on syscall timing.
87 *
88 ****************************************************************/
89
90#include <errno.h>
91#include <signal.h>
vapierd13d74b2006-02-11 07:24:00 +000092#include <sys/wait.h>
93
vapier919dca82009-11-03 19:42:12 +000094#include <config.h>
95#include "ptrace.h"
96
vapierd13d74b2006-02-11 07:24:00 +000097#include "test.h"
vapierd13d74b2006-02-11 07:24:00 +000098
99static void do_child(void);
100static void setup(void);
101static void cleanup(void);
102static void child_handler();
103static void parent_handler();
104
105static int got_signal = 0;
106
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200107char *TCID = "ptrace02";
subrata_modak56207ce2009-03-23 13:35:39 +0000108static int i; /* loop test case counter, shared with do_child */
vapierd13d74b2006-02-11 07:24:00 +0000109
110int TST_TOTAL = 2;
111
subrata_modak56207ce2009-03-23 13:35:39 +0000112int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +0000113{
114
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200115 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200116 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +0000117 pid_t child_pid;
118 int status;
119 struct sigaction parent_act;
subrata_modak56207ce2009-03-23 13:35:39 +0000120
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800121 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800122 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +0000123#ifdef UCLINUX
124 maybe_run_child(&do_child, "d", &i);
125#endif
126
vapierd13d74b2006-02-11 07:24:00 +0000127 setup();
128
vapierd13d74b2006-02-11 07:24:00 +0000129 for (lc = 0; TEST_LOOPING(lc); lc++) {
130
Caspar Zhangd59a6592013-03-07 14:59:12 +0800131 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000132
vapierd13d74b2006-02-11 07:24:00 +0000133 for (i = 0; i < TST_TOTAL; ++i) {
134 got_signal = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000135
vapierd13d74b2006-02-11 07:24:00 +0000136 /* Setup signal handler for parent */
137 if (i == 1) {
138 parent_act.sa_handler = parent_handler;
139 parent_act.sa_flags = SA_RESTART;
Jan Stancekc5532e42013-05-14 11:59:33 +0200140 sigemptyset(&parent_act.sa_mask);
vapierd13d74b2006-02-11 07:24:00 +0000141
142 if ((sigaction(SIGUSR2, &parent_act, NULL))
143 == -1) {
144 tst_resm(TWARN, "sigaction() failed"
subrata_modak56207ce2009-03-23 13:35:39 +0000145 " in parent");
vapierd13d74b2006-02-11 07:24:00 +0000146 continue;
147 }
148 }
149
150 switch (child_pid = FORK_OR_VFORK()) {
subrata_modakbdbaec52009-02-26 12:14:51 +0000151
vapierd13d74b2006-02-11 07:24:00 +0000152 case -1:
153 /* fork() failed */
154 tst_resm(TFAIL, "fork() failed");
155 continue;
156
157 case 0:
158 /* Child */
159#ifdef UCLINUX
160 if (self_exec(av[0], "d", i) < 0) {
161 tst_resm(TFAIL, "self_exec failed");
162 continue;
163 }
164#else
165 do_child();
166#endif
167
168 default:
169 /* Parent */
170 if ((waitpid(child_pid, &status, 0)) < 0) {
171 tst_resm(TFAIL, "waitpid() failed");
172 continue;
173 }
174
175 /*
176 * Check the exit status of child. If (it exits
177 * normally with exit value 1) OR (child came
178 * through signal handler), Test Failed
179 */
180
181 if (((WIFEXITED(status)) &&
subrata_modak56207ce2009-03-23 13:35:39 +0000182 (WEXITSTATUS(status))) ||
vapierd13d74b2006-02-11 07:24:00 +0000183 (got_signal == 1)) {
184 tst_resm(TFAIL, "Test Failed");
185 continue;
186 } else {
187 /* Restart child */
188 if ((ptrace(PTRACE_CONT, child_pid,
189 0, 0)) == -1) {
190 tst_resm(TFAIL, "Test Failed:"
subrata_modak56207ce2009-03-23 13:35:39 +0000191 " Parent was not able to do"
192 " ptrace(PTRACE_CONT, ..) on"
193 " child");
vapierd13d74b2006-02-11 07:24:00 +0000194 continue;
195 }
196 }
197
198 if ((waitpid(child_pid, &status, 0)) < 0) {
199 tst_resm(TFAIL, "waitpid() failed");
200 continue;
201 }
202
203 if (WIFEXITED(status)) {
204 /* Child exits normally */
205 tst_resm(TPASS, "Test Passed");
206 } else {
207 tst_resm(TFAIL, "Test Failed");
208 }
209
210 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000211 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800212 }
vapierd13d74b2006-02-11 07:24:00 +0000213
214 /* cleanup and exit */
215 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800216 tst_exit();
vapierd13d74b2006-02-11 07:24:00 +0000217
Garrett Cooper2c282152010-12-16 00:55:50 -0800218}
vapierd13d74b2006-02-11 07:24:00 +0000219
220/* do_child() */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400221void do_child(void)
vapierd13d74b2006-02-11 07:24:00 +0000222{
223 struct sigaction child_act;
224
225 /* Setup signal handler for child */
226 if (i == 0) {
227 child_act.sa_handler = SIG_IGN;
228 } else {
229 child_act.sa_handler = child_handler;
230 }
231 child_act.sa_flags = SA_RESTART;
Jan Stancekc5532e42013-05-14 11:59:33 +0200232 sigemptyset(&child_act.sa_mask);
subrata_modakbdbaec52009-02-26 12:14:51 +0000233
vapierd13d74b2006-02-11 07:24:00 +0000234 if ((sigaction(SIGUSR2, &child_act, NULL)) == -1) {
235 tst_resm(TWARN, "sigaction() failed in child");
236 exit(1);
237 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000238
subrata_modak56207ce2009-03-23 13:35:39 +0000239 if ((ptrace(PTRACE_TRACEME, 0, 0, 0)) == -1) {
vapierd13d74b2006-02-11 07:24:00 +0000240 tst_resm(TWARN, "ptrace() failed in child");
241 exit(1);
242 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000243
vapierd13d74b2006-02-11 07:24:00 +0000244 /* ensure that child bypasses signal handler */
245 if ((kill(getpid(), SIGUSR2)) == -1) {
246 tst_resm(TWARN, "kill() failed in child");
247 exit(1);
248 }
249 exit(1);
250}
subrata_modakbdbaec52009-02-26 12:14:51 +0000251
vapierd13d74b2006-02-11 07:24:00 +0000252/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400253void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000254{
subrata_modakbdbaec52009-02-26 12:14:51 +0000255
vapierd13d74b2006-02-11 07:24:00 +0000256 tst_sig(FORK, DEF_HANDLER, cleanup);
257
vapierd13d74b2006-02-11 07:24:00 +0000258 TEST_PAUSE;
259
Garrett Cooper2c282152010-12-16 00:55:50 -0800260}
vapierd13d74b2006-02-11 07:24:00 +0000261
262/*
263 *cleanup() - performs all ONE TIME cleanup for this test at
264 * completion or premature exit.
265 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400266void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000267{
268
Garrett Cooper2c282152010-12-16 00:55:50 -0800269}
vapierd13d74b2006-02-11 07:24:00 +0000270
271/*
272 * child_handler() - Signal handler for child
273 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400274void child_handler(void)
vapierd13d74b2006-02-11 07:24:00 +0000275{
276
277 if ((kill(getppid(), SIGUSR2)) == -1) {
278 tst_resm(TWARN, "kill() failed in child_handler()");
279 exit(1);
280 }
281}
282
283/*
284 * parent_handler() - Signal handler for parent
285 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400286void parent_handler(void)
vapierd13d74b2006-02-11 07:24:00 +0000287{
288
289 got_signal = 1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700290}