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.init_cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
230 { "lxc.init_uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
231 { "lxc.init_gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
232 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
233 { "lxc.syslog", set_config_syslog
, get_config_syslog
, clr_config_syslog
, },
234 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
239 { "lxc.limit", set_config_limit
, get_config_limit
, clr_config_limit
, },
241 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
249 static const struct signame signames
[] = {
282 { SIGSTKFLT
, "STKFLT" },
297 { SIGVTALRM
, "VTALRM" },
303 { SIGWINCH
, "WINCH" },
321 { SIGUNUSED
, "UNUSED" },
328 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
330 extern struct lxc_config_t
*lxc_getconfig(const char *key
)
334 for (i
= 0; i
< config_size
; i
++)
335 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
340 int lxc_listconfigs(char *retv
, int inlen
)
349 memset(retv
, 0, inlen
);
351 for (i
= 0; i
< config_size
; i
++) {
352 char *s
= config
[i
].name
;
353 if (s
[strlen(s
) - 1] == '.')
355 strprint(retv
, inlen
, "%s\n", s
);
361 static int set_config_net(const char *key
, const char *value
,
362 struct lxc_conf
*lxc_conf
, void *data
)
364 if (!lxc_config_value_empty(value
)) {
365 ERROR("lxc.net must not have a value");
369 return clr_config_net(key
, lxc_conf
, data
);
372 static int set_config_net_type(const char *key
, const char *value
,
373 struct lxc_conf
*lxc_conf
, void *data
)
375 struct lxc_netdev
*netdev
;
377 if (lxc_config_value_empty(value
))
378 return clr_config_net_type(key
, lxc_conf
, data
);
387 if (!strcmp(value
, "veth")) {
388 netdev
->type
= LXC_NET_VETH
;
389 } else if (!strcmp(value
, "macvlan")) {
390 netdev
->type
= LXC_NET_MACVLAN
;
391 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
393 } else if (!strcmp(value
, "vlan")) {
394 netdev
->type
= LXC_NET_VLAN
;
395 } else if (!strcmp(value
, "phys")) {
396 netdev
->type
= LXC_NET_PHYS
;
397 } else if (!strcmp(value
, "empty")) {
398 netdev
->type
= LXC_NET_EMPTY
;
399 } else if (!strcmp(value
, "none")) {
400 netdev
->type
= LXC_NET_NONE
;
402 ERROR("invalid network type %s", value
);
410 * If you have p="lxc.net.0.link", pass it p+8
411 * to get back '0' (the index of the nic).
413 static int get_network_netdev_idx(const char *key
)
417 if (*key
< '0' || *key
> '9')
420 ret
= sscanf(key
, "%d", &idx
);
428 * If you have p="lxc.net.0", pass this p+8 and it will return
429 * the netdev of the first configured nic.
431 static struct lxc_netdev
*get_netdev_from_key(const char *key
,
432 struct lxc_list
*network
)
437 struct lxc_netdev
*netdev
= NULL
;
439 idx
= get_network_netdev_idx(key
);
443 lxc_list_for_each(it
, network
) {
453 extern int lxc_list_nicconfigs(struct lxc_conf
*c
, const char *key
, char *retv
,
456 struct lxc_netdev
*netdev
;
460 netdev
= get_netdev_from_key(key
+ 8, &c
->network
);
467 memset(retv
, 0, inlen
);
469 strprint(retv
, inlen
, "type\n");
470 strprint(retv
, inlen
, "script.up\n");
471 strprint(retv
, inlen
, "script.down\n");
472 if (netdev
->type
!= LXC_NET_EMPTY
) {
473 strprint(retv
, inlen
, "flags\n");
474 strprint(retv
, inlen
, "link\n");
475 strprint(retv
, inlen
, "name\n");
476 strprint(retv
, inlen
, "hwaddr\n");
477 strprint(retv
, inlen
, "mtu\n");
478 strprint(retv
, inlen
, "ipv6\n");
479 strprint(retv
, inlen
, "ipv6.gateway\n");
480 strprint(retv
, inlen
, "ipv4\n");
481 strprint(retv
, inlen
, "ipv4.gateway\n");
484 switch (netdev
->type
) {
486 strprint(retv
, inlen
, "veth.pair\n");
488 case LXC_NET_MACVLAN
:
489 strprint(retv
, inlen
, "macvlan.mode\n");
492 strprint(retv
, inlen
, "vlan.id\n");
501 static int set_config_net_flags(const char *key
, const char *value
,
502 struct lxc_conf
*lxc_conf
, void *data
)
504 struct lxc_netdev
*netdev
;
506 if (lxc_config_value_empty(value
))
507 return clr_config_net_flags(key
, lxc_conf
, data
);
516 netdev
->flags
|= IFF_UP
;
521 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
522 struct lxc_netdev
*netdev
)
524 struct ifaddrs
*ifaddr
, *ifa
;
527 const char *type_key
= "lxc.net.type";
528 const char *link_key
= "lxc.net.link";
529 const char *tmpvalue
= "phys";
531 if (getifaddrs(&ifaddr
) == -1) {
532 SYSERROR("Get network interfaces failed");
536 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
539 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
542 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
543 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
546 ret
= set_config_net_link(
547 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
549 ERROR("failed to create matched ifnames");
553 ERROR("failed to create matched ifnames");
565 static int set_config_net_link(const char *key
, const char *value
,
566 struct lxc_conf
*lxc_conf
, void *data
)
568 struct lxc_netdev
*netdev
;
571 if (lxc_config_value_empty(value
))
572 return clr_config_net_link(key
, lxc_conf
, data
);
581 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
582 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
584 ret
= network_ifname(&netdev
->link
, value
);
589 static int set_config_net_name(const char *key
, const char *value
,
590 struct lxc_conf
*lxc_conf
, void *data
)
592 struct lxc_netdev
*netdev
;
594 if (lxc_config_value_empty(value
))
595 return clr_config_net_name(key
, lxc_conf
, data
);
604 return network_ifname(&netdev
->name
, value
);
607 static int set_config_net_veth_pair(const char *key
, const char *value
,
608 struct lxc_conf
*lxc_conf
, void *data
)
610 struct lxc_netdev
*netdev
;
612 if (lxc_config_value_empty(value
))
613 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
622 return network_ifname(&netdev
->priv
.veth_attr
.pair
, value
);
625 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
626 struct lxc_conf
*lxc_conf
, void *data
)
628 struct lxc_netdev
*netdev
;
630 if (lxc_config_value_empty(value
))
631 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
640 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
643 static int set_config_net_hwaddr(const char *key
, const char *value
,
644 struct lxc_conf
*lxc_conf
, void *data
)
646 struct lxc_netdev
*netdev
;
649 if (lxc_config_value_empty(value
))
650 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
659 new_value
= strdup(value
);
661 SYSERROR("failed to strdup \"%s\"", value
);
664 rand_complete_hwaddr(new_value
);
666 if (lxc_config_value_empty(new_value
)) {
668 netdev
->hwaddr
= NULL
;
672 netdev
->hwaddr
= new_value
;
676 static int set_config_net_vlan_id(const char *key
, const char *value
,
677 struct lxc_conf
*lxc_conf
, void *data
)
679 struct lxc_netdev
*netdev
;
681 if (lxc_config_value_empty(value
))
682 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
691 if (get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0))
697 static int set_config_net_mtu(const char *key
, const char *value
,
698 struct lxc_conf
*lxc_conf
, void *data
)
700 struct lxc_netdev
*netdev
;
702 if (lxc_config_value_empty(value
))
703 return clr_config_net_mtu(key
, lxc_conf
, data
);
712 return set_config_string_item(&netdev
->mtu
, value
);
715 static int set_config_net_ipv4(const char *key
, const char *value
,
716 struct lxc_conf
*lxc_conf
, void *data
)
718 struct lxc_netdev
*netdev
;
719 struct lxc_inetdev
*inetdev
;
720 struct lxc_list
*list
;
721 char *cursor
, *slash
;
722 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
724 if (lxc_config_value_empty(value
))
725 return clr_config_net_ipv4(key
, lxc_conf
, data
);
734 inetdev
= malloc(sizeof(*inetdev
));
736 SYSERROR("failed to allocate ipv4 address");
739 memset(inetdev
, 0, sizeof(*inetdev
));
741 list
= malloc(sizeof(*list
));
743 SYSERROR("failed to allocate memory");
749 list
->elem
= inetdev
;
751 addr
= strdup(value
);
753 ERROR("no address specified");
759 cursor
= strstr(addr
, " ");
765 slash
= strstr(addr
, "/");
771 if (!inet_pton(AF_INET
, addr
, &inetdev
->addr
)) {
772 SYSERROR("invalid ipv4 address: %s", value
);
779 if (bcast
&& !inet_pton(AF_INET
, bcast
, &inetdev
->bcast
)) {
780 SYSERROR("invalid ipv4 broadcast address: %s", value
);
787 /* No prefix specified, determine it from the network class. */
789 if (lxc_safe_uint(prefix
, &inetdev
->prefix
) < 0)
792 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
795 /* If no broadcast address, let compute one from the
796 * prefix and address.
799 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
800 inetdev
->bcast
.s_addr
|=
801 htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
804 lxc_list_add_tail(&netdev
->ipv4
, list
);
810 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
811 struct lxc_conf
*lxc_conf
, void *data
)
813 struct lxc_netdev
*netdev
;
815 if (lxc_config_value_empty(value
))
816 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
825 free(netdev
->ipv4_gateway
);
827 if (!strcmp(value
, "auto")) {
828 netdev
->ipv4_gateway
= NULL
;
829 netdev
->ipv4_gateway_auto
= true;
833 gw
= malloc(sizeof(*gw
));
835 SYSERROR("failed to allocate ipv4 gateway address");
839 if (!inet_pton(AF_INET
, value
, gw
)) {
840 SYSERROR("invalid ipv4 gateway address: %s", value
);
845 netdev
->ipv4_gateway
= gw
;
846 netdev
->ipv4_gateway_auto
= false;
852 static int set_config_net_ipv6(const char *key
, const char *value
,
853 struct lxc_conf
*lxc_conf
, void *data
)
855 struct lxc_netdev
*netdev
;
856 struct lxc_inet6dev
*inet6dev
;
857 struct lxc_list
*list
;
858 char *slash
, *valdup
, *netmask
;
860 if (lxc_config_value_empty(value
))
861 return clr_config_net_ipv6(key
, lxc_conf
, data
);
870 inet6dev
= malloc(sizeof(*inet6dev
));
872 SYSERROR("failed to allocate ipv6 address");
875 memset(inet6dev
, 0, sizeof(*inet6dev
));
877 list
= malloc(sizeof(*list
));
879 SYSERROR("failed to allocate memory");
885 list
->elem
= inet6dev
;
887 valdup
= strdup(value
);
889 ERROR("no address specified");
895 inet6dev
->prefix
= 64;
896 slash
= strstr(valdup
, "/");
900 if (lxc_safe_uint(netmask
, &inet6dev
->prefix
) < 0)
904 if (!inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
)) {
905 SYSERROR("invalid ipv6 address: %s", valdup
);
912 lxc_list_add_tail(&netdev
->ipv6
, list
);
918 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
919 struct lxc_conf
*lxc_conf
, void *data
)
921 struct lxc_netdev
*netdev
;
923 if (lxc_config_value_empty(value
))
924 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
933 free(netdev
->ipv6_gateway
);
935 if (!strcmp(value
, "auto")) {
936 netdev
->ipv6_gateway
= NULL
;
937 netdev
->ipv6_gateway_auto
= true;
941 gw
= malloc(sizeof(*gw
));
943 SYSERROR("failed to allocate ipv6 gateway address");
947 if (!inet_pton(AF_INET6
, value
, gw
)) {
948 SYSERROR("invalid ipv6 gateway address: %s", value
);
953 netdev
->ipv6_gateway
= gw
;
954 netdev
->ipv6_gateway_auto
= false;
960 static int set_config_net_script_up(const char *key
, const char *value
,
961 struct lxc_conf
*lxc_conf
, void *data
)
963 struct lxc_netdev
*netdev
;
965 if (lxc_config_value_empty(value
))
966 return clr_config_net_script_up(key
, lxc_conf
, data
);
975 return set_config_string_item(&netdev
->upscript
, value
);
978 static int set_config_net_script_down(const char *key
, const char *value
,
979 struct lxc_conf
*lxc_conf
, void *data
)
981 struct lxc_netdev
*netdev
;
983 if (lxc_config_value_empty(value
))
984 return clr_config_net_script_down(key
, lxc_conf
, data
);
993 return set_config_string_item(&netdev
->downscript
, value
);
996 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
998 struct lxc_list
*hooklist
;
1000 hooklist
= malloc(sizeof(*hooklist
));
1006 hooklist
->elem
= hook
;
1007 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
1011 static int set_config_seccomp(const char *key
, const char *value
,
1012 struct lxc_conf
*lxc_conf
, void *data
)
1014 return set_config_path_item(&lxc_conf
->seccomp
, value
);
1017 static int set_config_init_cmd(const char *key
, const char *value
,
1018 struct lxc_conf
*lxc_conf
, void *data
)
1020 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1023 static int set_config_init_uid(const char *key
, const char *value
,
1024 struct lxc_conf
*lxc_conf
, void *data
)
1026 unsigned int init_uid
;
1028 /* Set config value to default. */
1029 if (lxc_config_value_empty(value
)) {
1030 lxc_conf
->init_uid
= 0;
1034 /* Parse new config value. */
1035 if (lxc_safe_uint(value
, &init_uid
) < 0)
1037 lxc_conf
->init_uid
= init_uid
;
1042 static int set_config_init_gid(const char *key
, const char *value
,
1043 struct lxc_conf
*lxc_conf
, void *data
)
1045 unsigned int init_gid
;
1047 /* Set config value to default. */
1048 if (lxc_config_value_empty(value
)) {
1049 lxc_conf
->init_gid
= 0;
1053 /* Parse new config value. */
1054 if (lxc_safe_uint(value
, &init_gid
) < 0)
1056 lxc_conf
->init_gid
= init_gid
;
1061 static int set_config_hooks(const char *key
, const char *value
,
1062 struct lxc_conf
*lxc_conf
, void *data
)
1066 if (lxc_config_value_empty(value
))
1067 return lxc_clear_hooks(lxc_conf
, key
);
1069 if (strcmp(key
+ 4, "hook") == 0) {
1070 ERROR("lxc.hook cannot take a value");
1073 copy
= strdup(value
);
1075 SYSERROR("failed to dup string '%s'", value
);
1079 if (strcmp(key
+ 9, "pre-start") == 0)
1080 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
1081 else if (strcmp(key
+ 9, "pre-mount") == 0)
1082 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
1083 else if (strcmp(key
+ 9, "autodev") == 0)
1084 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
1085 else if (strcmp(key
+ 9, "mount") == 0)
1086 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
1087 else if (strcmp(key
+ 9, "start") == 0)
1088 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
1089 else if (strcmp(key
+ 9, "stop") == 0)
1090 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
1091 else if (strcmp(key
+ 9, "post-stop") == 0)
1092 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
1093 else if (strcmp(key
+ 9, "clone") == 0)
1094 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
1095 else if (strcmp(key
+ 9, "destroy") == 0)
1096 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
1098 SYSERROR("Unknown key: %s", key
);
1103 static int set_config_personality(const char *key
, const char *value
,
1104 struct lxc_conf
*lxc_conf
, void *data
)
1106 signed long personality
= lxc_config_parse_arch(value
);
1108 if (personality
>= 0)
1109 lxc_conf
->personality
= personality
;
1111 WARN("unsupported personality '%s'", value
);
1116 static int set_config_pts(const char *key
, const char *value
,
1117 struct lxc_conf
*lxc_conf
, void *data
)
1119 /* Set config value to default. */
1120 if (lxc_config_value_empty(value
)) {
1125 /* Parse new config value. */
1126 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1132 /* We only need to check whether the first byte of the key after the lxc.start.
1133 * prefix matches our expectations since they fortunately all start with a
1134 * different letter. If anything was wrong with the key we would have already
1135 * noticed when the callback was called.
1137 static int set_config_start(const char *key
, const char *value
,
1138 struct lxc_conf
*lxc_conf
, void *data
)
1142 is_empty
= lxc_config_value_empty(value
);
1144 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1145 /* Set config value to default. */
1147 lxc_conf
->start_auto
= 0;
1151 /* Parse new config value. */
1152 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1155 if (lxc_conf
->start_auto
> 1)
1159 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1160 /* Set config value to default. */
1162 lxc_conf
->start_delay
= 0;
1166 /* Parse new config value. */
1167 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1168 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1169 /* Set config value to default. */
1171 lxc_conf
->start_order
= 0;
1175 /* Parse new config value. */
1176 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1179 SYSERROR("Unknown key: %s", key
);
1183 static int set_config_monitor(const char *key
, const char *value
,
1184 struct lxc_conf
*lxc_conf
, void *data
)
1186 /* Set config value to default. */
1187 if (lxc_config_value_empty(value
)) {
1188 lxc_conf
->monitor_unshare
= 0;
1192 /* Parse new config value. */
1193 if (strcmp(key
+ 12, "unshare") == 0)
1194 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1196 SYSERROR("Unknown key: %s", key
);
1200 static int set_config_group(const char *key
, const char *value
,
1201 struct lxc_conf
*lxc_conf
, void *data
)
1203 char *groups
, *groupptr
, *sptr
, *token
;
1204 struct lxc_list
*grouplist
;
1207 if (lxc_config_value_empty(value
))
1208 return lxc_clear_groups(lxc_conf
);
1210 groups
= strdup(value
);
1212 SYSERROR("failed to dup '%s'", value
);
1216 /* In case several groups are specified in a single line
1217 * split these groups in a single element for the list.
1219 for (groupptr
= groups
;; groupptr
= NULL
) {
1220 token
= strtok_r(groupptr
, " \t", &sptr
);
1226 grouplist
= malloc(sizeof(*grouplist
));
1228 SYSERROR("failed to allocate groups list");
1232 grouplist
->elem
= strdup(token
);
1233 if (!grouplist
->elem
) {
1234 SYSERROR("failed to dup '%s'", token
);
1239 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1246 static int set_config_environment(const char *key
, const char *value
,
1247 struct lxc_conf
*lxc_conf
, void *data
)
1249 struct lxc_list
*list_item
= NULL
;
1251 if (lxc_config_value_empty(value
))
1252 return lxc_clear_environment(lxc_conf
);
1254 list_item
= malloc(sizeof(*list_item
));
1258 list_item
->elem
= strdup(value
);
1260 if (!list_item
->elem
)
1263 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1272 static int set_config_tty(const char *key
, const char *value
,
1273 struct lxc_conf
*lxc_conf
, void *data
)
1275 /* Set config value to default. */
1276 if (lxc_config_value_empty(value
)) {
1281 /* Parse new config value. */
1282 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1285 static int set_config_ttydir(const char *key
, const char *value
,
1286 struct lxc_conf
*lxc_conf
, void *data
)
1288 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1292 static int set_config_apparmor_profile(const char *key
, const char *value
,
1293 struct lxc_conf
*lxc_conf
, void *data
)
1295 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1298 static int set_config_apparmor_allow_incomplete(const char *key
,
1300 struct lxc_conf
*lxc_conf
,
1303 /* Set config value to default. */
1304 if (lxc_config_value_empty(value
)) {
1305 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1309 /* Parse new config value. */
1310 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1313 if (lxc_conf
->lsm_aa_allow_incomplete
> 1) {
1314 ERROR("Wrong value for lxc.lsm_aa_allow_incomplete. Can only "
1315 "be set to 0 or 1");
1322 static int set_config_selinux_context(const char *key
, const char *value
,
1323 struct lxc_conf
*lxc_conf
, void *data
)
1325 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1328 static int set_config_logfile(const char *key
, const char *value
,
1329 struct lxc_conf
*c
, void *data
)
1333 if (lxc_config_value_empty(value
)) {
1339 /* Store these values in the lxc_conf, and then try to set for actual
1342 ret
= set_config_path_item(&c
->logfile
, value
);
1344 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1348 static int set_config_loglevel(const char *key
, const char *value
,
1349 struct lxc_conf
*lxc_conf
, void *data
)
1353 /* Set config value to default. */
1354 if (lxc_config_value_empty(value
)) {
1355 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1359 /* Parse new config value. */
1360 if (value
[0] >= '0' && value
[0] <= '9') {
1361 if (lxc_safe_int(value
, &newlevel
) < 0)
1364 newlevel
= lxc_log_priority_to_int(value
);
1367 /* Store these values in the lxc_conf, and then try to set for actual
1370 lxc_conf
->loglevel
= newlevel
;
1371 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1374 static int set_config_autodev(const char *key
, const char *value
,
1375 struct lxc_conf
*lxc_conf
, void *data
)
1377 /* Set config value to default. */
1378 if (lxc_config_value_empty(value
)) {
1379 lxc_conf
->autodev
= 0;
1383 /* Parse new config value. */
1384 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1387 if (lxc_conf
->autodev
> 1) {
1388 ERROR("Wrong value for lxc.autodev. Can only be set to 0 or 1");
1395 static int sig_num(const char *sig
)
1397 unsigned int signum
;
1399 if (lxc_safe_uint(sig
, &signum
) < 0)
1405 static int rt_sig_num(const char *signame
)
1407 int rtmax
= 0, sig_n
= 0;
1409 if (strncasecmp(signame
, "max-", 4) == 0) {
1414 if (!isdigit(*signame
))
1417 sig_n
= sig_num(signame
);
1418 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1419 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1425 static int sig_parse(const char *signame
)
1429 if (isdigit(*signame
)) {
1430 return sig_num(signame
);
1431 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1433 if (strncasecmp(signame
, "rt", 2) == 0)
1434 return rt_sig_num(signame
+ 2);
1435 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1436 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1437 return signames
[n
].num
;
1444 static int set_config_haltsignal(const char *key
, const char *value
,
1445 struct lxc_conf
*lxc_conf
, void *data
)
1449 /* Set config value to default. */
1450 if (lxc_config_value_empty(value
)) {
1451 lxc_conf
->haltsignal
= 0;
1455 /* Parse new config value. */
1456 sig_n
= sig_parse(value
);
1460 lxc_conf
->haltsignal
= sig_n
;
1465 static int set_config_rebootsignal(const char *key
, const char *value
,
1466 struct lxc_conf
*lxc_conf
, void *data
)
1470 /* Set config value to default. */
1471 if (lxc_config_value_empty(value
)) {
1472 lxc_conf
->rebootsignal
= 0;
1476 /* Parse new config value. */
1477 sig_n
= sig_parse(value
);
1480 lxc_conf
->rebootsignal
= sig_n
;
1485 static int set_config_stopsignal(const char *key
, const char *value
,
1486 struct lxc_conf
*lxc_conf
, void *data
)
1490 /* Set config value to default. */
1491 if (lxc_config_value_empty(value
)) {
1492 lxc_conf
->stopsignal
= 0;
1496 /* Parse new config value. */
1497 sig_n
= sig_parse(value
);
1500 lxc_conf
->stopsignal
= sig_n
;
1505 static int set_config_cgroup(const char *key
, const char *value
,
1506 struct lxc_conf
*lxc_conf
, void *data
)
1509 char *token
= "lxc.cgroup.";
1510 struct lxc_list
*cglist
= NULL
;
1511 struct lxc_cgroup
*cgelem
= NULL
;
1513 if (lxc_config_value_empty(value
))
1514 return lxc_clear_cgroups(lxc_conf
, key
);
1516 subkey
= strstr(key
, token
);
1520 if (!strlen(subkey
))
1523 if (strlen(subkey
) == strlen(token
))
1526 subkey
+= strlen(token
);
1528 cglist
= malloc(sizeof(*cglist
));
1532 cgelem
= malloc(sizeof(*cgelem
));
1535 memset(cgelem
, 0, sizeof(*cgelem
));
1537 cgelem
->subsystem
= strdup(subkey
);
1538 cgelem
->value
= strdup(value
);
1540 if (!cgelem
->subsystem
|| !cgelem
->value
)
1543 cglist
->elem
= cgelem
;
1545 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1553 free(cgelem
->subsystem
);
1555 free(cgelem
->value
);
1563 static int set_config_prlimit(const char *key
, const char *value
,
1564 struct lxc_conf
*lxc_conf
, void *data
)
1566 struct lxc_list
*iter
;
1567 struct rlimit limit
;
1568 unsigned long limit_value
;
1569 struct lxc_list
*limlist
= NULL
;
1570 struct lxc_limit
*limelem
= NULL
;
1572 if (lxc_config_value_empty(value
))
1573 return lxc_clear_limits(lxc_conf
, key
);
1575 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1578 key
+= sizeof("lxc.prlimit.") - 1;
1580 /* soft limit comes first in the value */
1581 if (!parse_limit_value(&value
, &limit_value
))
1583 limit
.rlim_cur
= limit_value
;
1585 /* skip spaces and a colon */
1586 while (isspace(*value
))
1591 else if (*value
) /* any other character is an error here */
1594 while (isspace(*value
))
1597 /* optional hard limit */
1599 if (!parse_limit_value(&value
, &limit_value
))
1601 limit
.rlim_max
= limit_value
;
1603 /* check for trailing garbage */
1604 while (isspace(*value
))
1610 /* a single value sets both hard and soft limit */
1611 limit
.rlim_max
= limit
.rlim_cur
;
1614 /* find existing list element */
1615 lxc_list_for_each(iter
, &lxc_conf
->limits
)
1617 limelem
= iter
->elem
;
1618 if (!strcmp(key
, limelem
->resource
)) {
1619 limelem
->limit
= limit
;
1624 /* allocate list element */
1625 limlist
= malloc(sizeof(*limlist
));
1629 limelem
= malloc(sizeof(*limelem
));
1632 memset(limelem
, 0, sizeof(*limelem
));
1634 limelem
->resource
= strdup(key
);
1635 if (!limelem
->resource
)
1637 limelem
->limit
= limit
;
1639 limlist
->elem
= limelem
;
1641 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1648 free(limelem
->resource
);
1654 static int set_config_idmaps(const char *key
, const char *value
,
1655 struct lxc_conf
*lxc_conf
, void *data
)
1657 unsigned long hostid
, nsid
, range
;
1660 struct lxc_list
*idmaplist
= NULL
;
1661 struct id_map
*idmap
= NULL
;
1663 if (lxc_config_value_empty(value
))
1664 return lxc_clear_idmaps(lxc_conf
);
1666 idmaplist
= malloc(sizeof(*idmaplist
));
1670 idmap
= malloc(sizeof(*idmap
));
1673 memset(idmap
, 0, sizeof(*idmap
));
1675 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1679 INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1681 idmap
->idtype
= ID_TYPE_UID
;
1682 else if (type
== 'g')
1683 idmap
->idtype
= ID_TYPE_GID
;
1687 idmap
->hostid
= hostid
;
1689 idmap
->range
= range
;
1690 idmaplist
->elem
= idmap
;
1691 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1703 static int set_config_fstab(const char *key
, const char *value
,
1704 struct lxc_conf
*lxc_conf
, void *data
)
1706 if (lxc_config_value_empty(value
)) {
1707 clr_config_fstab(key
, lxc_conf
, NULL
);
1711 return set_config_path_item(&lxc_conf
->fstab
, value
);
1714 static int set_config_mount_auto(const char *key
, const char *value
,
1715 struct lxc_conf
*lxc_conf
, void *data
)
1717 char *autos
, *autoptr
, *sptr
, *token
;
1724 } allowed_auto_mounts
[] = {
1725 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1726 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1727 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1728 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1729 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1730 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1731 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1732 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1733 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1734 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1735 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1736 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1737 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1738 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1739 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1740 /* NB: For adding anything that is just a single on/off, but has
1741 * no options: keep mask and flag identical and just define the
1742 * enum value as an unused bit so far
1747 if (lxc_config_value_empty(value
)) {
1748 lxc_conf
->auto_mounts
= 0;
1752 autos
= strdup(value
);
1754 SYSERROR("failed to dup '%s'", value
);
1758 for (autoptr
= autos
;; autoptr
= NULL
) {
1759 token
= strtok_r(autoptr
, " \t", &sptr
);
1765 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1766 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1770 if (!allowed_auto_mounts
[i
].token
) {
1771 ERROR("Invalid filesystem to automount: %s", token
);
1775 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1776 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1783 static int set_config_mount(const char *key
, const char *value
,
1784 struct lxc_conf
*lxc_conf
, void *data
)
1787 struct lxc_list
*mntlist
;
1789 if (lxc_config_value_empty(value
))
1790 return lxc_clear_mount_entries(lxc_conf
);
1792 mntlist
= malloc(sizeof(*mntlist
));
1796 mntelem
= strdup(value
);
1801 mntlist
->elem
= mntelem
;
1803 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1808 static int set_config_cap_keep(const char *key
, const char *value
,
1809 struct lxc_conf
*lxc_conf
, void *data
)
1811 char *keepcaps
, *keepptr
, *sptr
, *token
;
1812 struct lxc_list
*keeplist
;
1815 if (lxc_config_value_empty(value
))
1816 return lxc_clear_config_keepcaps(lxc_conf
);
1818 keepcaps
= strdup(value
);
1820 SYSERROR("failed to dup '%s'", value
);
1824 /* In case several capability keep is specified in a single line
1825 * split these caps in a single element for the list.
1827 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1828 token
= strtok_r(keepptr
, " \t", &sptr
);
1834 if (!strcmp(token
, "none"))
1835 lxc_clear_config_keepcaps(lxc_conf
);
1837 keeplist
= malloc(sizeof(*keeplist
));
1839 SYSERROR("failed to allocate keepcap list");
1843 keeplist
->elem
= strdup(token
);
1844 if (!keeplist
->elem
) {
1845 SYSERROR("failed to dup '%s'", token
);
1850 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1858 static int set_config_cap_drop(const char *key
, const char *value
,
1859 struct lxc_conf
*lxc_conf
, void *data
)
1861 char *dropcaps
, *dropptr
, *sptr
, *token
;
1862 struct lxc_list
*droplist
;
1865 if (lxc_config_value_empty(value
))
1866 return lxc_clear_config_caps(lxc_conf
);
1868 dropcaps
= strdup(value
);
1870 SYSERROR("failed to dup '%s'", value
);
1874 /* In case several capability drop is specified in a single line
1875 * split these caps in a single element for the list.
1877 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1878 token
= strtok_r(dropptr
, " \t", &sptr
);
1884 droplist
= malloc(sizeof(*droplist
));
1886 SYSERROR("failed to allocate drop list");
1890 droplist
->elem
= strdup(token
);
1891 if (!droplist
->elem
) {
1892 SYSERROR("failed to dup '%s'", token
);
1897 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1905 static int set_config_console(const char *key
, const char *value
,
1906 struct lxc_conf
*lxc_conf
, void *data
)
1908 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1911 static int set_config_console_logfile(const char *key
, const char *value
,
1912 struct lxc_conf
*lxc_conf
, void *data
)
1914 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1917 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1919 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1921 update_hwaddr(line
);
1923 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1924 char *tmp
= realloc(conf
->unexpanded_config
,
1925 conf
->unexpanded_alloced
+ 1024);
1929 if (!conf
->unexpanded_config
)
1931 conf
->unexpanded_config
= tmp
;
1932 conf
->unexpanded_alloced
+= 1024;
1934 strcat(conf
->unexpanded_config
, line
);
1935 conf
->unexpanded_len
+= linelen
;
1936 if (line
[linelen
- 1] != '\n') {
1937 strcat(conf
->unexpanded_config
, "\n");
1938 conf
->unexpanded_len
++;
1943 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
1945 struct dirent
*direntp
;
1947 char path
[MAXPATHLEN
];
1951 dir
= opendir(dirp
);
1953 SYSERROR("failed to open '%s'", dirp
);
1957 while ((direntp
= readdir(dir
))) {
1962 fnam
= direntp
->d_name
;
1963 if (!strcmp(fnam
, "."))
1966 if (!strcmp(fnam
, ".."))
1970 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
1972 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
1973 if (len
< 0 || len
>= MAXPATHLEN
) {
1974 ERROR("lxc.include filename too long under '%s'", dirp
);
1979 ret
= lxc_config_read(path
, lxc_conf
, true);
1987 WARN("lxc.include dir: failed to close directory");
1992 static int set_config_includefiles(const char *key
, const char *value
,
1993 struct lxc_conf
*lxc_conf
, void *data
)
1995 /* Set config value to default. */
1996 if (lxc_config_value_empty(value
)) {
1997 clr_config_includefiles(key
, lxc_conf
, NULL
);
2001 /* Parse new config value. */
2003 return do_includedir(value
, lxc_conf
);
2005 return lxc_config_read(value
, lxc_conf
, true);
2008 static int set_config_rootfs(const char *key
, const char *value
,
2009 struct lxc_conf
*lxc_conf
, void *data
)
2011 return set_config_path_item(&lxc_conf
->rootfs
.path
, value
);
2014 static int set_config_rootfs_mount(const char *key
, const char *value
,
2015 struct lxc_conf
*lxc_conf
, void *data
)
2017 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2020 static int set_config_rootfs_options(const char *key
, const char *value
,
2021 struct lxc_conf
*lxc_conf
, void *data
)
2023 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2026 static int set_config_rootfs_backend(const char *key
, const char *value
,
2027 struct lxc_conf
*lxc_conf
, void *data
)
2029 if (lxc_config_value_empty(value
)) {
2030 free(lxc_conf
->rootfs
.bdev_type
);
2031 lxc_conf
->rootfs
.bdev_type
= NULL
;
2035 if (!is_valid_bdev_type(value
)) {
2036 ERROR("Bad rootfs.backend: '%s'", value
);
2040 return set_config_string_item(&lxc_conf
->rootfs
.bdev_type
, value
);
2043 static int set_config_utsname(const char *key
, const char *value
,
2044 struct lxc_conf
*lxc_conf
, void *data
)
2046 struct utsname
*utsname
;
2048 if (lxc_config_value_empty(value
)) {
2049 clr_config_utsname(key
, lxc_conf
, NULL
);
2053 utsname
= malloc(sizeof(*utsname
));
2055 SYSERROR("failed to allocate memory");
2059 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2060 ERROR("node name '%s' is too long", value
);
2065 strcpy(utsname
->nodename
, value
);
2066 free(lxc_conf
->utsname
);
2067 lxc_conf
->utsname
= utsname
;
2072 struct parse_line_conf
{
2073 struct lxc_conf
*conf
;
2077 static int parse_line(char *buffer
, void *data
)
2079 struct lxc_config_t
*config
;
2080 char *dot
, *key
, *line
, *linep
, *value
;
2081 struct parse_line_conf
*plc
= data
;
2084 if (lxc_is_line_empty(buffer
))
2087 /* we have to dup the buffer otherwise, at the re-exec for
2088 * reboot we modified the original string on the stack by
2089 * replacing '=' by '\0' below
2091 linep
= line
= strdup(buffer
);
2093 SYSERROR("failed to allocate memory for '%s'", buffer
);
2097 if (!plc
->from_include
)
2098 if ((ret
= append_unexp_config_line(line
, plc
->conf
)))
2101 line
+= lxc_char_left_gc(line
, strlen(line
));
2103 /* ignore comments */
2107 /* martian option - don't add it to the config itself */
2108 if (strncmp(line
, "lxc.", 4))
2113 dot
= strstr(line
, "=");
2115 ERROR("invalid configuration line: %s", line
);
2123 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2125 value
+= lxc_char_left_gc(value
, strlen(value
));
2126 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2128 if (*value
== '\'' || *value
== '\"') {
2129 size_t len
= strlen(value
);
2130 if (len
> 1 && value
[len
- 1] == *value
) {
2131 value
[len
- 1] = '\0';
2136 config
= lxc_getconfig(key
);
2138 ERROR("unknown key %s", key
);
2142 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2149 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2151 struct parse_line_conf c
;
2154 c
.from_include
= false;
2156 return parse_line(buffer
, &c
);
2159 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2161 struct parse_line_conf c
;
2164 c
.from_include
= from_include
;
2166 if (access(file
, R_OK
) == -1) {
2170 /* Catch only the top level config file name in the structure */
2172 conf
->rcfile
= strdup(file
);
2174 return lxc_file_for_each_line(file
, parse_line
, &c
);
2177 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2179 struct lxc_list
*dent
;
2181 dent
= malloc(sizeof(struct lxc_list
));
2186 lxc_list_add_tail(defines
, dent
);
2190 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2192 struct lxc_list
*it
, *next
;
2195 lxc_list_for_each(it
, defines
) {
2196 ret
= lxc_config_readline(it
->elem
, conf
);
2201 lxc_list_for_each_safe(it
, defines
, next
) {
2209 signed long lxc_config_parse_arch(const char *arch
)
2211 #if HAVE_SYS_PERSONALITY_H
2217 { "x86", PER_LINUX32
},
2218 { "linux32", PER_LINUX32
},
2219 { "i386", PER_LINUX32
},
2220 { "i486", PER_LINUX32
},
2221 { "i586", PER_LINUX32
},
2222 { "i686", PER_LINUX32
},
2223 { "athlon", PER_LINUX32
},
2224 { "mips", PER_LINUX32
},
2225 { "mipsel", PER_LINUX32
},
2226 { "ppc", PER_LINUX32
},
2227 { "arm", PER_LINUX32
},
2228 { "armv7l", PER_LINUX32
},
2229 { "armhf", PER_LINUX32
},
2230 { "armel", PER_LINUX32
},
2231 { "powerpc", PER_LINUX32
},
2232 { "linux64", PER_LINUX
},
2233 { "x86_64", PER_LINUX
},
2234 { "amd64", PER_LINUX
},
2235 { "mips64", PER_LINUX
},
2236 { "mips64el", PER_LINUX
},
2237 { "ppc64", PER_LINUX
},
2238 { "ppc64le", PER_LINUX
},
2239 { "ppc64el", PER_LINUX
},
2240 { "powerpc64", PER_LINUX
},
2241 { "s390x", PER_LINUX
},
2242 { "aarch64", PER_LINUX
},
2243 { "arm64", PER_LINUX
},
2245 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2247 for (i
= 0; i
< len
; i
++) {
2248 if (!strcmp(pername
[i
].name
, arch
))
2249 return pername
[i
].per
;
2256 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2258 char *token
, *saveptr
= NULL
;
2264 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2265 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2266 { "LSM", LXC_ATTACH_LSM_EXEC
},
2271 /* For the sake of backward compatibility, drop all privileges
2272 * if none is specified.
2274 for (i
= 0; all_privs
[i
].token
; i
++) {
2275 *flags
|= all_privs
[i
].flag
;
2280 token
= strtok_r(flaglist
, "|", &saveptr
);
2283 for (i
= 0; all_privs
[i
].token
; i
++) {
2284 if (!strcmp(all_privs
[i
].token
, token
))
2285 aflag
= all_privs
[i
].flag
;
2292 token
= strtok_r(NULL
, "|", &saveptr
);
2298 /* Write out a configuration file. */
2299 void write_config(FILE *fout
, struct lxc_conf
*c
)
2302 size_t len
= c
->unexpanded_len
;
2307 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2309 SYSERROR("Error writing configuration file");
2312 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2319 len
= strlen(key
) + strlen(v
) + 4;
2322 if (lxc_config_value_empty(v
))
2323 ret
= snprintf(tmp
, len
, "%s =", key
);
2325 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2326 if (ret
< 0 || ret
>= len
)
2329 /* Save the line verbatim into unexpanded_conf */
2330 if (append_unexp_config_line(tmp
, conf
))
2336 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2340 char *lstart
= conf
->unexpanded_config
;
2342 if (!conf
->unexpanded_config
)
2346 lend
= strchr(lstart
, '\n');
2349 lend
= lstart
+ strlen(lstart
);
2352 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2357 v
= lstart
[strlen(key
)];
2358 if (!isspace(v
) && v
!= '=') {
2363 conf
->unexpanded_len
-= (lend
- lstart
);
2364 if (*lend
== '\0') {
2368 memmove(lstart
, lend
, strlen(lend
) + 1);
2372 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2373 const char *newpath
, const char *oldname
,
2374 const char *newname
, const char *ovldir
)
2377 char *lend
, *newdir
, *olddir
, *p
, *q
;
2378 size_t newdirlen
, olddirlen
;
2379 char *lstart
= conf
->unexpanded_config
;
2380 const char *key
= "lxc.mount.entry";
2382 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2383 olddir
= alloca(olddirlen
+ 1);
2384 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2386 if (ret
< 0 || ret
>= olddirlen
+ 1) {
2387 ERROR("failed to create string");
2391 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2392 newdir
= alloca(newdirlen
+ 1);
2393 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2395 if (ret
< 0 || ret
>= newdirlen
+ 1) {
2396 ERROR("failed to create string");
2400 if (!conf
->unexpanded_config
)
2404 lend
= strchr(lstart
, '\n');
2406 lend
= lstart
+ strlen(lstart
);
2410 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2413 p
= strchr(lstart
+ strlen(key
), '=');
2424 /* Whenever an lxc.mount.entry entry is found in a line we check
2425 * if the substring " overlay" or the substring " aufs" is
2426 * present before doing any further work. We check for "
2427 * overlay" and " aufs" since both substrings need to have at
2428 * least one space before them in a valid overlay
2429 * lxc.mount.entry (/A B overlay). When the space before is
2430 * missing it is very likely that these substrings are part of a
2431 * path or something else. (Checking q >= lend ensures that we
2432 * only count matches in the current line.) */
2433 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2434 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2437 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2440 /* replace the olddir with newdir */
2441 if (olddirlen
>= newdirlen
) {
2442 size_t diff
= olddirlen
- newdirlen
;
2443 memcpy(q
, newdir
, newdirlen
);
2444 if (olddirlen
!= newdirlen
) {
2445 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2446 strlen(q
) - newdirlen
- diff
+ 1);
2448 conf
->unexpanded_len
-= diff
;
2452 size_t diff
= newdirlen
- olddirlen
;
2453 size_t oldlen
= conf
->unexpanded_len
;
2454 size_t newlen
= oldlen
+ diff
;
2455 size_t poffset
= q
- conf
->unexpanded_config
;
2457 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2459 ERROR("Out of memory");
2462 conf
->unexpanded_len
= newlen
;
2463 conf
->unexpanded_alloced
= newlen
+ 1;
2464 new[newlen
- 1] = '\0';
2465 lend
= new + (lend
- conf
->unexpanded_config
);
2466 /* move over the remainder to make room for the newdir
2468 memmove(new + poffset
+ newdirlen
,
2469 new + poffset
+ olddirlen
,
2470 oldlen
- poffset
- olddirlen
+ 1);
2471 conf
->unexpanded_config
= new;
2472 memcpy(new + poffset
, newdir
, newdirlen
);
2482 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2483 const char *newpath
, const char *oldname
,
2484 const char *newname
)
2487 char *lend
, *newdir
, *olddir
, *p
;
2488 char *lstart
= conf
->unexpanded_config
;
2489 size_t newdirlen
, olddirlen
;
2490 const char *key
= "lxc.hook";
2492 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2493 olddir
= alloca(olddirlen
+ 1);
2494 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2495 if (ret
< 0 || ret
>= olddirlen
+ 1) {
2496 ERROR("failed to create string");
2500 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2501 newdir
= alloca(newdirlen
+ 1);
2502 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2503 if (ret
< 0 || ret
>= newdirlen
+ 1) {
2504 ERROR("failed to create string");
2507 if (!conf
->unexpanded_config
)
2510 lend
= strchr(lstart
, '\n');
2512 lend
= lstart
+ strlen(lstart
);
2516 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2519 p
= strchr(lstart
+ strlen(key
), '=');
2530 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2533 /* replace the olddir with newdir */
2534 if (olddirlen
>= newdirlen
) {
2535 size_t diff
= olddirlen
- newdirlen
;
2536 memcpy(p
, newdir
, newdirlen
);
2537 if (olddirlen
!= newdirlen
) {
2538 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2539 strlen(p
) - newdirlen
- diff
+ 1);
2541 conf
->unexpanded_len
-= diff
;
2545 size_t diff
= newdirlen
- olddirlen
;
2546 size_t oldlen
= conf
->unexpanded_len
;
2547 size_t newlen
= oldlen
+ diff
;
2548 size_t poffset
= p
- conf
->unexpanded_config
;
2550 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2552 ERROR("failed to allocate memory");
2555 conf
->unexpanded_len
= newlen
;
2556 conf
->unexpanded_alloced
= newlen
+ 1;
2557 new[newlen
- 1] = '\0';
2558 lend
= new + (lend
- conf
->unexpanded_config
);
2559 /* move over the remainder to make room for the newdir
2561 memmove(new + poffset
+ newdirlen
,
2562 new + poffset
+ olddirlen
,
2563 oldlen
- poffset
- olddirlen
+ 1);
2564 conf
->unexpanded_config
= new;
2565 memcpy(new + poffset
, newdir
, newdirlen
);
2578 ERROR("Error writing to new config"); \
2584 * This is called only from clone. We wish to update all hwaddrs in the
2585 * unexpanded config file. We can't/don't want to update any which come from
2586 * lxc.includes (there shouldn't be any).
2587 * We can't just walk the c->lxc-conf->network list because that includes netifs
2588 * from the include files. So we update the ones which we find in the unexp
2589 * config file, then find the original macaddr in the conf->network, and update
2590 * that to the same value.
2592 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2594 char *lend
, *p
, *p2
;
2595 struct lxc_list
*it
;
2596 char *lstart
= conf
->unexpanded_config
;
2598 if (!conf
->unexpanded_config
)
2602 char newhwaddr
[18], oldhwaddr
[17];
2604 lend
= strchr(lstart
, '\n');
2606 lend
= lstart
+ strlen(lstart
);
2610 if (!lxc_config_net_hwaddr(lstart
)) {
2615 p
= strchr(lstart
, '=');
2628 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2631 if ((p2
- p
) != 17) {
2632 WARN("Bad hwaddr entry");
2637 memcpy(oldhwaddr
, p
, 17);
2639 if (!new_hwaddr(newhwaddr
))
2642 memcpy(p
, newhwaddr
, 17);
2643 lxc_list_for_each(it
, &conf
->network
)
2645 struct lxc_netdev
*n
= it
->elem
;
2646 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2647 memcpy(n
->hwaddr
, newhwaddr
, 17);
2656 static int set_config_ephemeral(const char *key
, const char *value
,
2657 struct lxc_conf
*lxc_conf
, void *data
)
2659 /* Set config value to default. */
2660 if (lxc_config_value_empty(value
)) {
2661 lxc_conf
->ephemeral
= 0;
2665 /* Parse new config value. */
2666 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2669 if (lxc_conf
->ephemeral
> 1) {
2671 "Wrong value for lxc.ephemeral. Can only be set to 0 or 1");
2678 static int set_config_syslog(const char *key
, const char *value
,
2679 struct lxc_conf
*lxc_conf
, void *data
)
2683 /* Clear any previously set value. */
2684 if (lxc_conf
->syslog
) {
2685 free(lxc_conf
->syslog
);
2686 lxc_conf
->syslog
= NULL
;
2689 /* Check if value is empty. */
2690 if (lxc_config_value_empty(value
))
2694 facility
= lxc_syslog_priority_to_int(value
);
2695 if (facility
== -EINVAL
) {
2696 ERROR("Wrong value for lxc.syslog.");
2700 lxc_log_syslog(facility
);
2701 return set_config_string_item(&lxc_conf
->syslog
, value
);
2704 static int set_config_no_new_privs(const char *key
, const char *value
,
2705 struct lxc_conf
*lxc_conf
, void *data
)
2709 /* Set config value to default. */
2710 if (lxc_config_value_empty(value
)) {
2711 lxc_conf
->no_new_privs
= false;
2715 /* Parse new config value. */
2716 if (lxc_safe_uint(value
, &v
) < 0)
2720 ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 "
2725 lxc_conf
->no_new_privs
= v
? true : false;
2730 /* Callbacks to get configuration items. */
2731 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2732 struct lxc_conf
*c
, void *data
)
2739 memset(retv
, 0, inlen
);
2741 #if HAVE_SYS_PERSONALITY_H
2744 switch (c
->personality
) {
2746 strprint(retv
, inlen
, "i686");
2749 strprint(retv
, inlen
, "x86_64");
2759 static int get_config_pts(const char *key
, char *retv
, int inlen
,
2760 struct lxc_conf
*c
, void *data
)
2762 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2765 static int get_config_tty(const char *key
, char *retv
, int inlen
,
2766 struct lxc_conf
*c
, void *data
)
2768 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2771 static int get_config_ttydir(const char *key
, char *retv
, int inlen
,
2772 struct lxc_conf
*c
, void *data
)
2774 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2777 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2778 struct lxc_conf
*c
, void *data
)
2780 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2783 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2784 int inlen
, struct lxc_conf
*c
,
2787 return lxc_get_conf_int(c
, retv
, inlen
,
2788 c
->lsm_aa_allow_incomplete
);
2791 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2792 struct lxc_conf
*c
, void *data
)
2794 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2798 * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list,
2799 * then just the value(s) will be printed. Since there still could be
2800 * more than one, it is newline-separated.
2801 * (Maybe that's ambigous, since some values, i.e. devices.list, will
2802 * already have newlines?)
2803 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed,
2804 * in 'lxc.cgroup.subsystem.key = value' format.
2806 static int get_config_cgroup(const char *key
, char *retv
, int inlen
,
2807 struct lxc_conf
*c
, void *data
)
2809 struct lxc_list
*it
;
2812 bool get_all
= false;
2817 memset(retv
, 0, inlen
);
2819 if (!strcmp(key
, "lxc.cgroup"))
2821 else if (!strncmp(key
, "lxc.cgroup.", 11))
2826 lxc_list_for_each(it
, &c
->cgroup
) {
2827 struct lxc_cgroup
*cg
= it
->elem
;
2829 strprint(retv
, inlen
, "lxc.cgroup.%s = %s\n", cg
->subsystem
, cg
->value
);
2830 } else if (!strcmp(cg
->subsystem
, key
)) {
2831 strprint(retv
, inlen
, "%s\n", cg
->value
);
2838 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2839 struct lxc_conf
*c
, void *data
)
2841 struct lxc_list
*it
;
2842 int len
, listlen
, ret
;
2844 /* "u 1000 1000000 65536"
2846 * let's render this as
2864 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2865 char buf
[__LXC_IDMAP_STR_BUF
];
2870 memset(retv
, 0, inlen
);
2872 listlen
= lxc_list_len(&c
->id_map
);
2873 lxc_list_for_each(it
, &c
->id_map
)
2875 struct id_map
*map
= it
->elem
;
2876 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
2877 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
2878 map
->nsid
, map
->hostid
, map
->range
);
2879 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
2882 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
2887 static int get_config_loglevel(const char *key
, char *retv
, int inlen
,
2888 struct lxc_conf
*c
, void *data
)
2891 v
= lxc_log_priority_to_string(c
->loglevel
);
2892 return lxc_get_conf_str(retv
, inlen
, v
);
2895 static int get_config_logfile(const char *key
, char *retv
, int inlen
,
2896 struct lxc_conf
*c
, void *data
)
2898 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
2901 static int get_config_fstab(const char *key
, char *retv
, int inlen
,
2902 struct lxc_conf
*c
, void *data
)
2904 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
2907 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
2908 struct lxc_conf
*c
, void *data
)
2910 int len
, fulllen
= 0;
2911 const char *sep
= "";
2916 memset(retv
, 0, inlen
);
2918 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
2921 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
2922 case LXC_AUTO_PROC_MIXED
:
2923 strprint(retv
, inlen
, "%sproc:mixed", sep
);
2926 case LXC_AUTO_PROC_RW
:
2927 strprint(retv
, inlen
, "%sproc:rw", sep
);
2934 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
2935 case LXC_AUTO_SYS_RO
:
2936 strprint(retv
, inlen
, "%ssys:ro", sep
);
2939 case LXC_AUTO_SYS_RW
:
2940 strprint(retv
, inlen
, "%ssys:rw", sep
);
2943 case LXC_AUTO_SYS_MIXED
:
2944 strprint(retv
, inlen
, "%ssys:mixed", sep
);
2951 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
2952 case LXC_AUTO_CGROUP_NOSPEC
:
2953 strprint(retv
, inlen
, "%scgroup", sep
);
2956 case LXC_AUTO_CGROUP_MIXED
:
2957 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
2960 case LXC_AUTO_CGROUP_RO
:
2961 strprint(retv
, inlen
, "%scgroup:ro", sep
);
2964 case LXC_AUTO_CGROUP_RW
:
2965 strprint(retv
, inlen
, "%scgroup:rw", sep
);
2968 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
2969 strprint(retv
, inlen
, "%scgroup-full", sep
);
2972 case LXC_AUTO_CGROUP_FULL_MIXED
:
2973 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
2976 case LXC_AUTO_CGROUP_FULL_RO
:
2977 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
2980 case LXC_AUTO_CGROUP_FULL_RW
:
2981 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
2991 static int get_config_mount(const char *key
, char *retv
, int inlen
,
2992 struct lxc_conf
*c
, void *data
)
2994 int len
, fulllen
= 0;
2995 struct lxc_list
*it
;
3000 memset(retv
, 0, inlen
);
3002 lxc_list_for_each(it
, &c
->mount_list
)
3004 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3010 static int get_config_rootfs(const char *key
, char *retv
, int inlen
,
3011 struct lxc_conf
*c
, void *data
)
3013 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3016 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3017 struct lxc_conf
*c
, void *data
)
3019 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3022 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3023 struct lxc_conf
*c
, void *data
)
3025 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3028 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3029 struct lxc_conf
*c
, void *data
)
3031 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.bdev_type
);
3034 static int get_config_utsname(const char *key
, char *retv
, int inlen
,
3035 struct lxc_conf
*c
, void *data
)
3037 return lxc_get_conf_str(
3039 c
->utsname
? c
->utsname
->nodename
: NULL
);
3042 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3043 struct lxc_conf
*c
, void *data
)
3046 int len
, fulllen
= 0, found
= -1;
3047 struct lxc_list
*it
;
3050 /* "lxc.hook.mount" */
3051 subkey
= strchr(key
, '.');
3053 subkey
= strchr(subkey
+ 1, '.');
3059 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3060 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3071 memset(retv
, 0, inlen
);
3073 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3074 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3079 static int get_config_net(const char *key
, char *retv
, int inlen
,
3080 struct lxc_conf
*c
, void *data
)
3082 int len
, fulllen
= 0;
3083 struct lxc_list
*it
;
3088 memset(retv
, 0, inlen
);
3090 lxc_list_for_each(it
, &c
->network
) {
3091 struct lxc_netdev
*n
= it
->elem
;
3092 const char *t
= lxc_net_type_to_str(n
->type
);
3093 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3099 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3100 struct lxc_conf
*c
, void *data
)
3102 int len
, fulllen
= 0;
3103 struct lxc_list
*it
;
3108 memset(retv
, 0, inlen
);
3110 lxc_list_for_each(it
, &c
->caps
) {
3111 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3116 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3117 struct lxc_conf
*c
, void *data
)
3119 int len
, fulllen
= 0;
3120 struct lxc_list
*it
;
3125 memset(retv
, 0, inlen
);
3127 lxc_list_for_each(it
, &c
->keepcaps
) {
3128 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3133 static int get_config_console(const char *key
, char *retv
, int inlen
,
3134 struct lxc_conf
*c
, void *data
)
3136 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3139 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3140 struct lxc_conf
*c
, void *data
)
3142 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3145 static int get_config_seccomp(const char *key
, char *retv
, int inlen
,
3146 struct lxc_conf
*c
, void *data
)
3148 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3151 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3152 struct lxc_conf
*c
, void *data
)
3154 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3157 static int get_config_haltsignal(const char *key
, char *retv
, int inlen
,
3158 struct lxc_conf
*c
, void *data
)
3160 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3163 static int get_config_rebootsignal(const char *key
, char *retv
, int inlen
,
3164 struct lxc_conf
*c
, void *data
)
3166 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3169 static int get_config_stopsignal(const char *key
, char *retv
, int inlen
,
3170 struct lxc_conf
*c
, void *data
)
3172 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3175 static int get_config_start(const char *key
, char *retv
, int inlen
,
3176 struct lxc_conf
*c
, void *data
)
3178 if (strcmp(key
+ 10, "auto") == 0)
3179 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3180 else if (strcmp(key
+ 10, "delay") == 0)
3181 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3182 else if (strcmp(key
+ 10, "order") == 0)
3183 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3188 static int get_config_syslog(const char *key
, char *retv
, int inlen
,
3189 struct lxc_conf
*c
, void *data
)
3191 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3194 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3195 struct lxc_conf
*c
, void *data
)
3197 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3200 static int get_config_group(const char *key
, char *retv
, int inlen
,
3201 struct lxc_conf
*c
, void *data
)
3203 int len
, fulllen
= 0;
3204 struct lxc_list
*it
;
3209 memset(retv
, 0, inlen
);
3211 lxc_list_for_each(it
, &c
->groups
) {
3212 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3217 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3218 struct lxc_conf
*c
, void *data
)
3220 int len
, fulllen
= 0;
3221 struct lxc_list
*it
;
3226 memset(retv
, 0, inlen
);
3228 lxc_list_for_each(it
, &c
->environment
) {
3229 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3234 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3235 struct lxc_conf
*c
, void *data
)
3237 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3240 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3241 struct lxc_conf
*c
, void *data
)
3243 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3246 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3247 struct lxc_conf
*c
, void *data
)
3249 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3252 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3253 struct lxc_conf
*c
, void *data
)
3255 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3258 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3259 struct lxc_conf
*c
, void *data
)
3261 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3265 * If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3266 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3267 * printed, in 'lxc.prlimit.resource = value' format.
3269 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3270 struct lxc_conf
*c
, void *data
)
3272 int fulllen
= 0, len
;
3273 bool get_all
= false;
3274 struct lxc_list
*it
;
3279 memset(retv
, 0, inlen
);
3281 if (!strcmp(key
, "lxc.prlimit"))
3283 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3288 lxc_list_for_each(it
, &c
->limits
) {
3289 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3290 integers or the word
3293 struct lxc_limit
*lim
= it
->elem
;
3295 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3296 memcpy(buf
, "unlimited", sizeof("unlimited"));
3297 partlen
= sizeof("unlimited") - 1;
3299 partlen
= sprintf(buf
, "%" PRIu64
,
3300 (uint64_t)lim
->limit
.rlim_cur
);
3302 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3303 if (lim
->limit
.rlim_max
== RLIM_INFINITY
) {
3304 memcpy(buf
+ partlen
, ":unlimited",
3305 sizeof(":unlimited"));
3307 sprintf(buf
+ partlen
, ":%" PRIu64
,
3308 (uint64_t)lim
->limit
.rlim_max
);
3313 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3314 lim
->resource
, buf
);
3315 } else if (strcmp(lim
->resource
, key
) == 0) {
3316 strprint(retv
, inlen
, "%s", buf
);
3323 /* Callbacks to clear config items. */
3324 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3327 c
->personality
= -1;
3331 static inline int clr_config_pts(const char *key
, struct lxc_conf
*c
,
3338 static inline int clr_config_tty(const char *key
, struct lxc_conf
*c
,
3345 static inline int clr_config_ttydir(const char *key
, struct lxc_conf
*c
,
3353 static inline int clr_config_apparmor_profile(const char *key
,
3354 struct lxc_conf
*c
, void *data
)
3356 free(c
->lsm_aa_profile
);
3357 c
->lsm_aa_profile
= NULL
;
3361 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3365 c
->lsm_aa_allow_incomplete
= 0;
3369 static inline int clr_config_selinux_context(const char *key
,
3370 struct lxc_conf
*c
, void *data
)
3372 free(c
->lsm_se_context
);
3373 c
->lsm_se_context
= NULL
;
3377 static inline int clr_config_cgroup(const char *key
, struct lxc_conf
*c
,
3380 return lxc_clear_cgroups(c
, key
);
3383 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3386 return lxc_clear_idmaps(c
);
3389 static inline int clr_config_loglevel(const char *key
, struct lxc_conf
*c
,
3392 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3396 static inline int clr_config_logfile(const char *key
, struct lxc_conf
*c
,
3404 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3407 return lxc_clear_mount_entries(c
);
3410 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3413 return lxc_clear_automounts(c
);
3416 static inline int clr_config_fstab(const char *key
, struct lxc_conf
*c
,
3424 static inline int clr_config_rootfs(const char *key
, struct lxc_conf
*c
,
3427 free(c
->rootfs
.path
);
3428 c
->rootfs
.path
= NULL
;
3432 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3435 free(c
->rootfs
.mount
);
3436 c
->rootfs
.mount
= NULL
;
3440 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3443 free(c
->rootfs
.options
);
3444 c
->rootfs
.options
= NULL
;
3448 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3451 free(c
->rootfs
.bdev_type
);
3452 c
->rootfs
.bdev_type
= NULL
;
3456 static inline int clr_config_utsname(const char *key
, struct lxc_conf
*c
,
3464 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3467 return lxc_clear_hooks(c
, key
);
3470 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3473 lxc_free_networks(&c
->network
);
3478 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3481 return lxc_clear_config_caps(c
);
3484 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3487 return lxc_clear_config_keepcaps(c
);
3490 static inline int clr_config_console(const char *key
, struct lxc_conf
*c
,
3493 free(c
->console
.path
);
3494 c
->console
.path
= NULL
;
3498 static inline int clr_config_console_logfile(const char *key
,
3499 struct lxc_conf
*c
, void *data
)
3501 free(c
->console
.log_path
);
3502 c
->console
.log_path
= NULL
;
3506 static inline int clr_config_seccomp(const char *key
, struct lxc_conf
*c
,
3514 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3521 static inline int clr_config_haltsignal(const char *key
, struct lxc_conf
*c
,
3528 static inline int clr_config_rebootsignal(const char *key
, struct lxc_conf
*c
,
3531 c
->rebootsignal
= 0;
3535 static inline int clr_config_stopsignal(const char *key
, struct lxc_conf
*c
,
3542 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3545 if (strcmp(key
+ 10, "auto") == 0)
3547 else if (strcmp(key
+ 10, "delay") == 0)
3549 else if (strcmp(key
+ 10, "order") == 0)
3555 static inline int clr_config_syslog(const char *key
, struct lxc_conf
*c
,
3563 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3566 c
->monitor_unshare
= 0;
3570 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3573 return lxc_clear_groups(c
);
3576 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3579 return lxc_clear_environment(c
);
3582 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3590 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3597 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3604 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3611 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3614 c
->no_new_privs
= false;
3618 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3621 return lxc_clear_limits(c
, key
);
3624 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3627 lxc_clear_includes(c
);
3631 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
3632 struct lxc_conf
*c
, void *data
)
3637 static struct lxc_config_t
*
3638 get_network_config_ops(const char *key
, struct lxc_conf
*lxc_conf
, ssize_t
*idx
)
3640 char *copy
, *idx_start
, *idx_end
;
3641 struct lxc_config_t
*config
= NULL
;
3643 /* check that this is a sensible network key */
3644 if (strncmp("lxc.net.", key
, 8))
3652 if (isdigit(*(key
+ 8))) {
3654 unsigned int tmpidx
;
3657 /* beginning of index string */
3658 idx_start
= (copy
+ 7);
3661 /* end of index string */
3662 idx_end
= strchr((copy
+ 8), '.');
3667 /* parse current index */
3668 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
3674 /* This, of course is utterly nonsensical on so many levels, but
3675 * better safe than sorry.
3676 * (Checking for INT_MAX here is intentional.)
3678 if (tmpidx
== INT_MAX
) {
3680 "number of configured networks would overflow the "
3681 "counter... what are you doing?");
3686 numstrlen
= strlen((idx_start
+ 1));
3688 /* repair configuration key */
3692 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
3693 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
3696 config
= lxc_getconfig(copy
);
3698 ERROR("unknown network configuration key %s", key
);
3706 * Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.'
3707 * was found. So we make sure next comes an integer, find the right callback
3708 * (by rewriting the key), and call it.
3710 static int set_config_net_nic(const char *key
, const char *value
,
3711 struct lxc_conf
*lxc_conf
, void *data
)
3713 struct lxc_config_t
*config
;
3714 struct lxc_netdev
*netdev
;
3717 if (lxc_config_value_empty(value
))
3718 return clr_config_net_nic(key
, lxc_conf
, data
);
3720 config
= get_network_config_ops(key
, lxc_conf
, &idx
);
3721 if (!config
|| idx
< 0)
3724 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
3728 return config
->set(key
, value
, lxc_conf
, netdev
);
3732 * Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.'
3733 * was found. So we make sure next comes an integer, find the right callback
3734 * (by rewriting the key), and call it.
3736 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
3739 const char *idxstring
;
3740 struct lxc_config_t
*config
;
3741 struct lxc_netdev
*netdev
;
3744 /* If we get passed "lxc.net.<n>" we clear the whole network. */
3745 if (strncmp("lxc.net.", key
, 8))
3748 idxstring
= key
+ 8;
3749 /* The left conjunct is pretty self-explanatory. The right conjunct
3750 * checks whether the two pointers are equal. If they are we now that
3751 * this is not a key that is namespaced any further and so we are
3752 * supposed to clear the whole network.
3754 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
3755 unsigned int rmnetdevidx
;
3757 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
3760 /* Remove network from network list. */
3761 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
3765 config
= get_network_config_ops(key
, lxc_conf
, &idx
);
3766 if (!config
|| idx
< 0)
3769 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
3773 return config
->clr(key
, lxc_conf
, netdev
);
3776 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
3779 struct lxc_netdev
*netdev
;
3793 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
3796 struct lxc_netdev
*netdev
;
3806 netdev
->name
= NULL
;
3811 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
3814 struct lxc_netdev
*netdev
;
3828 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
3831 struct lxc_netdev
*netdev
;
3841 netdev
->link
= NULL
;
3846 static int clr_config_net_macvlan_mode(const char *key
,
3847 struct lxc_conf
*lxc_conf
, void *data
)
3849 struct lxc_netdev
*netdev
;
3858 if (netdev
->type
!= LXC_NET_MACVLAN
)
3861 netdev
->priv
.macvlan_attr
.mode
= -1;
3866 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
3869 struct lxc_netdev
*netdev
;
3878 free(netdev
->priv
.veth_attr
.pair
);
3879 netdev
->priv
.veth_attr
.pair
= NULL
;
3884 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
3887 struct lxc_netdev
*netdev
;
3896 free(netdev
->upscript
);
3897 netdev
->upscript
= NULL
;
3902 static int clr_config_net_script_down(const char *key
,
3903 struct lxc_conf
*lxc_conf
, void *data
)
3905 struct lxc_netdev
*netdev
;
3914 free(netdev
->downscript
);
3915 netdev
->downscript
= NULL
;
3920 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
3923 struct lxc_netdev
*netdev
;
3932 free(netdev
->hwaddr
);
3933 netdev
->hwaddr
= NULL
;
3938 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
3941 struct lxc_netdev
*netdev
;
3956 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
3959 struct lxc_netdev
*netdev
;
3968 netdev
->priv
.vlan_attr
.vid
= 0;
3973 static int clr_config_net_ipv4_gateway(const char *key
,
3974 struct lxc_conf
*lxc_conf
, void *data
)
3976 struct lxc_netdev
*netdev
;
3985 free(netdev
->ipv4_gateway
);
3986 netdev
->ipv4_gateway
= NULL
;
3991 static int clr_config_net_ipv4(const char *key
, struct lxc_conf
*lxc_conf
,
3994 struct lxc_netdev
*netdev
;
3995 struct lxc_list
*cur
, *next
;
4004 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4013 static int clr_config_net_ipv6_gateway(const char *key
,
4014 struct lxc_conf
*lxc_conf
, void *data
)
4016 struct lxc_netdev
*netdev
;
4025 free(netdev
->ipv6_gateway
);
4026 netdev
->ipv6_gateway
= NULL
;
4031 static int clr_config_net_ipv6(const char *key
, struct lxc_conf
*lxc_conf
,
4034 struct lxc_netdev
*netdev
;
4035 struct lxc_list
*cur
, *next
;
4044 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4053 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4054 struct lxc_conf
*c
, void *data
)
4056 struct lxc_config_t
*config
;
4057 struct lxc_netdev
*netdev
;
4060 config
= get_network_config_ops(key
, c
, &idx
);
4061 if (!config
|| idx
< 0)
4064 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4068 return config
->get(key
, retv
, inlen
, c
, netdev
);
4071 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4072 struct lxc_conf
*c
, void *data
)
4074 int len
, fulllen
= 0;
4075 struct lxc_netdev
*netdev
;
4080 memset(retv
, 0, inlen
);
4089 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4094 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4095 struct lxc_conf
*c
, void *data
)
4097 int len
, fulllen
= 0;
4098 struct lxc_netdev
*netdev
;
4103 memset(retv
, 0, inlen
);
4112 if (netdev
->flags
& IFF_UP
)
4113 strprint(retv
, inlen
, "up");
4118 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4119 struct lxc_conf
*c
, void *data
)
4121 int len
, fulllen
= 0;
4122 struct lxc_netdev
*netdev
;
4127 memset(retv
, 0, inlen
);
4137 strprint(retv
, inlen
, "%s", netdev
->link
);
4142 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4143 struct lxc_conf
*c
, void *data
)
4145 int len
, fulllen
= 0;
4146 struct lxc_netdev
*netdev
;
4151 memset(retv
, 0, inlen
);
4161 strprint(retv
, inlen
, "%s", netdev
->name
);
4166 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4167 struct lxc_conf
*c
, void *data
)
4170 int len
, fulllen
= 0;
4171 struct lxc_netdev
*netdev
;
4176 memset(retv
, 0, inlen
);
4185 if (netdev
->type
!= LXC_NET_MACVLAN
)
4188 switch (netdev
->priv
.macvlan_attr
.mode
) {
4189 case MACVLAN_MODE_PRIVATE
:
4192 case MACVLAN_MODE_VEPA
:
4195 case MACVLAN_MODE_BRIDGE
:
4198 case MACVLAN_MODE_PASSTHRU
:
4206 strprint(retv
, inlen
, "%s", mode
);
4211 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4212 struct lxc_conf
*c
, void *data
)
4214 int len
, fulllen
= 0;
4215 struct lxc_netdev
*netdev
;
4220 memset(retv
, 0, inlen
);
4229 if (netdev
->type
!= LXC_NET_VETH
)
4232 strprint(retv
, inlen
, "%s",
4233 netdev
->priv
.veth_attr
.pair
? netdev
->priv
.veth_attr
.pair
4234 : netdev
->priv
.veth_attr
.veth1
);
4239 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4240 struct lxc_conf
*c
, void *data
)
4242 int len
, fulllen
= 0;
4243 struct lxc_netdev
*netdev
;
4248 memset(retv
, 0, inlen
);
4257 if (netdev
->upscript
)
4258 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4263 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4264 struct lxc_conf
*c
, void *data
)
4266 int len
, fulllen
= 0;
4267 struct lxc_netdev
*netdev
;
4272 memset(retv
, 0, inlen
);
4281 if (netdev
->downscript
)
4282 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4287 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4288 struct lxc_conf
*c
, void *data
)
4290 int len
, fulllen
= 0;
4291 struct lxc_netdev
*netdev
;
4296 memset(retv
, 0, inlen
);
4306 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4311 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4312 struct lxc_conf
*c
, void *data
)
4314 int len
, fulllen
= 0;
4315 struct lxc_netdev
*netdev
;
4320 memset(retv
, 0, inlen
);
4330 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4335 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4336 struct lxc_conf
*c
, void *data
)
4338 int len
, fulllen
= 0;
4339 struct lxc_netdev
*netdev
;
4344 memset(retv
, 0, inlen
);
4353 if (netdev
->type
!= LXC_NET_VLAN
)
4356 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4361 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4362 struct lxc_conf
*c
, void *data
)
4364 int len
, fulllen
= 0;
4365 char buf
[INET_ADDRSTRLEN
];
4366 struct lxc_netdev
*netdev
;
4371 memset(retv
, 0, inlen
);
4380 if (netdev
->ipv4_gateway_auto
) {
4381 strprint(retv
, inlen
, "auto");
4382 } else if (netdev
->ipv4_gateway
) {
4383 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4384 strprint(retv
, inlen
, "%s", buf
);
4390 static int get_config_net_ipv4(const char *key
, char *retv
, int inlen
,
4391 struct lxc_conf
*c
, void *data
)
4393 int len
, fulllen
= 0;
4395 char buf
[INET_ADDRSTRLEN
];
4396 struct lxc_netdev
*netdev
;
4397 struct lxc_list
*it
;
4402 memset(retv
, 0, inlen
);
4411 listlen
= lxc_list_len(&netdev
->ipv4
);
4412 lxc_list_for_each(it
, &netdev
->ipv4
) {
4413 struct lxc_inetdev
*i
= it
->elem
;
4414 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4415 strprint(retv
, inlen
, "%s/%d%s", buf
, i
->prefix
,
4416 (listlen
-- > 1) ? "\n" : "");
4422 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4423 struct lxc_conf
*c
, void *data
)
4425 int len
, fulllen
= 0;
4426 char buf
[INET6_ADDRSTRLEN
];
4427 struct lxc_netdev
*netdev
;
4432 memset(retv
, 0, inlen
);
4441 if (netdev
->ipv6_gateway_auto
) {
4442 strprint(retv
, inlen
, "auto");
4443 } else if (netdev
->ipv6_gateway
) {
4444 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4445 strprint(retv
, inlen
, "%s", buf
);
4451 static int get_config_net_ipv6(const char *key
, char *retv
, int inlen
,
4452 struct lxc_conf
*c
, void *data
)
4454 int len
, fulllen
= 0;
4456 char buf
[INET6_ADDRSTRLEN
];
4457 struct lxc_netdev
*netdev
;
4458 struct lxc_list
*it
;
4463 memset(retv
, 0, inlen
);
4472 listlen
= lxc_list_len(&netdev
->ipv6
);
4473 lxc_list_for_each(it
, &netdev
->ipv6
) {
4474 struct lxc_inet6dev
*i
= it
->elem
;
4475 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4476 strprint(retv
, inlen
, "%s/%d%s", buf
, i
->prefix
,
4477 (listlen
-- > 1) ? "\n" : "");