[lxc-devel] [PATCH 2/4] Add lxc_cgroup_attach function
Christian Seiler
christian at iwakd.de
Fri Feb 3 12:54:19 UTC 2012
This commit adds the lxc_cgroup_attach function that adds a pid to the tasks
file of a specific cgroup in all subsystems. This is required for lxc-attach
to be able to put newly started processes in the same cgroup as the
container.
---
src/lxc/cgroup.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
src/lxc/cgroup.h | 1 +
2 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index 6ae67bd..db5d2fa 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -433,3 +433,50 @@ int lxc_cgroup_nrtasks(const char *name)
return count;
}
+
+int lxc_cgroup_attach(const char *name, pid_t pid)
+{
+ char cgname[MAXPATHLEN];
+ struct mntent *mntent;
+ FILE *file = NULL;
+ int err = -1;
+ int found = 0;
+
+ file = setmntent(MTAB, "r");
+ if (!file) {
+ SYSERROR("failed to open %s", MTAB);
+ return -1;
+ }
+
+ while ((mntent = getmntent(file))) {
+
+ DEBUG("checking '%s' (%s)", mntent->mnt_dir, mntent->mnt_type);
+
+ if (!strcmp(mntent->mnt_type, "cgroup")) {
+
+ INFO("[%d] found cgroup mounted at '%s',opts='%s'",
+ ++found, mntent->mnt_dir, mntent->mnt_opts);
+
+ snprintf(cgname, MAXPATHLEN, "%s/%s",
+ mntent->mnt_dir, name);
+
+ if (access(cgname, F_OK)) {
+ ERROR("No cgroup '%s' found "
+ "in subsystem mounted at '%s'",
+ name, mntent->mnt_dir);
+ goto out;
+ }
+
+ err = cgroup_attach(cgname, pid);
+ if (err)
+ goto out;
+ }
+ };
+
+ if (!found)
+ ERROR("No cgroup mounted on the system");
+
+out:
+ endmntent(file);
+ return err;
+}
diff --git a/src/lxc/cgroup.h b/src/lxc/cgroup.h
index 31dd2de..3c90696 100644
--- a/src/lxc/cgroup.h
+++ b/src/lxc/cgroup.h
@@ -30,5 +30,6 @@ extern int lxc_cgroup_create(const char *name, pid_t pid);
extern int lxc_cgroup_destroy(const char *name);
extern int lxc_cgroup_path_get(char **path, const char *subsystem, const char *name);
extern int lxc_cgroup_nrtasks(const char *name);
+extern int lxc_cgroup_attach(const char *name, pid_t pid);
extern int lxc_ns_is_mounted(void);
#endif
--
1.7.2.5
More information about the lxc-devel
mailing list