forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (130 loc) · 3.95 KB
/
main.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
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"flag"
"fmt"
"io/ioutil"
"log/syslog"
"os"
"os/signal"
"sync"
"time"
"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
)
const (
shimName = "kata-shim"
exitFailure = 1
)
// version is the shim version. This variable is populated at build time.
var version = "unknown"
// if true, coredump when an internal error occurs or a fatal signal is received
var crashOnError = false
var shimLog *logrus.Entry
func logger() *logrus.Entry {
return shimLog
}
func initLogger(logLevel, container, execID string) error {
shimLog = logrus.WithFields(logrus.Fields{
"name": shimName,
"pid": os.Getpid(),
"source": "shim",
"container": container,
"exec-id": execID,
})
shimLog.Logger.Formatter = &logrus.TextFormatter{TimestampFormat: time.RFC3339Nano}
level, err := logrus.ParseLevel(logLevel)
if err != nil {
return err
}
shimLog.Logger.SetLevel(level)
// Make sure all output going to stdout/stderr is actually discarded.
shimLog.Logger.Out = ioutil.Discard
hook, err := lSyslog.NewSyslogHook("", "", syslog.LOG_INFO|syslog.LOG_USER, shimName)
if err == nil {
shimLog.Logger.AddHook(hook)
}
logger().WithField("version", version).Info()
return nil
}
func realMain() {
var (
logLevel string
agentAddr string
container string
execID string
terminal bool
proxyExitCode bool
showVersion bool
debug bool
)
flag.BoolVar(&debug, "debug", false, "enable debug mode")
flag.BoolVar(&showVersion, "version", false, "display program version and exit")
flag.StringVar(&logLevel, "log", "warn", "set shim log level: debug, info, warn, error, fatal or panic")
flag.StringVar(&agentAddr, "agent", "", "agent gRPC socket endpoint")
flag.StringVar(&container, "container", "", "container id for the shim")
flag.StringVar(&execID, "exec-id", "", "process id for the shim")
flag.BoolVar(&terminal, "terminal", false, "specify if a terminal is setup")
flag.BoolVar(&proxyExitCode, "proxy-exit-code", true, "proxy exit code of the process")
flag.Parse()
if showVersion {
fmt.Printf("%v version %v\n", shimName, version)
os.Exit(0)
}
if debug {
crashOnError = true
}
if agentAddr == "" || container == "" || execID == "" {
logger().WithField("agentAddr", agentAddr).WithField("container", container).WithField("exec-id", execID).Error("container ID, exec ID and agent socket endpoint must be set")
os.Exit(exitFailure)
}
err := initLogger(logLevel, container, execID)
if err != nil {
logger().WithError(err).WithField("loglevel", logLevel).Error("invalid log level")
os.Exit(exitFailure)
}
shim, err := newShim(agentAddr, container, execID)
if err != nil {
logger().WithError(err).Error("failed to create new shim")
os.Exit(exitFailure)
}
// winsize
if terminal {
termios, err := setupTerminal(int(os.Stdin.Fd()))
if err != nil {
logger().WithError(err).Error("failed to set raw terminal")
os.Exit(exitFailure)
}
defer restoreTerminal(int(os.Stdin.Fd()), termios)
}
shim.monitorTtySize(os.Stdin)
// signals
sigc := shim.forwardAllSignals()
defer signal.Stop(sigc)
// This wait call cannot be deferred and has to wait for every
// input/output to return before the code tries to go further
// and wait for the process. Indeed, after the process has been
// waited for, we cannot expect to do any more calls related to
// this process since it is going to be removed from the agent.
wg := &sync.WaitGroup{}
shim.proxyStdio(wg, terminal)
wg.Wait()
// wait until exit
exitcode, err := shim.wait()
if err != nil {
logger().WithError(err).WithField("exec-id", execID).Error("failed waiting for process")
os.Exit(exitFailure)
} else if proxyExitCode {
logger().WithField("exitcode", exitcode).Info("using shim to proxy exit code")
if exitcode != 0 {
os.Exit(int(exitcode))
}
}
}
func main() {
defer handlePanic()
realMain()
}