]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroup.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/inotify.h>
36 #include <netinet/in.h>
44 lxc_log_define(lxc_cgroup
, lxc
);
46 #define MTAB "/etc/mtab"
48 static int get_cgroup_mount(const char *mtab
, char *mnt
)
50 struct mntent
*mntent
;
54 file
= setmntent(mtab
, "r");
56 SYSERROR("failed to open %s", mtab
);
60 while ((mntent
= getmntent(file
))) {
62 /* there is a cgroup mounted named "lxc" */
63 if (!strcmp(mntent
->mnt_fsname
, "lxc") &&
64 !strcmp(mntent
->mnt_type
, "cgroup")) {
65 strcpy(mnt
, mntent
->mnt_dir
);
70 /* fallback to the first non-lxc cgroup found */
71 if (!strcmp(mntent
->mnt_type
, "cgroup") && err
) {
72 strcpy(mnt
, mntent
->mnt_dir
);
77 DEBUG("using cgroup mounted at '%s'", mnt
);
84 int lxc_rename_nsgroup(const char *name
, pid_t pid
)
86 char oldname
[MAXPATHLEN
];
87 char newname
[MAXPATHLEN
];
88 char cgroup
[MAXPATHLEN
];
91 if (get_cgroup_mount(MTAB
, cgroup
)) {
92 ERROR("cgroup is not mounted");
96 snprintf(oldname
, MAXPATHLEN
, "%s/%d", cgroup
, pid
);
97 snprintf(newname
, MAXPATHLEN
, "%s/%s", cgroup
, name
);
99 /* there is a previous cgroup, assume it is empty, otherwise
101 if (!access(newname
, F_OK
)) {
102 ret
= rmdir(newname
);
104 SYSERROR("failed to remove previous cgroup '%s'",
110 ret
= rename(oldname
, newname
);
112 SYSERROR("failed to rename cgroup %s->%s", oldname
, newname
);
114 DEBUG("'%s' renamed to '%s'", oldname
, newname
);
119 int lxc_link_nsgroup(const char *name
)
121 char lxc
[MAXPATHLEN
];
122 char nsgroup
[MAXPATHLEN
];
123 char cgroup
[MAXPATHLEN
];
126 if (get_cgroup_mount(MTAB
, cgroup
)) {
127 ERROR("cgroup is not mounted");
131 snprintf(lxc
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup", name
);
132 snprintf(nsgroup
, MAXPATHLEN
, "%s/%s", cgroup
, name
);
135 ret
= symlink(nsgroup
, lxc
);
137 SYSERROR("failed to create symlink %s->%s", nsgroup
, lxc
);
139 DEBUG("'%s' linked to '%s'", nsgroup
, lxc
);
144 int lxc_unlink_nsgroup(const char *name
)
146 char nsgroup
[MAXPATHLEN
];
147 char path
[MAXPATHLEN
];
150 snprintf(nsgroup
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup", name
);
152 len
= readlink(nsgroup
, path
, MAXPATHLEN
-1);
158 DEBUG("unlinking '%s'", nsgroup
);
160 return unlink(nsgroup
);
163 int lxc_cgroup_set(const char *name
, const char *subsystem
, const char *value
)
166 char path
[MAXPATHLEN
];
168 snprintf(path
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup/%s", name
, subsystem
);
170 fd
= open(path
, O_WRONLY
);
172 ERROR("open %s : %s", path
, strerror(errno
));
176 if (write(fd
, value
, strlen(value
)) < 0) {
177 ERROR("write %s : %s", path
, strerror(errno
));
187 int lxc_cgroup_get(const char *name
, const char *subsystem
,
188 char *value
, size_t len
)
191 char path
[MAXPATHLEN
];
193 snprintf(path
, MAXPATHLEN
, LXCPATH
"/%s/nsgroup/%s", name
, subsystem
);
195 fd
= open(path
, O_RDONLY
);
197 ERROR("open %s : %s", path
, strerror(errno
));
201 if (read(fd
, value
, len
) < 0) {
202 ERROR("read %s : %s", path
, strerror(errno
));