From: Masatake YAMATO Date: Thu, 27 Jul 2023 15:29:49 +0000 (+0900) Subject: lsfd: (refactor) make the code for traversing threads reusable X-Git-Tag: v2.40-rc1~244^2~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=174933b433d6a37651171ff8ab32e92c9b234def;p=thirdparty%2Futil-linux.git lsfd: (refactor) make the code for traversing threads reusable Signed-off-by: Masatake YAMATO --- 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);