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