]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroups/cgroup.c
cgroup: check for non-empty conf
[mirror_lxc.git] / src / lxc / cgroups / cgroup.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include "cgroup.h"
33 #include "conf.h"
34 #include "config.h"
35 #include "initutils.h"
36 #include "log.h"
37 #include "start.h"
38
39 lxc_log_define(cgroup, lxc);
40
41 extern struct cgroup_ops *cgfsng_ops_init(struct lxc_conf *conf);
42
43 struct cgroup_ops *cgroup_init(struct lxc_conf *conf)
44 {
45 struct cgroup_ops *cgroup_ops;
46
47 if (!conf) {
48 ERROR("No valid conf given");
49 return NULL;
50 }
51
52 cgroup_ops = cgfsng_ops_init(conf);
53 if (!cgroup_ops) {
54 ERROR("Failed to initialize cgroup driver");
55 return NULL;
56 }
57
58 if (!cgroup_ops->data_init(cgroup_ops))
59 return NULL;
60
61 TRACE("Initialized cgroup driver %s", cgroup_ops->driver);
62
63 if (cgroup_ops->cgroup_layout == CGROUP_LAYOUT_LEGACY)
64 TRACE("Running with legacy cgroup layout");
65 else if (cgroup_ops->cgroup_layout == CGROUP_LAYOUT_HYBRID)
66 TRACE("Running with hybrid cgroup layout");
67 else if (cgroup_ops->cgroup_layout == CGROUP_LAYOUT_UNIFIED)
68 TRACE("Running with unified cgroup layout");
69 else
70 WARN("Running with unknown cgroup layout");
71
72 return cgroup_ops;
73 }
74
75 void cgroup_exit(struct cgroup_ops *ops)
76 {
77 char **cur;
78 struct hierarchy **it;
79
80 if (!ops)
81 return;
82
83 for (cur = ops->cgroup_use; cur && *cur; cur++)
84 free(*cur);
85
86 free(ops->cgroup_pattern);
87 free(ops->container_cgroup);
88
89 for (it = ops->hierarchies; it && *it; it++) {
90 char **p;
91
92 for (p = (*it)->controllers; p && *p; p++)
93 free(*p);
94 free((*it)->controllers);
95
96 for (p = (*it)->cgroup2_chown; p && *p; p++)
97 free(*p);
98 free((*it)->cgroup2_chown);
99
100 free((*it)->mountpoint);
101 free((*it)->container_base_path);
102 free((*it)->container_full_path);
103 free((*it)->monitor_full_path);
104 free(*it);
105 }
106 free(ops->hierarchies);
107
108 free(ops);
109
110 return;
111 }
112
113 #define INIT_SCOPE "/init.scope"
114 void prune_init_scope(char *cg)
115 {
116 char *point;
117
118 if (!cg)
119 return;
120
121 point = cg + strlen(cg) - strlen(INIT_SCOPE);
122 if (point < cg)
123 return;
124
125 if (strcmp(point, INIT_SCOPE) == 0) {
126 if (point == cg)
127 *(point + 1) = '\0';
128 else
129 *point = '\0';
130 }
131 }