blob: 6bf496196fb7bf3ba15707d7f811c0d84bd4c93f [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
13 * with this program; if not, write the Free Software Foundation, Inc., 59
14 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
15 *
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
subrata_modakbdbaec52009-02-26 12:14:51 +000072 *
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"
98#include "usctest.h"
99
100static void do_child(void);
101static void setup(void);
102static void cleanup(void);
103static void child_handler();
104static void parent_handler();
105
106static int got_signal = 0;
107
108char *TCID = "ptrace02"; /* Test program identifier. */
109extern int Tst_count; /* Test Case counter for tst_* routines */
subrata_modak56207ce2009-03-23 13:35:39 +0000110static int i; /* loop test case counter, shared with do_child */
vapierd13d74b2006-02-11 07:24:00 +0000111
112int TST_TOTAL = 2;
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +0000115{
116
117 int lc; /* loop counter */
118 char *msg; /* message returned from parse_opts */
119 pid_t child_pid;
120 int status;
121 struct sigaction parent_act;
subrata_modak56207ce2009-03-23 13:35:39 +0000122
vapierd13d74b2006-02-11 07:24:00 +0000123 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800124 if ((msg = parse_opts(ac, av, NULL, NULL))
125 != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +0000127 }
vapierd13d74b2006-02-11 07:24:00 +0000128#ifdef UCLINUX
129 maybe_run_child(&do_child, "d", &i);
130#endif
131
132 /* perform global setup for test */
133 setup();
134
135 /* check looping state if -i option given */
136 for (lc = 0; TEST_LOOPING(lc); lc++) {
137
138 /* reset Tst_count in case we are looping. */
139 Tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000140
vapierd13d74b2006-02-11 07:24:00 +0000141 for (i = 0; i < TST_TOTAL; ++i) {
142 got_signal = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000143
vapierd13d74b2006-02-11 07:24:00 +0000144 /* Setup signal handler for parent */
145 if (i == 1) {
146 parent_act.sa_handler = parent_handler;
147 parent_act.sa_flags = SA_RESTART;
148
149 if ((sigaction(SIGUSR2, &parent_act, NULL))
150 == -1) {
151 tst_resm(TWARN, "sigaction() failed"
subrata_modak56207ce2009-03-23 13:35:39 +0000152 " in parent");
vapierd13d74b2006-02-11 07:24:00 +0000153 continue;
154 }
155 }
156
157 switch (child_pid = FORK_OR_VFORK()) {
subrata_modakbdbaec52009-02-26 12:14:51 +0000158
vapierd13d74b2006-02-11 07:24:00 +0000159 case -1:
160 /* fork() failed */
161 tst_resm(TFAIL, "fork() failed");
162 continue;
163
164 case 0:
165 /* Child */
166#ifdef UCLINUX
167 if (self_exec(av[0], "d", i) < 0) {
168 tst_resm(TFAIL, "self_exec failed");
169 continue;
170 }
171#else
172 do_child();
173#endif
174
175 default:
176 /* Parent */
177 if ((waitpid(child_pid, &status, 0)) < 0) {
178 tst_resm(TFAIL, "waitpid() failed");
179 continue;
180 }
181
182 /*
183 * Check the exit status of child. If (it exits
184 * normally with exit value 1) OR (child came
185 * through signal handler), Test Failed
186 */
187
188 if (((WIFEXITED(status)) &&
subrata_modak56207ce2009-03-23 13:35:39 +0000189 (WEXITSTATUS(status))) ||
vapierd13d74b2006-02-11 07:24:00 +0000190 (got_signal == 1)) {
191 tst_resm(TFAIL, "Test Failed");
192 continue;
193 } else {
194 /* Restart child */
195 if ((ptrace(PTRACE_CONT, child_pid,
196 0, 0)) == -1) {
197 tst_resm(TFAIL, "Test Failed:"
subrata_modak56207ce2009-03-23 13:35:39 +0000198 " Parent was not able to do"
199 " ptrace(PTRACE_CONT, ..) on"
200 " child");
vapierd13d74b2006-02-11 07:24:00 +0000201 continue;
202 }
203 }
204
205 if ((waitpid(child_pid, &status, 0)) < 0) {
206 tst_resm(TFAIL, "waitpid() failed");
207 continue;
208 }
209
210 if (WIFEXITED(status)) {
211 /* Child exits normally */
212 tst_resm(TPASS, "Test Passed");
213 } else {
214 tst_resm(TFAIL, "Test Failed");
215 }
216
217 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000218 }
subrata_modak56207ce2009-03-23 13:35:39 +0000219 } /* End for TEST_LOOPING */
vapierd13d74b2006-02-11 07:24:00 +0000220
221 /* cleanup and exit */
222 cleanup();
223
subrata_modak56207ce2009-03-23 13:35:39 +0000224 /*NOTREACHED*/ return 0;
vapierd13d74b2006-02-11 07:24:00 +0000225
subrata_modak56207ce2009-03-23 13:35:39 +0000226} /* End main */
vapierd13d74b2006-02-11 07:24:00 +0000227
228/* do_child() */
subrata_modak56207ce2009-03-23 13:35:39 +0000229void do_child()
vapierd13d74b2006-02-11 07:24:00 +0000230{
231 struct sigaction child_act;
232
233 /* Setup signal handler for child */
234 if (i == 0) {
235 child_act.sa_handler = SIG_IGN;
236 } else {
237 child_act.sa_handler = child_handler;
238 }
239 child_act.sa_flags = SA_RESTART;
subrata_modakbdbaec52009-02-26 12:14:51 +0000240
vapierd13d74b2006-02-11 07:24:00 +0000241 if ((sigaction(SIGUSR2, &child_act, NULL)) == -1) {
242 tst_resm(TWARN, "sigaction() failed in child");
243 exit(1);
244 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000245
subrata_modak56207ce2009-03-23 13:35:39 +0000246 if ((ptrace(PTRACE_TRACEME, 0, 0, 0)) == -1) {
vapierd13d74b2006-02-11 07:24:00 +0000247 tst_resm(TWARN, "ptrace() failed in child");
248 exit(1);
249 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000250
vapierd13d74b2006-02-11 07:24:00 +0000251 /* ensure that child bypasses signal handler */
252 if ((kill(getpid(), SIGUSR2)) == -1) {
253 tst_resm(TWARN, "kill() failed in child");
254 exit(1);
255 }
256 exit(1);
257}
subrata_modakbdbaec52009-02-26 12:14:51 +0000258
vapierd13d74b2006-02-11 07:24:00 +0000259/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000260void setup()
vapierd13d74b2006-02-11 07:24:00 +0000261{
subrata_modakbdbaec52009-02-26 12:14:51 +0000262
vapierd13d74b2006-02-11 07:24:00 +0000263 /* capture signals */
264 tst_sig(FORK, DEF_HANDLER, cleanup);
265
266 /* Pause if that option was specified */
267 TEST_PAUSE;
268
subrata_modak56207ce2009-03-23 13:35:39 +0000269} /* End setup() */
vapierd13d74b2006-02-11 07:24:00 +0000270
271/*
272 *cleanup() - performs all ONE TIME cleanup for this test at
273 * completion or premature exit.
274 */
subrata_modak56207ce2009-03-23 13:35:39 +0000275void cleanup()
vapierd13d74b2006-02-11 07:24:00 +0000276{
277
278 /*
279 * print timing stats if that option was specified.
280 * print errno log if that option was specified.
281 */
282 TEST_CLEANUP;
283
284 /* exit with return code appropriate for results */
285 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000286} /* End cleanup() */
vapierd13d74b2006-02-11 07:24:00 +0000287
288/*
289 * child_handler() - Signal handler for child
290 */
subrata_modak56207ce2009-03-23 13:35:39 +0000291void child_handler()
vapierd13d74b2006-02-11 07:24:00 +0000292{
293
294 if ((kill(getppid(), SIGUSR2)) == -1) {
295 tst_resm(TWARN, "kill() failed in child_handler()");
296 exit(1);
297 }
298}
299
300/*
301 * parent_handler() - Signal handler for parent
302 */
subrata_modak56207ce2009-03-23 13:35:39 +0000303void parent_handler()
vapierd13d74b2006-02-11 07:24:00 +0000304{
305
306 got_signal = 1;
307}