Skip to content

Commit 2db0c5e

Browse files
authored
Merge pull request #5155 from cyphar/intelrdt-improve-mkdir
libct: intelrdt: improve directory cleanup logic
2 parents 1c35df9 + fbaf5e3 commit 2db0c5e

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

libcontainer/intelrdt/intelrdt.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ type Manager struct {
151151
config *configs.Config
152152
id string
153153
path string
154-
directoryCreated bool
154+
shouldCleanupDir bool
155155
}
156156

157157
// NewManager returns a new instance of Manager, or nil if the Intel RDT
@@ -187,10 +187,9 @@ func NewManager(config *configs.Config, id, path string) *Manager {
187187
// is actually available. Used by unit tests that mock intelrdt paths.
188188
func newManager(config *configs.Config, id, path string) *Manager {
189189
return &Manager{
190-
config: config,
191-
id: id,
192-
path: path,
193-
directoryCreated: false,
190+
config: config,
191+
id: id,
192+
path: path,
194193
}
195194
}
196195

@@ -462,15 +461,10 @@ func (m *Manager) Apply(pid int) (err error) {
462461
}
463462
}
464463

465-
// If the directory doesn't exist we need to create it -> it means we also need
466-
// to clean it up afterwards. Make a note to the manager.
467-
if _, err := os.Stat(path); err != nil {
468-
if errors.Is(err, os.ErrNotExist) {
469-
m.directoryCreated = true
470-
}
471-
}
472-
473-
if err := os.Mkdir(path, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
464+
if err := os.Mkdir(path, 0o755); err == nil || errors.Is(err, os.ErrExist) {
465+
// Only clean up the directory if we actually created it.
466+
m.shouldCleanupDir = err == nil
467+
} else {
474468
return newLastCmdError(err)
475469
}
476470

@@ -501,7 +495,7 @@ func (m *Manager) Destroy() error {
501495
// group is likely externally managed, i.e. by some other entity than us.
502496
// There are probably other containers/tasks sharing the same group. Also
503497
// only remove the directory if it was created by us.
504-
if m.config.IntelRdt.ClosID == "" && m.directoryCreated {
498+
if m.config.IntelRdt.ClosID == "" && m.shouldCleanupDir {
505499
m.mu.Lock()
506500
defer m.mu.Unlock()
507501
if err := os.Remove(m.GetPath()); err != nil && !errors.Is(err, os.ErrNotExist) {

0 commit comments

Comments
 (0)