|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
1 | 2 | /*
|
2 | 3 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
|
3 | 4 | *
|
4 |
| - * This program is free software; you can redistribute it and/or modify it |
5 |
| - * under the terms of version 2 of the GNU General Public License as |
6 |
| - * published by the Free Software Foundation. |
7 |
| - * |
8 |
| - * This program is distributed in the hope that it would be useful, but |
9 |
| - * WITHOUT ANY WARRANTY; without even the implied warranty of |
10 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11 |
| - * |
12 |
| - * You should have received a copy of the GNU General Public License along |
13 |
| - * with this program; if not, write the Free Software Foundation, Inc., |
14 |
| - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 5 | + * AUTHOR: Saji Kumar.V.R <[email protected]> |
15 | 6 | *
|
| 7 | + * 1) ptrace() returns -1 and sets errno to ESRCH if process with |
| 8 | + * specified pid does not exist. |
| 9 | + * 2) ptrace() returns -1 and sets errno to EPERM if we are trying |
| 10 | + * to trace a process which is already been traced |
16 | 11 | */
|
17 |
| -/********************************************************** |
18 |
| - * |
19 |
| - * TEST IDENTIFIER : ptrace03 |
20 |
| - * |
21 |
| - * EXECUTED BY : anyone |
22 |
| - * |
23 |
| - * TEST TITLE : Tests for error conditions |
24 |
| - * |
25 |
| - * TEST CASE TOTAL : 3 |
26 |
| - * |
27 |
| - * AUTHOR : Saji Kumar.V.R <[email protected]> |
28 |
| - * |
29 |
| - * SIGNALS |
30 |
| - * Uses SIGUSR1 to pause before test if option set. |
31 |
| - * (See the parse_opts(3) man page). |
32 |
| - * |
33 |
| - * DESCRIPTION |
34 |
| - * Verifies that |
35 |
| - * 1) ptrace() returns -1 & sets errno to EPERM while tring to trace |
36 |
| - * process 1 |
37 |
| - * (This test case will be executed only if the kernel version |
38 |
| - * is 2.6.25 or below) |
39 |
| - * 2) ptrace() returns -1 & sets errno to ESRCH if process with |
40 |
| - * specified pid does not exist |
41 |
| - * 3) ptrace() returns -1 & sets errno to EPERM if we are trying |
42 |
| - * to trace a process which is already been traced |
43 |
| - * |
44 |
| - * Setup: |
45 |
| - * Setup signal handling. |
46 |
| - * Pause for SIGUSR1 if option specified. |
47 |
| - * |
48 |
| - * Test: |
49 |
| - * Loop if the proper options are given. |
50 |
| - * setup signal handler for SIGUSR2 signal |
51 |
| - * fork a child |
52 |
| - * |
53 |
| - * CHILD: |
54 |
| - * call ptrace() with proper arguments |
55 |
| - * if ptrace() failed with expected return value & errno |
56 |
| - * exit with errno |
57 |
| - * else |
58 |
| - * Give proper error message |
59 |
| - * exit with errno |
60 |
| - * |
61 |
| - * PARENT: |
62 |
| - * Wait for child to finish |
63 |
| - * if child exits with expected errno |
64 |
| - * Test Passed |
65 |
| - * else |
66 |
| - * Test failed |
67 |
| - * |
68 |
| - * Cleanup: |
69 |
| - * Print errno log and/or timing stats if options given |
70 |
| - * |
71 |
| - * USAGE: <for command-line> |
72 |
| - * ptrace03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] |
73 |
| - * where, -c n : Run n copies concurrently. |
74 |
| - * -e : Turn on errno logging. |
75 |
| - * -h : Show help screen |
76 |
| - * -f : Turn off functional testing |
77 |
| - * -i n : Execute test n times. |
78 |
| - * -I x : Execute test for x seconds. |
79 |
| - * -p : Pause for SIGUSR1 before starting |
80 |
| - * -P x : Pause for x seconds between iterations. |
81 |
| - * -t : Turn on syscall timing. |
82 |
| - * |
83 |
| - ****************************************************************/ |
84 | 12 |
|
85 | 13 | #include <errno.h>
|
86 | 14 | #include <signal.h>
|
87 | 15 | #include <sys/wait.h>
|
88 | 16 | #include <pwd.h>
|
89 |
| - |
90 | 17 | #include <config.h>
|
| 18 | +#include <stdlib.h> |
91 | 19 | #include "ptrace.h"
|
| 20 | +#include "tst_test.h" |
92 | 21 |
|
93 |
| -#include "test.h" |
94 |
| - |
95 |
| -static void setup(void); |
96 |
| -static void cleanup(void); |
97 |
| - |
98 |
| -char *TCID = "ptrace03"; |
99 |
| - |
100 |
| -static pid_t init_pid = 1; |
101 | 22 | static pid_t unused_pid;
|
102 | 23 | static pid_t zero_pid;
|
103 | 24 |
|
104 |
| -struct test_case_t { |
| 25 | +static struct tcase { |
105 | 26 | int request;
|
106 | 27 | pid_t *pid;
|
107 | 28 | int exp_errno;
|
108 |
| -} test_cases[] = { |
109 |
| - { |
110 |
| - PTRACE_ATTACH, &init_pid, EPERM}, { |
111 |
| - PTRACE_ATTACH, &unused_pid, ESRCH}, { |
112 |
| - PTRACE_TRACEME, &zero_pid, EPERM},}; |
| 29 | + char *tname; |
| 30 | +} tcases[] = { |
| 31 | + {PTRACE_ATTACH, &unused_pid, ESRCH, |
| 32 | + "Trace a process which does not exist"}, |
113 | 33 |
|
114 |
| -int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]); |
| 34 | + {PTRACE_TRACEME, &zero_pid, EPERM, |
| 35 | + "Trace a process which is already been traced"} |
| 36 | +}; |
115 | 37 |
|
116 |
| -int main(int ac, char **av) |
| 38 | +static void verify_ptrace(unsigned int n) |
117 | 39 | {
|
| 40 | + struct tcase *tc = &tcases[n]; |
| 41 | + int child_pid; |
118 | 42 |
|
119 |
| - int lc, i; |
120 |
| - pid_t child_pid; |
121 |
| - int status; |
| 43 | + tst_res(TINFO, "%s", tc->tname); |
122 | 44 |
|
123 |
| - tst_parse_opts(ac, av, NULL, NULL); |
| 45 | + child_pid = SAFE_FORK(); |
| 46 | + if (!child_pid) { |
| 47 | + if (tc->exp_errno == EPERM) |
| 48 | + SAFE_PTRACE(PTRACE_TRACEME, 0, NULL, NULL); |
124 | 49 |
|
125 |
| - setup(); |
126 |
| - |
127 |
| - for (lc = 0; TEST_LOOPING(lc); lc++) { |
128 |
| - |
129 |
| - tst_count = 0; |
130 |
| - |
131 |
| - for (i = 0; i < TST_TOTAL; ++i) { |
132 |
| - |
133 |
| - /* since Linux 2.6.26, it's allowed to trace init, |
134 |
| - so just skip this test case */ |
135 |
| - if (i == 0 && tst_kvercmp(2, 6, 25) > 0) { |
136 |
| - tst_resm(TCONF, |
137 |
| - "this kernel allows to trace init"); |
138 |
| - continue; |
139 |
| - } |
140 |
| - |
141 |
| - /* fork() */ |
142 |
| - switch (child_pid = FORK_OR_VFORK()) { |
143 |
| - |
144 |
| - case -1: |
145 |
| - /* fork() failed */ |
146 |
| - tst_resm(TFAIL, "fork() failed"); |
147 |
| - continue; |
148 |
| - |
149 |
| - case 0: |
150 |
| - /* Child */ |
151 |
| - |
152 |
| - /* setup for third test case */ |
153 |
| - if (i == 2) { |
154 |
| - if ((ptrace(PTRACE_TRACEME, 0, |
155 |
| - NULL, NULL)) == -1) { |
156 |
| - tst_resm(TWARN, "ptrace()" |
157 |
| - " falied with errno, %d : %s", |
158 |
| - errno, |
159 |
| - strerror(errno)); |
160 |
| - exit(0); |
161 |
| - } |
162 |
| - } |
163 |
| - |
164 |
| - TEST(ptrace(test_cases[i].request, |
165 |
| - *(test_cases[i].pid), NULL, NULL)); |
166 |
| - if ((TEST_RETURN == -1) && (TEST_ERRNO == |
167 |
| - test_cases |
168 |
| - [i].exp_errno)) { |
169 |
| - exit(TEST_ERRNO); |
170 |
| - } else { |
171 |
| - tst_resm(TWARN | TTERRNO, |
172 |
| - "ptrace() returned %ld", |
173 |
| - TEST_RETURN); |
174 |
| - exit(TEST_ERRNO); |
175 |
| - } |
176 |
| - |
177 |
| - default: |
178 |
| - /* Parent */ |
179 |
| - if ((waitpid(child_pid, &status, 0)) < 0) { |
180 |
| - tst_resm(TFAIL, "waitpid() failed"); |
181 |
| - continue; |
182 |
| - } |
183 |
| - if ((WIFEXITED(status)) && |
184 |
| - (WEXITSTATUS(status) == |
185 |
| - test_cases[i].exp_errno)) { |
186 |
| - tst_resm(TPASS, "Test Passed"); |
187 |
| - } else { |
188 |
| - tst_resm(TFAIL, "Test Failed"); |
189 |
| - } |
190 |
| - } |
| 50 | + TEST(ptrace(tc->request, *(tc->pid), NULL, NULL)); |
| 51 | + if (TST_RET == 0) { |
| 52 | + tst_res(TFAIL, "ptrace() succeeded unexpectedly"); |
| 53 | + exit(1); |
191 | 54 | }
|
| 55 | + if (tc->exp_errno == TST_ERR) |
| 56 | + tst_res(TPASS | TTERRNO, "ptrace() failed as expected"); |
| 57 | + else |
| 58 | + tst_res(TFAIL | TTERRNO, "ptrace() failed unexpectedly, expected %s", |
| 59 | + tst_strerrno(tc->exp_errno)); |
192 | 60 | }
|
193 |
| - |
194 |
| - /* cleanup and exit */ |
195 |
| - cleanup(); |
196 |
| - |
197 |
| - tst_exit(); |
198 |
| - |
| 61 | + tst_reap_children(); |
199 | 62 | }
|
200 | 63 |
|
201 |
| -/* setup() - performs all ONE TIME setup for this test */ |
202 |
| -void setup(void) |
| 64 | +static void setup(void) |
203 | 65 | {
|
204 |
| - unused_pid = tst_get_unused_pid(cleanup); |
205 |
| - |
206 |
| - TEST_PAUSE; |
207 |
| - |
| 66 | + unused_pid = tst_get_unused_pid(); |
208 | 67 | }
|
209 | 68 |
|
210 |
| -/* |
211 |
| - *cleanup() - performs all ONE TIME cleanup for this test at |
212 |
| - * completion or premature exit. |
213 |
| - */ |
214 |
| -void cleanup(void) |
215 |
| -{ |
216 |
| - |
217 |
| -} |
| 69 | +static struct tst_test test = { |
| 70 | + .test = verify_ptrace, |
| 71 | + .tcnt = ARRAY_SIZE(tcases), |
| 72 | + .setup = setup, |
| 73 | + .forks_child = 1, |
| 74 | +}; |
0 commit comments