libc: rewrite putenv and getenv.

These new implementations

- do several validity check on input parameters
- allow a bit larger variable names (127 bytes, aka sizeof(Proc.genbuf)-1)
- preserve nulls in the content (the original version used to replace
  '\0' with ' '). I can't see why they did, actually.
  See also http://marc.info/?l=9fans&m=148475801229908&w=2

Should also fix CID 155718
This commit is contained in:
Giacomo Tesio 2017-01-19 00:58:43 +01:00
parent d43be3861b
commit 65cdad4317
6 changed files with 118 additions and 58 deletions

View File

@ -8,6 +8,9 @@
"-std=gnu11" "-std=gnu11"
], ],
"Install": "/arch/$ARCH/qa/lib/c", "Install": "/arch/$ARCH/qa/lib/c",
"Post": [
"cp *.rc $JEHANNE/arch/$ARCH/qa/lib/c/"
],
"SourceFilesCmd": [ "SourceFilesCmd": [
"asmscall.c", "asmscall.c",
"cleanname.c", "cleanname.c",

28
qa/lib/c/env.rc Executable file
View File

@ -0,0 +1,28 @@
#!/cmd/rc
# verify that environment variables can have names 127 byte long
# why 127? because it's the size of genbuf in the kernel's Proc structure
# minus the ending \0
abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345678=10
if ( ! ~ $abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef012345678 10 ) {
echo FAIL: cannot read an environment variable with a long name
exit FAIL
}
# verify that rc lists work
list=(How now brown cow)
string=$"list
if( ! ~ $#list 4 ) {
echo FAIL: list count does not work on a 4 elements list
exit FAIL
}
if( ! ~ $#string 1 ) {
echo FAIL: list count does not work on a single string
exit FAIL
}
echo PASS
exit PASS

View File

@ -46,7 +46,7 @@ extern int strncmp(const char*, const char*, int32_t);
extern char* strpbrk(const char*, const char*); extern char* strpbrk(const char*, const char*);
extern char* strrchr(const char*, int); extern char* strrchr(const char*, int);
extern char* strtok(char*, char*); extern char* strtok(char*, char*);
extern int32_t strlen(const char*); extern int strlen(const char*);
extern int32_t strspn(const char*, const char*); extern int32_t strspn(const char*, const char*);
extern int32_t strcspn(const char*, const char*); extern int32_t strcspn(const char*, const char*);
extern char* strstr(const char*, const char*); extern char* strstr(const char*, const char*);

View File

@ -1,12 +1,19 @@
/* /*
* Copyright (C) 2016 Giacomo Tesio <giacomo@tesio.it> * This file is part of Jehanne.
* *
* This file is part of the UCB release of Plan 9. It is subject to the license * Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
* terms in the LICENSE file found in the top-level directory of this *
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * Jehanne is free software: you can redistribute it and/or modify
* part of the UCB release of Plan 9, including this file, may be copied, * it under the terms of the GNU General Public License as published by
* modified, propagated, or distributed except according to the terms contained * the Free Software Foundation, version 2 of the License.
* in the LICENSE file. *
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <u.h> #include <u.h>
@ -15,17 +22,23 @@
char* char*
getenv(const char *name) getenv(const char *name)
{ {
int r, f; int f;
int32_t s; int32_t l;
char *ans; char path[127+5+1], *value;
char *p, *ep, ename[100];
if(strchr(name, '/') != nil) assert(name != nil);
return nil; if(name[0]=='\0')
snprint(ename, sizeof ename, "/env/%s", name); goto BadName;
if(strcmp(ename+5, name) != 0) if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
return nil; goto BadName;
f = open(ename, OREAD); if(strchr(name, '/')!=nil)
goto BadName;
snprint(path, sizeof path, "/env/%s", name);
if(strcmp(path+5, name) != 0)
goto BadName;
f = open(path, OREAD);
if(f < 0){ if(f < 0){
/* try with #e, in case of a previous rfork(RFCNAMEG) /* try with #e, in case of a previous rfork(RFCNAMEG)
* *
@ -34,25 +47,24 @@ getenv(const char *name)
* using #ec when the open in #e fails is both * using #ec when the open in #e fails is both
* slow and not flexible enough. * slow and not flexible enough.
*/ */
snprint(ename, sizeof ename, "#e/%s", name); snprint(path, sizeof path, "#e/%s", name);
f = open(ename, OREAD); f = open(path, OREAD);
if(f < 0) if(f < 0)
return nil; return nil;
} }
s = seek(f, 0, 2); l = seek(f, 0, 2);
ans = malloc(s+1); value = malloc(l+1);
if(ans) { if(value == nil)
setmalloctag(ans, getcallerpc()); goto Done;
seek(f, 0, 0); setmalloctag(value, getcallerpc());
r = read(f, ans, s); seek(f, 0, 0);
if(r >= 0) { if(read(f, value, l) >= 0)
ep = ans + s - 1; value[l] = '\0';
for(p = ans; p < ep; p++) Done:
if(*p == '\0')
*p = ' ';
ans[s] = '\0';
}
}
close(f); close(f);
return ans; return value;
BadName:
werrstr("bad env name: '%s'", name);
return nil;
} }

View File

@ -1,44 +1,62 @@
/* /*
* Copyright (C) 2016 Giacomo Tesio <giacomo@tesio.it> * This file is part of Jehanne.
* *
* This file is part of the UCB release of Plan 9. It is subject to the license * Copyright (C) 2016-2017 Giacomo Tesio <giacomo@tesio.it>
* terms in the LICENSE file found in the top-level directory of this *
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * Jehanne is free software: you can redistribute it and/or modify
* part of the UCB release of Plan 9, including this file, may be copied, * it under the terms of the GNU General Public License as published by
* modified, propagated, or distributed except according to the terms contained * the Free Software Foundation, version 2 of the License.
* in the LICENSE file. *
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
int int
putenv(const char *name, const char *val) putenv(const char *name, const char *value)
{ {
int f; int f;
char ename[100]; int32_t l;
int32_t s; char path[127+5+1];
if(strchr(name, '/') != nil) assert(name != nil);
return -1; assert(value != nil);
snprint(ename, sizeof ename, "/env/%s", name); if(name[0]=='\0')
if(strcmp(ename+5, name) != 0) goto BadName;
return -1; if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
f = ocreate(ename, OWRITE, 0664); goto BadName;
if(strchr(name, '/')!=nil)
goto BadName;
snprint(path, sizeof path, "/env/%s", name);
if(strcmp(path+5, name) != 0)
goto BadName;
f = ocreate(path, OWRITE, 0664);
if(f < 0){ if(f < 0){
/* try with #e, in case of a previous rfork(RFCNAMEG) /* try with #e, in case of a previous rfork(RFCNAMEG)
*/ */
snprint(ename, sizeof ename, "#e/%s", name); snprint(path, sizeof path, "#e/%s", name);
f = ocreate(ename, OWRITE, 0664); f = ocreate(path, OWRITE, 0664);
if(f < 0) if(f < 0)
return -1; return -1;
return -1;
} }
s = strlen(val); l = strlen(value);
if(write(f, val, s) != s){ if(l > 0 && write(f, value, l) != l){
close(f); close(f);
return -1; return -1;
} }
close(f); close(f);
return 0; return 0;
BadName:
werrstr("bad env name: '%s'", name);
return -1;
} }

View File

@ -10,9 +10,8 @@
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
int32_t int
strlen(const char *s) strlen(const char *s)
{ {
return strchr(s, 0) - s; return strchr(s, 0) - s;
} }