blob: b2846cbab181be51fc1e1a42a4e9654513ea1962 [file] [log] [blame]
nstraze3219812003-07-14 17:06:37 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +08003 * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com>
nstraze3219812003-07-14 17:06:37 +00004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080014 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
nstraze3219812003-07-14 17:06:37 +000016 *
17 */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080018
19/*
nstraze3219812003-07-14 17:06:37 +000020 * Test to verify inheritance of environment variables by child.
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080021 */
nstraze3219812003-07-14 17:06:37 +000022
robbiewd34d5812005-07-11 22:28:09 +000023#if defined UCLINUX && !__THROW
24/* workaround for libc bug */
25#define __THROW
26#endif
27
nstraze3219812003-07-14 17:06:37 +000028#include <errno.h>
29#include <sched.h>
30#include <sys/wait.h>
31#include <fcntl.h>
32#include "test.h"
nstraz882ee2d2003-07-16 17:40:32 +000033#include "clone_platform.h"
nstraze3219812003-07-14 17:06:37 +000034
nstraze3219812003-07-14 17:06:37 +000035#define MAX_LINE_LENGTH 256
36
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080037static void setup(void);
38static void cleanup(void);
nstraze3219812003-07-14 17:06:37 +000039static int child_environ();
40
41static int pfd[2];
42
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080043char *TCID = "clone06";
44int TST_TOTAL = 1;
nstraze3219812003-07-14 17:06:37 +000045
subrata_modak56207ce2009-03-23 13:35:39 +000046int main(int ac, char **av)
nstraze3219812003-07-14 17:06:37 +000047{
48
Jan Stancek443d16e2012-12-03 13:19:57 +010049 int lc, status;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020050 const char *msg;
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080051 void *child_stack;
nstraze3219812003-07-14 17:06:37 +000052 char *parent_env;
53 char buff[MAX_LINE_LENGTH];
subrata_modak4bb656a2009-02-26 12:02:09 +000054
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080055 msg = parse_opts(ac, av, NULL, NULL);
56 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080057 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
nstraze3219812003-07-14 17:06:37 +000058
nstraze3219812003-07-14 17:06:37 +000059 setup();
60
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080061 child_stack = malloc(CHILD_STACK_SIZE);
62 if (child_stack == NULL)
nstraze3219812003-07-14 17:06:37 +000063 tst_brkm(TBROK, cleanup, "Cannot allocate stack for child");
nstraze3219812003-07-14 17:06:37 +000064
nstraze3219812003-07-14 17:06:37 +000065 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080066 tst_count = 0;
nstraze3219812003-07-14 17:06:37 +000067
Garrett Cooperfbb20142010-12-17 01:57:48 -080068 if ((pipe(pfd)) == -1)
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080069 tst_brkm(TBROK | TERRNO, cleanup, "pipe() failed");
nstraze3219812003-07-14 17:06:37 +000070
Jan Stancek443d16e2012-12-03 13:19:57 +010071 TEST(ltp_clone(SIGCHLD, child_environ, NULL, CHILD_STACK_SIZE,
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080072 child_stack));
subrata_modakbdbaec52009-02-26 12:14:51 +000073
Garrett Cooperfbb20142010-12-17 01:57:48 -080074 if (TEST_RETURN == -1)
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080075 tst_brkm(TFAIL | TTERRNO, cleanup, "clone failed");
nstraze3219812003-07-14 17:06:37 +000076
77 /* close write end from parent */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080078 if ((close(pfd[1])) == -1)
79 tst_resm(TWARN | TERRNO, "close(pfd[1]) failed");
nstraze3219812003-07-14 17:06:37 +000080
81 /* Read env var from read end */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080082 if ((read(pfd[0], buff, sizeof(buff))) == -1)
83 tst_brkm(TBROK | TERRNO, cleanup,
84 "read from pipe failed");
nstraze3219812003-07-14 17:06:37 +000085
86 /* Close read end from parent */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080087 if ((close(pfd[0])) == -1)
88 tst_resm(TWARN | TERRNO, "close(pfd[0]) failed");
nstraze3219812003-07-14 17:06:37 +000089
Cyril Hrubis60eddf82011-03-30 18:27:30 +020090 parent_env = getenv("TERM") ? : "";
nstraze3219812003-07-14 17:06:37 +000091
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080092 if ((strcmp(buff, parent_env)) == 0)
nstraze3219812003-07-14 17:06:37 +000093 tst_resm(TPASS, "Test Passed");
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080094 else
nstraze3219812003-07-14 17:06:37 +000095 tst_resm(TFAIL, "Test Failed");
Jan Stancek443d16e2012-12-03 13:19:57 +010096
97 if ((wait(&status)) == -1)
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080098 tst_brkm(TBROK | TERRNO, cleanup,
99 "wait failed, status: %d", status);
Garrett Cooper2c282152010-12-16 00:55:50 -0800100 }
nstraze3219812003-07-14 17:06:37 +0000101
102 free(child_stack);
103
nstraze3219812003-07-14 17:06:37 +0000104 cleanup();
Garrett Cooperfbb20142010-12-17 01:57:48 -0800105 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800106}
nstraze3219812003-07-14 17:06:37 +0000107
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800108static void setup(void)
nstraze3219812003-07-14 17:06:37 +0000109{
Jan Stancek443d16e2012-12-03 13:19:57 +0100110 tst_sig(FORK, DEF_HANDLER, cleanup);
nstraze3219812003-07-14 17:06:37 +0000111 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800112}
nstraze3219812003-07-14 17:06:37 +0000113
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800114static void cleanup(void)
nstraze3219812003-07-14 17:06:37 +0000115{
Garrett Cooper2c282152010-12-16 00:55:50 -0800116}
nstraze3219812003-07-14 17:06:37 +0000117
118/*
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800119 * Function executed by child.
120 * Gets the value for environment variable,TERM &
121 * writes it to a pipe.
nstraze3219812003-07-14 17:06:37 +0000122 */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800123static int child_environ(void)
nstraze3219812003-07-14 17:06:37 +0000124{
125
126 char var[MAX_LINE_LENGTH];
127
128 /* Close read end from child */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800129 if ((close(pfd[0])) == -1)
130 tst_brkm(TBROK | TERRNO, cleanup, "close(pfd[0]) failed");
subrata_modakbdbaec52009-02-26 12:14:51 +0000131
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800132 if ((sprintf(var, "%s", getenv("TERM") ? : "")) < 0)
133 tst_resm(TWARN | TERRNO, "sprintf() failed");
nstraze3219812003-07-14 17:06:37 +0000134
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800135 if ((write(pfd[1], var, MAX_LINE_LENGTH)) == -1)
136 tst_resm(TWARN | TERRNO, "write to pipe failed");
nstraze3219812003-07-14 17:06:37 +0000137
138 /* Close write end of pipe from child */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800139 if ((close(pfd[1])) == -1)
140 tst_resm(TWARN | TERRNO, "close(pfd[1]) failed");
subrata_modakbdbaec52009-02-26 12:14:51 +0000141
robbiewb8bd30c2003-07-31 20:41:11 +0000142 exit(0);
Cyril Hrubis60eddf82011-03-30 18:27:30 +0200143}