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