blob: 9abc976c26495df0b6256b9304717abdd8fa68b9 [file] [log] [blame]
Lorenz Brunc88c82d2020-05-08 14:35:04 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package containerd
18
19import (
20 "context"
Lorenz Brun878f5f92020-05-12 16:15:39 +020021 "fmt"
Lorenz Brun6acfc322020-05-13 17:01:26 +020022 "io"
Lorenz Brunc88c82d2020-05-08 14:35:04 +020023 "os"
24 "os/exec"
Lorenz Brun6acfc322020-05-13 17:01:26 +020025 "time"
26
27 "git.monogon.dev/source/nexantic.git/core/internal/common/supervisor"
Lorenz Brunc88c82d2020-05-08 14:35:04 +020028
29 "git.monogon.dev/source/nexantic.git/core/pkg/logbuffer"
30
31 "golang.org/x/sys/unix"
32)
33
Lorenz Brun6acfc322020-05-13 17:01:26 +020034const runscLogsFIFOPath = "/containerd/run/runsc-logs.fifo"
35
Lorenz Brun878f5f92020-05-12 16:15:39 +020036type Service struct {
Lorenz Brun6acfc322020-05-13 17:01:26 +020037 Log *logbuffer.LogBuffer
38 RunscLog *logbuffer.LogBuffer
Lorenz Brun878f5f92020-05-12 16:15:39 +020039}
Lorenz Brunc88c82d2020-05-08 14:35:04 +020040
Lorenz Brun878f5f92020-05-12 16:15:39 +020041func New() (*Service, error) {
Lorenz Brun6acfc322020-05-13 17:01:26 +020042 return &Service{Log: logbuffer.New(5000, 16384), RunscLog: logbuffer.New(5000, 16384)}, nil
Lorenz Brun878f5f92020-05-12 16:15:39 +020043}
Lorenz Brunc88c82d2020-05-08 14:35:04 +020044
Lorenz Brun878f5f92020-05-12 16:15:39 +020045func (s *Service) Run() supervisor.Runnable {
46 return func(ctx context.Context) error {
47 cmd := exec.CommandContext(ctx, "/containerd/bin/containerd", "--config", "/containerd/conf/config.toml")
48 cmd.Stdout = s.Log
49 cmd.Stderr = s.Log
50 cmd.Env = []string{"PATH=/containerd/bin", "TMPDIR=/containerd/run/tmp"}
51
52 if err := unix.Mount("tmpfs", "/containerd/run", "tmpfs", 0, ""); err != nil {
53 panic(err)
54 }
55 if err := os.MkdirAll("/containerd/run/tmp", 0755); err != nil {
56 panic(err)
57 }
58
Lorenz Brun6acfc322020-05-13 17:01:26 +020059 runscFifo, err := os.OpenFile(runscLogsFIFOPath, os.O_CREATE|os.O_RDONLY, os.ModeNamedPipe|0777)
60 if err != nil {
61 return err
62 }
63 go func() {
64 for {
65 n, err := io.Copy(s.RunscLog, runscFifo)
66 if n == 0 && err == nil {
67 // Hack because pipes/FIFOs can return zero reads when nobody is writing. To avoid busy-looping,
68 // sleep a bit before retrying. This does not loose data since the FIFO internal buffer will
69 // stall writes when it becomes full. 10ms maximum stall in a non-latency critical process (reading
70 // debug logs) is not an issue for us.
71 time.Sleep(10 * time.Millisecond)
72 } else if err != nil {
73 // TODO: Use supervisor.Logger() and Error() before exiting. Should never happen.
74 fmt.Println(err)
75 return // It's likely that this will busy-loop printing errors if it encounters one, so bail
76 }
77 }
78 }()
79
Lorenz Brun878f5f92020-05-12 16:15:39 +020080 // TODO(lorenz): Healthcheck against CRI RuntimeService.Status() and SignalHealthy
81
Lorenz Brun6acfc322020-05-13 17:01:26 +020082 err = cmd.Run()
Lorenz Brun878f5f92020-05-12 16:15:39 +020083 fmt.Fprintf(s.Log, "containerd stopped: %v\n", err)
84 return err
Lorenz Brunc88c82d2020-05-08 14:35:04 +020085 }
Lorenz Brunc88c82d2020-05-08 14:35:04 +020086}