github.com/AbhinandanKurakure/podman/v3@v3.4.10/libpod/network/lock.go (about)

     1  package network
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/containers/common/pkg/config"
     8  	"github.com/containers/storage"
     9  )
    10  
    11  // acquireCNILock gets a lock that should be used in create and
    12  // delete cases to avoid unwanted collisions in network names.
    13  // TODO this uses a file lock and should be converted to shared memory
    14  // when we have a more general shared memory lock in libpod
    15  func acquireCNILock(config *config.Config) (*CNILock, error) {
    16  	cniDir := GetCNIConfDir(config)
    17  	err := os.MkdirAll(cniDir, 0755)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	l, err := storage.GetLockfile(filepath.Join(cniDir, LockFileName))
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	l.Lock()
    26  	cnilock := CNILock{
    27  		Locker: l,
    28  	}
    29  	return &cnilock, nil
    30  }
    31  
    32  // ReleaseCNILock unlocks the previously held lock
    33  func (l *CNILock) releaseCNILock() {
    34  	l.Unlock()
    35  }