diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index dc8a52dc4421b7331069b1f0030c54e0427f7608..f7373afff2979aeeb77152295a5c331a4d8c225d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1988,6 +1988,7 @@ virProcessAbort; virProcessExitWithStatus; virProcessGetAffinity; virProcessGetNamespaces; +virProcessGetPids; virProcessGetStartTime; virProcessKill; virProcessKillPainfully; diff --git a/src/util/virprocess.c b/src/util/virprocess.c index b47df0f349dbb2c9ffeaa0223e2567084a14b0a4..f1924eb5c34d204eddf07ccf00bdf58cd2692d68 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -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) diff --git a/src/util/virprocess.h b/src/util/virprocess.h index 1b984933a89bc051815948a03f782b6002f672fc..1768009d376839f4b8e70ef46867b703fb41cfb6 100644 --- a/src/util/virprocess.h +++ b/src/util/virprocess.h @@ -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);