diff mbox series

[03/11] runqemu: rename the port lock helpers to generic lock helpers

Message ID 20261011195949.2118607-4-adrian.freihofer@siemens.com
State New
Headers show
Series runqemu: use QMP for slirp host port allocation | expand

Commit Message

AdrianF Oct. 11, 2026, 7:59 p.m. UTC
From: Adrian Freihofer <adrian.freihofer@siemens.com>

acquire_portlock()/release_portlock()/self.portlocks implement a plain
"hold a flock on a numbered lockfile, pass the fd to qemu, close it on
cleanup" scheme that is not specific to ports. Drop "port" from the
names so the same helpers can be used to allocate other per-instance
resources without the names becoming misleading.

No functional change.

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 scripts/runqemu | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/scripts/runqemu b/scripts/runqemu
index 5f1af740c9..6544c3884b 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -198,7 +198,7 @@  class BaseConfig(object):
         self.tcpserial_portnum = ''
         self.taplock = ''
         self.taplock_descriptor = None
-        self.portlocks = {}
+        self.locks = {}
         self.bitbake_e = ''
         self.snapshot = False
         self.wictypes = ('wic.zst', 'wic', 'wic.vmdk', 'wic.qcow2', 'wic.vdi', "wic.vhd", "wic.vhdx")
@@ -263,11 +263,11 @@  class BaseConfig(object):
     def check_free_port(self, host, port, lockdir):
         """ Check whether the port is free or not """
         lockfile = os.path.join(lockdir, str(port) + '.lock')
-        if self.acquire_portlock(lockfile):
+        if self.acquire_lock(lockfile):
             with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
                 if sock.connect_ex((host, port)) == 0:
                     # Port is open, so not free
-                    self.release_portlock(lockfile)
+                    self.release_lock(lockfile)
                     return False
                 else:
                     # Port is not open, so free
@@ -275,33 +275,33 @@  class BaseConfig(object):
         else:
             return False
 
-    def acquire_portlock(self, lockfile):
+    def acquire_lock(self, lockfile):
         logger.debug("Acquiring lockfile %s..." % lockfile)
-        portlock_descriptor = None
+        lock_descriptor = None
         try:
-            portlock_descriptor = open(lockfile, 'w')
-            fcntl.flock(portlock_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB)
+            lock_descriptor = open(lockfile, 'w')
+            fcntl.flock(lock_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB)
         except Exception as e:
             msg = "Acquiring lockfile %s failed: %s" % (lockfile, e)
             logger.info(msg)
-            if portlock_descriptor:
-                portlock_descriptor.close()
+            if lock_descriptor:
+                lock_descriptor.close()
             return False
-        self.portlocks[lockfile] = portlock_descriptor
+        self.locks[lockfile] = lock_descriptor
         return True
 
-    def release_portlock(self, lockfile=None):
+    def release_lock(self, lockfile=None):
         if lockfile != None:
             logger.debug("Releasing lockfile '%s'" % lockfile)
             # We pass the fd to the qemu process and if we unlock here, it would unlock for
             # that too. Therefore don't unlock, just close
-            # fcntl.flock(self.portlocks[lockfile], fcntl.LOCK_UN)
-            self.portlocks[lockfile].close()
+            # fcntl.flock(self.locks[lockfile], fcntl.LOCK_UN)
+            self.locks[lockfile].close()
             # Removing the file is a potential race, don't do that either
             # os.remove(lockfile)
-            del self.portlocks[lockfile]
-        elif len(self.portlocks):
-            for lockfile, descriptor in self.portlocks.items():
+            del self.locks[lockfile]
+        elif len(self.locks):
+            for lockfile, descriptor in self.locks.items():
                 logger.debug("Releasing lockfile '%s'" % lockfile)
                 # We pass the fd to the qemu process and if we unlock here, it would unlock for
                 # that too. Therefore don't unlock, just close
@@ -309,7 +309,7 @@  class BaseConfig(object):
                 descriptor.close()
                 # Removing the file is a potential race, don't do that either
                 # os.remove(lockfile)
-            self.portlocks = {}
+            self.locks = {}
 
     def get(self, key):
         if key in self.d:
@@ -1632,8 +1632,8 @@  to your build configuration.
         pass_fds = []
         if self.taplock_descriptor:
             pass_fds = [self.taplock_descriptor.fileno()]
-        if len(self.portlocks):
-            for descriptor in self.portlocks.values():
+        if len(self.locks):
+            for descriptor in self.locks.values():
                 pass_fds.append(descriptor.fileno())
         process = subprocess.Popen(cmds, stderr=subprocess.PIPE, pass_fds=pass_fds, env=self.qemu_environ)
         self.qemuprocess = process
@@ -1682,7 +1682,7 @@  to your build configuration.
             cmd = ("runqemu-export-rootfs", "stop", self.rootfs)
             logger.debug('Running %s' % str(cmd))
             subprocess.check_call(cmd)
-        self.release_portlock()
+        self.release_lock()
 
         if self.saved_stty:
             subprocess.check_call(("stty", self.saved_stty))