Skip to content

Commit 75298bb

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent e655925 commit 75298bb

File tree

8 files changed

+99
-15
lines changed

8 files changed

+99
-15
lines changed

src/os/exec.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
package os
22

3+
import "syscall"
4+
35
type Signal interface {
46
String() string
57
Signal() // to distinguish from other Stringers
68
}
9+
10+
// Getpid returns the process id of the caller, or -1 if unavailable.
11+
func Getpid() int {
12+
return syscall.Getpid()
13+
}
14+
15+
// Getppid returns the process id of the caller's parent, or -1 if unavailable.
16+
func Getppid() int {
17+
return syscall.Getppid()
18+
}

src/os/file.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,3 @@ func Readlink(name string) (string, error) {
196196
func TempDir() string {
197197
return "/tmp"
198198
}
199-
200-
// Getpid is a stub (for now), always returning 1
201-
func Getpid() int {
202-
return 1
203-
}

src/os/proc.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,31 @@ func runtime_args() []string // in package runtime
2424
func Exit(code int) {
2525
syscall.Exit(code)
2626
}
27+
28+
// Getuid returns the numeric user id of the caller.
29+
//
30+
// On non-POSIX systems, it returns -1.
31+
func Getuid() int {
32+
return syscall.Getuid()
33+
}
34+
35+
// Geteuid returns the numeric effective user id of the caller.
36+
//
37+
// On non-POSIX systems, it returns -1.
38+
func Geteuid() int {
39+
return syscall.Geteuid()
40+
}
41+
42+
// Getgid returns the numeric group id of the caller.
43+
//
44+
// On non-POSIX systems, it returns -1.
45+
func Getgid() int {
46+
return syscall.Getgid()
47+
}
48+
49+
// Getegid returns the numeric effective group id of the caller.
50+
//
51+
// On non-POSIX systems, it returns -1.
52+
func Getegid() int {
53+
return syscall.Getegid()
54+
}

src/syscall/proc_emulated.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build baremetal wasi wasm
2+
3+
// This file emulates some process-related functions that are only available
4+
// under a real operating system.
5+
6+
package syscall
7+
8+
func Getuid() int { return -1 }
9+
func Geteuid() int { return -1 }
10+
func Getgid() int { return -1 }
11+
func Getegid() int { return -1 }
12+
func Getpid() int { return -1 }
13+
func Getppid() int { return -1 }

src/syscall/proc_hosted.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// +build !baremetal,!wasi,!wasm
2+
3+
// This file assumes there is a libc available that runs on a real operating
4+
// system.
5+
6+
package syscall
7+
8+
func Getuid() int { return int(libc_getuid()) }
9+
func Geteuid() int { return int(libc_geteuid()) }
10+
func Getgid() int { return int(libc_getgid()) }
11+
func Getegid() int { return int(libc_getegid()) }
12+
func Getpid() int { return int(libc_getpid()) }
13+
func Getppid() int { return int(libc_getppid()) }
14+
15+
// uid_t getuid(void)
16+
//export getuid
17+
func libc_getuid() int32
18+
19+
// gid_t getgid(void)
20+
//export getgid
21+
func libc_getgid() int32
22+
23+
// uid_t geteuid(void)
24+
//export geteuid
25+
func libc_geteuid() int32
26+
27+
// gid_t getegid(void)
28+
//export getegid
29+
func libc_getegid() int32
30+
31+
// gid_t getpid(void)
32+
//export getpid
33+
func libc_getpid() int32
34+
35+
// gid_t getppid(void)
36+
//export getppid
37+
func libc_getppid() int32

src/syscall/syscall_baremetal.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,8 @@ type ProcAttr struct {
9898
type SysProcAttr struct {
9999
}
100100

101-
func Getegid() int { return 1 }
102-
func Geteuid() int { return 1 }
103-
func Getgid() int { return 1 }
104101
func Getgroups() ([]int, error) { return []int{1}, nil }
105-
func Getppid() int { return 2 }
106-
func Getpid() int { return 3 }
107102
func Gettimeofday(tv *Timeval) error { return ENOSYS }
108-
func Getuid() int { return 1 }
109103
func Kill(pid int, signum Signal) error { return ENOSYS }
110104
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
111105
return 0, ENOSYS

src/syscall/syscall_libc.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ func Kill(pid int, sig Signal) (err error) {
6262
return ENOSYS // TODO
6363
}
6464

65-
func Getpid() (pid int) {
66-
panic("unimplemented: getpid") // TODO
67-
}
68-
6965
func Getenv(key string) (value string, found bool) {
7066
data := append([]byte(key), 0)
7167
raw := libc_getenv(&data[0])

testdata/stdlib.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math/rand"
66
"os"
77
"strings"
8+
"syscall"
89
)
910

1011
func main() {
@@ -13,6 +14,14 @@ func main() {
1314
fmt.Println("stdout:", os.Stdout.Name())
1415
fmt.Println("stderr:", os.Stderr.Name())
1516

17+
// Package syscall, this mostly checks whether the calls don't trigger an error.
18+
syscall.Getuid()
19+
syscall.Geteuid()
20+
syscall.Getgid()
21+
syscall.Getegid()
22+
syscall.Getpid()
23+
syscall.Getppid()
24+
1625
// package math/rand
1726
fmt.Println("pseudorandom number:", rand.Int31())
1827

0 commit comments

Comments
 (0)