xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ptrace/ptrace11.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <[email protected]>
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 <stdlib.h>
20 #include <sys/ptrace.h>
21 #include "tst_test.h"
22 
verify_ptrace(void)23 static void verify_ptrace(void)
24 {
25 	TEST(ptrace(PTRACE_ATTACH, 1, NULL, NULL));
26 	if (TST_RET == 0)
27 		tst_res(TPASS, "ptrace() traces init process successfully");
28 	else
29 		tst_res(TFAIL | TTERRNO,
30 			"ptrace() returns %ld, failed unexpectedly", TST_RET);
31 
32 	/*
33 	 * Wait until tracee is stopped by SIGSTOP otherwise detach will fail
34 	 * with ESRCH.
35 	 */
36 	SAFE_WAITPID(1, NULL, 0);
37 
38 	SAFE_PTRACE(PTRACE_DETACH, 1, NULL, NULL);
39 }
40 
41 static struct tst_test test = {
42 	.test_all = verify_ptrace,
43 	.needs_root = 1,
44 };
45