From: Masatake YAMATO Date: Fri, 19 Dec 2025 18:19:42 +0000 (+0900) Subject: lslocks: (refactor) add a helper function refining value returned from get_lock X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9bccccf31ec404c53efcc3adc500b234451106c;p=thirdparty%2Futil-linux.git lslocks: (refactor) add a helper function refining value returned from get_lock Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c index 672bc89a0..bfdd3f160 100644 --- a/misc-utils/lslocks.c +++ b/misc-utils/lslocks.c @@ -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); }