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