1
0
mirror of https://github.com/golang/go synced 2024-11-05 11:36:10 -07:00

runtime: implement getenv for Plan 9

With this change the runtime can now read GOMAXPROCS, GOGC, etc.

I'm not quite sure how we missed this.

R=seed, lucio.dere, rsc
CC=golang-dev
https://golang.org/cl/6935062
This commit is contained in:
Anthony Martin 2012-12-17 11:07:40 -05:00 committed by Russ Cox
parent 94430937ac
commit 432f18221f
6 changed files with 130 additions and 53 deletions

View File

@ -0,0 +1,33 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "runtime.h"
#include "os_GOOS.h"
byte*
runtime·getenv(int8 *s)
{
int32 fd, len, n, r;
byte file[128];
byte *p;
len = runtime·findnull((byte*)s);
if(len > sizeof file-6)
return nil;
runtime·memclr(file, sizeof file);
runtime·memmove((void*)file, (void*)"/env/", 5);
runtime·memmove((void*)(file+5), (void*)s, len);
fd = runtime·open(file, OREAD);
if(fd < 0)
return nil;
n = runtime·seek(fd, 0, 2);
p = runtime·malloc(n+1);
r = runtime·pread(fd, p, n, 0);
runtime·close(fd);
if(r < 0)
return nil;
return p;
}

View File

@ -0,0 +1,61 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd linux netbsd openbsd windows
#include "runtime.h"
Slice syscall·envs;
byte*
runtime·getenv(int8 *s)
{
int32 i, j, len;
byte *v, *bs;
String* envv;
int32 envc;
bs = (byte*)s;
len = runtime·findnull(bs);
envv = (String*)syscall·envs.array;
envc = syscall·envs.len;
for(i=0; i<envc; i++){
if(envv[i].len <= len)
continue;
v = envv[i].str;
for(j=0; j<len; j++)
if(bs[j] != v[j])
goto nomatch;
if(v[len] != '=')
goto nomatch;
return v+len+1;
nomatch:;
}
return nil;
}
void (*libcgo_setenv)(byte**);
// Update the C environment if cgo is loaded.
// Called from syscall.Setenv.
void
syscall·setenv_c(String k, String v)
{
byte *arg[2];
if(libcgo_setenv == nil)
return;
arg[0] = runtime·malloc(k.len + 1);
runtime·memmove(arg[0], k.str, k.len);
arg[0][k.len] = 0;
arg[1] = runtime·malloc(v.len + 1);
runtime·memmove(arg[1], v.str, v.len);
arg[1][v.len] = 0;
runtime·asmcgocall((void*)libcgo_setenv, arg);
runtime·free(arg[0]);
runtime·free(arg[1]);
}

View File

@ -7,6 +7,7 @@ int32 runtime·open(uint8 *file, int32 mode);
int32 runtime·pread(int32 fd, void *buf, int32 nbytes, int64 offset);
int32 runtime·pwrite(int32 fd, void *buf, int32 nbytes, int64 offset);
int32 runtime·read(int32 fd, void *buf, int32 nbytes);
int64 runtime·seek(int32 fd, int64 offset, int32 whence);
int32 runtime·close(int32 fd);
void runtime·exits(int8* msg);
intptr runtime·brk_(void*);

View File

@ -110,58 +110,6 @@ runtime·goenvs_unix(void)
syscall·envs.cap = n;
}
byte*
runtime·getenv(int8 *s)
{
int32 i, j, len;
byte *v, *bs;
String* envv;
int32 envc;
bs = (byte*)s;
len = runtime·findnull(bs);
envv = (String*)syscall·envs.array;
envc = syscall·envs.len;
for(i=0; i<envc; i++){
if(envv[i].len <= len)
continue;
v = envv[i].str;
for(j=0; j<len; j++)
if(bs[j] != v[j])
goto nomatch;
if(v[len] != '=')
goto nomatch;
return v+len+1;
nomatch:;
}
return nil;
}
void (*libcgo_setenv)(byte**);
// Update the C environment if cgo is loaded.
// Called from syscall.Setenv.
void
syscall·setenv_c(String k, String v)
{
byte *arg[2];
if(libcgo_setenv == nil)
return;
arg[0] = runtime·malloc(k.len + 1);
runtime·memmove(arg[0], k.str, k.len);
arg[0][k.len] = 0;
arg[1] = runtime·malloc(v.len + 1);
runtime·memmove(arg[1], v.str, v.len);
arg[1][v.len] = 0;
runtime·asmcgocall((void*)libcgo_setenv, arg);
runtime·free(arg[0]);
runtime·free(arg[1]);
}
void
runtime·getgoroot(String out)
{

View File

@ -24,9 +24,19 @@ TEXT runtime·pwrite(SB),7,$0
INT $64
RET
TEXT runtime·seek(SB),7,$0
MOVL $39, AX
INT $64
CMPL AX, $-1
JNE 4(PC)
MOVL a+0(FP), CX
MOVL AX, 0(CX)
MOVL AX, 4(CX)
RET
TEXT runtime·close(SB),7,$0
MOVL $4, AX
INT $64
INT $64
RET
TEXT runtime·exits(SB),7,$0

View File

@ -27,6 +27,30 @@ TEXT runtime·pwrite(SB),7,$0
SYSCALL
RET
// int32 _seek(int64*, int32, int64, int32)
TEXT _seek<>(SB),7,$0
MOVQ $0x8000, AX
MOVQ $39, BP
SYSCALL
RET
// int64 seek(int32, int64, int32)
TEXT runtime·seek(SB),7,$56
LEAQ new+48(SP), CX
MOVQ CX, 0(SP)
MOVQ fd+0(FP), CX
MOVQ CX, 8(SP)
MOVQ off+8(FP), CX
MOVQ CX, 16(SP)
MOVQ whence+16(FP), CX
MOVQ CX, 24(SP)
CALL _seek<>(SB)
CMPL AX, $0
JGE 2(PC)
MOVQ $-1, new+48(SP)
MOVQ new+48(SP), AX
RET
TEXT runtime·close(SB),7,$0
MOVQ $0x8000, AX
MOVQ $4, BP