-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathzeusclient.go
262 lines (228 loc) · 5.95 KB
/
zeusclient.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package zeusclient
import (
"io"
"net"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"github.com/burke/ttyutils"
"github.com/burke/zeus/go/messages"
slog "github.com/burke/zeus/go/shinylog"
"github.com/burke/zeus/go/unixsocket"
"github.com/burke/zeus/go/zerror"
"github.com/kr/pty"
)
const (
sigInt = 3
sigQuit = 28
sigTstp = 26
)
// man signal | grep 'terminate process' | awk '{print $2}' | xargs -I '{}' echo -n "syscall.{}, "
var terminatingSignals = []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGKILL, syscall.SIGPIPE, syscall.SIGALRM, syscall.SIGTERM, syscall.SIGXCPU, syscall.SIGXFSZ, syscall.SIGVTALRM, syscall.SIGPROF, syscall.SIGUSR1, syscall.SIGUSR2}
// Run runs the main Zeus command.
func Run(args []string, input io.Reader, output *os.File, stderr *os.File) int {
if os.Getenv("RAILS_ENV") != "" {
println("Warning: Specifying a Rails environment via RAILS_ENV has no effect for commands run with zeus.")
println("As a safety precaution to protect you from nuking your development database,")
println("Zeus will now cowardly refuse to proceed. Please unset RAILS_ENV and try again.")
return 1
}
// setup stdout
localStdout, remoteStdout, outputIsTerminal, err := socketsForOutput(output)
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
defer localStdout.Close()
defer remoteStdout.Close()
// setup stderr
localStderr, remoteStderr, _, err := socketsForOutput(stderr)
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
defer localStderr.Close()
defer remoteStderr.Close()
addr, err := net.ResolveUnixAddr("unixgram", unixsocket.ZeusSockName())
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
conn, err := net.DialUnix("unix", nil, addr)
if err != nil {
zerror.ErrorCantConnectToMaster()
return 1
}
usock := unixsocket.New(conn)
msg := messages.CreateCommandAndArgumentsMessage(args, os.Getpid())
usock.WriteMessage(msg)
err = sendCommandLineArguments(usock, args)
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
usock.WriteFD(int(remoteStdout.Fd()))
usock.WriteFD(int(remoteStderr.Fd()))
msg, err = usock.ReadMessage()
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
parts := strings.Split(msg, "\000")
commandPid, err := strconv.Atoi(parts[0])
defer func() {
if commandPid > 0 {
// Just in case.
syscall.Kill(commandPid, 9)
}
}()
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
if outputIsTerminal {
c := make(chan os.Signal, 1)
handledSignals := append(append(terminatingSignals, syscall.SIGWINCH), syscall.SIGCONT)
signal.Notify(c, handledSignals...)
go func() {
for sig := range c {
if sig == syscall.SIGCONT {
syscall.Kill(commandPid, syscall.SIGCONT)
} else if sig == syscall.SIGWINCH {
ttyutils.MirrorWinsize(output, localStdout)
syscall.Kill(commandPid, syscall.SIGWINCH)
} else { // member of terminatingSignals
print("\r")
syscall.Kill(commandPid, sig.(syscall.Signal))
os.Exit(1)
}
}
}()
}
exitStatus := -1
if len(parts) > 2 {
exitStatus, err = strconv.Atoi(parts[0])
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
}
var endOfIO sync.WaitGroup
oldTerminalStateStdout, err := forwardOutput(localStdout, output, endOfIO)
if oldTerminalStateStdout != nil {
defer ttyutils.RestoreTerminalState(output.Fd(), oldTerminalStateStdout)
}
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
oldTerminalStateStderr, err := forwardOutput(localStderr, stderr, endOfIO)
if oldTerminalStateStderr != nil {
defer ttyutils.RestoreTerminalState(stderr.Fd(), oldTerminalStateStderr)
}
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
go func() {
endOfIO.Add(1)
buf := make([]byte, 8192)
for {
n, err := input.Read(buf)
if err != nil {
endOfIO.Done()
break
}
if outputIsTerminal {
for i := 0; i < n; i++ {
switch buf[i] {
case sigInt:
syscall.Kill(commandPid, syscall.SIGINT)
case sigQuit:
syscall.Kill(commandPid, syscall.SIGQUIT)
case sigTstp:
syscall.Kill(commandPid, syscall.SIGTSTP)
syscall.Kill(os.Getpid(), syscall.SIGTSTP)
}
}
}
localStdout.Write(buf[:n])
}
}()
endOfIO.Wait()
if exitStatus == -1 {
msg, err = usock.ReadMessage()
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
parts := strings.Split(msg, "\000")
exitStatus, err = strconv.Atoi(parts[0])
if err != nil {
slog.ErrorString(err.Error() + "\r")
return 1
}
}
return exitStatus
}
func sendCommandLineArguments(usock *unixsocket.Usock, args []string) error {
master, slave, err := unixsocket.Socketpair(syscall.SOCK_STREAM)
if err != nil {
return err
}
usock.WriteFD(int(slave.Fd()))
if err != nil {
return err
}
slave.Close()
go func() {
defer master.Close()
argAsBytes := []byte{}
for _, arg := range args[1:] {
argAsBytes = append(argAsBytes, []byte(arg)...)
argAsBytes = append(argAsBytes, byte(0))
}
_, err = master.Write(argAsBytes)
if err != nil {
slog.ErrorString("Could not send arguments across: " +
err.Error() + "\r")
return
}
}()
return nil
}
func socketsForOutput(out *os.File) (local, remote *os.File, outIsTerminal bool, err error) {
outIsTerminal = ttyutils.IsTerminal(out.Fd())
if outIsTerminal {
local, remote, err = pty.Open()
} else {
local, remote, err = unixsocket.Socketpair(syscall.SOCK_STREAM)
}
return
}
func forwardOutput(from, to *os.File, signalEnd sync.WaitGroup) (oldTerminalState *ttyutils.Termios, err error) {
ttyutils.MirrorWinsize(to, from)
if ttyutils.IsTerminal(to.Fd()) {
oldTerminalState, err = ttyutils.MakeTerminalRaw(to.Fd())
if err != nil {
return
}
}
go func() {
signalEnd.Add(1)
for {
buf := make([]byte, 1024)
n, err := from.Read(buf)
if err == nil || (err == io.EOF && n > 0) {
to.Write(buf[:n])
} else {
signalEnd.Done()
break
}
}
}()
return
}