1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Check for equality of getpid() from a child and return value of clone(2)
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <stdio.h>
14f08c3bdfSopenharmony_ci#include <stdlib.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci#include "clone_platform.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic void *child_stack;
19f08c3bdfSopenharmony_cistatic int *child_pid;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic int child_fn(void *arg LTP_ATTRIBUTE_UNUSED)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	*child_pid = getpid();
24f08c3bdfSopenharmony_ci	exit(0);
25f08c3bdfSopenharmony_ci}
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic void verify_clone(void)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	TST_EXP_PID_SILENT(ltp_clone(SIGCHLD, child_fn, NULL, CHILD_STACK_SIZE,
31f08c3bdfSopenharmony_ci				child_stack));
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	if (!TST_PASS)
34f08c3bdfSopenharmony_ci		return;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	tst_reap_children();
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	TST_EXP_VAL(TST_RET, *child_pid, "pid(%d)", *child_pid);
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void setup(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	child_pid = SAFE_MMAP(NULL, sizeof(*child_pid), PROT_READ | PROT_WRITE,
44f08c3bdfSopenharmony_ci				MAP_SHARED | MAP_ANONYMOUS, -1, 0);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void cleanup(void)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	if (child_pid)
50f08c3bdfSopenharmony_ci		SAFE_MUNMAP(child_pid, sizeof(*child_pid));
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic struct tst_test test = {
54f08c3bdfSopenharmony_ci	.setup = setup,
55f08c3bdfSopenharmony_ci	.test_all = verify_clone,
56f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
57f08c3bdfSopenharmony_ci		{&child_stack, .size = CHILD_STACK_SIZE},
58f08c3bdfSopenharmony_ci		{},
59f08c3bdfSopenharmony_ci	},
60f08c3bdfSopenharmony_ci	.cleanup = cleanup,
61f08c3bdfSopenharmony_ci};
62