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