xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ptrace/ptrace05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  ******************************************************************************
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  *   ptrace05 - an app which ptraces itself as per arbitrarily specified signals,
5*49cdfc7eSAndroid Build Coastguard Worker  *   over a user specified range.
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (C) 2009, Ngie Cooper
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software; you can redistribute it and/or modify
10*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
11*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
12*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
15*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*49cdfc7eSAndroid Build Coastguard Worker  *   GNU General Public License for more details.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  *   with this program; if not, write to the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  ******************************************************************************
24*49cdfc7eSAndroid Build Coastguard Worker  */
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <libgen.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <math.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ptrace.h>
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/signal.h"
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "ptrace05";
42*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 0;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker int usage(const char *);
45*49cdfc7eSAndroid Build Coastguard Worker 
usage(const char * argv0)46*49cdfc7eSAndroid Build Coastguard Worker int usage(const char *argv0)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0);
49*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char ** argv)52*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	int end_signum = -1;
56*49cdfc7eSAndroid Build Coastguard Worker 	int signum;
57*49cdfc7eSAndroid Build Coastguard Worker 	int start_signum = -1;
58*49cdfc7eSAndroid Build Coastguard Worker 	int status;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child;
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	if (start_signum == -1) {
65*49cdfc7eSAndroid Build Coastguard Worker 		start_signum = 0;
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker 	if (end_signum == -1) {
68*49cdfc7eSAndroid Build Coastguard Worker 		end_signum = SIGRTMAX;
69*49cdfc7eSAndroid Build Coastguard Worker 	}
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	for (signum = start_signum; signum <= end_signum; signum++) {
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 		if (signum >= __SIGRTMIN && signum < SIGRTMIN)
74*49cdfc7eSAndroid Build Coastguard Worker 			continue;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 		switch (child = fork()) {
77*49cdfc7eSAndroid Build Coastguard Worker 		case -1:
78*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
79*49cdfc7eSAndroid Build Coastguard Worker 		case 0:
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 			if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) {
82*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TINFO, "[child] Sending kill(.., %d)",
83*49cdfc7eSAndroid Build Coastguard Worker 					 signum);
84*49cdfc7eSAndroid Build Coastguard Worker 				if (kill(getpid(), signum) < 0) {
85*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TINFO | TERRNO,
86*49cdfc7eSAndroid Build Coastguard Worker 						 "[child] kill(.., %d) failed.",
87*49cdfc7eSAndroid Build Coastguard Worker 						 signum);
88*49cdfc7eSAndroid Build Coastguard Worker 				}
89*49cdfc7eSAndroid Build Coastguard Worker 			} else {
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 				/*
92*49cdfc7eSAndroid Build Coastguard Worker 				 * This won't increment the TST_COUNT var.
93*49cdfc7eSAndroid Build Coastguard Worker 				 * properly, but it'll show up as a failure
94*49cdfc7eSAndroid Build Coastguard Worker 				 * nonetheless.
95*49cdfc7eSAndroid Build Coastguard Worker 				 */
96*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL | TERRNO,
97*49cdfc7eSAndroid Build Coastguard Worker 					 "Failed to ptrace(PTRACE_TRACEME, ...) "
98*49cdfc7eSAndroid Build Coastguard Worker 					 "properly");
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 			}
101*49cdfc7eSAndroid Build Coastguard Worker 			/* Shouldn't get here if signum == 0. */
102*49cdfc7eSAndroid Build Coastguard Worker 			exit((signum == 0 ? 0 : 2));
103*49cdfc7eSAndroid Build Coastguard Worker 			break;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 		default:
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 			waitpid(child, &status, 0);
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 			switch (signum) {
110*49cdfc7eSAndroid Build Coastguard Worker 			case 0:
111*49cdfc7eSAndroid Build Coastguard Worker 				if (WIFEXITED(status)
112*49cdfc7eSAndroid Build Coastguard Worker 				    && WEXITSTATUS(status) == 0) {
113*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS,
114*49cdfc7eSAndroid Build Coastguard Worker 						 "kill(.., 0) exited "
115*49cdfc7eSAndroid Build Coastguard Worker 						 "with 0, as expected.");
116*49cdfc7eSAndroid Build Coastguard Worker 				} else {
117*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL,
118*49cdfc7eSAndroid Build Coastguard Worker 						 "kill(.., 0) didn't exit "
119*49cdfc7eSAndroid Build Coastguard Worker 						 "with 0.");
120*49cdfc7eSAndroid Build Coastguard Worker 				}
121*49cdfc7eSAndroid Build Coastguard Worker 				break;
122*49cdfc7eSAndroid Build Coastguard Worker 			case SIGKILL:
123*49cdfc7eSAndroid Build Coastguard Worker 				if (WIFSIGNALED(status)) {
124*49cdfc7eSAndroid Build Coastguard Worker 					/* SIGKILL must be uncatchable. */
125*49cdfc7eSAndroid Build Coastguard Worker 					if (WTERMSIG(status) == SIGKILL) {
126*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TPASS,
127*49cdfc7eSAndroid Build Coastguard Worker 							 "Killed with SIGKILL, "
128*49cdfc7eSAndroid Build Coastguard Worker 							 "as expected.");
129*49cdfc7eSAndroid Build Coastguard Worker 					} else {
130*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TPASS,
131*49cdfc7eSAndroid Build Coastguard Worker 							 "Didn't die with "
132*49cdfc7eSAndroid Build Coastguard Worker 							 "SIGKILL (?!) ");
133*49cdfc7eSAndroid Build Coastguard Worker 					}
134*49cdfc7eSAndroid Build Coastguard Worker 				} else if (WIFEXITED(status)) {
135*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL,
136*49cdfc7eSAndroid Build Coastguard Worker 						 "Exited unexpectedly instead "
137*49cdfc7eSAndroid Build Coastguard Worker 						 "of dying with SIGKILL.");
138*49cdfc7eSAndroid Build Coastguard Worker 				} else if (WIFSTOPPED(status)) {
139*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL,
140*49cdfc7eSAndroid Build Coastguard Worker 						 "Stopped instead of dying "
141*49cdfc7eSAndroid Build Coastguard Worker 						 "with SIGKILL.");
142*49cdfc7eSAndroid Build Coastguard Worker 				}
143*49cdfc7eSAndroid Build Coastguard Worker 				break;
144*49cdfc7eSAndroid Build Coastguard Worker 				/* All other processes should be stopped. */
145*49cdfc7eSAndroid Build Coastguard Worker 			default:
146*49cdfc7eSAndroid Build Coastguard Worker 				if (WIFSTOPPED(status)) {
147*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS, "Stopped as expected");
148*49cdfc7eSAndroid Build Coastguard Worker 				} else {
149*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL, "Didn't stop as "
150*49cdfc7eSAndroid Build Coastguard Worker 						 "expected.");
151*49cdfc7eSAndroid Build Coastguard Worker 					if (kill(child, 0)) {
152*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TINFO,
153*49cdfc7eSAndroid Build Coastguard Worker 							 "Is still alive!?");
154*49cdfc7eSAndroid Build Coastguard Worker 					} else if (WIFEXITED(status)) {
155*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TINFO,
156*49cdfc7eSAndroid Build Coastguard Worker 							 "Exited normally");
157*49cdfc7eSAndroid Build Coastguard Worker 					} else if (WIFSIGNALED(status)) {
158*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TINFO,
159*49cdfc7eSAndroid Build Coastguard Worker 							 "Was signaled with "
160*49cdfc7eSAndroid Build Coastguard Worker 							 "signum=%d",
161*49cdfc7eSAndroid Build Coastguard Worker 							 WTERMSIG(status));
162*49cdfc7eSAndroid Build Coastguard Worker 					}
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 				}
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker 				break;
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker 			}
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker 		}
171*49cdfc7eSAndroid Build Coastguard Worker 		/* Make sure the child dies a quick and painless death ... */
172*49cdfc7eSAndroid Build Coastguard Worker 		kill(child, 9);
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker 	}
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker }
179