Skip to content

Commit 31fecf2

Browse files
committedSep 22, 2013
Special-case bash to export variables properly.
1 parent 10b63eb commit 31fecf2

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed
 

‎enviable.c

+32-8
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,38 @@ static int (*real___libc_current_sigrtmin)(void) = NULL;
4747

4848
static char *watchfile = "/tmp/vars";
4949

50+
/* The following definitions are stolen from bash-4.2 */
51+
__attribute__((weak))
52+
extern int do_assignment_no_expand(char *);
53+
__attribute__((weak))
54+
extern void set_var_attribute(char *, int, int);
55+
#define att_exported 1
56+
5057
int
5158
__libc_current_sigrtmin(void)
5259
{
5360
return real___libc_current_sigrtmin() + 1;
5461
}
5562

63+
static void
64+
enviable_setenv(char *line)
65+
{
66+
char *eq = strchr(line, '=');
67+
if (do_assignment_no_expand && set_var_attribute) {
68+
/* We're running inside bash. Since bash maintains its
69+
* own idea of the environment (shell variables marked
70+
* exported), we need to go through that for child
71+
* processes to see changes. */
72+
do_assignment_no_expand(line);
73+
*eq = '\0';
74+
set_var_attribute(line, att_exported, 0);
75+
} else {
76+
/* Unknown host process -- fall back to libc. */
77+
*eq = '\0';
78+
setenv(line, eq + 1, 1);
79+
}
80+
}
81+
5682
static void
5783
enviable_callback(int signum, siginfo_t *si, void *context)
5884
{
@@ -64,21 +90,19 @@ enviable_callback(int signum, siginfo_t *si, void *context)
6490
if (!fd) {
6591
return;
6692
}
67-
char *buf = NULL;
93+
char *line = NULL;
6894
size_t n = 0;
6995
ssize_t len;
70-
while ((len = getline(&buf, &n, fd)) > 0) {
71-
if (buf[len - 1] == '\n') {
72-
buf[len - 1] = '\0';
96+
while ((len = getline(&line, &n, fd)) > 0) {
97+
if (line[len - 1] == '\n') {
98+
line[len - 1] = '\0';
7399
} else {
74100
/* Conservatively reject partial lines. */
75101
continue;
76102
}
77-
char *eq = strchr(buf, '=');
78-
*eq = '\0';
79-
setenv(buf, eq + 1, 1);
103+
enviable_setenv(line);
80104
}
81-
free(buf);
105+
free(line);
82106
fclose(fd);
83107
}
84108

0 commit comments

Comments
 (0)