Skip to content

Commit 0b27d93

Browse files
authored
Merge pull request #4150 from thaJeztah/1.3_backport_no_del_rootdir_ios
[release/1.3 backport] Correct logic of FIFO cleanup
2 parents 114bbed + fd2c9e3 commit 0b27d93

3 files changed

Lines changed: 60 additions & 31 deletions

File tree

container.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/containerd/containerd/images"
3333
"github.com/containerd/containerd/oci"
3434
"github.com/containerd/containerd/runtime/v2/runc/options"
35+
"github.com/containerd/containerd/sys"
3536
"github.com/containerd/typeurl"
3637
prototypes "github.com/gogo/protobuf/types"
3738
ver "github.com/opencontainers/image-spec/specs-go"
@@ -411,29 +412,37 @@ func attachExistingIO(response *tasks.GetResponse, ioAttach cio.Attach) (cio.IO,
411412

412413
// loadFifos loads the containers fifos
413414
func loadFifos(response *tasks.GetResponse) *cio.FIFOSet {
414-
path := getFifoDir([]string{
415+
fifos := []string{
415416
response.Process.Stdin,
416417
response.Process.Stdout,
417418
response.Process.Stderr,
418-
})
419+
}
419420
closer := func() error {
420-
return os.RemoveAll(path)
421+
var (
422+
err error
423+
dirs = map[string]struct{}{}
424+
)
425+
for _, fifo := range fifos {
426+
if isFifo, _ := sys.IsFifo(fifo); isFifo {
427+
if rerr := os.Remove(fifo); err == nil {
428+
err = rerr
429+
}
430+
dirs[filepath.Dir(fifo)] = struct{}{}
431+
}
432+
}
433+
for dir := range dirs {
434+
// we ignore errors here because we don't
435+
// want to remove the directory if it isn't
436+
// empty
437+
os.Remove(dir)
438+
}
439+
return err
421440
}
441+
422442
return cio.NewFIFOSet(cio.Config{
423443
Stdin: response.Process.Stdin,
424444
Stdout: response.Process.Stdout,
425445
Stderr: response.Process.Stderr,
426446
Terminal: response.Process.Terminal,
427447
}, closer)
428448
}
429-
430-
// getFifoDir looks for any non-empty path for a stdio fifo
431-
// and returns the dir for where it is located
432-
func getFifoDir(paths []string) string {
433-
for _, p := range paths {
434-
if p != "" {
435-
return filepath.Dir(p)
436-
}
437-
}
438-
return ""
439-
}

pkg/process/io.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/containerd/containerd/log"
3434
"github.com/containerd/containerd/namespaces"
3535
"github.com/containerd/containerd/pkg/stdio"
36+
"github.com/containerd/containerd/sys"
3637
"github.com/containerd/fifo"
3738
runc "github.com/containerd/go-runc"
3839
"github.com/pkg/errors"
@@ -174,7 +175,7 @@ func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, w
174175
},
175176
},
176177
} {
177-
ok, err := isFifo(i.name)
178+
ok, err := sys.IsFifo(i.name)
178179
if err != nil {
179180
return err
180181
}
@@ -240,22 +241,6 @@ func (c *countingWriteCloser) Close() error {
240241
return c.WriteCloser.Close()
241242
}
242243

243-
// isFifo checks if a file is a fifo
244-
// if the file does not exist then it returns false
245-
func isFifo(path string) (bool, error) {
246-
stat, err := os.Stat(path)
247-
if err != nil {
248-
if os.IsNotExist(err) {
249-
return false, nil
250-
}
251-
return false, err
252-
}
253-
if stat.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
254-
return true, nil
255-
}
256-
return false, nil
257-
}
258-
259244
// NewBinaryIO runs a custom binary process for pluggable shim logging
260245
func NewBinaryIO(ctx context.Context, id string, uri *url.URL) (runc.IO, error) {
261246
ns, err := namespaces.NamespaceRequired(ctx)

sys/filesys.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sys
18+
19+
import "os"
20+
21+
// IsFifo checks if a file is a (named pipe) fifo
22+
// if the file does not exist then it returns false
23+
func IsFifo(path string) (bool, error) {
24+
stat, err := os.Stat(path)
25+
if err != nil {
26+
if os.IsNotExist(err) {
27+
return false, nil
28+
}
29+
return false, err
30+
}
31+
if stat.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
32+
return true, nil
33+
}
34+
return false, nil
35+
}

0 commit comments

Comments
 (0)