Skip to content

Commit 9fbfb55

Browse files
committed
add cross-platform interrupt signal utility
1 parent bde3096 commit 9fbfb55

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

process/interrupt_unix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !windows
2+
3+
package process
4+
5+
import "os"
6+
7+
// SendInterrupt sends an interrupt signal to the current process.
8+
// On Unix systems, this sends os.Interrupt (SIGINT).
9+
func SendInterrupt() {
10+
if p, err := os.FindProcess(os.Getpid()); err == nil {
11+
_ = p.Signal(os.Interrupt)
12+
}
13+
}

process/interrupt_windows.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build windows
2+
3+
package process
4+
5+
import "syscall"
6+
7+
var (
8+
kernel32 = syscall.NewLazyDLL("kernel32.dll")
9+
procGenerateConsoleCtrlEvent = kernel32.NewProc("GenerateConsoleCtrlEvent")
10+
)
11+
12+
// SendInterrupt sends an interrupt signal to the current process.
13+
// On Windows, this uses GenerateConsoleCtrlEvent with CTRL_BREAK_EVENT
14+
// because Go's p.Signal(os.Interrupt) is not implemented on Windows.
15+
func SendInterrupt() {
16+
// CTRL_BREAK_EVENT = 1
17+
procGenerateConsoleCtrlEvent.Call(1, 0)
18+
}

0 commit comments

Comments
 (0)