]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: implement getenv for Plan 9
authorAnthony Martin <ality@pbrane.org>
Mon, 17 Dec 2012 16:07:40 +0000 (11:07 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 17 Dec 2012 16:07:40 +0000 (11:07 -0500)
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

src/pkg/runtime/env_plan9.c [new file with mode: 0644]
src/pkg/runtime/env_posix.c [new file with mode: 0644]
src/pkg/runtime/os_plan9.h
src/pkg/runtime/runtime.c
src/pkg/runtime/sys_plan9_386.s
src/pkg/runtime/sys_plan9_amd64.s

diff --git a/src/pkg/runtime/env_plan9.c b/src/pkg/runtime/env_plan9.c
new file mode 100644 (file)
index 0000000..848d733
--- /dev/null
@@ -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;
+}
diff --git a/src/pkg/runtime/env_posix.c b/src/pkg/runtime/env_posix.c
new file mode 100644 (file)
index 0000000..b4342d3
--- /dev/null
@@ -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]);
+}
index 7002897efa3f9bf62b0f501f549a1334cea3b365..7fb8aa6249721d39a09315dcf699b6438e202a2f 100644 (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*);
index e4346f03bfca3c1de399378064003bed36879ce3..4d57cbafdff1af4050729a621c610d5d8c39fa93 100644 (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)
 {
index 1b4d52be1f243fecee45a92aa82537861e32bef8..f8034d4778b74576bb4cb5c640e5872fe2ed5cbf 100644 (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
index d2ccfb2328796d090ac4f80f646b30bc29de77bc..b5e8c59b3ccd8bc920a6601d5a2a96d15db51e87 100644 (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