-
Notifications
You must be signed in to change notification settings - Fork 1
/
borrow.go
131 lines (106 loc) · 2.63 KB
/
borrow.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
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
// Copyright 2019 The vogo Authors. All rights reserved.
// author: wongoo
package gracego
import (
"fmt"
"net"
"os"
"path/filepath"
"syscall"
"time"
)
func getBorrowSockFile(pid int) string {
return filepath.Join(os.TempDir(), fmt.Sprintf("gracego_borrow_%d.sock", pid))
}
func borrow(addr string) (*os.File, error) {
pid, err := getPidFromAddr(addr)
if err != nil {
return nil, err
}
procInfo, ok := checkSameBinProcess(pid)
if !ok {
return nil, fmt.Errorf("addr %s hold by process: %s", addr, procInfo)
}
graceLog("borrow addr %s from process[%d]: %s", addr, pid, procInfo)
return borrowFromPid(pid)
}
func borrowFromPid(pid int) (*os.File, error) {
proc, err := os.FindProcess(pid)
if err != nil {
return nil, fmt.Errorf("can't find target process %d, error: %+v", pid, err)
}
err = proc.Signal(syscall.SIGUSR1)
if err != nil {
return nil, fmt.Errorf("failed to send signal to process %d, error: %+v", pid, err)
}
borrowSockFile := getBorrowSockFile(pid)
ticker := time.NewTicker(time.Millisecond * 100)
for i := 0; i < 20 && !existFile(borrowSockFile); i++ {
<-ticker.C
}
ticker.Stop()
borrowConn, err := net.Dial("unix", borrowSockFile)
if err != nil {
return nil, err
}
defer borrowConn.Close()
go func() {
// wait 2 seconds (timeout control)
<-time.After(time.Second * 2)
borrowConn.Close()
}()
sendFdConn := borrowConn.(*net.UnixConn)
sockFile, err := sendFdConn.File()
if err != nil {
return nil, err
}
defer sockFile.Close()
buf := make([]byte, syscall.CmsgSpace(4))
if _, _, _, _, err = syscall.Recvmsg(int(sockFile.Fd()), nil, buf, 0); err != nil {
return nil, err
}
var messages []syscall.SocketControlMessage
messages, err = syscall.ParseSocketControlMessage(buf)
if err != nil {
return nil, err
}
fds, err := syscall.ParseUnixRights(&messages[0])
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fds[0]), ""), nil
}
func borrowSend() error {
listenFile, err := listenFile()
if err != nil {
return err
}
borrowSockFile := getBorrowSockFile(os.Getpid())
sockListener, err := net.Listen("unix", borrowSockFile)
if err != nil {
return err
}
go func() {
// wait 2 seconds (timeout control)
<-time.After(time.Second * 2)
sockListener.Close()
}()
sockConn, err := sockListener.Accept()
if err != nil {
return err
}
defer sockConn.Close()
conn := sockConn.(*net.UnixConn)
sockFile, err := conn.File()
if err != nil {
return err
}
defer sockFile.Close()
rights := syscall.UnixRights(int(listenFile.Fd()))
err = syscall.Sendmsg(int(sockFile.Fd()), nil, rights, nil, 0)
if err != nil {
return err
}
time.Sleep(time.Second)
return nil
}