From: Daniel Lezcano Date: Thu, 8 Apr 2010 07:44:23 +0000 (+0200) Subject: count the number of tasks in the container X-Git-Tag: lxc-0.7.0~112 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0f888d910c2680acf6267391f88ab75ef66771f;p=thirdparty%2Flxc.git count the number of tasks in the container This patch adds a function to count the number of tasks in the container. The result is not reliable as it may change with a fork or an exit, but in some cases, for example, there is only one task, or the container is frozen, the result is accurate. Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c index 55aa8b22f..cc57e71fc 100644 --- a/src/lxc/cgroup.c +++ b/src/lxc/cgroup.c @@ -219,3 +219,30 @@ int lxc_cgroup_get(const char *name, const char *subsystem, close(fd); return ret; } + +int lxc_cgroup_nrtasks(const char *name) +{ + char *nsgroup; + char path[MAXPATHLEN]; + int pid, ret, count = 0; + FILE *file; + + ret = lxc_cgroup_path_get(&nsgroup, name); + if (ret) + return -1; + + snprintf(path, MAXPATHLEN, "%s/tasks", nsgroup); + + file = fopen(path, "r"); + if (!file) { + SYSERROR("fopen '%s' failed", path); + return -1; + } + + while (fscanf(file, "%d", &pid) != EOF) + count++; + + fclose(file); + + return count; +} diff --git a/src/lxc/cgroup.h b/src/lxc/cgroup.h index 88791bed8..640f1d245 100644 --- a/src/lxc/cgroup.h +++ b/src/lxc/cgroup.h @@ -29,5 +29,5 @@ struct lxc_handler; int lxc_rename_nsgroup(const char *name, struct lxc_handler *handler); int lxc_unlink_nsgroup(const char *name); int lxc_cgroup_path_get(char **path, const char *name); - +int lxc_cgroup_nrtasks(const char *name); #endif