1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Before kernel 2.6.26 we could not trace init(1) process and ptrace() would
11 * fail with EPERM. This case just checks whether we can trace init(1) process
12 * successfully.
13 */
14
15 #include <errno.h>
16 #include <signal.h>
17 #include <sys/wait.h>
18 #include <pwd.h>
19 #include <config.h>
20 #include <stdlib.h>
21 #include "ptrace.h"
22 #include "tst_test.h"
23
verify_ptrace(void)24 static void verify_ptrace(void)
25 {
26 TEST(ptrace(PTRACE_ATTACH, 1, NULL, NULL));
27 if (TST_RET == 0)
28 tst_res(TPASS, "ptrace() traces init process successfully");
29 else
30 tst_res(TFAIL | TTERRNO,
31 "ptrace() returns %ld, failed unexpectedly", TST_RET);
32
33 /*
34 * Wait until tracee is stopped by SIGSTOP otherwise detach will fail
35 * with ESRCH.
36 */
37 SAFE_WAITPID(1, NULL, 0);
38
39 SAFE_PTRACE(PTRACE_DETACH, 1, NULL, NULL);
40 }
41
42 static struct tst_test test = {
43 .test_all = verify_ptrace,
44 .needs_root = 1,
45 };
46