1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * Copyright (c) Linux Test Project, 2001-2023
5 * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6 */
7
8 /*\
9 *[Description]
10 *
11 * This test verifies that parent process shares environ variables with the
12 * child and that child doesn't change parent's environ variables.
13 */
14
15 #include <stdlib.h>
16 #include "tst_test.h"
17
18 #define ENV_KEY "LTP_FORK04"
19 #define ENV_VAL0 "PASS"
20 #define ENV_VAL1 "FAIL"
21
run_child(void)22 static void run_child(void)
23 {
24 const char *val;
25
26 val = getenv(ENV_KEY);
27 if (!val)
28 tst_brk(TBROK, "Can't find %s environ variable", ENV_KEY);
29
30 TST_EXP_EXPR(strcmp(ENV_VAL0, val) == 0,
31 "%s environ variable has been inherited by the child",
32 ENV_KEY)
33
34 tst_res(TINFO, "Unset %s environ variable inside child", ENV_KEY);
35
36 if (unsetenv(ENV_KEY) == -1)
37 tst_brk(TBROK, "Can't unset %s environ variable", ENV_KEY);
38
39 TST_CHECKPOINT_WAKE_AND_WAIT(0);
40
41 tst_res(TINFO, "Set %s=%s environ variable inside child", ENV_KEY, ENV_VAL1);
42
43 SAFE_SETENV(ENV_KEY, ENV_VAL1, 0);
44
45 TST_CHECKPOINT_WAKE(0);
46 }
47
run(void)48 static void run(void)
49 {
50 const char *val;
51
52 tst_res(TINFO,
53 "Set %s=%s environ variable inside parent",
54 ENV_KEY, ENV_VAL0);
55
56 SAFE_SETENV(ENV_KEY, ENV_VAL0, 0);
57
58 tst_res(TINFO, "Spawning child");
59
60 if (!SAFE_FORK()) {
61 run_child();
62 exit(0);
63 }
64
65 TST_CHECKPOINT_WAIT(0);
66
67 val = getenv(ENV_KEY);
68 if (!val) {
69 tst_res(TFAIL,
70 "%s environ variable has been unset inside parent",
71 ENV_KEY);
72 } else {
73 TST_EXP_EXPR(strcmp(ENV_VAL0, val) == 0,
74 "%s environ variable is still present inside parent",
75 ENV_KEY)
76 }
77
78 TST_CHECKPOINT_WAKE_AND_WAIT(0);
79
80 val = getenv(ENV_KEY);
81 if (!val)
82 tst_res(TFAIL,
83 "%s environ variable has been unset inside parent",
84 ENV_KEY);
85 else {
86 TST_EXP_EXPR(strcmp(ENV_VAL0, val) == 0,
87 "%s environ variable didn't change inside parent",
88 ENV_KEY)
89 }
90 }
91
92 static struct tst_test test = {
93 .test_all = run,
94 .forks_child = 1,
95 .needs_checkpoints = 1,
96 };
97