]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: (refactor) make the code for traversing threads reusable
authorMasatake YAMATO <yamato@redhat.com>
Thu, 27 Jul 2023 15:29:49 +0000 (00:29 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Mon, 11 Sep 2023 11:09:09 +0000 (20:09 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd.c

index 6e88a0db71962e83c27a30e8a5346eb39ff0a7ea..bb513c07e1d7ca560e189bda5869d60ba19d5ad2 100644 (file)
@@ -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/<task-pid>/. 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);