-
Notifications
You must be signed in to change notification settings - Fork 11
/
init_process.go
533 lines (450 loc) · 12.8 KB
/
init_process.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
Copyright The containerd Authors.
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 embedshim
import (
"context"
"encoding/json"
"fmt"
"io"
"path/filepath"
"strings"
"sync"
"time"
pkgbundle "github.com/fuweid/embedshim/pkg/bundle"
"github.com/containerd/console"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/pkg/stdio"
"github.com/containerd/containerd/runtime"
"github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/fifo"
"github.com/containerd/go-runc"
google_protobuf "github.com/gogo/protobuf/types"
"github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
)
type initProcess struct {
parent *shim
initState initState
bundle *pkgbundle.Bundle
runtime *runc.Runc
options *options.Options
traceEventID uint64
wg sync.WaitGroup
waitBlock chan struct{}
stdio stdio.Stdio
console console.Console
platform stdio.Platform // TODO: as shim level instead of initProcess
io *processIO
stdin io.Closer
closers []io.Closer
mu sync.Mutex
status int
exited time.Time
pid int
}
func newInitProcess(bundle *pkgbundle.Bundle) (_ *initProcess, retErr error) {
opts, err := readInitOptions(bundle)
if err != nil {
return nil, err
}
initIO, err := readInitStdio(bundle)
if err != nil {
return nil, err
}
eventID, err := readInitTraceEventID(bundle)
if err != nil {
return nil, err
}
platform, err := NewPlatform()
if err != nil {
return nil, err
}
defer func() {
if retErr != nil {
platform.Close()
}
}()
runtime := newRuncRuntime(
opts.Root, // for working dir
filepath.Join(bundle.Path, "work"), // for log.json
bundle.Namespace, // for isolation
opts.BinaryName, // other implementation, like crun, youki
"",
opts.SystemdCgroup, // use systemd's cgroup
)
p := &initProcess{
bundle: bundle,
options: opts,
traceEventID: eventID,
runtime: runtime,
stdio: stdio.Stdio{
Stdin: initIO.Stdin,
Stdout: initIO.Stdout,
Stderr: initIO.Stderr,
Terminal: initIO.Terminal,
},
status: 0,
platform: platform,
waitBlock: make(chan struct{}),
}
p.initState = &createdState{p: p}
return p, nil
}
func (p *initProcess) Create(ctx context.Context) (retErr error) {
var (
err error
socket *runc.Socket
pio *processIO
pidFile = newInitPidFile(p.bundle)
ioUID = int(p.options.IoUid)
ioGID = int(p.options.IoGid)
)
// TODO(fuweid):
//
// Terminal console poller should be shared in plugin Level.
if p.stdio.Terminal {
if socket, err = runc.NewTempConsoleSocket(); err != nil {
return fmt.Errorf("failed to create OCI runtime console socket: %w", err)
}
defer socket.Close()
} else {
if pio, err = createIO(ctx, p.ID(), ioUID, ioGID, p.stdio); err != nil {
return fmt.Errorf("failed to create init process I/O: %w", err)
}
p.io = pio
}
opts := &runc.CreateOpts{
PidFile: pidFile.Path(),
NoPivot: p.options.NoPivotRoot,
NoNewKeyring: p.options.NoNewKeyring,
}
if p.io != nil {
opts.IO = p.io.IO()
}
if socket != nil {
opts.ConsoleSocket = socket
}
if err := p.runtime.Create(ctx, p.ID(), p.bundle.Path, opts); err != nil {
return p.runtimeError(err, "OCI runtime create failed")
}
if p.stdio.Stdin != "" {
if err := p.openStdin(p.stdio.Stdin); err != nil {
return err
}
}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if socket != nil {
console, err := socket.ReceiveMaster()
if err != nil {
return fmt.Errorf("failed to retrieve console master: %w", err)
}
console, err = p.platform.CopyConsole(ctx, console, p.ID(), p.stdio.Stdin, p.stdio.Stdout, p.stdio.Stderr, &p.wg)
if err != nil {
return fmt.Errorf("failed to start console copy: %w", err)
}
p.console = console
} else {
// NOTE: There is no stdout/stderr copy because we open Read-Write
// fifo as init process's stdout/stderr. Unlike the shim server's
// pipe, the containerd restarts without closing the init process
// stdout/stderr so that it is easy to recover.
//
// But the stdin still needs pipe as relay because we need to
// notify the init process that the stdin has been closed, like
//
// echo "hello, world" | cat
//
// So, for the init process which needs stdin, we can't recover
// the stdin after containerd restart, A.K.A we can't re-attach
// to stdin.
//
// This embedshim plugin can't cover 100% cases from shim server,
// but in producation, most of workloads are headless. The stdin
// is used to debug or exec operations job. I think it is acceptable :P.
if err := pio.CopyStdin(); err != nil {
return fmt.Errorf("failed to start io pipe copy: %w", err)
}
}
pid, err := pidFile.Read()
if err != nil {
return fmt.Errorf("failed to retrieve OCI runtime container pid: %w", err)
}
p.pid = pid
return nil
}
func (p *initProcess) openStdin(path string) error {
sc, err := fifo.OpenFifo(context.Background(), path, unix.O_WRONLY|unix.O_NONBLOCK, 0)
if err != nil {
return fmt.Errorf("failed to open stdin fifo %s: %w", path, err)
}
p.stdin = sc
p.closers = append(p.closers, sc)
return nil
}
// Wait for the process to exit
func (p *initProcess) Wait() {
<-p.waitBlock
}
// ID of the process
func (p *initProcess) ID() string {
return p.bundle.ID
}
// Pid of the process
func (p *initProcess) Pid() int {
return p.pid
}
// exitStatus of the process
func (p *initProcess) ExitStatus() int {
p.mu.Lock()
defer p.mu.Unlock()
return p.status
}
// ExitedAt at time when the process exited
func (p *initProcess) ExitedAt() time.Time {
p.mu.Lock()
defer p.mu.Unlock()
return p.exited
}
// Status of the process
func (p *initProcess) Status(ctx context.Context) (string, error) {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Status(ctx)
}
// Start the init process
func (p *initProcess) Start(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Start(ctx)
}
func (p *initProcess) start(ctx context.Context) error {
err := p.runtime.Start(ctx, p.ID())
return p.runtimeError(err, "OCI runtime start failed")
}
// SetExited of the init process with the next status
func (p *initProcess) SetExited(status int) {
p.mu.Lock()
defer p.mu.Unlock()
p.initState.SetExited(status)
}
func (p *initProcess) setExited(status int) {
p.exited = time.Now()
p.status = unix.WaitStatus(status).ExitStatus()
if p.platform != nil {
p.platform.ShutdownConsole(context.Background(), p.console)
p.platform.Close()
p.platform = nil
}
close(p.waitBlock)
}
// Delete the init process
func (p *initProcess) Delete(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Delete(ctx)
}
func (p *initProcess) delete(ctx context.Context) error {
waitTimeout(ctx, &p.wg, 2*time.Second)
err := p.runtime.Delete(ctx, p.ID(), nil)
// ignore errors if a runtime has already deleted the process
// but we still hold metadata and pipes
//
// this is common during a checkpoint, runc will delete the container state
// after a checkpoint and the container will no longer exist within runc
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
err = nil
} else {
err = p.runtimeError(err, "failed to delete task")
}
}
if p.io != nil {
for _, c := range p.closers {
c.Close()
}
p.io.Close()
}
rootfs := p.bundle.Rootfs()
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
log.G(ctx).WithError(err2).Warn("failed to cleanup rootfs mount")
if err == nil {
err = fmt.Errorf("failed rootfs umount: %w", err)
}
}
return err
}
// Resize the init processes console
func (p *initProcess) Resize(ws console.WinSize) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.console == nil {
return nil
}
return p.console.Resize(ws)
}
// Pause the init process and all its child processes
func (p *initProcess) Pause(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Pause(ctx)
}
func (p *initProcess) pause(ctx context.Context) error {
err := p.runtime.Pause(ctx, p.ID())
return p.runtimeError(err, "OCI runtime pause failed")
}
// Resume the init process and all its child processes
func (p *initProcess) Resume(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Resume(ctx)
}
func (p *initProcess) resume(ctx context.Context) error {
err := p.runtime.Resume(ctx, p.ID())
return p.runtimeError(err, "OCI runtime resume failed")
}
// Kill the init process
func (p *initProcess) Kill(ctx context.Context, signal uint32, all bool) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Kill(ctx, signal, all)
}
func (p *initProcess) kill(ctx context.Context, signal uint32, all bool) error {
err := p.runtime.Kill(ctx, p.ID(), int(signal), &runc.KillOpts{
All: all,
})
return checkKillError(err)
}
// KillAll processes belonging to the init process
func (p *initProcess) KillAll(ctx context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
err := p.runtime.Kill(ctx, p.ID(), int(unix.SIGKILL), &runc.KillOpts{
All: true,
})
return p.runtimeError(err, "OCI runtime killall failed")
}
// Stdin of the process
func (p *initProcess) Stdin() io.Closer {
return p.stdin
}
// Runtime returns the OCI runtime configured for the init process
func (p *initProcess) Runtime() *runc.Runc {
return p.runtime
}
// Exec returns a new child process
func (p *initProcess) Exec(ctx context.Context, execID string, opts runtime.ExecOpts) (runtime.Process, error) {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Exec(ctx, execID, opts)
}
func (p *initProcess) exec(ctx context.Context, execID string, opts runtime.ExecOpts) (runtime.Process, error) {
traceID := traceIDFromContext(ctx)
// process exec request
var spec specs.Process
if err := json.Unmarshal(opts.Spec.Value, &spec); err != nil {
return nil, err
}
spec.Terminal = opts.IO.Terminal
e := &execProcess{
parent: p,
id: execID,
traceEventID: traceID,
spec: spec,
stdio: stdio.Stdio{
Stdin: opts.IO.Stdin,
Stdout: opts.IO.Stdout,
Stderr: opts.IO.Stderr,
Terminal: opts.IO.Terminal,
},
waitBlock: make(chan struct{}),
}
e.execState = &execCreatedState{p: e}
return e, nil
}
// Checkpoint the init process
func (p *initProcess) Checkpoint(_ context.Context, _ *CheckpointConfig) error {
return fmt.Errorf("checkpoint not implemented yet")
}
// Update the processes resource configuration
func (p *initProcess) Update(ctx context.Context, r *google_protobuf.Any) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.initState.Update(ctx, r)
}
func (p *initProcess) update(ctx context.Context, r *google_protobuf.Any) error {
var resources specs.LinuxResources
if err := json.Unmarshal(r.Value, &resources); err != nil {
return err
}
return p.runtime.Update(ctx, p.ID(), &resources)
}
// Stdio of the process
func (p *initProcess) Stdio() stdio.Stdio {
return p.stdio
}
func (p *initProcess) runtimeError(rErr error, msg string) error {
if rErr == nil {
return nil
}
rMsg, err := getLastRuntimeError(p.runtime)
switch {
case err != nil:
return fmt.Errorf("%s: %s (%s)", msg, "unable to retrieve OCI runtime error", err.Error())
case rMsg == "":
return rErr
default:
return fmt.Errorf("%s: %s", msg, rMsg)
}
}
func (p *initProcess) String() string {
return fmt.Sprintf("init process(id=%v, namespace=%v)", p.ID(), p.bundle.Namespace)
}
// waitTimeout handles waiting on a waitgroup with a specified timeout.
// this is commonly used for waiting on IO to finish after a process has exited
func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (p *initProcess) reload(ctx context.Context) error {
pid, err := newInitPidFile(p.bundle).Read()
if err != nil {
return fmt.Errorf("failed to read container pidfile: %w", err)
}
p.pid = pid
c, err := p.runtime.State(ctx, p.ID())
if err != nil {
return fmt.Errorf("failed to read container state: %w", err)
}
switch c.Status {
case "running":
p.initState = &runningState{p: p}
case "stopped":
p.initState = &stoppedState{p: p}
case "paused":
p.initState = &pausedState{p: p}
}
return nil
}