github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sighandling/sighandling_linux_unsafe.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build linux 16 // +build linux 17 18 package sighandling 19 20 import ( 21 "fmt" 22 "unsafe" 23 24 "golang.org/x/sys/unix" 25 "github.com/metacubex/gvisor/pkg/abi/linux" 26 ) 27 28 // IgnoreChildStop sets the SA_NOCLDSTOP flag, causing child processes to not 29 // generate SIGCHLD when they stop. 30 func IgnoreChildStop() error { 31 var sa linux.SigAction 32 33 // Get the existing signal handler information, and set the flag. 34 if _, _, e := unix.RawSyscall6(unix.SYS_RT_SIGACTION, uintptr(unix.SIGCHLD), 0, uintptr(unsafe.Pointer(&sa)), linux.SignalSetSize, 0, 0); e != 0 { 35 return e 36 } 37 sa.Flags |= linux.SA_NOCLDSTOP 38 if _, _, e := unix.RawSyscall6(unix.SYS_RT_SIGACTION, uintptr(unix.SIGCHLD), uintptr(unsafe.Pointer(&sa)), 0, linux.SignalSetSize, 0, 0); e != 0 { 39 return e 40 } 41 42 return nil 43 } 44 45 // ReplaceSignalHandler replaces the existing signal handler for the provided 46 // signal with the function pointer at `handler`. This bypasses the Go runtime 47 // signal handlers, and should only be used for low-level signal handlers where 48 // use of signal.Notify is not appropriate. 49 // 50 // It stores the value of the previously set handler in previous. 51 func ReplaceSignalHandler(sig unix.Signal, handler uintptr, previous *uintptr) error { 52 var sa linux.SigAction 53 const maskLen = 8 54 55 // Get the existing signal handler information, and save the current 56 // handler. Once we replace it, we will use this pointer to fall back to 57 // it when we receive other signals. 58 if _, _, e := unix.RawSyscall6(unix.SYS_RT_SIGACTION, uintptr(sig), 0, uintptr(unsafe.Pointer(&sa)), maskLen, 0, 0); e != 0 { 59 return e 60 } 61 62 // Fail if there isn't a previous handler. 63 if sa.Handler == 0 { 64 return fmt.Errorf("previous handler for signal %x isn't set", sig) 65 } 66 67 *previous = uintptr(sa.Handler) 68 69 // Install our own handler. 70 sa.Handler = uint64(handler) 71 if _, _, e := unix.RawSyscall6(unix.SYS_RT_SIGACTION, uintptr(sig), uintptr(unsafe.Pointer(&sa)), 0, maskLen, 0, 0); e != 0 { 72 return e 73 } 74 75 return nil 76 }