Files
Paul Holzinger 836fa4c493 Move the cni lock file into the cni config dir
Commit(fe3faa517e1b) introduced a lock file for network create/rm calls.
There is a problem with the location of the lock file. The lock file was
stored in the tmpdir. Running multiple podman network create/remove
commands in parallel with different tmpdirs made the lockfile inaccessible
to the other process, and so parallel read/write operations to the cni
config directory continued to occur. This scenario happened frequently
during the e2e tests and caused some flakes.

Fixes #9041

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2021-01-21 11:50:45 +01:00

36 lines
835 B
Go

package network
import (
"os"
"path/filepath"
"github.com/containers/common/pkg/config"
"github.com/containers/storage"
)
// acquireCNILock gets a lock that should be used in create and
// delete cases to avoid unwanted collisions in network names.
// TODO this uses a file lock and should be converted to shared memory
// when we have a more general shared memory lock in libpod
func acquireCNILock(config *config.Config) (*CNILock, error) {
cniDir := GetCNIConfDir(config)
err := os.MkdirAll(cniDir, 0755)
if err != nil {
return nil, err
}
l, err := storage.GetLockfile(filepath.Join(cniDir, LockFileName))
if err != nil {
return nil, err
}
l.Lock()
cnilock := CNILock{
Locker: l,
}
return &cnilock, nil
}
// ReleaseCNILock unlocks the previously held lock
func (l *CNILock) releaseCNILock() {
l.Unlock()
}