From 82164a2e9ed5c6112e3ef70895c153025807b282 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <gscrivan@redhat.com>
Date: Thu, 27 Jun 2019 11:06:24 +0200
Subject: runtime: allow to specify the lock mechanism

preparation for the next patch.  It currently supports only "shm".

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
---
 libpod.conf       |  3 +++
 libpod/runtime.go | 76 ++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 50 insertions(+), 29 deletions(-)

diff --git a/libpod.conf b/libpod.conf
index 71ac79dd4..c92f60a10 100644
--- a/libpod.conf
+++ b/libpod.conf
@@ -87,6 +87,9 @@ infra_command = "/pause"
 # Default libpod support for container labeling
 # label=true
 
+# The locking mechanism to use
+lock_type = "shm"
+
 # Number of locks available for containers and pods.
 # If this is changed, a lock renumber must be performed (e.g. with the
 # 'podman system renumber' command).
diff --git a/libpod/runtime.go b/libpod/runtime.go
index 02aa76731..e8442f4a1 100644
--- a/libpod/runtime.go
+++ b/libpod/runtime.go
@@ -236,6 +236,9 @@ type RuntimeConfig struct {
 	// pods.
 	NumLocks uint32 `toml:"num_locks,omitempty"`
 
+	// LockType is the type of locking to use.
+	LockType string `toml:"lock_type,omitempty"`
+
 	// EventsLogger determines where events should be logged
 	EventsLogger string `toml:"events_logger"`
 	// EventsLogFilePath is where the events log is stored.
@@ -656,6 +659,48 @@ func newRuntimeFromConfig(ctx context.Context, userConfigPath string, options ..
 	return runtime, nil
 }
 
+func getLockManager(runtime *Runtime) (lock.Manager, error) {
+	var err error
+	var manager lock.Manager
+
+	switch runtime.config.LockType {
+	case "", "shm":
+		lockPath := DefaultSHMLockPath
+		if rootless.IsRootless() {
+			lockPath = fmt.Sprintf("%s_%d", DefaultRootlessSHMLockPath, rootless.GetRootlessUID())
+		}
+		// Set up the lock manager
+		manager, err = lock.OpenSHMLockManager(lockPath, runtime.config.NumLocks)
+		if err != nil {
+			if os.IsNotExist(errors.Cause(err)) {
+				manager, err = lock.NewSHMLockManager(lockPath, runtime.config.NumLocks)
+				if err != nil {
+					return nil, errors.Wrapf(err, "failed to get new shm lock manager")
+				}
+			} else if errors.Cause(err) == syscall.ERANGE && runtime.doRenumber {
+				logrus.Debugf("Number of locks does not match - removing old locks")
+
+				// ERANGE indicates a lock numbering mismatch.
+				// Since we're renumbering, this is not fatal.
+				// Remove the earlier set of locks and recreate.
+				if err := os.Remove(filepath.Join("/dev/shm", lockPath)); err != nil {
+					return nil, errors.Wrapf(err, "error removing libpod locks file %s", lockPath)
+				}
+
+				manager, err = lock.NewSHMLockManager(lockPath, runtime.config.NumLocks)
+				if err != nil {
+					return nil, err
+				}
+			} else {
+				return nil, err
+			}
+		}
+	default:
+		return nil, errors.Wrapf(define.ErrInvalidArg, "unknown lock type %s", runtime.config.LockType)
+	}
+	return manager, nil
+}
+
 // Make a new runtime based on the given configuration
 // Sets up containers/storage, state store, OCI runtime
 func makeRuntime(ctx context.Context, runtime *Runtime) (err error) {
@@ -1038,37 +1083,10 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (err error) {
 		}
 	}
 
-	lockPath := DefaultSHMLockPath
-	if rootless.IsRootless() {
-		lockPath = fmt.Sprintf("%s_%d", DefaultRootlessSHMLockPath, rootless.GetRootlessUID())
-	}
-	// Set up the lock manager
-	manager, err := lock.OpenSHMLockManager(lockPath, runtime.config.NumLocks)
+	runtime.lockManager, err = getLockManager(runtime)
 	if err != nil {
-		if os.IsNotExist(errors.Cause(err)) {
-			manager, err = lock.NewSHMLockManager(lockPath, runtime.config.NumLocks)
-			if err != nil {
-				return errors.Wrapf(err, "failed to get new shm lock manager")
-			}
-		} else if errors.Cause(err) == syscall.ERANGE && runtime.doRenumber {
-			logrus.Debugf("Number of locks does not match - removing old locks")
-
-			// ERANGE indicates a lock numbering mismatch.
-			// Since we're renumbering, this is not fatal.
-			// Remove the earlier set of locks and recreate.
-			if err := os.Remove(filepath.Join("/dev/shm", lockPath)); err != nil {
-				return errors.Wrapf(err, "error removing libpod locks file %s", lockPath)
-			}
-
-			manager, err = lock.NewSHMLockManager(lockPath, runtime.config.NumLocks)
-			if err != nil {
-				return err
-			}
-		} else {
-			return err
-		}
+		return err
 	}
-	runtime.lockManager = manager
 
 	// If we're renumbering locks, do it now.
 	// It breaks out of normal runtime init, and will not return a valid
-- 
cgit v1.2.3-54-g00ecf