From 174933b433d6a37651171ff8ab32e92c9b234def Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Fri, 28 Jul 2023 00:29:49 +0900 Subject: [PATCH] lsfd: (refactor) make the code for traversing threads reusable Signed-off-by: Masatake YAMATO --- misc-utils/lsfd.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index 6e88a0db71..bb513c07e1 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -1418,6 +1418,21 @@ unsigned long add_name(struct name_manager *nm, const char *name) return e->id; } +static void walk_threads(struct lsfd_control *ctl, struct path_cxt *pc, + pid_t pid, struct proc *proc, + void (*cb)(struct lsfd_control *, struct path_cxt *, + pid_t, struct proc *)) +{ + DIR *sub = NULL; + pid_t tid = 0; + + while (procfs_process_next_tid(pc, &sub, &tid) == 0) { + if (tid == pid) + continue; + (*cb)(ctl, pc, tid, proc); + } +} + static void read_process(struct lsfd_control *ctl, struct path_cxt *pc, pid_t pid, struct proc *leader) { @@ -1490,16 +1505,8 @@ static void read_process(struct lsfd_control *ctl, struct path_cxt *pc, /* The tasks collecting overwrites @pc by /proc//. Keep it as * the last path based operation in read_process() */ - if (ctl->threads && leader == NULL) { - DIR *sub = NULL; - pid_t tid = 0; - - while (procfs_process_next_tid(pc, &sub, &tid) == 0) { - if (tid == pid) - continue; - read_process(ctl, pc, tid, proc); - } - } + if (ctl->threads && leader == NULL) + walk_threads(ctl, pc, pid, proc, read_process); /* Let's be careful with number of open files */ ul_path_close_dirfd(pc); -- 2.47.3