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
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>
36
37 #include <config.h>
38 #include "ptrace.h"
39
40 #include "test.h"
41
42 char *TCID = "ptrace05";
43 int TST_TOTAL = 0;
44
45 int usage(const char *);
46
usage(const char * argv0)47 int usage(const char *argv0)
48 {
49 fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0);
50 return 1;
51 }
52
main(int argc,char ** argv)53 int main(int argc, char **argv)
54 {
55
56 int end_signum = -1;
57 int signum;
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:
66 end_signum = (int)strtol((const char *)*(argv + 2), NULL, 10);
67 /* Parse the signal value. */
68 if (end_signum == 0 && errno != 0) {
69 tst_brkm(TBROK, NULL,
70 "argument (%s) isn't a valid number.\n",
71 *(argv + 2));
72 }
73 /* FALLTHROUGH */
74 case 2:
75 start_signum = (int)strtol((const char *)*(argv + 1), NULL, 10);
76 /* Parse the signal value. */
77 if (end_signum == 0 && errno != 0) {
78 tst_brkm(TBROK, NULL,
79 "argument (%s) isn't a valid number.\n",
80 *(argv + 1));
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
99 switch (child = fork()) {
100 case -1:
101 tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
102 case 0:
103
104 if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) {
105 tst_resm(TINFO, "[child] Sending kill(.., %d)",
106 signum);
107 if (kill(getpid(), signum) < 0) {
108 tst_resm(TINFO | TERRNO,
109 "[child] kill(.., %d) failed.",
110 signum);
111 }
112 } else {
113
114 /*
115 * This won't increment the TST_COUNT var.
116 * properly, but it'll show up as a failure
117 * nonetheless.
118 */
119 tst_resm(TFAIL | TERRNO,
120 "Failed to ptrace(PTRACE_TRACEME, ...) "
121 "properly");
122
123 }
124 /* Shouldn't get here if signum == 0. */
125 exit((signum == 0 ? 0 : 2));
126 break;
127
128 default:
129
130 waitpid(child, &status, 0);
131
132 switch (signum) {
133 case 0:
134 if (WIFEXITED(status)
135 && WEXITSTATUS(status) == 0) {
136 tst_resm(TPASS,
137 "kill(.., 0) exited "
138 "with 0, as expected.");
139 } else {
140 tst_resm(TFAIL,
141 "kill(.., 0) didn't exit "
142 "with 0.");
143 }
144 break;
145 case SIGKILL:
146 if (WIFSIGNALED(status)) {
147 /* SIGKILL must be uncatchable. */
148 if (WTERMSIG(status) == SIGKILL) {
149 tst_resm(TPASS,
150 "Killed with SIGKILL, "
151 "as expected.");
152 } else {
153 tst_resm(TPASS,
154 "Didn't die with "
155 "SIGKILL (?!) ");
156 }
157 } else if (WIFEXITED(status)) {
158 tst_resm(TFAIL,
159 "Exited unexpectedly instead "
160 "of dying with SIGKILL.");
161 } else if (WIFSTOPPED(status)) {
162 tst_resm(TFAIL,
163 "Stopped instead of dying "
164 "with SIGKILL.");
165 }
166 break;
167 /* All other processes should be stopped. */
168 default:
169 if (WIFSTOPPED(status)) {
170 tst_resm(TPASS, "Stopped as expected");
171 } else {
172 tst_resm(TFAIL, "Didn't stop as "
173 "expected.");
174 if (kill(child, 0)) {
175 tst_resm(TINFO,
176 "Is still alive!?");
177 } else if (WIFEXITED(status)) {
178 tst_resm(TINFO,
179 "Exited normally");
180 } else if (WIFSIGNALED(status)) {
181 tst_resm(TINFO,
182 "Was signaled with "
183 "signum=%d",
184 WTERMSIG(status));
185 }
186
187 }
188
189 break;
190
191 }
192
193 }
194 /* Make sure the child dies a quick and painless death ... */
195 kill(child, 9);
196
197 }
198
199 tst_exit();
200
201 }
202