Debian Codegraph @

cmd/containerd-shim-runc-v2/process/exec.go

raw
1 //go:build !windows
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 process
20
21 import (
22 "context"
23 "fmt"
24 "io"
25 "os"
26 "path/filepath"
27 "sync"
28 "syscall"
29 "time"
30
31 "golang.org/x/sys/unix"
32
33 "github.com/containerd/console"
34 "github.com/containerd/containerd/v2/pkg/stdio"
35 "github.com/containerd/errdefs"
36 "github.com/containerd/fifo"
37 runc "github.com/containerd/go-runc"
38 "github.com/containerd/log"
39 specs "github.com/opencontainers/runtime-spec/specs-go"
40 )
41
42 type execProcess struct {
43 wg sync.WaitGroup
44
45 execState execState
46
47 mu sync.Mutex
48 id string
49 console console.Console
50 io *processIO
51 status int
52 exited time.Time
53 pid safePid
54 closers []io.Closer
55 stdin io.Closer
56 stdio stdio.Stdio
57 path string
58 spec specs.Process
59
60 parent *Init
61 waitBlock chan struct{}
62 }
63
64 func (e *execProcess) Wait() {
65 <-e.waitBlock
66 }
67
68 func (e *execProcess) ID() string {
69 return e.id
70 }
71
72 func (e *execProcess) Pid() int {
73 return e.pid.get()
74 }
75
76 func (e *execProcess) ExitStatus() int {
77 e.mu.Lock()
78 defer e.mu.Unlock()
79 return e.status
80 }
81
82 func (e *execProcess) ExitedAt() time.Time {
83 e.mu.Lock()
84 defer e.mu.Unlock()
85 return e.exited
86 }
87
88 func (e *execProcess) SetExited(status int) {
89 e.mu.Lock()
90 defer e.mu.Unlock()
91
92 e.execState.SetExited(status)
93 }
94
95 func (e *execProcess) setExited(status int) {
96 e.status = status
97 e.exited = time.Now()
98 e.parent.Platform.ShutdownConsole(context.Background(), e.console)
99 close(e.waitBlock)
100 }
101
102 func (e *execProcess) Delete(ctx context.Context) error {
103 e.mu.Lock()
104 defer e.mu.Unlock()
105
106 return e.execState.Delete(ctx)
107 }
108
109 func (e *execProcess) delete(ctx context.Context) error {
110 if err := waitTimeout(ctx, &e.wg, 10*time.Second); err != nil {
111 log.G(ctx).WithError(err).Errorf("failed to drain exec process %s io", e.id)
112 }
113 if e.io != nil {
114 for _, c := range e.closers {
115 c.Close()
116 }
117 e.io.Close()
118 }
119 pidfile := filepath.Join(e.path, fmt.Sprintf("%s.pid", e.id))
120 // silently ignore error
121 os.Remove(pidfile)
122 return nil
123 }
124
125 func (e *execProcess) Resize(ws console.WinSize) error {
126 e.mu.Lock()
127 defer e.mu.Unlock()
128
129 return e.execState.Resize(ws)
130 }
131
132 func (e *execProcess) resize(ws console.WinSize) error {
133 if e.console == nil {
134 return nil
135 }
136 return e.console.Resize(ws)
137 }
138
139 func (e *execProcess) Kill(ctx context.Context, sig uint32, _ bool) error {
140 e.mu.Lock()
141 defer e.mu.Unlock()
142
143 return e.execState.Kill(ctx, sig, false)
144 }
145
146 func (e *execProcess) kill(ctx context.Context, sig uint32, _ bool) error {
147 pid := e.pid.get()
148 switch {
149 case pid == 0:
150 return fmt.Errorf("process not created: %w", errdefs.ErrFailedPrecondition)
151 case !e.exited.IsZero():
152 return fmt.Errorf("process already finished: %w", errdefs.ErrNotFound)
153 default:
154 if err := unix.Kill(pid, syscall.Signal(sig)); err != nil {
155 return fmt.Errorf("exec kill error: %w", checkKillError(err))
156 }
157 }
158 return nil
159 }
160
161 func (e *execProcess) Stdin() io.Closer {
162 return e.stdin
163 }
164
165 func (e *execProcess) Stdio() stdio.Stdio {
166 return e.stdio
167 }
168
169 func (e *execProcess) Start(ctx context.Context) error {
170 e.mu.Lock()
171 defer e.mu.Unlock()
172
173 return e.execState.Start(ctx)
174 }
175
176 func (e *execProcess) start(ctx context.Context) (err error) {
177 // The reaper may receive exit signal right after
178 // the container is started, before the e.pid is updated.
179 // In that case, we want to block the signal handler to
180 // access e.pid until it is updated.
181 e.pid.Lock()
182 defer e.pid.Unlock()
183
184 var (
185 socket *runc.Socket
186 pio *processIO
187 pidFile = newExecPidFile(e.path, e.id)
188 )
189 if e.stdio.Terminal {
190 if socket, err = runc.NewTempConsoleSocket(); err != nil {
191 return fmt.Errorf("failed to create runc console socket: %w", err)
192 }
193 defer socket.Close()
194 } else {
195 if pio, err = createIO(ctx, e.id, e.parent.IoUID, e.parent.IoGID, e.stdio); err != nil {
196 return fmt.Errorf("failed to create init process I/O: %w", err)
197 }
198 e.io = pio
199 }
200 opts := &runc.ExecOpts{
201 PidFile: pidFile.Path(),
202 Detach: true,
203 }
204 if pio != nil {
205 opts.IO = pio.IO()
206 }
207 if socket != nil {
208 opts.ConsoleSocket = socket
209 }
210 if err := e.parent.runtime.Exec(ctx, e.parent.id, e.spec, opts); err != nil {
211 close(e.waitBlock)
212 return e.parent.runtimeError(err, "OCI runtime exec failed")
213 }
214 if e.stdio.Stdin != "" {
215 if err := e.openStdin(e.stdio.Stdin); err != nil {
216 return err
217 }
218 }
219 ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
220 defer cancel()
221 if socket != nil {
222 console, err := socket.ReceiveMaster()
223 if err != nil {
224 return fmt.Errorf("failed to retrieve console master: %w", err)
225 }
226 if e.console, err = e.parent.Platform.CopyConsole(ctx, console, e.id, e.stdio.Stdin, e.stdio.Stdout, e.stdio.Stderr, &e.wg); err != nil {
227 return fmt.Errorf("failed to start console copy: %w", err)
228 }
229 } else {
230 if err := pio.Copy(ctx, &e.wg); err != nil {
231 return fmt.Errorf("failed to start io pipe copy: %w", err)
232 }
233 }
234 pid, err := pidFile.Read()
235 if err != nil {
236 return fmt.Errorf("failed to retrieve OCI runtime exec pi: %wd", err)
237 }
238 e.pid.pid = pid
239 return nil
240 }
241
242 func (e *execProcess) openStdin(path string) error {
243 sc, err := fifo.OpenFifo(context.Background(), path, syscall.O_WRONLY|syscall.O_NONBLOCK, 0)
244 if err != nil {
245 return fmt.Errorf("failed to open stdin fifo %s: %w", path, err)
246 }
247 e.stdin = sc
248 e.closers = append(e.closers, sc)
249 return nil
250 }
251
252 func (e *execProcess) Status(ctx context.Context) (string, error) {
253 s, err := e.parent.Status(ctx)
254 if err != nil {
255 return "", err
256 }
257 // if the container as a whole is in the pausing/paused state, so are all
258 // other processes inside the container, use container state here
259 switch s {
260 case "paused", "pausing":
261 return s, nil
262 }
263 e.mu.Lock()
264 defer e.mu.Unlock()
265 return e.execState.Status(ctx)
266 }