]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: (refactor) add a helper function refining value returned from get_lock
authorMasatake YAMATO <yamato@redhat.com>
Fri, 19 Dec 2025 18:19:42 +0000 (03:19 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Sun, 21 Dec 2025 19:41:13 +0000 (04:41 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lslocks.c

index 672bc89a03774666b8da6edb317440571bef1267..bfdd3f160532bbeee565ed09c649a46a35d84078 100644 (file)
@@ -420,21 +420,27 @@ static struct lock *get_lock(char *buf, struct override_info *oinfo, void *fallb
                        l->cmdname = xstrdup(_("(undefined)"));
        }
        l->path = get_filename_sz(l->inode, l->pid, &sz);
+       if (l->path)
+               l->size = sz;
+
+       return l;
+}
 
+static struct lock *refine_lock(struct lock *lock)
+{
        /* no permissions -- ignore */
-       if (!l->path && no_inaccessible) {
-               rem_lock(l);
+       if (!lock->path && no_inaccessible) {
+               rem_lock(lock);
                return NULL;
        }
 
-       if (!l->path) {
+       if (!lock->path) {
                /* probably no permission to peek into l->pid's path */
-               l->path = get_fallback_filename(l->dev);
-               l->size = 0;
-       } else
-               l->size = sz;
+               lock->path = get_fallback_filename(lock->dev);
+               lock->size = 0;
+       }
 
-       return l;
+       return lock;
 }
 
 static int get_pid_lock(void *locks, FILE *fp,
@@ -451,6 +457,8 @@ static int get_pid_lock(void *locks, FILE *fp,
                if (strncmp(buf, "lock:\t", 6))
                        continue;
                l = get_lock(buf + 6, &oinfo, NULL);
+               if (l)
+                       l = refine_lock(l);
                if (l) {
                        add_to_tree(locks, l);
                        l->fd = fd;
@@ -535,6 +543,8 @@ static int get_proc_locks(struct list_head *locks, void *fallback)
 
        while (fgets(buf, sizeof(buf), fp)) {
                struct lock *l = get_lock(buf, NULL, fallback);
+               if (l)
+                       l = refine_lock(l);
                if (l)
                        add_to_list(locks, l);
        }