-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminator_posix.go
97 lines (85 loc) · 2.97 KB
/
terminator_posix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//go:build !windows
package terminator
import (
"context"
"fmt"
"os"
"syscall"
"github.com/cockroachdb/errors"
"github.com/shirou/gopsutil/v4/process"
"golang.org/x/sys/unix"
)
// SendSigTerm is the same as SendSigTermWithContext with background context.
func SendSigTerm(pid int) error {
return SendSigTermWithContext(context.Background(), pid)
}
// SendSigTermWithContext sends SIGTERM signal to the process with PID `pid` using context `ctx`.
func SendSigTermWithContext(ctx context.Context, pid int) error {
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), fmt.Sprintf("Send SIGTERM to the process with PID %v", pid))
default:
break
}
proc, err := process.NewProcess(int32(pid))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Send SIGTERM to the process with PID %v", pid))
}
if err := proc.Terminate(); err != nil {
return errors.Wrap(err, fmt.Sprintf("Send SIGTERM to the process with PID %v", pid))
}
return nil
}
// SendSignal is the same as SendSignalWithContext with background context.
func SendSignal(pid int, sig syscall.Signal) error {
return SendSignalWithContext(context.Background(), pid, sig)
}
// SendSignalWithContext sends signal `sig` to the process with PID `pid` using context `ctx`.
func SendSignalWithContext(ctx context.Context, pid int, sig syscall.Signal) error {
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), fmt.Sprintf("Send signal %v to the process with PID %v", sig, pid))
default:
break
}
proc, err := process.NewProcess(int32(pid))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Send signal %v to the process with PID %v", sig, pid))
}
if err := proc.SendSignal(sig); err != nil {
return errors.Wrap(err, fmt.Sprintf("Send signal %v to the process with PID %v", sig, pid))
}
return nil
}
// SendMessage is the same as SendMessageWithContext with background context.
func SendMessage(pid int, msg string) error {
return SendMessageWithContext(context.Background(), pid, msg)
}
// SendMessageWithContext writes a `msg` message to the console process with PID `pid` using context `ctx`.
//
// `msg` must end with "\n" on Linux and with "\r" on macOS to be sent.
//
// Requires root privilegies (e.g. run as sudo).
func SendMessageWithContext(ctx context.Context, pid int, msg string) error {
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), fmt.Sprintf("Write message to stdin of the process with PID %v", pid))
default:
break
}
term, err := GetTerm(pid)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Write message to stdin of the process with PID %v", pid))
}
file, err := os.OpenFile(term, os.O_WRONLY, 0644)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Write message to stdin of the process with PID %v", pid))
}
defer file.Close()
for _, char := range msg {
if err = unix.IoctlSetPointerInt(int(file.Fd()), unix.TIOCSTI, int(char)); err != nil {
return errors.Wrap(err, fmt.Sprintf("Write message to stdin of the process with PID %v", pid))
}
}
return nil
}