]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/cgroup.c
detect a cgroup named 'lxc'
[mirror_lxc.git] / src / lxc / cgroup.c
CommitLineData
576f946d 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
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.
13 *
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.
18 *
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
22 */
23#define _GNU_SOURCE
24#include <stdio.h>
25#undef _GNU_SOURCE
26#include <stdlib.h>
27#include <errno.h>
28#include <mntent.h>
29#include <unistd.h>
30#include <string.h>
31#include <fcntl.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/param.h>
35#include <sys/inotify.h>
36#include <netinet/in.h>
37#include <net/if.h>
38
e2bcd7db 39#include "error.h"
36eb9bde 40
576f946d 41#include <lxc/lxc.h>
36eb9bde
CLG
42#include <lxc/log.h>
43
44lxc_log_define(lxc_cgroup, lxc);
576f946d 45
46#define MTAB "/etc/mtab"
47
48static int get_cgroup_mount(const char *mtab, char *mnt)
49{
50 struct mntent *mntent;
51 FILE *file = NULL;
52 int err = -1;
53
54 file = setmntent(mtab, "r");
55 if (!file) {
36eb9bde 56 SYSERROR("failed to open %s", mtab);
576f946d 57 goto out;
58 }
59
60 while ((mntent = getmntent(file))) {
0d9f8e18
DL
61
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);
66 err = 0;
67 break;
68 }
69
70 /* fallback to the first non-lxc cgroup found */
71 if (!strcmp(mntent->mnt_type, "cgroup") && err) {
72 strcpy(mnt, mntent->mnt_dir);
73 err = 0;
74 }
576f946d 75 };
76
77 fclose(file);
78out:
79 return err;
80}
81
6203de18
DL
82int lxc_rename_nsgroup(const char *name, pid_t pid)
83{
84 char oldname[MAXPATHLEN];
85 char newname[MAXPATHLEN];
86 char cgroup[MAXPATHLEN];
2b31f553 87 int ret;
6203de18
DL
88
89 if (get_cgroup_mount(MTAB, cgroup)) {
2b31f553 90 ERROR("cgroup is not mounted");
6203de18
DL
91 return -1;
92 }
93
94 snprintf(oldname, MAXPATHLEN, "%s/%d", cgroup, pid);
95 snprintf(newname, MAXPATHLEN, "%s/%s", cgroup, name);
96
2b31f553
MN
97 ret = rename(oldname, newname);
98 if (ret)
0d9f8e18 99 SYSERROR("failed to rename cgroup %s->%s", oldname, newname);
2b31f553 100 return ret;
6203de18
DL
101}
102
103int lxc_link_nsgroup(const char *name)
576f946d 104{
105 char lxc[MAXPATHLEN];
106 char nsgroup[MAXPATHLEN];
107 char cgroup[MAXPATHLEN];
108 int ret;
109
110 if (get_cgroup_mount(MTAB, cgroup)) {
2b31f553 111 ERROR("cgroup is not mounted");
576f946d 112 return -1;
113 }
114
115 snprintf(lxc, MAXPATHLEN, LXCPATH "/%s/nsgroup", name);
6203de18 116 snprintf(nsgroup, MAXPATHLEN, "%s/%s", cgroup, name);
576f946d 117
118 unlink(lxc);
119 ret = symlink(nsgroup, lxc);
120 if (ret)
0d9f8e18 121 SYSERROR("failed to create symlink %s->%s", nsgroup, lxc);
576f946d 122 return ret;
123}
124
125int lxc_unlink_nsgroup(const char *name)
126{
127 char nsgroup[MAXPATHLEN];
4e2121f5 128 char path[MAXPATHLEN];
2b4e286d 129 ssize_t len;
576f946d 130
131 snprintf(nsgroup, MAXPATHLEN, LXCPATH "/%s/nsgroup", name);
4e2121f5 132
2b4e286d
MN
133 len = readlink(nsgroup, path, MAXPATHLEN-1);
134 if (len > 0) {
135 path[len] = '\0';
4e2121f5 136 rmdir(path);
2b4e286d 137 }
4e2121f5 138
576f946d 139 return unlink(nsgroup);
140}
141
576f946d 142int lxc_cgroup_set(const char *name, const char *subsystem, const char *value)
143{
8b92dc3a 144 int fd, ret = -1;
576f946d 145 char path[MAXPATHLEN];
146
147 snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem);
148
149 fd = open(path, O_WRONLY);
8b92dc3a
MN
150 if (fd < 0) {
151 ERROR("open %s : %s", path, strerror(errno));
576f946d 152 return -1;
8b92dc3a 153 }
576f946d 154
8b92dc3a
MN
155 if (write(fd, value, strlen(value)) < 0) {
156 ERROR("write %s : %s", path, strerror(errno));
576f946d 157 goto out;
8b92dc3a 158 }
576f946d 159
160 ret = 0;
161out:
162 close(fd);
163 return ret;
164}
165
166int lxc_cgroup_get(const char *name, const char *subsystem,
167 char *value, size_t len)
168{
8b92dc3a 169 int fd, ret = -1;
576f946d 170 char path[MAXPATHLEN];
171
172 snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem);
173
174 fd = open(path, O_RDONLY);
8b92dc3a
MN
175 if (fd < 0) {
176 ERROR("open %s : %s", path, strerror(errno));
576f946d 177 return -1;
8b92dc3a 178 }
576f946d 179
8b92dc3a
MN
180 if (read(fd, value, len) < 0) {
181 ERROR("read %s : %s", path, strerror(errno));
576f946d 182 goto out;
8b92dc3a 183 }
576f946d 184
185 ret = 0;
186out:
187 close(fd);
188 return ret;
189}