| 1 |
//go:build linux |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright The containerd Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package runc |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"net/url" |
| 27 |
"os" |
| 28 |
"sync" |
| 29 |
"syscall" |
| 30 |
|
| 31 |
"github.com/containerd/console" |
| 32 |
"github.com/containerd/containerd/v2/cmd/containerd-shim-runc-v2/process" |
| 33 |
"github.com/containerd/containerd/v2/pkg/namespaces" |
| 34 |
"github.com/containerd/containerd/v2/pkg/stdio" |
| 35 |
"github.com/containerd/fifo" |
| 36 |
) |
| 37 |
|
| 38 |
var bufPool = sync.Pool{ |
| 39 |
New: func() interface{} { |
| 40 |
// setting to 4096 to align with PIPE_BUF |
| 41 |
// http://man7.org/linux/man-pages/man7/pipe.7.html |
| 42 |
buffer := make([]byte, 4096) |
| 43 |
return &buffer |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
// NewPlatform returns a linux platform for use with I/O operations |
| 48 |
func NewPlatform() (stdio.Platform, error) { |
| 49 |
epoller, err := console.NewEpoller() |
| 50 |
if err != nil { |
| 51 |
return nil, fmt.Errorf("failed to initialize epoller: %w", err) |
| 52 |
} |
| 53 |
go epoller.Wait() |
| 54 |
return &linuxPlatform{ |
| 55 |
epoller: epoller, |
| 56 |
}, nil |
| 57 |
} |
| 58 |
|
| 59 |
type linuxPlatform struct { |
| 60 |
epoller *console.Epoller |
| 61 |
} |
| 62 |
|
| 63 |
func (p *linuxPlatform) CopyConsole(ctx context.Context, console console.Console, id, stdin, stdout, stderr string, wg *sync.WaitGroup) (cons console.Console, retErr error) { |
| 64 |
if p.epoller == nil { |
| 65 |
return nil, errors.New("uninitialized epoller") |
| 66 |
} |
| 67 |
|
| 68 |
epollConsole, err := p.epoller.Add(console) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
|
| 73 |
var cwg sync.WaitGroup |
| 74 |
if stdin != "" { |
| 75 |
in, err := fifo.OpenFifo(context.Background(), stdin, syscall.O_RDONLY|syscall.O_NONBLOCK, 0) |
| 76 |
if err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
cwg.Add(1) |
| 80 |
go func() { |
| 81 |
cwg.Done() |
| 82 |
bp := bufPool.Get().(*[]byte) |
| 83 |
defer bufPool.Put(bp) |
| 84 |
io.CopyBuffer(epollConsole, in, *bp) |
| 85 |
// we need to shutdown epollConsole when pipe broken |
| 86 |
epollConsole.Shutdown(p.epoller.CloseConsole) |
| 87 |
epollConsole.Close() |
| 88 |
in.Close() |
| 89 |
}() |
| 90 |
} |
| 91 |
|
| 92 |
uri, err := url.Parse(stdout) |
| 93 |
if err != nil { |
| 94 |
return nil, fmt.Errorf("unable to parse stdout uri: %w", err) |
| 95 |
} |
| 96 |
|
| 97 |
switch uri.Scheme { |
| 98 |
case "binary": |
| 99 |
ns, err := namespaces.NamespaceRequired(ctx) |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
cmd := process.NewBinaryCmd(uri, id, ns) |
| 105 |
|
| 106 |
// In case of unexpected errors during logging binary start, close open pipes |
| 107 |
var filesToClose []*os.File |
| 108 |
|
| 109 |
defer func() { |
| 110 |
if retErr != nil { |
| 111 |
process.CloseFiles(filesToClose...) |
| 112 |
} |
| 113 |
}() |
| 114 |
|
| 115 |
// Create pipe to be used by logging binary for Stdout |
| 116 |
outR, outW, err := os.Pipe() |
| 117 |
if err != nil { |
| 118 |
return nil, fmt.Errorf("failed to create stdout pipes: %w", err) |
| 119 |
} |
| 120 |
filesToClose = append(filesToClose, outR) |
| 121 |
|
| 122 |
// Stderr is created for logging binary but unused when terminal is true |
| 123 |
serrR, _, err := os.Pipe() |
| 124 |
if err != nil { |
| 125 |
return nil, fmt.Errorf("failed to create stderr pipes: %w", err) |
| 126 |
} |
| 127 |
filesToClose = append(filesToClose, serrR) |
| 128 |
|
| 129 |
r, w, err := os.Pipe() |
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
filesToClose = append(filesToClose, r) |
| 134 |
|
| 135 |
cmd.ExtraFiles = append(cmd.ExtraFiles, outR, serrR, w) |
| 136 |
|
| 137 |
wg.Add(1) |
| 138 |
cwg.Add(1) |
| 139 |
go func() { |
| 140 |
cwg.Done() |
| 141 |
io.Copy(outW, epollConsole) |
| 142 |
outW.Close() |
| 143 |
wg.Done() |
| 144 |
}() |
| 145 |
|
| 146 |
if err := cmd.Start(); err != nil { |
| 147 |
return nil, fmt.Errorf("failed to start logging binary process: %w", err) |
| 148 |
} |
| 149 |
|
| 150 |
// Close our side of the pipe after start |
| 151 |
if err := w.Close(); err != nil { |
| 152 |
return nil, fmt.Errorf("failed to close write pipe after start: %w", err) |
| 153 |
} |
| 154 |
|
| 155 |
// Wait for the logging binary to be ready |
| 156 |
b := make([]byte, 1) |
| 157 |
if _, err := r.Read(b); err != nil && err != io.EOF { |
| 158 |
return nil, fmt.Errorf("failed to read from logging binary: %w", err) |
| 159 |
} |
| 160 |
cwg.Wait() |
| 161 |
|
| 162 |
default: |
| 163 |
outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0) |
| 164 |
if err != nil { |
| 165 |
return nil, err |
| 166 |
} |
| 167 |
outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0) |
| 168 |
if err != nil { |
| 169 |
return nil, err |
| 170 |
} |
| 171 |
wg.Add(1) |
| 172 |
cwg.Add(1) |
| 173 |
go func() { |
| 174 |
cwg.Done() |
| 175 |
buf := bufPool.Get().(*[]byte) |
| 176 |
defer bufPool.Put(buf) |
| 177 |
io.CopyBuffer(outw, epollConsole, *buf) |
| 178 |
|
| 179 |
outw.Close() |
| 180 |
outr.Close() |
| 181 |
wg.Done() |
| 182 |
}() |
| 183 |
cwg.Wait() |
| 184 |
} |
| 185 |
|
| 186 |
return epollConsole, nil |
| 187 |
} |
| 188 |
|
| 189 |
func (p *linuxPlatform) ShutdownConsole(ctx context.Context, cons console.Console) error { |
| 190 |
if p.epoller == nil { |
| 191 |
return errors.New("uninitialized epoller") |
| 192 |
} |
| 193 |
epollConsole, ok := cons.(*console.EpollConsole) |
| 194 |
if !ok { |
| 195 |
return fmt.Errorf("expected EpollConsole, got %#v", cons) |
| 196 |
} |
| 197 |
return epollConsole.Shutdown(p.epoller.CloseConsole) |
| 198 |
} |
| 199 |
|
| 200 |
func (p *linuxPlatform) Close() error { |
| 201 |
return p.epoller.Close() |
| 202 |
} |