Home | History | Annotate | Download | only in clone
      1 /*
      2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
      3  * Copyright (c) 2012 Wanlong Gao <gaowanlong (at) cn.fujitsu.com>
      4  *
      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
     14  * with this program; if not, write the Free Software Foundation, Inc.,
     15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16  *
     17  */
     18 
     19 /*
     20  *	Test to verify inheritance of environment variables by child.
     21  */
     22 
     23 #if defined UCLINUX && !__THROW
     24 /* workaround for libc bug */
     25 #define __THROW
     26 #endif
     27 
     28 #include <errno.h>
     29 #include <sched.h>
     30 #include <sys/wait.h>
     31 #include <fcntl.h>
     32 #include "test.h"
     33 #include "clone_platform.h"
     34 
     35 #define MAX_LINE_LENGTH 256
     36 
     37 static void setup(void);
     38 static void cleanup(void);
     39 static int child_environ();
     40 
     41 static int pfd[2];
     42 
     43 char *TCID = "clone06";
     44 int TST_TOTAL = 1;
     45 
     46 int main(int ac, char **av)
     47 {
     48 
     49 	int lc, status;
     50 	void *child_stack;
     51 	char *parent_env;
     52 	char buff[MAX_LINE_LENGTH];
     53 
     54 	tst_parse_opts(ac, av, NULL, NULL);
     55 
     56 	setup();
     57 
     58 	child_stack = malloc(CHILD_STACK_SIZE);
     59 	if (child_stack == NULL)
     60 		tst_brkm(TBROK, cleanup, "Cannot allocate stack for child");
     61 
     62 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     63 		tst_count = 0;
     64 
     65 		if ((pipe(pfd)) == -1)
     66 			tst_brkm(TBROK | TERRNO, cleanup, "pipe() failed");
     67 
     68 		TEST(ltp_clone(SIGCHLD, child_environ, NULL, CHILD_STACK_SIZE,
     69 			       child_stack));
     70 
     71 		if (TEST_RETURN == -1)
     72 			tst_brkm(TFAIL | TTERRNO, cleanup, "clone failed");
     73 
     74 		/* close write end from parent */
     75 		if ((close(pfd[1])) == -1)
     76 			tst_resm(TWARN | TERRNO, "close(pfd[1]) failed");
     77 
     78 		/* Read env var from read end */
     79 		if ((read(pfd[0], buff, sizeof(buff))) == -1)
     80 			tst_brkm(TBROK | TERRNO, cleanup,
     81 				 "read from pipe failed");
     82 
     83 		/* Close read end from parent */
     84 		if ((close(pfd[0])) == -1)
     85 			tst_resm(TWARN | TERRNO, "close(pfd[0]) failed");
     86 
     87 		parent_env = getenv("TERM") ? : "";
     88 
     89 		if ((strcmp(buff, parent_env)) == 0)
     90 			tst_resm(TPASS, "Test Passed");
     91 		else
     92 			tst_resm(TFAIL, "Test Failed");
     93 
     94 		if ((wait(&status)) == -1)
     95 			tst_brkm(TBROK | TERRNO, cleanup,
     96 				 "wait failed, status: %d", status);
     97 	}
     98 
     99 	free(child_stack);
    100 
    101 	cleanup();
    102 	tst_exit();
    103 }
    104 
    105 static void setup(void)
    106 {
    107 	tst_sig(FORK, DEF_HANDLER, cleanup);
    108 	TEST_PAUSE;
    109 }
    110 
    111 static void cleanup(void)
    112 {
    113 }
    114 
    115 /*
    116  *	Function executed by child.
    117  *	Gets the value for environment variable,TERM &
    118  *	writes it to  a pipe.
    119  */
    120 static int child_environ(void)
    121 {
    122 
    123 	char var[MAX_LINE_LENGTH];
    124 
    125 	/* Close read end from child */
    126 	if ((close(pfd[0])) == -1)
    127 		tst_brkm(TBROK | TERRNO, cleanup, "close(pfd[0]) failed");
    128 
    129 	if ((sprintf(var, "%s", getenv("TERM") ? : "")) < 0)
    130 		tst_resm(TWARN | TERRNO, "sprintf() failed");
    131 
    132 	if ((write(pfd[1], var, MAX_LINE_LENGTH)) == -1)
    133 		tst_resm(TWARN | TERRNO, "write to pipe failed");
    134 
    135 	/* Close write end of pipe from child */
    136 	if ((close(pfd[1])) == -1)
    137 		tst_resm(TWARN | TERRNO, "close(pfd[1]) failed");
    138 
    139 	exit(0);
    140 }
    141