9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] APE & environ
@ 2013-08-20 17:13 Jeff Sickel
  2013-08-20 17:54 ` Jeff Sickel
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Sickel @ 2013-08-20 17:13 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] APE & environ
  2013-08-20 17:13 [9fans] APE & environ Jeff Sickel
@ 2013-08-20 17:54 ` Jeff Sickel
  2013-08-20 19:30   ` Jeff Sickel
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Sickel @ 2013-08-20 17:54 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] APE & environ
  2013-08-20 17:54 ` Jeff Sickel
@ 2013-08-20 19:30   ` Jeff Sickel
  2013-08-21  1:43     ` erik quanstrom
  0 siblings, 1 reply; 4+ messages in thread
From: Jeff Sickel @ 2013-08-20 19:30 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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;





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9fans] APE & environ
  2013-08-20 19:30   ` Jeff Sickel
@ 2013-08-21  1:43     ` erik quanstrom
  0 siblings, 0 replies; 4+ messages in thread
From: erik quanstrom @ 2013-08-21  1:43 UTC (permalink / raw)
  To: 9fans

On Tue Aug 20 15:30:28 EDT 2013, jas@corpus-callosum.com wrote:
> 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;

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);



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-08-21  1:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-20 17:13 [9fans] APE & environ Jeff Sickel
2013-08-20 17:54 ` Jeff Sickel
2013-08-20 19:30   ` Jeff Sickel
2013-08-21  1:43     ` erik quanstrom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).