1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <dlezcano at fr.ibm.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <mntent.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/inotify.h>
#include <netinet/in.h>
#include <net/if.h>
#include "error.h"
#include "config.h"
#include <lxc/log.h>
#include <lxc/cgroup.h>
#include <lxc/start.h>
lxc_log_define(lxc_cgroup, lxc);
#define MTAB "/proc/mounts"
enum {
CGROUP_NS_CGROUP = 1,
CGROUP_CLONE_CHILDREN,
};
static char *hasmntopt_multiple(struct mntent *mntent, const char *options)
{
const char *ptr = options;
const char *ptr2 = strchr(options, ',');
char *result;
while (ptr2 != NULL) {
char *option = strndup(ptr, ptr2 - ptr);
if (!option) {
SYSERROR("Temporary memory allocation error");
return NULL;
}
result = hasmntopt(mntent, option);
free(option);
if (!result) {
return NULL;
}
ptr = ptr2 + 1;
ptr2 = strchr(ptr, ',');
}
/* for multiple mount options, the return value is basically NULL
* or non-NULL, so this should suffice for our purposes */
return hasmntopt(mntent, ptr);
}
static int get_cgroup_mount(const char *subsystem, char *mnt)
{
struct mntent *mntent;
FILE *file = NULL;
file = setmntent(MTAB, "r");
if (!file) {
SYSERROR("failed to open %s", MTAB);
return -1;
}
while ((mntent = getmntent(file))) {
if (strcmp(mntent->mnt_type, "cgroup"))
continue;
if (!subsystem || hasmntopt_multiple(mntent, subsystem)) {
strcpy(mnt, mntent->mnt_dir);
fclose(file);
DEBUG("using cgroup mounted at '%s'", mnt);
return 0;
}
};
DEBUG("Failed to find cgroup for %s\n", subsystem ? subsystem : "(NULL)");
fclose(file);
return -1;
}
int lxc_ns_is_mounted(void)
{
static char buf[MAXPATHLEN];
return (get_cgroup_mount("ns", buf) == 0);
}
static int get_cgroup_flags(struct mntent *mntent)
{
int flags = 0;
if (hasmntopt(mntent, "ns"))
flags |= CGROUP_NS_CGROUP;
if (hasmntopt(mntent, "clone_children"))
flags |= CGROUP_CLONE_CHILDREN;
DEBUG("cgroup %s has flags 0x%x", mntent->mnt_dir, flags);
return flags;
}
static int cgroup_rename_nsgroup(const char *mnt, const char *name, pid_t pid)
{
char oldname[MAXPATHLEN];
snprintf(oldname, MAXPATHLEN, "%s/%d", mnt, pid);
if (rename(oldname, name)) {
SYSERROR("failed to rename cgroup %s->%s", oldname, name);
return -1;
}
DEBUG("'%s' renamed to '%s'", oldname, name);
return 0;
}
static int cgroup_enable_clone_children(const char *path)
{
FILE *f;
int ret = 0;
f = fopen(path, "w");
if (!f) {
SYSERROR("failed to open '%s'", path);
return -1;
}
if (fprintf(f, "1") < 1) {
ERROR("failed to write flag to '%s'", path);
ret = -1;
}
fclose(f);
return ret;
}
int lxc_cgroup_attach(const char *path, pid_t pid)
{
FILE *f;
char tasks[MAXPATHLEN];
int ret = 0;
snprintf(tasks, MAXPATHLEN, "%s/tasks", path);
f = fopen(tasks, "w");
if (!f) {
SYSERROR("failed to open '%s'", tasks);
return -1;
}
if (fprintf(f, "%d", pid) <= 0) {
SYSERROR("failed to write pid '%d' to '%s'", pid, tasks);
ret = -1;
}
fclose(f);
return ret;
}
/*
* create a cgroup for the container in a particular subsystem.
* XXX TODO we will of course want to use cgroup_path{subsystem}/lxc/name,
* not just cgroup_path{subsystem}/name.
*/
static int lxc_one_cgroup_create(const char *name,
struct mntent *mntent, pid_t pid)
{
char cgname[MAXPATHLEN];
char clonechild[MAXPATHLEN];
int flags;
snprintf(cgname, MAXPATHLEN, "%s/%s", mntent->mnt_dir, name);
/*
* There is a previous cgroup, assume it is empty,
* otherwise that fails
*/
if (!access(cgname, F_OK) && rmdir(cgname)) {
SYSERROR("failed to remove previous cgroup '%s'", cgname);
return -1;
}
flags = get_cgroup_flags(mntent);
/* We have the deprecated ns_cgroup subsystem */
if (flags & CGROUP_NS_CGROUP) {
WARN("using deprecated ns_cgroup");
return cgroup_rename_nsgroup(mntent->mnt_dir, cgname, pid);
}
snprintf(clonechild, MAXPATHLEN, "%s/cgroup.clone_children",
mntent->mnt_dir);
/* we check if the kernel has clone_children, at this point if there
* no clone_children neither ns_cgroup, that means the cgroup is mounted
* without the ns_cgroup and it has not the compatibility flag
*/
if (access(clonechild, F_OK)) {
ERROR("no ns_cgroup option specified");
return -1;
}
/* we enable the clone_children flag of the cgroup */
if (cgroup_enable_clone_children(clonechild)) {
SYSERROR("failed to enable 'clone_children flag");
return -1;
}
/* Let's create the cgroup */
if (mkdir(cgname, 0700)) {
SYSERROR("failed to create '%s' directory", cgname);
return -1;
}
/* Let's add the pid to the 'tasks' file */
if (lxc_cgroup_attach(cgname, pid)) {
SYSERROR("failed to attach pid '%d' to '%s'", pid, cgname);
rmdir(cgname);
return -1;
}
INFO("created cgroup '%s'", cgname);
return 0;
}
/*
* for each mounted cgroup, create a cgroup for the container
*/
int lxc_cgroup_create(const char *name, pid_t pid)
{
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);
err = lxc_one_cgroup_create(name, mntent, pid);
if (err)
goto out;
}
};
if (!found)
ERROR("No cgroup mounted on the system");
out:
endmntent(file);
return err;
}
int lxc_one_cgroup_destroy(const char *cgmnt, const char *name)
{
char cgname[MAXPATHLEN];
snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name);
if (rmdir(cgname)) {
SYSERROR("failed to remove cgroup '%s'", cgname);
return -1;
}
DEBUG("'%s' unlinked", cgname);
return 0;
}
/*
* for each mounted cgroup, destroy the cgroup for the container
*/
int lxc_cgroup_destroy(const char *name)
{
struct mntent *mntent;
FILE *file = NULL;
int ret, err = -1;
file = setmntent(MTAB, "r");
if (!file) {
SYSERROR("failed to open %s", MTAB);
return -1;
}
while ((mntent = getmntent(file))) {
if (!strcmp(mntent->mnt_type, "cgroup")) {
DEBUG("destroying %s %s\n", mntent->mnt_dir, name);
ret = lxc_one_cgroup_destroy(mntent->mnt_dir, name);
if (ret) {
fclose(file);
return ret;
}
err = 0;
}
}
fclose(file);
return err;
}
/*
* lxc_cgroup_path_get: put into *path the pathname for
* %subsystem and cgroup %name. If %subsystem is NULL, then
* the first mounted cgroup will be used (for nr_tasks)
*/
int lxc_cgroup_path_get(char **path, const char *subsystem, const char *name)
{
static char buf[MAXPATHLEN];
static char retbuf[MAXPATHLEN];
/* what lxc_cgroup_set calls subsystem is actually the filename, i.e.
'devices.allow'. So for our purposee we trim it */
if (subsystem) {
snprintf(retbuf, MAXPATHLEN, "%s", subsystem);
char *s = index(retbuf, '.');
if (s)
*s = '\0';
DEBUG("%s: called for subsys %s name %s\n", __func__, retbuf, name);
}
if (get_cgroup_mount(subsystem ? retbuf : NULL, buf)) {
ERROR("cgroup is not mounted");
return -1;
}
snprintf(retbuf, MAXPATHLEN, "%s/%s", buf, name);
DEBUG("%s: returning %s for subsystem %s", __func__, retbuf, subsystem);
*path = retbuf;
return 0;
}
int lxc_cgroup_set(const char *name, const char *filename, const char *value)
{
int fd, ret;
char *dirpath;
char path[MAXPATHLEN];
ret = lxc_cgroup_path_get(&dirpath, filename, name);
if (ret)
return -1;
snprintf(path, MAXPATHLEN, "%s/%s", dirpath, filename);
fd = open(path, O_WRONLY);
if (fd < 0) {
ERROR("open %s : %s", path, strerror(errno));
return -1;
}
ret = write(fd, value, strlen(value));
if (ret < 0) {
ERROR("write %s : %s", path, strerror(errno));
goto out;
}
ret = 0;
out:
close(fd);
return ret;
}
int lxc_cgroup_get(const char *name, const char *filename,
char *value, size_t len)
{
int fd, ret = -1;
char *dirpath;
char path[MAXPATHLEN];
ret = lxc_cgroup_path_get(&dirpath, filename, name);
if (ret)
return -1;
snprintf(path, MAXPATHLEN, "%s/%s", dirpath, filename);
fd = open(path, O_RDONLY);
if (fd < 0) {
ERROR("open %s : %s", path, strerror(errno));
return -1;
}
ret = read(fd, value, len);
if (ret < 0)
ERROR("read %s : %s", path, strerror(errno));
close(fd);
return ret;
}
int lxc_cgroup_nrtasks(const char *name)
{
char *dpath;
char path[MAXPATHLEN];
int pid, ret, count = 0;
FILE *file;
ret = lxc_cgroup_path_get(&dpath, NULL, name);
if (ret)
return -1;
snprintf(path, MAXPATHLEN, "%s/tasks", dpath);
file = fopen(path, "r");
if (!file) {
SYSERROR("fopen '%s' failed", path);
return -1;
}
while (fscanf(file, "%d", &pid) != EOF)
count++;
fclose(file);
return count;
}
|