]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile.c
confile: add lxc.console.buffer.logfile
[mirror_lxc.git] / src / lxc / confile.c
1 /*
2 * lxc: linux Container library
3 * (C) Copyright IBM Corp. 2007, 2008
4 *
5 * Authors:
6 * Daniel Lezcano <daniel.lezcano at free.fr>
7 * Serge Hallyn <serge@hallyn.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
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
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #define _GNU_SOURCE
26 #define __STDC_FORMAT_MACROS
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <arpa/inet.h>
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <sys/utsname.h>
46
47 #include "conf.h"
48 #include "config.h"
49 #include "confile.h"
50 #include "confile_legacy.h"
51 #include "confile_utils.h"
52 #include "log.h"
53 #include "lxcseccomp.h"
54 #include "network.h"
55 #include "parse.h"
56 #include "storage.h"
57 #include "utils.h"
58
59 #if HAVE_IFADDRS_H
60 #include <ifaddrs.h>
61 #else
62 #include <../include/ifaddrs.h>
63 #endif
64
65 #if HAVE_SYS_PERSONALITY_H
66 #include <sys/personality.h>
67 #endif
68
69 lxc_log_define(lxc_confile, lxc);
70
71 #define lxc_config_define(name) \
72 static int set_config_##name(const char *, const char *, \
73 struct lxc_conf *, void *); \
74 static int get_config_##name(const char *, char *, int, \
75 struct lxc_conf *, void *); \
76 static int clr_config_##name(const char *, struct lxc_conf *, void *);
77
78 lxc_config_define(autodev);
79 lxc_config_define(apparmor_allow_incomplete);
80 lxc_config_define(apparmor_profile);
81 lxc_config_define(cap_drop);
82 lxc_config_define(cap_keep);
83 lxc_config_define(cgroup_controller);
84 lxc_config_define(cgroup_dir);
85 lxc_config_define(console_logfile);
86 lxc_config_define(console_buffer_logfile);
87 lxc_config_define(console_buffer_size);
88 lxc_config_define(console_path);
89 lxc_config_define(environment);
90 lxc_config_define(ephemeral);
91 lxc_config_define(execute_cmd);
92 lxc_config_define(group);
93 lxc_config_define(hooks);
94 lxc_config_define(idmaps);
95 lxc_config_define(includefiles);
96 lxc_config_define(init_cmd);
97 lxc_config_define(init_gid);
98 lxc_config_define(init_uid);
99 lxc_config_define(log_file);
100 lxc_config_define(log_level);
101 lxc_config_define(log_syslog);
102 lxc_config_define(monitor);
103 lxc_config_define(mount);
104 lxc_config_define(mount_auto);
105 lxc_config_define(mount_fstab);
106 lxc_config_define(namespace);
107 lxc_config_define(net);
108 lxc_config_define(net_flags);
109 lxc_config_define(net_hwaddr);
110 lxc_config_define(net_ipv4_address);
111 lxc_config_define(net_ipv4_gateway);
112 lxc_config_define(net_ipv6_address);
113 lxc_config_define(net_ipv6_gateway);
114 lxc_config_define(net_link);
115 lxc_config_define(net_macvlan_mode);
116 lxc_config_define(net_mtu);
117 lxc_config_define(net_name);
118 lxc_config_define(net_nic);
119 lxc_config_define(net_script_down);
120 lxc_config_define(net_script_up);
121 lxc_config_define(net_type);
122 lxc_config_define(net_veth_pair);
123 lxc_config_define(net_vlan_id);
124 lxc_config_define(no_new_privs);
125 lxc_config_define(noop);
126 lxc_config_define(personality);
127 lxc_config_define(prlimit);
128 lxc_config_define(pty_max);
129 lxc_config_define(rootfs_backend);
130 lxc_config_define(rootfs_mount);
131 lxc_config_define(rootfs_options);
132 lxc_config_define(rootfs_path);
133 lxc_config_define(seccomp_profile);
134 lxc_config_define(selinux_context);
135 lxc_config_define(signal_halt);
136 lxc_config_define(signal_reboot);
137 lxc_config_define(signal_stop);
138 lxc_config_define(start);
139 lxc_config_define(tty_max);
140 lxc_config_define(tty_dir);
141 lxc_config_define(uts_name);
142
143 static struct lxc_config_t config[] = {
144 /* REMOVE in LXC 3.0 */
145 { "lxc.arch", false, set_config_personality, get_config_personality, clr_config_personality, },
146 { "lxc.apparmor.profile", false, set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, },
147 { "lxc.apparmor.allow_incomplete", false, set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, },
148 { "lxc.autodev", false, set_config_autodev, get_config_autodev, clr_config_autodev, },
149 { "lxc.cap.drop", false, set_config_cap_drop, get_config_cap_drop, clr_config_cap_drop, },
150 { "lxc.cap.keep", false, set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, },
151 { "lxc.cgroup.dir", false, set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, },
152 { "lxc.cgroup", false, set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, },
153 { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile, get_config_console_buffer_logfile, clr_config_console_buffer_logfile, },
154 { "lxc.console.buffer.size", false, set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, },
155 { "lxc.console.logfile", false, set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, },
156 { "lxc.console.path", false, set_config_console_path, get_config_console_path, clr_config_console_path, },
157 { "lxc.environment", false, set_config_environment, get_config_environment, clr_config_environment, },
158 { "lxc.ephemeral", false, set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, },
159 { "lxc.execute.cmd", false, set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, },
160 { "lxc.group", false, set_config_group, get_config_group, clr_config_group, },
161 { "lxc.hook.autodev", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
162 { "lxc.hook.clone", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
163 { "lxc.hook.destroy", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
164 { "lxc.hook.mount", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
165 { "lxc.hook.post-stop", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
166 { "lxc.hook.start-host", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
167 { "lxc.hook.pre-start", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
168 { "lxc.hook.pre-mount", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
169 { "lxc.hook.start", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
170 { "lxc.hook.stop", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
171 { "lxc.hook", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
172 { "lxc.idmap", false, set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
173 { "lxc.include", false, set_config_includefiles, get_config_includefiles, clr_config_includefiles, },
174 { "lxc.init.cmd", false, set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
175 { "lxc.init.gid", false, set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
176 { "lxc.init.uid", false, set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
177 { "lxc.log.file", false, set_config_log_file, get_config_log_file, clr_config_log_file, },
178 { "lxc.log.level", false, set_config_log_level, get_config_log_level, clr_config_log_level, },
179 { "lxc.log.syslog", false, set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
180 { "lxc.monitor.unshare", false, set_config_monitor, get_config_monitor, clr_config_monitor, },
181 { "lxc.mount.auto", false, set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, },
182 { "lxc.mount.entry", false, set_config_mount, get_config_mount, clr_config_mount, },
183 { "lxc.mount.fstab", false, set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
184 { "lxc.namespace", false, set_config_namespace, get_config_namespace, clr_config_namespace, },
185
186 /* [START]: REMOVE IN LXC 3.0 */
187 { "lxc.network.type", true, set_config_network_legacy_type, get_config_network_legacy_item, clr_config_network_legacy_item, },
188 { "lxc.network.flags", true, set_config_network_legacy_flags, get_config_network_legacy_item, clr_config_network_legacy_item, },
189 { "lxc.network.link", true, set_config_network_legacy_link, get_config_network_legacy_item, clr_config_network_legacy_item, },
190 { "lxc.network.name", true, set_config_network_legacy_name, get_config_network_legacy_item, clr_config_network_legacy_item, },
191 { "lxc.network.macvlan.mode", true, set_config_network_legacy_macvlan_mode, get_config_network_legacy_item, clr_config_network_legacy_item, },
192 { "lxc.network.veth.pair", true, set_config_network_legacy_veth_pair, get_config_network_legacy_item, clr_config_network_legacy_item, },
193 { "lxc.network.script.up", true, set_config_network_legacy_script_up, get_config_network_legacy_item, clr_config_network_legacy_item, },
194 { "lxc.network.script.down", true, set_config_network_legacy_script_down, get_config_network_legacy_item, clr_config_network_legacy_item, },
195 { "lxc.network.hwaddr", true, set_config_network_legacy_hwaddr, get_config_network_legacy_item, clr_config_network_legacy_item, },
196 { "lxc.network.mtu", true, set_config_network_legacy_mtu, get_config_network_legacy_item, clr_config_network_legacy_item, },
197 { "lxc.network.vlan.id", true, set_config_network_legacy_vlan_id, get_config_network_legacy_item, clr_config_network_legacy_item, },
198 { "lxc.network.ipv4.gateway", true, set_config_network_legacy_ipv4_gateway, get_config_network_legacy_item, clr_config_network_legacy_item, },
199 { "lxc.network.ipv4", true, set_config_network_legacy_ipv4, get_config_network_legacy_item, clr_config_network_legacy_item, },
200 { "lxc.network.ipv6.gateway", true, set_config_network_legacy_ipv6_gateway, get_config_network_legacy_item, clr_config_network_legacy_item, },
201 { "lxc.network.ipv6", true, set_config_network_legacy_ipv6, get_config_network_legacy_item, clr_config_network_legacy_item, },
202 { "lxc.network.", true, set_config_network_legacy_nic, get_config_network_legacy_item, clr_config_network_legacy_item, },
203 { "lxc.network", true, set_config_network_legacy, get_config_network_legacy, clr_config_network_legacy, },
204 /* [END]: REMOVE IN LXC 3.0 */
205
206 { "lxc.net.flags", false, set_config_net_flags, get_config_net_flags, clr_config_net_flags, },
207 { "lxc.net.hwaddr", false, set_config_net_hwaddr, get_config_net_hwaddr, clr_config_net_hwaddr, },
208 { "lxc.net.ipv4.address", false, set_config_net_ipv4_address, get_config_net_ipv4_address, clr_config_net_ipv4_address, },
209 { "lxc.net.ipv4.gateway", false, set_config_net_ipv4_gateway, get_config_net_ipv4_gateway, clr_config_net_ipv4_gateway, },
210 { "lxc.net.ipv6.address", false, set_config_net_ipv6_address, get_config_net_ipv6_address, clr_config_net_ipv6_address, },
211 { "lxc.net.ipv6.gateway", false, set_config_net_ipv6_gateway, get_config_net_ipv6_gateway, clr_config_net_ipv6_gateway, },
212 { "lxc.net.link", false, set_config_net_link, get_config_net_link, clr_config_net_link, },
213 { "lxc.net.macvlan.mode", false, set_config_net_macvlan_mode, get_config_net_macvlan_mode, clr_config_net_macvlan_mode, },
214 { "lxc.net.mtu", false, set_config_net_mtu, get_config_net_mtu, clr_config_net_mtu, },
215 { "lxc.net.name", false, set_config_net_name, get_config_net_name, clr_config_net_name, },
216 { "lxc.net.script.down", false, set_config_net_script_down, get_config_net_script_down, clr_config_net_script_down, },
217 { "lxc.net.script.up", false, set_config_net_script_up, get_config_net_script_up, clr_config_net_script_up, },
218 { "lxc.net.type", false, set_config_net_type, get_config_net_type, clr_config_net_type, },
219 { "lxc.net.vlan.id", false, set_config_net_vlan_id, get_config_net_vlan_id, clr_config_net_vlan_id, },
220 { "lxc.net.veth.pair", false, set_config_net_veth_pair, get_config_net_veth_pair, clr_config_net_veth_pair, },
221 { "lxc.net.", false, set_config_net_nic, get_config_net_nic, clr_config_net_nic, },
222 { "lxc.net", false, set_config_net, get_config_net, clr_config_net, },
223 { "lxc.no_new_privs", false, set_config_no_new_privs, get_config_no_new_privs, clr_config_no_new_privs, },
224 { "lxc.prlimit", false, set_config_prlimit, get_config_prlimit, clr_config_prlimit, },
225 { "lxc.pty.max", false, set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
226 { "lxc.rootfs.mount", false, set_config_rootfs_mount, get_config_rootfs_mount, clr_config_rootfs_mount, },
227 { "lxc.rootfs.options", false, set_config_rootfs_options, get_config_rootfs_options, clr_config_rootfs_options, },
228 { "lxc.rootfs.path", false, set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
229 { "lxc.seccomp.profile", false, set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
230 { "lxc.selinux.context", false, set_config_selinux_context, get_config_selinux_context, clr_config_selinux_context, },
231 { "lxc.signal.halt", false, set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
232 { "lxc.signal.reboot", false, set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
233 { "lxc.signal.stop", false, set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
234 { "lxc.start.auto", false, set_config_start, get_config_start, clr_config_start, },
235 { "lxc.start.delay", false, set_config_start, get_config_start, clr_config_start, },
236 { "lxc.start.order", false, set_config_start, get_config_start, clr_config_start, },
237 { "lxc.tty.dir", false, set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
238 { "lxc.tty.max", false, set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
239 { "lxc.uts.name", false, set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
240
241 /* [START]: REMOVE IN LXC 3.0 */
242 { "lxc.pts", true, set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
243 { "lxc.devttydir", true, set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
244 { "lxc.tty", true, set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
245 { "lxc.aa_profile", true, set_config_lsm_aa_profile, get_config_lsm_aa_profile, clr_config_lsm_aa_profile, },
246 { "lxc.aa_allow_incomplete", true, set_config_lsm_aa_incomplete, get_config_lsm_aa_incomplete, clr_config_lsm_aa_incomplete, },
247 { "lxc.se_context", true, set_config_lsm_se_context, get_config_lsm_se_context, clr_config_lsm_se_context, },
248 { "lxc.id_map", true, set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
249 { "lxc.mount", true, set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
250 { "lxc.rootfs.backend", true, set_config_rootfs_backend, get_config_rootfs_backend, clr_config_rootfs_backend, },
251 { "lxc.rootfs", true, set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
252 { "lxc.utsname", true, set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
253 { "lxc.seccomp", true, set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
254 { "lxc.console", true, set_config_console_path, get_config_console_path, clr_config_console_path, },
255 { "lxc.haltsignal", true, set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
256 { "lxc.rebootsignal", true, set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
257 { "lxc.stopsignal", true, set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
258 { "lxc.syslog", true, set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
259 { "lxc.kmsg", true, set_config_noop, get_config_noop, clr_config_noop, },
260 { "lxc.loglevel", true, set_config_log_level, get_config_log_level, clr_config_log_level, },
261 { "lxc.logfile", true, set_config_log_file, get_config_log_file, clr_config_log_file, },
262 { "lxc.init_cmd", true, set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
263 { "lxc.init_uid", true, set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
264 { "lxc.init_gid", true, set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
265 { "lxc.limit", true, set_config_limit, get_config_limit, clr_config_limit, },
266 { "lxc.pivotdir", true, set_config_noop, get_config_noop, clr_config_noop, },
267 /* [END]: REMOVE IN LXC 3.0 */
268 };
269
270 struct signame {
271 int num;
272 const char *name;
273 };
274
275 static const struct signame signames[] = {
276 { SIGHUP, "HUP" },
277 { SIGINT, "INT" },
278 { SIGQUIT, "QUIT" },
279 { SIGILL, "ILL" },
280 { SIGABRT, "ABRT" },
281 { SIGFPE, "FPE" },
282 { SIGKILL, "KILL" },
283 { SIGSEGV, "SEGV" },
284 { SIGPIPE, "PIPE" },
285 { SIGALRM, "ALRM" },
286 { SIGTERM, "TERM" },
287 { SIGUSR1, "USR1" },
288 { SIGUSR2, "USR2" },
289 { SIGCHLD, "CHLD" },
290 { SIGCONT, "CONT" },
291 { SIGSTOP, "STOP" },
292 { SIGTSTP, "TSTP" },
293 { SIGTTIN, "TTIN" },
294 { SIGTTOU, "TTOU" },
295 #ifdef SIGTRAP
296 { SIGTRAP, "TRAP" },
297 #endif
298 #ifdef SIGIOT
299 { SIGIOT, "IOT" },
300 #endif
301 #ifdef SIGEMT
302 { SIGEMT, "EMT" },
303 #endif
304 #ifdef SIGBUS
305 { SIGBUS, "BUS" },
306 #endif
307 #ifdef SIGSTKFLT
308 { SIGSTKFLT, "STKFLT" },
309 #endif
310 #ifdef SIGCLD
311 { SIGCLD, "CLD" },
312 #endif
313 #ifdef SIGURG
314 { SIGURG, "URG" },
315 #endif
316 #ifdef SIGXCPU
317 { SIGXCPU, "XCPU" },
318 #endif
319 #ifdef SIGXFSZ
320 { SIGXFSZ, "XFSZ" },
321 #endif
322 #ifdef SIGVTALRM
323 { SIGVTALRM, "VTALRM" },
324 #endif
325 #ifdef SIGPROF
326 { SIGPROF, "PROF" },
327 #endif
328 #ifdef SIGWINCH
329 { SIGWINCH, "WINCH" },
330 #endif
331 #ifdef SIGIO
332 { SIGIO, "IO" },
333 #endif
334 #ifdef SIGPOLL
335 { SIGPOLL, "POLL" },
336 #endif
337 #ifdef SIGINFO
338 { SIGINFO, "INFO" },
339 #endif
340 #ifdef SIGLOST
341 { SIGLOST, "LOST" },
342 #endif
343 #ifdef SIGPWR
344 { SIGPWR, "PWR" },
345 #endif
346 #ifdef SIGUNUSED
347 { SIGUNUSED, "UNUSED" },
348 #endif
349 #ifdef SIGSYS
350 { SIGSYS, "SYS" },
351 #endif
352 };
353
354 static const size_t config_size = sizeof(config) / sizeof(struct lxc_config_t);
355
356 struct lxc_config_t *lxc_get_config(const char *key)
357 {
358 size_t i;
359
360 for (i = 0; i < config_size; i++)
361 if (!strncmp(config[i].name, key, strlen(config[i].name)))
362 return &config[i];
363
364 return NULL;
365 }
366
367 static int set_config_net(const char *key, const char *value,
368 struct lxc_conf *lxc_conf, void *data)
369 {
370 if (!lxc_config_value_empty(value)) {
371 ERROR("lxc.net must not have a value");
372 return -1;
373 }
374
375 return clr_config_net(key, lxc_conf, data);
376 }
377
378 static int set_config_net_type(const char *key, const char *value,
379 struct lxc_conf *lxc_conf, void *data)
380 {
381 struct lxc_netdev *netdev;
382
383 if (lxc_config_value_empty(value))
384 return clr_config_net_type(key, lxc_conf, data);
385
386 if (!data)
387 return -1;
388 else
389 netdev = data;
390 if (!netdev)
391 return -1;
392
393 if (!strcmp(value, "veth")) {
394 netdev->type = LXC_NET_VETH;
395 } else if (!strcmp(value, "macvlan")) {
396 netdev->type = LXC_NET_MACVLAN;
397 lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode,
398 "private");
399 } else if (!strcmp(value, "vlan")) {
400 netdev->type = LXC_NET_VLAN;
401 } else if (!strcmp(value, "phys")) {
402 netdev->type = LXC_NET_PHYS;
403 } else if (!strcmp(value, "empty")) {
404 netdev->type = LXC_NET_EMPTY;
405 } else if (!strcmp(value, "none")) {
406 netdev->type = LXC_NET_NONE;
407 } else {
408 ERROR("invalid network type %s", value);
409 return -1;
410 }
411
412 return 0;
413 }
414
415 static int set_config_net_flags(const char *key, const char *value,
416 struct lxc_conf *lxc_conf, void *data)
417 {
418 struct lxc_netdev *netdev;
419
420 if (lxc_config_value_empty(value))
421 return clr_config_net_flags(key, lxc_conf, data);
422
423 if (!data)
424 return -1;
425 else
426 netdev = data;
427 if (!netdev)
428 return -1;
429
430 netdev->flags |= IFF_UP;
431
432 return 0;
433 }
434
435 static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
436 struct lxc_netdev *netdev)
437 {
438 struct ifaddrs *ifaddr, *ifa;
439 int n;
440 int ret = 0;
441 const char *type_key = "lxc.net.type";
442 const char *link_key = "lxc.net.link";
443 const char *tmpvalue = "phys";
444
445 if (getifaddrs(&ifaddr) == -1) {
446 SYSERROR("Get network interfaces failed");
447 return -1;
448 }
449
450 for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
451 if (!ifa->ifa_addr)
452 continue;
453 if (ifa->ifa_addr->sa_family != AF_PACKET)
454 continue;
455
456 if (!strncmp(value, ifa->ifa_name, strlen(value) - 1)) {
457 ret = set_config_net_type(type_key, tmpvalue, lxc_conf,
458 netdev);
459 if (!ret) {
460 ret = set_config_net_link(
461 link_key, ifa->ifa_name, lxc_conf, netdev);
462 if (ret) {
463 ERROR("failed to create matched ifnames");
464 break;
465 }
466 } else {
467 ERROR("failed to create matched ifnames");
468 break;
469 }
470 }
471 }
472
473 freeifaddrs(ifaddr);
474 ifaddr = NULL;
475
476 return ret;
477 }
478
479 static int set_config_net_link(const char *key, const char *value,
480 struct lxc_conf *lxc_conf, void *data)
481 {
482 struct lxc_netdev *netdev;
483 int ret = 0;
484
485 if (lxc_config_value_empty(value))
486 return clr_config_net_link(key, lxc_conf, data);
487
488 if (!data)
489 return -1;
490 else
491 netdev = data;
492 if (!netdev)
493 return -1;
494
495 if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
496 ret = create_matched_ifnames(value, lxc_conf, netdev);
497 else
498 ret = network_ifname(netdev->link, value);
499
500 return ret;
501 }
502
503 static int set_config_net_name(const char *key, const char *value,
504 struct lxc_conf *lxc_conf, void *data)
505 {
506 struct lxc_netdev *netdev;
507
508 if (lxc_config_value_empty(value))
509 return clr_config_net_name(key, lxc_conf, data);
510
511 if (!data)
512 return -1;
513 else
514 netdev = data;
515 if (!netdev)
516 return -1;
517
518 return network_ifname(netdev->name, value);
519 }
520
521 static int set_config_net_veth_pair(const char *key, const char *value,
522 struct lxc_conf *lxc_conf, void *data)
523 {
524 struct lxc_netdev *netdev;
525
526 if (lxc_config_value_empty(value))
527 return clr_config_net_veth_pair(key, lxc_conf, data);
528
529 if (!data)
530 return -1;
531 else
532 netdev = data;
533 if (!netdev)
534 return -1;
535
536 return network_ifname(netdev->priv.veth_attr.pair, value);
537 }
538
539 static int set_config_net_macvlan_mode(const char *key, const char *value,
540 struct lxc_conf *lxc_conf, void *data)
541 {
542 struct lxc_netdev *netdev;
543
544 if (lxc_config_value_empty(value))
545 return clr_config_net_macvlan_mode(key, lxc_conf, data);
546
547 if (!data)
548 return -1;
549 else
550 netdev = data;
551 if (!netdev)
552 return -1;
553
554 return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
555 }
556
557 static int set_config_net_hwaddr(const char *key, const char *value,
558 struct lxc_conf *lxc_conf, void *data)
559 {
560 struct lxc_netdev *netdev;
561 char *new_value;
562
563 if (lxc_config_value_empty(value))
564 return clr_config_net_hwaddr(key, lxc_conf, data);
565
566 if (!data)
567 return -1;
568 else
569 netdev = data;
570 if (!netdev)
571 return -1;
572
573 new_value = strdup(value);
574 if (!new_value)
575 return -1;
576
577 rand_complete_hwaddr(new_value);
578
579 if (lxc_config_value_empty(new_value)) {
580 free(new_value);
581 netdev->hwaddr = NULL;
582 return 0;
583 }
584
585 netdev->hwaddr = new_value;
586 return 0;
587 }
588
589 static int set_config_net_vlan_id(const char *key, const char *value,
590 struct lxc_conf *lxc_conf, void *data)
591 {
592 int ret;
593 struct lxc_netdev *netdev;
594
595 if (lxc_config_value_empty(value))
596 return clr_config_net_vlan_id(key, lxc_conf, data);
597
598 if (!data)
599 return -1;
600 else
601 netdev = data;
602 if (!netdev)
603 return -1;
604
605 ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0);
606 if (ret < 0)
607 return -1;
608
609 return 0;
610 }
611
612 static int set_config_net_mtu(const char *key, const char *value,
613 struct lxc_conf *lxc_conf, void *data)
614 {
615 struct lxc_netdev *netdev;
616
617 if (lxc_config_value_empty(value))
618 return clr_config_net_mtu(key, lxc_conf, data);
619
620 if (!data)
621 return -1;
622 else
623 netdev = data;
624 if (!netdev)
625 return -1;
626
627 return set_config_string_item(&netdev->mtu, value);
628 }
629
630 static int set_config_net_ipv4_address(const char *key, const char *value,
631 struct lxc_conf *lxc_conf, void *data)
632 {
633 int ret;
634 struct lxc_netdev *netdev;
635 struct lxc_inetdev *inetdev;
636 struct lxc_list *list;
637 char *cursor, *slash;
638 char *addr = NULL, *bcast = NULL, *prefix = NULL;
639
640 if (lxc_config_value_empty(value))
641 return clr_config_net_ipv4_address(key, lxc_conf, data);
642
643 if (!data)
644 return -1;
645 else
646 netdev = data;
647 if (!netdev)
648 return -1;
649
650 inetdev = malloc(sizeof(*inetdev));
651 if (!inetdev)
652 return -1;
653
654 memset(inetdev, 0, sizeof(*inetdev));
655
656 list = malloc(sizeof(*list));
657 if (!list) {
658 free(inetdev);
659 return -1;
660 }
661
662 lxc_list_init(list);
663 list->elem = inetdev;
664
665 addr = strdup(value);
666 if (!addr) {
667 free(inetdev);
668 free(list);
669 return -1;
670 }
671
672 cursor = strstr(addr, " ");
673 if (cursor) {
674 *cursor = '\0';
675 bcast = cursor + 1;
676 }
677
678 slash = strstr(addr, "/");
679 if (slash) {
680 *slash = '\0';
681 prefix = slash + 1;
682 }
683
684 ret = inet_pton(AF_INET, addr, &inetdev->addr);
685 if (!ret || ret < 0) {
686 SYSERROR("Invalid ipv4 address \"%s\"", value);
687 free(inetdev);
688 free(addr);
689 free(list);
690 return -1;
691 }
692
693 if (bcast) {
694 ret = inet_pton(AF_INET, bcast, &inetdev->bcast);
695 if (!ret || ret < 0) {
696 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value);
697 free(inetdev);
698 free(list);
699 free(addr);
700 return -1;
701 }
702
703 }
704
705 /* No prefix specified, determine it from the network class. */
706 if (prefix) {
707 ret = lxc_safe_uint(prefix, &inetdev->prefix);
708 if (ret < 0)
709 return -1;
710 } else {
711 inetdev->prefix = config_ip_prefix(&inetdev->addr);
712 }
713
714 /* If no broadcast address, let compute one from the
715 * prefix and address.
716 */
717 if (!bcast) {
718 inetdev->bcast.s_addr = inetdev->addr.s_addr;
719 inetdev->bcast.s_addr |= htonl(INADDR_BROADCAST >> inetdev->prefix);
720 }
721
722 lxc_list_add_tail(&netdev->ipv4, list);
723
724 free(addr);
725 return 0;
726 }
727
728 static int set_config_net_ipv4_gateway(const char *key, const char *value,
729 struct lxc_conf *lxc_conf, void *data)
730 {
731 struct lxc_netdev *netdev;
732
733 if (lxc_config_value_empty(value))
734 return clr_config_net_ipv4_gateway(key, lxc_conf, data);
735
736 if (!data)
737 return -1;
738 else
739 netdev = data;
740 if (!netdev)
741 return -1;
742
743 free(netdev->ipv4_gateway);
744
745 if (!strcmp(value, "auto")) {
746 netdev->ipv4_gateway = NULL;
747 netdev->ipv4_gateway_auto = true;
748 } else {
749 int ret;
750 struct in_addr *gw;
751
752 gw = malloc(sizeof(*gw));
753 if (!gw)
754 return -1;
755
756 ret = inet_pton(AF_INET, value, gw);
757 if (!ret || ret < 0) {
758 SYSERROR("Invalid ipv4 gateway address \"%s\"", value);
759 free(gw);
760 return -1;
761 }
762
763 netdev->ipv4_gateway = gw;
764 netdev->ipv4_gateway_auto = false;
765 }
766
767 return 0;
768 }
769
770 static int set_config_net_ipv6_address(const char *key, const char *value,
771 struct lxc_conf *lxc_conf, void *data)
772 {
773 int ret;
774 struct lxc_netdev *netdev;
775 struct lxc_inet6dev *inet6dev;
776 struct lxc_list *list;
777 char *slash, *valdup, *netmask;
778
779 if (lxc_config_value_empty(value))
780 return clr_config_net_ipv6_address(key, lxc_conf, data);
781
782 if (!data)
783 return -1;
784 else
785 netdev = data;
786 if (!netdev)
787 return -1;
788
789 inet6dev = malloc(sizeof(*inet6dev));
790 if (!inet6dev)
791 return -1;
792
793 memset(inet6dev, 0, sizeof(*inet6dev));
794
795 list = malloc(sizeof(*list));
796 if (!list) {
797 free(inet6dev);
798 return -1;
799 }
800
801 lxc_list_init(list);
802 list->elem = inet6dev;
803
804 valdup = strdup(value);
805 if (!valdup) {
806 free(list);
807 free(inet6dev);
808 return -1;
809 }
810
811 inet6dev->prefix = 64;
812 slash = strstr(valdup, "/");
813 if (slash) {
814 *slash = '\0';
815 netmask = slash + 1;
816 if (lxc_safe_uint(netmask, &inet6dev->prefix) < 0)
817 return -1;
818 }
819
820 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
821 if (!ret || ret < 0) {
822 SYSERROR("Invalid ipv6 address \"%s\"", valdup);
823 free(list);
824 free(inet6dev);
825 free(valdup);
826 return -1;
827 }
828
829 lxc_list_add_tail(&netdev->ipv6, list);
830
831 free(valdup);
832 return 0;
833 }
834
835 static int set_config_net_ipv6_gateway(const char *key, const char *value,
836 struct lxc_conf *lxc_conf, void *data)
837 {
838 struct lxc_netdev *netdev;
839
840 if (lxc_config_value_empty(value))
841 return clr_config_net_ipv6_gateway(key, lxc_conf, data);
842
843 if (!data)
844 return -1;
845 else
846 netdev = data;
847 if (!netdev)
848 return -1;
849
850 free(netdev->ipv6_gateway);
851
852 if (!strcmp(value, "auto")) {
853 netdev->ipv6_gateway = NULL;
854 netdev->ipv6_gateway_auto = true;
855 } else {
856 int ret;
857 struct in6_addr *gw;
858
859 gw = malloc(sizeof(*gw));
860 if (!gw)
861 return -1;
862
863 ret = inet_pton(AF_INET6, value, gw);
864 if (!ret || ret < 0) {
865 SYSERROR("Invalid ipv6 gateway address \"%s\"", value);
866 free(gw);
867 return -1;
868 }
869
870 netdev->ipv6_gateway = gw;
871 netdev->ipv6_gateway_auto = false;
872 }
873
874 return 0;
875 }
876
877 static int set_config_net_script_up(const char *key, const char *value,
878 struct lxc_conf *lxc_conf, void *data)
879 {
880 struct lxc_netdev *netdev;
881
882 if (lxc_config_value_empty(value))
883 return clr_config_net_script_up(key, lxc_conf, data);
884
885 if (!data)
886 return -1;
887 else
888 netdev = data;
889 if (!netdev)
890 return -1;
891
892 return set_config_string_item(&netdev->upscript, value);
893 }
894
895 static int set_config_net_script_down(const char *key, const char *value,
896 struct lxc_conf *lxc_conf, void *data)
897 {
898 struct lxc_netdev *netdev;
899
900 if (lxc_config_value_empty(value))
901 return clr_config_net_script_down(key, lxc_conf, data);
902
903 if (!data)
904 return -1;
905 else
906 netdev = data;
907 if (!netdev)
908 return -1;
909
910 return set_config_string_item(&netdev->downscript, value);
911 }
912
913 static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook)
914 {
915 struct lxc_list *hooklist;
916
917 hooklist = malloc(sizeof(*hooklist));
918 if (!hooklist) {
919 free(hook);
920 return -1;
921 }
922
923 hooklist->elem = hook;
924 lxc_list_add_tail(&lxc_conf->hooks[which], hooklist);
925 return 0;
926 }
927
928 static int set_config_seccomp_profile(const char *key, const char *value,
929 struct lxc_conf *lxc_conf, void *data)
930 {
931 return set_config_path_item(&lxc_conf->seccomp, value);
932 }
933
934 static int set_config_execute_cmd(const char *key, const char *value,
935 struct lxc_conf *lxc_conf, void *data)
936 {
937 return set_config_path_item(&lxc_conf->execute_cmd, value);
938 }
939
940 static int set_config_init_cmd(const char *key, const char *value,
941 struct lxc_conf *lxc_conf, void *data)
942 {
943 return set_config_path_item(&lxc_conf->init_cmd, value);
944 }
945
946 static int set_config_init_uid(const char *key, const char *value,
947 struct lxc_conf *lxc_conf, void *data)
948 {
949 unsigned int init_uid;
950
951 if (lxc_config_value_empty(value)) {
952 lxc_conf->init_uid = 0;
953 return 0;
954 }
955
956 if (lxc_safe_uint(value, &init_uid) < 0)
957 return -1;
958
959 lxc_conf->init_uid = init_uid;
960
961 return 0;
962 }
963
964 static int set_config_init_gid(const char *key, const char *value,
965 struct lxc_conf *lxc_conf, void *data)
966 {
967 unsigned int init_gid;
968
969 if (lxc_config_value_empty(value)) {
970 lxc_conf->init_gid = 0;
971 return 0;
972 }
973
974 if (lxc_safe_uint(value, &init_gid) < 0)
975 return -1;
976
977 lxc_conf->init_gid = init_gid;
978
979 return 0;
980 }
981
982 static int set_config_hooks(const char *key, const char *value,
983 struct lxc_conf *lxc_conf, void *data)
984 {
985 char *copy;
986
987 if (lxc_config_value_empty(value))
988 return lxc_clear_hooks(lxc_conf, key);
989
990 if (strcmp(key + 4, "hook") == 0) {
991 ERROR("lxc.hook must not have a value");
992 return -1;
993 }
994
995 copy = strdup(value);
996 if (!copy)
997 return -1;
998
999 if (strcmp(key + 9, "pre-start") == 0)
1000 return add_hook(lxc_conf, LXCHOOK_PRESTART, copy);
1001 else if (strcmp(key + 9, "start-host") == 0)
1002 return add_hook(lxc_conf, LXCHOOK_START_HOST, copy);
1003 else if (strcmp(key + 9, "pre-mount") == 0)
1004 return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy);
1005 else if (strcmp(key + 9, "autodev") == 0)
1006 return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy);
1007 else if (strcmp(key + 9, "mount") == 0)
1008 return add_hook(lxc_conf, LXCHOOK_MOUNT, copy);
1009 else if (strcmp(key + 9, "start") == 0)
1010 return add_hook(lxc_conf, LXCHOOK_START, copy);
1011 else if (strcmp(key + 9, "stop") == 0)
1012 return add_hook(lxc_conf, LXCHOOK_STOP, copy);
1013 else if (strcmp(key + 9, "post-stop") == 0)
1014 return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy);
1015 else if (strcmp(key + 9, "clone") == 0)
1016 return add_hook(lxc_conf, LXCHOOK_CLONE, copy);
1017 else if (strcmp(key + 9, "destroy") == 0)
1018 return add_hook(lxc_conf, LXCHOOK_DESTROY, copy);
1019
1020 free(copy);
1021 return -1;
1022 }
1023
1024 static int set_config_personality(const char *key, const char *value,
1025 struct lxc_conf *lxc_conf, void *data)
1026 {
1027 signed long personality = lxc_config_parse_arch(value);
1028
1029 if (personality >= 0)
1030 lxc_conf->personality = personality;
1031 else
1032 WARN("Unsupported personality \"%s\"", value);
1033
1034 return 0;
1035 }
1036
1037 static int set_config_pty_max(const char *key, const char *value,
1038 struct lxc_conf *lxc_conf, void *data)
1039 {
1040 if (lxc_config_value_empty(value)) {
1041 lxc_conf->pts = 0;
1042 return 0;
1043 }
1044
1045 if (lxc_safe_uint(value, &lxc_conf->pts) < 0)
1046 return -1;
1047
1048 return 0;
1049 }
1050
1051 /* We only need to check whether the first byte of the key after the lxc.start.
1052 * prefix matches our expectations since they fortunately all start with a
1053 * different letter. If anything was wrong with the key we would have already
1054 * noticed when the callback was called.
1055 */
1056 static int set_config_start(const char *key, const char *value,
1057 struct lxc_conf *lxc_conf, void *data)
1058 {
1059 bool is_empty;
1060
1061 is_empty = lxc_config_value_empty(value);
1062
1063 if (*(key + 10) == 'a') { /* lxc.start.auto */
1064 if (is_empty) {
1065 lxc_conf->start_auto = 0;
1066 return 0;
1067 }
1068
1069 if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0)
1070 return -1;
1071
1072 if (lxc_conf->start_auto > 1)
1073 return -1;
1074
1075 return 0;
1076 } else if (*(key + 10) == 'd') { /* lxc.start.delay */
1077 if (is_empty) {
1078 lxc_conf->start_delay = 0;
1079 return 0;
1080 }
1081
1082 return lxc_safe_uint(value, &lxc_conf->start_delay);
1083 } else if (*(key + 10) == 'o') { /* lxc.start.order */
1084 if (is_empty) {
1085 lxc_conf->start_order = 0;
1086 return 0;
1087 }
1088
1089 return lxc_safe_int(value, &lxc_conf->start_order);
1090 }
1091
1092 return -1;
1093 }
1094
1095 static int set_config_monitor(const char *key, const char *value,
1096 struct lxc_conf *lxc_conf, void *data)
1097 {
1098 if (lxc_config_value_empty(value)) {
1099 lxc_conf->monitor_unshare = 0;
1100 return 0;
1101 }
1102
1103 if (strcmp(key + 12, "unshare") == 0)
1104 return lxc_safe_uint(value, &lxc_conf->monitor_unshare);
1105
1106 return -1;
1107 }
1108
1109 static int set_config_group(const char *key, const char *value,
1110 struct lxc_conf *lxc_conf, void *data)
1111 {
1112 char *groups, *groupptr, *sptr, *token;
1113 struct lxc_list *grouplist;
1114 int ret = -1;
1115
1116 if (lxc_config_value_empty(value))
1117 return lxc_clear_groups(lxc_conf);
1118
1119 groups = strdup(value);
1120 if (!groups)
1121 return -1;
1122
1123 /* In case several groups are specified in a single line split these
1124 * groups in a single element for the list.
1125 */
1126 for (groupptr = groups;; groupptr = NULL) {
1127 token = strtok_r(groupptr, " \t", &sptr);
1128 if (!token) {
1129 ret = 0;
1130 break;
1131 }
1132
1133 grouplist = malloc(sizeof(*grouplist));
1134 if (!grouplist)
1135 break;
1136
1137 grouplist->elem = strdup(token);
1138 if (!grouplist->elem) {
1139 free(grouplist);
1140 break;
1141 }
1142
1143 lxc_list_add_tail(&lxc_conf->groups, grouplist);
1144 }
1145
1146 free(groups);
1147 return ret;
1148 }
1149
1150 static int set_config_environment(const char *key, const char *value,
1151 struct lxc_conf *lxc_conf, void *data)
1152 {
1153 struct lxc_list *list_item = NULL;
1154
1155 if (lxc_config_value_empty(value))
1156 return lxc_clear_environment(lxc_conf);
1157
1158 list_item = malloc(sizeof(*list_item));
1159 if (!list_item)
1160 goto on_error;
1161
1162 list_item->elem = strdup(value);
1163
1164 if (!list_item->elem)
1165 goto on_error;
1166
1167 lxc_list_add_tail(&lxc_conf->environment, list_item);
1168
1169 return 0;
1170
1171 on_error:
1172 free(list_item);
1173 return -1;
1174 }
1175
1176 static int set_config_tty_max(const char *key, const char *value,
1177 struct lxc_conf *lxc_conf, void *data)
1178 {
1179 if (lxc_config_value_empty(value)) {
1180 lxc_conf->tty = 0;
1181 return 0;
1182 }
1183
1184 return lxc_safe_uint(value, &lxc_conf->tty);
1185 }
1186
1187 static int set_config_tty_dir(const char *key, const char *value,
1188 struct lxc_conf *lxc_conf, void *data)
1189 {
1190 return set_config_string_item_max(&lxc_conf->ttydir, value,
1191 NAME_MAX + 1);
1192 }
1193
1194 static int set_config_apparmor_profile(const char *key, const char *value,
1195 struct lxc_conf *lxc_conf, void *data)
1196 {
1197 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
1198 }
1199
1200 static int set_config_apparmor_allow_incomplete(const char *key,
1201 const char *value,
1202 struct lxc_conf *lxc_conf,
1203 void *data)
1204 {
1205 if (lxc_config_value_empty(value)) {
1206 lxc_conf->lsm_aa_allow_incomplete = 0;
1207 return 0;
1208 }
1209
1210 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0)
1211 return -1;
1212
1213 if (lxc_conf->lsm_aa_allow_incomplete > 1)
1214 return -1;
1215
1216 return 0;
1217 }
1218
1219 static int set_config_selinux_context(const char *key, const char *value,
1220 struct lxc_conf *lxc_conf, void *data)
1221 {
1222 return set_config_string_item(&lxc_conf->lsm_se_context, value);
1223 }
1224
1225 static int set_config_log_file(const char *key, const char *value,
1226 struct lxc_conf *c, void *data)
1227 {
1228 int ret;
1229
1230 if (lxc_config_value_empty(value)) {
1231 free(c->logfile);
1232 c->logfile = NULL;
1233 return 0;
1234 }
1235
1236 /* Store these values in the lxc_conf, and then try to set for actual
1237 * current logging.
1238 */
1239 ret = set_config_path_item(&c->logfile, value);
1240 if (ret == 0)
1241 ret = lxc_log_set_file(&c->logfd, c->logfile);
1242
1243 return ret;
1244 }
1245
1246 static int set_config_log_level(const char *key, const char *value,
1247 struct lxc_conf *lxc_conf, void *data)
1248 {
1249 int newlevel;
1250
1251 if (lxc_config_value_empty(value)) {
1252 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
1253 return 0;
1254 }
1255
1256 if (value[0] >= '0' && value[0] <= '9') {
1257 if (lxc_safe_int(value, &newlevel) < 0)
1258 return -1;
1259 } else {
1260 newlevel = lxc_log_priority_to_int(value);
1261 }
1262
1263 /* Store these values in the lxc_conf, and then try to set for actual
1264 * current logging.
1265 */
1266 lxc_conf->loglevel = newlevel;
1267 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
1268 }
1269
1270 static int set_config_autodev(const char *key, const char *value,
1271 struct lxc_conf *lxc_conf, void *data)
1272 {
1273 if (lxc_config_value_empty(value)) {
1274 lxc_conf->autodev = 0;
1275 return 0;
1276 }
1277
1278 if (lxc_safe_uint(value, &lxc_conf->autodev) < 0)
1279 return -1;
1280
1281 if (lxc_conf->autodev > 1)
1282 return -1;
1283
1284 return 0;
1285 }
1286
1287 static int sig_num(const char *sig)
1288 {
1289 unsigned int signum;
1290
1291 if (lxc_safe_uint(sig, &signum) < 0)
1292 return -1;
1293
1294 return signum;
1295 }
1296
1297 static int rt_sig_num(const char *signame)
1298 {
1299 int rtmax = 0, sig_n = 0;
1300
1301 if (strncasecmp(signame, "max-", 4) == 0) {
1302 rtmax = 1;
1303 }
1304
1305 signame += 4;
1306 if (!isdigit(*signame))
1307 return -1;
1308
1309 sig_n = sig_num(signame);
1310 sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n;
1311 if (sig_n > SIGRTMAX || sig_n < SIGRTMIN)
1312 return -1;
1313
1314 return sig_n;
1315 }
1316
1317 static int sig_parse(const char *signame)
1318 {
1319 size_t n;
1320
1321 if (isdigit(*signame)) {
1322 return sig_num(signame);
1323 } else if (strncasecmp(signame, "sig", 3) == 0) {
1324 signame += 3;
1325 if (strncasecmp(signame, "rt", 2) == 0)
1326 return rt_sig_num(signame + 2);
1327 for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) {
1328 if (strcasecmp(signames[n].name, signame) == 0)
1329 return signames[n].num;
1330 }
1331 }
1332
1333 return -1;
1334 }
1335
1336 static int set_config_signal_halt(const char *key, const char *value,
1337 struct lxc_conf *lxc_conf, void *data)
1338 {
1339 int sig_n;
1340
1341 if (lxc_config_value_empty(value)) {
1342 lxc_conf->haltsignal = 0;
1343 return 0;
1344 }
1345
1346 sig_n = sig_parse(value);
1347 if (sig_n < 0)
1348 return -1;
1349
1350 lxc_conf->haltsignal = sig_n;
1351
1352 return 0;
1353 }
1354
1355 static int set_config_signal_reboot(const char *key, const char *value,
1356 struct lxc_conf *lxc_conf, void *data)
1357 {
1358 int sig_n;
1359
1360 if (lxc_config_value_empty(value)) {
1361 lxc_conf->rebootsignal = 0;
1362 return 0;
1363 }
1364
1365 sig_n = sig_parse(value);
1366 if (sig_n < 0)
1367 return -1;
1368
1369 lxc_conf->rebootsignal = sig_n;
1370
1371 return 0;
1372 }
1373
1374 static int set_config_signal_stop(const char *key, const char *value,
1375 struct lxc_conf *lxc_conf, void *data)
1376 {
1377 int sig_n;
1378
1379 if (lxc_config_value_empty(value)) {
1380 lxc_conf->stopsignal = 0;
1381 return 0;
1382 }
1383
1384 sig_n = sig_parse(value);
1385 if (sig_n < 0)
1386 return -1;
1387
1388 lxc_conf->stopsignal = sig_n;
1389
1390 return 0;
1391 }
1392
1393 static int set_config_cgroup_controller(const char *key, const char *value,
1394 struct lxc_conf *lxc_conf, void *data)
1395 {
1396 char *subkey;
1397 char *token = "lxc.cgroup.";
1398 struct lxc_list *cglist = NULL;
1399 struct lxc_cgroup *cgelem = NULL;
1400
1401 if (lxc_config_value_empty(value))
1402 return lxc_clear_cgroups(lxc_conf, key);
1403
1404 subkey = strstr(key, token);
1405 if (!subkey)
1406 return -1;
1407
1408 if (!strlen(subkey))
1409 return -1;
1410
1411 if (strlen(subkey) == strlen(token))
1412 return -1;
1413
1414 subkey += strlen(token);
1415
1416 cglist = malloc(sizeof(*cglist));
1417 if (!cglist)
1418 goto out;
1419
1420 cgelem = malloc(sizeof(*cgelem));
1421 if (!cgelem)
1422 goto out;
1423 memset(cgelem, 0, sizeof(*cgelem));
1424
1425 cgelem->subsystem = strdup(subkey);
1426 cgelem->value = strdup(value);
1427
1428 if (!cgelem->subsystem || !cgelem->value)
1429 goto out;
1430
1431 cglist->elem = cgelem;
1432
1433 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
1434
1435 return 0;
1436
1437 out:
1438 free(cglist);
1439 if (cgelem) {
1440 free(cgelem->subsystem);
1441 free(cgelem->value);
1442 free(cgelem);
1443 }
1444
1445 return -1;
1446 }
1447
1448 static int set_config_cgroup_dir(const char *key, const char *value,
1449 struct lxc_conf *lxc_conf, void *data)
1450 {
1451 if (lxc_config_value_empty(value))
1452 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1453
1454 return set_config_string_item(&lxc_conf->cgroup_meta.dir, value);
1455 }
1456
1457 static int set_config_prlimit(const char *key, const char *value,
1458 struct lxc_conf *lxc_conf, void *data)
1459 {
1460 struct lxc_list *iter;
1461 struct rlimit limit;
1462 rlim_t limit_value;
1463 struct lxc_list *limlist = NULL;
1464 struct lxc_limit *limelem = NULL;
1465
1466 if (lxc_config_value_empty(value))
1467 return lxc_clear_limits(lxc_conf, key);
1468
1469 if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1470 return -1;
1471
1472 key += sizeof("lxc.prlimit.") - 1;
1473
1474 /* soft limit comes first in the value */
1475 if (!parse_limit_value(&value, &limit_value))
1476 return -1;
1477 limit.rlim_cur = limit_value;
1478
1479 /* skip spaces and a colon */
1480 while (isspace(*value))
1481 ++value;
1482
1483 if (*value == ':')
1484 ++value;
1485 else if (*value) /* any other character is an error here */
1486 return -1;
1487
1488 while (isspace(*value))
1489 ++value;
1490
1491 /* optional hard limit */
1492 if (*value) {
1493 if (!parse_limit_value(&value, &limit_value))
1494 return -1;
1495 limit.rlim_max = limit_value;
1496
1497 /* check for trailing garbage */
1498 while (isspace(*value))
1499 ++value;
1500
1501 if (*value)
1502 return -1;
1503 } else {
1504 /* a single value sets both hard and soft limit */
1505 limit.rlim_max = limit.rlim_cur;
1506 }
1507
1508 /* find existing list element */
1509 lxc_list_for_each(iter, &lxc_conf->limits)
1510 {
1511 limelem = iter->elem;
1512 if (!strcmp(key, limelem->resource)) {
1513 limelem->limit = limit;
1514 return 0;
1515 }
1516 }
1517
1518 /* allocate list element */
1519 limlist = malloc(sizeof(*limlist));
1520 if (!limlist)
1521 goto out;
1522
1523 limelem = malloc(sizeof(*limelem));
1524 if (!limelem)
1525 goto out;
1526 memset(limelem, 0, sizeof(*limelem));
1527
1528 limelem->resource = strdup(key);
1529 if (!limelem->resource)
1530 goto out;
1531 limelem->limit = limit;
1532
1533 limlist->elem = limelem;
1534
1535 lxc_list_add_tail(&lxc_conf->limits, limlist);
1536
1537 return 0;
1538
1539 out:
1540 free(limlist);
1541 if (limelem) {
1542 free(limelem->resource);
1543 free(limelem);
1544 }
1545 return -1;
1546 }
1547
1548 static int set_config_idmaps(const char *key, const char *value,
1549 struct lxc_conf *lxc_conf, void *data)
1550 {
1551 unsigned long hostid, nsid, range;
1552 char type;
1553 int ret;
1554 struct lxc_list *idmaplist = NULL;
1555 struct id_map *idmap = NULL;
1556
1557 if (lxc_config_value_empty(value))
1558 return lxc_clear_idmaps(lxc_conf);
1559
1560 idmaplist = malloc(sizeof(*idmaplist));
1561 if (!idmaplist)
1562 goto on_error;
1563
1564 idmap = malloc(sizeof(*idmap));
1565 if (!idmap)
1566 goto on_error;
1567 memset(idmap, 0, sizeof(*idmap));
1568
1569 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
1570 if (ret < 0)
1571 goto on_error;
1572
1573 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
1574 if (type == 'u')
1575 idmap->idtype = ID_TYPE_UID;
1576 else if (type == 'g')
1577 idmap->idtype = ID_TYPE_GID;
1578 else
1579 goto on_error;
1580
1581 idmap->hostid = hostid;
1582 idmap->nsid = nsid;
1583 idmap->range = range;
1584 idmaplist->elem = idmap;
1585 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
1586 idmap = NULL;
1587
1588 return 0;
1589
1590 on_error:
1591 free(idmaplist);
1592 free(idmap);
1593
1594 return -1;
1595 }
1596
1597 static int set_config_mount_fstab(const char *key, const char *value,
1598 struct lxc_conf *lxc_conf, void *data)
1599 {
1600 if (lxc_config_value_empty(value)) {
1601 clr_config_mount_fstab(key, lxc_conf, NULL);
1602 return -1;
1603 }
1604
1605 return set_config_path_item(&lxc_conf->fstab, value);
1606 }
1607
1608 static int set_config_mount_auto(const char *key, const char *value,
1609 struct lxc_conf *lxc_conf, void *data)
1610 {
1611 char *autos, *autoptr, *sptr, *token;
1612 int i;
1613 int ret = -1;
1614 static struct {
1615 const char *token;
1616 int mask;
1617 int flag;
1618 } allowed_auto_mounts[] = {
1619 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1620 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1621 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
1622 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1623 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
1624 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1625 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
1626 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
1627 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
1628 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
1629 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
1630 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
1631 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
1632 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
1633 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
1634 /* For adding anything that is just a single on/off, but has no
1635 * options: keep mask and flag identical and just define the enum
1636 * value as an unused bit so far
1637 */
1638 { NULL, 0, 0 }
1639 };
1640
1641 if (lxc_config_value_empty(value)) {
1642 lxc_conf->auto_mounts = 0;
1643 return 0;
1644 }
1645
1646 autos = strdup(value);
1647 if (!autos)
1648 return -1;
1649
1650 for (autoptr = autos;; autoptr = NULL) {
1651 token = strtok_r(autoptr, " \t", &sptr);
1652 if (!token) {
1653 ret = 0;
1654 break;
1655 }
1656
1657 for (i = 0; allowed_auto_mounts[i].token; i++) {
1658 if (!strcmp(allowed_auto_mounts[i].token, token))
1659 break;
1660 }
1661
1662 if (!allowed_auto_mounts[i].token) {
1663 ERROR("Invalid filesystem to automount \"%s\"", token);
1664 break;
1665 }
1666
1667 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
1668 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
1669 }
1670
1671 free(autos);
1672 return ret;
1673 }
1674
1675 static int set_config_mount(const char *key, const char *value,
1676 struct lxc_conf *lxc_conf, void *data)
1677 {
1678 char *mntelem;
1679 struct lxc_list *mntlist;
1680
1681 if (lxc_config_value_empty(value))
1682 return lxc_clear_mount_entries(lxc_conf);
1683
1684 mntlist = malloc(sizeof(*mntlist));
1685 if (!mntlist)
1686 return -1;
1687
1688 mntelem = strdup(value);
1689 if (!mntelem) {
1690 free(mntlist);
1691 return -1;
1692 }
1693 mntlist->elem = mntelem;
1694
1695 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
1696
1697 return 0;
1698 }
1699
1700 static int set_config_cap_keep(const char *key, const char *value,
1701 struct lxc_conf *lxc_conf, void *data)
1702 {
1703 char *keepcaps, *keepptr, *sptr, *token;
1704 struct lxc_list *keeplist;
1705 int ret = -1;
1706
1707 if (lxc_config_value_empty(value))
1708 return lxc_clear_config_keepcaps(lxc_conf);
1709
1710 keepcaps = strdup(value);
1711 if (!keepcaps)
1712 return -1;
1713
1714 /* In case several capability keep is specified in a single line
1715 * split these caps in a single element for the list.
1716 */
1717 for (keepptr = keepcaps;; keepptr = NULL) {
1718 token = strtok_r(keepptr, " \t", &sptr);
1719 if (!token) {
1720 ret = 0;
1721 break;
1722 }
1723
1724 if (!strcmp(token, "none"))
1725 lxc_clear_config_keepcaps(lxc_conf);
1726
1727 keeplist = malloc(sizeof(*keeplist));
1728 if (!keeplist)
1729 break;
1730
1731 keeplist->elem = strdup(token);
1732 if (!keeplist->elem) {
1733 free(keeplist);
1734 break;
1735 }
1736
1737 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
1738 }
1739
1740 free(keepcaps);
1741
1742 return ret;
1743 }
1744
1745 static int set_config_cap_drop(const char *key, const char *value,
1746 struct lxc_conf *lxc_conf, void *data)
1747 {
1748 char *dropcaps, *dropptr, *sptr, *token;
1749 struct lxc_list *droplist;
1750 int ret = -1;
1751
1752 if (lxc_config_value_empty(value))
1753 return lxc_clear_config_caps(lxc_conf);
1754
1755 dropcaps = strdup(value);
1756 if (!dropcaps)
1757 return -1;
1758
1759 /* In case several capability drop is specified in a single line
1760 * split these caps in a single element for the list.
1761 */
1762 for (dropptr = dropcaps;; dropptr = NULL) {
1763 token = strtok_r(dropptr, " \t", &sptr);
1764 if (!token) {
1765 ret = 0;
1766 break;
1767 }
1768
1769 droplist = malloc(sizeof(*droplist));
1770 if (!droplist)
1771 break;
1772
1773 droplist->elem = strdup(token);
1774 if (!droplist->elem) {
1775 free(droplist);
1776 break;
1777 }
1778
1779 lxc_list_add_tail(&lxc_conf->caps, droplist);
1780 }
1781
1782 free(dropcaps);
1783
1784 return ret;
1785 }
1786
1787 static int set_config_console_path(const char *key, const char *value,
1788 struct lxc_conf *lxc_conf, void *data)
1789 {
1790 return set_config_path_item(&lxc_conf->console.path, value);
1791 }
1792
1793 static int set_config_console_logfile(const char *key, const char *value,
1794 struct lxc_conf *lxc_conf, void *data)
1795 {
1796 return set_config_path_item(&lxc_conf->console.log_path, value);
1797 }
1798
1799 static int set_config_console_buffer_size(const char *key, const char *value,
1800 struct lxc_conf *lxc_conf, void *data)
1801 {
1802 int ret;
1803 int64_t size;
1804 uint64_t buffer_size, pgsz;
1805
1806 if (lxc_config_value_empty(value)) {
1807 lxc_conf->console.buffer_size = 0;
1808 return 0;
1809 }
1810
1811 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1812 if (!strcmp(value, "auto")) {
1813 lxc_conf->console.buffer_size = 1 << 17;
1814 return 0;
1815 }
1816
1817 ret = parse_byte_size_string(value, &size);
1818 if (ret < 0)
1819 return -1;
1820
1821 if (size < 0)
1822 return -EINVAL;
1823
1824 /* must be at least a page size */
1825 pgsz = lxc_getpagesize();
1826 if ((uint64_t)size < pgsz) {
1827 NOTICE("Requested ringbuffer size for the console is %" PRId64
1828 " but must be at least %" PRId64
1829 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
1830 size, pgsz, pgsz);
1831 size = pgsz;
1832 }
1833
1834 buffer_size = lxc_find_next_power2((uint64_t)size);
1835 if (buffer_size == 0)
1836 return -EINVAL;
1837
1838 if (buffer_size != size)
1839 NOTICE("Passed size was not a power of 2. Rounding log size to "
1840 "next power of two: %" PRIu64 " bytes", buffer_size);
1841
1842 lxc_conf->console.buffer_size = buffer_size;
1843 return 0;
1844 }
1845
1846 static int set_config_console_buffer_logfile(const char *key, const char *value,
1847 struct lxc_conf *lxc_conf,
1848 void *data)
1849 {
1850 return set_config_path_item(&lxc_conf->console.buffer_log_file, value);
1851 }
1852
1853 int append_unexp_config_line(const char *line, struct lxc_conf *conf)
1854 {
1855 size_t len = conf->unexpanded_len, linelen = strlen(line);
1856
1857 update_hwaddr(line);
1858
1859 while (conf->unexpanded_alloced <= len + linelen + 2) {
1860 char *tmp = realloc(conf->unexpanded_config,
1861 conf->unexpanded_alloced + 1024);
1862 if (!tmp)
1863 return -1;
1864
1865 if (!conf->unexpanded_config)
1866 *tmp = '\0';
1867 conf->unexpanded_config = tmp;
1868 conf->unexpanded_alloced += 1024;
1869 }
1870 strcat(conf->unexpanded_config, line);
1871 conf->unexpanded_len += linelen;
1872 if (line[linelen - 1] != '\n') {
1873 strcat(conf->unexpanded_config, "\n");
1874 conf->unexpanded_len++;
1875 }
1876
1877 return 0;
1878 }
1879
1880 static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
1881 {
1882 struct dirent *direntp;
1883 DIR *dir;
1884 char path[MAXPATHLEN];
1885 int len;
1886 int ret = -1;
1887
1888 dir = opendir(dirp);
1889 if (!dir)
1890 return -1;
1891
1892 while ((direntp = readdir(dir))) {
1893 const char *fnam;
1894 if (!direntp)
1895 break;
1896
1897 fnam = direntp->d_name;
1898 if (!strcmp(fnam, "."))
1899 continue;
1900
1901 if (!strcmp(fnam, ".."))
1902 continue;
1903
1904 len = strlen(fnam);
1905 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
1906 continue;
1907
1908 len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam);
1909 if (len < 0 || len >= MAXPATHLEN) {
1910 ret = -1;
1911 goto out;
1912 }
1913
1914 ret = lxc_config_read(path, lxc_conf, true);
1915 if (ret < 0)
1916 goto out;
1917 }
1918 ret = 0;
1919
1920 out:
1921 closedir(dir);
1922
1923 return ret;
1924 }
1925
1926 static int set_config_includefiles(const char *key, const char *value,
1927 struct lxc_conf *lxc_conf, void *data)
1928 {
1929 if (lxc_config_value_empty(value)) {
1930 clr_config_includefiles(key, lxc_conf, NULL);
1931 return 0;
1932 }
1933
1934 if (is_dir(value))
1935 return do_includedir(value, lxc_conf);
1936
1937 return lxc_config_read(value, lxc_conf, true);
1938 }
1939
1940 static int set_config_rootfs_path(const char *key, const char *value,
1941 struct lxc_conf *lxc_conf, void *data)
1942 {
1943 return set_config_path_item(&lxc_conf->rootfs.path, value);
1944 }
1945
1946 static int set_config_rootfs_mount(const char *key, const char *value,
1947 struct lxc_conf *lxc_conf, void *data)
1948 {
1949 return set_config_path_item(&lxc_conf->rootfs.mount, value);
1950 }
1951
1952 static int set_config_rootfs_options(const char *key, const char *value,
1953 struct lxc_conf *lxc_conf, void *data)
1954 {
1955 return set_config_string_item(&lxc_conf->rootfs.options, value);
1956 }
1957
1958 static int set_config_rootfs_backend(const char *key, const char *value,
1959 struct lxc_conf *lxc_conf, void *data)
1960 {
1961 return 0;
1962 }
1963
1964 static int set_config_uts_name(const char *key, const char *value,
1965 struct lxc_conf *lxc_conf, void *data)
1966 {
1967 struct utsname *utsname;
1968
1969 if (lxc_config_value_empty(value)) {
1970 clr_config_uts_name(key, lxc_conf, NULL);
1971 return 0;
1972 }
1973
1974 utsname = malloc(sizeof(*utsname));
1975 if (!utsname)
1976 return -1;
1977
1978 if (strlen(value) >= sizeof(utsname->nodename)) {
1979 free(utsname);
1980 return -1;
1981 }
1982
1983 strcpy(utsname->nodename, value);
1984 free(lxc_conf->utsname);
1985 lxc_conf->utsname = utsname;
1986
1987 return 0;
1988 }
1989
1990 static int set_config_namespace(const char *key, const char *value,
1991 struct lxc_conf *lxc_conf, void *data)
1992 {
1993 int ns_idx;
1994 const char *namespace;
1995
1996 if (lxc_config_value_empty(value))
1997 return clr_config_namespace(key, lxc_conf, data);
1998
1999 namespace = key + sizeof("lxc.namespace.") - 1;
2000 ns_idx = lxc_namespace_2_ns_idx(namespace);
2001 if (ns_idx < 0)
2002 return ns_idx;
2003
2004 return set_config_string_item(&lxc_conf->inherit_ns[ns_idx], value);
2005 }
2006
2007 struct parse_line_conf {
2008 struct lxc_conf *conf;
2009 bool from_include;
2010 };
2011
2012 static int parse_line(char *buffer, void *data)
2013 {
2014 char *dot, *key, *line, *linep, *value;
2015 bool empty_line;
2016 struct lxc_config_t *config;
2017 int ret = 0;
2018 char *dup = buffer;
2019 struct parse_line_conf *plc = data;
2020
2021 /* If there are newlines in the config file we should keep them. */
2022 empty_line = lxc_is_line_empty(dup);
2023 if (empty_line)
2024 dup = "\n";
2025
2026 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2027 * modified the original string on the stack by replacing '=' by '\0'
2028 * below.
2029 */
2030 linep = line = strdup(dup);
2031 if (!line)
2032 return -1;
2033
2034 if (!plc->from_include) {
2035 ret = append_unexp_config_line(line, plc->conf);
2036 if (ret < 0)
2037 goto on_error;
2038 }
2039
2040 if (empty_line)
2041 return 0;
2042
2043 line += lxc_char_left_gc(line, strlen(line));
2044
2045 /* ignore comments */
2046 if (line[0] == '#')
2047 goto on_error;
2048
2049 /* martian option - don't add it to the config itself */
2050 if (strncmp(line, "lxc.", 4))
2051 goto on_error;
2052
2053 ret = -1;
2054
2055 dot = strchr(line, '=');
2056 if (!dot) {
2057 ERROR("Invalid configuration line: %s", line);
2058 goto on_error;
2059 }
2060
2061 *dot = '\0';
2062 value = dot + 1;
2063
2064 key = line;
2065 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2066
2067 value += lxc_char_left_gc(value, strlen(value));
2068 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2069
2070 if (*value == '\'' || *value == '\"') {
2071 size_t len;
2072
2073 len = strlen(value);
2074 if (len > 1 && value[len - 1] == *value) {
2075 value[len - 1] = '\0';
2076 value++;
2077 }
2078 }
2079
2080 config = lxc_get_config(key);
2081 if (!config) {
2082 ERROR("Unknown configuration key \"%s\"", key);
2083 goto on_error;
2084 }
2085
2086 /* [START]: REMOVE IN LXC 3.0 */
2087 if (config->is_legacy_key && !plc->conf->contains_legacy_key) {
2088 plc->conf->contains_legacy_key = true;
2089 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2090 /* Warn the user once loud and clear that there is at
2091 * least one legacy configuration item in the
2092 * configuration file and then an update is required.
2093 */
2094 fprintf(stderr, "The configuration file contains "
2095 "legacy configuration keys.\nPlease "
2096 "update your configuration file!\n");
2097 }
2098 }
2099 /* [END]: REMOVE IN LXC 3.0 */
2100
2101 ret = config->set(key, value, plc->conf, NULL);
2102
2103 on_error:
2104 free(linep);
2105 return ret;
2106 }
2107
2108 static int lxc_config_readline(char *buffer, struct lxc_conf *conf)
2109 {
2110 struct parse_line_conf c;
2111
2112 c.conf = conf;
2113 c.from_include = false;
2114
2115 return parse_line(buffer, &c);
2116 }
2117
2118 int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
2119 {
2120 int ret;
2121 struct parse_line_conf c;
2122
2123 c.conf = conf;
2124 c.from_include = from_include;
2125
2126 ret = access(file, R_OK);
2127 if (ret < 0)
2128 return -1;
2129
2130 /* Catch only the top level config file name in the structure. */
2131 if (!conf->rcfile)
2132 conf->rcfile = strdup(file);
2133
2134 return lxc_file_for_each_line(file, parse_line, &c);
2135 }
2136
2137 int lxc_config_define_add(struct lxc_list *defines, char *arg)
2138 {
2139 struct lxc_list *dent;
2140
2141 dent = malloc(sizeof(struct lxc_list));
2142 if (!dent)
2143 return -1;
2144
2145 dent->elem = arg;
2146 lxc_list_add_tail(defines, dent);
2147 return 0;
2148 }
2149
2150 int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf)
2151 {
2152 struct lxc_list *it, *next;
2153 int ret = 0;
2154
2155 lxc_list_for_each(it, defines) {
2156 ret = lxc_config_readline(it->elem, conf);
2157 if (ret)
2158 break;
2159 }
2160
2161 lxc_list_for_each_safe(it, defines, next) {
2162 lxc_list_del(it);
2163 free(it);
2164 }
2165
2166 return ret;
2167 }
2168
2169 signed long lxc_config_parse_arch(const char *arch)
2170 {
2171 #if HAVE_SYS_PERSONALITY_H
2172 size_t i;
2173 struct per_name {
2174 char *name;
2175 unsigned long per;
2176 } pername[] = {
2177 { "x86", PER_LINUX32 },
2178 { "linux32", PER_LINUX32 },
2179 { "i386", PER_LINUX32 },
2180 { "i486", PER_LINUX32 },
2181 { "i586", PER_LINUX32 },
2182 { "i686", PER_LINUX32 },
2183 { "athlon", PER_LINUX32 },
2184 { "mips", PER_LINUX32 },
2185 { "mipsel", PER_LINUX32 },
2186 { "ppc", PER_LINUX32 },
2187 { "arm", PER_LINUX32 },
2188 { "armv7l", PER_LINUX32 },
2189 { "armhf", PER_LINUX32 },
2190 { "armel", PER_LINUX32 },
2191 { "powerpc", PER_LINUX32 },
2192 { "linux64", PER_LINUX },
2193 { "x86_64", PER_LINUX },
2194 { "amd64", PER_LINUX },
2195 { "mips64", PER_LINUX },
2196 { "mips64el", PER_LINUX },
2197 { "ppc64", PER_LINUX },
2198 { "ppc64le", PER_LINUX },
2199 { "ppc64el", PER_LINUX },
2200 { "powerpc64", PER_LINUX },
2201 { "s390x", PER_LINUX },
2202 { "aarch64", PER_LINUX },
2203 { "arm64", PER_LINUX },
2204 };
2205 size_t len = sizeof(pername) / sizeof(pername[0]);
2206
2207 for (i = 0; i < len; i++) {
2208 if (!strcmp(pername[i].name, arch))
2209 return pername[i].per;
2210 }
2211 #endif
2212
2213 return -1;
2214 }
2215
2216 int lxc_fill_elevated_privileges(char *flaglist, int *flags)
2217 {
2218 char *token, *saveptr = NULL;
2219 int i, aflag;
2220 struct {
2221 const char *token;
2222 int flag;
2223 } all_privs[] = {
2224 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
2225 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
2226 { "LSM", LXC_ATTACH_LSM_EXEC },
2227 { NULL, 0 }
2228 };
2229
2230 if (!flaglist) {
2231 /* For the sake of backward compatibility, drop all privileges
2232 * if none is specified.
2233 */
2234 for (i = 0; all_privs[i].token; i++)
2235 *flags |= all_privs[i].flag;
2236
2237 return 0;
2238 }
2239
2240 token = strtok_r(flaglist, "|", &saveptr);
2241 while (token) {
2242 aflag = -1;
2243 for (i = 0; all_privs[i].token; i++)
2244 if (!strcmp(all_privs[i].token, token))
2245 aflag = all_privs[i].flag;
2246 if (aflag < 0)
2247 return -1;
2248
2249 *flags |= aflag;
2250
2251 token = strtok_r(NULL, "|", &saveptr);
2252 }
2253
2254 return 0;
2255 }
2256
2257 /* Write out a configuration file. */
2258 void write_config(FILE *fout, struct lxc_conf *c)
2259 {
2260 int ret;
2261 size_t len = c->unexpanded_len;
2262
2263 if (!len)
2264 return;
2265
2266 ret = fwrite(c->unexpanded_config, 1, len, fout);
2267 if (ret != len)
2268 SYSERROR("Failed to write configuration file");
2269 }
2270
2271 bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
2272 const char *v)
2273 {
2274 int ret;
2275 size_t len;
2276 char *tmp;
2277
2278 len = strlen(key) + strlen(v) + 4;
2279 tmp = alloca(len);
2280
2281 if (lxc_config_value_empty(v))
2282 ret = snprintf(tmp, len, "%s =", key);
2283 else
2284 ret = snprintf(tmp, len, "%s = %s", key, v);
2285 if (ret < 0 || ret >= len)
2286 return false;
2287
2288 /* Save the line verbatim into unexpanded_conf */
2289 if (append_unexp_config_line(tmp, conf))
2290 return false;
2291
2292 return true;
2293 }
2294
2295 void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
2296 bool rm_subkeys)
2297 {
2298 char *lend;
2299 char *lstart = conf->unexpanded_config;
2300
2301 if (!conf->unexpanded_config)
2302 return;
2303
2304 while (*lstart) {
2305 lend = strchr(lstart, '\n');
2306 char v;
2307 if (!lend)
2308 lend = lstart + strlen(lstart);
2309 else
2310 lend++;
2311 if (strncmp(lstart, key, strlen(key)) != 0) {
2312 lstart = lend;
2313 continue;
2314 }
2315 if (!rm_subkeys) {
2316 v = lstart[strlen(key)];
2317 if (!isspace(v) && v != '=') {
2318 lstart = lend;
2319 continue;
2320 }
2321 }
2322 conf->unexpanded_len -= (lend - lstart);
2323 if (*lend == '\0') {
2324 *lstart = '\0';
2325 return;
2326 }
2327 memmove(lstart, lend, strlen(lend) + 1);
2328 }
2329 }
2330
2331 bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
2332 const char *newpath, const char *oldname,
2333 const char *newname, const char *ovldir)
2334 {
2335 int ret;
2336 char *lend, *newdir, *olddir, *p, *q;
2337 size_t newdirlen, olddirlen;
2338 char *lstart = conf->unexpanded_config;
2339 const char *key = "lxc.mount.entry";
2340
2341 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
2342 olddir = alloca(olddirlen + 1);
2343 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
2344 oldname);
2345 if (ret < 0 || ret >= olddirlen + 1)
2346 return false;
2347
2348 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
2349 newdir = alloca(newdirlen + 1);
2350 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
2351 newname);
2352 if (ret < 0 || ret >= newdirlen + 1)
2353 return false;
2354
2355 if (!conf->unexpanded_config)
2356 return true;
2357
2358 while (*lstart) {
2359 lend = strchr(lstart, '\n');
2360 if (!lend)
2361 lend = lstart + strlen(lstart);
2362 else
2363 lend++;
2364
2365 if (strncmp(lstart, key, strlen(key)) != 0)
2366 goto next;
2367
2368 p = strchr(lstart + strlen(key), '=');
2369 if (!p)
2370 goto next;
2371 p++;
2372
2373 while (isblank(*p))
2374 p++;
2375
2376 if (p >= lend)
2377 goto next;
2378
2379 /* Whenever an lxc.mount.entry entry is found in a line we check
2380 * if the substring " overlay" or the substring " aufs" is
2381 * present before doing any further work. We check for "
2382 * overlay" and " aufs" since both substrings need to have at
2383 * least one space before them in a valid overlay
2384 * lxc.mount.entry (/A B overlay). When the space before is
2385 * missing it is very likely that these substrings are part of a
2386 * path or something else. (Checking q >= lend ensures that we
2387 * only count matches in the current line.) */
2388 if ((!(q = strstr(p, " overlay")) || q >= lend) &&
2389 (!(q = strstr(p, " aufs")) || q >= lend))
2390 goto next;
2391
2392 if (!(q = strstr(p, olddir)) || (q >= lend))
2393 goto next;
2394
2395 /* replace the olddir with newdir */
2396 if (olddirlen >= newdirlen) {
2397 size_t diff = olddirlen - newdirlen;
2398 memcpy(q, newdir, newdirlen);
2399 if (olddirlen != newdirlen) {
2400 memmove(q + newdirlen, q + newdirlen + diff,
2401 strlen(q) - newdirlen - diff + 1);
2402 lend -= diff;
2403 conf->unexpanded_len -= diff;
2404 }
2405 } else {
2406 char *new;
2407 size_t diff = newdirlen - olddirlen;
2408 size_t oldlen = conf->unexpanded_len;
2409 size_t newlen = oldlen + diff;
2410 size_t poffset = q - conf->unexpanded_config;
2411
2412 new = realloc(conf->unexpanded_config, newlen + 1);
2413 if (!new)
2414 return false;
2415
2416 conf->unexpanded_len = newlen;
2417 conf->unexpanded_alloced = newlen + 1;
2418 new[newlen - 1] = '\0';
2419 lend = new + (lend - conf->unexpanded_config);
2420 /* Move over the remainder to make room for the newdir.
2421 */
2422 memmove(new + poffset + newdirlen,
2423 new + poffset + olddirlen,
2424 oldlen - poffset - olddirlen + 1);
2425 conf->unexpanded_config = new;
2426 memcpy(new + poffset, newdir, newdirlen);
2427 lend += diff;
2428 }
2429 next:
2430 lstart = lend;
2431 }
2432
2433 return true;
2434 }
2435
2436 bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
2437 const char *newpath, const char *oldname,
2438 const char *newname)
2439 {
2440 int ret;
2441 char *lend, *newdir, *olddir, *p;
2442 char *lstart = conf->unexpanded_config;
2443 size_t newdirlen, olddirlen;
2444 const char *key = "lxc.hook";
2445
2446 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
2447 olddir = alloca(olddirlen + 1);
2448 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
2449 if (ret < 0 || ret >= olddirlen + 1)
2450 return false;
2451
2452 newdirlen = strlen(newpath) + strlen(newname) + 1;
2453 newdir = alloca(newdirlen + 1);
2454 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
2455 if (ret < 0 || ret >= newdirlen + 1)
2456 return false;
2457
2458 if (!conf->unexpanded_config)
2459 return true;
2460
2461 while (*lstart) {
2462 lend = strchr(lstart, '\n');
2463 if (!lend)
2464 lend = lstart + strlen(lstart);
2465 else
2466 lend++;
2467
2468 if (strncmp(lstart, key, strlen(key)) != 0)
2469 goto next;
2470
2471 p = strchr(lstart + strlen(key), '=');
2472 if (!p)
2473 goto next;
2474 p++;
2475
2476 while (isblank(*p))
2477 p++;
2478
2479 if (p >= lend)
2480 goto next;
2481
2482 if (strncmp(p, olddir, strlen(olddir)) != 0)
2483 goto next;
2484
2485 /* replace the olddir with newdir */
2486 if (olddirlen >= newdirlen) {
2487 size_t diff = olddirlen - newdirlen;
2488 memcpy(p, newdir, newdirlen);
2489 if (olddirlen != newdirlen) {
2490 memmove(p + newdirlen, p + newdirlen + diff,
2491 strlen(p) - newdirlen - diff + 1);
2492 lend -= diff;
2493 conf->unexpanded_len -= diff;
2494 }
2495 } else {
2496 char *new;
2497 size_t diff = newdirlen - olddirlen;
2498 size_t oldlen = conf->unexpanded_len;
2499 size_t newlen = oldlen + diff;
2500 size_t poffset = p - conf->unexpanded_config;
2501
2502 new = realloc(conf->unexpanded_config, newlen + 1);
2503 if (!new)
2504 return false;
2505
2506 conf->unexpanded_len = newlen;
2507 conf->unexpanded_alloced = newlen + 1;
2508 new[newlen - 1] = '\0';
2509 lend = new + (lend - conf->unexpanded_config);
2510 /* Move over the remainder to make room for the newdir.
2511 */
2512 memmove(new + poffset + newdirlen,
2513 new + poffset + olddirlen,
2514 oldlen - poffset - olddirlen + 1);
2515 conf->unexpanded_config = new;
2516 memcpy(new + poffset, newdir, newdirlen);
2517 lend += diff;
2518 }
2519 next:
2520 lstart = lend;
2521 }
2522
2523 return true;
2524 }
2525
2526 #define DO(cmd) \
2527 { \
2528 if (!(cmd)) { \
2529 ERROR("Error writing to new config"); \
2530 return false; \
2531 } \
2532 }
2533
2534 /* This is called only from clone. We wish to update all hwaddrs in the
2535 * unexpanded config file. We can't/don't want to update any which come from
2536 * lxc.includes (there shouldn't be any).
2537 * We can't just walk the c->lxc-conf->network list because that includes netifs
2538 * from the include files. So we update the ones which we find in the unexp
2539 * config file, then find the original macaddr in the conf->network, and update
2540 * that to the same value.
2541 */
2542 bool network_new_hwaddrs(struct lxc_conf *conf)
2543 {
2544 char *lend, *p, *p2;
2545 struct lxc_list *it;
2546 char *lstart = conf->unexpanded_config;
2547
2548 if (!conf->unexpanded_config)
2549 return true;
2550
2551 while (*lstart) {
2552 char newhwaddr[18], oldhwaddr[17];
2553
2554 lend = strchr(lstart, '\n');
2555 if (!lend)
2556 lend = lstart + strlen(lstart);
2557 else
2558 lend++;
2559
2560 if (!lxc_config_net_hwaddr(lstart)) {
2561 lstart = lend;
2562 continue;
2563 }
2564
2565 p = strchr(lstart, '=');
2566 if (!p) {
2567 lstart = lend;
2568 continue;
2569 }
2570
2571 p++;
2572 while (isblank(*p))
2573 p++;
2574 if (!*p)
2575 return true;
2576
2577 p2 = p;
2578 while (*p2 && !isblank(*p2) && *p2 != '\n')
2579 p2++;
2580
2581 if ((p2 - p) != 17) {
2582 WARN("Bad hwaddr entry");
2583 lstart = lend;
2584 continue;
2585 }
2586
2587 memcpy(oldhwaddr, p, 17);
2588
2589 if (!new_hwaddr(newhwaddr))
2590 return false;
2591
2592 memcpy(p, newhwaddr, 17);
2593 lxc_list_for_each(it, &conf->network) {
2594 struct lxc_netdev *n = it->elem;
2595
2596 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
2597 memcpy(n->hwaddr, newhwaddr, 17);
2598 }
2599
2600 lstart = lend;
2601 }
2602
2603 return true;
2604 }
2605
2606 static int set_config_ephemeral(const char *key, const char *value,
2607 struct lxc_conf *lxc_conf, void *data)
2608 {
2609 if (lxc_config_value_empty(value)) {
2610 lxc_conf->ephemeral = 0;
2611 return 0;
2612 }
2613
2614 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
2615 return -1;
2616
2617 if (lxc_conf->ephemeral > 1)
2618 return -1;
2619
2620 return 0;
2621 }
2622
2623 static int set_config_log_syslog(const char *key, const char *value,
2624 struct lxc_conf *lxc_conf, void *data)
2625 {
2626 int facility;
2627
2628 if (lxc_conf->syslog) {
2629 free(lxc_conf->syslog);
2630 lxc_conf->syslog = NULL;
2631 }
2632
2633 if (lxc_config_value_empty(value))
2634 return 0;
2635
2636 facility = lxc_syslog_priority_to_int(value);
2637 if (facility == -EINVAL)
2638 return -1;
2639
2640 lxc_log_syslog(facility);
2641 return set_config_string_item(&lxc_conf->syslog, value);
2642 }
2643
2644 static int set_config_no_new_privs(const char *key, const char *value,
2645 struct lxc_conf *lxc_conf, void *data)
2646 {
2647 unsigned int v;
2648
2649 if (lxc_config_value_empty(value)) {
2650 lxc_conf->no_new_privs = false;
2651 return 0;
2652 }
2653
2654 if (lxc_safe_uint(value, &v) < 0)
2655 return -1;
2656
2657 if (v > 1)
2658 return -1;
2659
2660 lxc_conf->no_new_privs = v ? true : false;
2661
2662 return 0;
2663 }
2664
2665 static int set_config_noop(const char *key, const char *value,
2666 struct lxc_conf *lxc_conf, void *data)
2667 {
2668 return 0;
2669 }
2670
2671 /* Callbacks to get configuration items. */
2672 static int get_config_personality(const char *key, char *retv, int inlen,
2673 struct lxc_conf *c, void *data)
2674 {
2675 int fulllen = 0;
2676
2677 if (!retv)
2678 inlen = 0;
2679 else
2680 memset(retv, 0, inlen);
2681
2682 #if HAVE_SYS_PERSONALITY_H
2683 int len = 0;
2684
2685 switch (c->personality) {
2686 case PER_LINUX32:
2687 strprint(retv, inlen, "i686");
2688 break;
2689 case PER_LINUX:
2690 strprint(retv, inlen, "x86_64");
2691 break;
2692 default:
2693 break;
2694 }
2695 #endif
2696
2697 return fulllen;
2698 }
2699
2700 static int get_config_pty_max(const char *key, char *retv, int inlen,
2701 struct lxc_conf *c, void *data)
2702 {
2703 return lxc_get_conf_int(c, retv, inlen, c->pts);
2704 }
2705
2706 static int get_config_tty_max(const char *key, char *retv, int inlen,
2707 struct lxc_conf *c, void *data)
2708 {
2709 return lxc_get_conf_int(c, retv, inlen, c->tty);
2710 }
2711
2712 static int get_config_tty_dir(const char *key, char *retv, int inlen,
2713 struct lxc_conf *c, void *data)
2714 {
2715 return lxc_get_conf_str(retv, inlen, c->ttydir);
2716 }
2717
2718 static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
2719 struct lxc_conf *c, void *data)
2720 {
2721 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
2722 }
2723
2724 static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
2725 int inlen, struct lxc_conf *c,
2726 void *data)
2727 {
2728 return lxc_get_conf_int(c, retv, inlen,
2729 c->lsm_aa_allow_incomplete);
2730 }
2731
2732 static int get_config_selinux_context(const char *key, char *retv, int inlen,
2733 struct lxc_conf *c, void *data)
2734 {
2735 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
2736 }
2737
2738 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2739 * just the value(s) will be printed. Since there still could be more than one,
2740 * it is newline-separated.
2741 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2742 * have newlines?)
2743 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2744 * 'lxc.cgroup.subsystem.key = value' format.
2745 */
2746 static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
2747 struct lxc_conf *c, void *data)
2748 {
2749 struct lxc_list *it;
2750 int len;
2751 int fulllen = 0;
2752 bool get_all = false;
2753
2754 if (!retv)
2755 inlen = 0;
2756 else
2757 memset(retv, 0, inlen);
2758
2759 if (!strcmp(key, "lxc.cgroup"))
2760 get_all = true;
2761 else if (!strncmp(key, "lxc.cgroup.", 11))
2762 key += 11;
2763 else
2764 return -1;
2765
2766 lxc_list_for_each(it, &c->cgroup) {
2767 struct lxc_cgroup *cg = it->elem;
2768
2769 if (get_all) {
2770 strprint(retv, inlen, "lxc.cgroup.%s = %s\n",
2771 cg->subsystem, cg->value);
2772 } else if (!strcmp(cg->subsystem, key)) {
2773 strprint(retv, inlen, "%s\n", cg->value);
2774 }
2775 }
2776
2777 return fulllen;
2778 }
2779
2780 static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
2781 struct lxc_conf *lxc_conf, void *data)
2782 {
2783 int len;
2784 int fulllen = 0;
2785
2786 if (!retv)
2787 inlen = 0;
2788 else
2789 memset(retv, 0, inlen);
2790
2791 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
2792
2793 return fulllen;
2794 }
2795
2796 static int get_config_idmaps(const char *key, char *retv, int inlen,
2797 struct lxc_conf *c, void *data)
2798 {
2799 struct lxc_list *it;
2800 int len, listlen, ret;
2801 int fulllen = 0;
2802 /* "u 1000 1000000 65536"
2803 *
2804 * let's render this as
2805 *
2806 * sizeof(char)
2807 * +
2808 * sizeof(" ")
2809 * +
2810 * sizeof(uint64_t)
2811 * +
2812 * sizeof(" ")
2813 * +
2814 * sizeof(uint64_t)
2815 * +
2816 * sizeof(" ")
2817 * +
2818 * sizeof(uint64_t)
2819 * +
2820 * \0
2821 */
2822 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2823 char buf[__LXC_IDMAP_STR_BUF];
2824
2825 if (!retv)
2826 inlen = 0;
2827 else
2828 memset(retv, 0, inlen);
2829
2830 listlen = lxc_list_len(&c->id_map);
2831 lxc_list_for_each(it, &c->id_map)
2832 {
2833 struct id_map *map = it->elem;
2834 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
2835 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
2836 map->nsid, map->hostid, map->range);
2837 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
2838 return -1;
2839
2840 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
2841 }
2842 return fulllen;
2843 }
2844
2845 static int get_config_log_level(const char *key, char *retv, int inlen,
2846 struct lxc_conf *c, void *data)
2847 {
2848 const char *v;
2849 v = lxc_log_priority_to_string(c->loglevel);
2850 return lxc_get_conf_str(retv, inlen, v);
2851 }
2852
2853 static int get_config_log_file(const char *key, char *retv, int inlen,
2854 struct lxc_conf *c, void *data)
2855 {
2856 return lxc_get_conf_str(retv, inlen, c->logfile);
2857 }
2858
2859 static int get_config_mount_fstab(const char *key, char *retv, int inlen,
2860 struct lxc_conf *c, void *data)
2861 {
2862 return lxc_get_conf_str(retv, inlen, c->fstab);
2863 }
2864
2865 static int get_config_mount_auto(const char *key, char *retv, int inlen,
2866 struct lxc_conf *c, void *data)
2867 {
2868 int len, fulllen = 0;
2869 const char *sep = "";
2870
2871 if (!retv)
2872 inlen = 0;
2873 else
2874 memset(retv, 0, inlen);
2875
2876 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
2877 return 0;
2878
2879 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
2880 case LXC_AUTO_PROC_MIXED:
2881 strprint(retv, inlen, "%sproc:mixed", sep);
2882 sep = " ";
2883 break;
2884 case LXC_AUTO_PROC_RW:
2885 strprint(retv, inlen, "%sproc:rw", sep);
2886 sep = " ";
2887 break;
2888 default:
2889 break;
2890 }
2891
2892 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
2893 case LXC_AUTO_SYS_RO:
2894 strprint(retv, inlen, "%ssys:ro", sep);
2895 sep = " ";
2896 break;
2897 case LXC_AUTO_SYS_RW:
2898 strprint(retv, inlen, "%ssys:rw", sep);
2899 sep = " ";
2900 break;
2901 case LXC_AUTO_SYS_MIXED:
2902 strprint(retv, inlen, "%ssys:mixed", sep);
2903 sep = " ";
2904 break;
2905 default:
2906 break;
2907 }
2908
2909 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
2910 case LXC_AUTO_CGROUP_NOSPEC:
2911 strprint(retv, inlen, "%scgroup", sep);
2912 sep = " ";
2913 break;
2914 case LXC_AUTO_CGROUP_MIXED:
2915 strprint(retv, inlen, "%scgroup:mixed", sep);
2916 sep = " ";
2917 break;
2918 case LXC_AUTO_CGROUP_RO:
2919 strprint(retv, inlen, "%scgroup:ro", sep);
2920 sep = " ";
2921 break;
2922 case LXC_AUTO_CGROUP_RW:
2923 strprint(retv, inlen, "%scgroup:rw", sep);
2924 sep = " ";
2925 break;
2926 case LXC_AUTO_CGROUP_FULL_NOSPEC:
2927 strprint(retv, inlen, "%scgroup-full", sep);
2928 sep = " ";
2929 break;
2930 case LXC_AUTO_CGROUP_FULL_MIXED:
2931 strprint(retv, inlen, "%scgroup-full:mixed", sep);
2932 sep = " ";
2933 break;
2934 case LXC_AUTO_CGROUP_FULL_RO:
2935 strprint(retv, inlen, "%scgroup-full:ro", sep);
2936 sep = " ";
2937 break;
2938 case LXC_AUTO_CGROUP_FULL_RW:
2939 strprint(retv, inlen, "%scgroup-full:rw", sep);
2940 sep = " ";
2941 break;
2942 default:
2943 break;
2944 }
2945
2946 return fulllen;
2947 }
2948
2949 static int get_config_mount(const char *key, char *retv, int inlen,
2950 struct lxc_conf *c, void *data)
2951 {
2952 int len, fulllen = 0;
2953 struct lxc_list *it;
2954
2955 if (!retv)
2956 inlen = 0;
2957 else
2958 memset(retv, 0, inlen);
2959
2960 lxc_list_for_each(it, &c->mount_list) {
2961 strprint(retv, inlen, "%s\n", (char *)it->elem);
2962 }
2963
2964 return fulllen;
2965 }
2966
2967 static int get_config_rootfs_path(const char *key, char *retv, int inlen,
2968 struct lxc_conf *c, void *data)
2969 {
2970 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
2971 }
2972
2973 static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
2974 struct lxc_conf *c, void *data)
2975 {
2976 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
2977 }
2978
2979 static int get_config_rootfs_options(const char *key, char *retv, int inlen,
2980 struct lxc_conf *c, void *data)
2981 {
2982 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
2983 }
2984
2985 static int get_config_rootfs_backend(const char *key, char *retv, int inlen,
2986 struct lxc_conf *c, void *data)
2987 {
2988 return 0;
2989 }
2990
2991 static int get_config_uts_name(const char *key, char *retv, int inlen,
2992 struct lxc_conf *c, void *data)
2993 {
2994 return lxc_get_conf_str(
2995 retv, inlen,
2996 c->utsname ? c->utsname->nodename : NULL);
2997 }
2998
2999 static int get_config_hooks(const char *key, char *retv, int inlen,
3000 struct lxc_conf *c, void *data)
3001 {
3002 char *subkey;
3003 int len, fulllen = 0, found = -1;
3004 struct lxc_list *it;
3005 int i;
3006
3007 subkey = strchr(key, '.');
3008 if (subkey)
3009 subkey = strchr(subkey + 1, '.');
3010 if (!subkey)
3011 return -1;
3012 subkey++;
3013 if (!*subkey)
3014 return -1;
3015 for (i = 0; i < NUM_LXC_HOOKS; i++) {
3016 if (strcmp(lxchook_names[i], subkey) == 0) {
3017 found = i;
3018 break;
3019 }
3020 }
3021 if (found == -1)
3022 return -1;
3023
3024 if (!retv)
3025 inlen = 0;
3026 else
3027 memset(retv, 0, inlen);
3028
3029 lxc_list_for_each(it, &c->hooks[found]) {
3030 strprint(retv, inlen, "%s\n", (char *)it->elem);
3031 }
3032 return fulllen;
3033 }
3034
3035 static int get_config_net(const char *key, char *retv, int inlen,
3036 struct lxc_conf *c, void *data)
3037 {
3038 int len, fulllen = 0;
3039 struct lxc_list *it;
3040
3041 if (!retv)
3042 inlen = 0;
3043 else
3044 memset(retv, 0, inlen);
3045
3046 lxc_list_for_each(it, &c->network) {
3047 struct lxc_netdev *n = it->elem;
3048 const char *t = lxc_net_type_to_str(n->type);
3049 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
3050 }
3051
3052 return fulllen;
3053 }
3054
3055 static int get_config_cap_drop(const char *key, char *retv, int inlen,
3056 struct lxc_conf *c, void *data)
3057 {
3058 int len, fulllen = 0;
3059 struct lxc_list *it;
3060
3061 if (!retv)
3062 inlen = 0;
3063 else
3064 memset(retv, 0, inlen);
3065
3066 lxc_list_for_each(it, &c->caps) {
3067 strprint(retv, inlen, "%s\n", (char *)it->elem);
3068 }
3069
3070 return fulllen;
3071 }
3072
3073 static int get_config_cap_keep(const char *key, char *retv, int inlen,
3074 struct lxc_conf *c, void *data)
3075 {
3076 int len, fulllen = 0;
3077 struct lxc_list *it;
3078
3079 if (!retv)
3080 inlen = 0;
3081 else
3082 memset(retv, 0, inlen);
3083
3084 lxc_list_for_each(it, &c->keepcaps) {
3085 strprint(retv, inlen, "%s\n", (char *)it->elem);
3086 }
3087
3088 return fulllen;
3089 }
3090
3091 static int get_config_console_path(const char *key, char *retv, int inlen,
3092 struct lxc_conf *c, void *data)
3093 {
3094 return lxc_get_conf_str(retv, inlen, c->console.path);
3095 }
3096
3097 static int get_config_console_logfile(const char *key, char *retv, int inlen,
3098 struct lxc_conf *c, void *data)
3099 {
3100 return lxc_get_conf_str(retv, inlen, c->console.log_path);
3101 }
3102
3103 static int get_config_console_buffer_size(const char *key, char *retv,
3104 int inlen, struct lxc_conf *c,
3105 void *data)
3106 {
3107 return lxc_get_conf_uint64(c, retv, inlen, c->autodev);
3108 }
3109
3110 static int get_config_console_buffer_logfile(const char *key, char *retv,
3111 int inlen, struct lxc_conf *c,
3112 void *data)
3113 {
3114 return lxc_get_conf_str(retv, inlen, c->console.buffer_log_file);
3115 }
3116
3117 static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
3118 struct lxc_conf *c, void *data)
3119 {
3120 return lxc_get_conf_str(retv, inlen, c->seccomp);
3121 }
3122
3123 static int get_config_autodev(const char *key, char *retv, int inlen,
3124 struct lxc_conf *c, void *data)
3125 {
3126 return lxc_get_conf_int(c, retv, inlen, c->autodev);
3127 }
3128
3129 static int get_config_signal_halt(const char *key, char *retv, int inlen,
3130 struct lxc_conf *c, void *data)
3131 {
3132 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
3133 }
3134
3135 static int get_config_signal_reboot(const char *key, char *retv, int inlen,
3136 struct lxc_conf *c, void *data)
3137 {
3138 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3139 }
3140
3141 static int get_config_signal_stop(const char *key, char *retv, int inlen,
3142 struct lxc_conf *c, void *data)
3143 {
3144 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
3145 }
3146
3147 static int get_config_start(const char *key, char *retv, int inlen,
3148 struct lxc_conf *c, void *data)
3149 {
3150 if (strcmp(key + 10, "auto") == 0)
3151 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
3152 else if (strcmp(key + 10, "delay") == 0)
3153 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
3154 else if (strcmp(key + 10, "order") == 0)
3155 return lxc_get_conf_int(c, retv, inlen, c->start_order);
3156
3157 return -1;
3158 }
3159
3160 static int get_config_log_syslog(const char *key, char *retv, int inlen,
3161 struct lxc_conf *c, void *data)
3162 {
3163 return lxc_get_conf_str(retv, inlen, c->syslog);
3164 }
3165
3166 static int get_config_monitor(const char *key, char *retv, int inlen,
3167 struct lxc_conf *c, void *data)
3168 {
3169 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
3170 }
3171
3172 static int get_config_group(const char *key, char *retv, int inlen,
3173 struct lxc_conf *c, void *data)
3174 {
3175 int len, fulllen = 0;
3176 struct lxc_list *it;
3177
3178 if (!retv)
3179 inlen = 0;
3180 else
3181 memset(retv, 0, inlen);
3182
3183 lxc_list_for_each(it, &c->groups) {
3184 strprint(retv, inlen, "%s\n", (char *)it->elem);
3185 }
3186
3187 return fulllen;
3188 }
3189
3190 static int get_config_environment(const char *key, char *retv, int inlen,
3191 struct lxc_conf *c, void *data)
3192 {
3193 int len, fulllen = 0;
3194 struct lxc_list *it;
3195
3196 if (!retv)
3197 inlen = 0;
3198 else
3199 memset(retv, 0, inlen);
3200
3201 lxc_list_for_each(it, &c->environment) {
3202 strprint(retv, inlen, "%s\n", (char *)it->elem);
3203 }
3204
3205 return fulllen;
3206 }
3207
3208 static int get_config_execute_cmd(const char *key, char *retv, int inlen,
3209 struct lxc_conf *c, void *data)
3210 {
3211 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
3212 }
3213
3214 static int get_config_init_cmd(const char *key, char *retv, int inlen,
3215 struct lxc_conf *c, void *data)
3216 {
3217 return lxc_get_conf_str(retv, inlen, c->init_cmd);
3218 }
3219
3220 static int get_config_init_uid(const char *key, char *retv, int inlen,
3221 struct lxc_conf *c, void *data)
3222 {
3223 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
3224 }
3225
3226 static int get_config_init_gid(const char *key, char *retv, int inlen,
3227 struct lxc_conf *c, void *data)
3228 {
3229 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
3230 }
3231
3232 static int get_config_ephemeral(const char *key, char *retv, int inlen,
3233 struct lxc_conf *c, void *data)
3234 {
3235 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
3236 }
3237
3238 static int get_config_no_new_privs(const char *key, char *retv, int inlen,
3239 struct lxc_conf *c, void *data)
3240 {
3241 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
3242 }
3243
3244 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3245 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3246 * printed, in 'lxc.prlimit.resource = value' format.
3247 */
3248 static int get_config_prlimit(const char *key, char *retv, int inlen,
3249 struct lxc_conf *c, void *data)
3250 {
3251 int fulllen = 0, len;
3252 bool get_all = false;
3253 struct lxc_list *it;
3254
3255 if (!retv)
3256 inlen = 0;
3257 else
3258 memset(retv, 0, inlen);
3259
3260 if (!strcmp(key, "lxc.prlimit"))
3261 get_all = true;
3262 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
3263 key += 12;
3264 else
3265 return -1;
3266
3267 lxc_list_for_each(it, &c->limits) {
3268 char buf[LXC_NUMSTRLEN64 * 2 + 2]; /* 2 colon separated 64 bit
3269 integers or the word
3270 'unlimited' */
3271 int partlen;
3272 struct lxc_limit *lim = it->elem;
3273
3274 if (lim->limit.rlim_cur == RLIM_INFINITY) {
3275 memcpy(buf, "unlimited", sizeof("unlimited"));
3276 partlen = sizeof("unlimited") - 1;
3277 } else {
3278 partlen = sprintf(buf, "%" PRIu64,
3279 (uint64_t)lim->limit.rlim_cur);
3280 }
3281 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
3282 if (lim->limit.rlim_max == RLIM_INFINITY)
3283 memcpy(buf + partlen, ":unlimited",
3284 sizeof(":unlimited"));
3285 else
3286 sprintf(buf + partlen, ":%" PRIu64,
3287 (uint64_t)lim->limit.rlim_max);
3288 }
3289
3290 if (get_all) {
3291 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
3292 lim->resource, buf);
3293 } else if (!strcmp(lim->resource, key)) {
3294 strprint(retv, inlen, "%s", buf);
3295 }
3296 }
3297
3298 return fulllen;
3299 }
3300
3301 static int get_config_noop(const char *key, char *retv, int inlen,
3302 struct lxc_conf *c, void *data)
3303 {
3304 return 0;
3305 }
3306
3307 static int get_config_namespace(const char *key, char *retv, int inlen,
3308 struct lxc_conf *c, void *data)
3309 {
3310 int len, ns_idx;
3311 const char *namespace;
3312 int fulllen = 0;
3313
3314 if (!retv)
3315 inlen = 0;
3316 else
3317 memset(retv, 0, inlen);
3318
3319 namespace = key + sizeof("lxc.namespace.") - 1;
3320 ns_idx = lxc_namespace_2_ns_idx(namespace);
3321 if (ns_idx < 0)
3322 return ns_idx;
3323
3324 strprint(retv, inlen, "%s", c->inherit_ns[ns_idx]);
3325
3326 return fulllen;
3327 }
3328
3329 /* Callbacks to clear config items. */
3330 static inline int clr_config_personality(const char *key, struct lxc_conf *c,
3331 void *data)
3332 {
3333 c->personality = -1;
3334 return 0;
3335 }
3336
3337 static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
3338 void *data)
3339 {
3340 c->pts = 0;
3341 return 0;
3342 }
3343
3344 static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
3345 void *data)
3346 {
3347 c->tty = 0;
3348 return 0;
3349 }
3350
3351 static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
3352 void *data)
3353 {
3354 free(c->ttydir);
3355 c->ttydir = NULL;
3356 return 0;
3357 }
3358
3359 static inline int clr_config_apparmor_profile(const char *key,
3360 struct lxc_conf *c, void *data)
3361 {
3362 free(c->lsm_aa_profile);
3363 c->lsm_aa_profile = NULL;
3364 return 0;
3365 }
3366
3367 static inline int clr_config_apparmor_allow_incomplete(const char *key,
3368 struct lxc_conf *c,
3369 void *data)
3370 {
3371 c->lsm_aa_allow_incomplete = 0;
3372 return 0;
3373 }
3374
3375 static inline int clr_config_selinux_context(const char *key,
3376 struct lxc_conf *c, void *data)
3377 {
3378 free(c->lsm_se_context);
3379 c->lsm_se_context = NULL;
3380 return 0;
3381 }
3382
3383 static inline int clr_config_cgroup_controller(const char *key,
3384 struct lxc_conf *c, void *data)
3385 {
3386 return lxc_clear_cgroups(c, key);
3387 }
3388
3389 static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
3390 void *data)
3391 {
3392 if (lxc_conf->cgroup_meta.dir) {
3393 free(lxc_conf->cgroup_meta.dir);
3394 lxc_conf->cgroup_meta.dir = NULL;
3395 }
3396
3397 return 0;
3398 }
3399
3400 static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
3401 void *data)
3402 {
3403 return lxc_clear_idmaps(c);
3404 }
3405
3406 static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
3407 void *data)
3408 {
3409 c->loglevel = LXC_LOG_LEVEL_NOTSET;
3410 return 0;
3411 }
3412
3413 static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
3414 void *data)
3415 {
3416 free(c->logfile);
3417 c->logfile = NULL;
3418 return 0;
3419 }
3420
3421 static inline int clr_config_mount(const char *key, struct lxc_conf *c,
3422 void *data)
3423 {
3424 return lxc_clear_mount_entries(c);
3425 }
3426
3427 static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
3428 void *data)
3429 {
3430 return lxc_clear_automounts(c);
3431 }
3432
3433 static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
3434 void *data)
3435 {
3436 free(c->fstab);
3437 c->fstab = NULL;
3438 return 0;
3439 }
3440
3441 static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
3442 void *data)
3443 {
3444 free(c->rootfs.path);
3445 c->rootfs.path = NULL;
3446 return 0;
3447 }
3448
3449 static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
3450 void *data)
3451 {
3452 free(c->rootfs.mount);
3453 c->rootfs.mount = NULL;
3454 return 0;
3455 }
3456
3457 static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
3458 void *data)
3459 {
3460 free(c->rootfs.options);
3461 c->rootfs.options = NULL;
3462 return 0;
3463 }
3464
3465 static inline int clr_config_rootfs_backend(const char *key, struct lxc_conf *c,
3466 void *data)
3467 {
3468 return 0;
3469 }
3470
3471 static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
3472 void *data)
3473 {
3474 free(c->utsname);
3475 c->utsname = NULL;
3476 return 0;
3477 }
3478
3479 static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
3480 void *data)
3481 {
3482 return lxc_clear_hooks(c, key);
3483 }
3484
3485 static inline int clr_config_net(const char *key, struct lxc_conf *c,
3486 void *data)
3487 {
3488 lxc_free_networks(&c->network);
3489
3490 return 0;
3491 }
3492
3493 static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
3494 void *data)
3495 {
3496 return lxc_clear_config_caps(c);
3497 }
3498
3499 static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
3500 void *data)
3501 {
3502 return lxc_clear_config_keepcaps(c);
3503 }
3504
3505 static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
3506 void *data)
3507 {
3508 free(c->console.path);
3509 c->console.path = NULL;
3510 return 0;
3511 }
3512
3513 static inline int clr_config_console_logfile(const char *key,
3514 struct lxc_conf *c, void *data)
3515 {
3516 free(c->console.log_path);
3517 c->console.log_path = NULL;
3518 return 0;
3519 }
3520
3521 static inline int clr_config_console_buffer_size(const char *key,
3522 struct lxc_conf *c, void *data)
3523 {
3524 c->console.buffer_size = 0;
3525 return 0;
3526 }
3527
3528 static inline int clr_config_console_buffer_logfile(const char *key,
3529 struct lxc_conf *c,
3530 void *data)
3531 {
3532 free(c->console.buffer_log_file);
3533 c->console.buffer_log_file = NULL;
3534 return 0;
3535 }
3536
3537 static inline int clr_config_seccomp_profile(const char *key,
3538 struct lxc_conf *c, void *data)
3539 {
3540 free(c->seccomp);
3541 c->seccomp = NULL;
3542 return 0;
3543 }
3544
3545 static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
3546 void *data)
3547 {
3548 c->autodev = 1;
3549 return 0;
3550 }
3551
3552 static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
3553 void *data)
3554 {
3555 c->haltsignal = 0;
3556 return 0;
3557 }
3558
3559 static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
3560 void *data)
3561 {
3562 c->rebootsignal = 0;
3563 return 0;
3564 }
3565
3566 static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
3567 void *data)
3568 {
3569 c->stopsignal = 0;
3570 return 0;
3571 }
3572
3573 static inline int clr_config_start(const char *key, struct lxc_conf *c,
3574 void *data)
3575 {
3576 if (strcmp(key + 10, "auto") == 0)
3577 c->start_auto = 0;
3578 else if (strcmp(key + 10, "delay") == 0)
3579 c->start_delay = 0;
3580 else if (strcmp(key + 10, "order") == 0)
3581 c->start_order = 0;
3582
3583 return 0;
3584 }
3585
3586 static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
3587 void *data)
3588 {
3589 free(c->syslog);
3590 c->syslog = NULL;
3591 return 0;
3592 }
3593
3594 static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
3595 void *data)
3596 {
3597 c->monitor_unshare = 0;
3598 return 0;
3599 }
3600
3601 static inline int clr_config_group(const char *key, struct lxc_conf *c,
3602 void *data)
3603 {
3604 return lxc_clear_groups(c);
3605 }
3606
3607 static inline int clr_config_environment(const char *key, struct lxc_conf *c,
3608 void *data)
3609 {
3610 return lxc_clear_environment(c);
3611 }
3612
3613 static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
3614 void *data)
3615 {
3616 free(c->execute_cmd);
3617 c->execute_cmd = NULL;
3618 return 0;
3619 }
3620
3621 static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
3622 void *data)
3623 {
3624 free(c->init_cmd);
3625 c->init_cmd = NULL;
3626 return 0;
3627 }
3628
3629 static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
3630 void *data)
3631 {
3632 c->init_uid = 0;
3633 return 0;
3634 }
3635
3636 static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
3637 void *data)
3638 {
3639 c->init_gid = 0;
3640 return 0;
3641 }
3642
3643 static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
3644 void *data)
3645 {
3646 c->ephemeral = 0;
3647 return 0;
3648 }
3649
3650 static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
3651 void *data)
3652 {
3653 c->no_new_privs = false;
3654 return 0;
3655 }
3656
3657 static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
3658 void *data)
3659 {
3660 return lxc_clear_limits(c, key);
3661 }
3662
3663 static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
3664 void *data)
3665 {
3666 lxc_clear_includes(c);
3667 return 0;
3668 }
3669
3670 static inline int clr_config_noop(const char *key, struct lxc_conf *c,
3671 void *data)
3672 {
3673 return 0;
3674 }
3675
3676 static int clr_config_namespace(const char *key, struct lxc_conf *lxc_conf,
3677 void *data)
3678 {
3679 int ns_idx;
3680 const char *namespace;
3681
3682 namespace = key + sizeof("lxc.namespace.") - 1;
3683 ns_idx = lxc_namespace_2_ns_idx(namespace);
3684 if (ns_idx < 0)
3685 return ns_idx;
3686
3687 free(lxc_conf->inherit_ns[ns_idx]);
3688 lxc_conf->inherit_ns[ns_idx] = NULL;
3689
3690 return 0;
3691 }
3692
3693 static int get_config_includefiles(const char *key, char *retv, int inlen,
3694 struct lxc_conf *c, void *data)
3695 {
3696 return -ENOSYS;
3697 }
3698
3699 static struct lxc_config_t *get_network_config_ops(const char *key,
3700 struct lxc_conf *lxc_conf,
3701 ssize_t *idx,
3702 char **deindexed_key)
3703 {
3704 int ret;
3705 unsigned int tmpidx;
3706 size_t numstrlen;
3707 char *copy, *idx_start, *idx_end;
3708 struct lxc_config_t *config = NULL;
3709
3710 /* check that this is a sensible network key */
3711 if (strncmp("lxc.net.", key, 8)) {
3712 ERROR("Invalid network configuration key \"%s\"", key);
3713 return NULL;
3714 }
3715
3716 copy = strdup(key);
3717 if (!copy) {
3718 ERROR("Failed to duplicate string \"%s\"", key);
3719 return NULL;
3720 }
3721
3722 /* lxc.net.<n> */
3723 if (!isdigit(*(key + 8))) {
3724 ERROR("Failed to detect digit in string \"%s\"", key + 8);
3725 goto on_error;
3726 }
3727
3728 /* beginning of index string */
3729 idx_start = (copy + 7);
3730 *idx_start = '\0';
3731
3732 /* end of index string */
3733 idx_end = strchr((copy + 8), '.');
3734 if (idx_end)
3735 *idx_end = '\0';
3736
3737 /* parse current index */
3738 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
3739 if (ret < 0) {
3740 ERROR("Failed to parse usigned integer from string \"%s\": %s",
3741 idx_start + 1, strerror(-ret));
3742 *idx = ret;
3743 goto on_error;
3744 }
3745
3746 /* This, of course is utterly nonsensical on so many levels, but
3747 * better safe than sorry.
3748 * (Checking for INT_MAX here is intentional.)
3749 */
3750 if (tmpidx == INT_MAX) {
3751 SYSERROR("Number of configured networks would overflow the "
3752 "counter");
3753 goto on_error;
3754 }
3755 *idx = tmpidx;
3756
3757 numstrlen = strlen((idx_start + 1));
3758
3759 /* repair configuration key */
3760 *idx_start = '.';
3761
3762 /* lxc.net.<idx>.<subkey> */
3763 if (idx_end) {
3764 *idx_end = '.';
3765
3766 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
3767 copy[strlen(key) - numstrlen + 1] = '\0';
3768
3769 config = lxc_get_config(copy);
3770 if (!config) {
3771 ERROR("Unknown network configuration key \"%s\"", key);
3772 goto on_error;
3773 }
3774 }
3775
3776 if (deindexed_key)
3777 *deindexed_key = copy;
3778
3779 return config;
3780
3781 on_error:
3782 free(copy);
3783 return NULL;
3784 }
3785
3786 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
3787 * found. So we make sure next comes an integer, find the right callback (by
3788 * rewriting the key), and call it.
3789 */
3790 static int set_config_net_nic(const char *key, const char *value,
3791 struct lxc_conf *lxc_conf, void *data)
3792 {
3793 int ret;
3794 const char *idxstring;
3795 struct lxc_config_t *config;
3796 struct lxc_netdev *netdev;
3797 ssize_t idx = -1;
3798 char *deindexed_key = NULL;
3799
3800 idxstring = key + 8;
3801 if (!isdigit(*idxstring))
3802 return -1;
3803
3804 if (lxc_config_value_empty(value))
3805 return clr_config_net_nic(key, lxc_conf, data);
3806
3807 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
3808 if (!config || idx < 0)
3809 return -1;
3810
3811 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
3812 if (!netdev) {
3813 free(deindexed_key);
3814 return -1;
3815 }
3816
3817 ret = config->set(deindexed_key, value, lxc_conf, netdev);
3818 free(deindexed_key);
3819 return ret;
3820 }
3821
3822 static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
3823 void *data)
3824 {
3825 int ret;
3826 const char *idxstring;
3827 struct lxc_config_t *config;
3828 struct lxc_netdev *netdev;
3829 ssize_t idx = -1;
3830 char *deindexed_key = NULL;
3831
3832 idxstring = key + 8;
3833 if (!isdigit(*idxstring))
3834 return -1;
3835
3836 /* The left conjunct is pretty self-explanatory. The right conjunct
3837 * checks whether the two pointers are equal. If they are we know that
3838 * this is not a key that is namespaced any further and so we are
3839 * supposed to clear the whole network.
3840 */
3841 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
3842 unsigned int rmnetdevidx;
3843
3844 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
3845 return -1;
3846
3847 /* Remove network from network list. */
3848 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
3849 return 0;
3850 }
3851
3852 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
3853 if (!config || idx < 0)
3854 return -1;
3855
3856 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
3857 if (!netdev) {
3858 free(deindexed_key);
3859 return -1;
3860 }
3861
3862 ret = config->clr(deindexed_key, lxc_conf, netdev);
3863 free(deindexed_key);
3864 return ret;
3865 }
3866
3867 static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
3868 void *data)
3869 {
3870 struct lxc_netdev *netdev;
3871
3872 if (!data)
3873 return -1;
3874 else
3875 netdev = data;
3876 if (!netdev)
3877 return -1;
3878
3879 netdev->type = -1;
3880
3881 return 0;
3882 }
3883
3884 static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
3885 void *data)
3886 {
3887 struct lxc_netdev *netdev;
3888
3889 if (!data)
3890 return -1;
3891 else
3892 netdev = data;
3893 if (!netdev)
3894 return -1;
3895
3896 netdev->name[0] = '\0';
3897
3898 return 0;
3899 }
3900
3901 static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
3902 void *data)
3903 {
3904 struct lxc_netdev *netdev;
3905
3906 if (!data)
3907 return -1;
3908 else
3909 netdev = data;
3910 if (!netdev)
3911 return -1;
3912
3913 netdev->flags = 0;
3914
3915 return 0;
3916 }
3917
3918 static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
3919 void *data)
3920 {
3921 struct lxc_netdev *netdev;
3922
3923 if (!data)
3924 return -1;
3925 else
3926 netdev = data;
3927 if (!netdev)
3928 return -1;
3929
3930 netdev->link[0] = '\0';
3931
3932 return 0;
3933 }
3934
3935 static int clr_config_net_macvlan_mode(const char *key,
3936 struct lxc_conf *lxc_conf, void *data)
3937 {
3938 struct lxc_netdev *netdev;
3939
3940 if (!data)
3941 return -1;
3942 else
3943 netdev = data;
3944 if (!netdev)
3945 return -1;
3946
3947 if (netdev->type != LXC_NET_MACVLAN)
3948 return 0;
3949
3950 netdev->priv.macvlan_attr.mode = -1;
3951
3952 return 0;
3953 }
3954
3955 static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
3956 void *data)
3957 {
3958 struct lxc_netdev *netdev;
3959
3960 if (!data)
3961 return -1;
3962 else
3963 netdev = data;
3964 if (!netdev)
3965 return -1;
3966
3967 netdev->priv.veth_attr.pair[0] = '\0';
3968
3969 return 0;
3970 }
3971
3972 static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
3973 void *data)
3974 {
3975 struct lxc_netdev *netdev;
3976
3977 if (!data)
3978 return -1;
3979 else
3980 netdev = data;
3981 if (!netdev)
3982 return -1;
3983
3984 free(netdev->upscript);
3985 netdev->upscript = NULL;
3986
3987 return 0;
3988 }
3989
3990 static int clr_config_net_script_down(const char *key,
3991 struct lxc_conf *lxc_conf, void *data)
3992 {
3993 struct lxc_netdev *netdev;
3994
3995 if (!data)
3996 return -1;
3997 else
3998 netdev = data;
3999 if (!netdev)
4000 return -1;
4001
4002 free(netdev->downscript);
4003 netdev->downscript = NULL;
4004
4005 return 0;
4006 }
4007
4008 static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
4009 void *data)
4010 {
4011 struct lxc_netdev *netdev;
4012
4013 if (!data)
4014 return -1;
4015 else
4016 netdev = data;
4017 if (!netdev)
4018 return -1;
4019
4020 free(netdev->hwaddr);
4021 netdev->hwaddr = NULL;
4022
4023 return 0;
4024 }
4025
4026 static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
4027 void *data)
4028 {
4029 struct lxc_netdev *netdev;
4030
4031 if (!data)
4032 return -1;
4033 else
4034 netdev = data;
4035 if (!netdev)
4036 return -1;
4037
4038 free(netdev->mtu);
4039 netdev->mtu = NULL;
4040
4041 return 0;
4042 }
4043
4044 static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
4045 void *data)
4046 {
4047 struct lxc_netdev *netdev;
4048
4049 if (!data)
4050 return -1;
4051 else
4052 netdev = data;
4053 if (!netdev)
4054 return -1;
4055
4056 netdev->priv.vlan_attr.vid = 0;
4057
4058 return 0;
4059 }
4060
4061 static int clr_config_net_ipv4_gateway(const char *key,
4062 struct lxc_conf *lxc_conf, void *data)
4063 {
4064 struct lxc_netdev *netdev;
4065
4066 if (!data)
4067 return -1;
4068 else
4069 netdev = data;
4070 if (!netdev)
4071 return -1;
4072
4073 free(netdev->ipv4_gateway);
4074 netdev->ipv4_gateway = NULL;
4075
4076 return 0;
4077 }
4078
4079 static int clr_config_net_ipv4_address(const char *key,
4080 struct lxc_conf *lxc_conf, void *data)
4081 {
4082 struct lxc_netdev *netdev;
4083 struct lxc_list *cur, *next;
4084
4085 if (!data)
4086 return -1;
4087 else
4088 netdev = data;
4089 if (!netdev)
4090 return -1;
4091
4092 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
4093 lxc_list_del(cur);
4094 free(cur->elem);
4095 free(cur);
4096 }
4097
4098 return 0;
4099 }
4100
4101 static int clr_config_net_ipv6_gateway(const char *key,
4102 struct lxc_conf *lxc_conf, void *data)
4103 {
4104 struct lxc_netdev *netdev;
4105
4106 if (!data)
4107 return -1;
4108 else
4109 netdev = data;
4110 if (!netdev)
4111 return -1;
4112
4113 free(netdev->ipv6_gateway);
4114 netdev->ipv6_gateway = NULL;
4115
4116 return 0;
4117 }
4118
4119 static int clr_config_net_ipv6_address(const char *key,
4120 struct lxc_conf *lxc_conf, void *data)
4121 {
4122 struct lxc_netdev *netdev;
4123 struct lxc_list *cur, *next;
4124
4125 if (!data)
4126 return -1;
4127 else
4128 netdev = data;
4129 if (!netdev)
4130 return -1;
4131
4132 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
4133 lxc_list_del(cur);
4134 free(cur->elem);
4135 free(cur);
4136 }
4137
4138 return 0;
4139 }
4140
4141 static int get_config_net_nic(const char *key, char *retv, int inlen,
4142 struct lxc_conf *c, void *data)
4143 {
4144 int ret;
4145 const char *idxstring;
4146 struct lxc_config_t *config;
4147 struct lxc_netdev *netdev;
4148 ssize_t idx = -1;
4149 char *deindexed_key = NULL;
4150
4151 idxstring = key + 8;
4152 if (!isdigit(*idxstring))
4153 return -1;
4154
4155 config = get_network_config_ops(key, c, &idx, &deindexed_key);
4156 if (!config || idx < 0)
4157 return -1;
4158
4159 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
4160 if (!netdev) {
4161 free(deindexed_key);
4162 return -1;
4163 }
4164
4165 ret = config->get(deindexed_key, retv, inlen, c, netdev);
4166 free(deindexed_key);
4167 return ret;
4168 }
4169
4170 static int get_config_net_type(const char *key, char *retv, int inlen,
4171 struct lxc_conf *c, void *data)
4172 {
4173 int len, fulllen = 0;
4174 struct lxc_netdev *netdev;
4175
4176 if (!retv)
4177 inlen = 0;
4178 else
4179 memset(retv, 0, inlen);
4180
4181 if (!data)
4182 return -1;
4183 else
4184 netdev = data;
4185 if (!netdev)
4186 return -1;
4187
4188 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
4189
4190 return fulllen;
4191 }
4192
4193 static int get_config_net_flags(const char *key, char *retv, int inlen,
4194 struct lxc_conf *c, void *data)
4195 {
4196 int len, fulllen = 0;
4197 struct lxc_netdev *netdev;
4198
4199 if (!retv)
4200 inlen = 0;
4201 else
4202 memset(retv, 0, inlen);
4203
4204 if (!data)
4205 return -1;
4206 else
4207 netdev = data;
4208 if (!netdev)
4209 return -1;
4210
4211 if (netdev->flags & IFF_UP)
4212 strprint(retv, inlen, "up");
4213
4214 return fulllen;
4215 }
4216
4217 static int get_config_net_link(const char *key, char *retv, int inlen,
4218 struct lxc_conf *c, void *data)
4219 {
4220 int len, fulllen = 0;
4221 struct lxc_netdev *netdev;
4222
4223 if (!retv)
4224 inlen = 0;
4225 else
4226 memset(retv, 0, inlen);
4227
4228 if (!data)
4229 return -1;
4230 else
4231 netdev = data;
4232 if (!netdev)
4233 return -1;
4234
4235 if (netdev->link[0] != '\0')
4236 strprint(retv, inlen, "%s", netdev->link);
4237
4238 return fulllen;
4239 }
4240
4241 static int get_config_net_name(const char *key, char *retv, int inlen,
4242 struct lxc_conf *c, void *data)
4243 {
4244 int len, fulllen = 0;
4245 struct lxc_netdev *netdev;
4246
4247 if (!retv)
4248 inlen = 0;
4249 else
4250 memset(retv, 0, inlen);
4251
4252 if (!data)
4253 return -1;
4254 else
4255 netdev = data;
4256 if (!netdev)
4257 return -1;
4258
4259 if (netdev->name[0] != '\0')
4260 strprint(retv, inlen, "%s", netdev->name);
4261
4262 return fulllen;
4263 }
4264
4265 static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
4266 struct lxc_conf *c, void *data)
4267 {
4268 const char *mode;
4269 int len, fulllen = 0;
4270 struct lxc_netdev *netdev;
4271
4272 if (!retv)
4273 inlen = 0;
4274 else
4275 memset(retv, 0, inlen);
4276
4277 if (!data)
4278 return -1;
4279 else
4280 netdev = data;
4281 if (!netdev)
4282 return -1;
4283
4284 if (netdev->type != LXC_NET_MACVLAN)
4285 return 0;
4286
4287 switch (netdev->priv.macvlan_attr.mode) {
4288 case MACVLAN_MODE_PRIVATE:
4289 mode = "private";
4290 break;
4291 case MACVLAN_MODE_VEPA:
4292 mode = "vepa";
4293 break;
4294 case MACVLAN_MODE_BRIDGE:
4295 mode = "bridge";
4296 break;
4297 case MACVLAN_MODE_PASSTHRU:
4298 mode = "passthru";
4299 break;
4300 default:
4301 mode = "(invalid)";
4302 break;
4303 }
4304
4305 strprint(retv, inlen, "%s", mode);
4306
4307 return fulllen;
4308 }
4309
4310 static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
4311 struct lxc_conf *c, void *data)
4312 {
4313 int len, fulllen = 0;
4314 struct lxc_netdev *netdev;
4315
4316 if (!retv)
4317 inlen = 0;
4318 else
4319 memset(retv, 0, inlen);
4320
4321 if (!data)
4322 return -1;
4323 else
4324 netdev = data;
4325 if (!netdev)
4326 return -1;
4327
4328 if (netdev->type != LXC_NET_VETH)
4329 return 0;
4330
4331 strprint(retv, inlen, "%s",
4332 netdev->priv.veth_attr.pair[0] != '\0'
4333 ? netdev->priv.veth_attr.pair
4334 : netdev->priv.veth_attr.veth1);
4335
4336 return fulllen;
4337 }
4338
4339 static int get_config_net_script_up(const char *key, char *retv, int inlen,
4340 struct lxc_conf *c, void *data)
4341 {
4342 int len, fulllen = 0;
4343 struct lxc_netdev *netdev;
4344
4345 if (!retv)
4346 inlen = 0;
4347 else
4348 memset(retv, 0, inlen);
4349
4350 if (!data)
4351 return -1;
4352 else
4353 netdev = data;
4354 if (!netdev)
4355 return -1;
4356
4357 if (netdev->upscript)
4358 strprint(retv, inlen, "%s", netdev->upscript);
4359
4360 return fulllen;
4361 }
4362
4363 static int get_config_net_script_down(const char *key, char *retv, int inlen,
4364 struct lxc_conf *c, void *data)
4365 {
4366 int len, fulllen = 0;
4367 struct lxc_netdev *netdev;
4368
4369 if (!retv)
4370 inlen = 0;
4371 else
4372 memset(retv, 0, inlen);
4373
4374 if (!data)
4375 return -1;
4376 else
4377 netdev = data;
4378 if (!netdev)
4379 return -1;
4380
4381 if (netdev->downscript)
4382 strprint(retv, inlen, "%s", netdev->downscript);
4383
4384 return fulllen;
4385 }
4386
4387 static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
4388 struct lxc_conf *c, void *data)
4389 {
4390 int len, fulllen = 0;
4391 struct lxc_netdev *netdev;
4392
4393 if (!retv)
4394 inlen = 0;
4395 else
4396 memset(retv, 0, inlen);
4397
4398 if (!data)
4399 return -1;
4400 else
4401 netdev = data;
4402 if (!netdev)
4403 return -1;
4404
4405 if (netdev->hwaddr)
4406 strprint(retv, inlen, "%s", netdev->hwaddr);
4407
4408 return fulllen;
4409 }
4410
4411 static int get_config_net_mtu(const char *key, char *retv, int inlen,
4412 struct lxc_conf *c, void *data)
4413 {
4414 int len, fulllen = 0;
4415 struct lxc_netdev *netdev;
4416
4417 if (!retv)
4418 inlen = 0;
4419 else
4420 memset(retv, 0, inlen);
4421
4422 if (!data)
4423 return -1;
4424 else
4425 netdev = data;
4426 if (!netdev)
4427 return -1;
4428
4429 if (netdev->mtu)
4430 strprint(retv, inlen, "%s", netdev->mtu);
4431
4432 return fulllen;
4433 }
4434
4435 static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
4436 struct lxc_conf *c, void *data)
4437 {
4438 int len, fulllen = 0;
4439 struct lxc_netdev *netdev;
4440
4441 if (!retv)
4442 inlen = 0;
4443 else
4444 memset(retv, 0, inlen);
4445
4446 if (!data)
4447 return -1;
4448 else
4449 netdev = data;
4450 if (!netdev)
4451 return -1;
4452
4453 if (netdev->type != LXC_NET_VLAN)
4454 return 0;
4455
4456 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
4457
4458 return fulllen;
4459 }
4460
4461 static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
4462 struct lxc_conf *c, void *data)
4463 {
4464 int len, fulllen = 0;
4465 char buf[INET_ADDRSTRLEN];
4466 struct lxc_netdev *netdev;
4467
4468 if (!retv)
4469 inlen = 0;
4470 else
4471 memset(retv, 0, inlen);
4472
4473 if (!data)
4474 return -1;
4475 else
4476 netdev = data;
4477 if (!netdev)
4478 return -1;
4479
4480 if (netdev->ipv4_gateway_auto) {
4481 strprint(retv, inlen, "auto");
4482 } else if (netdev->ipv4_gateway) {
4483 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
4484 strprint(retv, inlen, "%s", buf);
4485 }
4486
4487 return fulllen;
4488 }
4489
4490 static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
4491 struct lxc_conf *c, void *data)
4492 {
4493 int len, fulllen = 0;
4494 size_t listlen;
4495 char buf[INET_ADDRSTRLEN];
4496 struct lxc_netdev *netdev;
4497 struct lxc_list *it;
4498
4499 if (!retv)
4500 inlen = 0;
4501 else
4502 memset(retv, 0, inlen);
4503
4504 if (!data)
4505 return -1;
4506 else
4507 netdev = data;
4508 if (!netdev)
4509 return -1;
4510
4511 listlen = lxc_list_len(&netdev->ipv4);
4512 lxc_list_for_each(it, &netdev->ipv4) {
4513 struct lxc_inetdev *i = it->elem;
4514 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
4515 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
4516 (listlen-- > 1) ? "\n" : "");
4517 }
4518
4519 return fulllen;
4520 }
4521
4522 static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
4523 struct lxc_conf *c, void *data)
4524 {
4525 int len, fulllen = 0;
4526 char buf[INET6_ADDRSTRLEN];
4527 struct lxc_netdev *netdev;
4528
4529 if (!retv)
4530 inlen = 0;
4531 else
4532 memset(retv, 0, inlen);
4533
4534 if (!data)
4535 return -1;
4536 else
4537 netdev = data;
4538 if (!netdev)
4539 return -1;
4540
4541 if (netdev->ipv6_gateway_auto) {
4542 strprint(retv, inlen, "auto");
4543 } else if (netdev->ipv6_gateway) {
4544 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
4545 strprint(retv, inlen, "%s", buf);
4546 }
4547
4548 return fulllen;
4549 }
4550
4551 static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
4552 struct lxc_conf *c, void *data)
4553 {
4554 int len, fulllen = 0;
4555 size_t listlen;
4556 char buf[INET6_ADDRSTRLEN];
4557 struct lxc_netdev *netdev;
4558 struct lxc_list *it;
4559
4560 if (!retv)
4561 inlen = 0;
4562 else
4563 memset(retv, 0, inlen);
4564
4565 if (!data)
4566 return -1;
4567 else
4568 netdev = data;
4569 if (!netdev)
4570 return -1;
4571
4572 listlen = lxc_list_len(&netdev->ipv6);
4573 lxc_list_for_each(it, &netdev->ipv6) {
4574 struct lxc_inet6dev *i = it->elem;
4575 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
4576 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
4577 (listlen-- > 1) ? "\n" : "");
4578 }
4579
4580 return fulllen;
4581 }
4582
4583 int lxc_list_config_items(char *retv, int inlen)
4584 {
4585 size_t i;
4586 int len;
4587 int fulllen = 0;
4588
4589 if (!retv)
4590 inlen = 0;
4591 else
4592 memset(retv, 0, inlen);
4593
4594 for (i = 0; i < config_size; i++) {
4595 char *s = config[i].name;
4596
4597 if (s[strlen(s) - 1] == '.')
4598 continue;
4599
4600 strprint(retv, inlen, "%s\n", s);
4601 }
4602
4603 return fulllen;
4604 }
4605
4606 int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
4607 int inlen)
4608 {
4609 int len;
4610 int fulllen = 0;
4611
4612 if (!retv)
4613 inlen = 0;
4614 else
4615 memset(retv, 0, inlen);
4616
4617 if (!strcmp(key, "lxc.apparmor")) {
4618 strprint(retv, inlen, "allow_incomplete\n");
4619 strprint(retv, inlen, "profile\n");
4620 } else if (!strcmp(key, "lxc.cgroup")) {
4621 strprint(retv, inlen, "dir\n");
4622 } else if (!strcmp(key, "lxc.selinux")) {
4623 strprint(retv, inlen, "context\n");
4624 } else if (!strcmp(key, "lxc.mount")) {
4625 strprint(retv, inlen, "auto\n");
4626 strprint(retv, inlen, "entry\n");
4627 strprint(retv, inlen, "fstab\n");
4628 } else if (!strcmp(key, "lxc.rootfs")) {
4629 strprint(retv, inlen, "mount\n");
4630 strprint(retv, inlen, "options\n");
4631 strprint(retv, inlen, "path\n");
4632 } else if (!strcmp(key, "lxc.uts")) {
4633 strprint(retv, inlen, "name\n");
4634 } else if (!strcmp(key, "lxc.hook")) {
4635 strprint(retv, inlen, "autodev\n");
4636 strprint(retv, inlen, "clone\n");
4637 strprint(retv, inlen, "destroy\n");
4638 strprint(retv, inlen, "mount\n");
4639 strprint(retv, inlen, "post-stop\n");
4640 strprint(retv, inlen, "pre-mount\n");
4641 strprint(retv, inlen, "pre-start\n");
4642 strprint(retv, inlen, "start-host\n");
4643 strprint(retv, inlen, "start\n");
4644 strprint(retv, inlen, "stop\n");
4645 } else if (!strcmp(key, "lxc.cap")) {
4646 strprint(retv, inlen, "drop\n");
4647 strprint(retv, inlen, "keep\n");
4648 } else if (!strcmp(key, "lxc.console")) {
4649 strprint(retv, inlen, "logfile\n");
4650 strprint(retv, inlen, "path\n");
4651 } else if (!strcmp(key, "lxc.seccomp")) {
4652 strprint(retv, inlen, "profile\n");
4653 } else if (!strcmp(key, "lxc.signal")) {
4654 strprint(retv, inlen, "halt\n");
4655 strprint(retv, inlen, "reboot\n");
4656 strprint(retv, inlen, "stop\n");
4657 } else if (!strcmp(key, "lxc.start")) {
4658 strprint(retv, inlen, "auto\n");
4659 strprint(retv, inlen, "delay\n");
4660 strprint(retv, inlen, "order\n");
4661 } else if (!strcmp(key, "lxc.monitor")) {
4662 strprint(retv, inlen, "unshare\n");
4663 } else {
4664 fulllen = -1;
4665 }
4666
4667 return fulllen;
4668 }
4669
4670 int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
4671 {
4672 int len;
4673 const char *idxstring;
4674 struct lxc_netdev *netdev;
4675 int fulllen = 0;
4676 ssize_t idx = -1;
4677
4678 idxstring = key + 8;
4679 if (!isdigit(*idxstring))
4680 return -1;
4681
4682 (void)get_network_config_ops(key, c, &idx, NULL);
4683 if (idx < 0)
4684 return -1;
4685
4686 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
4687 if (!netdev)
4688 return -1;
4689
4690 if (!retv)
4691 inlen = 0;
4692 else
4693 memset(retv, 0, inlen);
4694
4695 strprint(retv, inlen, "type\n");
4696 strprint(retv, inlen, "script.up\n");
4697 strprint(retv, inlen, "script.down\n");
4698 if (netdev->type != LXC_NET_EMPTY) {
4699 strprint(retv, inlen, "flags\n");
4700 strprint(retv, inlen, "link\n");
4701 strprint(retv, inlen, "name\n");
4702 strprint(retv, inlen, "hwaddr\n");
4703 strprint(retv, inlen, "mtu\n");
4704 strprint(retv, inlen, "ipv6.address\n");
4705 strprint(retv, inlen, "ipv6.gateway\n");
4706 strprint(retv, inlen, "ipv4.address\n");
4707 strprint(retv, inlen, "ipv4.gateway\n");
4708 }
4709
4710 switch (netdev->type) {
4711 case LXC_NET_VETH:
4712 strprint(retv, inlen, "veth.pair\n");
4713 break;
4714 case LXC_NET_MACVLAN:
4715 strprint(retv, inlen, "macvlan.mode\n");
4716 break;
4717 case LXC_NET_VLAN:
4718 strprint(retv, inlen, "vlan.id\n");
4719 break;
4720 case LXC_NET_PHYS:
4721 break;
4722 }
4723
4724 return fulllen;
4725 }