This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
shim.go
282 lines (239 loc) · 5.8 KB
/
shim.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
//
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package virtcontainers
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
ns "github.com/containers/virtcontainers/pkg/nsenter"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
)
// ShimType describes a shim type.
type ShimType string
const (
// CCShimType is the ccShim.
CCShimType ShimType = "ccShim"
// NoopShimType is the noopShim.
NoopShimType ShimType = "noopShim"
// KataShimType is the Kata Containers shim type.
KataShimType ShimType = "kataShim"
)
var waitForShimTimeout = 10.0
var consoleFileMode = os.FileMode(0660)
// ShimParams is the structure providing specific parameters needed
// for the execution of the shim binary.
type ShimParams struct {
Container string
Token string
URL string
Console string
Terminal bool
Detach bool
PID int
CreateNS []ns.NSType
EnterNS []ns.Namespace
}
// ShimConfig is the structure providing specific configuration
// for shim implementations.
type ShimConfig struct {
Path string
Debug bool
}
// Set sets a shim type based on the input string.
func (pType *ShimType) Set(value string) error {
switch value {
case "noopShim":
*pType = NoopShimType
return nil
case "ccShim":
*pType = CCShimType
return nil
case "kataShim":
*pType = KataShimType
return nil
default:
return fmt.Errorf("Unknown shim type %s", value)
}
}
// String converts a shim type to a string.
func (pType *ShimType) String() string {
switch *pType {
case NoopShimType:
return string(NoopShimType)
case CCShimType:
return string(CCShimType)
case KataShimType:
return string(KataShimType)
default:
return ""
}
}
// newShim returns a shim from a shim type.
func newShim(pType ShimType) (shim, error) {
switch pType {
case NoopShimType:
return &noopShim{}, nil
case CCShimType:
return &ccShim{}, nil
case KataShimType:
return &kataShim{}, nil
default:
return &noopShim{}, nil
}
}
// newShimConfig returns a shim config from a generic PodConfig interface.
func newShimConfig(config PodConfig) interface{} {
switch config.ShimType {
case NoopShimType:
return nil
case CCShimType, KataShimType:
var shimConfig ShimConfig
err := mapstructure.Decode(config.ShimConfig, &shimConfig)
if err != nil {
return err
}
return shimConfig
default:
return nil
}
}
func shimLogger() *logrus.Entry {
return virtLog.WithField("subsystem", "shim")
}
func signalShim(pid int, sig syscall.Signal) error {
if pid <= 0 {
return nil
}
shimLogger().WithFields(
logrus.Fields{
"shim-pid": pid,
"shim-signal": sig,
}).Info("Signalling shim")
return syscall.Kill(pid, sig)
}
func stopShim(pid int) error {
if err := signalShim(pid, syscall.SIGKILL); err != nil && err != syscall.ESRCH {
return err
}
return nil
}
func prepareAndStartShim(pod *Pod, shim shim, cid, token, url string, cmd Cmd,
createNSList []ns.NSType, enterNSList []ns.Namespace) (*Process, error) {
process := &Process{
Token: token,
StartTime: time.Now().UTC(),
}
shimParams := ShimParams{
Container: cid,
Token: token,
URL: url,
Console: cmd.Console,
Terminal: cmd.Interactive,
Detach: cmd.Detach,
CreateNS: createNSList,
EnterNS: enterNSList,
}
pid, err := shim.start(*pod, shimParams)
if err != nil {
return nil, err
}
process.Pid = pid
return process, nil
}
func startShim(args []string, params ShimParams) (int, error) {
cmd := exec.Command(args[0], args[1:]...)
if !params.Detach {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
cloneFlags := 0
for _, nsType := range params.CreateNS {
cloneFlags |= ns.CloneFlagsTable[nsType]
}
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: uintptr(cloneFlags),
}
var f *os.File
var err error
if params.Console != "" {
f, err = os.OpenFile(params.Console, os.O_RDWR, consoleFileMode)
if err != nil {
return -1, err
}
cmd.Stdin = f
cmd.Stdout = f
cmd.Stderr = f
// Create Session
cmd.SysProcAttr.Setsid = true
// Set Controlling terminal to Ctty
cmd.SysProcAttr.Setctty = true
cmd.SysProcAttr.Ctty = int(f.Fd())
}
defer func() {
if f != nil {
f.Close()
}
}()
if err := ns.NsEnter(params.EnterNS, func() error {
return cmd.Start()
}); err != nil {
return -1, err
}
return cmd.Process.Pid, nil
}
func isShimRunning(pid int) (bool, error) {
process, err := os.FindProcess(pid)
if err != nil {
return false, err
}
if err := process.Signal(syscall.Signal(0)); err != nil {
return false, nil
}
return true, nil
}
// waitForShim waits for the end of the shim unless it reaches the timeout
// first, returning an error in that case.
func waitForShim(pid int) error {
if pid <= 0 {
return nil
}
tInit := time.Now()
for {
running, err := isShimRunning(pid)
if err != nil {
return err
}
if !running {
break
}
if time.Since(tInit).Seconds() >= waitForShimTimeout {
return fmt.Errorf("Shim still running, timeout %f s has been reached", waitForShimTimeout)
}
// Let's avoid to run a too busy loop
time.Sleep(time.Duration(100) * time.Millisecond)
}
return nil
}
// shim is the virtcontainers shim interface.
type shim interface {
// start starts the shim relying on its configuration and on
// parameters provided.
start(pod Pod, params ShimParams) (int, error)
}