1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
4 *
5 * AUTHOR: Saji Kumar.V.R <saji.kumar@wipro.com>
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
11 */
12
13 #include <errno.h>
14 #include <signal.h>
15 #include <sys/wait.h>
16 #include <pwd.h>
17 #include <config.h>
18 #include <stdlib.h>
19 #include "ptrace.h"
20 #include "tst_test.h"
21
22 static pid_t unused_pid;
23 static pid_t zero_pid;
24
25 static struct tcase {
26 int request;
27 pid_t *pid;
28 int exp_errno;
29 char *tname;
30 } tcases[] = {
31 {PTRACE_ATTACH, &unused_pid, ESRCH,
32 "Trace a process which does not exist"},
33
34 {PTRACE_TRACEME, &zero_pid, EPERM,
35 "Trace a process which is already been traced"}
36 };
37
verify_ptrace(unsigned int n)38 static void verify_ptrace(unsigned int n)
39 {
40 struct tcase *tc = &tcases[n];
41 int child_pid;
42
43 tst_res(TINFO, "%s", tc->tname);
44
45 child_pid = SAFE_FORK();
46 if (!child_pid) {
47 if (tc->exp_errno == EPERM)
48 SAFE_PTRACE(PTRACE_TRACEME, 0, NULL, NULL);
49
50 TEST(ptrace(tc->request, *(tc->pid), NULL, NULL));
51 if (TST_RET == 0) {
52 tst_res(TFAIL, "ptrace() succeeded unexpectedly");
53 exit(1);
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));
60 }
61 tst_reap_children();
62 }
63
setup(void)64 static void setup(void)
65 {
66 unused_pid = tst_get_unused_pid();
67 }
68
69 static struct tst_test test = {
70 .test = verify_ptrace,
71 .tcnt = ARRAY_SIZE(tcases),
72 .setup = setup,
73 .forks_child = 1,
74 };
75