Commit b06d2122 authored by Richard Musiol's avatar Richard Musiol Committed by Brad Fitzpatrick

os,syscall: implement functions related to uid, gid and pid on js/wasm

This change implements the following functions on js/wasm:
- os.Chown
- os.Fchown
- os.Lchown
- syscall.Getuid
- syscall.Getgid
- syscall.Geteuid
- syscall.Getegid
- syscall.Getgroups
- syscall.Getpid
- syscall.Getppid
- syscall.Umask

Change-Id: Icdb0fafc02c9df6e9e3573542f8499c3464dc671
Reviewed-on: https://go-review.googlesource.com/c/go/+/154157
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent d0cbf9bf
...@@ -227,8 +227,8 @@ func TestRespectSetgidDir(t *testing.T) { ...@@ -227,8 +227,8 @@ func TestRespectSetgidDir(t *testing.T) {
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
t.Skip("can't set SetGID bit with chmod on iOS") t.Skip("can't set SetGID bit with chmod on iOS")
} }
case "windows", "plan9", "js": case "windows", "plan9":
t.Skip("chown/chmod setgid are not supported on Windows, Plan 9, or JS") t.Skip("chown/chmod setgid are not supported on Windows or Plan 9")
} }
var b Builder var b Builder
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
package os_test package os_test
...@@ -152,6 +152,9 @@ func TestLchown(t *testing.T) { ...@@ -152,6 +152,9 @@ func TestLchown(t *testing.T) {
gid := Getgid() gid := Getgid()
t.Log("gid:", gid) t.Log("gid:", gid)
if err = Lchown(linkname, -1, gid); err != nil { if err = Lchown(linkname, -1, gid); err != nil {
if err, ok := err.(*PathError); ok && err.Err == syscall.ENOSYS {
t.Skip("lchown is unavailable")
}
t.Fatalf("lchown %s -1 %d: %s", linkname, gid, err) t.Fatalf("lchown %s -1 %d: %s", linkname, gid, err)
} }
sys := dir.Sys().(*syscall.Stat_t) sys := dir.Sys().(*syscall.Stat_t)
...@@ -231,6 +234,10 @@ func TestMkdirStickyUmask(t *testing.T) { ...@@ -231,6 +234,10 @@ func TestMkdirStickyUmask(t *testing.T) {
// See also issues: 22939, 24331 // See also issues: 22939, 24331
func newFileTest(t *testing.T, blocking bool) { func newFileTest(t *testing.T, blocking bool) {
if runtime.GOOS == "js" {
t.Skipf("syscall.Pipe is not available on %s.", runtime.GOOS)
}
p := make([]int, 2) p := make([]int, 2)
if err := syscall.Pipe(p); err != nil { if err := syscall.Pipe(p); err != nil {
t.Fatalf("pipe: %v", err) t.Fatalf("pipe: %v", err)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build aix darwin dragonfly freebsd netbsd openbsd solaris // +build aix darwin dragonfly freebsd js,wasm netbsd openbsd solaris
package os package os
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// +build !darwin // +build !darwin
// +build !dragonfly // +build !dragonfly
// +build !freebsd // +build !freebsd
// +build !js !wasm
// +build !netbsd // +build !netbsd
// +build !openbsd // +build !openbsd
// +build !solaris // +build !solaris
......
...@@ -244,18 +244,26 @@ func Chown(path string, uid, gid int) error { ...@@ -244,18 +244,26 @@ func Chown(path string, uid, gid int) error {
if err := checkPath(path); err != nil { if err := checkPath(path); err != nil {
return err return err
} }
return ENOSYS _, err := fsCall("chown", path, uint32(uid), uint32(gid))
return err
} }
func Fchown(fd int, uid, gid int) error { func Fchown(fd int, uid, gid int) error {
return ENOSYS _, err := fsCall("fchown", fd, uint32(uid), uint32(gid))
return err
} }
func Lchown(path string, uid, gid int) error { func Lchown(path string, uid, gid int) error {
if err := checkPath(path); err != nil { if err := checkPath(path); err != nil {
return err return err
} }
if jsFS.Get("lchown") == js.Undefined() {
// fs.lchown is unavailable on Linux until Node.js 10.6.0
// TODO(neelance): remove when we require at least this Node.js version
return ENOSYS return ENOSYS
}
_, err := fsCall("lchown", path, uint32(uid), uint32(gid))
return err
} }
func UtimesNano(path string, ts []Timespec) error { func UtimesNano(path string, ts []Timespec) error {
......
...@@ -285,14 +285,45 @@ func Getwd() (wd string, err error) { ...@@ -285,14 +285,45 @@ func Getwd() (wd string, err error) {
return string(buf[:n]), nil return string(buf[:n]), nil
} }
func Getegid() int { return 1 } func Getuid() int {
func Geteuid() int { return 1 } return jsProcess.Call("getuid").Int()
func Getgid() int { return 1 } }
func Getgroups() ([]int, error) { return []int{1}, nil }
func Getppid() int { return 2 } func Getgid() int {
func Getpid() int { return 3 } return jsProcess.Call("getgid").Int()
}
func Geteuid() int {
return jsProcess.Call("geteuid").Int()
}
func Getegid() int {
return jsProcess.Call("getegid").Int()
}
func Getgroups() ([]int, error) {
array := jsProcess.Call("getgroups")
groups := make([]int, array.Length())
for i := range groups {
groups[i] = array.Index(i).Int()
}
return groups, nil
}
func Getpid() int {
return jsProcess.Get("pid").Int()
}
func Getppid() int {
return jsProcess.Get("ppid").Int()
}
func Umask(mask int) (oldmask int) {
return jsProcess.Call("umask", mask).Int()
}
func Gettimeofday(tv *Timeval) error { return ENOSYS } func Gettimeofday(tv *Timeval) error { return ENOSYS }
func Getuid() int { return 1 }
func Kill(pid int, signum Signal) error { return ENOSYS } func Kill(pid int, signum Signal) error { return ENOSYS }
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
return 0, ENOSYS return 0, ENOSYS
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment