diff options
author | Ayke van Laethem <[email protected]> | 2021-06-25 15:06:17 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-06-25 16:14:47 +0200 |
commit | 75298bb84bcb966998ca00f3cc5506a9757a5038 (patch) | |
tree | 2ec813d6a6f9d88a59821f992f6b4ecb80c19422 | |
parent | e65592599cb6c3a0ffd73d5a021b9aa4e00959ae (diff) | |
download | tinygo-75298bb84bcb966998ca00f3cc5506a9757a5038.tar.gz tinygo-75298bb84bcb966998ca00f3cc5506a9757a5038.zip |
os: implement process related functions
This commit implements various process related functions like
os.Getuid() and os.Getpid(). It also implements or improves this support
in the syscall package if it isn't available yet.
-rw-r--r-- | src/os/exec.go | 12 | ||||
-rw-r--r-- | src/os/file.go | 5 | ||||
-rw-r--r-- | src/os/proc.go | 28 | ||||
-rw-r--r-- | src/syscall/proc_emulated.go | 13 | ||||
-rw-r--r-- | src/syscall/proc_hosted.go | 37 | ||||
-rw-r--r-- | src/syscall/syscall_baremetal.go | 6 | ||||
-rw-r--r-- | src/syscall/syscall_libc.go | 4 | ||||
-rw-r--r-- | testdata/stdlib.go | 9 |
8 files changed, 99 insertions, 15 deletions
diff --git a/src/os/exec.go b/src/os/exec.go index 66cc79999..8bc544ba2 100644 --- a/src/os/exec.go +++ b/src/os/exec.go @@ -1,6 +1,18 @@ package os +import "syscall" + type Signal interface { String() string Signal() // to distinguish from other Stringers } + +// Getpid returns the process id of the caller, or -1 if unavailable. +func Getpid() int { + return syscall.Getpid() +} + +// Getppid returns the process id of the caller's parent, or -1 if unavailable. +func Getppid() int { + return syscall.Getppid() +} diff --git a/src/os/file.go b/src/os/file.go index a7aa40430..4d90bdde5 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -196,8 +196,3 @@ func Readlink(name string) (string, error) { func TempDir() string { return "/tmp" } - -// Getpid is a stub (for now), always returning 1 -func Getpid() int { - return 1 -} diff --git a/src/os/proc.go b/src/os/proc.go index d3bfb1270..fe2245f36 100644 --- a/src/os/proc.go +++ b/src/os/proc.go @@ -24,3 +24,31 @@ func runtime_args() []string // in package runtime func Exit(code int) { syscall.Exit(code) } + +// Getuid returns the numeric user id of the caller. +// +// On non-POSIX systems, it returns -1. +func Getuid() int { + return syscall.Getuid() +} + +// Geteuid returns the numeric effective user id of the caller. +// +// On non-POSIX systems, it returns -1. +func Geteuid() int { + return syscall.Geteuid() +} + +// Getgid returns the numeric group id of the caller. +// +// On non-POSIX systems, it returns -1. +func Getgid() int { + return syscall.Getgid() +} + +// Getegid returns the numeric effective group id of the caller. +// +// On non-POSIX systems, it returns -1. +func Getegid() int { + return syscall.Getegid() +} diff --git a/src/syscall/proc_emulated.go b/src/syscall/proc_emulated.go new file mode 100644 index 000000000..89b22e818 --- /dev/null +++ b/src/syscall/proc_emulated.go @@ -0,0 +1,13 @@ +// +build baremetal wasi wasm + +// This file emulates some process-related functions that are only available +// under a real operating system. + +package syscall + +func Getuid() int { return -1 } +func Geteuid() int { return -1 } +func Getgid() int { return -1 } +func Getegid() int { return -1 } +func Getpid() int { return -1 } +func Getppid() int { return -1 } diff --git a/src/syscall/proc_hosted.go b/src/syscall/proc_hosted.go new file mode 100644 index 000000000..5f52a4ca8 --- /dev/null +++ b/src/syscall/proc_hosted.go @@ -0,0 +1,37 @@ +// +build !baremetal,!wasi,!wasm + +// This file assumes there is a libc available that runs on a real operating +// system. + +package syscall + +func Getuid() int { return int(libc_getuid()) } +func Geteuid() int { return int(libc_geteuid()) } +func Getgid() int { return int(libc_getgid()) } +func Getegid() int { return int(libc_getegid()) } +func Getpid() int { return int(libc_getpid()) } +func Getppid() int { return int(libc_getppid()) } + +// uid_t getuid(void) +//export getuid +func libc_getuid() int32 + +// gid_t getgid(void) +//export getgid +func libc_getgid() int32 + +// uid_t geteuid(void) +//export geteuid +func libc_geteuid() int32 + +// gid_t getegid(void) +//export getegid +func libc_getegid() int32 + +// gid_t getpid(void) +//export getpid +func libc_getpid() int32 + +// gid_t getppid(void) +//export getppid +func libc_getppid() int32 diff --git a/src/syscall/syscall_baremetal.go b/src/syscall/syscall_baremetal.go index 28fa39ba8..4f0c6e530 100644 --- a/src/syscall/syscall_baremetal.go +++ b/src/syscall/syscall_baremetal.go @@ -98,14 +98,8 @@ type ProcAttr struct { type SysProcAttr struct { } -func Getegid() int { return 1 } -func Geteuid() int { return 1 } -func Getgid() int { return 1 } func Getgroups() ([]int, error) { return []int{1}, nil } -func Getppid() int { return 2 } -func Getpid() int { return 3 } func Gettimeofday(tv *Timeval) error { return ENOSYS } -func Getuid() int { return 1 } func Kill(pid int, signum Signal) error { return ENOSYS } func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { return 0, ENOSYS diff --git a/src/syscall/syscall_libc.go b/src/syscall/syscall_libc.go index 80675f191..bc6359630 100644 --- a/src/syscall/syscall_libc.go +++ b/src/syscall/syscall_libc.go @@ -62,10 +62,6 @@ func Kill(pid int, sig Signal) (err error) { return ENOSYS // TODO } -func Getpid() (pid int) { - panic("unimplemented: getpid") // TODO -} - func Getenv(key string) (value string, found bool) { data := append([]byte(key), 0) raw := libc_getenv(&data[0]) diff --git a/testdata/stdlib.go b/testdata/stdlib.go index e6d6677b6..55237ff68 100644 --- a/testdata/stdlib.go +++ b/testdata/stdlib.go @@ -5,6 +5,7 @@ import ( "math/rand" "os" "strings" + "syscall" ) func main() { @@ -13,6 +14,14 @@ func main() { fmt.Println("stdout:", os.Stdout.Name()) fmt.Println("stderr:", os.Stderr.Name()) + // Package syscall, this mostly checks whether the calls don't trigger an error. + syscall.Getuid() + syscall.Geteuid() + syscall.Getgid() + syscall.Getegid() + syscall.Getpid() + syscall.Getppid() + // package math/rand fmt.Println("pseudorandom number:", rand.Int31()) |