blob: d610cc0aa9b6a1feec89fbf76ab9ca4e17cfb690 [file] [log] [blame]
subrata_modak27274d82009-10-29 18:15:37 +00001/*
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_modak27274d82009-10-29 18:15:37 +000026#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>
vapier919dca82009-11-03 19:42:12 +000036
37#include <config.h>
38#include "ptrace.h"
39
subrata_modak27274d82009-10-29 18:15:37 +000040#include "test.h"
subrata_modak27274d82009-10-29 18:15:37 +000041
Wanlong Gao354ebb42012-12-07 10:10:04 +080042char *TCID = "ptrace05";
43int TST_TOTAL = 0;
subrata_modak27274d82009-10-29 18:15:37 +000044
Wanlong Gao354ebb42012-12-07 10:10:04 +080045int usage(const char *);
subrata_modak27274d82009-10-29 18:15:37 +000046
Wanlong Gao354ebb42012-12-07 10:10:04 +080047int usage(const char *argv0)
subrata_modak27274d82009-10-29 18:15:37 +000048{
49 fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0);
50 return 1;
51}
52
Wanlong Gao354ebb42012-12-07 10:10:04 +080053int main(int argc, char **argv)
subrata_modak27274d82009-10-29 18:15:37 +000054{
55
56 int end_signum = -1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080057 int signum;
subrata_modak27274d82009-10-29 18:15:37 +000058 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 Gao354ebb42012-12-07 10:10:04 +080066 end_signum = (int)strtol((const char *)*(argv + 2), NULL, 10);
subrata_modak27274d82009-10-29 18:15:37 +000067 /* Parse the signal value. */
68 if (end_signum == 0 && errno != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010069 tst_brkm(TBROK, NULL,
70 "argument (%s) isn't a valid number.\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +080071 *(argv + 2));
subrata_modak27274d82009-10-29 18:15:37 +000072 }
73 /* FALLTHROUGH */
74 case 2:
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 start_signum = (int)strtol((const char *)*(argv + 1), NULL, 10);
subrata_modak27274d82009-10-29 18:15:37 +000076 /* Parse the signal value. */
77 if (end_signum == 0 && errno != 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +010078 tst_brkm(TBROK, NULL,
79 "argument (%s) isn't a valid number.\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 *(argv + 1));
subrata_modak27274d82009-10-29 18:15:37 +000081 }
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 Cooper2c282152010-12-16 00:55:50 -080099 switch (child = fork()) {
subrata_modak27274d82009-10-29 18:15:37 +0000100 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 Gao354ebb42012-12-07 10:10:04 +0800107 signum);
subrata_modak27274d82009-10-29 18:15:37 +0000108 if (kill(getpid(), signum) < 0) {
109 tst_resm(TINFO | TERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 "[child] kill(.., %d) failed.",
111 signum);
subrata_modak27274d82009-10-29 18:15:37 +0000112 }
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 Gao354ebb42012-12-07 10:10:04 +0800121 "Failed to ptrace(PTRACE_TRACEME, ...) "
122 "properly");
subrata_modak27274d82009-10-29 18:15:37 +0000123
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 Gao354ebb42012-12-07 10:10:04 +0800133 switch (signum) {
subrata_modak27274d82009-10-29 18:15:37 +0000134 case 0:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 if (WIFEXITED(status)
136 && WEXITSTATUS(status) == 0) {
137 tst_resm(TPASS,
138 "kill(.., 0) exited "
139 "with 0, as expected.");
subrata_modak27274d82009-10-29 18:15:37 +0000140 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_resm(TFAIL,
142 "kill(.., 0) didn't exit "
143 "with 0.");
subrata_modak27274d82009-10-29 18:15:37 +0000144 }
145 break;
146 case SIGKILL:
147 if (WIFSIGNALED(status)) {
148 /* SIGKILL must be uncatchable. */
149 if (WTERMSIG(status) == SIGKILL) {
150 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 "Killed with SIGKILL, "
152 "as expected.");
subrata_modak27274d82009-10-29 18:15:37 +0000153 } else {
154 tst_resm(TPASS,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 "Didn't die with "
156 "SIGKILL (?!) ");
subrata_modak27274d82009-10-29 18:15:37 +0000157 }
158 } else if (WIFEXITED(status)) {
159 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 "Exited unexpectedly instead "
161 "of dying with SIGKILL.");
subrata_modak27274d82009-10-29 18:15:37 +0000162 } else if (WIFSTOPPED(status)) {
163 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 "Stopped instead of dying "
165 "with SIGKILL.");
subrata_modak27274d82009-10-29 18:15:37 +0000166 }
167 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 /* All other processes should be stopped. */
subrata_modak27274d82009-10-29 18:15:37 +0000169 default:
170 if (WIFSTOPPED(status)) {
171 tst_resm(TPASS, "Stopped as expected");
172 } else {
173 tst_resm(TFAIL, "Didn't stop as "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 "expected.");
175 if (kill(child, 0)) {
subrata_modak27274d82009-10-29 18:15:37 +0000176 tst_resm(TINFO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 "Is still alive!?");
subrata_modak27274d82009-10-29 18:15:37 +0000178 } else if (WIFEXITED(status)) {
179 tst_resm(TINFO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 "Exited normally");
subrata_modak27274d82009-10-29 18:15:37 +0000181 } else if (WIFSIGNALED(status)) {
182 tst_resm(TINFO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 "Was signaled with "
184 "signum=%d",
185 WTERMSIG(status));
subrata_modak27274d82009-10-29 18:15:37 +0000186 }
187
188 }
189
190 break;
191
192 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800193
subrata_modak27274d82009-10-29 18:15:37 +0000194 }
195 /* Make sure the child dies a quick and painless death ... */
196 kill(child, 9);
197
198 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800199
subrata_modak27274d82009-10-29 18:15:37 +0000200 tst_exit();
201
Chris Dearmanec6edca2012-10-17 19:54:01 -0700202}