Skip to content

Commit 5bd7355

Browse files
coolgwpevik
authored andcommitted
ptrace05: Refactor the test using new LTP API
Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Petr Vorel <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]> Signed-off-by: Wei Gao <[email protected]>
1 parent eabad6e commit 5bd7355

File tree

1 file changed

+62
-156
lines changed

1 file changed

+62
-156
lines changed
Lines changed: 62 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,84 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
12
/*
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.
3+
* Copyright (c) Linux Test Project, 2009-2019
4+
* Copyright (C) 2009, Ngie Cooper
5+
* Copyright (c) 2023 Wei Gao <[email protected]>
6+
*/
7+
8+
/*\
9+
* [Description]
2210
*
23-
******************************************************************************
11+
* This test ptraces itself as per arbitrarily specified signals,
12+
* over 0 to SIGRTMAX range.
2413
*/
2514

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>
3215
#include <stdlib.h>
33-
#include <stdio.h>
34-
#include <string.h>
35-
#include <unistd.h>
3616
#include <sys/ptrace.h>
37-
38-
#include "test.h"
3917
#include "lapi/signal.h"
18+
#include "tst_test.h"
4019

41-
char *TCID = "ptrace05";
42-
int TST_TOTAL = 0;
43-
44-
int usage(const char *);
20+
static int expect_stop;
4521

46-
int usage(const char *argv0)
22+
static void test_signal(int signum)
4723
{
48-
fprintf(stderr, "usage: %s [start-signum] [end-signum]\n", argv0);
49-
return 1;
50-
}
51-
52-
int main(int argc, char **argv)
53-
{
54-
55-
int end_signum = -1;
56-
int signum;
57-
int start_signum = -1;
5824
int status;
59-
6025
pid_t child;
6126

62-
tst_parse_opts(argc, argv, NULL, NULL);
27+
child = SAFE_FORK();
6328

64-
if (start_signum == -1) {
65-
start_signum = 0;
29+
if (!child) {
30+
TST_EXP_PASS_SILENT(ptrace(PTRACE_TRACEME, 0, NULL, NULL));
31+
tst_res(TDEBUG, "[child] Sending kill(.., %s)", tst_strsig(signum));
32+
SAFE_KILL(getpid(), signum);
33+
exit(0);
6634
}
67-
if (end_signum == -1) {
68-
end_signum = SIGRTMAX;
35+
36+
SAFE_WAITPID(child, &status, 0);
37+
38+
switch (signum) {
39+
case 0:
40+
if (WIFEXITED(status)
41+
&& WEXITSTATUS(status) == 0) {
42+
tst_res(TPASS,
43+
"kill(.., 0) exited with 0, as expected.");
44+
} else {
45+
tst_res(TFAIL,
46+
"kill(.., 0) exited with unexpected %s.", tst_strstatus(status));
47+
}
48+
break;
49+
case SIGKILL:
50+
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)
51+
tst_res(TPASS, "Child killed by SIGKILL");
52+
else
53+
tst_res(TFAIL, "Child %s", tst_strstatus(status));
54+
break;
55+
/* All other processes should be stopped. */
56+
default:
57+
if (WIFSTOPPED(status)) {
58+
tst_res(TDEBUG, "Stopped as expected");
59+
} else {
60+
tst_res(TFAIL, "Didn't stop as expected. Child %s", tst_strstatus(status));
61+
expect_stop++;
62+
}
63+
break;
6964
}
7065

71-
for (signum = start_signum; signum <= end_signum; signum++) {
66+
if (signum != 0 && signum != SIGKILL)
67+
SAFE_PTRACE(PTRACE_CONT, child, NULL, NULL);
68+
}
69+
70+
static void run(void)
71+
{
72+
int signum = 0;
7273

74+
for (signum = 0; signum <= SIGRTMAX; signum++) {
7375
if (signum >= __SIGRTMIN && signum < SIGRTMIN)
7476
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-
77+
test_signal(signum);
17478
}
175-
176-
tst_exit();
177-
17879
}
80+
81+
static struct tst_test test = {
82+
.test_all = run,
83+
.forks_child = 1,
84+
};

0 commit comments

Comments
 (0)