subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | ****************************************************************************** |
| 3 | * |
| 4 | * ptrace05 - an app which ptraces itself as per arbitrarily specified signals, |
| 5 | * over a user specified range. |
| 6 | * |
| 7 | * Copyright (C) 2009, Garrett Cooper |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | ****************************************************************************** |
| 24 | */ |
| 25 | |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <signal.h> |
| 29 | #include <errno.h> |
| 30 | #include <libgen.h> |
| 31 | #include <math.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <unistd.h> |
vapier | 919dca8 | 2009-11-03 19:42:12 +0000 | [diff] [blame] | 36 | |
| 37 | #include <config.h> |
| 38 | #include "ptrace.h" |
| 39 | |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 40 | #include "test.h" |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 41 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 42 | char *TCID = "ptrace05"; |
| 43 | int TST_TOTAL = 0; |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 44 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | int usage(const char *); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 46 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 47 | int usage(const char *argv0) |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 48 | { |
| 49 | fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0); |
| 50 | return 1; |
| 51 | } |
| 52 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 53 | int main(int argc, char **argv) |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 54 | { |
| 55 | |
| 56 | int end_signum = -1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 57 | int signum; |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 58 | int start_signum = -1; |
| 59 | int status; |
| 60 | |
| 61 | pid_t child; |
| 62 | |
| 63 | /* Parse the CLI args appropriately. */ |
| 64 | switch (argc) { |
| 65 | case 3: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | end_signum = (int)strtol((const char *)*(argv + 2), NULL, 10); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 67 | /* Parse the signal value. */ |
| 68 | if (end_signum == 0 && errno != 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 69 | tst_brkm(TBROK, NULL, |
| 70 | "argument (%s) isn't a valid number.\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | *(argv + 2)); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 72 | } |
| 73 | /* FALLTHROUGH */ |
| 74 | case 2: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | start_signum = (int)strtol((const char *)*(argv + 1), NULL, 10); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 76 | /* Parse the signal value. */ |
| 77 | if (end_signum == 0 && errno != 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 78 | tst_brkm(TBROK, NULL, |
| 79 | "argument (%s) isn't a valid number.\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | *(argv + 1)); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | case 1: |
| 84 | /* Do nothing. */ |
| 85 | break; |
| 86 | default: |
| 87 | return usage(basename(*argv)); |
| 88 | } |
| 89 | |
| 90 | if (start_signum == -1) { |
| 91 | start_signum = 0; |
| 92 | } |
| 93 | if (end_signum == -1) { |
| 94 | end_signum = SIGRTMAX; |
| 95 | } |
| 96 | |
| 97 | for (signum = start_signum; signum <= end_signum; signum++) { |
| 98 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 99 | switch (child = fork()) { |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 100 | case -1: |
| 101 | tst_resm(TBROK | TERRNO, "Failed to fork properly."); |
| 102 | break; |
| 103 | case 0: |
| 104 | |
| 105 | if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) { |
| 106 | tst_resm(TINFO, "[child] Sending kill(.., %d)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | signum); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 108 | if (kill(getpid(), signum) < 0) { |
| 109 | tst_resm(TINFO | TERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | "[child] kill(.., %d) failed.", |
| 111 | signum); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 112 | } |
| 113 | } else { |
| 114 | |
| 115 | /* |
| 116 | * This won't increment the TST_COUNT var. |
| 117 | * properly, but it'll show up as a failure |
| 118 | * nonetheless. |
| 119 | */ |
| 120 | tst_resm(TFAIL | TERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | "Failed to ptrace(PTRACE_TRACEME, ...) " |
| 122 | "properly"); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 123 | |
| 124 | } |
| 125 | /* Shouldn't get here if signum == 0. */ |
| 126 | exit((signum == 0 ? 0 : 2)); |
| 127 | break; |
| 128 | |
| 129 | default: |
| 130 | |
| 131 | waitpid(child, &status, 0); |
| 132 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | switch (signum) { |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 134 | case 0: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | if (WIFEXITED(status) |
| 136 | && WEXITSTATUS(status) == 0) { |
| 137 | tst_resm(TPASS, |
| 138 | "kill(.., 0) exited " |
| 139 | "with 0, as expected."); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 140 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_resm(TFAIL, |
| 142 | "kill(.., 0) didn't exit " |
| 143 | "with 0."); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 144 | } |
| 145 | break; |
| 146 | case SIGKILL: |
| 147 | if (WIFSIGNALED(status)) { |
| 148 | /* SIGKILL must be uncatchable. */ |
| 149 | if (WTERMSIG(status) == SIGKILL) { |
| 150 | tst_resm(TPASS, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | "Killed with SIGKILL, " |
| 152 | "as expected."); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 153 | } else { |
| 154 | tst_resm(TPASS, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | "Didn't die with " |
| 156 | "SIGKILL (?!) "); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 157 | } |
| 158 | } else if (WIFEXITED(status)) { |
| 159 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | "Exited unexpectedly instead " |
| 161 | "of dying with SIGKILL."); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 162 | } else if (WIFSTOPPED(status)) { |
| 163 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | "Stopped instead of dying " |
| 165 | "with SIGKILL."); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 166 | } |
| 167 | break; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | /* All other processes should be stopped. */ |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 169 | default: |
| 170 | if (WIFSTOPPED(status)) { |
| 171 | tst_resm(TPASS, "Stopped as expected"); |
| 172 | } else { |
| 173 | tst_resm(TFAIL, "Didn't stop as " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | "expected."); |
| 175 | if (kill(child, 0)) { |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 176 | tst_resm(TINFO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 177 | "Is still alive!?"); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 178 | } else if (WIFEXITED(status)) { |
| 179 | tst_resm(TINFO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | "Exited normally"); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 181 | } else if (WIFSIGNALED(status)) { |
| 182 | tst_resm(TINFO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 183 | "Was signaled with " |
| 184 | "signum=%d", |
| 185 | WTERMSIG(status)); |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | break; |
| 191 | |
| 192 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 193 | |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 194 | } |
| 195 | /* Make sure the child dies a quick and painless death ... */ |
| 196 | kill(child, 9); |
| 197 | |
| 198 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 199 | |
subrata_modak | 27274d8 | 2009-10-29 18:15:37 +0000 | [diff] [blame] | 200 | tst_exit(); |
| 201 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 202 | } |