]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/cgroup-label.c
nspawn: introduce the new /machine/ tree in the cgroup tree and move containers there
[thirdparty/systemd.git] / src / shared / cgroup-label.c
CommitLineData
cc527a47
KS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
cc527a47
KS
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 16 Lesser General Public License for more details.
cc527a47 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
cc527a47
KS
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <unistd.h>
24#include <signal.h>
25#include <string.h>
26#include <stdlib.h>
27#include <dirent.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <ftw.h>
31
32#include "cgroup-util.h"
33#include "log.h"
34#include "set.h"
35#include "macro.h"
36#include "util.h"
37#include "mkdir.h"
38
a32360f1 39int cg_create(const char *controller, const char *path, const char *suffix) {
974efc46 40 _cleanup_free_ char *fs = NULL;
cc527a47
KS
41 int r;
42
a32360f1 43 r = cg_get_path_and_check(controller, path, suffix, &fs);
3474ae3c 44 if (r < 0)
cc527a47
KS
45 return r;
46
d2e54fae 47 r = mkdir_parents_label(fs, 0755);
974efc46
LP
48 if (r < 0)
49 return r;
cc527a47 50
974efc46 51 if (mkdir(fs, 0755) < 0) {
cc527a47 52
974efc46
LP
53 if (errno == EEXIST)
54 return 0;
cc527a47 55
974efc46
LP
56 return -errno;
57 }
58
59 return 1;
cc527a47
KS
60}
61
62int cg_create_and_attach(const char *controller, const char *path, pid_t pid) {
63 int r, q;
64
cc527a47
KS
65 assert(pid >= 0);
66
a32360f1 67 r = cg_create(controller, path, NULL);
974efc46 68 if (r < 0)
cc527a47
KS
69 return r;
70
974efc46
LP
71 q = cg_attach(controller, path, pid);
72 if (q < 0)
cc527a47
KS
73 return q;
74
75 /* This does not remove the cgroup on failure */
cc527a47
KS
76 return r;
77}