-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain_linux.go
344 lines (284 loc) · 8.61 KB
/
main_linux.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
"unsafe"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/guardian/rundmc/execrunner/dadoo"
"github.com/eapache/go-resiliency/retrier"
"github.com/opencontainers/runc/libcontainer/system"
cmsg "github.com/opencontainers/runc/libcontainer/utils"
)
const MaxSocketDirPathLength = 80
func main() {
os.Exit(run())
}
func run() int {
tty := flag.Bool("tty", false, "tty requested")
socketDirPath := flag.String("socket-dir-path", "", "path to a dir in which to store console sockets")
runcRoot := flag.String("runc-root", "", "root directory for storage of container state")
flag.Parse()
runMode := flag.Args()[0] // exec or run
runtime := flag.Args()[1] // e.g. runc
processStateDir := flag.Args()[2]
containerId := flag.Args()[3]
signals := make(chan os.Signal, 100)
signal.Notify(signals, syscall.SIGCHLD)
runcExitCodePipe := os.NewFile(3, "/proc/self/fd/3")
logFile := fmt.Sprintf("/proc/%d/fd/4", os.Getpid())
logFD := os.NewFile(4, "/proc/self/fd/4")
syncPipe := os.NewFile(5, "/proc/self/fd/5")
pidFilePath := filepath.Join(processStateDir, "pidfile")
stdinR, stdoutW, stderrW, err := openStdioAndExitFifos(processStateDir)
defer closeFile(stdinR, stdoutW, stderrW)
if err != nil {
fmt.Println(err)
return 2
}
syncPipe.Write([]byte{0})
stdoutR, stderrR, err := openStdioKeepAlivePipes(processStateDir)
defer closeFile(stdoutR, stderrR)
if err != nil {
fmt.Println(err)
return 2
}
ioWg := &sync.WaitGroup{}
var runcCmd *exec.Cmd
if *tty {
winsz, err := openFile(filepath.Join(processStateDir, "winsz"), os.O_RDWR)
defer closeFile(winsz)
if err != nil {
fmt.Println(err)
return 2
}
if len(*socketDirPath) > MaxSocketDirPathLength {
return logAndExit(fmt.Sprintf("value for --socket-dir-path cannot exceed %d characters in length", MaxSocketDirPathLength))
}
ttySocketPath := setupTTYSocket(stdinR, stdoutW, winsz, pidFilePath, *socketDirPath, ioWg)
runcCmd = dadoo.BuildRuncCommand(runtime, runMode, *runcRoot, processStateDir, containerId, ttySocketPath, logFile)
} else {
runcCmd = dadoo.BuildRuncCommand(runtime, runMode, *runcRoot, processStateDir, containerId, "", logFile)
runcCmd.Stdin = stdinR
runcCmd.Stdout = stdoutW
runcCmd.Stderr = stderrW
}
// we need to be the subreaper so we can wait on the detached container process
system.SetSubreaper(os.Getpid())
if err := runcCmd.Start(); err != nil {
runcExitCodePipe.Write([]byte{2})
return 2
}
var status syscall.WaitStatus
var rusage syscall.Rusage
_, err = syscall.Wait4(runcCmd.Process.Pid, &status, 0, &rusage)
check(err) // Start succeeded but Wait4 failed, this can only be a programmer error
logFD.Close() // No more logs from runc so close fd
// also check that masterFD is received and streaming or whatevs
runcExitCodePipe.Write([]byte{byte(status.ExitStatus())})
if status.ExitStatus() != 0 {
return 3 // nothing to wait for, container didn't launch
}
containerPid, err := parsePid(pidFilePath)
check(err)
return waitForContainerToExit(processStateDir, containerPid, signals, ioWg)
}
// If gdn server process dies, we need dadoo to keep stdout/err reader
// FDs so that Linux does not SIGPIPE the user process if it tries to use its end of
// these pipes.
func openStdioKeepAlivePipes(processStateDir string) (io.ReadCloser, io.ReadCloser, error) {
keepStdoutAlive, err := openFile(filepath.Join(processStateDir, "stdout"), os.O_RDONLY)
if err != nil {
return nil, nil, err
}
keepStderrAlive, err := openFile(filepath.Join(processStateDir, "stderr"), os.O_RDONLY)
if err != nil {
return nil, nil, err
}
return keepStdoutAlive, keepStderrAlive, nil
}
func waitForContainerToExit(processStateDir string, containerPid int, signals chan os.Signal, ioWg *sync.WaitGroup) (exitCode int) {
for range signals {
for {
var status syscall.WaitStatus
var rusage syscall.Rusage
wpid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, &rusage)
if err != nil || wpid <= 0 {
break // wait for next SIGCHLD
}
if wpid == containerPid {
exitCode = status.ExitStatus()
if status.Signaled() {
exitCode = 128 + int(status.Signal())
}
ioWg.Wait() // wait for full output to be collected
check(ioutil.WriteFile(filepath.Join(processStateDir, "exitcode"), []byte(strconv.Itoa(exitCode)), 0600))
return exitCode
}
}
}
return logAndExit("ran out of signals") // cant happen
}
func openStdioAndExitFifos(processStateDir string) (io.ReadCloser, io.WriteCloser, io.WriteCloser, error) {
stdin, err := openFile(filepath.Join(processStateDir, "stdin"), os.O_RDONLY)
if err != nil {
return nil, nil, nil, err
}
stdout, err := openFile(filepath.Join(processStateDir, "stdout"), os.O_WRONLY)
if err != nil {
return nil, nil, nil, err
}
stderr, err := openFile(filepath.Join(processStateDir, "stderr"), os.O_WRONLY)
if err != nil {
return nil, nil, nil, err
}
// open just so guardian can detect it being closed when we exit
if _, err := openFile(filepath.Join(processStateDir, "exit"), os.O_RDWR); err != nil {
return nil, nil, nil, err
}
return stdin, stdout, stderr, nil
}
func openFile(path string, flags int) (*os.File, error) {
return os.OpenFile(path, flags, 0600)
}
func setupTTYSocket(stdin io.Reader, stdout io.Writer, winszFifo io.Reader, pidFilePath, sockDirBase string, ioWg *sync.WaitGroup) string {
sockDir, err := ioutil.TempDir(sockDirBase, "")
check(err)
ttySockPath := filepath.Join(sockDir, "tty.sock")
l, err := net.Listen("unix", ttySockPath)
check(err)
// go to the background and set master
go func(ln net.Listener) (err error) {
// if any of the following errors, it means runc has connected to the
// socket, so it must've started, thus we might need to kill the process
defer func() {
if err != nil {
killProcess(pidFilePath)
check(err)
}
}()
conn, err := ln.Accept()
if err != nil {
return
}
defer conn.Close()
// Close ln, to allow for other instances to take over.
ln.Close()
// Get the fd of the connection.
unixconn, ok := conn.(*net.UnixConn)
if !ok {
return
}
socket, err := unixconn.File()
if err != nil {
return
}
defer socket.Close()
// Get the master file descriptor from runC.
master, err := cmsg.RecvFd(socket)
if err != nil {
return
}
if err = os.RemoveAll(sockDir); err != nil {
return
}
if err = setOnlcr(master); err != nil {
return
}
streamProcess(master, stdin, stdout, winszFifo, ioWg)
return
}(l)
return ttySockPath
}
func streamProcess(m *os.File, stdin io.Reader, stdout io.Writer, winszFifo io.Reader, ioWg *sync.WaitGroup) {
ioWg.Add(1)
go func() {
defer ioWg.Done()
io.Copy(stdout, m)
}()
go io.Copy(m, stdin)
go func() {
for {
var winSize garden.WindowSize
if err := json.NewDecoder(winszFifo).Decode(&winSize); err != nil {
fmt.Printf("invalid winsz event: %s\n", err)
continue // not much we can do here..
}
dadoo.SetWinSize(m, winSize)
}
}()
}
func killProcess(pidFilePath string) {
pid, err := readPid(pidFilePath)
if err == nil {
syscall.Kill(pid, syscall.SIGKILL)
}
}
func readPid(pidFilePath string) (int, error) {
retrier := retrier.New(retrier.ConstantBackoff(20, 500*time.Millisecond), nil)
var (
pid = -1
err error
)
retrier.Run(func() error {
pid, err = parsePid(pidFilePath)
return err
})
return pid, err
}
func parsePid(pidFile string) (int, error) {
b, err := ioutil.ReadFile(pidFile)
if err != nil {
return -1, err
}
var pid int
if _, err := fmt.Sscanf(string(b), "%d", &pid); err != nil {
return -1, err
}
return pid, nil
}
func logAndExit(msg string) int {
fmt.Println(msg)
return 2
}
func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(2)
}
}
func closeFile(closers ...io.Closer) {
for _, closer := range closers {
closer.Close()
}
}
// setOnlcr copied from runc
// https://github.com/cloudfoundry-incubator/runc/blob/02ec89829b24dfce45bb207d2344e0e6d078a93c/libcontainer/console_linux.go#L144-L160
func setOnlcr(terminal *os.File) error {
var termios syscall.Termios
if err := ioctl(terminal.Fd(), syscall.TCGETS, uintptr(unsafe.Pointer(&termios))); err != nil {
return fmt.Errorf("ioctl(tty, tcgets): %s", err.Error())
}
termios.Oflag |= syscall.ONLCR
if err := ioctl(terminal.Fd(), syscall.TCSETS, uintptr(unsafe.Pointer(&termios))); err != nil {
return fmt.Errorf("ioctl(tty, tcsets): %s", err.Error())
}
return nil
}
func ioctl(fd uintptr, flag, data uintptr) error {
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 {
return err
}
return nil
}