]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/cgroups/cgroup.c
cgroups: use __do_free in cgfsng_attach()
[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 cgroup_ops = cgfsng_ops_init(conf);
48 if (!cgroup_ops) {
49 ERROR("Failed to initialize cgroup driver");
50 return NULL;
51 }
52
53 if (!cgroup_ops->data_init(cgroup_ops))
54 return NULL;
55
56 TRACE("Initialized cgroup driver %s", cgroup_ops->driver);
57
58 if (cgroup_ops->cgroup_layout == CGROUP_LAYOUT_LEGACY)
59 TRACE("Running with legacy cgroup layout");
60 else if (cgroup_ops->cgroup_layout == CGROUP_LAYOUT_HYBRID)
61 TRACE("Running with hybrid cgroup layout");
62 else if (cgroup_ops->cgroup_layout == CGROUP_LAYOUT_UNIFIED)
63 TRACE("Running with unified cgroup layout");
64 else
65 WARN("Running with unknown cgroup layout");
66
67 return cgroup_ops;
68 }
69
70 void cgroup_exit(struct cgroup_ops *ops)
71 {
72 char **cur;
73 struct hierarchy **it;
74
75 if (!ops)
76 return;
77
78 for (cur = ops->cgroup_use; cur && *cur; cur++)
79 free(*cur);
80
81 free(ops->cgroup_pattern);
82 free(ops->container_cgroup);
83
84 for (it = ops->hierarchies; it && *it; it++) {
85 char **p;
86
87 for (p = (*it)->controllers; p && *p; p++)
88 free(*p);
89 free((*it)->controllers);
90
91 for (p = (*it)->cgroup2_chown; p && *p; p++)
92 free(*p);
93 free((*it)->cgroup2_chown);
94
95 free((*it)->mountpoint);
96 free((*it)->container_base_path);
97 free((*it)->container_full_path);
98 free((*it)->monitor_full_path);
99 free(*it);
100 }
101 free(ops->hierarchies);
102
103 free(ops);
104
105 return;
106 }
107
108 #define INIT_SCOPE "/init.scope"
109 void prune_init_scope(char *cg)
110 {
111 char *point;
112
113 if (!cg)
114 return;
115
116 point = cg + strlen(cg) - strlen(INIT_SCOPE);
117 if (point < cg)
118 return;
119
120 if (strcmp(point, INIT_SCOPE) == 0) {
121 if (point == cg)
122 *(point + 1) = '\0';
123 else
124 *point = '\0';
125 }
126 }