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