mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
syscall: restore signal mask after setting foreground process group
Fixes #37217 Change-Id: I0151bb77fc4c4552d1b19c31d784943b72f84b80 Reviewed-on: https://go-review.googlesource.com/c/go/+/313653 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
parent
39844971fb
commit
7d22c2181b
5 changed files with 56 additions and 9 deletions
|
|
@ -90,8 +90,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
||||||
|
|
||||||
// Fork succeeded, now in child.
|
// Fork succeeded, now in child.
|
||||||
|
|
||||||
runtime_AfterForkInChild()
|
|
||||||
|
|
||||||
// Enable tracing if requested.
|
// Enable tracing if requested.
|
||||||
if sys.Ptrace {
|
if sys.Ptrace {
|
||||||
_, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
|
_, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
|
||||||
|
|
@ -137,6 +135,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the signal mask. We do this after TIOCSPGRP to avoid
|
||||||
|
// having the kernel send a SIGTTOU signal to the process group.
|
||||||
|
runtime_AfterForkInChild()
|
||||||
|
|
||||||
// Chroot
|
// Chroot
|
||||||
if chroot != nil {
|
if chroot != nil {
|
||||||
_, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
|
_, _, err1 = RawSyscall(SYS_CHROOT, uintptr(unsafe.Pointer(chroot)), 0, 0)
|
||||||
|
|
|
||||||
|
|
@ -116,8 +116,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
||||||
|
|
||||||
// Fork succeeded, now in child.
|
// Fork succeeded, now in child.
|
||||||
|
|
||||||
runtime_AfterForkInChild()
|
|
||||||
|
|
||||||
// Session ID
|
// Session ID
|
||||||
if sys.Setsid {
|
if sys.Setsid {
|
||||||
_, err1 = setsid()
|
_, err1 = setsid()
|
||||||
|
|
@ -153,6 +151,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the signal mask. We do this after TIOCSPGRP to avoid
|
||||||
|
// having the kernel send a SIGTTOU signal to the process group.
|
||||||
|
runtime_AfterForkInChild()
|
||||||
|
|
||||||
// Chroot
|
// Chroot
|
||||||
if chroot != nil {
|
if chroot != nil {
|
||||||
err1 = chroot1(uintptr(unsafe.Pointer(chroot)))
|
err1 = chroot1(uintptr(unsafe.Pointer(chroot)))
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,6 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
||||||
|
|
||||||
// Fork succeeded, now in child.
|
// Fork succeeded, now in child.
|
||||||
|
|
||||||
runtime_AfterForkInChild()
|
|
||||||
|
|
||||||
// Enable tracing if requested.
|
// Enable tracing if requested.
|
||||||
if sys.Ptrace {
|
if sys.Ptrace {
|
||||||
if err := ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
|
if err := ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
|
||||||
|
|
@ -136,6 +134,10 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the signal mask. We do this after TIOCSPGRP to avoid
|
||||||
|
// having the kernel send a SIGTTOU signal to the process group.
|
||||||
|
runtime_AfterForkInChild()
|
||||||
|
|
||||||
// Chroot
|
// Chroot
|
||||||
if chroot != nil {
|
if chroot != nil {
|
||||||
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
|
_, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
|
||||||
|
|
|
||||||
|
|
@ -233,8 +233,6 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
|
||||||
|
|
||||||
// Fork succeeded, now in child.
|
// Fork succeeded, now in child.
|
||||||
|
|
||||||
runtime_AfterForkInChild()
|
|
||||||
|
|
||||||
// Enable the "keep capabilities" flag to set ambient capabilities later.
|
// Enable the "keep capabilities" flag to set ambient capabilities later.
|
||||||
if len(sys.AmbientCaps) > 0 {
|
if len(sys.AmbientCaps) > 0 {
|
||||||
_, _, err1 = RawSyscall6(SYS_PRCTL, PR_SET_KEEPCAPS, 1, 0, 0, 0, 0)
|
_, _, err1 = RawSyscall6(SYS_PRCTL, PR_SET_KEEPCAPS, 1, 0, 0, 0, 0)
|
||||||
|
|
@ -294,6 +292,10 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore the signal mask. We do this after TIOCSPGRP to avoid
|
||||||
|
// having the kernel send a SIGTTOU signal to the process group.
|
||||||
|
runtime_AfterForkInChild()
|
||||||
|
|
||||||
// Unshare
|
// Unshare
|
||||||
if sys.Unshareflags != 0 {
|
if sys.Unshareflags != 0 {
|
||||||
_, _, err1 = RawSyscall(SYS_UNSHARE, sys.Unshareflags, 0, 0)
|
_, _, err1 = RawSyscall(SYS_UNSHARE, sys.Unshareflags, 0, 0)
|
||||||
|
|
|
||||||
|
|
@ -167,11 +167,13 @@ func TestPgid(t *testing.T) {
|
||||||
|
|
||||||
func TestForeground(t *testing.T) {
|
func TestForeground(t *testing.T) {
|
||||||
signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)
|
signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)
|
||||||
|
defer signal.Reset()
|
||||||
|
|
||||||
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Skipf("Can't test Foreground. Couldn't open /dev/tty: %s", err)
|
t.Skipf("Can't test Foreground. Couldn't open /dev/tty: %s", err)
|
||||||
}
|
}
|
||||||
|
defer tty.Close()
|
||||||
|
|
||||||
// This should really be pid_t, however _C_int (aka int32) is generally
|
// This should really be pid_t, however _C_int (aka int32) is generally
|
||||||
// equivalent.
|
// equivalent.
|
||||||
|
|
@ -216,8 +218,45 @@ func TestForeground(t *testing.T) {
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
t.Fatalf("TIOCSPGRP failed with error code: %s", errno)
|
t.Fatalf("TIOCSPGRP failed with error code: %s", errno)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
signal.Reset()
|
func TestForegroundSignal(t *testing.T) {
|
||||||
|
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("couldn't open /dev/tty: %s", err)
|
||||||
|
}
|
||||||
|
defer tty.Close()
|
||||||
|
|
||||||
|
ch1 := make(chan os.Signal, 1)
|
||||||
|
ch2 := make(chan bool)
|
||||||
|
|
||||||
|
signal.Notify(ch1, syscall.SIGTTIN, syscall.SIGTTOU)
|
||||||
|
defer signal.Stop(ch1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
cmd := create(t)
|
||||||
|
cmd.proc.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
Ctty: int(tty.Fd()),
|
||||||
|
Foreground: true,
|
||||||
|
}
|
||||||
|
cmd.Start()
|
||||||
|
cmd.Stop()
|
||||||
|
close(ch2)
|
||||||
|
}()
|
||||||
|
|
||||||
|
timer := time.NewTimer(30 * time.Second)
|
||||||
|
defer timer.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case sig := <-ch1:
|
||||||
|
t.Errorf("unexpected signal %v", sig)
|
||||||
|
case <-ch2:
|
||||||
|
// Success.
|
||||||
|
return
|
||||||
|
case <-timer.C:
|
||||||
|
t.Fatal("timed out waiting for child process")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test a couple of cases that SysProcAttr can't handle. Issue 29458.
|
// Test a couple of cases that SysProcAttr can't handle. Issue 29458.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue