Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[9fans] APE & environ

52 views
Skip to first unread message

Jeff Sickel

unread,
Aug 20, 2013, 1:13:29 PM8/20/13
to
I've only now noticed that using APE's putenv() does create an
entry for the variable in /env but does not reset environ (see
/sys/src/ape/lib/ap/plan9/_envsetup.c). This means that any
subsequent call to getenv() for the variable you're trying to
test will not actually produce your expected result unless you
fork or exec a subprocess that will call _envsetup() again.

It seems a shame that putenv() would need to rebuild environ
each time, but that may be the only option to a clean posixish
solution for APE programs.

-jas

Jeff Sickel

unread,
Aug 20, 2013, 1:54:35 PM8/20/13
to
For what it's worth, putenv() should also probably be moved
from ape/lib/bsd to ape/lib/ap and move the header entry
from bsd.h to stdlib.h.

Feedback/comments welcome.

-jas

Jeff Sickel

unread,
Aug 20, 2013, 3:30:08 PM8/20/13
to
Temporary fix, it only rebuilds environ on successful creation
of the new|modified variable:



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;

erik quanstrom

unread,
Aug 20, 2013, 9:43:19 PM8/20/13
to
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);

0 new messages