9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@labs.coraid.com>
To: 9fans@9fans.net
Subject: Re: [9fans] APE & environ
Date: Tue, 20 Aug 2013 21:43:19 -0400	[thread overview]
Message-ID: <bbb33c81d1c4f5ba47b4139e8266ed8b@coraid.com> (raw)
In-Reply-To: <098488AB-8AB8-4507-81A4-C13B0CCF03D8@corpus-callosum.com>

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



      reply	other threads:[~2013-08-21  1:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-20 17:13 Jeff Sickel
2013-08-20 17:54 ` Jeff Sickel
2013-08-20 19:30   ` Jeff Sickel
2013-08-21  1:43     ` erik quanstrom [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bbb33c81d1c4f5ba47b4139e8266ed8b@coraid.com \
    --to=quanstro@labs.coraid.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).