2 * lxc: linux Container library
3 * (C) Copyright IBM Corp. 2007, 2008
6 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
31 #include <inttypes.h> /* Required for PRIu64 to work. */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/utsname.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
48 #include "confile_legacy.h"
49 #include "confile_utils.h"
54 #include "lxcseccomp.h"
59 #include <../include/ifaddrs.h>
62 #if HAVE_SYS_PERSONALITY_H
63 #include <sys/personality.h>
66 lxc_log_define(lxc_confile
, lxc
);
68 #define lxc_config_define(name) \
69 static int set_config_##name(const char *, const char *, \
70 struct lxc_conf *, void *); \
71 static int get_config_##name(const char *, char *, int, \
72 struct lxc_conf *, void *); \
73 static int clr_config_##name(const char *, struct lxc_conf *, \
77 lxc_config_define(personality
);
78 lxc_config_define(pts
);
79 lxc_config_define(tty
);
80 lxc_config_define(ttydir
);
81 lxc_config_define(apparmor_profile
);
82 lxc_config_define(apparmor_allow_incomplete
);
83 lxc_config_define(selinux_context
);
84 lxc_config_define(cgroup
);
85 lxc_config_define(idmaps
);
86 lxc_config_define(loglevel
);
87 lxc_config_define(logfile
);
88 lxc_config_define(mount
);
89 lxc_config_define(mount_auto
);
90 lxc_config_define(fstab
);
91 lxc_config_define(rootfs_mount
);
92 lxc_config_define(rootfs_options
);
93 lxc_config_define(rootfs_backend
);
94 lxc_config_define(rootfs
);
95 lxc_config_define(utsname
);
96 lxc_config_define(hooks
);
97 lxc_config_define(net_type
);
98 lxc_config_define(net_flags
);
99 lxc_config_define(net_link
);
100 lxc_config_define(net_name
);
101 lxc_config_define(net_veth_pair
);
102 lxc_config_define(net_macvlan_mode
);
103 lxc_config_define(net_hwaddr
);
104 lxc_config_define(net_vlan_id
);
105 lxc_config_define(net_mtu
);
106 lxc_config_define(net_ipv4
);
107 lxc_config_define(net_ipv4_gateway
);
108 lxc_config_define(net_script_up
);
109 lxc_config_define(net_script_down
);
110 lxc_config_define(net_ipv6
);
111 lxc_config_define(net_ipv6_gateway
);
112 lxc_config_define(net_nic
);
113 lxc_config_define(net
);
114 lxc_config_define(cap_drop
);
115 lxc_config_define(cap_keep
);
116 lxc_config_define(console_logfile
);
117 lxc_config_define(console
);
118 lxc_config_define(seccomp
);
119 lxc_config_define(includefiles
);
120 lxc_config_define(autodev
);
121 lxc_config_define(haltsignal
);
122 lxc_config_define(rebootsignal
);
123 lxc_config_define(stopsignal
);
124 lxc_config_define(start
);
125 lxc_config_define(monitor
);
126 lxc_config_define(group
);
127 lxc_config_define(environment
);
128 lxc_config_define(init_cmd
);
129 lxc_config_define(init_uid
);
130 lxc_config_define(init_gid
);
131 lxc_config_define(ephemeral
);
132 lxc_config_define(syslog
);
133 lxc_config_define(no_new_privs
);
134 lxc_config_define(prlimit
);
136 static struct lxc_config_t config
[] = {
137 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
138 { "lxc.pts", set_config_pts
, get_config_pts
, clr_config_pts
, },
139 { "lxc.tty", set_config_tty
, get_config_tty
, clr_config_tty
, },
140 { "lxc.devttydir", set_config_ttydir
, get_config_ttydir
, clr_config_ttydir
, },
141 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
142 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
143 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
148 { "lxc.aa_profile", set_config_lsm_aa_profile
, get_config_lsm_aa_profile
, clr_config_lsm_aa_profile
, },
149 { "lxc.aa_allow_incomplete", set_config_lsm_aa_incomplete
, get_config_lsm_aa_incomplete
, clr_config_lsm_aa_incomplete
, },
150 { "lxc.se_context", set_config_lsm_se_context
, get_config_lsm_se_context
, clr_config_lsm_se_context
, },
152 { "lxc.cgroup", set_config_cgroup
, get_config_cgroup
, clr_config_cgroup
, },
153 { "lxc.id_map", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
154 { "lxc.loglevel", set_config_loglevel
, get_config_loglevel
, clr_config_loglevel
, },
155 { "lxc.logfile", set_config_logfile
, get_config_logfile
, clr_config_logfile
, },
156 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
157 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
158 { "lxc.mount", set_config_fstab
, get_config_fstab
, clr_config_fstab
, },
159 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
160 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
161 { "lxc.rootfs.backend", set_config_rootfs_backend
, get_config_rootfs_backend
, clr_config_rootfs_backend
, },
162 { "lxc.rootfs", set_config_rootfs
, get_config_rootfs
, clr_config_rootfs
, },
163 { "lxc.utsname", set_config_utsname
, get_config_utsname
, clr_config_utsname
, },
164 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
165 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
166 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
167 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
168 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
169 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
170 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
171 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
172 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
173 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
178 { "lxc.network.type", set_config_network_legacy_type
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
179 { "lxc.network.flags", set_config_network_legacy_flags
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
180 { "lxc.network.link", set_config_network_legacy_link
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
181 { "lxc.network.name", set_config_network_legacy_name
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
182 { "lxc.network.macvlan.mode", set_config_network_legacy_macvlan_mode
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
183 { "lxc.network.veth.pair", set_config_network_legacy_veth_pair
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
184 { "lxc.network.script.up", set_config_network_legacy_script_up
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
185 { "lxc.network.script.down", set_config_network_legacy_script_down
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
186 { "lxc.network.hwaddr", set_config_network_legacy_hwaddr
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
187 { "lxc.network.mtu", set_config_network_legacy_mtu
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
188 { "lxc.network.vlan.id", set_config_network_legacy_vlan_id
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
189 { "lxc.network.ipv4.gateway", set_config_network_legacy_ipv4_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
190 { "lxc.network.ipv4", set_config_network_legacy_ipv4
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
191 { "lxc.network.ipv6.gateway", set_config_network_legacy_ipv6_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
192 { "lxc.network.ipv6", set_config_network_legacy_ipv6
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
193 { "lxc.network.", set_config_network_legacy_nic
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
194 { "lxc.network", set_config_network_legacy
, get_config_network_legacy
, clr_config_network_legacy
, },
196 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
197 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
198 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
199 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
200 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
201 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
202 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
203 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
204 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
205 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
206 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
207 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
208 { "lxc.net.ipv4", set_config_net_ipv4
, get_config_net_ipv4
, clr_config_net_ipv4
, },
209 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
210 { "lxc.net.ipv6", set_config_net_ipv6
, get_config_net_ipv6
, clr_config_net_ipv6
, },
211 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
212 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
213 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
214 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
215 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
216 { "lxc.console", set_config_console
, get_config_console
, clr_config_console
, },
217 { "lxc.seccomp", set_config_seccomp
, get_config_seccomp
, clr_config_seccomp
, },
218 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
219 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
220 { "lxc.haltsignal", set_config_haltsignal
, get_config_haltsignal
, clr_config_haltsignal
, },
221 { "lxc.rebootsignal", set_config_rebootsignal
, get_config_rebootsignal
, clr_config_rebootsignal
, },
222 { "lxc.stopsignal", set_config_stopsignal
, get_config_stopsignal
, clr_config_stopsignal
, },
223 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
224 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
225 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
226 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
227 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
228 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
229 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
230 { "lxc.syslog", set_config_syslog
, get_config_syslog
, clr_config_syslog
, },
231 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
233 /* REMOVE IN LXC 3.0: legacy keys [START]*/
234 { "lxc.init_cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
235 { "lxc.init_uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
236 { "lxc.init_gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
237 { "lxc.limit", set_config_limit
, get_config_limit
, clr_config_limit
, },
238 /* REMOVE IN LXC 3.0: legacy keys [END]*/
240 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
241 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
242 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
243 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
251 static const struct signame signames
[] = {
284 { SIGSTKFLT
, "STKFLT" },
299 { SIGVTALRM
, "VTALRM" },
305 { SIGWINCH
, "WINCH" },
323 { SIGUNUSED
, "UNUSED" },
330 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
332 extern struct lxc_config_t
*lxc_getconfig(const char *key
)
336 for (i
= 0; i
< config_size
; i
++)
337 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
342 int lxc_listconfigs(char *retv
, int inlen
)
351 memset(retv
, 0, inlen
);
353 for (i
= 0; i
< config_size
; i
++) {
354 char *s
= config
[i
].name
;
355 if (s
[strlen(s
) - 1] == '.')
357 strprint(retv
, inlen
, "%s\n", s
);
363 static int set_config_net(const char *key
, const char *value
,
364 struct lxc_conf
*lxc_conf
, void *data
)
366 if (!lxc_config_value_empty(value
)) {
367 ERROR("lxc.net must not have a value");
371 return clr_config_net(key
, lxc_conf
, data
);
374 static int set_config_net_type(const char *key
, const char *value
,
375 struct lxc_conf
*lxc_conf
, void *data
)
377 struct lxc_netdev
*netdev
;
379 if (lxc_config_value_empty(value
))
380 return clr_config_net_type(key
, lxc_conf
, data
);
389 if (!strcmp(value
, "veth")) {
390 netdev
->type
= LXC_NET_VETH
;
391 } else if (!strcmp(value
, "macvlan")) {
392 netdev
->type
= LXC_NET_MACVLAN
;
393 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
395 } else if (!strcmp(value
, "vlan")) {
396 netdev
->type
= LXC_NET_VLAN
;
397 } else if (!strcmp(value
, "phys")) {
398 netdev
->type
= LXC_NET_PHYS
;
399 } else if (!strcmp(value
, "empty")) {
400 netdev
->type
= LXC_NET_EMPTY
;
401 } else if (!strcmp(value
, "none")) {
402 netdev
->type
= LXC_NET_NONE
;
404 ERROR("invalid network type %s", value
);
412 * If you have p="lxc.net.0.link", pass it p+8
413 * to get back '0' (the index of the nic).
415 static int get_network_netdev_idx(const char *key
)
419 if (*key
< '0' || *key
> '9')
422 ret
= sscanf(key
, "%d", &idx
);
430 * If you have p="lxc.net.0", pass this p+8 and it will return
431 * the netdev of the first configured nic.
433 static struct lxc_netdev
*get_netdev_from_key(const char *key
,
434 struct lxc_list
*network
)
439 struct lxc_netdev
*netdev
= NULL
;
441 idx
= get_network_netdev_idx(key
);
445 lxc_list_for_each(it
, network
) {
455 extern int lxc_list_nicconfigs(struct lxc_conf
*c
, const char *key
, char *retv
,
458 struct lxc_netdev
*netdev
;
462 netdev
= get_netdev_from_key(key
+ 8, &c
->network
);
469 memset(retv
, 0, inlen
);
471 strprint(retv
, inlen
, "type\n");
472 strprint(retv
, inlen
, "script.up\n");
473 strprint(retv
, inlen
, "script.down\n");
474 if (netdev
->type
!= LXC_NET_EMPTY
) {
475 strprint(retv
, inlen
, "flags\n");
476 strprint(retv
, inlen
, "link\n");
477 strprint(retv
, inlen
, "name\n");
478 strprint(retv
, inlen
, "hwaddr\n");
479 strprint(retv
, inlen
, "mtu\n");
480 strprint(retv
, inlen
, "ipv6\n");
481 strprint(retv
, inlen
, "ipv6.gateway\n");
482 strprint(retv
, inlen
, "ipv4\n");
483 strprint(retv
, inlen
, "ipv4.gateway\n");
486 switch (netdev
->type
) {
488 strprint(retv
, inlen
, "veth.pair\n");
490 case LXC_NET_MACVLAN
:
491 strprint(retv
, inlen
, "macvlan.mode\n");
494 strprint(retv
, inlen
, "vlan.id\n");
503 static int set_config_net_flags(const char *key
, const char *value
,
504 struct lxc_conf
*lxc_conf
, void *data
)
506 struct lxc_netdev
*netdev
;
508 if (lxc_config_value_empty(value
))
509 return clr_config_net_flags(key
, lxc_conf
, data
);
518 netdev
->flags
|= IFF_UP
;
523 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
524 struct lxc_netdev
*netdev
)
526 struct ifaddrs
*ifaddr
, *ifa
;
529 const char *type_key
= "lxc.net.type";
530 const char *link_key
= "lxc.net.link";
531 const char *tmpvalue
= "phys";
533 if (getifaddrs(&ifaddr
) == -1) {
534 SYSERROR("Get network interfaces failed");
538 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
541 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
544 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
545 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
548 ret
= set_config_net_link(
549 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
551 ERROR("failed to create matched ifnames");
555 ERROR("failed to create matched ifnames");
567 static int set_config_net_link(const char *key
, const char *value
,
568 struct lxc_conf
*lxc_conf
, void *data
)
570 struct lxc_netdev
*netdev
;
573 if (lxc_config_value_empty(value
))
574 return clr_config_net_link(key
, lxc_conf
, data
);
583 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
584 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
586 ret
= network_ifname(&netdev
->link
, value
);
591 static int set_config_net_name(const char *key
, const char *value
,
592 struct lxc_conf
*lxc_conf
, void *data
)
594 struct lxc_netdev
*netdev
;
596 if (lxc_config_value_empty(value
))
597 return clr_config_net_name(key
, lxc_conf
, data
);
606 return network_ifname(&netdev
->name
, value
);
609 static int set_config_net_veth_pair(const char *key
, const char *value
,
610 struct lxc_conf
*lxc_conf
, void *data
)
612 struct lxc_netdev
*netdev
;
614 if (lxc_config_value_empty(value
))
615 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
624 return network_ifname(&netdev
->priv
.veth_attr
.pair
, value
);
627 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
628 struct lxc_conf
*lxc_conf
, void *data
)
630 struct lxc_netdev
*netdev
;
632 if (lxc_config_value_empty(value
))
633 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
642 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
645 static int set_config_net_hwaddr(const char *key
, const char *value
,
646 struct lxc_conf
*lxc_conf
, void *data
)
648 struct lxc_netdev
*netdev
;
651 if (lxc_config_value_empty(value
))
652 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
661 new_value
= strdup(value
);
663 SYSERROR("failed to strdup \"%s\"", value
);
666 rand_complete_hwaddr(new_value
);
668 if (lxc_config_value_empty(new_value
)) {
670 netdev
->hwaddr
= NULL
;
674 netdev
->hwaddr
= new_value
;
678 static int set_config_net_vlan_id(const char *key
, const char *value
,
679 struct lxc_conf
*lxc_conf
, void *data
)
681 struct lxc_netdev
*netdev
;
683 if (lxc_config_value_empty(value
))
684 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
693 if (get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0))
699 static int set_config_net_mtu(const char *key
, const char *value
,
700 struct lxc_conf
*lxc_conf
, void *data
)
702 struct lxc_netdev
*netdev
;
704 if (lxc_config_value_empty(value
))
705 return clr_config_net_mtu(key
, lxc_conf
, data
);
714 return set_config_string_item(&netdev
->mtu
, value
);
717 static int set_config_net_ipv4(const char *key
, const char *value
,
718 struct lxc_conf
*lxc_conf
, void *data
)
720 struct lxc_netdev
*netdev
;
721 struct lxc_inetdev
*inetdev
;
722 struct lxc_list
*list
;
723 char *cursor
, *slash
;
724 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
726 if (lxc_config_value_empty(value
))
727 return clr_config_net_ipv4(key
, lxc_conf
, data
);
736 inetdev
= malloc(sizeof(*inetdev
));
738 SYSERROR("failed to allocate ipv4 address");
741 memset(inetdev
, 0, sizeof(*inetdev
));
743 list
= malloc(sizeof(*list
));
745 SYSERROR("failed to allocate memory");
751 list
->elem
= inetdev
;
753 addr
= strdup(value
);
755 ERROR("no address specified");
761 cursor
= strstr(addr
, " ");
767 slash
= strstr(addr
, "/");
773 if (!inet_pton(AF_INET
, addr
, &inetdev
->addr
)) {
774 SYSERROR("invalid ipv4 address: %s", value
);
781 if (bcast
&& !inet_pton(AF_INET
, bcast
, &inetdev
->bcast
)) {
782 SYSERROR("invalid ipv4 broadcast address: %s", value
);
789 /* No prefix specified, determine it from the network class. */
791 if (lxc_safe_uint(prefix
, &inetdev
->prefix
) < 0)
794 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
797 /* If no broadcast address, let compute one from the
798 * prefix and address.
801 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
802 inetdev
->bcast
.s_addr
|=
803 htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
806 lxc_list_add_tail(&netdev
->ipv4
, list
);
812 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
813 struct lxc_conf
*lxc_conf
, void *data
)
815 struct lxc_netdev
*netdev
;
817 if (lxc_config_value_empty(value
))
818 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
827 free(netdev
->ipv4_gateway
);
829 if (!strcmp(value
, "auto")) {
830 netdev
->ipv4_gateway
= NULL
;
831 netdev
->ipv4_gateway_auto
= true;
835 gw
= malloc(sizeof(*gw
));
837 SYSERROR("failed to allocate ipv4 gateway address");
841 if (!inet_pton(AF_INET
, value
, gw
)) {
842 SYSERROR("invalid ipv4 gateway address: %s", value
);
847 netdev
->ipv4_gateway
= gw
;
848 netdev
->ipv4_gateway_auto
= false;
854 static int set_config_net_ipv6(const char *key
, const char *value
,
855 struct lxc_conf
*lxc_conf
, void *data
)
857 struct lxc_netdev
*netdev
;
858 struct lxc_inet6dev
*inet6dev
;
859 struct lxc_list
*list
;
860 char *slash
, *valdup
, *netmask
;
862 if (lxc_config_value_empty(value
))
863 return clr_config_net_ipv6(key
, lxc_conf
, data
);
872 inet6dev
= malloc(sizeof(*inet6dev
));
874 SYSERROR("failed to allocate ipv6 address");
877 memset(inet6dev
, 0, sizeof(*inet6dev
));
879 list
= malloc(sizeof(*list
));
881 SYSERROR("failed to allocate memory");
887 list
->elem
= inet6dev
;
889 valdup
= strdup(value
);
891 ERROR("no address specified");
897 inet6dev
->prefix
= 64;
898 slash
= strstr(valdup
, "/");
902 if (lxc_safe_uint(netmask
, &inet6dev
->prefix
) < 0)
906 if (!inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
)) {
907 SYSERROR("invalid ipv6 address: %s", valdup
);
914 lxc_list_add_tail(&netdev
->ipv6
, list
);
920 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
921 struct lxc_conf
*lxc_conf
, void *data
)
923 struct lxc_netdev
*netdev
;
925 if (lxc_config_value_empty(value
))
926 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
935 free(netdev
->ipv6_gateway
);
937 if (!strcmp(value
, "auto")) {
938 netdev
->ipv6_gateway
= NULL
;
939 netdev
->ipv6_gateway_auto
= true;
943 gw
= malloc(sizeof(*gw
));
945 SYSERROR("failed to allocate ipv6 gateway address");
949 if (!inet_pton(AF_INET6
, value
, gw
)) {
950 SYSERROR("invalid ipv6 gateway address: %s", value
);
955 netdev
->ipv6_gateway
= gw
;
956 netdev
->ipv6_gateway_auto
= false;
962 static int set_config_net_script_up(const char *key
, const char *value
,
963 struct lxc_conf
*lxc_conf
, void *data
)
965 struct lxc_netdev
*netdev
;
967 if (lxc_config_value_empty(value
))
968 return clr_config_net_script_up(key
, lxc_conf
, data
);
977 return set_config_string_item(&netdev
->upscript
, value
);
980 static int set_config_net_script_down(const char *key
, const char *value
,
981 struct lxc_conf
*lxc_conf
, void *data
)
983 struct lxc_netdev
*netdev
;
985 if (lxc_config_value_empty(value
))
986 return clr_config_net_script_down(key
, lxc_conf
, data
);
995 return set_config_string_item(&netdev
->downscript
, value
);
998 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
1000 struct lxc_list
*hooklist
;
1002 hooklist
= malloc(sizeof(*hooklist
));
1008 hooklist
->elem
= hook
;
1009 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
1013 static int set_config_seccomp(const char *key
, const char *value
,
1014 struct lxc_conf
*lxc_conf
, void *data
)
1016 return set_config_path_item(&lxc_conf
->seccomp
, value
);
1019 static int set_config_init_cmd(const char *key
, const char *value
,
1020 struct lxc_conf
*lxc_conf
, void *data
)
1022 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1025 static int set_config_init_uid(const char *key
, const char *value
,
1026 struct lxc_conf
*lxc_conf
, void *data
)
1028 unsigned int init_uid
;
1030 /* Set config value to default. */
1031 if (lxc_config_value_empty(value
)) {
1032 lxc_conf
->init_uid
= 0;
1036 /* Parse new config value. */
1037 if (lxc_safe_uint(value
, &init_uid
) < 0)
1039 lxc_conf
->init_uid
= init_uid
;
1044 static int set_config_init_gid(const char *key
, const char *value
,
1045 struct lxc_conf
*lxc_conf
, void *data
)
1047 unsigned int init_gid
;
1049 /* Set config value to default. */
1050 if (lxc_config_value_empty(value
)) {
1051 lxc_conf
->init_gid
= 0;
1055 /* Parse new config value. */
1056 if (lxc_safe_uint(value
, &init_gid
) < 0)
1058 lxc_conf
->init_gid
= init_gid
;
1063 static int set_config_hooks(const char *key
, const char *value
,
1064 struct lxc_conf
*lxc_conf
, void *data
)
1068 if (lxc_config_value_empty(value
))
1069 return lxc_clear_hooks(lxc_conf
, key
);
1071 if (strcmp(key
+ 4, "hook") == 0) {
1072 ERROR("lxc.hook cannot take a value");
1075 copy
= strdup(value
);
1077 SYSERROR("failed to dup string '%s'", value
);
1081 if (strcmp(key
+ 9, "pre-start") == 0)
1082 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
1083 else if (strcmp(key
+ 9, "pre-mount") == 0)
1084 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
1085 else if (strcmp(key
+ 9, "autodev") == 0)
1086 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
1087 else if (strcmp(key
+ 9, "mount") == 0)
1088 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
1089 else if (strcmp(key
+ 9, "start") == 0)
1090 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
1091 else if (strcmp(key
+ 9, "stop") == 0)
1092 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
1093 else if (strcmp(key
+ 9, "post-stop") == 0)
1094 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
1095 else if (strcmp(key
+ 9, "clone") == 0)
1096 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
1097 else if (strcmp(key
+ 9, "destroy") == 0)
1098 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
1100 SYSERROR("Unknown key: %s", key
);
1105 static int set_config_personality(const char *key
, const char *value
,
1106 struct lxc_conf
*lxc_conf
, void *data
)
1108 signed long personality
= lxc_config_parse_arch(value
);
1110 if (personality
>= 0)
1111 lxc_conf
->personality
= personality
;
1113 WARN("unsupported personality '%s'", value
);
1118 static int set_config_pts(const char *key
, const char *value
,
1119 struct lxc_conf
*lxc_conf
, void *data
)
1121 /* Set config value to default. */
1122 if (lxc_config_value_empty(value
)) {
1127 /* Parse new config value. */
1128 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1134 /* We only need to check whether the first byte of the key after the lxc.start.
1135 * prefix matches our expectations since they fortunately all start with a
1136 * different letter. If anything was wrong with the key we would have already
1137 * noticed when the callback was called.
1139 static int set_config_start(const char *key
, const char *value
,
1140 struct lxc_conf
*lxc_conf
, void *data
)
1144 is_empty
= lxc_config_value_empty(value
);
1146 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1147 /* Set config value to default. */
1149 lxc_conf
->start_auto
= 0;
1153 /* Parse new config value. */
1154 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1157 if (lxc_conf
->start_auto
> 1)
1161 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1162 /* Set config value to default. */
1164 lxc_conf
->start_delay
= 0;
1168 /* Parse new config value. */
1169 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1170 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1171 /* Set config value to default. */
1173 lxc_conf
->start_order
= 0;
1177 /* Parse new config value. */
1178 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1181 SYSERROR("Unknown key: %s", key
);
1185 static int set_config_monitor(const char *key
, const char *value
,
1186 struct lxc_conf
*lxc_conf
, void *data
)
1188 /* Set config value to default. */
1189 if (lxc_config_value_empty(value
)) {
1190 lxc_conf
->monitor_unshare
= 0;
1194 /* Parse new config value. */
1195 if (strcmp(key
+ 12, "unshare") == 0)
1196 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1198 SYSERROR("Unknown key: %s", key
);
1202 static int set_config_group(const char *key
, const char *value
,
1203 struct lxc_conf
*lxc_conf
, void *data
)
1205 char *groups
, *groupptr
, *sptr
, *token
;
1206 struct lxc_list
*grouplist
;
1209 if (lxc_config_value_empty(value
))
1210 return lxc_clear_groups(lxc_conf
);
1212 groups
= strdup(value
);
1214 SYSERROR("failed to dup '%s'", value
);
1218 /* In case several groups are specified in a single line
1219 * split these groups in a single element for the list.
1221 for (groupptr
= groups
;; groupptr
= NULL
) {
1222 token
= strtok_r(groupptr
, " \t", &sptr
);
1228 grouplist
= malloc(sizeof(*grouplist
));
1230 SYSERROR("failed to allocate groups list");
1234 grouplist
->elem
= strdup(token
);
1235 if (!grouplist
->elem
) {
1236 SYSERROR("failed to dup '%s'", token
);
1241 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1248 static int set_config_environment(const char *key
, const char *value
,
1249 struct lxc_conf
*lxc_conf
, void *data
)
1251 struct lxc_list
*list_item
= NULL
;
1253 if (lxc_config_value_empty(value
))
1254 return lxc_clear_environment(lxc_conf
);
1256 list_item
= malloc(sizeof(*list_item
));
1260 list_item
->elem
= strdup(value
);
1262 if (!list_item
->elem
)
1265 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1274 static int set_config_tty(const char *key
, const char *value
,
1275 struct lxc_conf
*lxc_conf
, void *data
)
1277 /* Set config value to default. */
1278 if (lxc_config_value_empty(value
)) {
1283 /* Parse new config value. */
1284 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1287 static int set_config_ttydir(const char *key
, const char *value
,
1288 struct lxc_conf
*lxc_conf
, void *data
)
1290 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1294 static int set_config_apparmor_profile(const char *key
, const char *value
,
1295 struct lxc_conf
*lxc_conf
, void *data
)
1297 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1300 static int set_config_apparmor_allow_incomplete(const char *key
,
1302 struct lxc_conf
*lxc_conf
,
1305 /* Set config value to default. */
1306 if (lxc_config_value_empty(value
)) {
1307 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1311 /* Parse new config value. */
1312 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1315 if (lxc_conf
->lsm_aa_allow_incomplete
> 1) {
1316 ERROR("Wrong value for lxc.lsm_aa_allow_incomplete. Can only "
1317 "be set to 0 or 1");
1324 static int set_config_selinux_context(const char *key
, const char *value
,
1325 struct lxc_conf
*lxc_conf
, void *data
)
1327 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1330 static int set_config_logfile(const char *key
, const char *value
,
1331 struct lxc_conf
*c
, void *data
)
1335 if (lxc_config_value_empty(value
)) {
1341 /* Store these values in the lxc_conf, and then try to set for actual
1344 ret
= set_config_path_item(&c
->logfile
, value
);
1346 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1350 static int set_config_loglevel(const char *key
, const char *value
,
1351 struct lxc_conf
*lxc_conf
, void *data
)
1355 /* Set config value to default. */
1356 if (lxc_config_value_empty(value
)) {
1357 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1361 /* Parse new config value. */
1362 if (value
[0] >= '0' && value
[0] <= '9') {
1363 if (lxc_safe_int(value
, &newlevel
) < 0)
1366 newlevel
= lxc_log_priority_to_int(value
);
1369 /* Store these values in the lxc_conf, and then try to set for actual
1372 lxc_conf
->loglevel
= newlevel
;
1373 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1376 static int set_config_autodev(const char *key
, const char *value
,
1377 struct lxc_conf
*lxc_conf
, void *data
)
1379 /* Set config value to default. */
1380 if (lxc_config_value_empty(value
)) {
1381 lxc_conf
->autodev
= 0;
1385 /* Parse new config value. */
1386 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1389 if (lxc_conf
->autodev
> 1) {
1390 ERROR("Wrong value for lxc.autodev. Can only be set to 0 or 1");
1397 static int sig_num(const char *sig
)
1399 unsigned int signum
;
1401 if (lxc_safe_uint(sig
, &signum
) < 0)
1407 static int rt_sig_num(const char *signame
)
1409 int rtmax
= 0, sig_n
= 0;
1411 if (strncasecmp(signame
, "max-", 4) == 0) {
1416 if (!isdigit(*signame
))
1419 sig_n
= sig_num(signame
);
1420 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1421 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1427 static int sig_parse(const char *signame
)
1431 if (isdigit(*signame
)) {
1432 return sig_num(signame
);
1433 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1435 if (strncasecmp(signame
, "rt", 2) == 0)
1436 return rt_sig_num(signame
+ 2);
1437 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1438 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1439 return signames
[n
].num
;
1446 static int set_config_haltsignal(const char *key
, const char *value
,
1447 struct lxc_conf
*lxc_conf
, void *data
)
1451 /* Set config value to default. */
1452 if (lxc_config_value_empty(value
)) {
1453 lxc_conf
->haltsignal
= 0;
1457 /* Parse new config value. */
1458 sig_n
= sig_parse(value
);
1462 lxc_conf
->haltsignal
= sig_n
;
1467 static int set_config_rebootsignal(const char *key
, const char *value
,
1468 struct lxc_conf
*lxc_conf
, void *data
)
1472 /* Set config value to default. */
1473 if (lxc_config_value_empty(value
)) {
1474 lxc_conf
->rebootsignal
= 0;
1478 /* Parse new config value. */
1479 sig_n
= sig_parse(value
);
1482 lxc_conf
->rebootsignal
= sig_n
;
1487 static int set_config_stopsignal(const char *key
, const char *value
,
1488 struct lxc_conf
*lxc_conf
, void *data
)
1492 /* Set config value to default. */
1493 if (lxc_config_value_empty(value
)) {
1494 lxc_conf
->stopsignal
= 0;
1498 /* Parse new config value. */
1499 sig_n
= sig_parse(value
);
1502 lxc_conf
->stopsignal
= sig_n
;
1507 static int set_config_cgroup(const char *key
, const char *value
,
1508 struct lxc_conf
*lxc_conf
, void *data
)
1511 char *token
= "lxc.cgroup.";
1512 struct lxc_list
*cglist
= NULL
;
1513 struct lxc_cgroup
*cgelem
= NULL
;
1515 if (lxc_config_value_empty(value
))
1516 return lxc_clear_cgroups(lxc_conf
, key
);
1518 subkey
= strstr(key
, token
);
1522 if (!strlen(subkey
))
1525 if (strlen(subkey
) == strlen(token
))
1528 subkey
+= strlen(token
);
1530 cglist
= malloc(sizeof(*cglist
));
1534 cgelem
= malloc(sizeof(*cgelem
));
1537 memset(cgelem
, 0, sizeof(*cgelem
));
1539 cgelem
->subsystem
= strdup(subkey
);
1540 cgelem
->value
= strdup(value
);
1542 if (!cgelem
->subsystem
|| !cgelem
->value
)
1545 cglist
->elem
= cgelem
;
1547 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1555 free(cgelem
->subsystem
);
1557 free(cgelem
->value
);
1565 static int set_config_prlimit(const char *key
, const char *value
,
1566 struct lxc_conf
*lxc_conf
, void *data
)
1568 struct lxc_list
*iter
;
1569 struct rlimit limit
;
1570 unsigned long limit_value
;
1571 struct lxc_list
*limlist
= NULL
;
1572 struct lxc_limit
*limelem
= NULL
;
1574 if (lxc_config_value_empty(value
))
1575 return lxc_clear_limits(lxc_conf
, key
);
1577 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1580 key
+= sizeof("lxc.prlimit.") - 1;
1582 /* soft limit comes first in the value */
1583 if (!parse_limit_value(&value
, &limit_value
))
1585 limit
.rlim_cur
= limit_value
;
1587 /* skip spaces and a colon */
1588 while (isspace(*value
))
1593 else if (*value
) /* any other character is an error here */
1596 while (isspace(*value
))
1599 /* optional hard limit */
1601 if (!parse_limit_value(&value
, &limit_value
))
1603 limit
.rlim_max
= limit_value
;
1605 /* check for trailing garbage */
1606 while (isspace(*value
))
1612 /* a single value sets both hard and soft limit */
1613 limit
.rlim_max
= limit
.rlim_cur
;
1616 /* find existing list element */
1617 lxc_list_for_each(iter
, &lxc_conf
->limits
)
1619 limelem
= iter
->elem
;
1620 if (!strcmp(key
, limelem
->resource
)) {
1621 limelem
->limit
= limit
;
1626 /* allocate list element */
1627 limlist
= malloc(sizeof(*limlist
));
1631 limelem
= malloc(sizeof(*limelem
));
1634 memset(limelem
, 0, sizeof(*limelem
));
1636 limelem
->resource
= strdup(key
);
1637 if (!limelem
->resource
)
1639 limelem
->limit
= limit
;
1641 limlist
->elem
= limelem
;
1643 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1650 free(limelem
->resource
);
1656 static int set_config_idmaps(const char *key
, const char *value
,
1657 struct lxc_conf
*lxc_conf
, void *data
)
1659 unsigned long hostid
, nsid
, range
;
1662 struct lxc_list
*idmaplist
= NULL
;
1663 struct id_map
*idmap
= NULL
;
1665 if (lxc_config_value_empty(value
))
1666 return lxc_clear_idmaps(lxc_conf
);
1668 idmaplist
= malloc(sizeof(*idmaplist
));
1672 idmap
= malloc(sizeof(*idmap
));
1675 memset(idmap
, 0, sizeof(*idmap
));
1677 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1681 INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1683 idmap
->idtype
= ID_TYPE_UID
;
1684 else if (type
== 'g')
1685 idmap
->idtype
= ID_TYPE_GID
;
1689 idmap
->hostid
= hostid
;
1691 idmap
->range
= range
;
1692 idmaplist
->elem
= idmap
;
1693 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1705 static int set_config_fstab(const char *key
, const char *value
,
1706 struct lxc_conf
*lxc_conf
, void *data
)
1708 if (lxc_config_value_empty(value
)) {
1709 clr_config_fstab(key
, lxc_conf
, NULL
);
1713 return set_config_path_item(&lxc_conf
->fstab
, value
);
1716 static int set_config_mount_auto(const char *key
, const char *value
,
1717 struct lxc_conf
*lxc_conf
, void *data
)
1719 char *autos
, *autoptr
, *sptr
, *token
;
1726 } allowed_auto_mounts
[] = {
1727 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1728 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1729 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1730 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1731 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1732 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1733 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1734 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1735 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1736 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1737 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1738 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1739 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1740 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1741 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1742 /* NB: For adding anything that is just a single on/off, but has
1743 * no options: keep mask and flag identical and just define the
1744 * enum value as an unused bit so far
1749 if (lxc_config_value_empty(value
)) {
1750 lxc_conf
->auto_mounts
= 0;
1754 autos
= strdup(value
);
1756 SYSERROR("failed to dup '%s'", value
);
1760 for (autoptr
= autos
;; autoptr
= NULL
) {
1761 token
= strtok_r(autoptr
, " \t", &sptr
);
1767 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1768 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1772 if (!allowed_auto_mounts
[i
].token
) {
1773 ERROR("Invalid filesystem to automount: %s", token
);
1777 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1778 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1785 static int set_config_mount(const char *key
, const char *value
,
1786 struct lxc_conf
*lxc_conf
, void *data
)
1789 struct lxc_list
*mntlist
;
1791 if (lxc_config_value_empty(value
))
1792 return lxc_clear_mount_entries(lxc_conf
);
1794 mntlist
= malloc(sizeof(*mntlist
));
1798 mntelem
= strdup(value
);
1803 mntlist
->elem
= mntelem
;
1805 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1810 static int set_config_cap_keep(const char *key
, const char *value
,
1811 struct lxc_conf
*lxc_conf
, void *data
)
1813 char *keepcaps
, *keepptr
, *sptr
, *token
;
1814 struct lxc_list
*keeplist
;
1817 if (lxc_config_value_empty(value
))
1818 return lxc_clear_config_keepcaps(lxc_conf
);
1820 keepcaps
= strdup(value
);
1822 SYSERROR("failed to dup '%s'", value
);
1826 /* In case several capability keep is specified in a single line
1827 * split these caps in a single element for the list.
1829 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1830 token
= strtok_r(keepptr
, " \t", &sptr
);
1836 if (!strcmp(token
, "none"))
1837 lxc_clear_config_keepcaps(lxc_conf
);
1839 keeplist
= malloc(sizeof(*keeplist
));
1841 SYSERROR("failed to allocate keepcap list");
1845 keeplist
->elem
= strdup(token
);
1846 if (!keeplist
->elem
) {
1847 SYSERROR("failed to dup '%s'", token
);
1852 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1860 static int set_config_cap_drop(const char *key
, const char *value
,
1861 struct lxc_conf
*lxc_conf
, void *data
)
1863 char *dropcaps
, *dropptr
, *sptr
, *token
;
1864 struct lxc_list
*droplist
;
1867 if (lxc_config_value_empty(value
))
1868 return lxc_clear_config_caps(lxc_conf
);
1870 dropcaps
= strdup(value
);
1872 SYSERROR("failed to dup '%s'", value
);
1876 /* In case several capability drop is specified in a single line
1877 * split these caps in a single element for the list.
1879 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1880 token
= strtok_r(dropptr
, " \t", &sptr
);
1886 droplist
= malloc(sizeof(*droplist
));
1888 SYSERROR("failed to allocate drop list");
1892 droplist
->elem
= strdup(token
);
1893 if (!droplist
->elem
) {
1894 SYSERROR("failed to dup '%s'", token
);
1899 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1907 static int set_config_console(const char *key
, const char *value
,
1908 struct lxc_conf
*lxc_conf
, void *data
)
1910 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1913 static int set_config_console_logfile(const char *key
, const char *value
,
1914 struct lxc_conf
*lxc_conf
, void *data
)
1916 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1919 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1921 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1923 update_hwaddr(line
);
1925 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1926 char *tmp
= realloc(conf
->unexpanded_config
,
1927 conf
->unexpanded_alloced
+ 1024);
1931 if (!conf
->unexpanded_config
)
1933 conf
->unexpanded_config
= tmp
;
1934 conf
->unexpanded_alloced
+= 1024;
1936 strcat(conf
->unexpanded_config
, line
);
1937 conf
->unexpanded_len
+= linelen
;
1938 if (line
[linelen
- 1] != '\n') {
1939 strcat(conf
->unexpanded_config
, "\n");
1940 conf
->unexpanded_len
++;
1945 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
1947 struct dirent
*direntp
;
1949 char path
[MAXPATHLEN
];
1953 dir
= opendir(dirp
);
1955 SYSERROR("failed to open '%s'", dirp
);
1959 while ((direntp
= readdir(dir
))) {
1964 fnam
= direntp
->d_name
;
1965 if (!strcmp(fnam
, "."))
1968 if (!strcmp(fnam
, ".."))
1972 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
1974 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
1975 if (len
< 0 || len
>= MAXPATHLEN
) {
1976 ERROR("lxc.include filename too long under '%s'", dirp
);
1981 ret
= lxc_config_read(path
, lxc_conf
, true);
1989 WARN("lxc.include dir: failed to close directory");
1994 static int set_config_includefiles(const char *key
, const char *value
,
1995 struct lxc_conf
*lxc_conf
, void *data
)
1997 /* Set config value to default. */
1998 if (lxc_config_value_empty(value
)) {
1999 clr_config_includefiles(key
, lxc_conf
, NULL
);
2003 /* Parse new config value. */
2005 return do_includedir(value
, lxc_conf
);
2007 return lxc_config_read(value
, lxc_conf
, true);
2010 static int set_config_rootfs(const char *key
, const char *value
,
2011 struct lxc_conf
*lxc_conf
, void *data
)
2013 return set_config_path_item(&lxc_conf
->rootfs
.path
, value
);
2016 static int set_config_rootfs_mount(const char *key
, const char *value
,
2017 struct lxc_conf
*lxc_conf
, void *data
)
2019 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2022 static int set_config_rootfs_options(const char *key
, const char *value
,
2023 struct lxc_conf
*lxc_conf
, void *data
)
2025 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2028 static int set_config_rootfs_backend(const char *key
, const char *value
,
2029 struct lxc_conf
*lxc_conf
, void *data
)
2031 if (lxc_config_value_empty(value
)) {
2032 free(lxc_conf
->rootfs
.bdev_type
);
2033 lxc_conf
->rootfs
.bdev_type
= NULL
;
2037 if (!is_valid_bdev_type(value
)) {
2038 ERROR("Bad rootfs.backend: '%s'", value
);
2042 return set_config_string_item(&lxc_conf
->rootfs
.bdev_type
, value
);
2045 static int set_config_utsname(const char *key
, const char *value
,
2046 struct lxc_conf
*lxc_conf
, void *data
)
2048 struct utsname
*utsname
;
2050 if (lxc_config_value_empty(value
)) {
2051 clr_config_utsname(key
, lxc_conf
, NULL
);
2055 utsname
= malloc(sizeof(*utsname
));
2057 SYSERROR("failed to allocate memory");
2061 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2062 ERROR("node name '%s' is too long", value
);
2067 strcpy(utsname
->nodename
, value
);
2068 free(lxc_conf
->utsname
);
2069 lxc_conf
->utsname
= utsname
;
2074 struct parse_line_conf
{
2075 struct lxc_conf
*conf
;
2079 static int parse_line(char *buffer
, void *data
)
2081 struct lxc_config_t
*config
;
2082 char *dot
, *key
, *line
, *linep
, *value
;
2083 struct parse_line_conf
*plc
= data
;
2086 if (lxc_is_line_empty(buffer
))
2089 /* we have to dup the buffer otherwise, at the re-exec for
2090 * reboot we modified the original string on the stack by
2091 * replacing '=' by '\0' below
2093 linep
= line
= strdup(buffer
);
2095 SYSERROR("failed to allocate memory for '%s'", buffer
);
2099 if (!plc
->from_include
)
2100 if ((ret
= append_unexp_config_line(line
, plc
->conf
)))
2103 line
+= lxc_char_left_gc(line
, strlen(line
));
2105 /* ignore comments */
2109 /* martian option - don't add it to the config itself */
2110 if (strncmp(line
, "lxc.", 4))
2115 dot
= strstr(line
, "=");
2117 ERROR("invalid configuration line: %s", line
);
2125 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2127 value
+= lxc_char_left_gc(value
, strlen(value
));
2128 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2130 if (*value
== '\'' || *value
== '\"') {
2131 size_t len
= strlen(value
);
2132 if (len
> 1 && value
[len
- 1] == *value
) {
2133 value
[len
- 1] = '\0';
2138 config
= lxc_getconfig(key
);
2140 ERROR("unknown key %s", key
);
2144 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2151 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2153 struct parse_line_conf c
;
2156 c
.from_include
= false;
2158 return parse_line(buffer
, &c
);
2161 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2163 struct parse_line_conf c
;
2166 c
.from_include
= from_include
;
2168 if (access(file
, R_OK
) == -1) {
2172 /* Catch only the top level config file name in the structure */
2174 conf
->rcfile
= strdup(file
);
2176 return lxc_file_for_each_line(file
, parse_line
, &c
);
2179 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2181 struct lxc_list
*dent
;
2183 dent
= malloc(sizeof(struct lxc_list
));
2188 lxc_list_add_tail(defines
, dent
);
2192 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2194 struct lxc_list
*it
, *next
;
2197 lxc_list_for_each(it
, defines
) {
2198 ret
= lxc_config_readline(it
->elem
, conf
);
2203 lxc_list_for_each_safe(it
, defines
, next
) {
2211 signed long lxc_config_parse_arch(const char *arch
)
2213 #if HAVE_SYS_PERSONALITY_H
2219 { "x86", PER_LINUX32
},
2220 { "linux32", PER_LINUX32
},
2221 { "i386", PER_LINUX32
},
2222 { "i486", PER_LINUX32
},
2223 { "i586", PER_LINUX32
},
2224 { "i686", PER_LINUX32
},
2225 { "athlon", PER_LINUX32
},
2226 { "mips", PER_LINUX32
},
2227 { "mipsel", PER_LINUX32
},
2228 { "ppc", PER_LINUX32
},
2229 { "arm", PER_LINUX32
},
2230 { "armv7l", PER_LINUX32
},
2231 { "armhf", PER_LINUX32
},
2232 { "armel", PER_LINUX32
},
2233 { "powerpc", PER_LINUX32
},
2234 { "linux64", PER_LINUX
},
2235 { "x86_64", PER_LINUX
},
2236 { "amd64", PER_LINUX
},
2237 { "mips64", PER_LINUX
},
2238 { "mips64el", PER_LINUX
},
2239 { "ppc64", PER_LINUX
},
2240 { "ppc64le", PER_LINUX
},
2241 { "ppc64el", PER_LINUX
},
2242 { "powerpc64", PER_LINUX
},
2243 { "s390x", PER_LINUX
},
2244 { "aarch64", PER_LINUX
},
2245 { "arm64", PER_LINUX
},
2247 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2249 for (i
= 0; i
< len
; i
++) {
2250 if (!strcmp(pername
[i
].name
, arch
))
2251 return pername
[i
].per
;
2258 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2260 char *token
, *saveptr
= NULL
;
2266 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2267 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2268 { "LSM", LXC_ATTACH_LSM_EXEC
},
2273 /* For the sake of backward compatibility, drop all privileges
2274 * if none is specified.
2276 for (i
= 0; all_privs
[i
].token
; i
++) {
2277 *flags
|= all_privs
[i
].flag
;
2282 token
= strtok_r(flaglist
, "|", &saveptr
);
2285 for (i
= 0; all_privs
[i
].token
; i
++) {
2286 if (!strcmp(all_privs
[i
].token
, token
))
2287 aflag
= all_privs
[i
].flag
;
2294 token
= strtok_r(NULL
, "|", &saveptr
);
2300 /* Write out a configuration file. */
2301 void write_config(FILE *fout
, struct lxc_conf
*c
)
2304 size_t len
= c
->unexpanded_len
;
2309 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2311 SYSERROR("Error writing configuration file");
2314 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2321 len
= strlen(key
) + strlen(v
) + 4;
2324 if (lxc_config_value_empty(v
))
2325 ret
= snprintf(tmp
, len
, "%s =", key
);
2327 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2328 if (ret
< 0 || ret
>= len
)
2331 /* Save the line verbatim into unexpanded_conf */
2332 if (append_unexp_config_line(tmp
, conf
))
2338 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2342 char *lstart
= conf
->unexpanded_config
;
2344 if (!conf
->unexpanded_config
)
2348 lend
= strchr(lstart
, '\n');
2351 lend
= lstart
+ strlen(lstart
);
2354 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2359 v
= lstart
[strlen(key
)];
2360 if (!isspace(v
) && v
!= '=') {
2365 conf
->unexpanded_len
-= (lend
- lstart
);
2366 if (*lend
== '\0') {
2370 memmove(lstart
, lend
, strlen(lend
) + 1);
2374 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2375 const char *newpath
, const char *oldname
,
2376 const char *newname
, const char *ovldir
)
2379 char *lend
, *newdir
, *olddir
, *p
, *q
;
2380 size_t newdirlen
, olddirlen
;
2381 char *lstart
= conf
->unexpanded_config
;
2382 const char *key
= "lxc.mount.entry";
2384 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2385 olddir
= alloca(olddirlen
+ 1);
2386 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2388 if (ret
< 0 || ret
>= olddirlen
+ 1) {
2389 ERROR("failed to create string");
2393 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2394 newdir
= alloca(newdirlen
+ 1);
2395 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2397 if (ret
< 0 || ret
>= newdirlen
+ 1) {
2398 ERROR("failed to create string");
2402 if (!conf
->unexpanded_config
)
2406 lend
= strchr(lstart
, '\n');
2408 lend
= lstart
+ strlen(lstart
);
2412 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2415 p
= strchr(lstart
+ strlen(key
), '=');
2426 /* Whenever an lxc.mount.entry entry is found in a line we check
2427 * if the substring " overlay" or the substring " aufs" is
2428 * present before doing any further work. We check for "
2429 * overlay" and " aufs" since both substrings need to have at
2430 * least one space before them in a valid overlay
2431 * lxc.mount.entry (/A B overlay). When the space before is
2432 * missing it is very likely that these substrings are part of a
2433 * path or something else. (Checking q >= lend ensures that we
2434 * only count matches in the current line.) */
2435 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2436 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2439 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2442 /* replace the olddir with newdir */
2443 if (olddirlen
>= newdirlen
) {
2444 size_t diff
= olddirlen
- newdirlen
;
2445 memcpy(q
, newdir
, newdirlen
);
2446 if (olddirlen
!= newdirlen
) {
2447 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2448 strlen(q
) - newdirlen
- diff
+ 1);
2450 conf
->unexpanded_len
-= diff
;
2454 size_t diff
= newdirlen
- olddirlen
;
2455 size_t oldlen
= conf
->unexpanded_len
;
2456 size_t newlen
= oldlen
+ diff
;
2457 size_t poffset
= q
- conf
->unexpanded_config
;
2459 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2461 ERROR("Out of memory");
2464 conf
->unexpanded_len
= newlen
;
2465 conf
->unexpanded_alloced
= newlen
+ 1;
2466 new[newlen
- 1] = '\0';
2467 lend
= new + (lend
- conf
->unexpanded_config
);
2468 /* move over the remainder to make room for the newdir
2470 memmove(new + poffset
+ newdirlen
,
2471 new + poffset
+ olddirlen
,
2472 oldlen
- poffset
- olddirlen
+ 1);
2473 conf
->unexpanded_config
= new;
2474 memcpy(new + poffset
, newdir
, newdirlen
);
2484 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2485 const char *newpath
, const char *oldname
,
2486 const char *newname
)
2489 char *lend
, *newdir
, *olddir
, *p
;
2490 char *lstart
= conf
->unexpanded_config
;
2491 size_t newdirlen
, olddirlen
;
2492 const char *key
= "lxc.hook";
2494 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2495 olddir
= alloca(olddirlen
+ 1);
2496 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2497 if (ret
< 0 || ret
>= olddirlen
+ 1) {
2498 ERROR("failed to create string");
2502 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2503 newdir
= alloca(newdirlen
+ 1);
2504 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2505 if (ret
< 0 || ret
>= newdirlen
+ 1) {
2506 ERROR("failed to create string");
2509 if (!conf
->unexpanded_config
)
2512 lend
= strchr(lstart
, '\n');
2514 lend
= lstart
+ strlen(lstart
);
2518 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2521 p
= strchr(lstart
+ strlen(key
), '=');
2532 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2535 /* replace the olddir with newdir */
2536 if (olddirlen
>= newdirlen
) {
2537 size_t diff
= olddirlen
- newdirlen
;
2538 memcpy(p
, newdir
, newdirlen
);
2539 if (olddirlen
!= newdirlen
) {
2540 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2541 strlen(p
) - newdirlen
- diff
+ 1);
2543 conf
->unexpanded_len
-= diff
;
2547 size_t diff
= newdirlen
- olddirlen
;
2548 size_t oldlen
= conf
->unexpanded_len
;
2549 size_t newlen
= oldlen
+ diff
;
2550 size_t poffset
= p
- conf
->unexpanded_config
;
2552 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2554 ERROR("failed to allocate memory");
2557 conf
->unexpanded_len
= newlen
;
2558 conf
->unexpanded_alloced
= newlen
+ 1;
2559 new[newlen
- 1] = '\0';
2560 lend
= new + (lend
- conf
->unexpanded_config
);
2561 /* move over the remainder to make room for the newdir
2563 memmove(new + poffset
+ newdirlen
,
2564 new + poffset
+ olddirlen
,
2565 oldlen
- poffset
- olddirlen
+ 1);
2566 conf
->unexpanded_config
= new;
2567 memcpy(new + poffset
, newdir
, newdirlen
);
2580 ERROR("Error writing to new config"); \
2586 * This is called only from clone. We wish to update all hwaddrs in the
2587 * unexpanded config file. We can't/don't want to update any which come from
2588 * lxc.includes (there shouldn't be any).
2589 * We can't just walk the c->lxc-conf->network list because that includes netifs
2590 * from the include files. So we update the ones which we find in the unexp
2591 * config file, then find the original macaddr in the conf->network, and update
2592 * that to the same value.
2594 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2596 char *lend
, *p
, *p2
;
2597 struct lxc_list
*it
;
2598 char *lstart
= conf
->unexpanded_config
;
2600 if (!conf
->unexpanded_config
)
2604 char newhwaddr
[18], oldhwaddr
[17];
2606 lend
= strchr(lstart
, '\n');
2608 lend
= lstart
+ strlen(lstart
);
2612 if (!lxc_config_net_hwaddr(lstart
)) {
2617 p
= strchr(lstart
, '=');
2630 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2633 if ((p2
- p
) != 17) {
2634 WARN("Bad hwaddr entry");
2639 memcpy(oldhwaddr
, p
, 17);
2641 if (!new_hwaddr(newhwaddr
))
2644 memcpy(p
, newhwaddr
, 17);
2645 lxc_list_for_each(it
, &conf
->network
)
2647 struct lxc_netdev
*n
= it
->elem
;
2648 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2649 memcpy(n
->hwaddr
, newhwaddr
, 17);
2658 static int set_config_ephemeral(const char *key
, const char *value
,
2659 struct lxc_conf
*lxc_conf
, void *data
)
2661 /* Set config value to default. */
2662 if (lxc_config_value_empty(value
)) {
2663 lxc_conf
->ephemeral
= 0;
2667 /* Parse new config value. */
2668 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2671 if (lxc_conf
->ephemeral
> 1) {
2673 "Wrong value for lxc.ephemeral. Can only be set to 0 or 1");
2680 static int set_config_syslog(const char *key
, const char *value
,
2681 struct lxc_conf
*lxc_conf
, void *data
)
2685 /* Clear any previously set value. */
2686 if (lxc_conf
->syslog
) {
2687 free(lxc_conf
->syslog
);
2688 lxc_conf
->syslog
= NULL
;
2691 /* Check if value is empty. */
2692 if (lxc_config_value_empty(value
))
2696 facility
= lxc_syslog_priority_to_int(value
);
2697 if (facility
== -EINVAL
) {
2698 ERROR("Wrong value for lxc.syslog.");
2702 lxc_log_syslog(facility
);
2703 return set_config_string_item(&lxc_conf
->syslog
, value
);
2706 static int set_config_no_new_privs(const char *key
, const char *value
,
2707 struct lxc_conf
*lxc_conf
, void *data
)
2711 /* Set config value to default. */
2712 if (lxc_config_value_empty(value
)) {
2713 lxc_conf
->no_new_privs
= false;
2717 /* Parse new config value. */
2718 if (lxc_safe_uint(value
, &v
) < 0)
2722 ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 "
2727 lxc_conf
->no_new_privs
= v
? true : false;
2732 /* Callbacks to get configuration items. */
2733 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2734 struct lxc_conf
*c
, void *data
)
2741 memset(retv
, 0, inlen
);
2743 #if HAVE_SYS_PERSONALITY_H
2746 switch (c
->personality
) {
2748 strprint(retv
, inlen
, "i686");
2751 strprint(retv
, inlen
, "x86_64");
2761 static int get_config_pts(const char *key
, char *retv
, int inlen
,
2762 struct lxc_conf
*c
, void *data
)
2764 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2767 static int get_config_tty(const char *key
, char *retv
, int inlen
,
2768 struct lxc_conf
*c
, void *data
)
2770 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2773 static int get_config_ttydir(const char *key
, char *retv
, int inlen
,
2774 struct lxc_conf
*c
, void *data
)
2776 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2779 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2780 struct lxc_conf
*c
, void *data
)
2782 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2785 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2786 int inlen
, struct lxc_conf
*c
,
2789 return lxc_get_conf_int(c
, retv
, inlen
,
2790 c
->lsm_aa_allow_incomplete
);
2793 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2794 struct lxc_conf
*c
, void *data
)
2796 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2800 * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list,
2801 * then just the value(s) will be printed. Since there still could be
2802 * more than one, it is newline-separated.
2803 * (Maybe that's ambigous, since some values, i.e. devices.list, will
2804 * already have newlines?)
2805 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed,
2806 * in 'lxc.cgroup.subsystem.key = value' format.
2808 static int get_config_cgroup(const char *key
, char *retv
, int inlen
,
2809 struct lxc_conf
*c
, void *data
)
2811 struct lxc_list
*it
;
2814 bool get_all
= false;
2819 memset(retv
, 0, inlen
);
2821 if (!strcmp(key
, "lxc.cgroup"))
2823 else if (!strncmp(key
, "lxc.cgroup.", 11))
2828 lxc_list_for_each(it
, &c
->cgroup
) {
2829 struct lxc_cgroup
*cg
= it
->elem
;
2831 strprint(retv
, inlen
, "lxc.cgroup.%s = %s\n", cg
->subsystem
, cg
->value
);
2832 } else if (!strcmp(cg
->subsystem
, key
)) {
2833 strprint(retv
, inlen
, "%s\n", cg
->value
);
2840 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2841 struct lxc_conf
*c
, void *data
)
2843 struct lxc_list
*it
;
2844 int len
, listlen
, ret
;
2846 /* "u 1000 1000000 65536"
2848 * let's render this as
2866 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2867 char buf
[__LXC_IDMAP_STR_BUF
];
2872 memset(retv
, 0, inlen
);
2874 listlen
= lxc_list_len(&c
->id_map
);
2875 lxc_list_for_each(it
, &c
->id_map
)
2877 struct id_map
*map
= it
->elem
;
2878 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
2879 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
2880 map
->nsid
, map
->hostid
, map
->range
);
2881 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
2884 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
2889 static int get_config_loglevel(const char *key
, char *retv
, int inlen
,
2890 struct lxc_conf
*c
, void *data
)
2893 v
= lxc_log_priority_to_string(c
->loglevel
);
2894 return lxc_get_conf_str(retv
, inlen
, v
);
2897 static int get_config_logfile(const char *key
, char *retv
, int inlen
,
2898 struct lxc_conf
*c
, void *data
)
2900 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
2903 static int get_config_fstab(const char *key
, char *retv
, int inlen
,
2904 struct lxc_conf
*c
, void *data
)
2906 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
2909 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
2910 struct lxc_conf
*c
, void *data
)
2912 int len
, fulllen
= 0;
2913 const char *sep
= "";
2918 memset(retv
, 0, inlen
);
2920 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
2923 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
2924 case LXC_AUTO_PROC_MIXED
:
2925 strprint(retv
, inlen
, "%sproc:mixed", sep
);
2928 case LXC_AUTO_PROC_RW
:
2929 strprint(retv
, inlen
, "%sproc:rw", sep
);
2936 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
2937 case LXC_AUTO_SYS_RO
:
2938 strprint(retv
, inlen
, "%ssys:ro", sep
);
2941 case LXC_AUTO_SYS_RW
:
2942 strprint(retv
, inlen
, "%ssys:rw", sep
);
2945 case LXC_AUTO_SYS_MIXED
:
2946 strprint(retv
, inlen
, "%ssys:mixed", sep
);
2953 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
2954 case LXC_AUTO_CGROUP_NOSPEC
:
2955 strprint(retv
, inlen
, "%scgroup", sep
);
2958 case LXC_AUTO_CGROUP_MIXED
:
2959 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
2962 case LXC_AUTO_CGROUP_RO
:
2963 strprint(retv
, inlen
, "%scgroup:ro", sep
);
2966 case LXC_AUTO_CGROUP_RW
:
2967 strprint(retv
, inlen
, "%scgroup:rw", sep
);
2970 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
2971 strprint(retv
, inlen
, "%scgroup-full", sep
);
2974 case LXC_AUTO_CGROUP_FULL_MIXED
:
2975 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
2978 case LXC_AUTO_CGROUP_FULL_RO
:
2979 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
2982 case LXC_AUTO_CGROUP_FULL_RW
:
2983 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
2993 static int get_config_mount(const char *key
, char *retv
, int inlen
,
2994 struct lxc_conf
*c
, void *data
)
2996 int len
, fulllen
= 0;
2997 struct lxc_list
*it
;
3002 memset(retv
, 0, inlen
);
3004 lxc_list_for_each(it
, &c
->mount_list
)
3006 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3012 static int get_config_rootfs(const char *key
, char *retv
, int inlen
,
3013 struct lxc_conf
*c
, void *data
)
3015 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3018 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3019 struct lxc_conf
*c
, void *data
)
3021 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3024 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3025 struct lxc_conf
*c
, void *data
)
3027 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3030 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3031 struct lxc_conf
*c
, void *data
)
3033 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.bdev_type
);
3036 static int get_config_utsname(const char *key
, char *retv
, int inlen
,
3037 struct lxc_conf
*c
, void *data
)
3039 return lxc_get_conf_str(
3041 c
->utsname
? c
->utsname
->nodename
: NULL
);
3044 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3045 struct lxc_conf
*c
, void *data
)
3048 int len
, fulllen
= 0, found
= -1;
3049 struct lxc_list
*it
;
3052 /* "lxc.hook.mount" */
3053 subkey
= strchr(key
, '.');
3055 subkey
= strchr(subkey
+ 1, '.');
3061 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3062 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3073 memset(retv
, 0, inlen
);
3075 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3076 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3081 static int get_config_net(const char *key
, char *retv
, int inlen
,
3082 struct lxc_conf
*c
, void *data
)
3084 int len
, fulllen
= 0;
3085 struct lxc_list
*it
;
3090 memset(retv
, 0, inlen
);
3092 lxc_list_for_each(it
, &c
->network
) {
3093 struct lxc_netdev
*n
= it
->elem
;
3094 const char *t
= lxc_net_type_to_str(n
->type
);
3095 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3101 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3102 struct lxc_conf
*c
, void *data
)
3104 int len
, fulllen
= 0;
3105 struct lxc_list
*it
;
3110 memset(retv
, 0, inlen
);
3112 lxc_list_for_each(it
, &c
->caps
) {
3113 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3118 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3119 struct lxc_conf
*c
, void *data
)
3121 int len
, fulllen
= 0;
3122 struct lxc_list
*it
;
3127 memset(retv
, 0, inlen
);
3129 lxc_list_for_each(it
, &c
->keepcaps
) {
3130 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3135 static int get_config_console(const char *key
, char *retv
, int inlen
,
3136 struct lxc_conf
*c
, void *data
)
3138 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3141 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3142 struct lxc_conf
*c
, void *data
)
3144 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3147 static int get_config_seccomp(const char *key
, char *retv
, int inlen
,
3148 struct lxc_conf
*c
, void *data
)
3150 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3153 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3154 struct lxc_conf
*c
, void *data
)
3156 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3159 static int get_config_haltsignal(const char *key
, char *retv
, int inlen
,
3160 struct lxc_conf
*c
, void *data
)
3162 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3165 static int get_config_rebootsignal(const char *key
, char *retv
, int inlen
,
3166 struct lxc_conf
*c
, void *data
)
3168 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3171 static int get_config_stopsignal(const char *key
, char *retv
, int inlen
,
3172 struct lxc_conf
*c
, void *data
)
3174 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3177 static int get_config_start(const char *key
, char *retv
, int inlen
,
3178 struct lxc_conf
*c
, void *data
)
3180 if (strcmp(key
+ 10, "auto") == 0)
3181 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3182 else if (strcmp(key
+ 10, "delay") == 0)
3183 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3184 else if (strcmp(key
+ 10, "order") == 0)
3185 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3190 static int get_config_syslog(const char *key
, char *retv
, int inlen
,
3191 struct lxc_conf
*c
, void *data
)
3193 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3196 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3197 struct lxc_conf
*c
, void *data
)
3199 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3202 static int get_config_group(const char *key
, char *retv
, int inlen
,
3203 struct lxc_conf
*c
, void *data
)
3205 int len
, fulllen
= 0;
3206 struct lxc_list
*it
;
3211 memset(retv
, 0, inlen
);
3213 lxc_list_for_each(it
, &c
->groups
) {
3214 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3219 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3220 struct lxc_conf
*c
, void *data
)
3222 int len
, fulllen
= 0;
3223 struct lxc_list
*it
;
3228 memset(retv
, 0, inlen
);
3230 lxc_list_for_each(it
, &c
->environment
) {
3231 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3236 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3237 struct lxc_conf
*c
, void *data
)
3239 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3242 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3243 struct lxc_conf
*c
, void *data
)
3245 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3248 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3249 struct lxc_conf
*c
, void *data
)
3251 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3254 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3255 struct lxc_conf
*c
, void *data
)
3257 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3260 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3261 struct lxc_conf
*c
, void *data
)
3263 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3267 * If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3268 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3269 * printed, in 'lxc.prlimit.resource = value' format.
3271 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3272 struct lxc_conf
*c
, void *data
)
3274 int fulllen
= 0, len
;
3275 bool get_all
= false;
3276 struct lxc_list
*it
;
3281 memset(retv
, 0, inlen
);
3283 if (!strcmp(key
, "lxc.prlimit"))
3285 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3290 lxc_list_for_each(it
, &c
->limits
) {
3291 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3292 integers or the word
3295 struct lxc_limit
*lim
= it
->elem
;
3297 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3298 memcpy(buf
, "unlimited", sizeof("unlimited"));
3299 partlen
= sizeof("unlimited") - 1;
3301 partlen
= sprintf(buf
, "%" PRIu64
,
3302 (uint64_t)lim
->limit
.rlim_cur
);
3304 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3305 if (lim
->limit
.rlim_max
== RLIM_INFINITY
) {
3306 memcpy(buf
+ partlen
, ":unlimited",
3307 sizeof(":unlimited"));
3309 sprintf(buf
+ partlen
, ":%" PRIu64
,
3310 (uint64_t)lim
->limit
.rlim_max
);
3315 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3316 lim
->resource
, buf
);
3317 } else if (strcmp(lim
->resource
, key
) == 0) {
3318 strprint(retv
, inlen
, "%s", buf
);
3325 /* Callbacks to clear config items. */
3326 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3329 c
->personality
= -1;
3333 static inline int clr_config_pts(const char *key
, struct lxc_conf
*c
,
3340 static inline int clr_config_tty(const char *key
, struct lxc_conf
*c
,
3347 static inline int clr_config_ttydir(const char *key
, struct lxc_conf
*c
,
3355 static inline int clr_config_apparmor_profile(const char *key
,
3356 struct lxc_conf
*c
, void *data
)
3358 free(c
->lsm_aa_profile
);
3359 c
->lsm_aa_profile
= NULL
;
3363 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3367 c
->lsm_aa_allow_incomplete
= 0;
3371 static inline int clr_config_selinux_context(const char *key
,
3372 struct lxc_conf
*c
, void *data
)
3374 free(c
->lsm_se_context
);
3375 c
->lsm_se_context
= NULL
;
3379 static inline int clr_config_cgroup(const char *key
, struct lxc_conf
*c
,
3382 return lxc_clear_cgroups(c
, key
);
3385 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3388 return lxc_clear_idmaps(c
);
3391 static inline int clr_config_loglevel(const char *key
, struct lxc_conf
*c
,
3394 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3398 static inline int clr_config_logfile(const char *key
, struct lxc_conf
*c
,
3406 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3409 return lxc_clear_mount_entries(c
);
3412 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3415 return lxc_clear_automounts(c
);
3418 static inline int clr_config_fstab(const char *key
, struct lxc_conf
*c
,
3426 static inline int clr_config_rootfs(const char *key
, struct lxc_conf
*c
,
3429 free(c
->rootfs
.path
);
3430 c
->rootfs
.path
= NULL
;
3434 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3437 free(c
->rootfs
.mount
);
3438 c
->rootfs
.mount
= NULL
;
3442 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3445 free(c
->rootfs
.options
);
3446 c
->rootfs
.options
= NULL
;
3450 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3453 free(c
->rootfs
.bdev_type
);
3454 c
->rootfs
.bdev_type
= NULL
;
3458 static inline int clr_config_utsname(const char *key
, struct lxc_conf
*c
,
3466 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3469 return lxc_clear_hooks(c
, key
);
3472 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3475 lxc_free_networks(&c
->network
);
3480 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3483 return lxc_clear_config_caps(c
);
3486 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3489 return lxc_clear_config_keepcaps(c
);
3492 static inline int clr_config_console(const char *key
, struct lxc_conf
*c
,
3495 free(c
->console
.path
);
3496 c
->console
.path
= NULL
;
3500 static inline int clr_config_console_logfile(const char *key
,
3501 struct lxc_conf
*c
, void *data
)
3503 free(c
->console
.log_path
);
3504 c
->console
.log_path
= NULL
;
3508 static inline int clr_config_seccomp(const char *key
, struct lxc_conf
*c
,
3516 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3523 static inline int clr_config_haltsignal(const char *key
, struct lxc_conf
*c
,
3530 static inline int clr_config_rebootsignal(const char *key
, struct lxc_conf
*c
,
3533 c
->rebootsignal
= 0;
3537 static inline int clr_config_stopsignal(const char *key
, struct lxc_conf
*c
,
3544 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3547 if (strcmp(key
+ 10, "auto") == 0)
3549 else if (strcmp(key
+ 10, "delay") == 0)
3551 else if (strcmp(key
+ 10, "order") == 0)
3557 static inline int clr_config_syslog(const char *key
, struct lxc_conf
*c
,
3565 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3568 c
->monitor_unshare
= 0;
3572 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3575 return lxc_clear_groups(c
);
3578 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3581 return lxc_clear_environment(c
);
3584 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3592 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3599 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3606 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3613 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3616 c
->no_new_privs
= false;
3620 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3623 return lxc_clear_limits(c
, key
);
3626 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3629 lxc_clear_includes(c
);
3633 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
3634 struct lxc_conf
*c
, void *data
)
3639 static struct lxc_config_t
*
3640 get_network_config_ops(const char *key
, struct lxc_conf
*lxc_conf
, ssize_t
*idx
)
3642 char *copy
, *idx_start
, *idx_end
;
3643 struct lxc_config_t
*config
= NULL
;
3645 /* check that this is a sensible network key */
3646 if (strncmp("lxc.net.", key
, 8))
3654 if (isdigit(*(key
+ 8))) {
3656 unsigned int tmpidx
;
3659 /* beginning of index string */
3660 idx_start
= (copy
+ 7);
3663 /* end of index string */
3664 idx_end
= strchr((copy
+ 8), '.');
3669 /* parse current index */
3670 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
3676 /* This, of course is utterly nonsensical on so many levels, but
3677 * better safe than sorry.
3678 * (Checking for INT_MAX here is intentional.)
3680 if (tmpidx
== INT_MAX
) {
3682 "number of configured networks would overflow the "
3683 "counter... what are you doing?");
3688 numstrlen
= strlen((idx_start
+ 1));
3690 /* repair configuration key */
3694 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
3695 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
3698 config
= lxc_getconfig(copy
);
3700 ERROR("unknown network configuration key %s", key
);
3708 * Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.'
3709 * was found. So we make sure next comes an integer, find the right callback
3710 * (by rewriting the key), and call it.
3712 static int set_config_net_nic(const char *key
, const char *value
,
3713 struct lxc_conf
*lxc_conf
, void *data
)
3715 struct lxc_config_t
*config
;
3716 struct lxc_netdev
*netdev
;
3719 if (lxc_config_value_empty(value
))
3720 return clr_config_net_nic(key
, lxc_conf
, data
);
3722 config
= get_network_config_ops(key
, lxc_conf
, &idx
);
3723 if (!config
|| idx
< 0)
3726 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
3730 return config
->set(key
, value
, lxc_conf
, netdev
);
3734 * Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.'
3735 * was found. So we make sure next comes an integer, find the right callback
3736 * (by rewriting the key), and call it.
3738 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
3741 const char *idxstring
;
3742 struct lxc_config_t
*config
;
3743 struct lxc_netdev
*netdev
;
3746 /* If we get passed "lxc.net.<n>" we clear the whole network. */
3747 if (strncmp("lxc.net.", key
, 8))
3750 idxstring
= key
+ 8;
3751 /* The left conjunct is pretty self-explanatory. The right conjunct
3752 * checks whether the two pointers are equal. If they are we now that
3753 * this is not a key that is namespaced any further and so we are
3754 * supposed to clear the whole network.
3756 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
3757 unsigned int rmnetdevidx
;
3759 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
3762 /* Remove network from network list. */
3763 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
3767 config
= get_network_config_ops(key
, lxc_conf
, &idx
);
3768 if (!config
|| idx
< 0)
3771 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
3775 return config
->clr(key
, lxc_conf
, netdev
);
3778 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
3781 struct lxc_netdev
*netdev
;
3795 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
3798 struct lxc_netdev
*netdev
;
3808 netdev
->name
= NULL
;
3813 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
3816 struct lxc_netdev
*netdev
;
3830 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
3833 struct lxc_netdev
*netdev
;
3843 netdev
->link
= NULL
;
3848 static int clr_config_net_macvlan_mode(const char *key
,
3849 struct lxc_conf
*lxc_conf
, void *data
)
3851 struct lxc_netdev
*netdev
;
3860 if (netdev
->type
!= LXC_NET_MACVLAN
)
3863 netdev
->priv
.macvlan_attr
.mode
= -1;
3868 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
3871 struct lxc_netdev
*netdev
;
3880 free(netdev
->priv
.veth_attr
.pair
);
3881 netdev
->priv
.veth_attr
.pair
= NULL
;
3886 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
3889 struct lxc_netdev
*netdev
;
3898 free(netdev
->upscript
);
3899 netdev
->upscript
= NULL
;
3904 static int clr_config_net_script_down(const char *key
,
3905 struct lxc_conf
*lxc_conf
, void *data
)
3907 struct lxc_netdev
*netdev
;
3916 free(netdev
->downscript
);
3917 netdev
->downscript
= NULL
;
3922 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
3925 struct lxc_netdev
*netdev
;
3934 free(netdev
->hwaddr
);
3935 netdev
->hwaddr
= NULL
;
3940 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
3943 struct lxc_netdev
*netdev
;
3958 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
3961 struct lxc_netdev
*netdev
;
3970 netdev
->priv
.vlan_attr
.vid
= 0;
3975 static int clr_config_net_ipv4_gateway(const char *key
,
3976 struct lxc_conf
*lxc_conf
, void *data
)
3978 struct lxc_netdev
*netdev
;
3987 free(netdev
->ipv4_gateway
);
3988 netdev
->ipv4_gateway
= NULL
;
3993 static int clr_config_net_ipv4(const char *key
, struct lxc_conf
*lxc_conf
,
3996 struct lxc_netdev
*netdev
;
3997 struct lxc_list
*cur
, *next
;
4006 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4015 static int clr_config_net_ipv6_gateway(const char *key
,
4016 struct lxc_conf
*lxc_conf
, void *data
)
4018 struct lxc_netdev
*netdev
;
4027 free(netdev
->ipv6_gateway
);
4028 netdev
->ipv6_gateway
= NULL
;
4033 static int clr_config_net_ipv6(const char *key
, struct lxc_conf
*lxc_conf
,
4036 struct lxc_netdev
*netdev
;
4037 struct lxc_list
*cur
, *next
;
4046 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4055 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4056 struct lxc_conf
*c
, void *data
)
4058 struct lxc_config_t
*config
;
4059 struct lxc_netdev
*netdev
;
4062 config
= get_network_config_ops(key
, c
, &idx
);
4063 if (!config
|| idx
< 0)
4066 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4070 return config
->get(key
, retv
, inlen
, c
, netdev
);
4073 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4074 struct lxc_conf
*c
, void *data
)
4076 int len
, fulllen
= 0;
4077 struct lxc_netdev
*netdev
;
4082 memset(retv
, 0, inlen
);
4091 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4096 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4097 struct lxc_conf
*c
, void *data
)
4099 int len
, fulllen
= 0;
4100 struct lxc_netdev
*netdev
;
4105 memset(retv
, 0, inlen
);
4114 if (netdev
->flags
& IFF_UP
)
4115 strprint(retv
, inlen
, "up");
4120 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4121 struct lxc_conf
*c
, void *data
)
4123 int len
, fulllen
= 0;
4124 struct lxc_netdev
*netdev
;
4129 memset(retv
, 0, inlen
);
4139 strprint(retv
, inlen
, "%s", netdev
->link
);
4144 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4145 struct lxc_conf
*c
, void *data
)
4147 int len
, fulllen
= 0;
4148 struct lxc_netdev
*netdev
;
4153 memset(retv
, 0, inlen
);
4163 strprint(retv
, inlen
, "%s", netdev
->name
);
4168 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4169 struct lxc_conf
*c
, void *data
)
4172 int len
, fulllen
= 0;
4173 struct lxc_netdev
*netdev
;
4178 memset(retv
, 0, inlen
);
4187 if (netdev
->type
!= LXC_NET_MACVLAN
)
4190 switch (netdev
->priv
.macvlan_attr
.mode
) {
4191 case MACVLAN_MODE_PRIVATE
:
4194 case MACVLAN_MODE_VEPA
:
4197 case MACVLAN_MODE_BRIDGE
:
4200 case MACVLAN_MODE_PASSTHRU
:
4208 strprint(retv
, inlen
, "%s", mode
);
4213 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4214 struct lxc_conf
*c
, void *data
)
4216 int len
, fulllen
= 0;
4217 struct lxc_netdev
*netdev
;
4222 memset(retv
, 0, inlen
);
4231 if (netdev
->type
!= LXC_NET_VETH
)
4234 strprint(retv
, inlen
, "%s",
4235 netdev
->priv
.veth_attr
.pair
? netdev
->priv
.veth_attr
.pair
4236 : netdev
->priv
.veth_attr
.veth1
);
4241 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4242 struct lxc_conf
*c
, void *data
)
4244 int len
, fulllen
= 0;
4245 struct lxc_netdev
*netdev
;
4250 memset(retv
, 0, inlen
);
4259 if (netdev
->upscript
)
4260 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4265 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4266 struct lxc_conf
*c
, void *data
)
4268 int len
, fulllen
= 0;
4269 struct lxc_netdev
*netdev
;
4274 memset(retv
, 0, inlen
);
4283 if (netdev
->downscript
)
4284 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4289 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4290 struct lxc_conf
*c
, void *data
)
4292 int len
, fulllen
= 0;
4293 struct lxc_netdev
*netdev
;
4298 memset(retv
, 0, inlen
);
4308 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4313 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4314 struct lxc_conf
*c
, void *data
)
4316 int len
, fulllen
= 0;
4317 struct lxc_netdev
*netdev
;
4322 memset(retv
, 0, inlen
);
4332 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4337 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4338 struct lxc_conf
*c
, void *data
)
4340 int len
, fulllen
= 0;
4341 struct lxc_netdev
*netdev
;
4346 memset(retv
, 0, inlen
);
4355 if (netdev
->type
!= LXC_NET_VLAN
)
4358 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4363 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4364 struct lxc_conf
*c
, void *data
)
4366 int len
, fulllen
= 0;
4367 char buf
[INET_ADDRSTRLEN
];
4368 struct lxc_netdev
*netdev
;
4373 memset(retv
, 0, inlen
);
4382 if (netdev
->ipv4_gateway_auto
) {
4383 strprint(retv
, inlen
, "auto");
4384 } else if (netdev
->ipv4_gateway
) {
4385 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4386 strprint(retv
, inlen
, "%s", buf
);
4392 static int get_config_net_ipv4(const char *key
, char *retv
, int inlen
,
4393 struct lxc_conf
*c
, void *data
)
4395 int len
, fulllen
= 0;
4397 char buf
[INET_ADDRSTRLEN
];
4398 struct lxc_netdev
*netdev
;
4399 struct lxc_list
*it
;
4404 memset(retv
, 0, inlen
);
4413 listlen
= lxc_list_len(&netdev
->ipv4
);
4414 lxc_list_for_each(it
, &netdev
->ipv4
) {
4415 struct lxc_inetdev
*i
= it
->elem
;
4416 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4417 strprint(retv
, inlen
, "%s/%d%s", buf
, i
->prefix
,
4418 (listlen
-- > 1) ? "\n" : "");
4424 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4425 struct lxc_conf
*c
, void *data
)
4427 int len
, fulllen
= 0;
4428 char buf
[INET6_ADDRSTRLEN
];
4429 struct lxc_netdev
*netdev
;
4434 memset(retv
, 0, inlen
);
4443 if (netdev
->ipv6_gateway_auto
) {
4444 strprint(retv
, inlen
, "auto");
4445 } else if (netdev
->ipv6_gateway
) {
4446 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4447 strprint(retv
, inlen
, "%s", buf
);
4453 static int get_config_net_ipv6(const char *key
, char *retv
, int inlen
,
4454 struct lxc_conf
*c
, void *data
)
4456 int len
, fulllen
= 0;
4458 char buf
[INET6_ADDRSTRLEN
];
4459 struct lxc_netdev
*netdev
;
4460 struct lxc_list
*it
;
4465 memset(retv
, 0, inlen
);
4474 listlen
= lxc_list_len(&netdev
->ipv6
);
4475 lxc_list_for_each(it
, &netdev
->ipv6
) {
4476 struct lxc_inet6dev
*i
= it
->elem
;
4477 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4478 strprint(retv
, inlen
, "%s/%d%s", buf
, i
->prefix
,
4479 (listlen
-- > 1) ? "\n" : "");