[libvirt] [PATCH v2 1/2] Add virProcessGetPids to get all tasks of a process

Cédric Bosdonnat cbosdonnat at suse.com
Thu Jun 11 06:57:54 UTC 2015


This function gets all the PIDs listed in /proc/PID/task. This will be
needed at least to move all qmeu-nbd tasks to the container cgroup.
---
 src/libvirt_private.syms |  1 +
 src/util/virprocess.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virprocess.h    |  2 ++
 3 files changed, 50 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6a95fb9..780cfbb 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1986,6 +1986,7 @@ virProcessAbort;
 virProcessExitWithStatus;
 virProcessGetAffinity;
 virProcessGetNamespaces;
+virProcessGetPids;
 virProcessGetStartTime;
 virProcessKill;
 virProcessKillPainfully;
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index 7a79970..ce5e106 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -608,6 +608,53 @@ int virProcessGetAffinity(pid_t pid ATTRIBUTE_UNUSED,
 #endif /* HAVE_SCHED_GETAFFINITY */
 
 
+int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
+{
+    int ret = -1;
+    char *taskPath = NULL;
+    DIR *dir = NULL;
+    int value;
+    struct dirent *ent;
+
+    *npids = 0;
+    *pids = NULL;
+
+    if (virAsprintf(&taskPath, "/proc/%llu/task",
+                    (unsigned long long)pid) < 0)
+        goto cleanup;
+
+    if (!(dir = opendir(taskPath)))
+        goto cleanup;
+
+    while ((value = virDirRead(dir, &ent, taskPath)) > 0) {
+        pid_t tmp_pid;
+
+        /* Skip . and .. */
+        if (STRPREFIX(ent->d_name, "."))
+            continue;
+
+        if (virStrToLong_i(ent->d_name, NULL, 10, &tmp_pid) < 0)
+            goto cleanup;
+
+        if (VIR_APPEND_ELEMENT(*pids, *npids, tmp_pid) < 0)
+            goto cleanup;
+    }
+
+    if (value < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    if (!dir)
+        closedir(dir);
+    VIR_FREE(taskPath);
+    if (ret < 0)
+        VIR_FREE(*pids);
+    return ret;
+}
+
+
 int virProcessGetNamespaces(pid_t pid,
                             size_t *nfdlist,
                             int **fdlist)
diff --git a/src/util/virprocess.h b/src/util/virprocess.h
index c812882..86a633d 100644
--- a/src/util/virprocess.h
+++ b/src/util/virprocess.h
@@ -62,6 +62,8 @@ int virProcessGetAffinity(pid_t pid,
                           virBitmapPtr *map,
                           int maxcpu);
 
+int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids);
+
 int virProcessGetStartTime(pid_t pid,
                            unsigned long long *timestamp);
 
-- 
2.1.4




More information about the libvir-list mailing list