Post by Jeff SickelTemporary fix, it only rebuilds environ on successful creation
acme# diff -c /sys/src/ape/lib/bsd/putenv.c putenv.c
/sys/src/ape/lib/bsd/putenv.c:2,8 - putenv.c:2,12
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
+ #include <stdlib.h>
+ extern char **environ;
+ extern void _envsetup(void);
+
int
putenv(char *s)
{
/sys/src/ape/lib/bsd/putenv.c:26,31 - putenv.c:30,38
if(write(f, value, n) != n)
return -1;
close(f);
+ if(environ != NULL)
+ free(environ);
+ _envsetup();
return 0;
} else
return -1;
unfortunately this reinterprets some magic in the _envsetup.
after some discussion, and unhappiness with all possible solutions,
the least bad one seemed to be to rebuild environ. this is way too
much code for the task, but posix shuts off easy escape routes. note
this is completely unthreadsafe. as per posix.
(it turns out that at least bsd does similar.)
/n/atom/patch/applied/apeputenv
/n/atom/patch/applied/envsetupmal
; pwd
/n/atom/patch/applied
; diff -c apeputenv/^(*.orig *.c)
apeputenv/putenv.c.orig:1,8 - apeputenv/putenv.c:1,56
+ #include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
+ extern char **environ;
+
+ static int
+ envredo(char *s)
+ {
+ char *x, **p, *q, **v, *mem;
+ int n, nsz, nenv, esz;
+
+ x = strchr(s, '=');
+ if(x == NULL)
+ return -1;
+ nsz = x - s + 1; /* compare the var=, not just var */
+
+ nenv = 1;
+ esz = strlen(s)+1;
+ for(p = environ; (q = *p) != NULL; p++){
+ esz += strlen(q)+1;
+ nenv++;
+ }
+
+ /* overestimate in the case we have changed a variable. */
+ v = malloc((nenv+1)*sizeof(char**) + esz);
+ if(v == NULL)
+ return -1;
+ mem = (char*)(v + nenv + 1);
+
+ nenv = 0;
+ for(p = environ; (q = *p) != NULL; p++){
+ if(strncmp(q, s, nsz) == 0)
+ continue;
+ v[nenv++] = mem;
+ n = strlen(q)+1;
+ memcpy(mem, q, n);
+ mem += n;
+ }
+ v[nenv++] = mem;
+ n = strlen(s)+1;
+ memcpy(mem, s, n);
+
+ v[nenv] = NULL;
+
+ free(environ);
+ environ = v;
+
+ return 0;
+ }
+
int
putenv(char *s)
{
apeputenv/putenv.c.orig:26,32 - apeputenv/putenv.c:74,80
if(write(f, value, n) != n)
return -1;
close(f);
- return 0;
+ return envredo(s);
} else
return -1;
}
; diff -c envsetupmal/^(*.orig *.c)
envsetupmal/_envsetup.c.orig:45,52 - envsetupmal/_envsetup.c:45,52
cnt = 0;
dfd = _OPEN("/env", 0);
if(dfd < 0) {
- static char **emptyenvp = 0;
- environ = emptyenvp;
+ environ = malloc(sizeof(char**));
+ *environ = NULL;
return;
}
ps = p = malloc(Envhunk);