blob: c4375aca903949f5cee950f86ad7e1a1b3d421f2 [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
3 *
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
Wanlong Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapierd13d74b2006-02-11 07:24:00 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
vapierd13d74b2006-02-11 07:24:00 +000019 * TEST IDENTIFIER : ptrace03
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
vapierd13d74b2006-02-11 07:24:00 +000021 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
vapierd13d74b2006-02-11 07:24:00 +000023 * TEST TITLE : Tests for error conditions
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
vapierd13d74b2006-02-11 07:24:00 +000025 * TEST CASE TOTAL : 3
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
vapierd13d74b2006-02-11 07:24:00 +000027 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
vapierd13d74b2006-02-11 07:24:00 +000029 * 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
subrata_modak7a2fd9b2008-05-22 13:17:50 +000037 * (This test case will be executed only if the kernel version
38 * is 2.6.25 or below)
vapierd13d74b2006-02-11 07:24:00 +000039 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
vapierd13d74b2006-02-11 07:24:00 +000048 * 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
Garrett Cooper2c282152010-12-16 00:55:50 -080067 *
vapierd13d74b2006-02-11 07:24:00 +000068 * Cleanup:
69 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *
vapierd13d74b2006-02-11 07:24:00 +000071 * 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
85#include <errno.h>
86#include <signal.h>
vapierd13d74b2006-02-11 07:24:00 +000087#include <sys/wait.h>
subrata_modak4bb656a2009-02-26 12:02:09 +000088#include <pwd.h>
vapierd13d74b2006-02-11 07:24:00 +000089
vapier919dca82009-11-03 19:42:12 +000090#include <config.h>
91#include "ptrace.h"
92
vapierd13d74b2006-02-11 07:24:00 +000093#include "test.h"
vapierd13d74b2006-02-11 07:24:00 +000094
vapierd13d74b2006-02-11 07:24:00 +000095static void setup(void);
96static void cleanup(void);
97
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020098char *TCID = "ptrace03";
vapierd13d74b2006-02-11 07:24:00 +000099
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400100static pid_t init_pid = 1;
101static pid_t unused_pid;
102static pid_t zero_pid;
103
vapierd13d74b2006-02-11 07:24:00 +0000104struct test_case_t {
105 enum __ptrace_request request;
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400106 pid_t *pid;
vapierd13d74b2006-02-11 07:24:00 +0000107 int exp_errno;
108} test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000109 {
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400110 PTRACE_ATTACH, &init_pid, EPERM}, {
111 PTRACE_ATTACH, &unused_pid, ESRCH}, {
112 PTRACE_TRACEME, &zero_pid, EPERM},};
vapierd13d74b2006-02-11 07:24:00 +0000113
114int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116int main(int ac, char **av)
vapierd13d74b2006-02-11 07:24:00 +0000117{
118
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200119 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200120 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +0000121 pid_t child_pid;
122 int status;
123
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800124 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800125 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +0000126
vapierd13d74b2006-02-11 07:24:00 +0000127 setup();
128
vapierd13d74b2006-02-11 07:24:00 +0000129 for (lc = 0; TEST_LOOPING(lc); lc++) {
130
Caspar Zhangd59a6592013-03-07 14:59:12 +0800131 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +0000132
133 for (i = 0; i < TST_TOTAL; ++i) {
134
subrata_modak7a2fd9b2008-05-22 13:17:50 +0000135 /* since Linux 2.6.26, it's allowed to trace init,
136 so just skip this test case */
subrata_modak56207ce2009-03-23 13:35:39 +0000137 if (i == 0 && tst_kvercmp(2, 6, 25) > 0) {
subrata_modak7a2fd9b2008-05-22 13:17:50 +0000138 tst_resm(TCONF,
139 "this kernel allows to trace init");
140 continue;
141 }
142
vapierd13d74b2006-02-11 07:24:00 +0000143 /* fork() */
mridgeed06ca62006-02-14 16:55:09 +0000144 switch (child_pid = FORK_OR_VFORK()) {
vapierd13d74b2006-02-11 07:24:00 +0000145
146 case -1:
147 /* fork() failed */
148 tst_resm(TFAIL, "fork() failed");
149 continue;
150
151 case 0:
152 /* Child */
153
154 /* setup for third test case */
155 if (i == 2) {
156 if ((ptrace(PTRACE_TRACEME, 0,
subrata_modak56207ce2009-03-23 13:35:39 +0000157 NULL, NULL)) == -1) {
vapierd13d74b2006-02-11 07:24:00 +0000158 tst_resm(TWARN, "ptrace()"
subrata_modak56207ce2009-03-23 13:35:39 +0000159 " falied with errno, %d : %s",
160 errno,
161 strerror(errno));
vapierd13d74b2006-02-11 07:24:00 +0000162 exit(0);
163 }
164 }
165
166 TEST(ptrace(test_cases[i].request,
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400167 *(test_cases[i].pid), NULL, NULL));
vapierd13d74b2006-02-11 07:24:00 +0000168 if ((TEST_RETURN == -1) && (TEST_ERRNO ==
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 test_cases
170 [i].exp_errno)) {
vapierd13d74b2006-02-11 07:24:00 +0000171 exit(TEST_ERRNO);
172 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 tst_resm(TWARN | TTERRNO,
174 "ptrace() returned %ld",
subrata_modak923b23f2009-11-02 13:57:16 +0000175 TEST_RETURN);
vapierd13d74b2006-02-11 07:24:00 +0000176 exit(TEST_ERRNO);
177 }
178
179 default:
180 /* Parent */
181 if ((waitpid(child_pid, &status, 0)) < 0) {
182 tst_resm(TFAIL, "waitpid() failed");
183 continue;
184 }
185 if ((WIFEXITED(status)) &&
subrata_modak56207ce2009-03-23 13:35:39 +0000186 (WEXITSTATUS(status) ==
187 test_cases[i].exp_errno)) {
vapierd13d74b2006-02-11 07:24:00 +0000188 tst_resm(TPASS, "Test Passed");
189 } else {
190 tst_resm(TFAIL, "Test Failed");
191 }
vapierd13d74b2006-02-11 07:24:00 +0000192 }
193 }
194 }
195
196 /* cleanup and exit */
197 cleanup();
198
Garrett Cooper53740502010-12-16 00:04:01 -0800199 tst_exit();
vapierd13d74b2006-02-11 07:24:00 +0000200
Garrett Cooper2c282152010-12-16 00:55:50 -0800201}
vapierd13d74b2006-02-11 07:24:00 +0000202
203/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400204void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000205{
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +0400206 unused_pid = tst_get_unused_pid(cleanup);
subrata_modakbdbaec52009-02-26 12:14:51 +0000207
vapierd13d74b2006-02-11 07:24:00 +0000208 TEST_PAUSE;
subrata_modakbdbaec52009-02-26 12:14:51 +0000209
Garrett Cooper2c282152010-12-16 00:55:50 -0800210}
vapierd13d74b2006-02-11 07:24:00 +0000211
212/*
213 *cleanup() - performs all ONE TIME cleanup for this test at
214 * completion or premature exit.
215 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400216void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000217{
218
Chris Dearmanec6edca2012-10-17 19:54:01 -0700219}