]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virProcessGetPids to get all tasks of a process
authorCédric Bosdonnat <cbosdonnat@suse.com>
Mon, 1 Jun 2015 12:31:44 +0000 (14:31 +0200)
committerCédric Bosdonnat <cbosdonnat@suse.com>
Tue, 16 Jun 2015 10:38:03 +0000 (12:38 +0200)
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
src/util/virprocess.c
src/util/virprocess.h

index dc8a52dc4421b7331069b1f0030c54e0427f7608..f7373afff2979aeeb77152295a5c331a4d8c225d 100644 (file)
@@ -1988,6 +1988,7 @@ virProcessAbort;
 virProcessExitWithStatus;
 virProcessGetAffinity;
 virProcessGetNamespaces;
+virProcessGetPids;
 virProcessGetStartTime;
 virProcessKill;
 virProcessKillPainfully;
index b47df0f349dbb2c9ffeaa0223e2567084a14b0a4..f1924eb5c34d204eddf07ccf00bdf58cd2692d68 100644 (file)
@@ -600,6 +600,53 @@ 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)
index 1b984933a89bc051815948a03f782b6002f672fc..1768009d376839f4b8e70ef46867b703fb41cfb6 100644 (file)
@@ -60,6 +60,8 @@ int virProcessSetAffinity(pid_t pid, virBitmapPtr map);
 
 virBitmapPtr virProcessGetAffinity(pid_t pid);
 
+int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids);
+
 int virProcessGetStartTime(pid_t pid,
                            unsigned long long *timestamp);