]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
count the number of tasks in the container
authorDaniel Lezcano <daniel.lezcano@free.fr>
Thu, 8 Apr 2010 07:44:23 +0000 (09:44 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 8 Apr 2010 07:44:23 +0000 (09:44 +0200)
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 <dlezcano@fr.ibm.com>
src/lxc/cgroup.c
src/lxc/cgroup.h

index 55aa8b22f82ad7d04fda14ce01799de4ca89ff53..cc57e71fc712bc0f00733d11849490f13d6b6277 100644 (file)
@@ -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;
+}
index 88791bed802b0d681c5246ba164db37837dfd58e..640f1d24514a83f8b852510828b36c5008168dd7 100644 (file)
@@ -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