vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 1 | /* |
| 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : ptrace02 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 21 | * EXECUTED BY : anyone |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 23 | * TEST TITLE : functionality test for ptrace(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 2 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 27 | * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 29 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 46 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 47 | * Setup: |
| 48 | * Setup signal handling. |
| 49 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 50 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 51 | * 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 72 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 73 | * Cleanup: |
| 74 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 75 | * |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 76 | * 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> |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 92 | #include <sys/wait.h> |
| 93 | |
vapier | 919dca8 | 2009-11-03 19:42:12 +0000 | [diff] [blame] | 94 | #include <config.h> |
| 95 | #include "ptrace.h" |
| 96 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 97 | #include "test.h" |
| 98 | #include "usctest.h" |
| 99 | |
| 100 | static void do_child(void); |
| 101 | static void setup(void); |
| 102 | static void cleanup(void); |
| 103 | static void child_handler(); |
| 104 | static void parent_handler(); |
| 105 | |
| 106 | static int got_signal = 0; |
| 107 | |
| 108 | char *TCID = "ptrace02"; /* Test program identifier. */ |
| 109 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | static int i; /* loop test case counter, shared with do_child */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 111 | |
| 112 | int TST_TOTAL = 2; |
| 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | int main(int ac, char **av) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 115 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 123 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 124 | if ((msg = parse_opts(ac, av, NULL, NULL)) |
| 125 | != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 127 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 128 | #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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 141 | for (i = 0; i < TST_TOTAL; ++i) { |
| 142 | got_signal = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 144 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | " in parent"); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 153 | continue; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | switch (child_pid = FORK_OR_VFORK()) { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 158 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 159 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | (WEXITSTATUS(status))) || |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 190 | (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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 198 | " Parent was not able to do" |
| 199 | " ptrace(PTRACE_CONT, ..) on" |
| 200 | " child"); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 201 | 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 218 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | } /* End for TEST_LOOPING */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 220 | |
| 221 | /* cleanup and exit */ |
| 222 | cleanup(); |
| 223 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | /*NOTREACHED*/ return 0; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 225 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | } /* End main */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 227 | |
| 228 | /* do_child() */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | void do_child() |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 230 | { |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 240 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 241 | if ((sigaction(SIGUSR2, &child_act, NULL)) == -1) { |
| 242 | tst_resm(TWARN, "sigaction() failed in child"); |
| 243 | exit(1); |
| 244 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 245 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 246 | if ((ptrace(PTRACE_TRACEME, 0, 0, 0)) == -1) { |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 247 | tst_resm(TWARN, "ptrace() failed in child"); |
| 248 | exit(1); |
| 249 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 250 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 251 | /* 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 258 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 259 | /* setup() - performs all ONE TIME setup for this test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 260 | void setup() |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 261 | { |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 262 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 263 | /* capture signals */ |
| 264 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 265 | |
| 266 | /* Pause if that option was specified */ |
| 267 | TEST_PAUSE; |
| 268 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | } /* End setup() */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | *cleanup() - performs all ONE TIME cleanup for this test at |
| 273 | * completion or premature exit. |
| 274 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 275 | void cleanup() |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 276 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 286 | } /* End cleanup() */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * child_handler() - Signal handler for child |
| 290 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 291 | void child_handler() |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 292 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 303 | void parent_handler() |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 304 | { |
| 305 | |
| 306 | got_signal = 1; |
| 307 | } |