]>
Commit | Line | Data |
---|---|---|
c2cc9f0a | 1 | /* |
2 | * lxc: linux Container library | |
c2cc9f0a | 3 | * (C) Copyright IBM Corp. 2007, 2008 |
4 | * | |
5 | * Authors: | |
9afe19d6 | 6 | * Daniel Lezcano <daniel.lezcano at free.fr> |
70c1e708 CB |
7 | * Serge Hallyn <serge@hallyn.com> |
8 | * Christian Brauner <christian.brauner@ubuntu.com> | |
c2cc9f0a | 9 | * |
10 | * This library is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2.1 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This library is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * Lesser General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public | |
21 | * License along with this library; if not, write to the Free Software | |
250b1eec | 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
c2cc9f0a | 23 | */ |
70c1e708 | 24 | |
d38dd64a CB |
25 | #ifndef _GNU_SOURCE |
26 | #define _GNU_SOURCE 1 | |
27 | #endif | |
70c1e708 | 28 | #define __STDC_FORMAT_MACROS |
d38dd64a | 29 | #include <arpa/inet.h> |
70c1e708 CB |
30 | #include <ctype.h> |
31 | #include <dirent.h> | |
32 | #include <errno.h> | |
33 | #include <fcntl.h> | |
34 | #include <inttypes.h> | |
d38dd64a CB |
35 | #include <net/if.h> |
36 | #include <netinet/in.h> | |
70c1e708 | 37 | #include <signal.h> |
c2cc9f0a | 38 | #include <stdio.h> |
39 | #include <stdlib.h> | |
40 | #include <string.h> | |
70c1e708 | 41 | #include <sys/param.h> |
63376d7d | 42 | #include <sys/stat.h> |
c2cc9f0a | 43 | #include <sys/types.h> |
c2cc9f0a | 44 | #include <sys/utsname.h> |
d38dd64a CB |
45 | #include <syslog.h> |
46 | #include <time.h> | |
47 | #include <unistd.h> | |
c2cc9f0a | 48 | |
70c1e708 | 49 | #include "conf.h" |
6ff05e18 | 50 | #include "config.h" |
525f0002 | 51 | #include "confile.h" |
0b843d35 | 52 | #include "confile_utils.h" |
d38dd64a | 53 | #include "../include/netns_ifaddrs.h" |
f2363e38 | 54 | #include "log.h" |
58e0f57d | 55 | #include "lxcseccomp.h" |
70c1e708 CB |
56 | #include "network.h" |
57 | #include "parse.h" | |
28d832c4 | 58 | #include "storage.h" |
70c1e708 | 59 | #include "utils.h" |
36eb9bde | 60 | |
6ff05e18 SG |
61 | #if HAVE_SYS_PERSONALITY_H |
62 | #include <sys/personality.h> | |
63 | #endif | |
64 | ||
43f984ea DJ |
65 | #ifndef HAVE_STRLCPY |
66 | #include "include/strlcpy.h" | |
67 | #endif | |
68 | ||
ebcd5140 DJ |
69 | #ifndef HAVE_STRLCAT |
70 | #include "include/strlcat.h" | |
71 | #endif | |
72 | ||
ac2cecc4 | 73 | lxc_log_define(confile, lxc); |
576f946d | 74 | |
afeec9b7 CB |
75 | #define lxc_config_define(name) \ |
76 | __hot static int set_config_##name(const char *, const char *, \ | |
77 | struct lxc_conf *, void *); \ | |
78 | __hot static int get_config_##name(const char *, char *, int, \ | |
79 | struct lxc_conf *, void *); \ | |
80 | __hot static int clr_config_##name(const char *, struct lxc_conf *, \ | |
81 | void *); | |
71e287ca | 82 | |
63bab717 | 83 | lxc_config_define(autodev); |
71e287ca | 84 | lxc_config_define(apparmor_allow_incomplete); |
1800f924 | 85 | lxc_config_define(apparmor_allow_nesting); |
63bab717 | 86 | lxc_config_define(apparmor_profile); |
1800f924 | 87 | lxc_config_define(apparmor_raw); |
63bab717 CB |
88 | lxc_config_define(cap_drop); |
89 | lxc_config_define(cap_keep); | |
43654d34 | 90 | lxc_config_define(cgroup_controller); |
54860ed0 | 91 | lxc_config_define(cgroup2_controller); |
43654d34 | 92 | lxc_config_define(cgroup_dir); |
9caee129 | 93 | lxc_config_define(cgroup_relative); |
28f3b1cd | 94 | lxc_config_define(console_buffer_size); |
861813e5 | 95 | lxc_config_define(console_logfile); |
63bab717 | 96 | lxc_config_define(console_path); |
861813e5 CB |
97 | lxc_config_define(console_rotate); |
98 | lxc_config_define(console_size); | |
63bab717 CB |
99 | lxc_config_define(environment); |
100 | lxc_config_define(ephemeral); | |
101 | lxc_config_define(execute_cmd); | |
102 | lxc_config_define(group); | |
103 | lxc_config_define(hooks); | |
44ae0fb6 | 104 | lxc_config_define(hooks_version); |
71e287ca | 105 | lxc_config_define(idmaps); |
63bab717 CB |
106 | lxc_config_define(includefiles); |
107 | lxc_config_define(init_cmd); | |
3c491553 | 108 | lxc_config_define(init_cwd); |
63bab717 CB |
109 | lxc_config_define(init_gid); |
110 | lxc_config_define(init_uid); | |
46cc906d | 111 | lxc_config_define(log_file); |
63bab717 CB |
112 | lxc_config_define(log_level); |
113 | lxc_config_define(log_syslog); | |
114 | lxc_config_define(monitor); | |
258f8051 | 115 | lxc_config_define(monitor_signal_pdeath); |
71e287ca | 116 | lxc_config_define(mount); |
117 | lxc_config_define(mount_auto); | |
47148e96 | 118 | lxc_config_define(mount_fstab); |
1d8d3676 | 119 | lxc_config_define(namespace_clone); |
abeb5bba | 120 | lxc_config_define(namespace_keep); |
b074bbf1 | 121 | lxc_config_define(namespace_share); |
63bab717 | 122 | lxc_config_define(net); |
71e287ca | 123 | lxc_config_define(net_flags); |
71e287ca | 124 | lxc_config_define(net_hwaddr); |
9ff60df2 | 125 | lxc_config_define(net_ipv4_address); |
71e287ca | 126 | lxc_config_define(net_ipv4_gateway); |
2e44ae28 | 127 | lxc_config_define(net_ipv6_address); |
71e287ca | 128 | lxc_config_define(net_ipv6_gateway); |
63bab717 CB |
129 | lxc_config_define(net_link); |
130 | lxc_config_define(net_macvlan_mode); | |
131 | lxc_config_define(net_mtu); | |
132 | lxc_config_define(net_name); | |
71e287ca | 133 | lxc_config_define(net_nic); |
63bab717 CB |
134 | lxc_config_define(net_script_down); |
135 | lxc_config_define(net_script_up); | |
136 | lxc_config_define(net_type); | |
137 | lxc_config_define(net_veth_pair); | |
138 | lxc_config_define(net_vlan_id); | |
139 | lxc_config_define(no_new_privs); | |
63bab717 CB |
140 | lxc_config_define(personality); |
141 | lxc_config_define(prlimit); | |
142 | lxc_config_define(pty_max); | |
6e54330c | 143 | lxc_config_define(rootfs_managed); |
63bab717 CB |
144 | lxc_config_define(rootfs_mount); |
145 | lxc_config_define(rootfs_options); | |
146 | lxc_config_define(rootfs_path); | |
0b427da0 | 147 | lxc_config_define(seccomp_profile); |
63bab717 | 148 | lxc_config_define(selinux_context); |
55c84efc | 149 | lxc_config_define(signal_halt); |
150 | lxc_config_define(signal_reboot); | |
151 | lxc_config_define(signal_stop); | |
71e287ca | 152 | lxc_config_define(start); |
63bab717 CB |
153 | lxc_config_define(tty_max); |
154 | lxc_config_define(tty_dir); | |
155 | lxc_config_define(uts_name); | |
7edd0540 | 156 | lxc_config_define(sysctl); |
61d7a733 | 157 | lxc_config_define(proc); |
c2cc9f0a | 158 | |
33eb2ec1 | 159 | static struct lxc_config_t config_jump_table[] = { |
6fba98b5 CB |
160 | { "lxc.arch", set_config_personality, get_config_personality, clr_config_personality, }, |
161 | { "lxc.apparmor.profile", set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, }, | |
162 | { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, }, | |
1800f924 WB |
163 | { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting, get_config_apparmor_allow_nesting, clr_config_apparmor_allow_nesting, }, |
164 | { "lxc.apparmor.raw", set_config_apparmor_raw, get_config_apparmor_raw, clr_config_apparmor_raw, }, | |
6fba98b5 CB |
165 | { "lxc.autodev", set_config_autodev, get_config_autodev, clr_config_autodev, }, |
166 | { "lxc.cap.drop", set_config_cap_drop, get_config_cap_drop, clr_config_cap_drop, }, | |
167 | { "lxc.cap.keep", set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, }, | |
168 | { "lxc.cgroup2", set_config_cgroup2_controller, get_config_cgroup2_controller, clr_config_cgroup2_controller, }, | |
169 | { "lxc.cgroup.dir", set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, }, | |
9caee129 | 170 | { "lxc.cgroup.relative", set_config_cgroup_relative, get_config_cgroup_relative, clr_config_cgroup_relative, }, |
6fba98b5 | 171 | { "lxc.cgroup", set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, }, |
6fba98b5 CB |
172 | { "lxc.console.buffer.size", set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, }, |
173 | { "lxc.console.logfile", set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, }, | |
174 | { "lxc.console.path", set_config_console_path, get_config_console_path, clr_config_console_path, }, | |
175 | { "lxc.console.rotate", set_config_console_rotate, get_config_console_rotate, clr_config_console_rotate, }, | |
861813e5 | 176 | { "lxc.console.size", set_config_console_size, get_config_console_size, clr_config_console_size, }, |
6fba98b5 CB |
177 | { "lxc.environment", set_config_environment, get_config_environment, clr_config_environment, }, |
178 | { "lxc.ephemeral", set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, }, | |
179 | { "lxc.execute.cmd", set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, }, | |
180 | { "lxc.group", set_config_group, get_config_group, clr_config_group, }, | |
181 | { "lxc.hook.autodev", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
182 | { "lxc.hook.clone", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
183 | { "lxc.hook.destroy", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
184 | { "lxc.hook.mount", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
185 | { "lxc.hook.post-stop", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
186 | { "lxc.hook.pre-mount", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
187 | { "lxc.hook.pre-start", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
188 | { "lxc.hook.start", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
189 | { "lxc.hook.start-host", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
190 | { "lxc.hook.stop", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
191 | { "lxc.hook.version", set_config_hooks_version, get_config_hooks_version, clr_config_hooks_version, }, | |
192 | { "lxc.hook", set_config_hooks, get_config_hooks, clr_config_hooks, }, | |
193 | { "lxc.idmap", set_config_idmaps, get_config_idmaps, clr_config_idmaps, }, | |
194 | { "lxc.include", set_config_includefiles, get_config_includefiles, clr_config_includefiles, }, | |
195 | { "lxc.init.cmd", set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, }, | |
196 | { "lxc.init.gid", set_config_init_gid, get_config_init_gid, clr_config_init_gid, }, | |
197 | { "lxc.init.uid", set_config_init_uid, get_config_init_uid, clr_config_init_uid, }, | |
198 | { "lxc.init.cwd", set_config_init_cwd, get_config_init_cwd, clr_config_init_cwd, }, | |
199 | { "lxc.log.file", set_config_log_file, get_config_log_file, clr_config_log_file, }, | |
200 | { "lxc.log.level", set_config_log_level, get_config_log_level, clr_config_log_level, }, | |
201 | { "lxc.log.syslog", set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, }, | |
202 | { "lxc.monitor.unshare", set_config_monitor, get_config_monitor, clr_config_monitor, }, | |
258f8051 | 203 | { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath, get_config_monitor_signal_pdeath, clr_config_monitor_signal_pdeath, }, |
6fba98b5 CB |
204 | { "lxc.mount.auto", set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, }, |
205 | { "lxc.mount.entry", set_config_mount, get_config_mount, clr_config_mount, }, | |
206 | { "lxc.mount.fstab", set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, }, | |
207 | { "lxc.namespace.clone", set_config_namespace_clone, get_config_namespace_clone, clr_config_namespace_clone, }, | |
208 | { "lxc.namespace.keep", set_config_namespace_keep, get_config_namespace_keep, clr_config_namespace_keep, }, | |
209 | { "lxc.namespace.share", set_config_namespace_share, get_config_namespace_share, clr_config_namespace_share, }, | |
210 | { "lxc.net.flags", set_config_net_flags, get_config_net_flags, clr_config_net_flags, }, | |
211 | { "lxc.net.hwaddr", set_config_net_hwaddr, get_config_net_hwaddr, clr_config_net_hwaddr, }, | |
212 | { "lxc.net.ipv4.address", set_config_net_ipv4_address, get_config_net_ipv4_address, clr_config_net_ipv4_address, }, | |
213 | { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway, get_config_net_ipv4_gateway, clr_config_net_ipv4_gateway, }, | |
214 | { "lxc.net.ipv6.address", set_config_net_ipv6_address, get_config_net_ipv6_address, clr_config_net_ipv6_address, }, | |
215 | { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway, get_config_net_ipv6_gateway, clr_config_net_ipv6_gateway, }, | |
216 | { "lxc.net.link", set_config_net_link, get_config_net_link, clr_config_net_link, }, | |
217 | { "lxc.net.macvlan.mode", set_config_net_macvlan_mode, get_config_net_macvlan_mode, clr_config_net_macvlan_mode, }, | |
218 | { "lxc.net.mtu", set_config_net_mtu, get_config_net_mtu, clr_config_net_mtu, }, | |
219 | { "lxc.net.name", set_config_net_name, get_config_net_name, clr_config_net_name, }, | |
220 | { "lxc.net.script.down", set_config_net_script_down, get_config_net_script_down, clr_config_net_script_down, }, | |
221 | { "lxc.net.script.up", set_config_net_script_up, get_config_net_script_up, clr_config_net_script_up, }, | |
222 | { "lxc.net.type", set_config_net_type, get_config_net_type, clr_config_net_type, }, | |
223 | { "lxc.net.vlan.id", set_config_net_vlan_id, get_config_net_vlan_id, clr_config_net_vlan_id, }, | |
224 | { "lxc.net.veth.pair", set_config_net_veth_pair, get_config_net_veth_pair, clr_config_net_veth_pair, }, | |
225 | { "lxc.net.", set_config_net_nic, get_config_net_nic, clr_config_net_nic, }, | |
226 | { "lxc.net", set_config_net, get_config_net, clr_config_net, }, | |
227 | { "lxc.no_new_privs", set_config_no_new_privs, get_config_no_new_privs, clr_config_no_new_privs, }, | |
228 | { "lxc.prlimit", set_config_prlimit, get_config_prlimit, clr_config_prlimit, }, | |
229 | { "lxc.pty.max", set_config_pty_max, get_config_pty_max, clr_config_pty_max, }, | |
6e54330c | 230 | { "lxc.rootfs.managed", set_config_rootfs_managed, get_config_rootfs_managed, clr_config_rootfs_managed, }, |
6fba98b5 CB |
231 | { "lxc.rootfs.mount", set_config_rootfs_mount, get_config_rootfs_mount, clr_config_rootfs_mount, }, |
232 | { "lxc.rootfs.options", set_config_rootfs_options, get_config_rootfs_options, clr_config_rootfs_options, }, | |
233 | { "lxc.rootfs.path", set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, }, | |
234 | { "lxc.seccomp.profile", set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, }, | |
235 | { "lxc.selinux.context", set_config_selinux_context, get_config_selinux_context, clr_config_selinux_context, }, | |
236 | { "lxc.signal.halt", set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, }, | |
237 | { "lxc.signal.reboot", set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, }, | |
238 | { "lxc.signal.stop", set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, }, | |
239 | { "lxc.start.auto", set_config_start, get_config_start, clr_config_start, }, | |
240 | { "lxc.start.delay", set_config_start, get_config_start, clr_config_start, }, | |
241 | { "lxc.start.order", set_config_start, get_config_start, clr_config_start, }, | |
242 | { "lxc.tty.dir", set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, }, | |
243 | { "lxc.tty.max", set_config_tty_max, get_config_tty_max, clr_config_tty_max, }, | |
244 | { "lxc.uts.name", set_config_uts_name, get_config_uts_name, clr_config_uts_name, }, | |
245 | { "lxc.sysctl", set_config_sysctl, get_config_sysctl, clr_config_sysctl, }, | |
246 | { "lxc.proc", set_config_proc, get_config_proc, clr_config_proc, }, | |
a84b9932 AV |
247 | }; |
248 | ||
33eb2ec1 | 249 | static const size_t config_jump_table_size = sizeof(config_jump_table) / sizeof(struct lxc_config_t); |
c2cc9f0a | 250 | |
300df83e | 251 | struct lxc_config_t *lxc_get_config(const char *key) |
c2cc9f0a | 252 | { |
84760c11 | 253 | size_t i; |
c2cc9f0a | 254 | |
33eb2ec1 CB |
255 | for (i = 0; i < config_jump_table_size; i++) |
256 | if (!strncmp(config_jump_table[i].name, key, strlen(config_jump_table[i].name))) | |
257 | return &config_jump_table[i]; | |
300df83e | 258 | |
c2cc9f0a | 259 | return NULL; |
260 | } | |
261 | ||
f9373e40 CB |
262 | static int set_config_net(const char *key, const char *value, |
263 | struct lxc_conf *lxc_conf, void *data) | |
6b0d5538 | 264 | { |
663e9916 | 265 | if (!lxc_config_value_empty(value)) { |
f9373e40 | 266 | ERROR("lxc.net must not have a value"); |
6b0d5538 SH |
267 | return -1; |
268 | } | |
269 | ||
f9373e40 | 270 | return clr_config_net(key, lxc_conf, data); |
6b0d5538 SH |
271 | } |
272 | ||
f9373e40 CB |
273 | static int set_config_net_type(const char *key, const char *value, |
274 | struct lxc_conf *lxc_conf, void *data) | |
c2cc9f0a | 275 | { |
070a05af | 276 | struct lxc_netdev *netdev = data; |
c2cc9f0a | 277 | |
663e9916 | 278 | if (lxc_config_value_empty(value)) |
f9373e40 | 279 | return clr_config_net_type(key, lxc_conf, data); |
c302b476 | 280 | |
bbc079cf | 281 | if (!netdev) |
c2cc9f0a | 282 | return -1; |
c2cc9f0a | 283 | |
bbc079cf | 284 | if (!strcmp(value, "veth")) { |
24654103 | 285 | netdev->type = LXC_NET_VETH; |
bbc079cf | 286 | } else if (!strcmp(value, "macvlan")) { |
24654103 | 287 | netdev->type = LXC_NET_MACVLAN; |
9b0df30f CB |
288 | lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, |
289 | "private"); | |
bbc079cf | 290 | } else if (!strcmp(value, "vlan")) { |
24654103 | 291 | netdev->type = LXC_NET_VLAN; |
bbc079cf | 292 | } else if (!strcmp(value, "phys")) { |
24654103 | 293 | netdev->type = LXC_NET_PHYS; |
bbc079cf | 294 | } else if (!strcmp(value, "empty")) { |
24654103 | 295 | netdev->type = LXC_NET_EMPTY; |
bbc079cf | 296 | } else if (!strcmp(value, "none")) { |
26b797f3 | 297 | netdev->type = LXC_NET_NONE; |
bbc079cf | 298 | } else { |
47903908 | 299 | ERROR("Invalid network type %s", value); |
c2cc9f0a | 300 | return -1; |
301 | } | |
bbc079cf | 302 | |
c2cc9f0a | 303 | return 0; |
304 | } | |
305 | ||
f9373e40 CB |
306 | static int set_config_net_flags(const char *key, const char *value, |
307 | struct lxc_conf *lxc_conf, void *data) | |
c2cc9f0a | 308 | { |
070a05af | 309 | struct lxc_netdev *netdev = data; |
c2cc9f0a | 310 | |
9d4bf22d | 311 | if (lxc_config_value_empty(value)) |
f9373e40 | 312 | return clr_config_net_flags(key, lxc_conf, data); |
9d4bf22d | 313 | |
33c945e0 | 314 | if (!netdev) |
c2cc9f0a | 315 | return -1; |
c2cc9f0a | 316 | |
33c945e0 | 317 | netdev->flags |= IFF_UP; |
c2cc9f0a | 318 | |
33c945e0 MT |
319 | return 0; |
320 | } | |
321 | ||
b45e32f9 | 322 | static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf, |
d5aba460 | 323 | struct lxc_netdev *netdev) |
576400e5 | 324 | { |
b1e44ed1 | 325 | struct netns_ifaddrs *ifaddr, *ifa; |
504a2217 CB |
326 | int n; |
327 | int ret = 0; | |
f9373e40 CB |
328 | const char *type_key = "lxc.net.type"; |
329 | const char *link_key = "lxc.net.link"; | |
576400e5 | 330 | const char *tmpvalue = "phys"; |
576400e5 | 331 | |
b1e44ed1 | 332 | if (netns_getifaddrs(&ifaddr, -1, &(bool){false}) < 0) { |
47903908 | 333 | SYSERROR("Failed to get network interfaces"); |
576400e5 | 334 | return -1; |
335 | } | |
336 | ||
337 | for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { | |
338 | if (!ifa->ifa_addr) | |
339 | continue; | |
47903908 | 340 | |
576400e5 | 341 | if (ifa->ifa_addr->sa_family != AF_PACKET) |
342 | continue; | |
343 | ||
504a2217 | 344 | if (!strncmp(value, ifa->ifa_name, strlen(value) - 1)) { |
f9373e40 CB |
345 | ret = set_config_net_type(type_key, tmpvalue, lxc_conf, |
346 | netdev); | |
576400e5 | 347 | if (!ret) { |
f9373e40 | 348 | ret = set_config_net_link( |
c302b476 | 349 | link_key, ifa->ifa_name, lxc_conf, netdev); |
576400e5 | 350 | if (ret) { |
47903908 | 351 | ERROR("Failed to create matched ifnames"); |
576400e5 | 352 | break; |
353 | } | |
354 | } else { | |
47903908 | 355 | ERROR("Failed to create matched ifnames"); |
576400e5 | 356 | break; |
357 | } | |
358 | } | |
359 | } | |
360 | ||
b1e44ed1 | 361 | netns_freeifaddrs(ifaddr); |
504a2217 | 362 | ifaddr = NULL; |
091045f8 | 363 | |
576400e5 | 364 | return ret; |
365 | } | |
366 | ||
f9373e40 CB |
367 | static int set_config_net_link(const char *key, const char *value, |
368 | struct lxc_conf *lxc_conf, void *data) | |
33c945e0 | 369 | { |
070a05af | 370 | struct lxc_netdev *netdev = data; |
576400e5 | 371 | int ret = 0; |
33c945e0 | 372 | |
6bed0fb6 | 373 | if (lxc_config_value_empty(value)) |
f9373e40 | 374 | return clr_config_net_link(key, lxc_conf, data); |
6bed0fb6 | 375 | |
33c945e0 | 376 | if (!netdev) |
c2cc9f0a | 377 | return -1; |
c2cc9f0a | 378 | |
b45e32f9 CB |
379 | if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS) |
380 | ret = create_matched_ifnames(value, lxc_conf, netdev); | |
381 | else | |
18cd4b54 | 382 | ret = network_ifname(netdev->link, value, sizeof(netdev->link)); |
576400e5 | 383 | |
384 | return ret; | |
c2cc9f0a | 385 | } |
386 | ||
f9373e40 CB |
387 | static int set_config_net_name(const char *key, const char *value, |
388 | struct lxc_conf *lxc_conf, void *data) | |
c2cc9f0a | 389 | { |
070a05af | 390 | struct lxc_netdev *netdev = data; |
c2cc9f0a | 391 | |
6bed0fb6 | 392 | if (lxc_config_value_empty(value)) |
f9373e40 | 393 | return clr_config_net_name(key, lxc_conf, data); |
6bed0fb6 | 394 | |
33c945e0 | 395 | if (!netdev) |
c2cc9f0a | 396 | return -1; |
c2cc9f0a | 397 | |
18cd4b54 | 398 | return network_ifname(netdev->name, value, sizeof(netdev->name)); |
33c945e0 MT |
399 | } |
400 | ||
f9373e40 CB |
401 | static int set_config_net_veth_pair(const char *key, const char *value, |
402 | struct lxc_conf *lxc_conf, void *data) | |
e892973e | 403 | { |
070a05af | 404 | struct lxc_netdev *netdev = data; |
e892973e | 405 | |
6bed0fb6 | 406 | if (lxc_config_value_empty(value)) |
f9373e40 | 407 | return clr_config_net_veth_pair(key, lxc_conf, data); |
6bed0fb6 | 408 | |
e892973e DL |
409 | if (!netdev) |
410 | return -1; | |
411 | ||
18cd4b54 | 412 | return network_ifname(netdev->priv.veth_attr.pair, value, sizeof(netdev->priv.veth_attr.pair)); |
e892973e DL |
413 | } |
414 | ||
f9373e40 CB |
415 | static int set_config_net_macvlan_mode(const char *key, const char *value, |
416 | struct lxc_conf *lxc_conf, void *data) | |
8634bc19 | 417 | { |
070a05af | 418 | struct lxc_netdev *netdev = data; |
8634bc19 | 419 | |
6bed0fb6 | 420 | if (lxc_config_value_empty(value)) |
f9373e40 | 421 | return clr_config_net_macvlan_mode(key, lxc_conf, data); |
6bed0fb6 | 422 | |
8634bc19 MT |
423 | if (!netdev) |
424 | return -1; | |
425 | ||
9b0df30f | 426 | return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value); |
8634bc19 MT |
427 | } |
428 | ||
f9373e40 CB |
429 | static int set_config_net_hwaddr(const char *key, const char *value, |
430 | struct lxc_conf *lxc_conf, void *data) | |
33c945e0 | 431 | { |
070a05af | 432 | struct lxc_netdev *netdev = data; |
504a2217 | 433 | char *new_value; |
33c945e0 | 434 | |
6bed0fb6 | 435 | if (lxc_config_value_empty(value)) |
f9373e40 | 436 | return clr_config_net_hwaddr(key, lxc_conf, data); |
6bed0fb6 | 437 | |
ecbb3790 CB |
438 | if (!netdev) |
439 | return -1; | |
440 | ||
504a2217 | 441 | new_value = strdup(value); |
d5aba460 | 442 | if (!new_value) |
c2cc9f0a | 443 | return -1; |
d5aba460 | 444 | |
508c263e | 445 | rand_complete_hwaddr(new_value); |
c2cc9f0a | 446 | |
663e9916 | 447 | if (lxc_config_value_empty(new_value)) { |
508c263e SH |
448 | free(new_value); |
449 | netdev->hwaddr = NULL; | |
450 | return 0; | |
451 | } | |
452 | ||
453 | netdev->hwaddr = new_value; | |
47903908 | 454 | |
508c263e | 455 | return 0; |
c2cc9f0a | 456 | } |
457 | ||
f9373e40 CB |
458 | static int set_config_net_vlan_id(const char *key, const char *value, |
459 | struct lxc_conf *lxc_conf, void *data) | |
26c39028 | 460 | { |
d5aba460 | 461 | int ret; |
070a05af | 462 | struct lxc_netdev *netdev = data; |
26c39028 | 463 | |
6bed0fb6 | 464 | if (lxc_config_value_empty(value)) |
f9373e40 | 465 | return clr_config_net_vlan_id(key, lxc_conf, data); |
6bed0fb6 | 466 | |
26c39028 JHS |
467 | if (!netdev) |
468 | return -1; | |
469 | ||
d5aba460 CB |
470 | ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0); |
471 | if (ret < 0) | |
26c39028 JHS |
472 | return -1; |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
f9373e40 CB |
477 | static int set_config_net_mtu(const char *key, const char *value, |
478 | struct lxc_conf *lxc_conf, void *data) | |
442cbbe6 | 479 | { |
070a05af | 480 | struct lxc_netdev *netdev = data; |
442cbbe6 | 481 | |
6bed0fb6 | 482 | if (lxc_config_value_empty(value)) |
f9373e40 | 483 | return clr_config_net_mtu(key, lxc_conf, data); |
6bed0fb6 | 484 | |
33c945e0 | 485 | if (!netdev) |
442cbbe6 | 486 | return -1; |
442cbbe6 | 487 | |
713046e3 | 488 | return set_config_string_item(&netdev->mtu, value); |
442cbbe6 TR |
489 | } |
490 | ||
9ff60df2 CB |
491 | static int set_config_net_ipv4_address(const char *key, const char *value, |
492 | struct lxc_conf *lxc_conf, void *data) | |
c2cc9f0a | 493 | { |
d5aba460 | 494 | int ret; |
070a05af | 495 | struct lxc_netdev *netdev = data; |
33c945e0 | 496 | struct lxc_inetdev *inetdev; |
c2cc9f0a | 497 | struct lxc_list *list; |
504a2217 CB |
498 | char *cursor, *slash; |
499 | char *addr = NULL, *bcast = NULL, *prefix = NULL; | |
c2cc9f0a | 500 | |
663e9916 | 501 | if (lxc_config_value_empty(value)) |
9ff60df2 | 502 | return clr_config_net_ipv4_address(key, lxc_conf, data); |
0797e123 | 503 | |
33c945e0 | 504 | if (!netdev) |
c2cc9f0a | 505 | return -1; |
c2cc9f0a | 506 | |
507 | inetdev = malloc(sizeof(*inetdev)); | |
d5aba460 | 508 | if (!inetdev) |
c2cc9f0a | 509 | return -1; |
c2cc9f0a | 510 | memset(inetdev, 0, sizeof(*inetdev)); |
511 | ||
512 | list = malloc(sizeof(*list)); | |
513 | if (!list) { | |
53719062 | 514 | free(inetdev); |
c2cc9f0a | 515 | return -1; |
516 | } | |
517 | ||
518 | lxc_list_init(list); | |
519 | list->elem = inetdev; | |
520 | ||
956edc54 SG |
521 | addr = strdup(value); |
522 | if (!addr) { | |
53719062 SH |
523 | free(inetdev); |
524 | free(list); | |
956edc54 SG |
525 | return -1; |
526 | } | |
c2cc9f0a | 527 | |
528 | cursor = strstr(addr, " "); | |
529 | if (cursor) { | |
530 | *cursor = '\0'; | |
531 | bcast = cursor + 1; | |
532 | } | |
533 | ||
534 | slash = strstr(addr, "/"); | |
535 | if (slash) { | |
536 | *slash = '\0'; | |
537 | prefix = slash + 1; | |
538 | } | |
539 | ||
d5aba460 CB |
540 | ret = inet_pton(AF_INET, addr, &inetdev->addr); |
541 | if (!ret || ret < 0) { | |
542 | SYSERROR("Invalid ipv4 address \"%s\"", value); | |
53719062 | 543 | free(inetdev); |
956edc54 | 544 | free(addr); |
53719062 | 545 | free(list); |
c2cc9f0a | 546 | return -1; |
547 | } | |
548 | ||
d5aba460 CB |
549 | if (bcast) { |
550 | ret = inet_pton(AF_INET, bcast, &inetdev->bcast); | |
551 | if (!ret || ret < 0) { | |
552 | SYSERROR("Invalid ipv4 broadcast address \"%s\"", value); | |
553 | free(inetdev); | |
554 | free(list); | |
555 | free(addr); | |
556 | return -1; | |
557 | } | |
558 | ||
0093bb8c | 559 | } |
c2cc9f0a | 560 | |
504a2217 | 561 | /* No prefix specified, determine it from the network class. */ |
1c633398 | 562 | if (prefix) { |
d5aba460 | 563 | ret = lxc_safe_uint(prefix, &inetdev->prefix); |
aa7c5841 DJ |
564 | if (ret < 0) { |
565 | free(inetdev); | |
566 | free(list); | |
567 | free(addr); | |
1c633398 | 568 | return -1; |
aa7c5841 | 569 | } |
1c633398 CB |
570 | } else { |
571 | inetdev->prefix = config_ip_prefix(&inetdev->addr); | |
572 | } | |
a059591e | 573 | |
504a2217 CB |
574 | /* If no broadcast address, let compute one from the |
575 | * prefix and address. | |
0093bb8c DL |
576 | */ |
577 | if (!bcast) { | |
1b7d4743 | 578 | inetdev->bcast.s_addr = inetdev->addr.s_addr; |
d5aba460 | 579 | inetdev->bcast.s_addr |= htonl(INADDR_BROADCAST >> inetdev->prefix); |
0093bb8c | 580 | } |
c2cc9f0a | 581 | |
8538f388 | 582 | lxc_list_add_tail(&netdev->ipv4, list); |
956edc54 | 583 | free(addr); |
47903908 | 584 | |
c2cc9f0a | 585 | return 0; |
586 | } | |
587 | ||
f9373e40 CB |
588 | static int set_config_net_ipv4_gateway(const char *key, const char *value, |
589 | struct lxc_conf *lxc_conf, void *data) | |
f8fee0e2 | 590 | { |
070a05af | 591 | struct lxc_netdev *netdev = data; |
f8fee0e2 | 592 | |
6bed0fb6 | 593 | if (lxc_config_value_empty(value)) |
f9373e40 | 594 | return clr_config_net_ipv4_gateway(key, lxc_conf, data); |
6bed0fb6 | 595 | |
f8fee0e2 MK |
596 | if (!netdev) |
597 | return -1; | |
598 | ||
e088e926 | 599 | free(netdev->ipv4_gateway); |
f8fee0e2 | 600 | |
ab6faf2e | 601 | if (!strcmp(value, "auto")) { |
19a26f82 MK |
602 | netdev->ipv4_gateway = NULL; |
603 | netdev->ipv4_gateway_auto = true; | |
604 | } else { | |
25a908b8 | 605 | int ret; |
e088e926 SG |
606 | struct in_addr *gw; |
607 | ||
608 | gw = malloc(sizeof(*gw)); | |
25a908b8 | 609 | if (!gw) |
e088e926 | 610 | return -1; |
e088e926 | 611 | |
25a908b8 CB |
612 | ret = inet_pton(AF_INET, value, gw); |
613 | if (!ret || ret < 0) { | |
614 | SYSERROR("Invalid ipv4 gateway address \"%s\"", value); | |
53719062 | 615 | free(gw); |
19a26f82 MK |
616 | return -1; |
617 | } | |
618 | ||
619 | netdev->ipv4_gateway = gw; | |
620 | netdev->ipv4_gateway_auto = false; | |
f8fee0e2 MK |
621 | } |
622 | ||
f8fee0e2 MK |
623 | return 0; |
624 | } | |
625 | ||
2e44ae28 CB |
626 | static int set_config_net_ipv6_address(const char *key, const char *value, |
627 | struct lxc_conf *lxc_conf, void *data) | |
c2cc9f0a | 628 | { |
25a908b8 | 629 | int ret; |
070a05af | 630 | struct lxc_netdev *netdev = data; |
c2cc9f0a | 631 | struct lxc_inet6dev *inet6dev; |
632 | struct lxc_list *list; | |
504a2217 | 633 | char *slash, *valdup, *netmask; |
c2cc9f0a | 634 | |
6bed0fb6 | 635 | if (lxc_config_value_empty(value)) |
2e44ae28 | 636 | return clr_config_net_ipv6_address(key, lxc_conf, data); |
6bed0fb6 | 637 | |
33c945e0 | 638 | if (!netdev) |
c2cc9f0a | 639 | return -1; |
c2cc9f0a | 640 | |
641 | inet6dev = malloc(sizeof(*inet6dev)); | |
25a908b8 | 642 | if (!inet6dev) |
c2cc9f0a | 643 | return -1; |
c2cc9f0a | 644 | memset(inet6dev, 0, sizeof(*inet6dev)); |
645 | ||
646 | list = malloc(sizeof(*list)); | |
647 | if (!list) { | |
28027320 | 648 | free(inet6dev); |
c2cc9f0a | 649 | return -1; |
650 | } | |
651 | ||
652 | lxc_list_init(list); | |
653 | list->elem = inet6dev; | |
654 | ||
956edc54 SG |
655 | valdup = strdup(value); |
656 | if (!valdup) { | |
28027320 SH |
657 | free(list); |
658 | free(inet6dev); | |
956edc54 SG |
659 | return -1; |
660 | } | |
661 | ||
a059591e | 662 | inet6dev->prefix = 64; |
12a50cc6 | 663 | slash = strstr(valdup, "/"); |
c2cc9f0a | 664 | if (slash) { |
665 | *slash = '\0'; | |
666 | netmask = slash + 1; | |
47903908 | 667 | |
f54f8d0b CB |
668 | ret = lxc_safe_uint(netmask, &inet6dev->prefix); |
669 | if (ret < 0) { | |
670 | free(list); | |
671 | free(inet6dev); | |
672 | free(valdup); | |
1c633398 | 673 | return -1; |
f54f8d0b | 674 | } |
c2cc9f0a | 675 | } |
676 | ||
25a908b8 CB |
677 | ret = inet_pton(AF_INET6, valdup, &inet6dev->addr); |
678 | if (!ret || ret < 0) { | |
679 | SYSERROR("Invalid ipv6 address \"%s\"", valdup); | |
28027320 SH |
680 | free(list); |
681 | free(inet6dev); | |
956edc54 | 682 | free(valdup); |
c2cc9f0a | 683 | return -1; |
684 | } | |
685 | ||
8538f388 | 686 | lxc_list_add_tail(&netdev->ipv6, list); |
956edc54 | 687 | free(valdup); |
47903908 | 688 | |
c2cc9f0a | 689 | return 0; |
690 | } | |
691 | ||
f9373e40 CB |
692 | static int set_config_net_ipv6_gateway(const char *key, const char *value, |
693 | struct lxc_conf *lxc_conf, void *data) | |
f8fee0e2 | 694 | { |
070a05af | 695 | struct lxc_netdev *netdev = data; |
f8fee0e2 | 696 | |
6bed0fb6 | 697 | if (lxc_config_value_empty(value)) |
f9373e40 | 698 | return clr_config_net_ipv6_gateway(key, lxc_conf, data); |
6bed0fb6 | 699 | |
f8fee0e2 MK |
700 | if (!netdev) |
701 | return -1; | |
702 | ||
e088e926 | 703 | free(netdev->ipv6_gateway); |
f8fee0e2 | 704 | |
ab6faf2e | 705 | if (!strcmp(value, "auto")) { |
19a26f82 MK |
706 | netdev->ipv6_gateway = NULL; |
707 | netdev->ipv6_gateway_auto = true; | |
708 | } else { | |
25a908b8 | 709 | int ret; |
8fb86a37 SH |
710 | struct in6_addr *gw; |
711 | ||
bec695f3 | 712 | gw = malloc(sizeof(*gw)); |
25a908b8 | 713 | if (!gw) |
bec695f3 | 714 | return -1; |
bec695f3 | 715 | |
25a908b8 CB |
716 | ret = inet_pton(AF_INET6, value, gw); |
717 | if (!ret || ret < 0) { | |
718 | SYSERROR("Invalid ipv6 gateway address \"%s\"", value); | |
28027320 | 719 | free(gw); |
19a26f82 MK |
720 | return -1; |
721 | } | |
722 | ||
723 | netdev->ipv6_gateway = gw; | |
724 | netdev->ipv6_gateway_auto = false; | |
f8fee0e2 MK |
725 | } |
726 | ||
f8fee0e2 MK |
727 | return 0; |
728 | } | |
729 | ||
f9373e40 CB |
730 | static int set_config_net_script_up(const char *key, const char *value, |
731 | struct lxc_conf *lxc_conf, void *data) | |
e3b4c4c4 | 732 | { |
070a05af | 733 | struct lxc_netdev *netdev = data; |
e3b4c4c4 | 734 | |
6bed0fb6 | 735 | if (lxc_config_value_empty(value)) |
f9373e40 | 736 | return clr_config_net_script_up(key, lxc_conf, data); |
6bed0fb6 | 737 | |
e3b4c4c4 | 738 | if (!netdev) |
504a2217 | 739 | return -1; |
e3b4c4c4 | 740 | |
713046e3 | 741 | return set_config_string_item(&netdev->upscript, value); |
8fc8295a DE |
742 | } |
743 | ||
f9373e40 CB |
744 | static int set_config_net_script_down(const char *key, const char *value, |
745 | struct lxc_conf *lxc_conf, void *data) | |
8fc8295a | 746 | { |
070a05af | 747 | struct lxc_netdev *netdev = data; |
8fc8295a | 748 | |
6bed0fb6 | 749 | if (lxc_config_value_empty(value)) |
f9373e40 | 750 | return clr_config_net_script_down(key, lxc_conf, data); |
6bed0fb6 | 751 | |
8fc8295a | 752 | if (!netdev) |
504a2217 | 753 | return -1; |
8fc8295a | 754 | |
713046e3 | 755 | return set_config_string_item(&netdev->downscript, value); |
e3b4c4c4 ST |
756 | } |
757 | ||
26ddeedd SH |
758 | static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook) |
759 | { | |
760 | struct lxc_list *hooklist; | |
761 | ||
762 | hooklist = malloc(sizeof(*hooklist)); | |
763 | if (!hooklist) { | |
764 | free(hook); | |
765 | return -1; | |
766 | } | |
504a2217 | 767 | |
26ddeedd SH |
768 | hooklist->elem = hook; |
769 | lxc_list_add_tail(&lxc_conf->hooks[which], hooklist); | |
47903908 | 770 | |
26ddeedd SH |
771 | return 0; |
772 | } | |
773 | ||
0b427da0 CB |
774 | static int set_config_seccomp_profile(const char *key, const char *value, |
775 | struct lxc_conf *lxc_conf, void *data) | |
8f2c3a70 | 776 | { |
713046e3 | 777 | return set_config_path_item(&lxc_conf->seccomp, value); |
8f2c3a70 SH |
778 | } |
779 | ||
5cda27c1 SH |
780 | static int set_config_execute_cmd(const char *key, const char *value, |
781 | struct lxc_conf *lxc_conf, void *data) | |
782 | { | |
783 | return set_config_path_item(&lxc_conf->execute_cmd, value); | |
784 | } | |
785 | ||
713046e3 | 786 | static int set_config_init_cmd(const char *key, const char *value, |
c7e27aaf | 787 | struct lxc_conf *lxc_conf, void *data) |
67c660d0 | 788 | { |
713046e3 | 789 | return set_config_path_item(&lxc_conf->init_cmd, value); |
67c660d0 SG |
790 | } |
791 | ||
3c491553 L |
792 | static int set_config_init_cwd(const char *key, const char *value, |
793 | struct lxc_conf *lxc_conf, void *data) | |
794 | { | |
795 | return set_config_path_item(&lxc_conf->init_cwd, value); | |
796 | } | |
797 | ||
713046e3 | 798 | static int set_config_init_uid(const char *key, const char *value, |
c7e27aaf | 799 | struct lxc_conf *lxc_conf, void *data) |
72bb04e4 | 800 | { |
d1e5d636 CB |
801 | unsigned int init_uid; |
802 | ||
663e9916 | 803 | if (lxc_config_value_empty(value)) { |
2e7cde40 | 804 | lxc_conf->init_uid = 0; |
fee80911 | 805 | return 0; |
2e7cde40 | 806 | } |
fee80911 | 807 | |
d1e5d636 CB |
808 | if (lxc_safe_uint(value, &init_uid) < 0) |
809 | return -1; | |
25a908b8 | 810 | |
d1e5d636 CB |
811 | lxc_conf->init_uid = init_uid; |
812 | ||
72bb04e4 PT |
813 | return 0; |
814 | } | |
815 | ||
713046e3 | 816 | static int set_config_init_gid(const char *key, const char *value, |
c7e27aaf | 817 | struct lxc_conf *lxc_conf, void *data) |
72bb04e4 | 818 | { |
d1e5d636 CB |
819 | unsigned int init_gid; |
820 | ||
663e9916 | 821 | if (lxc_config_value_empty(value)) { |
2debb6e6 | 822 | lxc_conf->init_gid = 0; |
a757cc7d | 823 | return 0; |
2debb6e6 | 824 | } |
a757cc7d | 825 | |
d1e5d636 CB |
826 | if (lxc_safe_uint(value, &init_gid) < 0) |
827 | return -1; | |
25a908b8 | 828 | |
d1e5d636 CB |
829 | lxc_conf->init_gid = init_gid; |
830 | ||
72bb04e4 PT |
831 | return 0; |
832 | } | |
833 | ||
466c2e93 | 834 | static int set_config_hooks(const char *key, const char *value, |
c7e27aaf | 835 | struct lxc_conf *lxc_conf, void *data) |
26ddeedd | 836 | { |
7d0eb87e | 837 | char *copy; |
72bb04e4 | 838 | |
663e9916 | 839 | if (lxc_config_value_empty(value)) |
7d0eb87e SH |
840 | return lxc_clear_hooks(lxc_conf, key); |
841 | ||
a182feae | 842 | if (strcmp(key + 4, "hook") == 0) { |
25a908b8 | 843 | ERROR("lxc.hook must not have a value"); |
6b0d5538 SH |
844 | return -1; |
845 | } | |
25a908b8 | 846 | |
7d0eb87e | 847 | copy = strdup(value); |
25a908b8 | 848 | if (!copy) |
26ddeedd | 849 | return -1; |
504a2217 | 850 | |
a182feae | 851 | if (strcmp(key + 9, "pre-start") == 0) |
26ddeedd | 852 | return add_hook(lxc_conf, LXCHOOK_PRESTART, copy); |
08dd2805 SH |
853 | else if (strcmp(key + 9, "start-host") == 0) |
854 | return add_hook(lxc_conf, LXCHOOK_START_HOST, copy); | |
a182feae | 855 | else if (strcmp(key + 9, "pre-mount") == 0) |
5ea6163a | 856 | return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy); |
a182feae | 857 | else if (strcmp(key + 9, "autodev") == 0) |
f7bee6c6 | 858 | return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy); |
a182feae | 859 | else if (strcmp(key + 9, "mount") == 0) |
26ddeedd | 860 | return add_hook(lxc_conf, LXCHOOK_MOUNT, copy); |
a182feae | 861 | else if (strcmp(key + 9, "start") == 0) |
26ddeedd | 862 | return add_hook(lxc_conf, LXCHOOK_START, copy); |
a182feae | 863 | else if (strcmp(key + 9, "stop") == 0) |
52492063 | 864 | return add_hook(lxc_conf, LXCHOOK_STOP, copy); |
a182feae | 865 | else if (strcmp(key + 9, "post-stop") == 0) |
26ddeedd | 866 | return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy); |
a182feae | 867 | else if (strcmp(key + 9, "clone") == 0) |
148e91f5 | 868 | return add_hook(lxc_conf, LXCHOOK_CLONE, copy); |
a182feae | 869 | else if (strcmp(key + 9, "destroy") == 0) |
37cf711b | 870 | return add_hook(lxc_conf, LXCHOOK_DESTROY, copy); |
504a2217 | 871 | |
26ddeedd | 872 | free(copy); |
47903908 | 873 | |
26ddeedd SH |
874 | return -1; |
875 | } | |
876 | ||
44ae0fb6 CB |
877 | static int set_config_hooks_version(const char *key, const char *value, |
878 | struct lxc_conf *lxc_conf, void *data) | |
879 | { | |
880 | int ret; | |
881 | unsigned int tmp; | |
882 | ||
883 | if (lxc_config_value_empty(value)) | |
884 | return clr_config_hooks_version(key, lxc_conf, NULL); | |
885 | ||
886 | ret = lxc_safe_uint(value, &tmp); | |
887 | if (ret < 0) | |
888 | return -1; | |
889 | ||
890 | if (tmp > 1) { | |
891 | ERROR("Invalid hook version specified. Currently only 0 " | |
892 | "(legacy) and 1 are supported"); | |
893 | return -1; | |
894 | } | |
895 | ||
896 | lxc_conf->hooks_version = tmp; | |
47903908 | 897 | |
44ae0fb6 CB |
898 | return 0; |
899 | } | |
900 | ||
713046e3 | 901 | static int set_config_personality(const char *key, const char *value, |
c7e27aaf | 902 | struct lxc_conf *lxc_conf, void *data) |
cccc74b5 | 903 | { |
525f0002 | 904 | signed long personality = lxc_config_parse_arch(value); |
cccc74b5 | 905 | |
525f0002 CS |
906 | if (personality >= 0) |
907 | lxc_conf->personality = personality; | |
908 | else | |
25a908b8 | 909 | WARN("Unsupported personality \"%s\"", value); |
970ab589 DL |
910 | |
911 | return 0; | |
cccc74b5 DL |
912 | } |
913 | ||
232763d6 CB |
914 | static int set_config_pty_max(const char *key, const char *value, |
915 | struct lxc_conf *lxc_conf, void *data) | |
10db618d | 916 | { |
e528c735 CB |
917 | int ret; |
918 | unsigned int max = 0; | |
919 | ||
663e9916 | 920 | if (lxc_config_value_empty(value)) { |
e528c735 | 921 | lxc_conf->pty_max = 0; |
884a4580 | 922 | return 0; |
ec200ce9 | 923 | } |
884a4580 | 924 | |
e528c735 CB |
925 | ret = lxc_safe_uint(value, &max); |
926 | if (ret < 0) | |
17919969 | 927 | return -1; |
10db618d | 928 | |
e528c735 | 929 | lxc_conf->pty_max = max; |
47903908 | 930 | |
10db618d | 931 | return 0; |
932 | } | |
933 | ||
a182feae CB |
934 | /* We only need to check whether the first byte of the key after the lxc.start. |
935 | * prefix matches our expectations since they fortunately all start with a | |
936 | * different letter. If anything was wrong with the key we would have already | |
937 | * noticed when the callback was called. | |
938 | */ | |
713046e3 | 939 | static int set_config_start(const char *key, const char *value, |
c7e27aaf | 940 | struct lxc_conf *lxc_conf, void *data) |
ee1e7aa0 | 941 | { |
ebb80f95 CB |
942 | bool is_empty; |
943 | ||
663e9916 | 944 | is_empty = lxc_config_value_empty(value); |
ebb80f95 | 945 | |
a182feae | 946 | if (*(key + 10) == 'a') { /* lxc.start.auto */ |
ebb80f95 CB |
947 | if (is_empty) { |
948 | lxc_conf->start_auto = 0; | |
949 | return 0; | |
950 | } | |
61ff8fc8 | 951 | |
3590152f CB |
952 | if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0) |
953 | return -1; | |
ebb80f95 | 954 | |
3590152f CB |
955 | if (lxc_conf->start_auto > 1) |
956 | return -1; | |
ebb80f95 | 957 | |
ee1e7aa0 | 958 | return 0; |
a182feae | 959 | } else if (*(key + 10) == 'd') { /* lxc.start.delay */ |
ebb80f95 CB |
960 | if (is_empty) { |
961 | lxc_conf->start_delay = 0; | |
962 | return 0; | |
963 | } | |
964 | ||
ebb80f95 | 965 | return lxc_safe_uint(value, &lxc_conf->start_delay); |
a182feae | 966 | } else if (*(key + 10) == 'o') { /* lxc.start.order */ |
ebb80f95 CB |
967 | if (is_empty) { |
968 | lxc_conf->start_order = 0; | |
969 | return 0; | |
970 | } | |
971 | ||
ebb80f95 | 972 | return lxc_safe_int(value, &lxc_conf->start_order); |
ee1e7aa0 | 973 | } |
ebb80f95 | 974 | |
ee1e7aa0 SG |
975 | return -1; |
976 | } | |
977 | ||
713046e3 | 978 | static int set_config_monitor(const char *key, const char *value, |
c7e27aaf | 979 | struct lxc_conf *lxc_conf, void *data) |
a8dfe4e0 | 980 | { |
663e9916 | 981 | if (lxc_config_value_empty(value)) { |
4ad9cd26 | 982 | lxc_conf->monitor_unshare = 0; |
a8dfe4e0 WB |
983 | return 0; |
984 | } | |
4ad9cd26 | 985 | |
a182feae | 986 | if (strcmp(key + 12, "unshare") == 0) |
4ad9cd26 CB |
987 | return lxc_safe_uint(value, &lxc_conf->monitor_unshare); |
988 | ||
a8dfe4e0 WB |
989 | return -1; |
990 | } | |
991 | ||
258f8051 CB |
992 | static int set_config_monitor_signal_pdeath(const char *key, const char *value, |
993 | struct lxc_conf *lxc_conf, void *data) | |
994 | { | |
995 | if (lxc_config_value_empty(value)) { | |
996 | lxc_conf->monitor_signal_pdeath = 0; | |
997 | return 0; | |
998 | } | |
999 | ||
1000 | if (strcmp(key + 12, "signal.pdeath") == 0) { | |
1001 | int sig_n; | |
1002 | ||
1003 | sig_n = sig_parse(value); | |
1004 | if (sig_n < 0) | |
1005 | return -1; | |
1006 | ||
1007 | lxc_conf->monitor_signal_pdeath = sig_n; | |
1008 | return 0; | |
1009 | } | |
1010 | ||
1011 | return -EINVAL; | |
1012 | } | |
1013 | ||
713046e3 | 1014 | static int set_config_group(const char *key, const char *value, |
c7e27aaf | 1015 | struct lxc_conf *lxc_conf, void *data) |
ee1e7aa0 | 1016 | { |
62dd965e | 1017 | char *groups, *token; |
ee1e7aa0 | 1018 | struct lxc_list *grouplist; |
62dd965e | 1019 | int ret = 0; |
ee1e7aa0 | 1020 | |
663e9916 | 1021 | if (lxc_config_value_empty(value)) |
ee1e7aa0 SG |
1022 | return lxc_clear_groups(lxc_conf); |
1023 | ||
1024 | groups = strdup(value); | |
25a908b8 | 1025 | if (!groups) |
ee1e7aa0 | 1026 | return -1; |
ee1e7aa0 | 1027 | |
25a908b8 CB |
1028 | /* In case several groups are specified in a single line split these |
1029 | * groups in a single element for the list. | |
504a2217 | 1030 | */ |
62dd965e | 1031 | lxc_iterate_parts(token, groups, " \t") { |
ee1e7aa0 | 1032 | grouplist = malloc(sizeof(*grouplist)); |
62dd965e CB |
1033 | if (!grouplist) { |
1034 | ret = -1; | |
ee1e7aa0 | 1035 | break; |
62dd965e | 1036 | } |
ee1e7aa0 SG |
1037 | |
1038 | grouplist->elem = strdup(token); | |
1039 | if (!grouplist->elem) { | |
ee1e7aa0 | 1040 | free(grouplist); |
62dd965e | 1041 | ret = -1; |
ee1e7aa0 SG |
1042 | break; |
1043 | } | |
1044 | ||
1045 | lxc_list_add_tail(&lxc_conf->groups, grouplist); | |
d028235d | 1046 | } |
ee1e7aa0 SG |
1047 | |
1048 | free(groups); | |
47903908 | 1049 | |
ee1e7aa0 SG |
1050 | return ret; |
1051 | } | |
1052 | ||
713046e3 | 1053 | static int set_config_environment(const char *key, const char *value, |
c7e27aaf | 1054 | struct lxc_conf *lxc_conf, void *data) |
7c661726 MP |
1055 | { |
1056 | struct lxc_list *list_item = NULL; | |
1057 | ||
663e9916 | 1058 | if (lxc_config_value_empty(value)) |
ab799c0b SG |
1059 | return lxc_clear_environment(lxc_conf); |
1060 | ||
7c661726 MP |
1061 | list_item = malloc(sizeof(*list_item)); |
1062 | if (!list_item) | |
504a2217 | 1063 | goto on_error; |
7c661726 | 1064 | |
5eab47bc CB |
1065 | if (!strchr(value, '=')) { |
1066 | const char *env_val; | |
1067 | const char *env_key = value; | |
1068 | const char *env_var[3] = {0}; | |
1069 | ||
1070 | env_val = getenv(env_key); | |
1071 | if (!env_val) | |
1072 | goto on_error; | |
1073 | ||
1074 | env_var[0] = env_key; | |
1075 | env_var[1] = env_val; | |
1076 | list_item->elem = lxc_string_join("=", env_var, false); | |
1077 | } else { | |
1078 | list_item->elem = strdup(value); | |
1079 | } | |
7c661726 MP |
1080 | |
1081 | if (!list_item->elem) | |
504a2217 | 1082 | goto on_error; |
7c661726 MP |
1083 | |
1084 | lxc_list_add_tail(&lxc_conf->environment, list_item); | |
1085 | ||
1086 | return 0; | |
1087 | ||
504a2217 | 1088 | on_error: |
f10fad2f | 1089 | free(list_item); |
47903908 | 1090 | |
7c661726 MP |
1091 | return -1; |
1092 | } | |
1093 | ||
fe1c5887 CB |
1094 | static int set_config_tty_max(const char *key, const char *value, |
1095 | struct lxc_conf *lxc_conf, void *data) | |
b0a33c1e | 1096 | { |
885766f5 CB |
1097 | int ret; |
1098 | unsigned int nbtty = 0; | |
1099 | ||
663e9916 | 1100 | if (lxc_config_value_empty(value)) { |
885766f5 | 1101 | lxc_conf->ttys.max = 0; |
fb12b12a | 1102 | return 0; |
cb508ee8 | 1103 | } |
fb12b12a | 1104 | |
885766f5 CB |
1105 | ret = lxc_safe_uint(value, &nbtty); |
1106 | if (ret < 0) | |
1107 | return -1; | |
1108 | ||
1109 | lxc_conf->ttys.max = nbtty; | |
47903908 | 1110 | |
885766f5 | 1111 | return 0; |
b0a33c1e | 1112 | } |
1113 | ||
42e53c29 | 1114 | static int set_config_tty_dir(const char *key, const char *value, |
c7e27aaf | 1115 | struct lxc_conf *lxc_conf, void *data) |
7c6ef2a2 | 1116 | { |
885766f5 | 1117 | return set_config_string_item_max(&lxc_conf->ttys.dir, value, |
504a2217 | 1118 | NAME_MAX + 1); |
7c6ef2a2 SH |
1119 | } |
1120 | ||
953fe44f CB |
1121 | static int set_config_apparmor_profile(const char *key, const char *value, |
1122 | struct lxc_conf *lxc_conf, void *data) | |
e075f5d9 | 1123 | { |
713046e3 | 1124 | return set_config_string_item(&lxc_conf->lsm_aa_profile, value); |
fe4de9a6 DE |
1125 | } |
1126 | ||
953fe44f CB |
1127 | static int set_config_apparmor_allow_incomplete(const char *key, |
1128 | const char *value, | |
1129 | struct lxc_conf *lxc_conf, | |
1130 | void *data) | |
7aff4f43 | 1131 | { |
663e9916 | 1132 | if (lxc_config_value_empty(value)) { |
cccfa758 | 1133 | lxc_conf->lsm_aa_allow_incomplete = 0; |
a678e9fa | 1134 | return 0; |
cccfa758 | 1135 | } |
a678e9fa | 1136 | |
a56e2df9 CB |
1137 | if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0) |
1138 | return -1; | |
7aff4f43 | 1139 | |
25a908b8 | 1140 | if (lxc_conf->lsm_aa_allow_incomplete > 1) |
a56e2df9 | 1141 | return -1; |
7aff4f43 SH |
1142 | |
1143 | return 0; | |
1144 | } | |
1145 | ||
1800f924 WB |
1146 | static int set_config_apparmor_allow_nesting(const char *key, |
1147 | const char *value, | |
1148 | struct lxc_conf *lxc_conf, | |
1149 | void *data) | |
1150 | { | |
1151 | if (lxc_config_value_empty(value)) | |
1152 | return clr_config_apparmor_allow_nesting(key, lxc_conf, NULL); | |
1153 | ||
1154 | if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_nesting) < 0) | |
1155 | return -1; | |
1156 | ||
1157 | if (lxc_conf->lsm_aa_allow_nesting > 1) | |
1158 | return -1; | |
1159 | ||
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | static int set_config_apparmor_raw(const char *key, | |
1164 | const char *value, | |
1165 | struct lxc_conf *lxc_conf, | |
1166 | void *data) | |
1167 | { | |
1168 | char *elem; | |
1169 | struct lxc_list *list; | |
1170 | ||
1171 | if (lxc_config_value_empty(value)) | |
1172 | return lxc_clear_apparmor_raw(lxc_conf); | |
1173 | ||
1174 | list = malloc(sizeof(*list)); | |
1175 | if (!list) { | |
1176 | errno = ENOMEM; | |
1177 | return -1; | |
1178 | } | |
1179 | ||
1180 | elem = strdup(value); | |
1181 | if (!elem) { | |
1182 | free(list); | |
1183 | return -1; | |
1184 | } | |
1185 | list->elem = elem; | |
1186 | ||
1187 | lxc_list_add_tail(&lxc_conf->lsm_aa_raw, list); | |
1188 | ||
1189 | return 0; | |
1190 | } | |
1191 | ||
953fe44f CB |
1192 | static int set_config_selinux_context(const char *key, const char *value, |
1193 | struct lxc_conf *lxc_conf, void *data) | |
fe4de9a6 | 1194 | { |
713046e3 | 1195 | return set_config_string_item(&lxc_conf->lsm_se_context, value); |
e075f5d9 | 1196 | } |
e075f5d9 | 1197 | |
46cc906d | 1198 | static int set_config_log_file(const char *key, const char *value, |
c7e27aaf | 1199 | struct lxc_conf *c, void *data) |
4a85ce2a | 1200 | { |
6d03d92a DE |
1201 | int ret; |
1202 | ||
663e9916 | 1203 | if (lxc_config_value_empty(value)) { |
0d601acb CB |
1204 | free(c->logfile); |
1205 | c->logfile = NULL; | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | /* Store these values in the lxc_conf, and then try to set for actual | |
504a2217 CB |
1210 | * current logging. |
1211 | */ | |
713046e3 | 1212 | ret = set_config_path_item(&c->logfile, value); |
6d03d92a | 1213 | if (ret == 0) |
858377e4 | 1214 | ret = lxc_log_set_file(&c->logfd, c->logfile); |
25a908b8 | 1215 | |
6d03d92a | 1216 | return ret; |
4a85ce2a SH |
1217 | } |
1218 | ||
46cc906d | 1219 | static int set_config_log_level(const char *key, const char *value, |
c7e27aaf | 1220 | struct lxc_conf *lxc_conf, void *data) |
4a85ce2a | 1221 | { |
9ea87d5d SH |
1222 | int newlevel; |
1223 | ||
663e9916 | 1224 | if (lxc_config_value_empty(value)) { |
4b73005c | 1225 | lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET; |
4a85ce2a | 1226 | return 0; |
575b9745 | 1227 | } |
4a85ce2a | 1228 | |
a56e2df9 CB |
1229 | if (value[0] >= '0' && value[0] <= '9') { |
1230 | if (lxc_safe_int(value, &newlevel) < 0) | |
1231 | return -1; | |
1232 | } else { | |
9ea87d5d | 1233 | newlevel = lxc_log_priority_to_int(value); |
a56e2df9 | 1234 | } |
575b9745 | 1235 | |
504a2217 | 1236 | /* Store these values in the lxc_conf, and then try to set for actual |
575b9745 CB |
1237 | * current logging. |
1238 | */ | |
b40a606e | 1239 | lxc_conf->loglevel = newlevel; |
47903908 | 1240 | |
858377e4 | 1241 | return lxc_log_set_level(&lxc_conf->loglevel, newlevel); |
4a85ce2a SH |
1242 | } |
1243 | ||
713046e3 | 1244 | static int set_config_autodev(const char *key, const char *value, |
c7e27aaf | 1245 | struct lxc_conf *lxc_conf, void *data) |
c6883f38 | 1246 | { |
663e9916 | 1247 | if (lxc_config_value_empty(value)) { |
1045031e | 1248 | lxc_conf->autodev = 0; |
180abbc0 | 1249 | return 0; |
1045031e | 1250 | } |
180abbc0 | 1251 | |
ff6cb4ed CB |
1252 | if (lxc_safe_uint(value, &lxc_conf->autodev) < 0) |
1253 | return -1; | |
c6883f38 | 1254 | |
25a908b8 | 1255 | if (lxc_conf->autodev > 1) |
ff6cb4ed | 1256 | return -1; |
c6883f38 SH |
1257 | |
1258 | return 0; | |
1259 | } | |
1260 | ||
55c84efc | 1261 | static int set_config_signal_halt(const char *key, const char *value, |
c7e27aaf | 1262 | struct lxc_conf *lxc_conf, void *data) |
f0f1d8c0 | 1263 | { |
62a085fb | 1264 | int sig_n; |
f0f1d8c0 | 1265 | |
663e9916 | 1266 | if (lxc_config_value_empty(value)) { |
c1a64603 | 1267 | lxc_conf->haltsignal = 0; |
955912f0 | 1268 | return 0; |
c1a64603 | 1269 | } |
955912f0 | 1270 | |
62a085fb | 1271 | sig_n = sig_parse(value); |
f0f1d8c0 DE |
1272 | if (sig_n < 0) |
1273 | return -1; | |
25a908b8 | 1274 | |
f0f1d8c0 DE |
1275 | lxc_conf->haltsignal = sig_n; |
1276 | ||
1277 | return 0; | |
1278 | } | |
1279 | ||
55c84efc | 1280 | static int set_config_signal_reboot(const char *key, const char *value, |
c7e27aaf | 1281 | struct lxc_conf *lxc_conf, void *data) |
dd267776 | 1282 | { |
9d7e7587 | 1283 | int sig_n; |
dd267776 | 1284 | |
663e9916 | 1285 | if (lxc_config_value_empty(value)) { |
18fcee44 | 1286 | lxc_conf->rebootsignal = 0; |
9d7e7587 | 1287 | return 0; |
18fcee44 | 1288 | } |
9d7e7587 CB |
1289 | |
1290 | sig_n = sig_parse(value); | |
dd267776 BP |
1291 | if (sig_n < 0) |
1292 | return -1; | |
25a908b8 | 1293 | |
dd267776 BP |
1294 | lxc_conf->rebootsignal = sig_n; |
1295 | ||
1296 | return 0; | |
1297 | } | |
1298 | ||
55c84efc | 1299 | static int set_config_signal_stop(const char *key, const char *value, |
c7e27aaf | 1300 | struct lxc_conf *lxc_conf, void *data) |
a84b9932 | 1301 | { |
6ca6aedd | 1302 | int sig_n; |
a84b9932 | 1303 | |
663e9916 | 1304 | if (lxc_config_value_empty(value)) { |
4100d1a7 | 1305 | lxc_conf->stopsignal = 0; |
6ca6aedd | 1306 | return 0; |
4100d1a7 | 1307 | } |
6ca6aedd CB |
1308 | |
1309 | sig_n = sig_parse(value); | |
a84b9932 AV |
1310 | if (sig_n < 0) |
1311 | return -1; | |
25a908b8 | 1312 | |
a84b9932 AV |
1313 | lxc_conf->stopsignal = sig_n; |
1314 | ||
1315 | return 0; | |
1316 | } | |
1317 | ||
54860ed0 CB |
1318 | static int __set_config_cgroup_controller(const char *key, const char *value, |
1319 | struct lxc_conf *lxc_conf, int version) | |
576f946d | 1320 | { |
54860ed0 CB |
1321 | const char *subkey, *token; |
1322 | size_t token_len; | |
bf83c5b9 MS |
1323 | struct lxc_list *cglist = NULL; |
1324 | struct lxc_cgroup *cgelem = NULL; | |
576f946d | 1325 | |
663e9916 | 1326 | if (lxc_config_value_empty(value)) |
54860ed0 CB |
1327 | return lxc_clear_cgroups(lxc_conf, key, version); |
1328 | ||
1329 | if (version == CGROUP2_SUPER_MAGIC) { | |
1330 | token = "lxc.cgroup2."; | |
1331 | token_len = 12; | |
1332 | } else if (version == CGROUP_SUPER_MAGIC) { | |
1333 | token = "lxc.cgroup."; | |
1334 | token_len = 11; | |
1335 | } else { | |
1336 | return -EINVAL; | |
1337 | } | |
576f946d | 1338 | |
54860ed0 CB |
1339 | if (strncmp(key, token, token_len) != 0) |
1340 | return -EINVAL; | |
a871ff6b | 1341 | |
54860ed0 CB |
1342 | subkey = key + token_len; |
1343 | if (*subkey == '\0') | |
1344 | return -EINVAL; | |
576f946d | 1345 | |
1346 | cglist = malloc(sizeof(*cglist)); | |
1347 | if (!cglist) | |
bf83c5b9 | 1348 | goto out; |
576f946d | 1349 | |
1350 | cgelem = malloc(sizeof(*cgelem)); | |
bf83c5b9 MS |
1351 | if (!cgelem) |
1352 | goto out; | |
1353 | memset(cgelem, 0, sizeof(*cgelem)); | |
576f946d | 1354 | |
1355 | cgelem->subsystem = strdup(subkey); | |
54860ed0 CB |
1356 | if (!cgelem->subsystem) |
1357 | goto out; | |
bf83c5b9 | 1358 | |
54860ed0 CB |
1359 | cgelem->value = strdup(value); |
1360 | if (!cgelem->value) | |
bf83c5b9 MS |
1361 | goto out; |
1362 | ||
54860ed0 CB |
1363 | cgelem->version = version; |
1364 | ||
1365 | lxc_list_add_elem(cglist, cgelem); | |
576f946d | 1366 | |
54860ed0 CB |
1367 | if (version == CGROUP2_SUPER_MAGIC) |
1368 | lxc_list_add_tail(&lxc_conf->cgroup2, cglist); | |
1369 | else | |
1370 | lxc_list_add_tail(&lxc_conf->cgroup, cglist); | |
576f946d | 1371 | |
1372 | return 0; | |
bf83c5b9 MS |
1373 | |
1374 | out: | |
f10fad2f | 1375 | free(cglist); |
bf83c5b9 | 1376 | if (cgelem) { |
f10fad2f | 1377 | free(cgelem->subsystem); |
f10fad2f | 1378 | free(cgelem->value); |
bf83c5b9 MS |
1379 | free(cgelem); |
1380 | } | |
1381 | ||
1382 | return -1; | |
576f946d | 1383 | } |
1384 | ||
54860ed0 CB |
1385 | static int set_config_cgroup_controller(const char *key, const char *value, |
1386 | struct lxc_conf *lxc_conf, void *data) | |
1387 | { | |
1388 | return __set_config_cgroup_controller(key, value, lxc_conf, | |
1389 | CGROUP_SUPER_MAGIC); | |
1390 | } | |
1391 | ||
1392 | static int set_config_cgroup2_controller(const char *key, const char *value, | |
1393 | struct lxc_conf *lxc_conf, void *data) | |
1394 | { | |
1395 | return __set_config_cgroup_controller(key, value, lxc_conf, | |
1396 | CGROUP2_SUPER_MAGIC); | |
1397 | } | |
1398 | ||
1399 | ||
43654d34 CB |
1400 | static int set_config_cgroup_dir(const char *key, const char *value, |
1401 | struct lxc_conf *lxc_conf, void *data) | |
1402 | { | |
1403 | if (lxc_config_value_empty(value)) | |
1404 | return clr_config_cgroup_dir(key, lxc_conf, NULL); | |
1405 | ||
43654d34 CB |
1406 | return set_config_string_item(&lxc_conf->cgroup_meta.dir, value); |
1407 | } | |
1408 | ||
9caee129 CB |
1409 | static int set_config_cgroup_relative(const char *key, const char *value, |
1410 | struct lxc_conf *lxc_conf, void *data) | |
76f0e2e7 CB |
1411 | { |
1412 | unsigned int converted; | |
1413 | int ret; | |
1414 | ||
1415 | if (lxc_config_value_empty(value)) | |
9caee129 | 1416 | return clr_config_cgroup_relative(key, lxc_conf, NULL); |
76f0e2e7 CB |
1417 | |
1418 | ret = lxc_safe_uint(value, &converted); | |
1419 | if (ret < 0) | |
1420 | return -ret; | |
1421 | ||
1422 | if (converted == 1) { | |
9caee129 | 1423 | lxc_conf->cgroup_meta.relative = true; |
76f0e2e7 CB |
1424 | return 0; |
1425 | } | |
1426 | ||
1427 | if (converted == 0) { | |
9caee129 | 1428 | lxc_conf->cgroup_meta.relative = false; |
76f0e2e7 CB |
1429 | return 0; |
1430 | } | |
1431 | ||
1432 | return -EINVAL; | |
1433 | } | |
1434 | ||
240d4b74 | 1435 | static int set_config_prlimit(const char *key, const char *value, |
c7e27aaf | 1436 | struct lxc_conf *lxc_conf, void *data) |
c6d09e15 | 1437 | { |
c6d09e15 WB |
1438 | struct lxc_list *iter; |
1439 | struct rlimit limit; | |
71460831 | 1440 | rlim_t limit_value; |
504a2217 CB |
1441 | struct lxc_list *limlist = NULL; |
1442 | struct lxc_limit *limelem = NULL; | |
c6d09e15 | 1443 | |
663e9916 | 1444 | if (lxc_config_value_empty(value)) |
c6d09e15 WB |
1445 | return lxc_clear_limits(lxc_conf, key); |
1446 | ||
6333c915 | 1447 | if (strncmp(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0) |
c6d09e15 WB |
1448 | return -1; |
1449 | ||
6333c915 | 1450 | key += STRLITERALLEN("lxc.prlimit."); |
c6d09e15 WB |
1451 | |
1452 | /* soft limit comes first in the value */ | |
1453 | if (!parse_limit_value(&value, &limit_value)) | |
1454 | return -1; | |
47903908 | 1455 | |
c6d09e15 WB |
1456 | limit.rlim_cur = limit_value; |
1457 | ||
1458 | /* skip spaces and a colon */ | |
1459 | while (isspace(*value)) | |
1460 | ++value; | |
504a2217 | 1461 | |
c6d09e15 WB |
1462 | if (*value == ':') |
1463 | ++value; | |
1464 | else if (*value) /* any other character is an error here */ | |
1465 | return -1; | |
504a2217 | 1466 | |
c6d09e15 WB |
1467 | while (isspace(*value)) |
1468 | ++value; | |
1469 | ||
1470 | /* optional hard limit */ | |
1471 | if (*value) { | |
1472 | if (!parse_limit_value(&value, &limit_value)) | |
1473 | return -1; | |
47903908 | 1474 | |
c6d09e15 | 1475 | limit.rlim_max = limit_value; |
504a2217 | 1476 | |
c6d09e15 WB |
1477 | /* check for trailing garbage */ |
1478 | while (isspace(*value)) | |
1479 | ++value; | |
504a2217 | 1480 | |
c6d09e15 WB |
1481 | if (*value) |
1482 | return -1; | |
1483 | } else { | |
1484 | /* a single value sets both hard and soft limit */ | |
1485 | limit.rlim_max = limit.rlim_cur; | |
1486 | } | |
1487 | ||
1488 | /* find existing list element */ | |
7edd0540 | 1489 | lxc_list_for_each(iter, &lxc_conf->limits) { |
c6d09e15 WB |
1490 | limelem = iter->elem; |
1491 | if (!strcmp(key, limelem->resource)) { | |
1492 | limelem->limit = limit; | |
1493 | return 0; | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | /* allocate list element */ | |
1498 | limlist = malloc(sizeof(*limlist)); | |
1499 | if (!limlist) | |
7edd0540 | 1500 | goto on_error; |
2e6e3feb | 1501 | |
c6d09e15 WB |
1502 | limelem = malloc(sizeof(*limelem)); |
1503 | if (!limelem) | |
7edd0540 | 1504 | goto on_error; |
c6d09e15 WB |
1505 | memset(limelem, 0, sizeof(*limelem)); |
1506 | ||
1507 | limelem->resource = strdup(key); | |
1508 | if (!limelem->resource) | |
7edd0540 | 1509 | goto on_error; |
c6d09e15 | 1510 | |
47903908 | 1511 | limelem->limit = limit; |
7edd0540 | 1512 | lxc_list_add_elem(limlist, limelem);; |
c6d09e15 WB |
1513 | lxc_list_add_tail(&lxc_conf->limits, limlist); |
1514 | ||
1515 | return 0; | |
1516 | ||
7edd0540 | 1517 | on_error: |
c6d09e15 | 1518 | free(limlist); |
47903908 | 1519 | |
c6d09e15 WB |
1520 | if (limelem) { |
1521 | free(limelem->resource); | |
1522 | free(limelem); | |
1523 | } | |
47903908 | 1524 | |
c6d09e15 WB |
1525 | return -1; |
1526 | } | |
1527 | ||
7edd0540 L |
1528 | static int set_config_sysctl(const char *key, const char *value, |
1529 | struct lxc_conf *lxc_conf, void *data) | |
1530 | { | |
1531 | struct lxc_list *iter; | |
e409b214 | 1532 | char *replace_value = NULL; |
7edd0540 L |
1533 | struct lxc_list *sysctl_list = NULL; |
1534 | struct lxc_sysctl *sysctl_elem = NULL; | |
7edd0540 L |
1535 | |
1536 | if (lxc_config_value_empty(value)) | |
e409b214 | 1537 | return clr_config_sysctl(key, lxc_conf, NULL); |
7edd0540 | 1538 | |
6333c915 | 1539 | if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0) |
7edd0540 L |
1540 | return -1; |
1541 | ||
6333c915 | 1542 | key += STRLITERALLEN("lxc.sysctl."); |
7edd0540 L |
1543 | |
1544 | /* find existing list element */ | |
1545 | lxc_list_for_each(iter, &lxc_conf->sysctls) { | |
1546 | sysctl_elem = iter->elem; | |
e409b214 CB |
1547 | |
1548 | if (strcmp(key, sysctl_elem->key) != 0) | |
1549 | continue; | |
1550 | ||
1551 | replace_value = strdup(value); | |
1552 | if (!replace_value) | |
1553 | return -1; | |
1554 | ||
1555 | free(sysctl_elem->value); | |
1556 | sysctl_elem->value = replace_value; | |
47903908 | 1557 | |
e409b214 | 1558 | return 0; |
7edd0540 L |
1559 | } |
1560 | ||
1561 | /* allocate list element */ | |
1562 | sysctl_list = malloc(sizeof(*sysctl_list)); | |
1563 | if (!sysctl_list) | |
1564 | goto on_error; | |
1565 | ||
1566 | sysctl_elem = malloc(sizeof(*sysctl_elem)); | |
1567 | if (!sysctl_elem) | |
1568 | goto on_error; | |
1569 | memset(sysctl_elem, 0, sizeof(*sysctl_elem)); | |
1570 | ||
1571 | sysctl_elem->key = strdup(key); | |
1572 | if (!sysctl_elem->key) | |
1573 | goto on_error; | |
1574 | ||
1575 | sysctl_elem->value = strdup(value); | |
1576 | if (!sysctl_elem->value) | |
1577 | goto on_error; | |
1578 | ||
1579 | lxc_list_add_elem(sysctl_list, sysctl_elem); | |
7edd0540 L |
1580 | lxc_list_add_tail(&lxc_conf->sysctls, sysctl_list); |
1581 | ||
1582 | return 0; | |
1583 | ||
1584 | on_error: | |
1585 | free(sysctl_list); | |
47903908 | 1586 | |
7edd0540 L |
1587 | if (sysctl_elem) { |
1588 | free(sysctl_elem->key); | |
1589 | free(sysctl_elem->value); | |
1590 | free(sysctl_elem); | |
1591 | } | |
47903908 | 1592 | |
7edd0540 | 1593 | return -1; |
7edd0540 L |
1594 | } |
1595 | ||
61d7a733 YT |
1596 | static int set_config_proc(const char *key, const char *value, |
1597 | struct lxc_conf *lxc_conf, void *data) | |
1598 | { | |
1599 | const char *subkey; | |
1600 | struct lxc_list *proclist = NULL; | |
1601 | struct lxc_proc *procelem = NULL; | |
1602 | ||
1603 | if (lxc_config_value_empty(value)) | |
1604 | return clr_config_proc(key, lxc_conf, NULL); | |
1605 | ||
6333c915 | 1606 | if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0) |
61d7a733 YT |
1607 | return -1; |
1608 | ||
6333c915 | 1609 | subkey = key + STRLITERALLEN("lxc.proc."); |
61d7a733 YT |
1610 | if (*subkey == '\0') |
1611 | return -EINVAL; | |
1612 | ||
1613 | proclist = malloc(sizeof(*proclist)); | |
1614 | if (!proclist) | |
1615 | goto on_error; | |
1616 | ||
1617 | procelem = malloc(sizeof(*procelem)); | |
1618 | if (!procelem) | |
1619 | goto on_error; | |
1620 | memset(procelem, 0, sizeof(*procelem)); | |
1621 | ||
1622 | procelem->filename = strdup(subkey); | |
1623 | procelem->value = strdup(value); | |
1624 | ||
1625 | if (!procelem->filename || !procelem->value) | |
1626 | goto on_error; | |
1627 | ||
1628 | proclist->elem = procelem; | |
1629 | ||
1630 | lxc_list_add_tail(&lxc_conf->procs, proclist); | |
1631 | ||
1632 | return 0; | |
1633 | ||
1634 | on_error: | |
1635 | free(proclist); | |
47903908 | 1636 | |
61d7a733 YT |
1637 | if (procelem) { |
1638 | free(procelem->filename); | |
1639 | free(procelem->value); | |
1640 | free(procelem); | |
1641 | } | |
1642 | ||
1643 | return -1; | |
1644 | } | |
1645 | ||
5014ff2e | 1646 | static int set_config_idmaps(const char *key, const char *value, |
c7e27aaf | 1647 | struct lxc_conf *lxc_conf, void *data) |
f6d3e3e4 | 1648 | { |
251d0d2a | 1649 | unsigned long hostid, nsid, range; |
f6d3e3e4 | 1650 | char type; |
0b843d35 | 1651 | int ret; |
34a7a4c6 CB |
1652 | struct lxc_list *idmaplist = NULL; |
1653 | struct id_map *idmap = NULL; | |
f6d3e3e4 | 1654 | |
663e9916 | 1655 | if (lxc_config_value_empty(value)) |
7d0eb87e SH |
1656 | return lxc_clear_idmaps(lxc_conf); |
1657 | ||
f6d3e3e4 SH |
1658 | idmaplist = malloc(sizeof(*idmaplist)); |
1659 | if (!idmaplist) | |
34a7a4c6 | 1660 | goto on_error; |
f6d3e3e4 SH |
1661 | |
1662 | idmap = malloc(sizeof(*idmap)); | |
1663 | if (!idmap) | |
34a7a4c6 | 1664 | goto on_error; |
f6d3e3e4 SH |
1665 | memset(idmap, 0, sizeof(*idmap)); |
1666 | ||
0b843d35 | 1667 | ret = parse_idmaps(value, &type, &nsid, &hostid, &range); |
a8b1ac78 | 1668 | if (ret < 0) { |
f37d1c22 | 1669 | ERROR("Failed to parse id mappings"); |
34a7a4c6 | 1670 | goto on_error; |
a8b1ac78 | 1671 | } |
34a7a4c6 | 1672 | |
25a908b8 | 1673 | INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range); |
ac7725e7 | 1674 | if (type == 'u') |
f6d3e3e4 | 1675 | idmap->idtype = ID_TYPE_UID; |
ac7725e7 | 1676 | else if (type == 'g') |
f6d3e3e4 SH |
1677 | idmap->idtype = ID_TYPE_GID; |
1678 | else | |
34a7a4c6 | 1679 | goto on_error; |
7e60c3f0 | 1680 | |
f6d3e3e4 SH |
1681 | idmap->hostid = hostid; |
1682 | idmap->nsid = nsid; | |
1683 | idmap->range = range; | |
7e60c3f0 SG |
1684 | idmaplist->elem = idmap; |
1685 | lxc_list_add_tail(&lxc_conf->id_map, idmaplist); | |
46ad64ab CB |
1686 | |
1687 | if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID) | |
1688 | if (idmap->nsid == 0) | |
1689 | lxc_conf->root_nsuid_map = idmap; | |
1690 | ||
4160c3a0 | 1691 | if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID) |
46ad64ab CB |
1692 | if (idmap->nsid == 0) |
1693 | lxc_conf->root_nsgid_map = idmap; | |
1694 | ||
34a7a4c6 | 1695 | idmap = NULL; |
7e60c3f0 | 1696 | |
f6d3e3e4 SH |
1697 | return 0; |
1698 | ||
34a7a4c6 | 1699 | on_error: |
f10fad2f | 1700 | free(idmaplist); |
34a7a4c6 | 1701 | free(idmap); |
f6d3e3e4 SH |
1702 | |
1703 | return -1; | |
1704 | } | |
1705 | ||
47148e96 CB |
1706 | static int set_config_mount_fstab(const char *key, const char *value, |
1707 | struct lxc_conf *lxc_conf, void *data) | |
d95db067 | 1708 | { |
663e9916 | 1709 | if (lxc_config_value_empty(value)) { |
47148e96 | 1710 | clr_config_mount_fstab(key, lxc_conf, NULL); |
d9192f5d | 1711 | return -1; |
46f3de30 | 1712 | } |
6f5685f0 | 1713 | |
713046e3 | 1714 | return set_config_path_item(&lxc_conf->fstab, value); |
d95db067 DE |
1715 | } |
1716 | ||
713046e3 | 1717 | static int set_config_mount_auto(const char *key, const char *value, |
c7e27aaf | 1718 | struct lxc_conf *lxc_conf, void *data) |
368bbc02 | 1719 | { |
62dd965e | 1720 | char *autos, *token; |
368bbc02 CS |
1721 | int i; |
1722 | int ret = -1; | |
504a2217 CB |
1723 | static struct { |
1724 | const char *token; | |
1725 | int mask; | |
1726 | int flag; | |
1727 | } allowed_auto_mounts[] = { | |
6812d833 CB |
1728 | { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, |
1729 | { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, | |
1730 | { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW }, | |
1731 | { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, | |
1732 | { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO }, | |
1733 | { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, | |
1734 | { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW }, | |
1735 | { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC }, | |
1736 | { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED }, | |
1737 | { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO }, | |
1738 | { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW }, | |
1739 | { "cgroup:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC | LXC_AUTO_CGROUP_FORCE }, | |
1740 | { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED | LXC_AUTO_CGROUP_FORCE }, | |
1741 | { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO | LXC_AUTO_CGROUP_FORCE }, | |
1742 | { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW | LXC_AUTO_CGROUP_FORCE }, | |
1743 | { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC }, | |
1744 | { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED }, | |
1745 | { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO }, | |
1746 | { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW }, | |
1747 | { "cgroup-full:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC | LXC_AUTO_CGROUP_FORCE }, | |
1748 | { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED | LXC_AUTO_CGROUP_FORCE }, | |
1749 | { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO | LXC_AUTO_CGROUP_FORCE }, | |
1750 | { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW | LXC_AUTO_CGROUP_FORCE }, | |
adf0ba1f | 1751 | { "shmounts:", LXC_AUTO_SHMOUNTS_MASK, LXC_AUTO_SHMOUNTS }, |
25a908b8 | 1752 | /* For adding anything that is just a single on/off, but has no |
3f69fb12 SY |
1753 | * options: keep mask and flag identical and just define the enum |
1754 | * value as an unused bit so far | |
504a2217 | 1755 | */ |
3f69fb12 | 1756 | { NULL, 0, 0 } |
504a2217 | 1757 | }; |
368bbc02 | 1758 | |
663e9916 | 1759 | if (lxc_config_value_empty(value)) { |
d9192f5d SH |
1760 | lxc_conf->auto_mounts = 0; |
1761 | return 0; | |
1762 | } | |
368bbc02 CS |
1763 | |
1764 | autos = strdup(value); | |
25a908b8 | 1765 | if (!autos) |
368bbc02 | 1766 | return -1; |
368bbc02 | 1767 | |
62dd965e | 1768 | lxc_iterate_parts(token, autos, " \t") { |
0d190408 LT |
1769 | bool is_shmounts = false; |
1770 | ||
368bbc02 CS |
1771 | for (i = 0; allowed_auto_mounts[i].token; i++) { |
1772 | if (!strcmp(allowed_auto_mounts[i].token, token)) | |
1773 | break; | |
0d190408 | 1774 | |
fd14fdb8 | 1775 | if (strcmp("shmounts:", allowed_auto_mounts[i].token) == 0 && |
6333c915 | 1776 | strncmp("shmounts:", token, STRLITERALLEN("shmounts:")) == 0) { |
0d190408 LT |
1777 | is_shmounts = true; |
1778 | break; | |
1779 | } | |
368bbc02 CS |
1780 | } |
1781 | ||
1782 | if (!allowed_auto_mounts[i].token) { | |
25a908b8 | 1783 | ERROR("Invalid filesystem to automount \"%s\"", token); |
62dd965e | 1784 | goto on_error; |
368bbc02 CS |
1785 | } |
1786 | ||
b06b8511 | 1787 | lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask; |
368bbc02 | 1788 | lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag; |
47903908 | 1789 | |
0d190408 | 1790 | if (is_shmounts) { |
6333c915 | 1791 | lxc_conf->shmount.path_host = strdup(token + STRLITERALLEN("shmounts:")); |
fd14fdb8 LT |
1792 | if (!lxc_conf->shmount.path_host) { |
1793 | SYSERROR("Failed to copy shmounts host path"); | |
62dd965e | 1794 | goto on_error; |
fd14fdb8 LT |
1795 | } |
1796 | ||
7a41e857 | 1797 | if (strcmp(lxc_conf->shmount.path_host, "") == 0) { |
0d190408 | 1798 | ERROR("Invalid shmounts path: empty"); |
62dd965e | 1799 | goto on_error; |
0d190408 | 1800 | } |
fd14fdb8 | 1801 | |
7a41e857 | 1802 | lxc_conf->shmount.path_cont = strdup("/dev/.lxc-mounts"); |
fd14fdb8 LT |
1803 | if(!lxc_conf->shmount.path_cont) { |
1804 | SYSERROR("Failed to copy shmounts container path"); | |
62dd965e | 1805 | goto on_error; |
fd14fdb8 | 1806 | } |
0d190408 | 1807 | } |
d028235d | 1808 | } |
368bbc02 | 1809 | |
62dd965e CB |
1810 | ret = 0; |
1811 | ||
1812 | on_error: | |
368bbc02 | 1813 | free(autos); |
62dd965e | 1814 | |
368bbc02 CS |
1815 | return ret; |
1816 | } | |
1817 | ||
713046e3 | 1818 | static int set_config_mount(const char *key, const char *value, |
c7e27aaf | 1819 | struct lxc_conf *lxc_conf, void *data) |
e7938e9e | 1820 | { |
e7938e9e MN |
1821 | char *mntelem; |
1822 | struct lxc_list *mntlist; | |
1823 | ||
663e9916 | 1824 | if (lxc_config_value_empty(value)) |
d9192f5d | 1825 | return lxc_clear_mount_entries(lxc_conf); |
e7938e9e MN |
1826 | |
1827 | mntlist = malloc(sizeof(*mntlist)); | |
1828 | if (!mntlist) | |
1829 | return -1; | |
1830 | ||
1831 | mntelem = strdup(value); | |
00b6be44 SH |
1832 | if (!mntelem) { |
1833 | free(mntlist); | |
bf83c5b9 | 1834 | return -1; |
00b6be44 | 1835 | } |
e7938e9e MN |
1836 | mntlist->elem = mntelem; |
1837 | ||
1838 | lxc_list_add_tail(&lxc_conf->mount_list, mntlist); | |
1839 | ||
1840 | return 0; | |
1841 | } | |
1842 | ||
0d190408 LT |
1843 | int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) { |
1844 | return set_config_mount(NULL, value, lxc_conf, NULL); | |
1845 | } | |
1846 | ||
713046e3 | 1847 | static int set_config_cap_keep(const char *key, const char *value, |
c7e27aaf | 1848 | struct lxc_conf *lxc_conf, void *data) |
1fb86a7c | 1849 | { |
62dd965e | 1850 | char *keepcaps, *token; |
1fb86a7c SH |
1851 | struct lxc_list *keeplist; |
1852 | int ret = -1; | |
1853 | ||
663e9916 | 1854 | if (lxc_config_value_empty(value)) |
7d0eb87e | 1855 | return lxc_clear_config_keepcaps(lxc_conf); |
1fb86a7c SH |
1856 | |
1857 | keepcaps = strdup(value); | |
25a908b8 | 1858 | if (!keepcaps) |
1fb86a7c | 1859 | return -1; |
1fb86a7c | 1860 | |
504a2217 CB |
1861 | /* In case several capability keep is specified in a single line |
1862 | * split these caps in a single element for the list. | |
1863 | */ | |
62dd965e | 1864 | lxc_iterate_parts(token, keepcaps, " \t") { |
7035407c DE |
1865 | if (!strcmp(token, "none")) |
1866 | lxc_clear_config_keepcaps(lxc_conf); | |
1867 | ||
1fb86a7c | 1868 | keeplist = malloc(sizeof(*keeplist)); |
25a908b8 | 1869 | if (!keeplist) |
62dd965e | 1870 | goto on_error; |
1fb86a7c SH |
1871 | |
1872 | keeplist->elem = strdup(token); | |
1873 | if (!keeplist->elem) { | |
1fb86a7c | 1874 | free(keeplist); |
62dd965e | 1875 | goto on_error; |
1fb86a7c SH |
1876 | } |
1877 | ||
1878 | lxc_list_add_tail(&lxc_conf->keepcaps, keeplist); | |
d028235d | 1879 | } |
1fb86a7c | 1880 | |
62dd965e CB |
1881 | ret = 0; |
1882 | ||
1883 | on_error: | |
1fb86a7c SH |
1884 | free(keepcaps); |
1885 | ||
1886 | return ret; | |
1887 | } | |
1888 | ||
713046e3 | 1889 | static int set_config_cap_drop(const char *key, const char *value, |
c7e27aaf | 1890 | struct lxc_conf *lxc_conf, void *data) |
81810dd1 | 1891 | { |
62dd965e | 1892 | char *dropcaps, *token; |
81810dd1 DL |
1893 | struct lxc_list *droplist; |
1894 | int ret = -1; | |
1895 | ||
663e9916 | 1896 | if (lxc_config_value_empty(value)) |
7d0eb87e | 1897 | return lxc_clear_config_caps(lxc_conf); |
81810dd1 DL |
1898 | |
1899 | dropcaps = strdup(value); | |
25a908b8 | 1900 | if (!dropcaps) |
81810dd1 | 1901 | return -1; |
81810dd1 | 1902 | |
504a2217 CB |
1903 | /* In case several capability drop is specified in a single line |
1904 | * split these caps in a single element for the list. | |
1905 | */ | |
62dd965e | 1906 | lxc_iterate_parts(token, dropcaps, " \t") { |
81810dd1 | 1907 | droplist = malloc(sizeof(*droplist)); |
25a908b8 | 1908 | if (!droplist) |
62dd965e | 1909 | goto on_error; |
81810dd1 DL |
1910 | |
1911 | droplist->elem = strdup(token); | |
1912 | if (!droplist->elem) { | |
81810dd1 | 1913 | free(droplist); |
62dd965e | 1914 | goto on_error; |
81810dd1 DL |
1915 | } |
1916 | ||
1917 | lxc_list_add_tail(&lxc_conf->caps, droplist); | |
d028235d | 1918 | } |
81810dd1 | 1919 | |
62dd965e CB |
1920 | ret = 0; |
1921 | ||
1922 | on_error: | |
81810dd1 DL |
1923 | free(dropcaps); |
1924 | ||
1925 | return ret; | |
1926 | } | |
1927 | ||
3aed4934 CB |
1928 | static int set_config_console_path(const char *key, const char *value, |
1929 | struct lxc_conf *lxc_conf, void *data) | |
28a4b0e5 | 1930 | { |
713046e3 | 1931 | return set_config_path_item(&lxc_conf->console.path, value); |
28a4b0e5 DL |
1932 | } |
1933 | ||
d91adfa6 CB |
1934 | static int set_config_console_rotate(const char *key, const char *value, |
1935 | struct lxc_conf *lxc_conf, void *data) | |
1936 | { | |
1937 | if (lxc_config_value_empty(value)) { | |
1938 | lxc_conf->console.log_rotate = 0; | |
1939 | return 0; | |
1940 | } | |
1941 | ||
1942 | if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0) | |
1943 | return -1; | |
1944 | ||
966b9ecd CB |
1945 | if (lxc_conf->console.log_rotate > 1) { |
1946 | ERROR("The \"lxc.console.rotate\" config key can only be set " | |
1947 | "to 0 or 1"); | |
d91adfa6 | 1948 | return -1; |
966b9ecd | 1949 | } |
d91adfa6 CB |
1950 | |
1951 | return 0; | |
1952 | } | |
1953 | ||
713046e3 | 1954 | static int set_config_console_logfile(const char *key, const char *value, |
c7e27aaf | 1955 | struct lxc_conf *lxc_conf, void *data) |
96f15ca1 | 1956 | { |
713046e3 | 1957 | return set_config_path_item(&lxc_conf->console.log_path, value); |
96f15ca1 SH |
1958 | } |
1959 | ||
28f3b1cd CB |
1960 | static int set_config_console_buffer_size(const char *key, const char *value, |
1961 | struct lxc_conf *lxc_conf, void *data) | |
a04220de CB |
1962 | { |
1963 | int ret; | |
1964 | int64_t size; | |
28f3b1cd | 1965 | uint64_t buffer_size, pgsz; |
a04220de CB |
1966 | |
1967 | if (lxc_config_value_empty(value)) { | |
28f3b1cd | 1968 | lxc_conf->console.buffer_size = 0; |
a04220de CB |
1969 | return 0; |
1970 | } | |
1971 | ||
1972 | /* If the user specified "auto" the default log size is 2^17 = 128 Kib */ | |
1973 | if (!strcmp(value, "auto")) { | |
28f3b1cd | 1974 | lxc_conf->console.buffer_size = 1 << 17; |
a04220de CB |
1975 | return 0; |
1976 | } | |
1977 | ||
1978 | ret = parse_byte_size_string(value, &size); | |
1979 | if (ret < 0) | |
1980 | return -1; | |
1981 | ||
1982 | if (size < 0) | |
1983 | return -EINVAL; | |
1984 | ||
1985 | /* must be at least a page size */ | |
1986 | pgsz = lxc_getpagesize(); | |
1987 | if ((uint64_t)size < pgsz) { | |
1988 | NOTICE("Requested ringbuffer size for the console is %" PRId64 | |
1989 | " but must be at least %" PRId64 | |
1990 | " bytes. Setting ringbuffer size to %" PRId64 " bytes", | |
1991 | size, pgsz, pgsz); | |
1992 | size = pgsz; | |
1993 | } | |
1994 | ||
28f3b1cd CB |
1995 | buffer_size = lxc_find_next_power2((uint64_t)size); |
1996 | if (buffer_size == 0) | |
a04220de CB |
1997 | return -EINVAL; |
1998 | ||
28f3b1cd | 1999 | if (buffer_size != size) |
a04220de | 2000 | NOTICE("Passed size was not a power of 2. Rounding log size to " |
28f3b1cd | 2001 | "next power of two: %" PRIu64 " bytes", buffer_size); |
a04220de | 2002 | |
28f3b1cd | 2003 | lxc_conf->console.buffer_size = buffer_size; |
47903908 | 2004 | |
a04220de CB |
2005 | return 0; |
2006 | } | |
2007 | ||
861813e5 CB |
2008 | static int set_config_console_size(const char *key, const char *value, |
2009 | struct lxc_conf *lxc_conf, void *data) | |
2010 | { | |
2011 | int ret; | |
2012 | int64_t size; | |
2013 | uint64_t log_size, pgsz; | |
2014 | ||
2015 | if (lxc_config_value_empty(value)) { | |
2016 | lxc_conf->console.log_size = 0; | |
2017 | return 0; | |
2018 | } | |
2019 | ||
2020 | /* If the user specified "auto" the default log size is 2^17 = 128 Kib */ | |
2021 | if (!strcmp(value, "auto")) { | |
2022 | lxc_conf->console.log_size = 1 << 17; | |
2023 | return 0; | |
2024 | } | |
2025 | ||
2026 | ret = parse_byte_size_string(value, &size); | |
2027 | if (ret < 0) | |
2028 | return -1; | |
2029 | ||
2030 | if (size < 0) | |
2031 | return -EINVAL; | |
2032 | ||
2033 | /* must be at least a page size */ | |
2034 | pgsz = lxc_getpagesize(); | |
2035 | if ((uint64_t)size < pgsz) { | |
2036 | NOTICE("Requested ringbuffer size for the console is %" PRId64 | |
2037 | " but must be at least %" PRId64 | |
2038 | " bytes. Setting ringbuffer size to %" PRId64 " bytes", | |
2039 | size, pgsz, pgsz); | |
2040 | size = pgsz; | |
2041 | } | |
2042 | ||
2043 | log_size = lxc_find_next_power2((uint64_t)size); | |
2044 | if (log_size == 0) | |
2045 | return -EINVAL; | |
2046 | ||
2047 | if (log_size != size) | |
2048 | NOTICE("Passed size was not a power of 2. Rounding log size to " | |
2049 | "next power of two: %" PRIu64 " bytes", log_size); | |
2050 | ||
2051 | lxc_conf->console.log_size = log_size; | |
47903908 | 2052 | |
861813e5 CB |
2053 | return 0; |
2054 | } | |
2055 | ||
6b0d5538 | 2056 | int append_unexp_config_line(const char *line, struct lxc_conf *conf) |
f979ac15 | 2057 | { |
62a821f1 CB |
2058 | size_t linelen; |
2059 | size_t len = conf->unexpanded_len; | |
f979ac15 | 2060 | |
e6744e9b SH |
2061 | update_hwaddr(line); |
2062 | ||
62a821f1 | 2063 | linelen = strlen(line); |
6b0d5538 | 2064 | while (conf->unexpanded_alloced <= len + linelen + 2) { |
504a2217 CB |
2065 | char *tmp = realloc(conf->unexpanded_config, |
2066 | conf->unexpanded_alloced + 1024); | |
6b0d5538 SH |
2067 | if (!tmp) |
2068 | return -1; | |
504a2217 | 2069 | |
6b0d5538 SH |
2070 | if (!conf->unexpanded_config) |
2071 | *tmp = '\0'; | |
62a821f1 | 2072 | |
6b0d5538 SH |
2073 | conf->unexpanded_config = tmp; |
2074 | conf->unexpanded_alloced += 1024; | |
2075 | } | |
efed99a4 | 2076 | |
62a821f1 | 2077 | memcpy(conf->unexpanded_config + conf->unexpanded_len, line, linelen); |
6b0d5538 | 2078 | conf->unexpanded_len += linelen; |
62a821f1 CB |
2079 | if (line[linelen - 1] != '\n') |
2080 | conf->unexpanded_config[conf->unexpanded_len++] = '\n'; | |
2081 | conf->unexpanded_config[conf->unexpanded_len] = '\0'; | |
25a908b8 | 2082 | |
f979ac15 SH |
2083 | return 0; |
2084 | } | |
2085 | ||
e1daebd9 SH |
2086 | static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf) |
2087 | { | |
74f96976 | 2088 | struct dirent *direntp; |
e1daebd9 | 2089 | DIR *dir; |
18740311 | 2090 | char path[PATH_MAX]; |
504a2217 CB |
2091 | int len; |
2092 | int ret = -1; | |
e1daebd9 SH |
2093 | |
2094 | dir = opendir(dirp); | |
25a908b8 | 2095 | if (!dir) |
e1daebd9 | 2096 | return -1; |
e1daebd9 | 2097 | |
74f96976 | 2098 | while ((direntp = readdir(dir))) { |
e1daebd9 | 2099 | const char *fnam; |
e1daebd9 SH |
2100 | |
2101 | fnam = direntp->d_name; | |
2102 | if (!strcmp(fnam, ".")) | |
2103 | continue; | |
2104 | ||
2105 | if (!strcmp(fnam, "..")) | |
2106 | continue; | |
2107 | ||
2108 | len = strlen(fnam); | |
504a2217 | 2109 | if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0) |
e1daebd9 | 2110 | continue; |
25a908b8 | 2111 | |
18740311 CB |
2112 | len = snprintf(path, PATH_MAX, "%s/%s", dirp, fnam); |
2113 | if (len < 0 || len >= PATH_MAX) { | |
e1daebd9 SH |
2114 | ret = -1; |
2115 | goto out; | |
2116 | } | |
2117 | ||
2118 | ret = lxc_config_read(path, lxc_conf, true); | |
2119 | if (ret < 0) | |
2120 | goto out; | |
2121 | } | |
2122 | ret = 0; | |
2123 | ||
2124 | out: | |
25a908b8 | 2125 | closedir(dir); |
e1daebd9 SH |
2126 | |
2127 | return ret; | |
2128 | } | |
2129 | ||
973082f5 | 2130 | static int set_config_includefiles(const char *key, const char *value, |
c7e27aaf | 2131 | struct lxc_conf *lxc_conf, void *data) |
09ad6246 | 2132 | { |
663e9916 | 2133 | if (lxc_config_value_empty(value)) { |
26471403 | 2134 | clr_config_includefiles(key, lxc_conf, NULL); |
616422f1 | 2135 | return 0; |
355c5701 | 2136 | } |
616422f1 | 2137 | |
e1daebd9 SH |
2138 | if (is_dir(value)) |
2139 | return do_includedir(value, lxc_conf); | |
2140 | ||
6b0d5538 | 2141 | return lxc_config_read(value, lxc_conf, true); |
09ad6246 SH |
2142 | } |
2143 | ||
7a96a068 CB |
2144 | static int set_config_rootfs_path(const char *key, const char *value, |
2145 | struct lxc_conf *lxc_conf, void *data) | |
c2cc9f0a | 2146 | { |
693dbdb9 CB |
2147 | int ret; |
2148 | char *dup, *tmp; | |
2149 | const char *container_path; | |
2150 | ||
2151 | if (lxc_config_value_empty(value)) { | |
2152 | free(lxc_conf->rootfs.path); | |
2153 | lxc_conf->rootfs.path = NULL; | |
2154 | return 0; | |
2155 | } | |
2156 | ||
2157 | dup = strdup(value); | |
2158 | if (!dup) | |
2159 | return -1; | |
2160 | ||
2161 | /* Split <storage type>:<container path> into <storage type> and | |
2162 | * <container path>. Set "rootfs.bdev_type" to <storage type> and | |
2163 | * "rootfs.path" to <container path>. | |
2164 | */ | |
2165 | tmp = strchr(dup, ':'); | |
2166 | if (tmp) { | |
2167 | *tmp = '\0'; | |
47903908 | 2168 | |
693dbdb9 CB |
2169 | ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup); |
2170 | if (ret < 0) { | |
2171 | free(dup); | |
2172 | return -1; | |
2173 | } | |
47903908 | 2174 | |
693dbdb9 CB |
2175 | tmp++; |
2176 | container_path = tmp; | |
2177 | } else { | |
2178 | container_path = value; | |
2179 | } | |
2180 | ||
2181 | ret = set_config_path_item(&lxc_conf->rootfs.path, container_path); | |
2182 | free(dup); | |
47903908 | 2183 | |
693dbdb9 | 2184 | return ret; |
c2cc9f0a | 2185 | } |
2186 | ||
6e54330c CB |
2187 | static int set_config_rootfs_managed(const char *key, const char *value, |
2188 | struct lxc_conf *lxc_conf, void *data) | |
2189 | { | |
2190 | unsigned int val = 0; | |
2191 | ||
2192 | if (lxc_config_value_empty(value)) { | |
2193 | lxc_conf->rootfs.managed = true; | |
2194 | return 0; | |
2195 | } | |
2196 | ||
2197 | if (lxc_safe_uint(value, &val) < 0) | |
2198 | return -EINVAL; | |
2199 | ||
2200 | switch (val) { | |
2201 | case 0: | |
2202 | lxc_conf->rootfs.managed = false; | |
2203 | return 0; | |
2204 | case 1: | |
2205 | lxc_conf->rootfs.managed = true; | |
2206 | return 0; | |
2207 | } | |
2208 | ||
2209 | return -EINVAL; | |
2210 | } | |
2211 | ||
713046e3 | 2212 | static int set_config_rootfs_mount(const char *key, const char *value, |
c7e27aaf | 2213 | struct lxc_conf *lxc_conf, void *data) |
23b7ea69 | 2214 | { |
713046e3 | 2215 | return set_config_path_item(&lxc_conf->rootfs.mount, value); |
23b7ea69 DL |
2216 | } |
2217 | ||
713046e3 | 2218 | static int set_config_rootfs_options(const char *key, const char *value, |
c7e27aaf | 2219 | struct lxc_conf *lxc_conf, void *data) |
a17b1e65 | 2220 | { |
3437f95c CB |
2221 | int ret; |
2222 | unsigned long mflags = 0, pflags = 0; | |
2223 | char *mdata = NULL, *opts = NULL; | |
2224 | struct lxc_rootfs *rootfs = &lxc_conf->rootfs; | |
2225 | ||
2226 | ret = parse_mntopts(value, &mflags, &mdata); | |
2227 | if (ret < 0) | |
2228 | return -EINVAL; | |
2229 | ||
2230 | ret = parse_propagationopts(value, &pflags); | |
2231 | if (ret < 0) { | |
2232 | free(mdata); | |
2233 | return -EINVAL; | |
2234 | } | |
2235 | ||
2236 | ret = set_config_string_item(&opts, value); | |
2237 | if (ret < 0) { | |
2238 | free(mdata); | |
2239 | return -ENOMEM; | |
2240 | } | |
2241 | ||
2242 | rootfs->mountflags = mflags | pflags; | |
2243 | rootfs->options = opts; | |
2244 | rootfs->data = mdata; | |
2245 | ||
2246 | return 0; | |
a17b1e65 SG |
2247 | } |
2248 | ||
b67771bc | 2249 | static int set_config_uts_name(const char *key, const char *value, |
c7e27aaf | 2250 | struct lxc_conf *lxc_conf, void *data) |
c2cc9f0a | 2251 | { |
2252 | struct utsname *utsname; | |
2253 | ||
663e9916 | 2254 | if (lxc_config_value_empty(value)) { |
b67771bc | 2255 | clr_config_uts_name(key, lxc_conf, NULL); |
1939e73d | 2256 | return 0; |
00cd8039 | 2257 | } |
1939e73d | 2258 | |
c2cc9f0a | 2259 | utsname = malloc(sizeof(*utsname)); |
25a908b8 | 2260 | if (!utsname) |
c2cc9f0a | 2261 | return -1; |
c2cc9f0a | 2262 | |
2263 | if (strlen(value) >= sizeof(utsname->nodename)) { | |
b6f24d54 | 2264 | free(utsname); |
c2cc9f0a | 2265 | return -1; |
2266 | } | |
2267 | ||
43f984ea | 2268 | (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename)); |
f10fad2f | 2269 | free(lxc_conf->utsname); |
c2cc9f0a | 2270 | lxc_conf->utsname = utsname; |
2271 | ||
2272 | return 0; | |
2273 | } | |
2274 | ||
1d8d3676 CB |
2275 | static int set_config_namespace_clone(const char *key, const char *value, |
2276 | struct lxc_conf *lxc_conf, void *data) | |
2277 | { | |
62dd965e | 2278 | char *ns, *token; |
1d8d3676 | 2279 | int cloneflag = 0; |
1d8d3676 CB |
2280 | |
2281 | if (lxc_config_value_empty(value)) | |
2282 | return clr_config_namespace_clone(key, lxc_conf, data); | |
2283 | ||
8bc8c715 | 2284 | if (lxc_conf->ns_keep != 0) { |
6d1400b5 | 2285 | errno = EINVAL; |
2286 | SYSERROR("Cannot set both \"lxc.namespace.clone\" and " | |
2287 | "\"lxc.namespace.keep\""); | |
8bc8c715 CB |
2288 | return -EINVAL; |
2289 | } | |
2290 | ||
1d8d3676 CB |
2291 | ns = strdup(value); |
2292 | if (!ns) | |
2293 | return -1; | |
1d8d3676 | 2294 | |
62dd965e | 2295 | lxc_iterate_parts(token, ns, " \t") { |
1d8d3676 CB |
2296 | token += lxc_char_left_gc(token, strlen(token)); |
2297 | token[lxc_char_right_gc(token, strlen(token))] = '\0'; | |
2298 | cloneflag = lxc_namespace_2_cloneflag(token); | |
2299 | if (cloneflag < 0) { | |
2300 | free(ns); | |
2301 | return -EINVAL; | |
2302 | } | |
2303 | lxc_conf->ns_clone |= cloneflag; | |
2304 | } | |
2305 | free(ns); | |
2306 | ||
2307 | return 0; | |
2308 | } | |
2309 | ||
abeb5bba CB |
2310 | static int set_config_namespace_keep(const char *key, const char *value, |
2311 | struct lxc_conf *lxc_conf, void *data) | |
2312 | { | |
62dd965e | 2313 | char *ns, *token; |
abeb5bba | 2314 | int cloneflag = 0; |
abeb5bba CB |
2315 | |
2316 | if (lxc_config_value_empty(value)) | |
2317 | return clr_config_namespace_keep(key, lxc_conf, data); | |
2318 | ||
8bc8c715 | 2319 | if (lxc_conf->ns_clone != 0) { |
6d1400b5 | 2320 | errno = EINVAL; |
2321 | SYSERROR("Cannot set both \"lxc.namespace.clone\" and " | |
2322 | "\"lxc.namespace.keep\""); | |
8bc8c715 CB |
2323 | return -EINVAL; |
2324 | } | |
2325 | ||
abeb5bba CB |
2326 | ns = strdup(value); |
2327 | if (!ns) | |
2328 | return -1; | |
abeb5bba | 2329 | |
62dd965e | 2330 | lxc_iterate_parts(token, ns, " \t") { |
abeb5bba CB |
2331 | token += lxc_char_left_gc(token, strlen(token)); |
2332 | token[lxc_char_right_gc(token, strlen(token))] = '\0'; | |
2333 | cloneflag = lxc_namespace_2_cloneflag(token); | |
2334 | if (cloneflag < 0) { | |
2335 | free(ns); | |
2336 | return -EINVAL; | |
2337 | } | |
2338 | lxc_conf->ns_keep |= cloneflag; | |
2339 | } | |
2340 | free(ns); | |
2341 | ||
2342 | return 0; | |
2343 | } | |
2344 | ||
b074bbf1 CB |
2345 | static int set_config_namespace_share(const char *key, const char *value, |
2346 | struct lxc_conf *lxc_conf, void *data) | |
28d9e29e CB |
2347 | { |
2348 | int ns_idx; | |
2349 | const char *namespace; | |
2350 | ||
2351 | if (lxc_config_value_empty(value)) | |
b074bbf1 | 2352 | return clr_config_namespace_share(key, lxc_conf, data); |
28d9e29e | 2353 | |
6333c915 | 2354 | namespace = key + STRLITERALLEN("lxc.namespace.share."); |
28d9e29e CB |
2355 | ns_idx = lxc_namespace_2_ns_idx(namespace); |
2356 | if (ns_idx < 0) | |
2357 | return ns_idx; | |
2358 | ||
b074bbf1 | 2359 | return set_config_string_item(&lxc_conf->ns_share[ns_idx], value); |
28d9e29e CB |
2360 | } |
2361 | ||
6b0d5538 SH |
2362 | struct parse_line_conf { |
2363 | struct lxc_conf *conf; | |
2364 | bool from_include; | |
2365 | }; | |
4184c3e1 | 2366 | |
7a7ff0c6 | 2367 | static int parse_line(char *buffer, void *data) |
c2cc9f0a | 2368 | { |
504a2217 | 2369 | char *dot, *key, *line, *linep, *value; |
3b13691d CB |
2370 | bool empty_line; |
2371 | struct lxc_config_t *config; | |
504a2217 | 2372 | int ret = 0; |
3b13691d CB |
2373 | char *dup = buffer; |
2374 | struct parse_line_conf *plc = data; | |
c2cc9f0a | 2375 | |
3b13691d CB |
2376 | /* If there are newlines in the config file we should keep them. */ |
2377 | empty_line = lxc_is_line_empty(dup); | |
2378 | if (empty_line) | |
2379 | dup = "\n"; | |
c2cc9f0a | 2380 | |
25a908b8 CB |
2381 | /* We have to dup the buffer otherwise, at the re-exec for reboot we |
2382 | * modified the original string on the stack by replacing '=' by '\0' | |
2383 | * below. | |
91480a0f | 2384 | */ |
3b13691d | 2385 | linep = line = strdup(dup); |
25a908b8 | 2386 | if (!line) |
81192358 | 2387 | return -1; |
91480a0f | 2388 | |
3b13691d CB |
2389 | if (!plc->from_include) { |
2390 | ret = append_unexp_config_line(line, plc->conf); | |
2391 | if (ret < 0) | |
2392 | goto on_error; | |
2393 | } | |
2394 | ||
2395 | if (empty_line) | |
327500d4 | 2396 | goto on_error; |
6b0d5538 | 2397 | |
b2718c72 | 2398 | line += lxc_char_left_gc(line, strlen(line)); |
476d4cf1 | 2399 | |
4184c3e1 SH |
2400 | /* ignore comments */ |
2401 | if (line[0] == '#') | |
3b13691d | 2402 | goto on_error; |
476d4cf1 | 2403 | |
6b0d5538 SH |
2404 | /* martian option - don't add it to the config itself */ |
2405 | if (strncmp(line, "lxc.", 4)) | |
3b13691d | 2406 | goto on_error; |
4184c3e1 | 2407 | |
476d4cf1 | 2408 | ret = -1; |
c2cc9f0a | 2409 | |
3b13691d | 2410 | dot = strchr(line, '='); |
c2cc9f0a | 2411 | if (!dot) { |
3b13691d CB |
2412 | ERROR("Invalid configuration line: %s", line); |
2413 | goto on_error; | |
c2cc9f0a | 2414 | } |
a871ff6b | 2415 | |
c2cc9f0a | 2416 | *dot = '\0'; |
2417 | value = dot + 1; | |
2418 | ||
b2718c72 | 2419 | key = line; |
2420 | key[lxc_char_right_gc(key, strlen(key))] = '\0'; | |
c2cc9f0a | 2421 | |
b2718c72 | 2422 | value += lxc_char_left_gc(value, strlen(value)); |
2423 | value[lxc_char_right_gc(value, strlen(value))] = '\0'; | |
c2cc9f0a | 2424 | |
bd878dee | 2425 | if (*value == '\'' || *value == '\"') { |
25a908b8 CB |
2426 | size_t len; |
2427 | ||
2428 | len = strlen(value); | |
504a2217 CB |
2429 | if (len > 1 && value[len - 1] == *value) { |
2430 | value[len - 1] = '\0'; | |
bd878dee SB |
2431 | value++; |
2432 | } | |
2433 | } | |
2434 | ||
300df83e | 2435 | config = lxc_get_config(key); |
c2cc9f0a | 2436 | if (!config) { |
25a908b8 | 2437 | ERROR("Unknown configuration key \"%s\"", key); |
3b13691d | 2438 | goto on_error; |
c2cc9f0a | 2439 | } |
2440 | ||
bbc079cf | 2441 | ret = config->set(key, value, plc->conf, NULL); |
91480a0f | 2442 | |
3b13691d | 2443 | on_error: |
81192358 | 2444 | free(linep); |
47903908 | 2445 | |
91480a0f | 2446 | return ret; |
c2cc9f0a | 2447 | } |
2448 | ||
d899f11b | 2449 | static struct new_config_item *parse_new_conf_line(char *buffer) |
2450 | { | |
2451 | char *dot, *key, *line, *linep, *value; | |
2452 | int ret = 0; | |
2453 | char *dup = buffer; | |
2454 | struct new_config_item *new = NULL; | |
2455 | ||
2456 | linep = line = strdup(dup); | |
2457 | if (!line) | |
2458 | return NULL; | |
2459 | ||
2460 | line += lxc_char_left_gc(line, strlen(line)); | |
2461 | ||
2462 | /* martian option - don't add it to the config itself */ | |
85d49873 | 2463 | if (strncmp(line, "lxc.", 4)) |
d899f11b | 2464 | goto on_error; |
2465 | ||
2466 | ret = -1; | |
2467 | dot = strchr(line, '='); | |
2468 | if (!dot) { | |
2469 | ERROR("Invalid configuration item: %s", line); | |
2470 | goto on_error; | |
2471 | } | |
2472 | ||
2473 | *dot = '\0'; | |
2474 | value = dot + 1; | |
2475 | ||
2476 | key = line; | |
2477 | key[lxc_char_right_gc(key, strlen(key))] = '\0'; | |
2478 | ||
2479 | value += lxc_char_left_gc(value, strlen(value)); | |
2480 | value[lxc_char_right_gc(value, strlen(value))] = '\0'; | |
2481 | ||
2482 | if (*value == '\'' || *value == '\"') { | |
2483 | size_t len; | |
2484 | ||
2485 | len = strlen(value); | |
2486 | if (len > 1 && value[len - 1] == *value) { | |
2487 | value[len - 1] = '\0'; | |
2488 | value++; | |
2489 | } | |
2490 | } | |
2491 | ||
2492 | ret = -1; | |
2493 | new = malloc(sizeof(struct new_config_item)); | |
2494 | if (!new) | |
2495 | goto on_error; | |
2496 | ||
2497 | new->key = strdup(key); | |
2498 | new->val = strdup(value); | |
2499 | if (!new->val || !new->key) | |
2500 | goto on_error; | |
2501 | ||
2502 | ret = 0; | |
2503 | ||
2504 | on_error: | |
2505 | free(linep); | |
2506 | ||
2507 | if (ret < 0 && new) { | |
2508 | free(new->key); | |
2509 | free(new->val); | |
2510 | free(new); | |
2511 | new = NULL; | |
2512 | } | |
2513 | ||
2514 | return new; | |
2515 | } | |
2516 | ||
6b0d5538 | 2517 | int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include) |
c2cc9f0a | 2518 | { |
6b0d5538 SH |
2519 | struct parse_line_conf c; |
2520 | ||
2521 | c.conf = conf; | |
2522 | c.from_include = from_include; | |
f979ac15 | 2523 | |
25a908b8 | 2524 | /* Catch only the top level config file name in the structure. */ |
504a2217 | 2525 | if (!conf->rcfile) |
76d0127f | 2526 | conf->rcfile = strdup(file); |
f979ac15 | 2527 | |
9994d140 | 2528 | return lxc_file_for_each_line_mmap(file, parse_line, &c); |
c2cc9f0a | 2529 | } |
62e46035 | 2530 | |
504a2217 | 2531 | int lxc_config_define_add(struct lxc_list *defines, char *arg) |
62e46035 CLG |
2532 | { |
2533 | struct lxc_list *dent; | |
2534 | ||
2535 | dent = malloc(sizeof(struct lxc_list)); | |
2536 | if (!dent) | |
2537 | return -1; | |
2538 | ||
d899f11b | 2539 | dent->elem = parse_new_conf_line(arg); |
2540 | if (!dent->elem) { | |
2541 | free(dent); | |
2542 | return -1; | |
2543 | } | |
2544 | ||
62e46035 | 2545 | lxc_list_add_tail(defines, dent); |
47903908 | 2546 | |
62e46035 CLG |
2547 | return 0; |
2548 | } | |
2549 | ||
eb0c9382 | 2550 | bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c) |
62e46035 | 2551 | { |
eb0c9382 | 2552 | struct lxc_list *it; |
2553 | bool bret = true; | |
62e46035 CLG |
2554 | |
2555 | lxc_list_for_each(it, defines) { | |
eb0c9382 | 2556 | struct new_config_item *new_item = it->elem; |
2557 | bret = c->set_config_item(c, new_item->key, new_item->val); | |
2558 | if (!bret) | |
62e46035 CLG |
2559 | break; |
2560 | } | |
2561 | ||
eb0c9382 | 2562 | lxc_config_define_free(defines); |
47903908 | 2563 | |
eb0c9382 | 2564 | return bret; |
2565 | } | |
2566 | ||
2567 | void lxc_config_define_free(struct lxc_list *defines) | |
2568 | { | |
2569 | struct lxc_list *it, *next; | |
2570 | ||
9ebb03ad | 2571 | lxc_list_for_each_safe(it, defines, next) { |
eb0c9382 | 2572 | struct new_config_item *new_item = it->elem; |
2573 | free(new_item->key); | |
2574 | free(new_item->val); | |
62e46035 CLG |
2575 | lxc_list_del(it); |
2576 | free(it); | |
2577 | } | |
62e46035 | 2578 | } |
525f0002 CS |
2579 | |
2580 | signed long lxc_config_parse_arch(const char *arch) | |
2581 | { | |
504a2217 CB |
2582 | #if HAVE_SYS_PERSONALITY_H |
2583 | size_t i; | |
525f0002 CS |
2584 | struct per_name { |
2585 | char *name; | |
2586 | unsigned long per; | |
bb8d8207 | 2587 | } pername[] = { |
8f3e3c13 CB |
2588 | { "arm", PER_LINUX32 }, |
2589 | { "armel", PER_LINUX32 }, | |
2590 | { "armhf", PER_LINUX32 }, | |
2591 | { "armv7l", PER_LINUX32 }, | |
2592 | { "athlon", PER_LINUX32 }, | |
504a2217 CB |
2593 | { "i386", PER_LINUX32 }, |
2594 | { "i486", PER_LINUX32 }, | |
2595 | { "i586", PER_LINUX32 }, | |
2596 | { "i686", PER_LINUX32 }, | |
8f3e3c13 | 2597 | { "linux32", PER_LINUX32 }, |
504a2217 CB |
2598 | { "mips", PER_LINUX32 }, |
2599 | { "mipsel", PER_LINUX32 }, | |
2600 | { "ppc", PER_LINUX32 }, | |
504a2217 | 2601 | { "powerpc", PER_LINUX32 }, |
8f3e3c13 | 2602 | { "x86", PER_LINUX32 }, |
504a2217 | 2603 | { "amd64", PER_LINUX }, |
8f3e3c13 CB |
2604 | { "arm64", PER_LINUX }, |
2605 | { "linux64", PER_LINUX }, | |
504a2217 CB |
2606 | { "mips64", PER_LINUX }, |
2607 | { "mips64el", PER_LINUX }, | |
2608 | { "ppc64", PER_LINUX }, | |
504a2217 | 2609 | { "ppc64el", PER_LINUX }, |
8f3e3c13 | 2610 | { "ppc64le", PER_LINUX }, |
504a2217 CB |
2611 | { "powerpc64", PER_LINUX }, |
2612 | { "s390x", PER_LINUX }, | |
8f3e3c13 | 2613 | { "x86_64", PER_LINUX }, |
525f0002 CS |
2614 | }; |
2615 | size_t len = sizeof(pername) / sizeof(pername[0]); | |
2616 | ||
8f3e3c13 | 2617 | for (i = 0; i < len; i++) |
525f0002 | 2618 | if (!strcmp(pername[i].name, arch)) |
504a2217 | 2619 | return pername[i].per; |
504a2217 | 2620 | #endif |
525f0002 CS |
2621 | |
2622 | return -1; | |
2623 | } | |
72d0e1cb | 2624 | |
eb0c9382 | 2625 | int lxc_fill_elevated_privileges(char *flaglist, int *flags) |
2626 | { | |
62dd965e | 2627 | char *token; |
eb0c9382 | 2628 | int i, aflag; |
2629 | struct { | |
2630 | const char *token; | |
2631 | int flag; | |
2632 | } all_privs[] = { | |
2633 | { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP }, | |
2634 | { "CAP", LXC_ATTACH_DROP_CAPABILITIES }, | |
2635 | { "LSM", LXC_ATTACH_LSM_EXEC }, | |
2636 | { NULL, 0 } | |
2637 | }; | |
2638 | ||
2639 | if (!flaglist) { | |
2640 | /* For the sake of backward compatibility, drop all privileges | |
2641 | * if none is specified. | |
2642 | */ | |
2643 | for (i = 0; all_privs[i].token; i++) | |
2644 | *flags |= all_privs[i].flag; | |
2645 | ||
2646 | return 0; | |
2647 | } | |
2648 | ||
62dd965e | 2649 | lxc_iterate_parts(token, flaglist, "|") { |
eb0c9382 | 2650 | aflag = -1; |
2651 | ||
2652 | for (i = 0; all_privs[i].token; i++) | |
2653 | if (!strcmp(all_privs[i].token, token)) | |
2654 | aflag = all_privs[i].flag; | |
2655 | ||
2656 | if (aflag < 0) | |
2657 | return -1; | |
2658 | ||
2659 | *flags |= aflag; | |
eb0c9382 | 2660 | } |
2661 | ||
2662 | return 0; | |
2663 | } | |
2664 | ||
504a2217 | 2665 | /* Write out a configuration file. */ |
0e1a60b0 | 2666 | int write_config(int fd, const struct lxc_conf *conf) |
72d0e1cb | 2667 | { |
6b0d5538 | 2668 | int ret; |
0e1a60b0 | 2669 | size_t len = conf->unexpanded_len; |
72d0e1cb | 2670 | |
0e1a60b0 CB |
2671 | if (len == 0) |
2672 | return 0; | |
504a2217 | 2673 | |
0e1a60b0 CB |
2674 | ret = lxc_write_nointr(fd, conf->unexpanded_config, len); |
2675 | if (ret < 0) { | |
25a908b8 | 2676 | SYSERROR("Failed to write configuration file"); |
0e1a60b0 CB |
2677 | return -1; |
2678 | } | |
2679 | ||
2680 | return 0; | |
6b0d5538 | 2681 | } |
f979ac15 | 2682 | |
504a2217 CB |
2683 | bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key, |
2684 | const char *v) | |
6b0d5538 SH |
2685 | { |
2686 | int ret; | |
151d2da2 CB |
2687 | size_t len; |
2688 | char *tmp; | |
4184c3e1 | 2689 | |
151d2da2 CB |
2690 | len = strlen(key) + strlen(v) + 4; |
2691 | tmp = alloca(len); | |
2692 | ||
663e9916 | 2693 | if (lxc_config_value_empty(v)) |
151d2da2 CB |
2694 | ret = snprintf(tmp, len, "%s =", key); |
2695 | else | |
2696 | ret = snprintf(tmp, len, "%s = %s", key, v); | |
6b0d5538 SH |
2697 | if (ret < 0 || ret >= len) |
2698 | return false; | |
2699 | ||
2700 | /* Save the line verbatim into unexpanded_conf */ | |
2701 | if (append_unexp_config_line(tmp, conf)) | |
2702 | return false; | |
2703 | ||
2704 | return true; | |
2705 | } | |
2706 | ||
504a2217 CB |
2707 | void clear_unexp_config_line(struct lxc_conf *conf, const char *key, |
2708 | bool rm_subkeys) | |
6b0d5538 | 2709 | { |
504a2217 CB |
2710 | char *lend; |
2711 | char *lstart = conf->unexpanded_config; | |
6b0d5538 SH |
2712 | |
2713 | if (!conf->unexpanded_config) | |
2714 | return; | |
504a2217 | 2715 | |
6b0d5538 SH |
2716 | while (*lstart) { |
2717 | lend = strchr(lstart, '\n'); | |
2718 | char v; | |
47903908 | 2719 | |
6b0d5538 SH |
2720 | if (!lend) |
2721 | lend = lstart + strlen(lstart); | |
2722 | else | |
2723 | lend++; | |
47903908 | 2724 | |
6b0d5538 SH |
2725 | if (strncmp(lstart, key, strlen(key)) != 0) { |
2726 | lstart = lend; | |
2727 | continue; | |
5f62730e | 2728 | } |
47903908 | 2729 | |
6b0d5538 SH |
2730 | if (!rm_subkeys) { |
2731 | v = lstart[strlen(key)]; | |
2732 | if (!isspace(v) && v != '=') { | |
2733 | lstart = lend; | |
2734 | continue; | |
2735 | } | |
5f62730e | 2736 | } |
47903908 | 2737 | |
6b0d5538 | 2738 | conf->unexpanded_len -= (lend - lstart); |
47903908 | 2739 | |
6b0d5538 SH |
2740 | if (*lend == '\0') { |
2741 | *lstart = '\0'; | |
2742 | return; | |
fd986e08 | 2743 | } |
47903908 | 2744 | |
504a2217 | 2745 | memmove(lstart, lend, strlen(lend) + 1); |
fd986e08 | 2746 | } |
6b0d5538 SH |
2747 | } |
2748 | ||
329b3625 CB |
2749 | bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath, |
2750 | const char *newpath, const char *oldname, | |
2751 | const char *newname, const char *ovldir) | |
2752 | { | |
329b3625 | 2753 | int ret; |
504a2217 CB |
2754 | char *lend, *newdir, *olddir, *p, *q; |
2755 | size_t newdirlen, olddirlen; | |
329b3625 | 2756 | char *lstart = conf->unexpanded_config; |
504a2217 | 2757 | const char *key = "lxc.mount.entry"; |
329b3625 | 2758 | |
504a2217 CB |
2759 | olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2; |
2760 | olddir = alloca(olddirlen + 1); | |
2761 | ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, | |
2762 | oldname); | |
25a908b8 | 2763 | if (ret < 0 || ret >= olddirlen + 1) |
329b3625 | 2764 | return false; |
504a2217 CB |
2765 | |
2766 | newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2; | |
2767 | newdir = alloca(newdirlen + 1); | |
2768 | ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, | |
2769 | newname); | |
25a908b8 | 2770 | if (ret < 0 || ret >= newdirlen + 1) |
329b3625 | 2771 | return false; |
504a2217 | 2772 | |
329b3625 CB |
2773 | if (!conf->unexpanded_config) |
2774 | return true; | |
504a2217 | 2775 | |
329b3625 CB |
2776 | while (*lstart) { |
2777 | lend = strchr(lstart, '\n'); | |
2778 | if (!lend) | |
2779 | lend = lstart + strlen(lstart); | |
2780 | else | |
2781 | lend++; | |
504a2217 | 2782 | |
329b3625 | 2783 | if (strncmp(lstart, key, strlen(key)) != 0) |
504a2217 CB |
2784 | goto next; |
2785 | ||
329b3625 CB |
2786 | p = strchr(lstart + strlen(key), '='); |
2787 | if (!p) | |
504a2217 | 2788 | goto next; |
329b3625 | 2789 | p++; |
504a2217 | 2790 | |
329b3625 CB |
2791 | while (isblank(*p)) |
2792 | p++; | |
504a2217 | 2793 | |
329b3625 | 2794 | if (p >= lend) |
504a2217 CB |
2795 | goto next; |
2796 | ||
12e6ab5d CB |
2797 | /* Whenever a lxc.mount.entry entry is found in a line we check |
2798 | * if the substring "overlay" is present before doing any | |
2799 | * further work. We check for "overlay" because substrings need | |
2800 | * to have at least one space before them in a valid overlay | |
504a2217 CB |
2801 | * lxc.mount.entry (/A B overlay). When the space before is |
2802 | * missing it is very likely that these substrings are part of a | |
2803 | * path or something else. (Checking q >= lend ensures that we | |
2804 | * only count matches in the current line.) */ | |
12e6ab5d CB |
2805 | q = strstr(p, " overlay"); |
2806 | if (!q || q >= lend) | |
504a2217 CB |
2807 | goto next; |
2808 | ||
329b3625 | 2809 | if (!(q = strstr(p, olddir)) || (q >= lend)) |
504a2217 | 2810 | goto next; |
329b3625 CB |
2811 | |
2812 | /* replace the olddir with newdir */ | |
2813 | if (olddirlen >= newdirlen) { | |
2814 | size_t diff = olddirlen - newdirlen; | |
2815 | memcpy(q, newdir, newdirlen); | |
47903908 | 2816 | |
329b3625 CB |
2817 | if (olddirlen != newdirlen) { |
2818 | memmove(q + newdirlen, q + newdirlen + diff, | |
47903908 | 2819 | strlen(q) - newdirlen - diff + 1); |
329b3625 CB |
2820 | lend -= diff; |
2821 | conf->unexpanded_len -= diff; | |
2822 | } | |
2823 | } else { | |
2824 | char *new; | |
2825 | size_t diff = newdirlen - olddirlen; | |
2826 | size_t oldlen = conf->unexpanded_len; | |
2827 | size_t newlen = oldlen + diff; | |
2828 | size_t poffset = q - conf->unexpanded_config; | |
504a2217 | 2829 | |
329b3625 | 2830 | new = realloc(conf->unexpanded_config, newlen + 1); |
25a908b8 | 2831 | if (!new) |
329b3625 | 2832 | return false; |
25a908b8 | 2833 | |
329b3625 CB |
2834 | conf->unexpanded_len = newlen; |
2835 | conf->unexpanded_alloced = newlen + 1; | |
2836 | new[newlen - 1] = '\0'; | |
2837 | lend = new + (lend - conf->unexpanded_config); | |
47903908 | 2838 | |
25a908b8 | 2839 | /* Move over the remainder to make room for the newdir. |
504a2217 | 2840 | */ |
329b3625 | 2841 | memmove(new + poffset + newdirlen, |
47903908 | 2842 | new + poffset + olddirlen, |
2843 | oldlen - poffset - olddirlen + 1); | |
329b3625 | 2844 | conf->unexpanded_config = new; |
47903908 | 2845 | |
329b3625 CB |
2846 | memcpy(new + poffset, newdir, newdirlen); |
2847 | lend += diff; | |
2848 | } | |
47903908 | 2849 | |
504a2217 CB |
2850 | next: |
2851 | lstart = lend; | |
329b3625 | 2852 | } |
504a2217 | 2853 | |
329b3625 CB |
2854 | return true; |
2855 | } | |
2856 | ||
67702c21 | 2857 | bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath, |
d546aa0e CB |
2858 | const char *newpath, const char *oldname, |
2859 | const char *newname) | |
6b0d5538 | 2860 | { |
67702c21 | 2861 | int ret; |
504a2217 CB |
2862 | char *lend, *newdir, *olddir, *p; |
2863 | char *lstart = conf->unexpanded_config; | |
2864 | size_t newdirlen, olddirlen; | |
2865 | const char *key = "lxc.hook"; | |
67702c21 | 2866 | |
504a2217 CB |
2867 | olddirlen = strlen(oldpath) + strlen(oldname) + 1; |
2868 | olddir = alloca(olddirlen + 1); | |
d546aa0e | 2869 | ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname); |
25a908b8 | 2870 | if (ret < 0 || ret >= olddirlen + 1) |
67702c21 | 2871 | return false; |
504a2217 CB |
2872 | |
2873 | newdirlen = strlen(newpath) + strlen(newname) + 1; | |
2874 | newdir = alloca(newdirlen + 1); | |
d546aa0e | 2875 | ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname); |
25a908b8 | 2876 | if (ret < 0 || ret >= newdirlen + 1) |
67702c21 | 2877 | return false; |
25a908b8 | 2878 | |
67702c21 SH |
2879 | if (!conf->unexpanded_config) |
2880 | return true; | |
25a908b8 | 2881 | |
67702c21 SH |
2882 | while (*lstart) { |
2883 | lend = strchr(lstart, '\n'); | |
2884 | if (!lend) | |
2885 | lend = lstart + strlen(lstart); | |
2886 | else | |
2887 | lend++; | |
504a2217 | 2888 | |
d546aa0e | 2889 | if (strncmp(lstart, key, strlen(key)) != 0) |
504a2217 CB |
2890 | goto next; |
2891 | ||
d546aa0e CB |
2892 | p = strchr(lstart + strlen(key), '='); |
2893 | if (!p) | |
504a2217 | 2894 | goto next; |
67702c21 | 2895 | p++; |
504a2217 | 2896 | |
67702c21 SH |
2897 | while (isblank(*p)) |
2898 | p++; | |
504a2217 CB |
2899 | |
2900 | if (p >= lend) | |
2901 | goto next; | |
2902 | ||
d546aa0e | 2903 | if (strncmp(p, olddir, strlen(olddir)) != 0) |
504a2217 CB |
2904 | goto next; |
2905 | ||
67702c21 SH |
2906 | /* replace the olddir with newdir */ |
2907 | if (olddirlen >= newdirlen) { | |
2908 | size_t diff = olddirlen - newdirlen; | |
2909 | memcpy(p, newdir, newdirlen); | |
47903908 | 2910 | |
67702c21 | 2911 | if (olddirlen != newdirlen) { |
d546aa0e | 2912 | memmove(p + newdirlen, p + newdirlen + diff, |
47903908 | 2913 | strlen(p) - newdirlen - diff + 1); |
67702c21 SH |
2914 | lend -= diff; |
2915 | conf->unexpanded_len -= diff; | |
2916 | } | |
67702c21 SH |
2917 | } else { |
2918 | char *new; | |
2919 | size_t diff = newdirlen - olddirlen; | |
2920 | size_t oldlen = conf->unexpanded_len; | |
2921 | size_t newlen = oldlen + diff; | |
2922 | size_t poffset = p - conf->unexpanded_config; | |
504a2217 | 2923 | |
d546aa0e | 2924 | new = realloc(conf->unexpanded_config, newlen + 1); |
25a908b8 | 2925 | if (!new) |
6b0d5538 | 2926 | return false; |
25a908b8 | 2927 | |
67702c21 | 2928 | conf->unexpanded_len = newlen; |
d546aa0e CB |
2929 | conf->unexpanded_alloced = newlen + 1; |
2930 | new[newlen - 1] = '\0'; | |
67702c21 | 2931 | lend = new + (lend - conf->unexpanded_config); |
47903908 | 2932 | |
25a908b8 | 2933 | /* Move over the remainder to make room for the newdir. |
504a2217 | 2934 | */ |
d546aa0e | 2935 | memmove(new + poffset + newdirlen, |
47903908 | 2936 | new + poffset + olddirlen, |
2937 | oldlen - poffset - olddirlen + 1); | |
67702c21 | 2938 | conf->unexpanded_config = new; |
47903908 | 2939 | |
d546aa0e CB |
2940 | memcpy(new + poffset, newdir, newdirlen); |
2941 | lend += diff; | |
fd986e08 | 2942 | } |
47903908 | 2943 | |
504a2217 CB |
2944 | next: |
2945 | lstart = lend; | |
fd986e08 | 2946 | } |
504a2217 | 2947 | |
6b0d5538 SH |
2948 | return true; |
2949 | } | |
2950 | ||
504a2217 CB |
2951 | #define DO(cmd) \ |
2952 | { \ | |
2953 | if (!(cmd)) { \ | |
2954 | ERROR("Error writing to new config"); \ | |
2955 | return false; \ | |
2956 | } \ | |
2957 | } | |
6b0d5538 | 2958 | |
25a908b8 CB |
2959 | /* This is called only from clone. We wish to update all hwaddrs in the |
2960 | * unexpanded config file. We can't/don't want to update any which come from | |
504a2217 CB |
2961 | * lxc.includes (there shouldn't be any). |
2962 | * We can't just walk the c->lxc-conf->network list because that includes netifs | |
2963 | * from the include files. So we update the ones which we find in the unexp | |
2964 | * config file, then find the original macaddr in the conf->network, and update | |
2965 | * that to the same value. | |
67702c21 SH |
2966 | */ |
2967 | bool network_new_hwaddrs(struct lxc_conf *conf) | |
6b0d5538 | 2968 | { |
504a2217 | 2969 | char *lend, *p, *p2; |
6b0d5538 | 2970 | struct lxc_list *it; |
504a2217 | 2971 | char *lstart = conf->unexpanded_config; |
6b0d5538 | 2972 | |
67702c21 SH |
2973 | if (!conf->unexpanded_config) |
2974 | return true; | |
091045f8 | 2975 | |
67702c21 SH |
2976 | while (*lstart) { |
2977 | char newhwaddr[18], oldhwaddr[17]; | |
091045f8 | 2978 | |
67702c21 SH |
2979 | lend = strchr(lstart, '\n'); |
2980 | if (!lend) | |
2981 | lend = lstart + strlen(lstart); | |
2982 | else | |
2983 | lend++; | |
091045f8 | 2984 | |
4a787c27 | 2985 | if (!lxc_config_net_hwaddr(lstart)) { |
67702c21 SH |
2986 | lstart = lend; |
2987 | continue; | |
72d0e1cb | 2988 | } |
091045f8 | 2989 | |
4a787c27 | 2990 | p = strchr(lstart, '='); |
67702c21 SH |
2991 | if (!p) { |
2992 | lstart = lend; | |
2993 | continue; | |
72d0e1cb | 2994 | } |
091045f8 | 2995 | |
67702c21 SH |
2996 | p++; |
2997 | while (isblank(*p)) | |
2998 | p++; | |
2999 | if (!*p) | |
3000 | return true; | |
091045f8 | 3001 | |
67702c21 SH |
3002 | p2 = p; |
3003 | while (*p2 && !isblank(*p2) && *p2 != '\n') | |
3004 | p2++; | |
504a2217 CB |
3005 | |
3006 | if ((p2 - p) != 17) { | |
67702c21 SH |
3007 | WARN("Bad hwaddr entry"); |
3008 | lstart = lend; | |
3009 | continue; | |
72d0e1cb | 3010 | } |
091045f8 | 3011 | |
67702c21 | 3012 | memcpy(oldhwaddr, p, 17); |
091045f8 CB |
3013 | |
3014 | if (!new_hwaddr(newhwaddr)) | |
3015 | return false; | |
3016 | ||
67702c21 | 3017 | memcpy(p, newhwaddr, 17); |
25a908b8 | 3018 | lxc_list_for_each(it, &conf->network) { |
67702c21 | 3019 | struct lxc_netdev *n = it->elem; |
25a908b8 | 3020 | |
67702c21 SH |
3021 | if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0) |
3022 | memcpy(n->hwaddr, newhwaddr, 17); | |
72d0e1cb | 3023 | } |
67702c21 SH |
3024 | |
3025 | lstart = lend; | |
72d0e1cb | 3026 | } |
091045f8 | 3027 | |
6b0d5538 | 3028 | return true; |
72d0e1cb | 3029 | } |
8796becf | 3030 | |
713046e3 | 3031 | static int set_config_ephemeral(const char *key, const char *value, |
c7e27aaf | 3032 | struct lxc_conf *lxc_conf, void *data) |
8796becf | 3033 | { |
663e9916 | 3034 | if (lxc_config_value_empty(value)) { |
3c6cf53a | 3035 | lxc_conf->ephemeral = 0; |
78304622 | 3036 | return 0; |
3c6cf53a | 3037 | } |
78304622 | 3038 | |
66ffdb1a CB |
3039 | if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0) |
3040 | return -1; | |
8796becf | 3041 | |
25a908b8 | 3042 | if (lxc_conf->ephemeral > 1) |
8796becf | 3043 | return -1; |
8796becf CB |
3044 | |
3045 | return 0; | |
3046 | } | |
3047 | ||
46cc906d | 3048 | static int set_config_log_syslog(const char *key, const char *value, |
c7e27aaf | 3049 | struct lxc_conf *lxc_conf, void *data) |
64c57ea1 | 3050 | { |
76d0127f | 3051 | int facility; |
7ca56b84 | 3052 | |
ee10a69c CB |
3053 | if (lxc_conf->syslog) { |
3054 | free(lxc_conf->syslog); | |
3055 | lxc_conf->syslog = NULL; | |
3056 | } | |
3057 | ||
663e9916 | 3058 | if (lxc_config_value_empty(value)) |
7ca56b84 CB |
3059 | return 0; |
3060 | ||
76d0127f | 3061 | facility = lxc_syslog_priority_to_int(value); |
25a908b8 | 3062 | if (facility == -EINVAL) |
76d0127f | 3063 | return -1; |
64c57ea1 | 3064 | |
76d0127f | 3065 | lxc_log_syslog(facility); |
47903908 | 3066 | |
713046e3 | 3067 | return set_config_string_item(&lxc_conf->syslog, value); |
64c57ea1 | 3068 | } |
5a46f283 | 3069 | |
713046e3 | 3070 | static int set_config_no_new_privs(const char *key, const char *value, |
c7e27aaf | 3071 | struct lxc_conf *lxc_conf, void *data) |
5a46f283 | 3072 | { |
e8ec7c9e | 3073 | unsigned int v; |
5a46f283 | 3074 | |
663e9916 | 3075 | if (lxc_config_value_empty(value)) { |
cf3f8bf6 | 3076 | lxc_conf->no_new_privs = false; |
80926845 | 3077 | return 0; |
cf3f8bf6 | 3078 | } |
80926845 | 3079 | |
e8ec7c9e CB |
3080 | if (lxc_safe_uint(value, &v) < 0) |
3081 | return -1; | |
3082 | ||
25a908b8 | 3083 | if (v > 1) |
5a46f283 | 3084 | return -1; |
e8ec7c9e | 3085 | |
5a46f283 CB |
3086 | lxc_conf->no_new_privs = v ? true : false; |
3087 | ||
3088 | return 0; | |
3089 | } | |
7b992a3e CB |
3090 | |
3091 | /* Callbacks to get configuration items. */ | |
6bede808 | 3092 | static int get_config_personality(const char *key, char *retv, int inlen, |
cccd2219 | 3093 | struct lxc_conf *c, void *data) |
7b992a3e CB |
3094 | { |
3095 | int fulllen = 0; | |
3096 | ||
3097 | if (!retv) | |
3098 | inlen = 0; | |
3099 | else | |
3100 | memset(retv, 0, inlen); | |
3101 | ||
3102 | #if HAVE_SYS_PERSONALITY_H | |
3103 | int len = 0; | |
3104 | ||
6bede808 | 3105 | switch (c->personality) { |
7b992a3e CB |
3106 | case PER_LINUX32: |
3107 | strprint(retv, inlen, "i686"); | |
3108 | break; | |
3109 | case PER_LINUX: | |
3110 | strprint(retv, inlen, "x86_64"); | |
3111 | break; | |
3112 | default: | |
3113 | break; | |
3114 | } | |
3115 | #endif | |
3116 | ||
3117 | return fulllen; | |
3118 | } | |
bdf91ab4 | 3119 | |
232763d6 CB |
3120 | static int get_config_pty_max(const char *key, char *retv, int inlen, |
3121 | struct lxc_conf *c, void *data) | |
bdf91ab4 | 3122 | { |
e528c735 | 3123 | return lxc_get_conf_size_t(c, retv, inlen, c->pty_max); |
bdf91ab4 | 3124 | } |
5485782f | 3125 | |
fe1c5887 CB |
3126 | static int get_config_tty_max(const char *key, char *retv, int inlen, |
3127 | struct lxc_conf *c, void *data) | |
5485782f | 3128 | { |
885766f5 | 3129 | return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max); |
5485782f | 3130 | } |
8015e018 | 3131 | |
42e53c29 | 3132 | static int get_config_tty_dir(const char *key, char *retv, int inlen, |
cccd2219 | 3133 | struct lxc_conf *c, void *data) |
8015e018 | 3134 | { |
885766f5 | 3135 | return lxc_get_conf_str(retv, inlen, c->ttys.dir); |
8015e018 | 3136 | } |
de1ede69 | 3137 | |
953fe44f CB |
3138 | static int get_config_apparmor_profile(const char *key, char *retv, int inlen, |
3139 | struct lxc_conf *c, void *data) | |
104c8e6c | 3140 | { |
6bede808 | 3141 | return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile); |
104c8e6c | 3142 | } |
d60d18c6 | 3143 | |
953fe44f CB |
3144 | static int get_config_apparmor_allow_incomplete(const char *key, char *retv, |
3145 | int inlen, struct lxc_conf *c, | |
3146 | void *data) | |
d60d18c6 | 3147 | { |
6bede808 CB |
3148 | return lxc_get_conf_int(c, retv, inlen, |
3149 | c->lsm_aa_allow_incomplete); | |
d60d18c6 | 3150 | } |
4203a0b5 | 3151 | |
1800f924 WB |
3152 | static int get_config_apparmor_allow_nesting(const char *key, char *retv, |
3153 | int inlen, struct lxc_conf *c, | |
3154 | void *data) | |
3155 | { | |
3156 | return lxc_get_conf_int(c, retv, inlen, | |
3157 | c->lsm_aa_allow_nesting); | |
3158 | } | |
3159 | ||
3160 | static int get_config_apparmor_raw(const char *key, char *retv, | |
3161 | int inlen, struct lxc_conf *c, | |
3162 | void *data) | |
3163 | { | |
3164 | int len; | |
3165 | struct lxc_list *it; | |
3166 | int fulllen = 0; | |
3167 | ||
3168 | if (!retv) | |
3169 | inlen = 0; | |
3170 | else | |
3171 | memset(retv, 0, inlen); | |
3172 | ||
3173 | lxc_list_for_each(it, &c->lsm_aa_raw) { | |
3174 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
3175 | } | |
3176 | ||
3177 | return fulllen; | |
3178 | } | |
3179 | ||
953fe44f CB |
3180 | static int get_config_selinux_context(const char *key, char *retv, int inlen, |
3181 | struct lxc_conf *c, void *data) | |
4203a0b5 | 3182 | { |
6bede808 | 3183 | return lxc_get_conf_str(retv, inlen, c->lsm_se_context); |
4203a0b5 | 3184 | } |
b863bf92 | 3185 | |
25a908b8 CB |
3186 | /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then |
3187 | * just the value(s) will be printed. Since there still could be more than one, | |
3188 | * it is newline-separated. | |
3189 | * (Maybe that's ambigous, since some values, i.e. devices.list, will already | |
3190 | * have newlines?) | |
3191 | * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in | |
3192 | * 'lxc.cgroup.subsystem.key = value' format. | |
b863bf92 | 3193 | */ |
54860ed0 CB |
3194 | static int __get_config_cgroup_controller(const char *key, char *retv, |
3195 | int inlen, struct lxc_conf *c, | |
3196 | int version) | |
b863bf92 | 3197 | { |
b863bf92 | 3198 | int len; |
54860ed0 CB |
3199 | size_t namespaced_token_len; |
3200 | char *global_token, *namespaced_token; | |
3201 | struct lxc_list *it; | |
b863bf92 CB |
3202 | int fulllen = 0; |
3203 | bool get_all = false; | |
3204 | ||
3205 | if (!retv) | |
3206 | inlen = 0; | |
3207 | else | |
3208 | memset(retv, 0, inlen); | |
3209 | ||
54860ed0 CB |
3210 | if (version == CGROUP2_SUPER_MAGIC) { |
3211 | global_token = "lxc.cgroup2"; | |
3212 | namespaced_token = "lxc.cgroup2."; | |
6333c915 | 3213 | namespaced_token_len = STRLITERALLEN("lxc.cgroup2."); |
54860ed0 CB |
3214 | } else if (version == CGROUP_SUPER_MAGIC) { |
3215 | global_token = "lxc.cgroup"; | |
3216 | namespaced_token = "lxc.cgroup."; | |
6333c915 | 3217 | namespaced_token_len = STRLITERALLEN("lxc.cgroup."); |
54860ed0 CB |
3218 | } else { |
3219 | return -1; | |
3220 | } | |
3221 | ||
3222 | if (strcmp(key, global_token) == 0) | |
b863bf92 | 3223 | get_all = true; |
54860ed0 CB |
3224 | else if (strncmp(key, namespaced_token, namespaced_token_len) == 0) |
3225 | key += namespaced_token_len; | |
b863bf92 CB |
3226 | else |
3227 | return -1; | |
3228 | ||
6bede808 | 3229 | lxc_list_for_each(it, &c->cgroup) { |
b863bf92 | 3230 | struct lxc_cgroup *cg = it->elem; |
25a908b8 | 3231 | |
b863bf92 | 3232 | if (get_all) { |
54860ed0 CB |
3233 | if (version != cg->version) |
3234 | continue; | |
3235 | ||
47903908 | 3236 | strprint(retv, inlen, "%s.%s = %s\n", global_token, |
3237 | cg->subsystem, cg->value); | |
3238 | } else if (strcmp(cg->subsystem, key) == 0) { | |
b863bf92 CB |
3239 | strprint(retv, inlen, "%s\n", cg->value); |
3240 | } | |
3241 | } | |
3242 | ||
3243 | return fulllen; | |
3244 | } | |
5014ff2e | 3245 | |
54860ed0 CB |
3246 | static int get_config_cgroup_controller(const char *key, char *retv, int inlen, |
3247 | struct lxc_conf *c, void *data) | |
3248 | { | |
3249 | return __get_config_cgroup_controller(key, retv, inlen, c, | |
3250 | CGROUP_SUPER_MAGIC); | |
3251 | } | |
3252 | ||
3253 | static int get_config_cgroup2_controller(const char *key, char *retv, int inlen, | |
3254 | struct lxc_conf *c, void *data) | |
3255 | { | |
3256 | return __get_config_cgroup_controller(key, retv, inlen, c, | |
3257 | CGROUP2_SUPER_MAGIC); | |
3258 | } | |
3259 | ||
43654d34 CB |
3260 | static int get_config_cgroup_dir(const char *key, char *retv, int inlen, |
3261 | struct lxc_conf *lxc_conf, void *data) | |
3262 | { | |
3263 | int len; | |
3264 | int fulllen = 0; | |
3265 | ||
3266 | if (!retv) | |
3267 | inlen = 0; | |
3268 | else | |
3269 | memset(retv, 0, inlen); | |
3270 | ||
3271 | strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir); | |
3272 | ||
3273 | return fulllen; | |
3274 | } | |
3275 | ||
9caee129 CB |
3276 | static inline int get_config_cgroup_relative(const char *key, char *retv, |
3277 | int inlen, struct lxc_conf *lxc_conf, | |
3278 | void *data) | |
76f0e2e7 CB |
3279 | { |
3280 | return lxc_get_conf_int(lxc_conf, retv, inlen, | |
9caee129 | 3281 | lxc_conf->cgroup_meta.relative); |
76f0e2e7 CB |
3282 | } |
3283 | ||
6bede808 | 3284 | static int get_config_idmaps(const char *key, char *retv, int inlen, |
cccd2219 | 3285 | struct lxc_conf *c, void *data) |
5014ff2e CB |
3286 | { |
3287 | struct lxc_list *it; | |
3288 | int len, listlen, ret; | |
3289 | int fulllen = 0; | |
3290 | /* "u 1000 1000000 65536" | |
3291 | * | |
3292 | * let's render this as | |
3293 | * | |
3294 | * sizeof(char) | |
3295 | * + | |
3296 | * sizeof(" ") | |
3297 | * + | |
c77aee64 | 3298 | * sizeof(uint32_t) |
5014ff2e CB |
3299 | * + |
3300 | * sizeof(" ") | |
3301 | * + | |
c77aee64 | 3302 | * sizeof(uint32_t) |
5014ff2e CB |
3303 | * + |
3304 | * sizeof(" ") | |
3305 | * + | |
c77aee64 | 3306 | * sizeof(uint32_t) |
5014ff2e CB |
3307 | * + |
3308 | * \0 | |
3309 | */ | |
c77aee64 | 3310 | #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1) |
5014ff2e CB |
3311 | char buf[__LXC_IDMAP_STR_BUF]; |
3312 | ||
3313 | if (!retv) | |
3314 | inlen = 0; | |
3315 | else | |
3316 | memset(retv, 0, inlen); | |
3317 | ||
6bede808 | 3318 | listlen = lxc_list_len(&c->id_map); |
c77aee64 | 3319 | lxc_list_for_each(it, &c->id_map) { |
5014ff2e CB |
3320 | struct id_map *map = it->elem; |
3321 | ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu", | |
3322 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', | |
3323 | map->nsid, map->hostid, map->range); | |
3324 | if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF) | |
3325 | return -1; | |
3326 | ||
3327 | strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : ""); | |
3328 | } | |
47903908 | 3329 | |
5014ff2e CB |
3330 | return fulllen; |
3331 | } | |
b29b29be | 3332 | |
46cc906d | 3333 | static int get_config_log_level(const char *key, char *retv, int inlen, |
cccd2219 | 3334 | struct lxc_conf *c, void *data) |
b29b29be CB |
3335 | { |
3336 | const char *v; | |
6bede808 | 3337 | v = lxc_log_priority_to_string(c->loglevel); |
b29b29be CB |
3338 | return lxc_get_conf_str(retv, inlen, v); |
3339 | } | |
3d4630ab | 3340 | |
46cc906d | 3341 | static int get_config_log_file(const char *key, char *retv, int inlen, |
cccd2219 | 3342 | struct lxc_conf *c, void *data) |
3d4630ab | 3343 | { |
6bede808 | 3344 | return lxc_get_conf_str(retv, inlen, c->logfile); |
3d4630ab | 3345 | } |
0d601acb | 3346 | |
47148e96 CB |
3347 | static int get_config_mount_fstab(const char *key, char *retv, int inlen, |
3348 | struct lxc_conf *c, void *data) | |
0d601acb | 3349 | { |
6bede808 | 3350 | return lxc_get_conf_str(retv, inlen, c->fstab); |
0d601acb | 3351 | } |
43fbf8d9 | 3352 | |
6bede808 | 3353 | static int get_config_mount_auto(const char *key, char *retv, int inlen, |
cccd2219 | 3354 | struct lxc_conf *c, void *data) |
43fbf8d9 CB |
3355 | { |
3356 | int len, fulllen = 0; | |
3357 | const char *sep = ""; | |
3358 | ||
3359 | if (!retv) | |
3360 | inlen = 0; | |
3361 | else | |
3362 | memset(retv, 0, inlen); | |
3363 | ||
6bede808 | 3364 | if (!(c->auto_mounts & LXC_AUTO_ALL_MASK)) |
43fbf8d9 CB |
3365 | return 0; |
3366 | ||
6bede808 | 3367 | switch (c->auto_mounts & LXC_AUTO_PROC_MASK) { |
43fbf8d9 CB |
3368 | case LXC_AUTO_PROC_MIXED: |
3369 | strprint(retv, inlen, "%sproc:mixed", sep); | |
3370 | sep = " "; | |
3371 | break; | |
3372 | case LXC_AUTO_PROC_RW: | |
3373 | strprint(retv, inlen, "%sproc:rw", sep); | |
3374 | sep = " "; | |
3375 | break; | |
3376 | default: | |
3377 | break; | |
3378 | } | |
3379 | ||
6bede808 | 3380 | switch (c->auto_mounts & LXC_AUTO_SYS_MASK) { |
43fbf8d9 CB |
3381 | case LXC_AUTO_SYS_RO: |
3382 | strprint(retv, inlen, "%ssys:ro", sep); | |
3383 | sep = " "; | |
3384 | break; | |
3385 | case LXC_AUTO_SYS_RW: | |
3386 | strprint(retv, inlen, "%ssys:rw", sep); | |
3387 | sep = " "; | |
3388 | break; | |
3389 | case LXC_AUTO_SYS_MIXED: | |
3390 | strprint(retv, inlen, "%ssys:mixed", sep); | |
3391 | sep = " "; | |
3392 | break; | |
3393 | default: | |
3394 | break; | |
3395 | } | |
3396 | ||
6bede808 | 3397 | switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) { |
43fbf8d9 CB |
3398 | case LXC_AUTO_CGROUP_NOSPEC: |
3399 | strprint(retv, inlen, "%scgroup", sep); | |
43fbf8d9 CB |
3400 | break; |
3401 | case LXC_AUTO_CGROUP_MIXED: | |
3402 | strprint(retv, inlen, "%scgroup:mixed", sep); | |
43fbf8d9 CB |
3403 | break; |
3404 | case LXC_AUTO_CGROUP_RO: | |
3405 | strprint(retv, inlen, "%scgroup:ro", sep); | |
43fbf8d9 CB |
3406 | break; |
3407 | case LXC_AUTO_CGROUP_RW: | |
3408 | strprint(retv, inlen, "%scgroup:rw", sep); | |
43fbf8d9 CB |
3409 | break; |
3410 | case LXC_AUTO_CGROUP_FULL_NOSPEC: | |
3411 | strprint(retv, inlen, "%scgroup-full", sep); | |
43fbf8d9 CB |
3412 | break; |
3413 | case LXC_AUTO_CGROUP_FULL_MIXED: | |
3414 | strprint(retv, inlen, "%scgroup-full:mixed", sep); | |
43fbf8d9 CB |
3415 | break; |
3416 | case LXC_AUTO_CGROUP_FULL_RO: | |
3417 | strprint(retv, inlen, "%scgroup-full:ro", sep); | |
43fbf8d9 CB |
3418 | break; |
3419 | case LXC_AUTO_CGROUP_FULL_RW: | |
3420 | strprint(retv, inlen, "%scgroup-full:rw", sep); | |
43fbf8d9 CB |
3421 | break; |
3422 | default: | |
3423 | break; | |
3424 | } | |
3425 | ||
3426 | return fulllen; | |
3427 | } | |
cc921848 | 3428 | |
6bede808 | 3429 | static int get_config_mount(const char *key, char *retv, int inlen, |
cccd2219 | 3430 | struct lxc_conf *c, void *data) |
cc921848 CB |
3431 | { |
3432 | int len, fulllen = 0; | |
3433 | struct lxc_list *it; | |
3434 | ||
3435 | if (!retv) | |
3436 | inlen = 0; | |
3437 | else | |
3438 | memset(retv, 0, inlen); | |
3439 | ||
25a908b8 | 3440 | lxc_list_for_each(it, &c->mount_list) { |
cc921848 CB |
3441 | strprint(retv, inlen, "%s\n", (char *)it->elem); |
3442 | } | |
3443 | ||
3444 | return fulllen; | |
3445 | } | |
819114b6 | 3446 | |
7a96a068 CB |
3447 | static int get_config_rootfs_path(const char *key, char *retv, int inlen, |
3448 | struct lxc_conf *c, void *data) | |
819114b6 | 3449 | { |
6bede808 | 3450 | return lxc_get_conf_str(retv, inlen, c->rootfs.path); |
819114b6 | 3451 | } |
3af60359 | 3452 | |
6e54330c CB |
3453 | static int get_config_rootfs_managed(const char *key, char *retv, int inlen, |
3454 | struct lxc_conf *c, void *data) | |
3455 | { | |
3456 | return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed); | |
3457 | } | |
3458 | ||
6bede808 | 3459 | static int get_config_rootfs_mount(const char *key, char *retv, int inlen, |
cccd2219 | 3460 | struct lxc_conf *c, void *data) |
3af60359 | 3461 | { |
6bede808 | 3462 | return lxc_get_conf_str(retv, inlen, c->rootfs.mount); |
3af60359 | 3463 | } |
0e9db631 | 3464 | |
6bede808 | 3465 | static int get_config_rootfs_options(const char *key, char *retv, int inlen, |
cccd2219 | 3466 | struct lxc_conf *c, void *data) |
0e9db631 | 3467 | { |
6bede808 | 3468 | return lxc_get_conf_str(retv, inlen, c->rootfs.options); |
0e9db631 | 3469 | } |
8f183f38 | 3470 | |
b67771bc | 3471 | static int get_config_uts_name(const char *key, char *retv, int inlen, |
cccd2219 | 3472 | struct lxc_conf *c, void *data) |
e274f8b0 CB |
3473 | { |
3474 | return lxc_get_conf_str( | |
3475 | retv, inlen, | |
6bede808 | 3476 | c->utsname ? c->utsname->nodename : NULL); |
e274f8b0 | 3477 | } |
466c2e93 | 3478 | |
6bede808 | 3479 | static int get_config_hooks(const char *key, char *retv, int inlen, |
cccd2219 | 3480 | struct lxc_conf *c, void *data) |
466c2e93 CB |
3481 | { |
3482 | char *subkey; | |
3483 | int len, fulllen = 0, found = -1; | |
3484 | struct lxc_list *it; | |
3485 | int i; | |
3486 | ||
466c2e93 | 3487 | subkey = strchr(key, '.'); |
466c2e93 CB |
3488 | if (!subkey) |
3489 | return -1; | |
47903908 | 3490 | |
3491 | subkey = strchr(subkey + 1, '.'); | |
466c2e93 CB |
3492 | subkey++; |
3493 | if (!*subkey) | |
3494 | return -1; | |
47903908 | 3495 | |
466c2e93 CB |
3496 | for (i = 0; i < NUM_LXC_HOOKS; i++) { |
3497 | if (strcmp(lxchook_names[i], subkey) == 0) { | |
3498 | found = i; | |
3499 | break; | |
3500 | } | |
3501 | } | |
47903908 | 3502 | |
466c2e93 CB |
3503 | if (found == -1) |
3504 | return -1; | |
3505 | ||
3506 | if (!retv) | |
3507 | inlen = 0; | |
3508 | else | |
3509 | memset(retv, 0, inlen); | |
3510 | ||
6bede808 | 3511 | lxc_list_for_each(it, &c->hooks[found]) { |
466c2e93 CB |
3512 | strprint(retv, inlen, "%s\n", (char *)it->elem); |
3513 | } | |
47903908 | 3514 | |
466c2e93 CB |
3515 | return fulllen; |
3516 | } | |
d2ceff53 | 3517 | |
44ae0fb6 CB |
3518 | static int get_config_hooks_version(const char *key, char *retv, int inlen, |
3519 | struct lxc_conf *c, void *data) | |
3520 | { | |
3521 | return lxc_get_conf_int(c, retv, inlen, c->hooks_version); | |
3522 | } | |
3523 | ||
f9373e40 CB |
3524 | static int get_config_net(const char *key, char *retv, int inlen, |
3525 | struct lxc_conf *c, void *data) | |
d2ceff53 CB |
3526 | { |
3527 | int len, fulllen = 0; | |
3528 | struct lxc_list *it; | |
3529 | ||
3530 | if (!retv) | |
3531 | inlen = 0; | |
3532 | else | |
3533 | memset(retv, 0, inlen); | |
3534 | ||
6bede808 | 3535 | lxc_list_for_each(it, &c->network) { |
d2ceff53 CB |
3536 | struct lxc_netdev *n = it->elem; |
3537 | const char *t = lxc_net_type_to_str(n->type); | |
3538 | strprint(retv, inlen, "%s\n", t ? t : "(invalid)"); | |
3539 | } | |
3540 | ||
3541 | return fulllen; | |
3542 | } | |
bdccff60 | 3543 | |
6bede808 | 3544 | static int get_config_cap_drop(const char *key, char *retv, int inlen, |
cccd2219 | 3545 | struct lxc_conf *c, void *data) |
1c96d6d8 CB |
3546 | { |
3547 | int len, fulllen = 0; | |
3548 | struct lxc_list *it; | |
3549 | ||
3550 | if (!retv) | |
3551 | inlen = 0; | |
3552 | else | |
3553 | memset(retv, 0, inlen); | |
3554 | ||
6bede808 | 3555 | lxc_list_for_each(it, &c->caps) { |
b80927f2 CB |
3556 | strprint(retv, inlen, "%s\n", (char *)it->elem); |
3557 | } | |
25a908b8 | 3558 | |
b80927f2 CB |
3559 | return fulllen; |
3560 | } | |
3561 | ||
6bede808 | 3562 | static int get_config_cap_keep(const char *key, char *retv, int inlen, |
cccd2219 | 3563 | struct lxc_conf *c, void *data) |
b80927f2 CB |
3564 | { |
3565 | int len, fulllen = 0; | |
3566 | struct lxc_list *it; | |
3567 | ||
3568 | if (!retv) | |
3569 | inlen = 0; | |
3570 | else | |
3571 | memset(retv, 0, inlen); | |
3572 | ||
6bede808 | 3573 | lxc_list_for_each(it, &c->keepcaps) { |
1c96d6d8 CB |
3574 | strprint(retv, inlen, "%s\n", (char *)it->elem); |
3575 | } | |
25a908b8 | 3576 | |
1c96d6d8 CB |
3577 | return fulllen; |
3578 | } | |
0692663a | 3579 | |
3aed4934 CB |
3580 | static int get_config_console_path(const char *key, char *retv, int inlen, |
3581 | struct lxc_conf *c, void *data) | |
0692663a | 3582 | { |
6bede808 | 3583 | return lxc_get_conf_str(retv, inlen, c->console.path); |
0692663a | 3584 | } |
794d1c06 | 3585 | |
6bede808 | 3586 | static int get_config_console_logfile(const char *key, char *retv, int inlen, |
cccd2219 | 3587 | struct lxc_conf *c, void *data) |
794d1c06 | 3588 | { |
6bede808 | 3589 | return lxc_get_conf_str(retv, inlen, c->console.log_path); |
794d1c06 | 3590 | } |
75f55b1f | 3591 | |
d91adfa6 CB |
3592 | static int get_config_console_rotate(const char *key, char *retv, int inlen, |
3593 | struct lxc_conf *c, void *data) | |
3594 | { | |
3595 | return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate); | |
3596 | } | |
3597 | ||
3598 | ||
28f3b1cd CB |
3599 | static int get_config_console_buffer_size(const char *key, char *retv, |
3600 | int inlen, struct lxc_conf *c, | |
3601 | void *data) | |
a04220de | 3602 | { |
dcea90a0 | 3603 | return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size); |
a04220de CB |
3604 | } |
3605 | ||
861813e5 CB |
3606 | static int get_config_console_size(const char *key, char *retv, int inlen, |
3607 | struct lxc_conf *c, void *data) | |
3608 | { | |
3609 | return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size); | |
3610 | } | |
3611 | ||
3612 | ||
0b427da0 CB |
3613 | static int get_config_seccomp_profile(const char *key, char *retv, int inlen, |
3614 | struct lxc_conf *c, void *data) | |
75f55b1f | 3615 | { |
6bede808 | 3616 | return lxc_get_conf_str(retv, inlen, c->seccomp); |
75f55b1f | 3617 | } |
97f6dad0 | 3618 | |
6bede808 | 3619 | static int get_config_autodev(const char *key, char *retv, int inlen, |
cccd2219 | 3620 | struct lxc_conf *c, void *data) |
97f6dad0 | 3621 | { |
6bede808 | 3622 | return lxc_get_conf_int(c, retv, inlen, c->autodev); |
97f6dad0 | 3623 | } |
afee4324 | 3624 | |
55c84efc | 3625 | static int get_config_signal_halt(const char *key, char *retv, int inlen, |
25a908b8 | 3626 | struct lxc_conf *c, void *data) |
afee4324 | 3627 | { |
6bede808 | 3628 | return lxc_get_conf_int(c, retv, inlen, c->haltsignal); |
afee4324 | 3629 | } |
3aa8f359 | 3630 | |
55c84efc | 3631 | static int get_config_signal_reboot(const char *key, char *retv, int inlen, |
25a908b8 | 3632 | struct lxc_conf *c, void *data) |
3aa8f359 | 3633 | { |
6bede808 | 3634 | return lxc_get_conf_int(c, retv, inlen, c->rebootsignal); |
3aa8f359 | 3635 | } |
2e16269f | 3636 | |
55c84efc | 3637 | static int get_config_signal_stop(const char *key, char *retv, int inlen, |
25a908b8 | 3638 | struct lxc_conf *c, void *data) |
2e16269f | 3639 | { |
6bede808 | 3640 | return lxc_get_conf_int(c, retv, inlen, c->stopsignal); |
2e16269f | 3641 | } |
54345299 | 3642 | |
6bede808 | 3643 | static int get_config_start(const char *key, char *retv, int inlen, |
cccd2219 | 3644 | struct lxc_conf *c, void *data) |
54345299 | 3645 | { |
c6182222 | 3646 | if (strcmp(key + 10, "auto") == 0) |
6bede808 | 3647 | return lxc_get_conf_int(c, retv, inlen, c->start_auto); |
c6182222 | 3648 | else if (strcmp(key + 10, "delay") == 0) |
6bede808 | 3649 | return lxc_get_conf_int(c, retv, inlen, c->start_delay); |
c6182222 | 3650 | else if (strcmp(key + 10, "order") == 0) |
6bede808 | 3651 | return lxc_get_conf_int(c, retv, inlen, c->start_order); |
54345299 CB |
3652 | |
3653 | return -1; | |
3654 | } | |
de9df15e | 3655 | |
46cc906d | 3656 | static int get_config_log_syslog(const char *key, char *retv, int inlen, |
25a908b8 | 3657 | struct lxc_conf *c, void *data) |
de9df15e | 3658 | { |
6bede808 | 3659 | return lxc_get_conf_str(retv, inlen, c->syslog); |
de9df15e | 3660 | } |
ac0f949c | 3661 | |
6bede808 | 3662 | static int get_config_monitor(const char *key, char *retv, int inlen, |
cccd2219 | 3663 | struct lxc_conf *c, void *data) |
ac0f949c | 3664 | { |
6bede808 | 3665 | return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare); |
ac0f949c | 3666 | } |
90ec7b6e | 3667 | |
258f8051 CB |
3668 | static int get_config_monitor_signal_pdeath(const char *key, char *retv, |
3669 | int inlen, struct lxc_conf *c, | |
3670 | void *data) | |
3671 | { | |
3672 | return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath); | |
3673 | } | |
3674 | ||
6bede808 | 3675 | static int get_config_group(const char *key, char *retv, int inlen, |
cccd2219 | 3676 | struct lxc_conf *c, void *data) |
90ec7b6e CB |
3677 | { |
3678 | int len, fulllen = 0; | |
3679 | struct lxc_list *it; | |
3680 | ||
3681 | if (!retv) | |
3682 | inlen = 0; | |
3683 | else | |
3684 | memset(retv, 0, inlen); | |
3685 | ||
6bede808 | 3686 | lxc_list_for_each(it, &c->groups) { |
90ec7b6e CB |
3687 | strprint(retv, inlen, "%s\n", (char *)it->elem); |
3688 | } | |
25a908b8 | 3689 | |
90ec7b6e CB |
3690 | return fulllen; |
3691 | } | |
aa0db7d3 | 3692 | |
6bede808 | 3693 | static int get_config_environment(const char *key, char *retv, int inlen, |
cccd2219 | 3694 | struct lxc_conf *c, void *data) |
aa0db7d3 CB |
3695 | { |
3696 | int len, fulllen = 0; | |
3697 | struct lxc_list *it; | |
3698 | ||
3699 | if (!retv) | |
3700 | inlen = 0; | |
3701 | else | |
3702 | memset(retv, 0, inlen); | |
3703 | ||
6bede808 | 3704 | lxc_list_for_each(it, &c->environment) { |
aa0db7d3 CB |
3705 | strprint(retv, inlen, "%s\n", (char *)it->elem); |
3706 | } | |
25a908b8 | 3707 | |
aa0db7d3 CB |
3708 | return fulllen; |
3709 | } | |
96dfcb7d | 3710 | |
5cda27c1 SH |
3711 | static int get_config_execute_cmd(const char *key, char *retv, int inlen, |
3712 | struct lxc_conf *c, void *data) | |
3713 | { | |
3714 | return lxc_get_conf_str(retv, inlen, c->execute_cmd); | |
3715 | } | |
3716 | ||
6bede808 | 3717 | static int get_config_init_cmd(const char *key, char *retv, int inlen, |
cccd2219 | 3718 | struct lxc_conf *c, void *data) |
96dfcb7d | 3719 | { |
6bede808 | 3720 | return lxc_get_conf_str(retv, inlen, c->init_cmd); |
96dfcb7d | 3721 | } |
1398e9b1 | 3722 | |
3c491553 L |
3723 | static int get_config_init_cwd(const char *key, char *retv, int inlen, |
3724 | struct lxc_conf *c, void *data) | |
3725 | { | |
3726 | return lxc_get_conf_str(retv, inlen, c->init_cwd); | |
3727 | } | |
3728 | ||
6bede808 | 3729 | static int get_config_init_uid(const char *key, char *retv, int inlen, |
cccd2219 | 3730 | struct lxc_conf *c, void *data) |
1398e9b1 | 3731 | { |
6bede808 | 3732 | return lxc_get_conf_int(c, retv, inlen, c->init_uid); |
1398e9b1 | 3733 | } |
dfeb7e42 | 3734 | |
6bede808 | 3735 | static int get_config_init_gid(const char *key, char *retv, int inlen, |
cccd2219 | 3736 | struct lxc_conf *c, void *data) |
dfeb7e42 | 3737 | { |
6bede808 | 3738 | return lxc_get_conf_int(c, retv, inlen, c->init_gid); |
dfeb7e42 | 3739 | } |
62048afe | 3740 | |
6bede808 | 3741 | static int get_config_ephemeral(const char *key, char *retv, int inlen, |
cccd2219 | 3742 | struct lxc_conf *c, void *data) |
62048afe | 3743 | { |
6bede808 | 3744 | return lxc_get_conf_int(c, retv, inlen, c->ephemeral); |
62048afe | 3745 | } |
b09521ac | 3746 | |
6bede808 | 3747 | static int get_config_no_new_privs(const char *key, char *retv, int inlen, |
cccd2219 | 3748 | struct lxc_conf *c, void *data) |
b09521ac | 3749 | { |
6bede808 | 3750 | return lxc_get_conf_int(c, retv, inlen, c->no_new_privs); |
b09521ac | 3751 | } |
389f6466 | 3752 | |
25a908b8 | 3753 | /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value |
240d4b74 | 3754 | * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be |
3755 | * printed, in 'lxc.prlimit.resource = value' format. | |
389f6466 | 3756 | */ |
240d4b74 | 3757 | static int get_config_prlimit(const char *key, char *retv, int inlen, |
47903908 | 3758 | struct lxc_conf *c, void *data) |
389f6466 CB |
3759 | { |
3760 | int fulllen = 0, len; | |
3761 | bool get_all = false; | |
3762 | struct lxc_list *it; | |
3763 | ||
3764 | if (!retv) | |
3765 | inlen = 0; | |
3766 | else | |
3767 | memset(retv, 0, inlen); | |
3768 | ||
240d4b74 | 3769 | if (!strcmp(key, "lxc.prlimit")) |
389f6466 | 3770 | get_all = true; |
240d4b74 | 3771 | else if (strncmp(key, "lxc.prlimit.", 12) == 0) |
3772 | key += 12; | |
389f6466 CB |
3773 | else |
3774 | return -1; | |
3775 | ||
6bede808 | 3776 | lxc_list_for_each(it, &c->limits) { |
c77aee64 CB |
3777 | /* 2 colon separated 64 bit integers or the word 'unlimited' */ |
3778 | char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2]; | |
389f6466 CB |
3779 | int partlen; |
3780 | struct lxc_limit *lim = it->elem; | |
3781 | ||
3782 | if (lim->limit.rlim_cur == RLIM_INFINITY) { | |
6333c915 CB |
3783 | memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1); |
3784 | partlen = STRLITERALLEN("unlimited"); | |
389f6466 CB |
3785 | } else { |
3786 | partlen = sprintf(buf, "%" PRIu64, | |
3787 | (uint64_t)lim->limit.rlim_cur); | |
3788 | } | |
47903908 | 3789 | |
389f6466 | 3790 | if (lim->limit.rlim_cur != lim->limit.rlim_max) { |
25a908b8 | 3791 | if (lim->limit.rlim_max == RLIM_INFINITY) |
389f6466 | 3792 | memcpy(buf + partlen, ":unlimited", |
6333c915 | 3793 | STRLITERALLEN(":unlimited") + 1); |
25a908b8 | 3794 | else |
389f6466 CB |
3795 | sprintf(buf + partlen, ":%" PRIu64, |
3796 | (uint64_t)lim->limit.rlim_max); | |
389f6466 CB |
3797 | } |
3798 | ||
3799 | if (get_all) { | |
240d4b74 | 3800 | strprint(retv, inlen, "lxc.prlimit.%s = %s\n", |
389f6466 | 3801 | lim->resource, buf); |
47903908 | 3802 | } else if (strcmp(lim->resource, key) == 0) { |
389f6466 CB |
3803 | strprint(retv, inlen, "%s", buf); |
3804 | } | |
3805 | } | |
3806 | ||
3807 | return fulllen; | |
3808 | } | |
e08cb901 | 3809 | |
e409b214 CB |
3810 | /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then |
3811 | * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl | |
3812 | * entries will be printed, in 'lxc.sysctl.key = value' format. | |
7edd0540 L |
3813 | */ |
3814 | static int get_config_sysctl(const char *key, char *retv, int inlen, | |
47903908 | 3815 | struct lxc_conf *c, void *data) |
7edd0540 L |
3816 | { |
3817 | int len; | |
3818 | struct lxc_list *it; | |
3819 | int fulllen = 0; | |
3820 | bool get_all = false; | |
3821 | ||
3822 | if (!retv) | |
3823 | inlen = 0; | |
3824 | else | |
3825 | memset(retv, 0, inlen); | |
3826 | ||
3827 | if (strcmp(key, "lxc.sysctl") == 0) | |
3828 | get_all = true; | |
6333c915 CB |
3829 | else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0) |
3830 | key += STRLITERALLEN("lxc.sysctl."); | |
7edd0540 L |
3831 | else |
3832 | return -1; | |
3833 | ||
3834 | lxc_list_for_each(it, &c->sysctls) { | |
3835 | struct lxc_sysctl *elem = it->elem; | |
3836 | if (get_all) { | |
47903908 | 3837 | strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key, |
3838 | elem->value); | |
7edd0540 L |
3839 | } else if (strcmp(elem->key, key) == 0) { |
3840 | strprint(retv, inlen, "%s", elem->value); | |
3841 | } | |
3842 | } | |
3843 | ||
3844 | return fulllen; | |
3845 | } | |
3846 | ||
61d7a733 | 3847 | static int get_config_proc(const char *key, char *retv, int inlen, |
47903908 | 3848 | struct lxc_conf *c, void *data) |
61d7a733 YT |
3849 | { |
3850 | struct lxc_list *it; | |
3851 | int len; | |
3852 | int fulllen = 0; | |
3853 | bool get_all = false; | |
3854 | ||
3855 | if (!retv) | |
3856 | inlen = 0; | |
3857 | else | |
3858 | memset(retv, 0, inlen); | |
3859 | ||
3860 | if (strcmp(key, "lxc.proc") == 0) | |
3861 | get_all = true; | |
6333c915 CB |
3862 | else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0) |
3863 | key += STRLITERALLEN("lxc.proc."); | |
61d7a733 YT |
3864 | else |
3865 | return -1; | |
3866 | ||
3867 | lxc_list_for_each(it, &c->procs) { | |
3868 | struct lxc_proc *proc = it->elem; | |
3869 | ||
3870 | if (get_all) { | |
3871 | strprint(retv, inlen, "lxc.proc.%s = %s\n", | |
47903908 | 3872 | proc->filename, proc->value); |
61d7a733 YT |
3873 | } else if (strcmp(proc->filename, key) == 0) { |
3874 | strprint(retv, inlen, "%s", proc->value); | |
3875 | } | |
3876 | } | |
3877 | ||
3878 | return fulllen; | |
3879 | } | |
3880 | ||
1d8d3676 CB |
3881 | static int get_config_namespace_clone(const char *key, char *retv, int inlen, |
3882 | struct lxc_conf *c, void *data) | |
3883 | { | |
3884 | int i, len; | |
3885 | int fulllen = 0; | |
3886 | ||
3887 | if (!retv) | |
3888 | inlen = 0; | |
3889 | else | |
3890 | memset(retv, 0, inlen); | |
3891 | ||
3892 | for (i = 0; i < LXC_NS_MAX; i++) { | |
3893 | if (c->ns_clone & ns_info[i].clone_flag) | |
3894 | strprint(retv, inlen, "%s\n", ns_info[i].proc_name); | |
3895 | } | |
3896 | ||
3897 | return fulllen; | |
3898 | } | |
3899 | ||
abeb5bba CB |
3900 | static int get_config_namespace_keep(const char *key, char *retv, int inlen, |
3901 | struct lxc_conf *c, void *data) | |
3902 | { | |
3903 | int i, len; | |
3904 | int fulllen = 0; | |
3905 | ||
3906 | if (!retv) | |
3907 | inlen = 0; | |
3908 | else | |
3909 | memset(retv, 0, inlen); | |
3910 | ||
3911 | for (i = 0; i < LXC_NS_MAX; i++) { | |
3912 | if (c->ns_keep & ns_info[i].clone_flag) | |
3913 | strprint(retv, inlen, "%s\n", ns_info[i].proc_name); | |
3914 | } | |
3915 | ||
3916 | return fulllen; | |
3917 | } | |
3918 | ||
b074bbf1 CB |
3919 | static int get_config_namespace_share(const char *key, char *retv, int inlen, |
3920 | struct lxc_conf *c, void *data) | |
28d9e29e CB |
3921 | { |
3922 | int len, ns_idx; | |
3923 | const char *namespace; | |
3924 | int fulllen = 0; | |
3925 | ||
3926 | if (!retv) | |
3927 | inlen = 0; | |
3928 | else | |
3929 | memset(retv, 0, inlen); | |
3930 | ||
6333c915 | 3931 | namespace = key + STRLITERALLEN("lxc.namespace.share."); |
28d9e29e CB |
3932 | ns_idx = lxc_namespace_2_ns_idx(namespace); |
3933 | if (ns_idx < 0) | |
3934 | return ns_idx; | |
3935 | ||
b074bbf1 | 3936 | strprint(retv, inlen, "%s", c->ns_share[ns_idx]); |
28d9e29e CB |
3937 | |
3938 | return fulllen; | |
3939 | } | |
3940 | ||
e08cb901 | 3941 | /* Callbacks to clear config items. */ |
26471403 CB |
3942 | static inline int clr_config_personality(const char *key, struct lxc_conf *c, |
3943 | void *data) | |
e08cb901 CB |
3944 | { |
3945 | c->personality = -1; | |
3946 | return 0; | |
3947 | } | |
03818ae3 | 3948 | |
232763d6 CB |
3949 | static inline int clr_config_pty_max(const char *key, struct lxc_conf *c, |
3950 | void *data) | |
03818ae3 | 3951 | { |
e528c735 | 3952 | c->pty_max = 0; |
03818ae3 CB |
3953 | return 0; |
3954 | } | |
e7a4b096 | 3955 | |
fe1c5887 CB |
3956 | static inline int clr_config_tty_max(const char *key, struct lxc_conf *c, |
3957 | void *data) | |
e7a4b096 | 3958 | { |
885766f5 | 3959 | c->ttys.tty = 0; |
e7a4b096 CB |
3960 | return 0; |
3961 | } | |
eaf8c0c7 | 3962 | |
42e53c29 | 3963 | static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c, |
26471403 | 3964 | void *data) |
eaf8c0c7 | 3965 | { |
885766f5 CB |
3966 | free(c->ttys.dir); |
3967 | c->ttys.dir = NULL; | |
eaf8c0c7 CB |
3968 | return 0; |
3969 | } | |
6bd86308 | 3970 | |
953fe44f CB |
3971 | static inline int clr_config_apparmor_profile(const char *key, |
3972 | struct lxc_conf *c, void *data) | |
025718fb CB |
3973 | { |
3974 | free(c->lsm_aa_profile); | |
3975 | c->lsm_aa_profile = NULL; | |
3976 | return 0; | |
3977 | } | |
3061e04e | 3978 | |
953fe44f CB |
3979 | static inline int clr_config_apparmor_allow_incomplete(const char *key, |
3980 | struct lxc_conf *c, | |
3981 | void *data) | |
3061e04e CB |
3982 | { |
3983 | c->lsm_aa_allow_incomplete = 0; | |
3984 | return 0; | |
3985 | } | |
31fc3494 | 3986 | |
1800f924 WB |
3987 | static inline int clr_config_apparmor_allow_nesting(const char *key, |
3988 | struct lxc_conf *c, | |
3989 | void *data) | |
3990 | { | |
3991 | c->lsm_aa_allow_nesting = 0; | |
3992 | return 0; | |
3993 | } | |
3994 | ||
3995 | static inline int clr_config_apparmor_raw(const char *key, | |
3996 | struct lxc_conf *c, | |
3997 | void *data) | |
3998 | { | |
3999 | return lxc_clear_apparmor_raw(c); | |
4000 | } | |
4001 | ||
953fe44f CB |
4002 | static inline int clr_config_selinux_context(const char *key, |
4003 | struct lxc_conf *c, void *data) | |
31fc3494 CB |
4004 | { |
4005 | free(c->lsm_se_context); | |
4006 | c->lsm_se_context = NULL; | |
4007 | return 0; | |
4008 | } | |
754d01ce | 4009 | |
43654d34 CB |
4010 | static inline int clr_config_cgroup_controller(const char *key, |
4011 | struct lxc_conf *c, void *data) | |
754d01ce | 4012 | { |
54860ed0 CB |
4013 | return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC); |
4014 | } | |
4015 | ||
4016 | static inline int clr_config_cgroup2_controller(const char *key, | |
4017 | struct lxc_conf *c, void *data) | |
4018 | { | |
4019 | return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC); | |
754d01ce | 4020 | } |
d3a178de | 4021 | |
43654d34 CB |
4022 | static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf, |
4023 | void *data) | |
4024 | { | |
4025 | if (lxc_conf->cgroup_meta.dir) { | |
4026 | free(lxc_conf->cgroup_meta.dir); | |
4027 | lxc_conf->cgroup_meta.dir = NULL; | |
4028 | } | |
4029 | ||
4030 | return 0; | |
4031 | } | |
4032 | ||
9caee129 CB |
4033 | static inline int clr_config_cgroup_relative(const char *key, |
4034 | struct lxc_conf *lxc_conf, | |
4035 | void *data) | |
76f0e2e7 | 4036 | { |
9caee129 | 4037 | lxc_conf->cgroup_meta.relative = false; |
76f0e2e7 CB |
4038 | return 0; |
4039 | } | |
4040 | ||
26471403 CB |
4041 | static inline int clr_config_idmaps(const char *key, struct lxc_conf *c, |
4042 | void *data) | |
d3a178de CB |
4043 | { |
4044 | return lxc_clear_idmaps(c); | |
4045 | } | |
97d3338d | 4046 | |
46cc906d | 4047 | static inline int clr_config_log_level(const char *key, struct lxc_conf *c, |
26471403 | 4048 | void *data) |
97d3338d | 4049 | { |
4b73005c | 4050 | c->loglevel = LXC_LOG_LEVEL_NOTSET; |
97d3338d CB |
4051 | return 0; |
4052 | } | |
de46099c | 4053 | |
46cc906d | 4054 | static inline int clr_config_log_file(const char *key, struct lxc_conf *c, |
26471403 | 4055 | void *data) |
de46099c CB |
4056 | { |
4057 | free(c->logfile); | |
4058 | c->logfile = NULL; | |
4059 | return 0; | |
4060 | } | |
b4fa13cd | 4061 | |
26471403 CB |
4062 | static inline int clr_config_mount(const char *key, struct lxc_conf *c, |
4063 | void *data) | |
b4fa13cd CB |
4064 | { |
4065 | return lxc_clear_mount_entries(c); | |
4066 | } | |
4be81021 | 4067 | |
26471403 CB |
4068 | static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c, |
4069 | void *data) | |
4be81021 CB |
4070 | { |
4071 | return lxc_clear_automounts(c); | |
4072 | } | |
350d4b15 | 4073 | |
47148e96 CB |
4074 | static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c, |
4075 | void *data) | |
350d4b15 CB |
4076 | { |
4077 | free(c->fstab); | |
4078 | c->fstab = NULL; | |
4079 | return 0; | |
4080 | } | |
faca124d | 4081 | |
7a96a068 CB |
4082 | static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c, |
4083 | void *data) | |
faca124d CB |
4084 | { |
4085 | free(c->rootfs.path); | |
4086 | c->rootfs.path = NULL; | |
4087 | return 0; | |
4088 | } | |
4089 | ||
6e54330c CB |
4090 | static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c, |
4091 | void *data) | |
4092 | { | |
4093 | c->rootfs.managed = true; | |
4094 | return 0; | |
4095 | } | |
4096 | ||
26471403 CB |
4097 | static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c, |
4098 | void *data) | |
fddefc2d CB |
4099 | { |
4100 | free(c->rootfs.mount); | |
4101 | c->rootfs.mount = NULL; | |
4102 | return 0; | |
4103 | } | |
7b1eb67d | 4104 | |
26471403 CB |
4105 | static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c, |
4106 | void *data) | |
7b1eb67d CB |
4107 | { |
4108 | free(c->rootfs.options); | |
4109 | c->rootfs.options = NULL; | |
3437f95c CB |
4110 | |
4111 | free(c->rootfs.data); | |
4112 | c->rootfs.data = NULL; | |
4113 | ||
7b1eb67d CB |
4114 | return 0; |
4115 | } | |
02becb8d | 4116 | |
b67771bc | 4117 | static inline int clr_config_uts_name(const char *key, struct lxc_conf *c, |
26471403 | 4118 | void *data) |
d31d0103 CB |
4119 | { |
4120 | free(c->utsname); | |
4121 | c->utsname = NULL; | |
4122 | return 0; | |
4123 | } | |
c9eeb90c | 4124 | |
26471403 CB |
4125 | static inline int clr_config_hooks(const char *key, struct lxc_conf *c, |
4126 | void *data) | |
c9eeb90c CB |
4127 | { |
4128 | return lxc_clear_hooks(c, key); | |
4129 | } | |
4130 | ||
44ae0fb6 CB |
4131 | static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c, |
4132 | void *data) | |
4133 | { | |
4134 | /* default to legacy hooks version */ | |
4135 | c->hooks_version = 0; | |
4136 | return 0; | |
4137 | } | |
4138 | ||
f9373e40 CB |
4139 | static inline int clr_config_net(const char *key, struct lxc_conf *c, |
4140 | void *data) | |
f4488271 | 4141 | { |
c302b476 CB |
4142 | lxc_free_networks(&c->network); |
4143 | ||
e5d2fd7c | 4144 | return 0; |
f4488271 CB |
4145 | } |
4146 | ||
26471403 CB |
4147 | static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c, |
4148 | void *data) | |
244cb55b CB |
4149 | { |
4150 | return lxc_clear_config_caps(c); | |
4151 | } | |
c74cc490 | 4152 | |
26471403 CB |
4153 | static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c, |
4154 | void *data) | |
c74cc490 CB |
4155 | { |
4156 | return lxc_clear_config_keepcaps(c); | |
4157 | } | |
4e5b633f | 4158 | |
3aed4934 CB |
4159 | static inline int clr_config_console_path(const char *key, struct lxc_conf *c, |
4160 | void *data) | |
4e5b633f CB |
4161 | { |
4162 | free(c->console.path); | |
4163 | c->console.path = NULL; | |
4164 | return 0; | |
4165 | } | |
4166 | ||
7c2ec23a | 4167 | static inline int clr_config_console_logfile(const char *key, |
26471403 | 4168 | struct lxc_conf *c, void *data) |
7c2ec23a CB |
4169 | { |
4170 | free(c->console.log_path); | |
4171 | c->console.log_path = NULL; | |
4172 | return 0; | |
4173 | } | |
bbca37d8 | 4174 | |
d91adfa6 CB |
4175 | static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c, |
4176 | void *data) | |
4177 | { | |
4178 | c->console.log_rotate = 0; | |
4179 | return 0; | |
4180 | } | |
4181 | ||
28f3b1cd CB |
4182 | static inline int clr_config_console_buffer_size(const char *key, |
4183 | struct lxc_conf *c, void *data) | |
a04220de | 4184 | { |
28f3b1cd | 4185 | c->console.buffer_size = 0; |
a04220de CB |
4186 | return 0; |
4187 | } | |
4188 | ||
861813e5 CB |
4189 | static inline int clr_config_console_size(const char *key, struct lxc_conf *c, |
4190 | void *data) | |
4191 | { | |
4192 | c->console.log_size = 0; | |
4193 | return 0; | |
4194 | } | |
4195 | ||
0b427da0 CB |
4196 | static inline int clr_config_seccomp_profile(const char *key, |
4197 | struct lxc_conf *c, void *data) | |
bbca37d8 CB |
4198 | { |
4199 | free(c->seccomp); | |
4200 | c->seccomp = NULL; | |
4201 | return 0; | |
4202 | } | |
c721e86c | 4203 | |
26471403 CB |
4204 | static inline int clr_config_autodev(const char *key, struct lxc_conf *c, |
4205 | void *data) | |
c721e86c CB |
4206 | { |
4207 | c->autodev = 1; | |
4208 | return 0; | |
4209 | } | |
87b288d1 | 4210 | |
55c84efc | 4211 | static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c, |
26471403 | 4212 | void *data) |
87b288d1 CB |
4213 | { |
4214 | c->haltsignal = 0; | |
4215 | return 0; | |
4216 | } | |
cae63cfa | 4217 | |
55c84efc | 4218 | static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c, |
26471403 | 4219 | void *data) |
cae63cfa CB |
4220 | { |
4221 | c->rebootsignal = 0; | |
4222 | return 0; | |
4223 | } | |
de45f3a8 | 4224 | |
55c84efc | 4225 | static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c, |
26471403 | 4226 | void *data) |
de45f3a8 CB |
4227 | { |
4228 | c->stopsignal = 0; | |
4229 | return 0; | |
4230 | } | |
c6182222 | 4231 | |
26471403 CB |
4232 | static inline int clr_config_start(const char *key, struct lxc_conf *c, |
4233 | void *data) | |
c6182222 CB |
4234 | { |
4235 | if (strcmp(key + 10, "auto") == 0) | |
4236 | c->start_auto = 0; | |
4237 | else if (strcmp(key + 10, "delay") == 0) | |
4238 | c->start_delay = 0; | |
4239 | else if (strcmp(key + 10, "order") == 0) | |
4240 | c->start_order = 0; | |
4241 | ||
4242 | return 0; | |
4243 | } | |
998ca94f | 4244 | |
46cc906d | 4245 | static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c, |
26471403 | 4246 | void *data) |
998ca94f CB |
4247 | { |
4248 | free(c->syslog); | |
4249 | c->syslog = NULL; | |
4250 | return 0; | |
4251 | } | |
adad12ca | 4252 | |
26471403 CB |
4253 | static inline int clr_config_monitor(const char *key, struct lxc_conf *c, |
4254 | void *data) | |
adad12ca CB |
4255 | { |
4256 | c->monitor_unshare = 0; | |
4257 | return 0; | |
4258 | } | |
4850d223 | 4259 | |
258f8051 CB |
4260 | static inline int clr_config_monitor_signal_pdeath(const char *key, |
4261 | struct lxc_conf *c, void *data) | |
4262 | { | |
4263 | c->monitor_signal_pdeath = 0; | |
4264 | return 0; | |
4265 | } | |
4266 | ||
26471403 CB |
4267 | static inline int clr_config_group(const char *key, struct lxc_conf *c, |
4268 | void *data) | |
4850d223 CB |
4269 | { |
4270 | return lxc_clear_groups(c); | |
4271 | } | |
832fb63a | 4272 | |
26471403 CB |
4273 | static inline int clr_config_environment(const char *key, struct lxc_conf *c, |
4274 | void *data) | |
832fb63a CB |
4275 | { |
4276 | return lxc_clear_environment(c); | |
4850d223 | 4277 | } |
8e90af3e | 4278 | |
5cda27c1 SH |
4279 | static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c, |
4280 | void *data) | |
4281 | { | |
4282 | free(c->execute_cmd); | |
4283 | c->execute_cmd = NULL; | |
4284 | return 0; | |
4285 | } | |
4286 | ||
26471403 CB |
4287 | static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c, |
4288 | void *data) | |
8e90af3e CB |
4289 | { |
4290 | free(c->init_cmd); | |
4291 | c->init_cmd = NULL; | |
4292 | return 0; | |
4293 | } | |
ec76dcfb | 4294 | |
3c491553 L |
4295 | static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c, |
4296 | void *data) | |
4297 | { | |
4298 | free(c->init_cwd); | |
4299 | c->init_cwd = NULL; | |
4300 | return 0; | |
4301 | } | |
4302 | ||
26471403 CB |
4303 | static inline int clr_config_init_uid(const char *key, struct lxc_conf *c, |
4304 | void *data) | |
ec76dcfb CB |
4305 | { |
4306 | c->init_uid = 0; | |
4307 | return 0; | |
4308 | } | |
1044b247 | 4309 | |
26471403 CB |
4310 | static inline int clr_config_init_gid(const char *key, struct lxc_conf *c, |
4311 | void *data) | |
1044b247 CB |
4312 | { |
4313 | c->init_gid = 0; | |
4314 | return 0; | |
4315 | } | |
59e370db | 4316 | |
26471403 CB |
4317 | static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c, |
4318 | void *data) | |
59e370db CB |
4319 | { |
4320 | c->ephemeral = 0; | |
4321 | return 0; | |
4322 | } | |
b98c5ab0 | 4323 | |
26471403 CB |
4324 | static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c, |
4325 | void *data) | |
b98c5ab0 CB |
4326 | { |
4327 | c->no_new_privs = false; | |
4328 | return 0; | |
4329 | } | |
715ccc96 | 4330 | |
240d4b74 | 4331 | static inline int clr_config_prlimit(const char *key, struct lxc_conf *c, |
26471403 | 4332 | void *data) |
715ccc96 CB |
4333 | { |
4334 | return lxc_clear_limits(c, key); | |
4335 | } | |
fdf3c589 | 4336 | |
7edd0540 L |
4337 | static inline int clr_config_sysctl(const char *key, struct lxc_conf *c, |
4338 | void *data) | |
4339 | { | |
4340 | return lxc_clear_sysctls(c, key); | |
4341 | } | |
4342 | ||
61d7a733 YT |
4343 | static inline int clr_config_proc(const char *key, struct lxc_conf *c, |
4344 | void *data) | |
4345 | { | |
4346 | return lxc_clear_procs(c, key); | |
4347 | } | |
4348 | ||
26471403 CB |
4349 | static inline int clr_config_includefiles(const char *key, struct lxc_conf *c, |
4350 | void *data) | |
fdf3c589 CB |
4351 | { |
4352 | lxc_clear_includes(c); | |
4353 | return 0; | |
4354 | } | |
a3c8e600 | 4355 | |
1d8d3676 CB |
4356 | static int clr_config_namespace_clone(const char *key, |
4357 | struct lxc_conf *lxc_conf, void *data) | |
4358 | { | |
4359 | lxc_conf->ns_clone = 0; | |
4360 | return 0; | |
4361 | } | |
4362 | ||
abeb5bba CB |
4363 | static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf, |
4364 | void *data) | |
4365 | { | |
4366 | lxc_conf->ns_keep = 0; | |
4367 | return 0; | |
4368 | } | |
4369 | ||
b074bbf1 CB |
4370 | static int clr_config_namespace_share(const char *key, |
4371 | struct lxc_conf *lxc_conf, void *data) | |
28d9e29e CB |
4372 | { |
4373 | int ns_idx; | |
4374 | const char *namespace; | |
4375 | ||
6333c915 | 4376 | namespace = key + STRLITERALLEN("lxc.namespace.share."); |
28d9e29e CB |
4377 | ns_idx = lxc_namespace_2_ns_idx(namespace); |
4378 | if (ns_idx < 0) | |
4379 | return ns_idx; | |
4380 | ||
b074bbf1 CB |
4381 | free(lxc_conf->ns_share[ns_idx]); |
4382 | lxc_conf->ns_share[ns_idx] = NULL; | |
28d9e29e CB |
4383 | |
4384 | return 0; | |
4385 | } | |
4386 | ||
a3c8e600 | 4387 | static int get_config_includefiles(const char *key, char *retv, int inlen, |
cccd2219 | 4388 | struct lxc_conf *c, void *data) |
a3c8e600 CB |
4389 | { |
4390 | return -ENOSYS; | |
4391 | } | |
40db5d2f | 4392 | |
31ee747b CB |
4393 | static struct lxc_config_t *get_network_config_ops(const char *key, |
4394 | struct lxc_conf *lxc_conf, | |
4395 | ssize_t *idx, | |
4396 | char **deindexed_key) | |
40db5d2f | 4397 | { |
31ee747b CB |
4398 | int ret; |
4399 | unsigned int tmpidx; | |
4400 | size_t numstrlen; | |
40db5d2f CB |
4401 | char *copy, *idx_start, *idx_end; |
4402 | struct lxc_config_t *config = NULL; | |
4403 | ||
4404 | /* check that this is a sensible network key */ | |
31ee747b CB |
4405 | if (strncmp("lxc.net.", key, 8)) { |
4406 | ERROR("Invalid network configuration key \"%s\"", key); | |
40db5d2f | 4407 | return NULL; |
31ee747b | 4408 | } |
40db5d2f CB |
4409 | |
4410 | copy = strdup(key); | |
31ee747b CB |
4411 | if (!copy) { |
4412 | ERROR("Failed to duplicate string \"%s\"", key); | |
40db5d2f | 4413 | return NULL; |
31ee747b | 4414 | } |
40db5d2f | 4415 | |
f9373e40 | 4416 | /* lxc.net.<n> */ |
31ee747b CB |
4417 | if (!isdigit(*(key + 8))) { |
4418 | ERROR("Failed to detect digit in string \"%s\"", key + 8); | |
4419 | goto on_error; | |
4420 | } | |
40db5d2f | 4421 | |
31ee747b CB |
4422 | /* beginning of index string */ |
4423 | idx_start = (copy + 7); | |
4424 | *idx_start = '\0'; | |
40db5d2f | 4425 | |
31ee747b CB |
4426 | /* end of index string */ |
4427 | idx_end = strchr((copy + 8), '.'); | |
7451daf8 CB |
4428 | if (idx_end) |
4429 | *idx_end = '\0'; | |
31ee747b CB |
4430 | |
4431 | /* parse current index */ | |
4432 | ret = lxc_safe_uint((idx_start + 1), &tmpidx); | |
4433 | if (ret < 0) { | |
6d1400b5 | 4434 | errno = -ret; |
4435 | SYSERROR("Failed to parse usigned integer from string \"%s\"", | |
4436 | idx_start + 1); | |
31ee747b CB |
4437 | *idx = ret; |
4438 | goto on_error; | |
4439 | } | |
40db5d2f | 4440 | |
31ee747b CB |
4441 | /* This, of course is utterly nonsensical on so many levels, but |
4442 | * better safe than sorry. | |
4443 | * (Checking for INT_MAX here is intentional.) | |
4444 | */ | |
4445 | if (tmpidx == INT_MAX) { | |
25a908b8 | 4446 | SYSERROR("Number of configured networks would overflow the " |
47903908 | 4447 | "counter"); |
31ee747b | 4448 | goto on_error; |
40db5d2f | 4449 | } |
31ee747b CB |
4450 | *idx = tmpidx; |
4451 | ||
4452 | numstrlen = strlen((idx_start + 1)); | |
4453 | ||
4454 | /* repair configuration key */ | |
4455 | *idx_start = '.'; | |
31ee747b | 4456 | |
7451daf8 CB |
4457 | /* lxc.net.<idx>.<subkey> */ |
4458 | if (idx_end) { | |
4459 | *idx_end = '.'; | |
24fa7df6 FA |
4460 | if (strlen(idx_end + 1) == 0) { |
4461 | ERROR("No subkey in network configuration key \"%s\"", key); | |
4462 | goto on_error; | |
4463 | } | |
40db5d2f | 4464 | |
7451daf8 CB |
4465 | memmove(copy + 8, idx_end + 1, strlen(idx_end + 1)); |
4466 | copy[strlen(key) - numstrlen + 1] = '\0'; | |
4467 | ||
300df83e | 4468 | config = lxc_get_config(copy); |
7451daf8 | 4469 | if (!config) { |
25a908b8 | 4470 | ERROR("Unknown network configuration key \"%s\"", key); |
7451daf8 CB |
4471 | goto on_error; |
4472 | } | |
31ee747b CB |
4473 | } |
4474 | ||
7451daf8 CB |
4475 | if (deindexed_key) |
4476 | *deindexed_key = copy; | |
4477 | ||
31ee747b | 4478 | return config; |
40db5d2f CB |
4479 | |
4480 | on_error: | |
4481 | free(copy); | |
31ee747b | 4482 | return NULL; |
40db5d2f CB |
4483 | } |
4484 | ||
25a908b8 CB |
4485 | /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was |
4486 | * found. So we make sure next comes an integer, find the right callback (by | |
4487 | * rewriting the key), and call it. | |
40db5d2f | 4488 | */ |
f9373e40 CB |
4489 | static int set_config_net_nic(const char *key, const char *value, |
4490 | struct lxc_conf *lxc_conf, void *data) | |
40db5d2f | 4491 | { |
31ee747b CB |
4492 | int ret; |
4493 | const char *idxstring; | |
40db5d2f CB |
4494 | struct lxc_config_t *config; |
4495 | struct lxc_netdev *netdev; | |
4496 | ssize_t idx = -1; | |
31ee747b CB |
4497 | char *deindexed_key = NULL; |
4498 | ||
4499 | idxstring = key + 8; | |
4500 | if (!isdigit(*idxstring)) | |
4501 | return -1; | |
40db5d2f | 4502 | |
6bed0fb6 | 4503 | if (lxc_config_value_empty(value)) |
f9373e40 | 4504 | return clr_config_net_nic(key, lxc_conf, data); |
6bed0fb6 | 4505 | |
31ee747b | 4506 | config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key); |
40db5d2f CB |
4507 | if (!config || idx < 0) |
4508 | return -1; | |
4509 | ||
0070b1c4 | 4510 | netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true); |
31ee747b CB |
4511 | if (!netdev) { |
4512 | free(deindexed_key); | |
40db5d2f | 4513 | return -1; |
31ee747b | 4514 | } |
40db5d2f | 4515 | |
31ee747b CB |
4516 | ret = config->set(deindexed_key, value, lxc_conf, netdev); |
4517 | free(deindexed_key); | |
47903908 | 4518 | |
31ee747b | 4519 | return ret; |
40db5d2f | 4520 | } |
ff6da295 | 4521 | |
f9373e40 CB |
4522 | static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf, |
4523 | void *data) | |
ff6da295 | 4524 | { |
31ee747b | 4525 | int ret; |
519df1c1 | 4526 | const char *idxstring; |
ff6da295 CB |
4527 | struct lxc_config_t *config; |
4528 | struct lxc_netdev *netdev; | |
31ee747b CB |
4529 | ssize_t idx = -1; |
4530 | char *deindexed_key = NULL; | |
519df1c1 | 4531 | |
31ee747b CB |
4532 | idxstring = key + 8; |
4533 | if (!isdigit(*idxstring)) | |
519df1c1 CB |
4534 | return -1; |
4535 | ||
519df1c1 | 4536 | /* The left conjunct is pretty self-explanatory. The right conjunct |
31ee747b | 4537 | * checks whether the two pointers are equal. If they are we know that |
519df1c1 CB |
4538 | * this is not a key that is namespaced any further and so we are |
4539 | * supposed to clear the whole network. | |
4540 | */ | |
4541 | if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) { | |
4542 | unsigned int rmnetdevidx; | |
4543 | ||
4544 | if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0) | |
4545 | return -1; | |
4546 | ||
4547 | /* Remove network from network list. */ | |
4548 | lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx); | |
4549 | return 0; | |
4550 | } | |
ff6da295 | 4551 | |
31ee747b | 4552 | config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key); |
ff6da295 CB |
4553 | if (!config || idx < 0) |
4554 | return -1; | |
4555 | ||
0070b1c4 | 4556 | netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false); |
31ee747b CB |
4557 | if (!netdev) { |
4558 | free(deindexed_key); | |
ff6da295 | 4559 | return -1; |
31ee747b | 4560 | } |
ff6da295 | 4561 | |
31ee747b CB |
4562 | ret = config->clr(deindexed_key, lxc_conf, netdev); |
4563 | free(deindexed_key); | |
47903908 | 4564 | |
31ee747b | 4565 | return ret; |
ff6da295 CB |
4566 | } |
4567 | ||
f9373e40 CB |
4568 | static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf, |
4569 | void *data) | |
ff6da295 | 4570 | { |
070a05af | 4571 | struct lxc_netdev *netdev = data; |
ff6da295 | 4572 | |
ff6da295 CB |
4573 | if (!netdev) |
4574 | return -1; | |
4575 | ||
4576 | netdev->type = -1; | |
4577 | ||
4578 | return 0; | |
4579 | } | |
4580 | ||
f9373e40 CB |
4581 | static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf, |
4582 | void *data) | |
ff6da295 | 4583 | { |
070a05af | 4584 | struct lxc_netdev *netdev = data; |
ff6da295 | 4585 | |
ff6da295 CB |
4586 | if (!netdev) |
4587 | return -1; | |
4588 | ||
de4855a8 | 4589 | netdev->name[0] = '\0'; |
ff6da295 CB |
4590 | |
4591 | return 0; | |
4592 | } | |
4593 | ||
f9373e40 CB |
4594 | static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf, |
4595 | void *data) | |
ff6da295 | 4596 | { |
070a05af | 4597 | struct lxc_netdev *netdev = data; |
ff6da295 | 4598 | |
ff6da295 CB |
4599 | if (!netdev) |
4600 | return -1; | |
4601 | ||
4602 | netdev->flags = 0; | |
4603 | ||
4604 | return 0; | |
4605 | } | |
4606 | ||
f9373e40 CB |
4607 | static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf, |
4608 | void *data) | |
ff6da295 | 4609 | { |
070a05af | 4610 | struct lxc_netdev *netdev = data; |
ff6da295 | 4611 | |
ff6da295 CB |
4612 | if (!netdev) |
4613 | return -1; | |
4614 | ||
de4855a8 | 4615 | netdev->link[0] = '\0'; |
ff6da295 CB |
4616 | |
4617 | return 0; | |
4618 | } | |
4619 | ||
f9373e40 CB |
4620 | static int clr_config_net_macvlan_mode(const char *key, |
4621 | struct lxc_conf *lxc_conf, void *data) | |
ff6da295 | 4622 | { |
070a05af | 4623 | struct lxc_netdev *netdev = data; |
ff6da295 | 4624 | |
ff6da295 CB |
4625 | if (!netdev) |
4626 | return -1; | |
4627 | ||
6bed0fb6 CB |
4628 | if (netdev->type != LXC_NET_MACVLAN) |
4629 | return 0; | |
4630 | ||
ff6da295 CB |
4631 | netdev->priv.macvlan_attr.mode = -1; |
4632 | ||
4633 | return 0; | |
4634 | } | |
4635 | ||
f9373e40 CB |
4636 | static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf, |
4637 | void *data) | |
ff6da295 | 4638 | { |
070a05af | 4639 | struct lxc_netdev *netdev = data; |
ff6da295 | 4640 | |
ff6da295 CB |
4641 | if (!netdev) |
4642 | return -1; | |
4643 | ||
de4855a8 | 4644 | netdev->priv.veth_attr.pair[0] = '\0'; |
ff6da295 CB |
4645 | |
4646 | return 0; | |
4647 | } | |
4648 | ||
f9373e40 CB |
4649 | static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf, |
4650 | void *data) | |
ff6da295 | 4651 | { |
070a05af | 4652 | struct lxc_netdev *netdev = data; |
ff6da295 | 4653 | |
ff6da295 CB |
4654 | if (!netdev) |
4655 | return -1; | |
4656 | ||
4657 | free(netdev->upscript); | |
4658 | netdev->upscript = NULL; | |
4659 | ||
4660 | return 0; | |
4661 | } | |
4662 | ||
f9373e40 CB |
4663 | static int clr_config_net_script_down(const char *key, |
4664 | struct lxc_conf *lxc_conf, void *data) | |
ff6da295 | 4665 | { |
070a05af | 4666 | struct lxc_netdev *netdev = data; |
ff6da295 | 4667 | |
ff6da295 CB |
4668 | if (!netdev) |
4669 | return -1; | |
4670 | ||
4671 | free(netdev->downscript); | |
4672 | netdev->downscript = NULL; | |
4673 | ||
4674 | return 0; | |
4675 | } | |
4676 | ||
f9373e40 CB |
4677 | static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf, |
4678 | void *data) | |
ff6da295 | 4679 | { |
070a05af | 4680 | struct lxc_netdev *netdev = data; |
ff6da295 | 4681 | |
ff6da295 CB |
4682 | if (!netdev) |
4683 | return -1; | |
4684 | ||
4685 | free(netdev->hwaddr); | |
4686 | netdev->hwaddr = NULL; | |
4687 | ||
4688 | return 0; | |
4689 | } | |
4690 | ||
f9373e40 CB |
4691 | static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf, |
4692 | void *data) | |
ff6da295 | 4693 | { |
070a05af | 4694 | struct lxc_netdev *netdev = data; |
ff6da295 | 4695 | |
ff6da295 CB |
4696 | if (!netdev) |
4697 | return -1; | |
4698 | ||
4699 | free(netdev->mtu); | |
4700 | netdev->mtu = NULL; | |
4701 | ||
4702 | return 0; | |
4703 | } | |
4704 | ||
f9373e40 CB |
4705 | static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf, |
4706 | void *data) | |
ff6da295 | 4707 | { |
070a05af | 4708 | struct lxc_netdev *netdev = data; |
ff6da295 | 4709 | |
ff6da295 CB |
4710 | if (!netdev) |
4711 | return -1; | |
4712 | ||
4713 | netdev->priv.vlan_attr.vid = 0; | |
4714 | ||
4715 | return 0; | |
4716 | } | |
4717 | ||
f9373e40 CB |
4718 | static int clr_config_net_ipv4_gateway(const char *key, |
4719 | struct lxc_conf *lxc_conf, void *data) | |
ff6da295 | 4720 | { |
070a05af | 4721 | struct lxc_netdev *netdev = data; |
ff6da295 | 4722 | |
ff6da295 CB |
4723 | if (!netdev) |
4724 | return -1; | |
4725 | ||
4726 | free(netdev->ipv4_gateway); | |
4727 | netdev->ipv4_gateway = NULL; | |
4728 | ||
4729 | return 0; | |
4730 | } | |
4731 | ||
9ff60df2 CB |
4732 | static int clr_config_net_ipv4_address(const char *key, |
4733 | struct lxc_conf *lxc_conf, void *data) | |
ff6da295 | 4734 | { |
070a05af | 4735 | struct lxc_netdev *netdev = data; |
ff6da295 CB |
4736 | struct lxc_list *cur, *next; |
4737 | ||
ecbb3790 CB |
4738 | if (!netdev) |
4739 | return -1; | |
ff6da295 CB |
4740 | |
4741 | lxc_list_for_each_safe(cur, &netdev->ipv4, next) { | |
4742 | lxc_list_del(cur); | |
4743 | free(cur->elem); | |
4744 | free(cur); | |
4745 | } | |
4746 | ||
4747 | return 0; | |
4748 | } | |
4749 | ||
f9373e40 CB |
4750 | static int clr_config_net_ipv6_gateway(const char *key, |
4751 | struct lxc_conf *lxc_conf, void *data) | |
ff6da295 | 4752 | { |
070a05af | 4753 | struct lxc_netdev *netdev = data; |
ff6da295 | 4754 | |
ff6da295 CB |
4755 | if (!netdev) |
4756 | return -1; | |
4757 | ||
4758 | free(netdev->ipv6_gateway); | |
4759 | netdev->ipv6_gateway = NULL; | |
4760 | ||
4761 | return 0; | |
4762 | } | |
4763 | ||
2e44ae28 CB |
4764 | static int clr_config_net_ipv6_address(const char *key, |
4765 | struct lxc_conf *lxc_conf, void *data) | |
ff6da295 | 4766 | { |
070a05af | 4767 | struct lxc_netdev *netdev = data; |
ff6da295 CB |
4768 | struct lxc_list *cur, *next; |
4769 | ||
ecbb3790 CB |
4770 | if (!netdev) |
4771 | return -1; | |
ff6da295 CB |
4772 | |
4773 | lxc_list_for_each_safe(cur, &netdev->ipv6, next) { | |
4774 | lxc_list_del(cur); | |
4775 | free(cur->elem); | |
4776 | free(cur); | |
4777 | } | |
4778 | ||
4779 | return 0; | |
4780 | } | |
9d4bf22d | 4781 | |
f9373e40 CB |
4782 | static int get_config_net_nic(const char *key, char *retv, int inlen, |
4783 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4784 | { |
31ee747b CB |
4785 | int ret; |
4786 | const char *idxstring; | |
9d4bf22d CB |
4787 | struct lxc_config_t *config; |
4788 | struct lxc_netdev *netdev; | |
4789 | ssize_t idx = -1; | |
31ee747b | 4790 | char *deindexed_key = NULL; |
9d4bf22d | 4791 | |
31ee747b CB |
4792 | idxstring = key + 8; |
4793 | if (!isdigit(*idxstring)) | |
4794 | return -1; | |
4795 | ||
4796 | config = get_network_config_ops(key, c, &idx, &deindexed_key); | |
9d4bf22d CB |
4797 | if (!config || idx < 0) |
4798 | return -1; | |
4799 | ||
0070b1c4 | 4800 | netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false); |
31ee747b CB |
4801 | if (!netdev) { |
4802 | free(deindexed_key); | |
9d4bf22d | 4803 | return -1; |
31ee747b | 4804 | } |
9d4bf22d | 4805 | |
31ee747b CB |
4806 | ret = config->get(deindexed_key, retv, inlen, c, netdev); |
4807 | free(deindexed_key); | |
47903908 | 4808 | |
31ee747b | 4809 | return ret; |
9d4bf22d CB |
4810 | } |
4811 | ||
f9373e40 CB |
4812 | static int get_config_net_type(const char *key, char *retv, int inlen, |
4813 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4814 | { |
070a05af CB |
4815 | int len; |
4816 | int fulllen = 0; | |
4817 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4818 | |
4819 | if (!retv) | |
4820 | inlen = 0; | |
4821 | else | |
4822 | memset(retv, 0, inlen); | |
4823 | ||
9d4bf22d CB |
4824 | if (!netdev) |
4825 | return -1; | |
4826 | ||
4827 | strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type)); | |
4828 | ||
4829 | return fulllen; | |
4830 | } | |
4831 | ||
f9373e40 CB |
4832 | static int get_config_net_flags(const char *key, char *retv, int inlen, |
4833 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4834 | { |
070a05af CB |
4835 | int len; |
4836 | int fulllen = 0; | |
4837 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4838 | |
4839 | if (!retv) | |
4840 | inlen = 0; | |
4841 | else | |
4842 | memset(retv, 0, inlen); | |
4843 | ||
9d4bf22d CB |
4844 | if (!netdev) |
4845 | return -1; | |
4846 | ||
4847 | if (netdev->flags & IFF_UP) | |
4848 | strprint(retv, inlen, "up"); | |
4849 | ||
4850 | return fulllen; | |
4851 | } | |
4852 | ||
f9373e40 CB |
4853 | static int get_config_net_link(const char *key, char *retv, int inlen, |
4854 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4855 | { |
070a05af CB |
4856 | int len; |
4857 | int fulllen = 0; | |
4858 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4859 | |
4860 | if (!retv) | |
4861 | inlen = 0; | |
4862 | else | |
4863 | memset(retv, 0, inlen); | |
4864 | ||
9d4bf22d CB |
4865 | if (!netdev) |
4866 | return -1; | |
4867 | ||
de4855a8 | 4868 | if (netdev->link[0] != '\0') |
9d4bf22d CB |
4869 | strprint(retv, inlen, "%s", netdev->link); |
4870 | ||
4871 | return fulllen; | |
4872 | } | |
4873 | ||
f9373e40 CB |
4874 | static int get_config_net_name(const char *key, char *retv, int inlen, |
4875 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4876 | { |
070a05af CB |
4877 | int len; |
4878 | int fulllen = 0; | |
4879 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4880 | |
4881 | if (!retv) | |
4882 | inlen = 0; | |
4883 | else | |
4884 | memset(retv, 0, inlen); | |
4885 | ||
9d4bf22d CB |
4886 | if (!netdev) |
4887 | return -1; | |
4888 | ||
de4855a8 | 4889 | if (netdev->name[0] != '\0') |
9d4bf22d CB |
4890 | strprint(retv, inlen, "%s", netdev->name); |
4891 | ||
4892 | return fulllen; | |
4893 | } | |
4894 | ||
f9373e40 CB |
4895 | static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen, |
4896 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4897 | { |
070a05af CB |
4898 | int len; |
4899 | int fulllen = 0; | |
9d4bf22d | 4900 | const char *mode; |
070a05af | 4901 | struct lxc_netdev *netdev = data; |
9d4bf22d CB |
4902 | |
4903 | if (!retv) | |
4904 | inlen = 0; | |
4905 | else | |
4906 | memset(retv, 0, inlen); | |
4907 | ||
9d4bf22d CB |
4908 | if (!netdev) |
4909 | return -1; | |
4910 | ||
4911 | if (netdev->type != LXC_NET_MACVLAN) | |
4912 | return 0; | |
4913 | ||
4914 | switch (netdev->priv.macvlan_attr.mode) { | |
4915 | case MACVLAN_MODE_PRIVATE: | |
4916 | mode = "private"; | |
4917 | break; | |
4918 | case MACVLAN_MODE_VEPA: | |
4919 | mode = "vepa"; | |
4920 | break; | |
4921 | case MACVLAN_MODE_BRIDGE: | |
4922 | mode = "bridge"; | |
4923 | break; | |
4924 | case MACVLAN_MODE_PASSTHRU: | |
4925 | mode = "passthru"; | |
4926 | break; | |
4927 | default: | |
4928 | mode = "(invalid)"; | |
4929 | break; | |
4930 | } | |
4931 | ||
4932 | strprint(retv, inlen, "%s", mode); | |
4933 | ||
4934 | return fulllen; | |
4935 | } | |
4936 | ||
f9373e40 CB |
4937 | static int get_config_net_veth_pair(const char *key, char *retv, int inlen, |
4938 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4939 | { |
070a05af CB |
4940 | int len; |
4941 | int fulllen = 0; | |
4942 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4943 | |
4944 | if (!retv) | |
4945 | inlen = 0; | |
4946 | else | |
4947 | memset(retv, 0, inlen); | |
4948 | ||
9d4bf22d CB |
4949 | if (!netdev) |
4950 | return -1; | |
4951 | ||
4952 | if (netdev->type != LXC_NET_VETH) | |
4953 | return 0; | |
4954 | ||
4955 | strprint(retv, inlen, "%s", | |
de4855a8 CB |
4956 | netdev->priv.veth_attr.pair[0] != '\0' |
4957 | ? netdev->priv.veth_attr.pair | |
4958 | : netdev->priv.veth_attr.veth1); | |
9d4bf22d CB |
4959 | |
4960 | return fulllen; | |
4961 | } | |
4962 | ||
f9373e40 CB |
4963 | static int get_config_net_script_up(const char *key, char *retv, int inlen, |
4964 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4965 | { |
070a05af CB |
4966 | int len; |
4967 | int fulllen = 0; | |
4968 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4969 | |
4970 | if (!retv) | |
4971 | inlen = 0; | |
4972 | else | |
4973 | memset(retv, 0, inlen); | |
4974 | ||
9d4bf22d CB |
4975 | if (!netdev) |
4976 | return -1; | |
4977 | ||
4978 | if (netdev->upscript) | |
4979 | strprint(retv, inlen, "%s", netdev->upscript); | |
4980 | ||
4981 | return fulllen; | |
4982 | } | |
4983 | ||
f9373e40 CB |
4984 | static int get_config_net_script_down(const char *key, char *retv, int inlen, |
4985 | struct lxc_conf *c, void *data) | |
9d4bf22d | 4986 | { |
070a05af CB |
4987 | int len; |
4988 | int fulllen = 0; | |
4989 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
4990 | |
4991 | if (!retv) | |
4992 | inlen = 0; | |
4993 | else | |
4994 | memset(retv, 0, inlen); | |
4995 | ||
9d4bf22d CB |
4996 | if (!netdev) |
4997 | return -1; | |
4998 | ||
4999 | if (netdev->downscript) | |
5000 | strprint(retv, inlen, "%s", netdev->downscript); | |
5001 | ||
5002 | return fulllen; | |
5003 | } | |
5004 | ||
f9373e40 CB |
5005 | static int get_config_net_hwaddr(const char *key, char *retv, int inlen, |
5006 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5007 | { |
070a05af CB |
5008 | int len; |
5009 | int fulllen = 0; | |
5010 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5011 | |
5012 | if (!retv) | |
5013 | inlen = 0; | |
5014 | else | |
5015 | memset(retv, 0, inlen); | |
5016 | ||
9d4bf22d CB |
5017 | if (!netdev) |
5018 | return -1; | |
5019 | ||
5020 | if (netdev->hwaddr) | |
5021 | strprint(retv, inlen, "%s", netdev->hwaddr); | |
5022 | ||
5023 | return fulllen; | |
5024 | } | |
5025 | ||
f9373e40 CB |
5026 | static int get_config_net_mtu(const char *key, char *retv, int inlen, |
5027 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5028 | { |
070a05af CB |
5029 | int len; |
5030 | int fulllen = 0; | |
5031 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5032 | |
5033 | if (!retv) | |
5034 | inlen = 0; | |
5035 | else | |
5036 | memset(retv, 0, inlen); | |
5037 | ||
9d4bf22d CB |
5038 | if (!netdev) |
5039 | return -1; | |
5040 | ||
5041 | if (netdev->mtu) | |
5042 | strprint(retv, inlen, "%s", netdev->mtu); | |
5043 | ||
5044 | return fulllen; | |
5045 | } | |
5046 | ||
f9373e40 CB |
5047 | static int get_config_net_vlan_id(const char *key, char *retv, int inlen, |
5048 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5049 | { |
070a05af CB |
5050 | int len; |
5051 | int fulllen = 0; | |
5052 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5053 | |
5054 | if (!retv) | |
5055 | inlen = 0; | |
5056 | else | |
5057 | memset(retv, 0, inlen); | |
5058 | ||
9d4bf22d CB |
5059 | if (!netdev) |
5060 | return -1; | |
5061 | ||
5062 | if (netdev->type != LXC_NET_VLAN) | |
5063 | return 0; | |
5064 | ||
5065 | strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid); | |
5066 | ||
5067 | return fulllen; | |
5068 | } | |
5069 | ||
f9373e40 CB |
5070 | static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen, |
5071 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5072 | { |
070a05af | 5073 | int len; |
9d4bf22d | 5074 | char buf[INET_ADDRSTRLEN]; |
070a05af CB |
5075 | int fulllen = 0; |
5076 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5077 | |
5078 | if (!retv) | |
5079 | inlen = 0; | |
5080 | else | |
5081 | memset(retv, 0, inlen); | |
5082 | ||
9d4bf22d CB |
5083 | if (!netdev) |
5084 | return -1; | |
5085 | ||
5086 | if (netdev->ipv4_gateway_auto) { | |
5087 | strprint(retv, inlen, "auto"); | |
5088 | } else if (netdev->ipv4_gateway) { | |
5089 | inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf)); | |
5090 | strprint(retv, inlen, "%s", buf); | |
5091 | } | |
5092 | ||
5093 | return fulllen; | |
5094 | } | |
5095 | ||
9ff60df2 CB |
5096 | static int get_config_net_ipv4_address(const char *key, char *retv, int inlen, |
5097 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5098 | { |
070a05af | 5099 | int len; |
9d4bf22d CB |
5100 | size_t listlen; |
5101 | char buf[INET_ADDRSTRLEN]; | |
9d4bf22d | 5102 | struct lxc_list *it; |
070a05af CB |
5103 | int fulllen = 0; |
5104 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5105 | |
5106 | if (!retv) | |
5107 | inlen = 0; | |
5108 | else | |
5109 | memset(retv, 0, inlen); | |
5110 | ||
9d4bf22d CB |
5111 | if (!netdev) |
5112 | return -1; | |
5113 | ||
5114 | listlen = lxc_list_len(&netdev->ipv4); | |
47903908 | 5115 | |
9d4bf22d CB |
5116 | lxc_list_for_each(it, &netdev->ipv4) { |
5117 | struct lxc_inetdev *i = it->elem; | |
5118 | inet_ntop(AF_INET, &i->addr, buf, sizeof(buf)); | |
6fc7d8b6 | 5119 | strprint(retv, inlen, "%s/%u%s", buf, i->prefix, |
9d4bf22d CB |
5120 | (listlen-- > 1) ? "\n" : ""); |
5121 | } | |
5122 | ||
5123 | return fulllen; | |
5124 | } | |
5125 | ||
f9373e40 CB |
5126 | static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen, |
5127 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5128 | { |
070a05af | 5129 | int len; |
9d4bf22d | 5130 | char buf[INET6_ADDRSTRLEN]; |
070a05af CB |
5131 | int fulllen = 0; |
5132 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5133 | |
5134 | if (!retv) | |
5135 | inlen = 0; | |
5136 | else | |
5137 | memset(retv, 0, inlen); | |
5138 | ||
9d4bf22d CB |
5139 | if (!netdev) |
5140 | return -1; | |
5141 | ||
5142 | if (netdev->ipv6_gateway_auto) { | |
5143 | strprint(retv, inlen, "auto"); | |
5144 | } else if (netdev->ipv6_gateway) { | |
5145 | inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf)); | |
5146 | strprint(retv, inlen, "%s", buf); | |
5147 | } | |
5148 | ||
5149 | return fulllen; | |
5150 | } | |
5151 | ||
2e44ae28 CB |
5152 | static int get_config_net_ipv6_address(const char *key, char *retv, int inlen, |
5153 | struct lxc_conf *c, void *data) | |
9d4bf22d | 5154 | { |
070a05af | 5155 | int len; |
9d4bf22d CB |
5156 | size_t listlen; |
5157 | char buf[INET6_ADDRSTRLEN]; | |
9d4bf22d | 5158 | struct lxc_list *it; |
070a05af CB |
5159 | int fulllen = 0; |
5160 | struct lxc_netdev *netdev = data; | |
9d4bf22d CB |
5161 | |
5162 | if (!retv) | |
5163 | inlen = 0; | |
5164 | else | |
5165 | memset(retv, 0, inlen); | |
5166 | ||
9d4bf22d CB |
5167 | if (!netdev) |
5168 | return -1; | |
5169 | ||
5170 | listlen = lxc_list_len(&netdev->ipv6); | |
47903908 | 5171 | |
9d4bf22d CB |
5172 | lxc_list_for_each(it, &netdev->ipv6) { |
5173 | struct lxc_inet6dev *i = it->elem; | |
5174 | inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf)); | |
6fc7d8b6 | 5175 | strprint(retv, inlen, "%s/%u%s", buf, i->prefix, |
9d4bf22d CB |
5176 | (listlen-- > 1) ? "\n" : ""); |
5177 | } | |
5178 | ||
5179 | return fulllen; | |
5180 | } | |
cfc67626 CB |
5181 | |
5182 | int lxc_list_config_items(char *retv, int inlen) | |
5183 | { | |
5184 | size_t i; | |
5185 | int len; | |
5186 | int fulllen = 0; | |
5187 | ||
5188 | if (!retv) | |
5189 | inlen = 0; | |
5190 | else | |
5191 | memset(retv, 0, inlen); | |
5192 | ||
33eb2ec1 CB |
5193 | for (i = 0; i < config_jump_table_size; i++) { |
5194 | char *s = config_jump_table[i].name; | |
300df83e | 5195 | |
cfc67626 CB |
5196 | if (s[strlen(s) - 1] == '.') |
5197 | continue; | |
300df83e | 5198 | |
cfc67626 CB |
5199 | strprint(retv, inlen, "%s\n", s); |
5200 | } | |
5201 | ||
5202 | return fulllen; | |
5203 | } | |
a5448c15 | 5204 | |
fe9b7349 CB |
5205 | int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv, |
5206 | int inlen) | |
5207 | { | |
5208 | int len; | |
5209 | int fulllen = 0; | |
5210 | ||
5211 | if (!retv) | |
5212 | inlen = 0; | |
5213 | else | |
5214 | memset(retv, 0, inlen); | |
5215 | ||
5216 | if (!strcmp(key, "lxc.apparmor")) { | |
5217 | strprint(retv, inlen, "allow_incomplete\n"); | |
1800f924 | 5218 | strprint(retv, inlen, "allow_nesting\n"); |
fe9b7349 | 5219 | strprint(retv, inlen, "profile\n"); |
1800f924 | 5220 | strprint(retv, inlen, "raw\n"); |
dae8c253 CB |
5221 | } else if (!strcmp(key, "lxc.cgroup")) { |
5222 | strprint(retv, inlen, "dir\n"); | |
fe9b7349 CB |
5223 | } else if (!strcmp(key, "lxc.selinux")) { |
5224 | strprint(retv, inlen, "context\n"); | |
5225 | } else if (!strcmp(key, "lxc.mount")) { | |
5226 | strprint(retv, inlen, "auto\n"); | |
5227 | strprint(retv, inlen, "entry\n"); | |
5228 | strprint(retv, inlen, "fstab\n"); | |
5229 | } else if (!strcmp(key, "lxc.rootfs")) { | |
5230 | strprint(retv, inlen, "mount\n"); | |
5231 | strprint(retv, inlen, "options\n"); | |
5232 | strprint(retv, inlen, "path\n"); | |
5233 | } else if (!strcmp(key, "lxc.uts")) { | |
5234 | strprint(retv, inlen, "name\n"); | |
5235 | } else if (!strcmp(key, "lxc.hook")) { | |
5236 | strprint(retv, inlen, "autodev\n"); | |
5237 | strprint(retv, inlen, "clone\n"); | |
5238 | strprint(retv, inlen, "destroy\n"); | |
5239 | strprint(retv, inlen, "mount\n"); | |
5240 | strprint(retv, inlen, "post-stop\n"); | |
5241 | strprint(retv, inlen, "pre-mount\n"); | |
5242 | strprint(retv, inlen, "pre-start\n"); | |
08dd2805 | 5243 | strprint(retv, inlen, "start-host\n"); |
fe9b7349 CB |
5244 | strprint(retv, inlen, "start\n"); |
5245 | strprint(retv, inlen, "stop\n"); | |
5246 | } else if (!strcmp(key, "lxc.cap")) { | |
5247 | strprint(retv, inlen, "drop\n"); | |
5248 | strprint(retv, inlen, "keep\n"); | |
5249 | } else if (!strcmp(key, "lxc.console")) { | |
5250 | strprint(retv, inlen, "logfile\n"); | |
5251 | strprint(retv, inlen, "path\n"); | |
5252 | } else if (!strcmp(key, "lxc.seccomp")) { | |
5253 | strprint(retv, inlen, "profile\n"); | |
5254 | } else if (!strcmp(key, "lxc.signal")) { | |
5255 | strprint(retv, inlen, "halt\n"); | |
5256 | strprint(retv, inlen, "reboot\n"); | |
5257 | strprint(retv, inlen, "stop\n"); | |
5258 | } else if (!strcmp(key, "lxc.start")) { | |
5259 | strprint(retv, inlen, "auto\n"); | |
5260 | strprint(retv, inlen, "delay\n"); | |
5261 | strprint(retv, inlen, "order\n"); | |
5262 | } else if (!strcmp(key, "lxc.monitor")) { | |
5263 | strprint(retv, inlen, "unshare\n"); | |
5264 | } else { | |
5265 | fulllen = -1; | |
5266 | } | |
5267 | ||
5268 | return fulllen; | |
5269 | } | |
5270 | ||
a5448c15 CB |
5271 | int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen) |
5272 | { | |
5273 | int len; | |
5274 | const char *idxstring; | |
a5448c15 CB |
5275 | struct lxc_netdev *netdev; |
5276 | int fulllen = 0; | |
5277 | ssize_t idx = -1; | |
a5448c15 CB |
5278 | |
5279 | idxstring = key + 8; | |
5280 | if (!isdigit(*idxstring)) | |
5281 | return -1; | |
5282 | ||
7451daf8 CB |
5283 | (void)get_network_config_ops(key, c, &idx, NULL); |
5284 | if (idx < 0) | |
a5448c15 CB |
5285 | return -1; |
5286 | ||
5287 | netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false); | |
a5448c15 CB |
5288 | if (!netdev) |
5289 | return -1; | |
5290 | ||
5291 | if (!retv) | |
5292 | inlen = 0; | |
5293 | else | |
5294 | memset(retv, 0, inlen); | |
5295 | ||
5296 | strprint(retv, inlen, "type\n"); | |
5297 | strprint(retv, inlen, "script.up\n"); | |
5298 | strprint(retv, inlen, "script.down\n"); | |
47903908 | 5299 | |
a5448c15 CB |
5300 | if (netdev->type != LXC_NET_EMPTY) { |
5301 | strprint(retv, inlen, "flags\n"); | |
5302 | strprint(retv, inlen, "link\n"); | |
5303 | strprint(retv, inlen, "name\n"); | |
5304 | strprint(retv, inlen, "hwaddr\n"); | |
5305 | strprint(retv, inlen, "mtu\n"); | |
5306 | strprint(retv, inlen, "ipv6.address\n"); | |
5307 | strprint(retv, inlen, "ipv6.gateway\n"); | |
5308 | strprint(retv, inlen, "ipv4.address\n"); | |
5309 | strprint(retv, inlen, "ipv4.gateway\n"); | |
5310 | } | |
5311 | ||
5312 | switch (netdev->type) { | |
5313 | case LXC_NET_VETH: | |
5314 | strprint(retv, inlen, "veth.pair\n"); | |
5315 | break; | |
5316 | case LXC_NET_MACVLAN: | |
5317 | strprint(retv, inlen, "macvlan.mode\n"); | |
5318 | break; | |
5319 | case LXC_NET_VLAN: | |
5320 | strprint(retv, inlen, "vlan.id\n"); | |
5321 | break; | |
5322 | case LXC_NET_PHYS: | |
5323 | break; | |
5324 | } | |
5325 | ||
5326 | return fulllen; | |
5327 | } |