[projects/pld-ftp-admin] ftpiod: name the holder of a lock it refuses

arekm arekm at pld-linux.org
Thu Nov 12 00:28:31 CET 2026


commit 20307153663c20d7383f4099acffa95628b0801d
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Wed Nov 11 22:48:43 2026 +0100

    ftpiod: name the holder of a lock it refuses
    
    A tool that finds a tree locked said only "tree already locked", so
    finding out what held it, and whether it still ran, took
    pfa-dump-locks and guesswork.
    
    ftpiod now keeps the pid of the client that took a lock, as the kernel
    reports it for the unix socket, and sends the holder's name, pid and
    lock time with a refusal; the tools add them to their message, and
    pfa-dump-locks shows the pid. An older ftpiod sends no holder, and the
    message stays as it was; ftpiod needs a restart to send it.

 bin/pfa-checksign     |  2 +-
 bin/pfa-checktree     |  2 +-
 bin/pfa-from-incoming |  2 +-
 bin/pfa-genindex      |  2 +-
 bin/pfa-lintpkg       |  4 ++--
 bin/pfa-mvpkg         |  2 +-
 bin/pfa-rmpkg         |  2 +-
 bin/pfa-signpkg       |  2 +-
 bin/pfa-testmvpkg     |  2 +-
 modules/cmds.py       | 11 ++++++-----
 modules/cons.py       |  3 +++
 modules/ftpio.py      | 18 ++++++++++++++++--
 wwwbin/clean-dups.py  |  2 +-
 13 files changed, 36 insertions(+), 18 deletions(-)
---
diff --git a/bin/pfa-checksign b/bin/pfa-checksign
index 3e3b112..7076f28 100755
--- a/bin/pfa-checksign
+++ b/bin/pfa-checksign
@@ -30,7 +30,7 @@ checkdir(treename)
 ftpio.connect('sign')
 
 if not ftpio.lock(treename, True):
-    print(f"ERR: {treename} tree already locked", file=sys.stderr)
+    print(f"ERR: {treename} tree already locked{ftpio.locked_by()}", file=sys.stderr)
     sys.exit(1)
 
 try:
diff --git a/bin/pfa-checktree b/bin/pfa-checktree
index df7bdfc..cbaaa07 100755
--- a/bin/pfa-checktree
+++ b/bin/pfa-checktree
@@ -35,7 +35,7 @@ locked = []
 try:
     for name in (srcname, dstname):
         if not ftpio.lock(name, True):
-            print(f"ERR: {name} tree already locked")
+            print(f"ERR: {name} tree already locked{ftpio.locked_by()}")
             sys.exit(1)
         locked.append(name)
 
diff --git a/bin/pfa-from-incoming b/bin/pfa-from-incoming
index 98aa110..db17fd1 100755
--- a/bin/pfa-from-incoming
+++ b/bin/pfa-from-incoming
@@ -126,7 +126,7 @@ except OSError:
 ftptree = BaseFtpTree(cval['default_to'])
 
 if not ftpio.lock(cval['default_to']):
-    print(f"Can't get lock: {cval['default_to']}")
+    print(f"Can't get lock: {cval['default_to']}{ftpio.locked_by()}")
     sys.exit(1)
 
 moved_anything = False
diff --git a/bin/pfa-genindex b/bin/pfa-genindex
index 6c4814e..7007316 100755
--- a/bin/pfa-genindex
+++ b/bin/pfa-genindex
@@ -179,7 +179,7 @@ pool = concurrent.futures.ThreadPoolExecutor(args.jobs)
 try:
     for tree in trees:
         if not ftpio.lock(tree, True):
-            print(f"ERR: {tree} tree already locked", file=sys.stderr)
+            print(f"ERR: {tree} tree already locked{ftpio.locked_by()}", file=sys.stderr)
             sys.exit(1)
         locked.append(tree)
     if do_yum:
diff --git a/bin/pfa-lintpkg b/bin/pfa-lintpkg
index 728100f..bb5d9a6 100755
--- a/bin/pfa-lintpkg
+++ b/bin/pfa-lintpkg
@@ -45,7 +45,7 @@ if not os.access(RPMLINT, os.X_OK):
 ftpio.connect('rpmlint')
 
 if not ftpio.lock(treename, True):
-    print(f"ERR: {treename} tree already locked", file=sys.stderr)
+    print(f"ERR: {treename} tree already locked{ftpio.locked_by()}", file=sys.stderr)
     sys.exit(1)
 
 try:
@@ -169,7 +169,7 @@ class LintPkg:
 try:
     lock = 'rpmlint:'+treename
     if not ftpio.lock(lock, True):
-        print(f"ERR: {treename} tree already locked for rpmlint", file=sys.stderr)
+        print(f"ERR: {treename} tree already locked for rpmlint{ftpio.locked_by()}", file=sys.stderr)
         sys.exit(1)
 
     if not quiet:
diff --git a/bin/pfa-mvpkg b/bin/pfa-mvpkg
index a7589d5..092863a 100755
--- a/bin/pfa-mvpkg
+++ b/bin/pfa-mvpkg
@@ -48,7 +48,7 @@ locked = []
 try:
     for name in treenames:
         if not ftpio.lock(name, True):
-            print(f"ERR: {name} tree already locked", file=sys.stderr)
+            print(f"ERR: {name} tree already locked{ftpio.locked_by()}", file=sys.stderr)
             sys.exit(1)
         locked.append(name)
     srctree = ftptree.FtpTree(srcname, loadall = True)
diff --git a/bin/pfa-rmpkg b/bin/pfa-rmpkg
index a793e7b..b307cb2 100755
--- a/bin/pfa-rmpkg
+++ b/bin/pfa-rmpkg
@@ -24,7 +24,7 @@ checkdir(treename)
 ftpio.connect('remove')
 
 if not ftpio.lock(treename, True):
-    print(f"ERR: {treename} tree already locked")
+    print(f"ERR: {treename} tree already locked{ftpio.locked_by()}")
     sys.exit(1)
 
 try:
diff --git a/bin/pfa-signpkg b/bin/pfa-signpkg
index 1488e82..a086934 100755
--- a/bin/pfa-signpkg
+++ b/bin/pfa-signpkg
@@ -42,7 +42,7 @@ if not unlock_key():
 ftpio.connect('sign')
 
 if not ftpio.lock(treename, True):
-    print(f"ERR: {treename} tree already locked", file=sys.stderr)
+    print(f"ERR: {treename} tree already locked{ftpio.locked_by()}", file=sys.stderr)
     sys.exit(1)
 
 try:
diff --git a/bin/pfa-testmvpkg b/bin/pfa-testmvpkg
index 8a33502..1e77166 100755
--- a/bin/pfa-testmvpkg
+++ b/bin/pfa-testmvpkg
@@ -43,7 +43,7 @@ locked = []
 try:
     for name in treenames:
         if not ftpio.lock(name, True):
-            print(f"ERR: {name} tree already locked")
+            print(f"ERR: {name} tree already locked{ftpio.locked_by()}")
             sys.exit(1)
         locked.append(name)
     srctree = ftptree.FtpTree(srcname, loadall = True)
diff --git a/modules/cmds.py b/modules/cmds.py
index 9231f66..e0632cd 100644
--- a/modules/cmds.py
+++ b/modules/cmds.py
@@ -49,12 +49,13 @@ def lock_key(arg):
 def lock(con, arg, hard):
     arg = lock_key(arg)
     if arg not in locks:
-        locks[arg]={'hard': hard, 'name': con.name, 'time': int(time.time())}
+        locks[arg]={'hard': hard, 'name': con.name, 'pid': con.pid, 'time': int(time.time())}
         con.sock.send(bytearray("OK", encoding='utf-8'))
-    elif locks[arg]['hard']:
-        con.sock.send(bytearray("HARD", encoding='utf-8')) # Hard lock - you can go get a cup of tea
     else:
-        con.sock.send(bytearray("SOFT", encoding='utf-8')) # Soft lock - try in a second or two
+        # hard lock - you can go get a cup of tea; soft lock - try in a second or two
+        held = locks[arg]
+        kind = "HARD" if held['hard'] else "SOFT"
+        con.sock.send(bytearray(f"{kind} {held['pid']} {held['time']} {held['name']}", encoding='utf-8'))
 
 def cmd_unlock(con, arg):
     arg = lock_key(arg)
@@ -77,7 +78,7 @@ def cmd_show_locks(con):
         for lockdata in locks.items():
             tree, data = lockdata
             when = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data['time']))
-            msg = f"Tree: {tree}, Conn name: {data['name']}, Hard Lock: {data['hard']}, Time: {when}"
+            msg = f"Tree: {tree}, Conn name: {data['name']}, Pid: {data['pid']}, Hard Lock: {data['hard']}, Time: {when}"
             cmd_log(con, msg)
             res.append(msg)
 #        con.sock.send(bytearray("BLOB:%d" % len(res), encoding='utf-8')))
diff --git a/modules/cons.py b/modules/cons.py
index 1b58d96..86b5fb2 100644
--- a/modules/cons.py
+++ b/modules/cons.py
@@ -3,6 +3,7 @@
 import os
 import select
 import socket
+import struct
 import traceback
 
 import cmds
@@ -16,6 +17,8 @@ class Connection:
         self.sock=sock
         self.authorized=authorized
         self.fileno=sock.fileno
+        # the pid of the client process, as the kernel reports it for a unix socket
+        self.pid=struct.unpack('3i', sock.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, struct.calcsize('3i')))[0]
         self.name=""
         self.data=""
         self.cookie=None
diff --git a/modules/ftpio.py b/modules/ftpio.py
index 3d3eac1..31a6e2a 100644
--- a/modules/ftpio.py
+++ b/modules/ftpio.py
@@ -44,23 +44,37 @@ def login_cookie(cookie):
 def logout():
     sock.send(bytearray('lout\0', encoding='utf-8'))
 
+# the holder of the lock lock() last did not get, see locked_by()
+holder = ''
+
 def lock(path, hard=False):
+    global holder
     def dolock():
         if hard:
             sock.send(bytearray(f'lckh {path}\0', encoding='utf-8'))
         else:
             sock.send(bytearray(f'lcks {path}\0', encoding='utf-8'))
-        return sock.recv(20).decode("utf-8")
+        return sock.recv(4096).decode("utf-8")
     for i in range(3):
         retcode=dolock()
         if retcode=="OK":
             return True
-        elif retcode=="HARD":
+        # HARD or SOFT, then the pid, the time and the name of the holder; an older ftpiod sends no holder
+        kind, _, held = retcode.partition(' ')
+        holder = ''
+        if held:
+            pid, when, name = held.split(' ', 2)
+            holder = f" by {name} (pid {pid}) since {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(when)))}"
+        if kind=="HARD":
             return False
         if i!=2:
             time.sleep(1)
     return False
     
+def locked_by():
+    """' by NAME (pid N) since TIME' of the lock lock() last did not get, '' when ftpiod did not say"""
+    return holder
+
 def unlock(path):
     sock.send(bytearray(f'ulck {path}\0', encoding='utf-8'))
     ret = sock.recv(20).decode("utf-8")
diff --git a/wwwbin/clean-dups.py b/wwwbin/clean-dups.py
index a8b7513..d15fab3 100755
--- a/wwwbin/clean-dups.py
+++ b/wwwbin/clean-dups.py
@@ -156,7 +156,7 @@ ftp_tree = "test"
 ftpio.connect('clean-dups')
 
 if not ftpio.lock(ftp_tree, True):
-    print(f"ERR: {ftp_tree} tree already locked")
+    print(f"ERR: {ftp_tree} tree already locked{ftpio.locked_by()}")
     sys.exit(1)
 
 try:
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-ftp-admin.git/commitdiff/f0f51b4f65cdffd92042e4d10e359be0a751e080



More information about the pld-cvs-commit mailing list