Skip to content

Commit

Permalink
unix: add PrctlRetInt, a Prctl variant that returns (int, error)
Browse files Browse the repository at this point in the history
Fixes golang/go#35978

Change-Id: I396f0c2c6443e000bcecc9081841cd55145c050b
GitHub-Last-Rev: d461448
GitHub-Pull-Request: #48
Reviewed-on: https://go-review.googlesource.com/c/sys/+/209969
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
  • Loading branch information
darkLord19 authored and tklauser committed Dec 18, 2019
1 parent ac6580d commit 4a24b40
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions unix/syscall_linux.go
Expand Up @@ -1631,6 +1631,17 @@ func Getpgrp() (pid int) {
//sysnb Settimeofday(tv *Timeval) (err error)
//sys Setns(fd int, nstype int) (err error)

// PrctlRetInt performs a prctl operation specified by option and further
// optional arguments arg2 through arg5 depending on option. It returns a
// non-negative integer that is returned by the prctl syscall.
func PrctlRetInt(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (int, error) {
ret, _, err := Syscall6(SYS_PRCTL, uintptr(option), uintptr(arg2), uintptr(arg3), uintptr(arg4), uintptr(arg5), 0)
if err != 0 {
return 0, err
}
return int(ret), nil
}

// issue 1435.
// On linux Setuid and Setgid only affects the current thread, not the process.
// This does not match what most callers expect so we must return an error
Expand Down
14 changes: 14 additions & 0 deletions unix/syscall_linux_test.go
Expand Up @@ -686,3 +686,17 @@ func TestEpoll(t *testing.T) {
t.Errorf("EpollWait: wrong Fd in event: got %v, expected %v", got, fd)
}
}

func TestPrctlRetInt(t *testing.T) {
err := unix.Prctl(unix.PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)
if err != nil {
t.Skipf("Prctl: %v, skipping test", err)
}
v, err := unix.PrctlRetInt(unix.PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)
if err != nil {
t.Fatalf("failed to perform prctl: %v", err)
}
if v != 1 {
t.Fatalf("unexpected return from prctl; got %v, expected %v", v, 1)
}
}

0 comments on commit 4a24b40

Please sign in to comment.