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