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.
This commit is contained in:
Ayke van Laethem
2021-06-25 15:06:17 +02:00
committed by Ron Evans
parent e65592599c
commit 75298bb84b
8 changed files with 99 additions and 15 deletions
+12
View File
@@ -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()
}
-5
View File
@@ -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
}
+28
View File
@@ -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()
}
+13
View File
@@ -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 }
+37
View File
@@ -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
-6
View File
@@ -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
-4
View File
@@ -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])
+9
View File
@@ -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())