Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libct/cg: retry opening a cgroup file #4442

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions libcontainer/cgroups/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
Expand All @@ -25,7 +25,24 @@ func OpenFile(dir, file string, flags int) (*os.File, error) {
if dir == "" {
return nil, fmt.Errorf("no directory specified for %s", file)
}
return openFile(dir, file, flags)
path := filepath.Join(dir, utils.CleanPath(file))
dir = filepath.Dir(path)
try := 0
const maxRetries = 3
for {
fd, err := openFile(path, flags)
switch {
case err == nil:
return fd, nil
case errors.Is(err, unix.ENOENT) || errors.Is(err, unix.ENODEV):
if try > maxRetries || unix.Access(dir, unix.F_OK) != nil {
return nil, err
}
try++
default:
return nil, err
}
}
}

// ReadFile reads data from a cgroup file in dir.
Expand Down Expand Up @@ -140,14 +157,13 @@ func prepareOpenat2() error {
return prepErr
}

func openFile(dir, file string, flags int) (*os.File, error) {
func openFile(path string, flags int) (*os.File, error) {
mode := os.FileMode(0)
if TestMode && flags&os.O_WRONLY != 0 {
// "emulate" cgroup fs for unit tests
flags |= os.O_TRUNC | os.O_CREATE
mode = 0o600
}
path := path.Join(dir, utils.CleanPath(file))
if prepareOpenat2() != nil {
return openFallback(path, flags, mode)
}
Expand Down