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