xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/clone/clone06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Test to verify inheritance of environment variables by child.
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sched.h>
16 #include "tst_test.h"
17 #include "clone_platform.h"
18 
19 #define MAX_LINE_LENGTH 256
20 #define ENV_VAL "LTP test variable value"
21 #define ENV_ID "LTP_CLONE_TEST"
22 
23 static void *child_stack;
24 
child_environ(void * arg LTP_ATTRIBUTE_UNUSED)25 static int child_environ(void *arg LTP_ATTRIBUTE_UNUSED)
26 {
27 	const char *env_val = getenv(ENV_ID);
28 	if (!env_val) {
29 		tst_res(TFAIL, "Variable " ENV_ID " not defined in child");
30 		exit(0);
31 	}
32 
33 	if (strcmp(env_val, ENV_VAL)) {
34 		tst_res(TFAIL, "Variable value is different");
35 		exit(0);
36 	}
37 
38 	tst_res(TPASS, "The environment variables of the child and the parent are the same ");
39 
40 	exit(0);
41 }
42 
verify_clone(void)43 static void verify_clone(void)
44 {
45 	TST_EXP_PID_SILENT(ltp_clone(SIGCHLD, child_environ, NULL, CHILD_STACK_SIZE,
46 				child_stack));
47 
48 	if (!TST_PASS)
49 		return;
50 
51 	tst_reap_children();
52 }
53 
setup(void)54 static void setup(void)
55 {
56 	SAFE_SETENV(ENV_ID, ENV_VAL, 0);
57 }
58 
59 static struct tst_test test = {
60 	.setup = setup,
61 	.test_all = verify_clone,
62 	.bufs = (struct tst_buffers []) {
63 		{&child_stack, .size = CHILD_STACK_SIZE},
64 		{},
65 	},
66 };
67