Commit b48e4cbf authored by Pavel Emelyanov's avatar Pavel Emelyanov

proc: Introduce helper for parsing /proc/$pid/cgroup file

Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent e5eb73ea
......@@ -178,4 +178,23 @@ extern int parse_threads(int pid, struct pid **_t, int *_n);
extern int check_mnt_id(void);
/*
* This struct describes a group controlled by one controller.
* The @name is the controller name or 'name=...' for named cgroups.
* The @path is the path from the hierarchy root.
*/
struct cg_ctl {
struct list_head l;
char *name;
char *path;
};
/*
* Returns the list of cg_ctl-s sorted by name
*/
extern int parse_task_cgroup(int pid, struct list_head *l, unsigned int *n);
extern void put_ctls(struct list_head *);
#endif /* __CR_PROC_PARSE_H__ */
......@@ -1497,3 +1497,62 @@ int parse_threads(int pid, struct pid **_t, int *_n)
return 0;
}
int parse_task_cgroup(int pid, struct list_head *retl, unsigned int *n)
{
int ret = 0;
FILE *f;
f = fopen_proc(pid, "cgroup");
while (fgets(buf, BUF_SIZE, f)) {
struct cg_ctl *ncc, *cc;
char *name, *path, *e;
ret = -1;
ncc = xmalloc(sizeof(*cc));
if (!ncc)
goto err;
name = strchr(buf, ':') + 1;
path = strchr(name, ':');
e = strchr(name, '\n');
*path++ = '\0';
if (e)
*e = '\0';
ncc->name = xstrdup(name);
ncc->path = xstrdup(path);
if (!ncc->name || !ncc->name) {
xfree(ncc->name);
xfree(ncc->path);
xfree(ncc);
goto err;
}
list_for_each_entry(cc, retl, l)
if (strcmp(cc->name, name) >= 0)
break;
list_add_tail(&ncc->l, &cc->l);
(*n)++;
}
fclose(f);
return 0;
err:
put_ctls(retl);
fclose(f);
return ret;
}
void put_ctls(struct list_head *l)
{
struct cg_ctl *c, *n;
list_for_each_entry_safe(c, n, l, l) {
xfree(c->name);
xfree(c->path);
xfree(c);
}
}
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