Commit e0f888d9 by Daniel Lezcano Committed by Daniel Lezcano

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: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent f78a1f32
......@@ -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;
}
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment