• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, Ngie 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 #include "lapi/signal.h"
42 
43 char *TCID = "ptrace05";
44 int TST_TOTAL = 0;
45 
46 int usage(const char *);
47 
usage(const char * argv0)48 int usage(const char *argv0)
49 {
50 	fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0);
51 	return 1;
52 }
53 
main(int argc,char ** argv)54 int main(int argc, char **argv)
55 {
56 
57 	int end_signum = -1;
58 	int signum;
59 	int start_signum = -1;
60 	int status;
61 
62 	pid_t child;
63 
64 	tst_parse_opts(argc, argv, NULL, NULL);
65 
66 	if (start_signum == -1) {
67 		start_signum = 0;
68 	}
69 	if (end_signum == -1) {
70 		end_signum = SIGRTMAX;
71 	}
72 
73 	for (signum = start_signum; signum <= end_signum; signum++) {
74 
75 		if (signum >= __SIGRTMIN && signum < SIGRTMIN)
76 			continue;
77 
78 		switch (child = fork()) {
79 		case -1:
80 			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
81 		case 0:
82 
83 			if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) {
84 				tst_resm(TINFO, "[child] Sending kill(.., %d)",
85 					 signum);
86 				if (kill(getpid(), signum) < 0) {
87 					tst_resm(TINFO | TERRNO,
88 						 "[child] kill(.., %d) failed.",
89 						 signum);
90 				}
91 			} else {
92 
93 				/*
94 				 * This won't increment the TST_COUNT var.
95 				 * properly, but it'll show up as a failure
96 				 * nonetheless.
97 				 */
98 				tst_resm(TFAIL | TERRNO,
99 					 "Failed to ptrace(PTRACE_TRACEME, ...) "
100 					 "properly");
101 
102 			}
103 			/* Shouldn't get here if signum == 0. */
104 			exit((signum == 0 ? 0 : 2));
105 			break;
106 
107 		default:
108 
109 			waitpid(child, &status, 0);
110 
111 			switch (signum) {
112 			case 0:
113 				if (WIFEXITED(status)
114 				    && WEXITSTATUS(status) == 0) {
115 					tst_resm(TPASS,
116 						 "kill(.., 0) exited "
117 						 "with 0, as expected.");
118 				} else {
119 					tst_resm(TFAIL,
120 						 "kill(.., 0) didn't exit "
121 						 "with 0.");
122 				}
123 				break;
124 			case SIGKILL:
125 				if (WIFSIGNALED(status)) {
126 					/* SIGKILL must be uncatchable. */
127 					if (WTERMSIG(status) == SIGKILL) {
128 						tst_resm(TPASS,
129 							 "Killed with SIGKILL, "
130 							 "as expected.");
131 					} else {
132 						tst_resm(TPASS,
133 							 "Didn't die with "
134 							 "SIGKILL (?!) ");
135 					}
136 				} else if (WIFEXITED(status)) {
137 					tst_resm(TFAIL,
138 						 "Exited unexpectedly instead "
139 						 "of dying with SIGKILL.");
140 				} else if (WIFSTOPPED(status)) {
141 					tst_resm(TFAIL,
142 						 "Stopped instead of dying "
143 						 "with SIGKILL.");
144 				}
145 				break;
146 				/* All other processes should be stopped. */
147 			default:
148 				if (WIFSTOPPED(status)) {
149 					tst_resm(TPASS, "Stopped as expected");
150 				} else {
151 					tst_resm(TFAIL, "Didn't stop as "
152 						 "expected.");
153 					if (kill(child, 0)) {
154 						tst_resm(TINFO,
155 							 "Is still alive!?");
156 					} else if (WIFEXITED(status)) {
157 						tst_resm(TINFO,
158 							 "Exited normally");
159 					} else if (WIFSIGNALED(status)) {
160 						tst_resm(TINFO,
161 							 "Was signaled with "
162 							 "signum=%d",
163 							 WTERMSIG(status));
164 					}
165 
166 				}
167 
168 				break;
169 
170 			}
171 
172 		}
173 		/* Make sure the child dies a quick and painless death ... */
174 		kill(child, 9);
175 
176 	}
177 
178 	tst_exit();
179 
180 }
181