]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile_utils.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <arpa/inet.h>
32 #include "confile_utils.h"
36 #include "lxccontainer.h"
43 #include "include/strlcpy.h"
46 lxc_log_define(confile_utils
, lxc
);
48 int parse_idmaps(const char *idmap
, char *type
, unsigned long *nsid
,
49 unsigned long *hostid
, unsigned long *range
)
52 unsigned long tmp_hostid
, tmp_nsid
, tmp_range
;
57 /* Duplicate string. */
62 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
67 slide
+= strspn(slide
, " \t\r");
68 if (slide
!= window
&& *slide
== '\0')
72 if (*slide
!= 'u' && *slide
!= 'g') {
73 ERROR("Invalid id mapping type: %c", *slide
);
80 /* move beyond type */
84 /* Validate that only whitespace follows. */
85 slide
+= strspn(slide
, " \t\r");
86 /* There must be whitespace. */
90 /* Mark beginning of nsid. */
92 /* Validate that non-whitespace follows. */
93 slide
+= strcspn(slide
, " \t\r");
94 /* There must be non-whitespace. */
95 if (slide
== window
|| *slide
== '\0')
97 /* Mark end of nsid. */
101 if (lxc_safe_ulong(window
, &tmp_nsid
) < 0) {
102 ERROR("Failed to parse nsid: %s", window
);
106 /* Move beyond \0. */
108 /* Validate that only whitespace follows. */
109 slide
+= strspn(slide
, " \t\r");
110 /* If there was only one whitespace then we whiped it with our \0 above.
111 * So only ensure that we're not at the end of the string.
116 /* Mark beginning of hostid. */
118 /* Validate that non-whitespace follows. */
119 slide
+= strcspn(slide
, " \t\r");
120 /* There must be non-whitespace. */
121 if (slide
== window
|| *slide
== '\0')
123 /* Mark end of nsid. */
127 if (lxc_safe_ulong(window
, &tmp_hostid
) < 0) {
128 ERROR("Failed to parse hostid: %s", window
);
132 /* Move beyond \0. */
134 /* Validate that only whitespace follows. */
135 slide
+= strspn(slide
, " \t\r");
136 /* If there was only one whitespace then we whiped it with our \0 above.
137 * So only ensure that we're not at the end of the string.
142 /* Mark beginning of range. */
144 /* Validate that non-whitespace follows. */
145 slide
+= strcspn(slide
, " \t\r");
146 /* There must be non-whitespace. */
150 /* The range is the last valid entry we expect. So make sure that there
151 * is no trailing garbage and if there is, error out.
153 if (*(slide
+ strspn(slide
, " \t\r\n")) != '\0')
156 /* Mark end of range. */
160 if (lxc_safe_ulong(window
, &tmp_range
) < 0) {
161 ERROR("Failed to parse id mapping range: %s", window
);
167 *hostid
= tmp_hostid
;
170 /* Yay, we survived. */
179 bool lxc_config_value_empty(const char *value
)
181 if (value
&& strlen(value
) > 0)
187 struct lxc_netdev
*lxc_network_add(struct lxc_list
*networks
, int idx
, bool tail
)
189 struct lxc_list
*newlist
;
190 struct lxc_netdev
*netdev
= NULL
;
192 /* network does not exist */
193 netdev
= malloc(sizeof(*netdev
));
197 memset(netdev
, 0, sizeof(*netdev
));
198 lxc_list_init(&netdev
->ipv4
);
199 lxc_list_init(&netdev
->ipv6
);
201 /* give network a unique index */
204 /* prepare new list */
205 newlist
= malloc(sizeof(*newlist
));
211 lxc_list_init(newlist
);
212 newlist
->elem
= netdev
;
215 lxc_list_add_tail(networks
, newlist
);
217 lxc_list_add(networks
, newlist
);
222 /* Takes care of finding the correct netdev struct in the networks list or
223 * allocates a new one if it couldn't be found.
225 struct lxc_netdev
*lxc_get_netdev_by_idx(struct lxc_conf
*conf
,
226 unsigned int idx
, bool allocate
)
228 struct lxc_netdev
*netdev
= NULL
;
229 struct lxc_list
*networks
= &conf
->network
;
230 struct lxc_list
*insert
= networks
;
233 if (!lxc_list_empty(networks
)) {
234 lxc_list_for_each(insert
, networks
) {
235 netdev
= insert
->elem
;
236 if (netdev
->idx
== idx
)
238 else if (netdev
->idx
> idx
)
246 return lxc_network_add(insert
, idx
, true);
249 void lxc_log_configured_netdevs(const struct lxc_conf
*conf
)
251 struct lxc_netdev
*netdev
;
252 struct lxc_list
*it
= (struct lxc_list
*)&conf
->network
;;
254 if ((conf
->loglevel
!= LXC_LOG_LEVEL_TRACE
) &&
255 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE
))
258 if (lxc_list_empty(it
)) {
259 TRACE("container has no networks configured");
263 lxc_list_for_each(it
, &conf
->network
) {
264 struct lxc_list
*cur
, *next
;
265 struct lxc_inetdev
*inet4dev
;
266 struct lxc_inet6dev
*inet6dev
;
267 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
271 TRACE("index: %zd", netdev
->idx
);
272 TRACE("ifindex: %d", netdev
->ifindex
);
274 switch (netdev
->type
) {
278 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
279 TRACE("veth pair: %s",
280 netdev
->priv
.veth_attr
.pair
);
282 if (netdev
->priv
.veth_attr
.veth1
[0] != '\0')
284 netdev
->priv
.veth_attr
.veth1
);
286 if (netdev
->priv
.veth_attr
.ifindex
> 0)
287 TRACE("host side ifindex for veth device: %d",
288 netdev
->priv
.veth_attr
.ifindex
);
290 case LXC_NET_MACVLAN
:
291 TRACE("type: macvlan");
293 if (netdev
->priv
.macvlan_attr
.mode
> 0) {
296 mode
= lxc_macvlan_flag_to_mode(
297 netdev
->priv
.macvlan_attr
.mode
);
298 TRACE("macvlan mode: %s",
299 mode
? mode
: "(invalid mode)");
304 TRACE("vlan id: %d", netdev
->priv
.vlan_attr
.vid
);
309 if (netdev
->priv
.phys_attr
.ifindex
> 0)
310 TRACE("host side ifindex for phys device: %d",
311 netdev
->priv
.phys_attr
.ifindex
);
314 TRACE("type: empty");
320 ERROR("invalid network type %d", netdev
->type
);
324 if (netdev
->type
!= LXC_NET_EMPTY
) {
326 netdev
->flags
== IFF_UP
? "up" : "none");
328 if (netdev
->link
[0] != '\0')
329 TRACE("link: %s", netdev
->link
);
331 if (netdev
->name
[0] != '\0')
332 TRACE("name: %s", netdev
->name
);
335 TRACE("hwaddr: %s", netdev
->hwaddr
);
338 TRACE("mtu: %s", netdev
->mtu
);
340 if (netdev
->upscript
)
341 TRACE("upscript: %s", netdev
->upscript
);
343 if (netdev
->downscript
)
344 TRACE("downscript: %s", netdev
->downscript
);
346 TRACE("ipv4 gateway auto: %s",
347 netdev
->ipv4_gateway_auto
? "true" : "false");
349 if (netdev
->ipv4_gateway
) {
350 inet_ntop(AF_INET
, netdev
->ipv4_gateway
,
351 bufinet4
, sizeof(bufinet4
));
352 TRACE("ipv4 gateway: %s", bufinet4
);
355 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
356 inet4dev
= cur
->elem
;
357 inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
,
359 TRACE("ipv4 addr: %s", bufinet4
);
362 TRACE("ipv6 gateway auto: %s",
363 netdev
->ipv6_gateway_auto
? "true" : "false");
365 if (netdev
->ipv6_gateway
) {
366 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
,
367 bufinet6
, sizeof(bufinet6
));
368 TRACE("ipv6 gateway: %s", bufinet6
);
371 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
372 inet6dev
= cur
->elem
;
373 inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
,
375 TRACE("ipv6 addr: %s", bufinet6
);
381 static void lxc_free_netdev(struct lxc_netdev
*netdev
)
383 struct lxc_list
*cur
, *next
;
385 free(netdev
->upscript
);
386 free(netdev
->downscript
);
387 free(netdev
->hwaddr
);
390 free(netdev
->ipv4_gateway
);
391 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
397 free(netdev
->ipv6_gateway
);
398 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
407 bool lxc_remove_nic_by_idx(struct lxc_conf
*conf
, unsigned int idx
)
409 struct lxc_list
*cur
, *next
;
410 struct lxc_netdev
*netdev
;
413 lxc_list_for_each_safe(cur
, &conf
->network
, next
) {
415 if (netdev
->idx
!= idx
)
426 lxc_free_netdev(netdev
);
432 void lxc_free_networks(struct lxc_list
*networks
)
434 struct lxc_list
*cur
, *next
;
435 struct lxc_netdev
*netdev
;
437 lxc_list_for_each_safe(cur
, networks
, next
) {
439 lxc_free_netdev(netdev
);
443 /* prevent segfaults */
444 lxc_list_init(networks
);
447 static struct lxc_macvlan_mode
{
451 { "private", MACVLAN_MODE_PRIVATE
},
452 { "vepa", MACVLAN_MODE_VEPA
},
453 { "bridge", MACVLAN_MODE_BRIDGE
},
454 { "passthru", MACVLAN_MODE_PASSTHRU
},
457 int lxc_macvlan_mode_to_flag(int *mode
, const char *value
)
461 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
462 if (strcmp(macvlan_mode
[i
].name
, value
))
465 *mode
= macvlan_mode
[i
].mode
;
472 char *lxc_macvlan_flag_to_mode(int mode
)
476 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
477 if (macvlan_mode
[i
].mode
== mode
)
480 return macvlan_mode
[i
].name
;
486 int set_config_string_item(char **conf_item
, const char *value
)
490 if (lxc_config_value_empty(value
)) {
496 new_value
= strdup(value
);
498 SYSERROR("Failed to duplicate string \"%s\"", value
);
503 *conf_item
= new_value
;
507 int set_config_string_item_max(char **conf_item
, const char *value
, size_t max
)
509 if (strlen(value
) >= max
) {
510 ERROR("%s is too long (>= %lu)", value
, (unsigned long)max
);
514 return set_config_string_item(conf_item
, value
);
517 int set_config_path_item(char **conf_item
, const char *value
)
519 return set_config_string_item_max(conf_item
, value
, PATH_MAX
);
522 int config_ip_prefix(struct in_addr
*addr
)
524 if (IN_CLASSA(addr
->s_addr
))
525 return 32 - IN_CLASSA_NSHIFT
;
527 if (IN_CLASSB(addr
->s_addr
))
528 return 32 - IN_CLASSB_NSHIFT
;
530 if (IN_CLASSC(addr
->s_addr
))
531 return 32 - IN_CLASSC_NSHIFT
;
536 int network_ifname(char *valuep
, const char *value
, size_t size
)
540 if (!valuep
|| !value
)
543 retlen
= strlcpy(valuep
, value
, size
);
545 ERROR("Network device name \"%s\" is too long (>= %zu)", value
,
551 void rand_complete_hwaddr(char *hwaddr
)
553 const char hex
[] = "0123456789abcdef";
558 seed
= randseed(false);
561 (void)randseed(true);
564 while (*curs
!= '\0' && *curs
!= '\n') {
565 if (*curs
== 'x' || *curs
== 'X') {
566 if (curs
- hwaddr
== 1) {
567 /* ensure address is unicast */
569 *curs
= hex
[rand_r(&seed
) & 0x0E];
571 *curs
= hex
[rand_r(&seed
) & 0x0F];
573 *curs
= hex
[rand() & 0x0E];
575 *curs
= hex
[rand() & 0x0F];
583 bool lxc_config_net_hwaddr(const char *line
)
588 if (strncmp(line
, "lxc.net", 7) != 0)
591 if (strncmp(line
, "lxc.net.hwaddr", 14) == 0)
594 if (strncmp(line
, "lxc.network.hwaddr", 18) == 0)
597 if (sscanf(line
, "lxc.net.%u.%6s", &index
, tmp
) == 2 ||
598 sscanf(line
, "lxc.network.%u.%6s", &index
, tmp
) == 2)
599 return strncmp(tmp
, "hwaddr", 6) == 0;
605 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
606 * file, we expand it in the unexpanded_config, so that after a save_config we
607 * store the hwaddr for re-use.
608 * This is only called when reading the config file, not when executing a
610 * 'x' and 'X' are substituted in-place.
612 void update_hwaddr(const char *line
)
616 line
+= lxc_char_left_gc(line
, strlen(line
));
620 if (!lxc_config_net_hwaddr(line
))
623 /* Let config_net_hwaddr raise the error. */
624 p
= strchr(line
, '=');
635 rand_complete_hwaddr(p
);
638 bool new_hwaddr(char *hwaddr
)
644 seed
= randseed(false);
646 ret
= snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x", rand_r(&seed
) % 255,
647 rand_r(&seed
) % 255, rand_r(&seed
) % 255);
650 (void)randseed(true);
652 ret
= snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
653 rand() % 255, rand() % 255);
655 if (ret
< 0 || ret
>= 18) {
656 SYSERROR("Failed to call snprintf()");
663 int lxc_get_conf_str(char *retv
, int inlen
, const char *value
)
670 value_len
= strlen(value
);
671 if (retv
&& inlen
>= value_len
+ 1)
672 memcpy(retv
, value
, value_len
+ 1);
677 int lxc_get_conf_bool(struct lxc_conf
*c
, char *retv
, int inlen
, bool v
)
685 memset(retv
, 0, inlen
);
687 strprint(retv
, inlen
, "%d", v
);
692 int lxc_get_conf_int(struct lxc_conf
*c
, char *retv
, int inlen
, int v
)
700 memset(retv
, 0, inlen
);
702 strprint(retv
, inlen
, "%d", v
);
707 int lxc_get_conf_size_t(struct lxc_conf
*c
, char *retv
, int inlen
, size_t v
)
715 memset(retv
, 0, inlen
);
717 strprint(retv
, inlen
, "%zu", v
);
722 int lxc_get_conf_uint64(struct lxc_conf
*c
, char *retv
, int inlen
, uint64_t v
)
730 memset(retv
, 0, inlen
);
732 strprint(retv
, inlen
, "%"PRIu64
, v
);
737 bool parse_limit_value(const char **value
, rlim_t
*res
)
741 if (strncmp(*value
, "unlimited", STRLITERALLEN("unlimited")) == 0) {
742 *res
= RLIM_INFINITY
;
743 *value
+= STRLITERALLEN("unlimited");
748 *res
= strtoull(*value
, &endptr
, 10);
749 if (errno
|| !endptr
)
757 static int lxc_container_name_to_pid(const char *lxcname_or_pid
,
764 pid
= strtol(lxcname_or_pid
, &err
, 10);
765 if (*err
!= '\0' || pid
< 1) {
766 struct lxc_container
*c
;
768 c
= lxc_container_new(lxcname_or_pid
, lxcpath
);
770 ERROR("\"%s\" is not a valid pid nor a container name",
775 if (!c
->may_control(c
)) {
776 ERROR("Insufficient privileges to control container "
778 lxc_container_put(c
);
782 pid
= c
->init_pid(c
);
784 ERROR("Container \"%s\" is not running", c
->name
);
785 lxc_container_put(c
);
789 lxc_container_put(c
);
794 SYSERROR("Failed to send signal to pid %d", (int)pid
);
801 int lxc_inherit_namespace(const char *lxcname_or_pid
, const char *lxcpath
,
802 const char *namespace)
805 char *dup
, *lastslash
;
807 lastslash
= strrchr(lxcname_or_pid
, '/');
809 dup
= strdup(lxcname_or_pid
);
813 dup
[lastslash
- lxcname_or_pid
] = '\0';
814 pid
= lxc_container_name_to_pid(lastslash
+ 1, dup
);
817 pid
= lxc_container_name_to_pid(lxcname_or_pid
, lxcpath
);
823 fd
= lxc_preserve_ns(pid
, namespace);
835 static const struct signame signames
[] = {
868 { SIGSTKFLT
, "STKFLT" },
883 { SIGVTALRM
, "VTALRM" },
889 { SIGWINCH
, "WINCH" },
907 { SIGUNUSED
, "UNUSED" },
914 static int sig_num(const char *sig
)
918 if (lxc_safe_uint(sig
, &signum
) < 0)
924 static int rt_sig_num(const char *signame
)
926 int rtmax
= 0, sig_n
= 0;
928 if (strncasecmp(signame
, "max-", 4) == 0)
932 if (!isdigit(*signame
))
935 sig_n
= sig_num(signame
);
936 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
937 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
943 int sig_parse(const char *signame
)
947 if (isdigit(*signame
)) {
948 return sig_num(signame
);
949 } else if (strncasecmp(signame
, "sig", 3) == 0) {
951 if (strncasecmp(signame
, "rt", 2) == 0)
952 return rt_sig_num(signame
+ 2);
954 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++)
955 if (strcasecmp(signames
[n
].name
, signame
) == 0)
956 return signames
[n
].num
;