-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstdio_process_unix.go
More file actions
170 lines (155 loc) · 3.52 KB
/
Copy pathstdio_process_unix.go
File metadata and controls
170 lines (155 loc) · 3.52 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//go:build unix
// Tencent is pleased to support the open source community by making trpc-mcp-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-mcp-go is licensed under the Apache License Version 2.0.
package mcp
import (
"errors"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
type stdioProcEntry struct {
pid int
ppid int
pgid int
}
func configureStdioProcess(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
func stdioProcessGroupID(cmd *exec.Cmd) int {
if cmd == nil || cmd.Process == nil {
return 0
}
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
return 0
}
return pgid
}
func stdioDescendantProcessGroupIDs(cmd *exec.Cmd) []int {
if cmd == nil || cmd.Process == nil {
return nil
}
entries := readStdioProcEntries()
children := make(map[int][]stdioProcEntry)
for _, entry := range entries {
children[entry.ppid] = append(children[entry.ppid], entry)
}
seenPID := map[int]bool{cmd.Process.Pid: true}
seenPGID := make(map[int]bool)
queue := []int{cmd.Process.Pid}
for len(queue) > 0 {
parent := queue[0]
queue = queue[1:]
for _, child := range children[parent] {
if seenPID[child.pid] {
continue
}
seenPID[child.pid] = true
queue = append(queue, child.pid)
if child.pgid > 0 {
seenPGID[child.pgid] = true
}
}
}
pgids := make([]int, 0, len(seenPGID))
for pgid := range seenPGID {
pgids = append(pgids, pgid)
}
return pgids
}
func signalStdioProcess(cmd *exec.Cmd, pgid int, signal os.Signal) error {
if cmd == nil || cmd.Process == nil {
return nil
}
sig, ok := signal.(syscall.Signal)
if !ok {
return cmd.Process.Signal(signal)
}
if pgid > 0 {
if err := killProcessGroup(pgid, sig); err == nil {
return nil
}
}
return cmd.Process.Signal(signal)
}
func killStdioProcess(cmd *exec.Cmd, pgid int) error {
if cmd == nil || cmd.Process == nil {
return nil
}
if pgid > 0 {
if err := killProcessGroup(pgid, syscall.SIGKILL); err == nil {
return nil
}
}
return cmd.Process.Kill()
}
func killStdioProcessGroups(pgids []int) error {
var errs []error
seen := make(map[int]bool)
for _, pgid := range pgids {
if pgid <= 0 || seen[pgid] {
continue
}
seen[pgid] = true
if err := killProcessGroup(pgid, syscall.SIGKILL); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func killProcessGroup(pgid int, sig syscall.Signal) error {
err := syscall.Kill(-pgid, sig)
if errors.Is(err, syscall.ESRCH) {
return nil
}
return err
}
func readStdioProcEntries() []stdioProcEntry {
files, err := os.ReadDir("/proc")
if err != nil {
return nil
}
entries := make([]stdioProcEntry, 0, len(files))
for _, file := range files {
if !file.IsDir() {
continue
}
pid, err := strconv.Atoi(file.Name())
if err != nil {
continue
}
data, err := os.ReadFile("/proc/" + file.Name() + "/stat")
if err != nil {
continue
}
if entry, ok := parseStdioProcStat(pid, string(data)); ok {
entries = append(entries, entry)
}
}
return entries
}
func parseStdioProcStat(pid int, stat string) (stdioProcEntry, bool) {
end := strings.LastIndex(stat, ") ")
if end < 0 || end+2 >= len(stat) {
return stdioProcEntry{}, false
}
fields := strings.Fields(stat[end+2:])
if len(fields) < 3 {
return stdioProcEntry{}, false
}
ppid, err := strconv.Atoi(fields[1])
if err != nil {
return stdioProcEntry{}, false
}
pgid, err := strconv.Atoi(fields[2])
if err != nil {
return stdioProcEntry{}, false
}
return stdioProcEntry{pid: pid, ppid: ppid, pgid: pgid}, true
}