From b34fc2a9cfdfec3abe8d1e1831a0ebf9dc50ff07 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Mon, 3 Jan 2022 18:47:56 +0000 Subject: [PATCH] ape: allow exporting rc functions in environ[] --- sys/src/ape/lib/ap/plan9/_envsetup.c | 33 +++++++++---- sys/src/ape/lib/ap/plan9/execve.c | 71 +++++++++++++++++----------- 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/sys/src/ape/lib/ap/plan9/_envsetup.c b/sys/src/ape/lib/ap/plan9/_envsetup.c index 58ed05342..9d393e27a 100644 --- a/sys/src/ape/lib/ap/plan9/_envsetup.c +++ b/sys/src/ape/lib/ap/plan9/_envsetup.c @@ -45,7 +45,7 @@ _envsetup(void) nohandle = 0; fdinited = 0; cnt = 0; - dfd = _OPEN("/env", OREAD); + dfd = _OPEN("/env", OREAD|OCEXEC); if(dfd < 0) goto done; psize = Envhunk; @@ -62,21 +62,36 @@ _envsetup(void) ps = realloc(ps, psize); p = ps + i; } - strcpy(p, "/env/"); + memcpy(p, "/env/", 5); memcpy(p+5, d9->name, n+1); - f = _OPEN(p, OREAD); - memset(p, 0, n+6); + f = _OPEN(p, OREAD|OCEXEC); + if(f < 0) + continue; + if(n > 3 && memcmp(d9->name, "fn#", 3)==0){ + if(m > 3 + && _READ(f, p+3, m) == m + && memcmp(p+3, "fn ", 3) == 0 + && memcmp(p+3+3, d9->name+3, n-3) == 0 + && memchr(p+3+n, '{', m-n) != 0){ + memcpy(p, "#()", 3); + p[3+m] = '\0'; + p += m+4; + cnt++; + } + _CLOSE(f); + continue; + } memcpy(p, d9->name, n); p[n] = '='; - if(f < 0 || _READ(f, p+n+1, m) != m) + if(_READ(f, p+n+1, m) != m) m = 0; _CLOSE(f); - if(p[n+m] == 0) + if(p[n+m]=='\0') m--; for(i=0; iname, "_fdinfo") == 0) { _fdinit(p+n+1, p+n+1+m); fdinited = 1; diff --git a/sys/src/ape/lib/ap/plan9/execve.c b/sys/src/ape/lib/ap/plan9/execve.c index 1761f3183..276933431 100644 --- a/sys/src/ape/lib/ap/plan9/execve.c +++ b/sys/src/ape/lib/ap/plan9/execve.c @@ -23,7 +23,7 @@ execve(const char *name, const char *argv[], const char *envp[]) * in $_fdinfo (for open fd's) */ - f = _CREATE("/env/_fdinfo", OWRITE, 0666); + f = _CREATE("/env/_fdinfo", OWRITE|OCEXEC, 0666); ss = buf; for(i = 0; i= 0){ ss = buf; for(i = 0; i <=MAXSIG; i++) { @@ -86,32 +86,49 @@ execve(const char *name, const char *argv[], const char *envp[]) } if(envp){ for(e = (char**)envp; (ss = *e); e++) { - se = strchr(ss, '='); - if(!se || ss==se) - continue; /* what is name? value? */ - n = se-ss; - if(n >= sizeof(buf)-5) - continue; /* name too long */ - strcpy(buf, "/env/"); - memcpy(buf+5, ss, n); - buf[5+n] = 0; - f = _CREATE(buf, OWRITE, 0666); - if(f < 0) - continue; - ss = ++se; /* past = */ - se += strlen(ss); - while((n = (se - ss)) > 0){ - if(n > sizeof(buf)) - n = sizeof(buf); - /* decode nulls (see _envsetup()) */ - for(i=0; i= sizeof(buf)-8) + continue; /* name too long */ + memcpy(buf, "/env/fn#", 8); + memcpy(buf+8, ss+6, n); + buf[8+n] = '\0'; + f = _CREATE(buf, OWRITE|OCEXEC, 0666); + if(f < 0) + continue; + ss += 3; /* past #() */ + _WRITE(f, ss, strlen(ss)); + _CLOSE(f); + } else { + if((se = strchr(ss, '='))==0) + continue; + n = se-ss; + if(n <= 0 || n >= sizeof(buf)-5) + continue; /* name too long */ + memcpy(buf, "/env/", 5); + memcpy(buf+5, ss, n); + buf[5+n] = '\0'; + f = _CREATE(buf, OWRITE|OCEXEC, 0666); + if(f < 0) + continue; + ss = ++se; /* past = */ + se += strlen(se); + while((n = (se - ss)) > 0){ + if(n > sizeof(buf)) + n = sizeof(buf); + /* decode nulls (see _envsetup()) */ + for(i=0; i