-
Notifications
You must be signed in to change notification settings - Fork 1k
/
oci_unix.go
73 lines (59 loc) · 1.58 KB
/
oci_unix.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
//go:build !windows
// +build !windows
package oci
import (
"fmt"
"io"
"os/exec"
"github.com/containers/storage/pkg/pools"
"github.com/creack/pty"
"github.com/cri-o/cri-o/utils"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"k8s.io/client-go/tools/remotecommand"
)
func Kill(pid int) error {
err := unix.Kill(pid, unix.SIGKILL)
if err != nil && err != unix.ESRCH {
return fmt.Errorf("failed to kill process: %w", err)
}
return nil
}
func setSize(fd uintptr, size remotecommand.TerminalSize) error {
winsize := &unix.Winsize{Row: size.Height, Col: size.Width}
return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, winsize)
}
func ttyCmd(execCmd *exec.Cmd, stdin io.Reader, stdout io.WriteCloser, resizeChan <-chan remotecommand.TerminalSize, c *Container) error {
p, err := pty.Start(execCmd)
if err != nil {
return err
}
defer p.Close()
// make sure to close the stdout stream
defer stdout.Close()
pid := execCmd.Process.Pid
if err := c.AddExecPID(pid, true); err != nil {
return err
}
defer c.DeleteExecPID(pid)
utils.HandleResizing(resizeChan, func(size remotecommand.TerminalSize) {
if err := setSize(p.Fd(), size); err != nil {
logrus.Warnf("Unable to set terminal size: %v", err)
}
})
var stdinErr, stdoutErr error
if stdin != nil {
go func() { _, stdinErr = pools.Copy(p, stdin) }()
}
if stdout != nil {
go func() { _, stdoutErr = pools.Copy(stdout, p) }()
}
err = execCmd.Wait()
if stdinErr != nil {
logrus.Warnf("Stdin copy error: %v", stdinErr)
}
if stdoutErr != nil {
logrus.Warnf("Stdout copy error: %v", stdoutErr)
}
return err
}