]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile_utils.c
confile_utils: cleanup lxc_ipvlan_mode
[mirror_lxc.git] / src / lxc / confile_utils.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <arpa/inet.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "conf.h"
13 #include "config.h"
14 #include "confile.h"
15 #include "confile_utils.h"
16 #include "error.h"
17 #include "list.h"
18 #include "log.h"
19 #include "lxccontainer.h"
20 #include "macro.h"
21 #include "memory_utils.h"
22 #include "network.h"
23 #include "parse.h"
24 #include "utils.h"
25
26 #ifndef HAVE_STRLCPY
27 #include "include/strlcpy.h"
28 #endif
29
30 lxc_log_define(confile_utils, lxc);
31
32 int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
33 unsigned long *hostid, unsigned long *range)
34 {
35 __do_free char *dup = NULL;
36 int ret = -1;
37 unsigned long tmp_hostid, tmp_nsid, tmp_range;
38 char tmp_type;
39 char *window, *slide;
40
41 /* Duplicate string. */
42 dup = strdup(idmap);
43 if (!dup)
44 return ret_errno(ENOMEM);
45
46 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
47
48 /* align */
49 slide = window = dup;
50 /* skip whitespace */
51 slide += strspn(slide, " \t\r");
52 if (slide != window && *slide == '\0')
53 return ret_errno(EINVAL);
54
55 /* Validate type. */
56 if (*slide != 'u' && *slide != 'g')
57 return log_error_errno(-EINVAL, EINVAL, "Invalid id mapping type: %c", *slide);
58
59 /* Assign type. */
60 tmp_type = *slide;
61
62 /* move beyond type */
63 slide++;
64 /* align */
65 window = slide;
66 /* Validate that only whitespace follows. */
67 slide += strspn(slide, " \t\r");
68 /* There must be whitespace. */
69 if (slide == window)
70 return ret_errno(EINVAL);
71
72 /* Mark beginning of nsid. */
73 window = slide;
74 /* Validate that non-whitespace follows. */
75 slide += strcspn(slide, " \t\r");
76 /* There must be non-whitespace. */
77 if (slide == window || *slide == '\0')
78 return ret_errno(EINVAL);
79 /* Mark end of nsid. */
80 *slide = '\0';
81
82 /* Parse nsid. */
83 ret = lxc_safe_ulong(window, &tmp_nsid);
84 if (ret < 0)
85 return log_error_errno(ret, errno, "Failed to parse nsid: %s", window);
86
87 /* Move beyond \0. */
88 slide++;
89 /* Validate that only whitespace follows. */
90 slide += strspn(slide, " \t\r");
91 /* If there was only one whitespace then we whiped it with our \0 above.
92 * So only ensure that we're not at the end of the string.
93 */
94 if (*slide == '\0')
95 return ret_errno(EINVAL);
96
97 /* Mark beginning of hostid. */
98 window = slide;
99 /* Validate that non-whitespace follows. */
100 slide += strcspn(slide, " \t\r");
101 /* There must be non-whitespace. */
102 if (slide == window || *slide == '\0')
103 return ret_errno(EINVAL);
104 /* Mark end of nsid. */
105 *slide = '\0';
106
107 /* Parse hostid. */
108 ret = lxc_safe_ulong(window, &tmp_hostid);
109 if (ret < 0)
110 return log_error_errno(ret, errno, "Failed to parse hostid: %s", window);
111
112 /* Move beyond \0. */
113 slide++;
114 /* Validate that only whitespace follows. */
115 slide += strspn(slide, " \t\r");
116 /* If there was only one whitespace then we whiped it with our \0 above.
117 * So only ensure that we're not at the end of the string.
118 */
119 if (*slide == '\0')
120 return ret_errno(EINVAL);
121
122 /* Mark beginning of range. */
123 window = slide;
124 /* Validate that non-whitespace follows. */
125 slide += strcspn(slide, " \t\r");
126 /* There must be non-whitespace. */
127 if (slide == window)
128 return ret_errno(EINVAL);
129
130 /* The range is the last valid entry we expect. So make sure that there
131 * is no trailing garbage and if there is, error out.
132 */
133 if (*(slide + strspn(slide, " \t\r\n")) != '\0')
134 return ret_errno(EINVAL);
135
136 /* Mark end of range. */
137 *slide = '\0';
138
139 /* Parse range. */
140 ret = lxc_safe_ulong(window, &tmp_range);
141 if (ret < 0)
142 return log_error_errno(ret, errno, "Failed to parse id mapping range: %s", window);
143
144 *type = tmp_type;
145 *nsid = tmp_nsid;
146 *hostid = tmp_hostid;
147 *range = tmp_range;
148
149 /* Yay, we survived. */
150 return 0;
151 }
152
153 bool lxc_config_value_empty(const char *value)
154 {
155 if (value && strlen(value) > 0)
156 return false;
157
158 return true;
159 }
160
161 struct lxc_netdev *lxc_network_add(struct lxc_list *networks, int idx, bool tail)
162 {
163 __do_free struct lxc_list *newlist = NULL;
164 __do_free struct lxc_netdev *netdev = NULL;
165
166 /* network does not exist */
167 netdev = malloc(sizeof(*netdev));
168 if (!netdev)
169 return ret_set_errno(NULL, ENOMEM);
170
171 memset(netdev, 0, sizeof(*netdev));
172 lxc_list_init(&netdev->ipv4);
173 lxc_list_init(&netdev->ipv6);
174
175 /* give network a unique index */
176 netdev->idx = idx;
177
178 /* prepare new list */
179 newlist = malloc(sizeof(*newlist));
180 if (!newlist)
181 return ret_set_errno(NULL, ENOMEM);
182
183 lxc_list_init(newlist);
184 newlist->elem = netdev;
185
186 if (tail)
187 lxc_list_add_tail(networks, newlist);
188 else
189 lxc_list_add(networks, newlist);
190 move_ptr(newlist);
191
192 return move_ptr(netdev);
193 }
194
195 /* Takes care of finding the correct netdev struct in the networks list or
196 * allocates a new one if it couldn't be found.
197 */
198 struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
199 unsigned int idx, bool allocate)
200 {
201 struct lxc_netdev *netdev = NULL;
202 struct lxc_list *networks = &conf->network;
203 struct lxc_list *insert = networks;
204
205 /* lookup network */
206 if (!lxc_list_empty(networks)) {
207 lxc_list_for_each(insert, networks) {
208 netdev = insert->elem;
209 if (netdev->idx == idx)
210 return netdev;
211 else if (netdev->idx > idx)
212 break;
213 }
214 }
215
216 if (!allocate)
217 return ret_set_errno(NULL, EINVAL);
218
219 return lxc_network_add(insert, idx, true);
220 }
221
222 void lxc_log_configured_netdevs(const struct lxc_conf *conf)
223 {
224 struct lxc_netdev *netdev;
225 struct lxc_list *it = (struct lxc_list *)&conf->network;;
226
227 if ((conf->loglevel != LXC_LOG_LEVEL_TRACE) &&
228 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE))
229 return;
230
231 if (lxc_list_empty(it)) {
232 TRACE("container has no networks configured");
233 return;
234 }
235
236 lxc_list_for_each(it, &conf->network) {
237 struct lxc_list *cur, *next;
238 struct lxc_inetdev *inet4dev;
239 struct lxc_inet6dev *inet6dev;
240 char bufinet4[INET_ADDRSTRLEN], bufinet6[INET6_ADDRSTRLEN];
241
242 netdev = it->elem;
243
244 TRACE("index: %zd", netdev->idx);
245 TRACE("ifindex: %d", netdev->ifindex);
246
247 switch (netdev->type) {
248 case LXC_NET_VETH:
249 TRACE("type: veth");
250 TRACE("veth mode: %d", netdev->priv.veth_attr.mode);
251
252 if (netdev->priv.veth_attr.pair[0] != '\0')
253 TRACE("veth pair: %s",
254 netdev->priv.veth_attr.pair);
255
256 if (netdev->priv.veth_attr.veth1[0] != '\0')
257 TRACE("veth1 : %s",
258 netdev->priv.veth_attr.veth1);
259
260 if (netdev->priv.veth_attr.ifindex > 0)
261 TRACE("host side ifindex for veth device: %d",
262 netdev->priv.veth_attr.ifindex);
263
264 if (netdev->priv.veth_attr.vlan_id_set)
265 TRACE("veth vlan id: %d", netdev->priv.veth_attr.vlan_id);
266
267 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
268 unsigned short vlan_tagged_id = PTR_TO_USHORT(cur->elem);
269 TRACE("veth vlan tagged id: %u", vlan_tagged_id);
270 }
271
272 break;
273 case LXC_NET_MACVLAN:
274 TRACE("type: macvlan");
275
276 if (netdev->priv.macvlan_attr.mode > 0) {
277 char *mode;
278
279 mode = lxc_macvlan_flag_to_mode(
280 netdev->priv.macvlan_attr.mode);
281 TRACE("macvlan mode: %s",
282 mode ? mode : "(invalid mode)");
283 }
284 break;
285 case LXC_NET_IPVLAN:
286 TRACE("type: ipvlan");
287
288 char *mode;
289 mode = lxc_ipvlan_flag_to_mode(netdev->priv.ipvlan_attr.mode);
290 TRACE("ipvlan mode: %s", mode ? mode : "(invalid mode)");
291
292 char *isolation;
293 isolation = lxc_ipvlan_flag_to_isolation(netdev->priv.ipvlan_attr.isolation);
294 TRACE("ipvlan isolation: %s", isolation ? isolation : "(invalid isolation)");
295 break;
296 case LXC_NET_VLAN:
297 TRACE("type: vlan");
298 TRACE("vlan id: %d", netdev->priv.vlan_attr.vid);
299 break;
300 case LXC_NET_PHYS:
301 TRACE("type: phys");
302
303 if (netdev->priv.phys_attr.ifindex > 0)
304 TRACE("host side ifindex for phys device: %d",
305 netdev->priv.phys_attr.ifindex);
306 break;
307 case LXC_NET_EMPTY:
308 TRACE("type: empty");
309 break;
310 case LXC_NET_NONE:
311 TRACE("type: none");
312 break;
313 default:
314 ERROR("Invalid network type %d", netdev->type);
315 return;
316 }
317
318 if (netdev->type != LXC_NET_EMPTY) {
319 TRACE("flags: %s",
320 netdev->flags == IFF_UP ? "up" : "none");
321
322 if (netdev->link[0] != '\0')
323 TRACE("link: %s", netdev->link);
324
325 /* l2proxy only used when link is specified */
326 if (netdev->link[0] != '\0')
327 TRACE("l2proxy: %s", netdev->l2proxy ? "true" : "false");
328
329 if (netdev->name[0] != '\0')
330 TRACE("name: %s", netdev->name);
331
332 if (netdev->hwaddr)
333 TRACE("hwaddr: %s", netdev->hwaddr);
334
335 if (netdev->mtu)
336 TRACE("mtu: %s", netdev->mtu);
337
338 if (netdev->upscript)
339 TRACE("upscript: %s", netdev->upscript);
340
341 if (netdev->downscript)
342 TRACE("downscript: %s", netdev->downscript);
343
344 TRACE("ipv4 gateway auto: %s",
345 netdev->ipv4_gateway_auto ? "true" : "false");
346
347 TRACE("ipv4 gateway dev: %s",
348 netdev->ipv4_gateway_dev ? "true" : "false");
349
350 if (netdev->ipv4_gateway) {
351 inet_ntop(AF_INET, netdev->ipv4_gateway,
352 bufinet4, sizeof(bufinet4));
353 TRACE("ipv4 gateway: %s", bufinet4);
354 }
355
356 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
357 inet4dev = cur->elem;
358 inet_ntop(AF_INET, &inet4dev->addr, bufinet4,
359 sizeof(bufinet4));
360 TRACE("ipv4 addr: %s", bufinet4);
361 }
362
363 TRACE("ipv6 gateway auto: %s",
364 netdev->ipv6_gateway_auto ? "true" : "false");
365
366 TRACE("ipv6 gateway dev: %s",
367 netdev->ipv6_gateway_dev ? "true" : "false");
368
369 if (netdev->ipv6_gateway) {
370 inet_ntop(AF_INET6, netdev->ipv6_gateway,
371 bufinet6, sizeof(bufinet6));
372 TRACE("ipv6 gateway: %s", bufinet6);
373 }
374
375 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
376 inet6dev = cur->elem;
377 inet_ntop(AF_INET6, &inet6dev->addr, bufinet6,
378 sizeof(bufinet6));
379 TRACE("ipv6 addr: %s", bufinet6);
380 }
381
382 if (netdev->type == LXC_NET_VETH) {
383 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
384 inet4dev = cur->elem;
385 if (!inet_ntop(AF_INET, &inet4dev->addr, bufinet4, sizeof(bufinet4))) {
386 ERROR("Invalid ipv4 veth route");
387 return;
388 }
389
390 TRACE("ipv4 veth route: %s/%u", bufinet4, inet4dev->prefix);
391 }
392
393 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
394 inet6dev = cur->elem;
395 if (!inet_ntop(AF_INET6, &inet6dev->addr, bufinet6, sizeof(bufinet6))) {
396 ERROR("Invalid ipv6 veth route");
397 return;
398 }
399
400 TRACE("ipv6 veth route: %s/%u", bufinet6, inet6dev->prefix);
401 }
402 }
403 }
404 }
405 }
406
407 static void lxc_free_netdev(struct lxc_netdev *netdev)
408 {
409 struct lxc_list *cur, *next;
410
411 if (!netdev)
412 return;
413
414 free(netdev->upscript);
415 free(netdev->downscript);
416 free(netdev->hwaddr);
417 free(netdev->mtu);
418
419 free(netdev->ipv4_gateway);
420 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
421 lxc_list_del(cur);
422 free(cur->elem);
423 free(cur);
424 }
425
426 free(netdev->ipv6_gateway);
427 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
428 lxc_list_del(cur);
429 free(cur->elem);
430 free(cur);
431 }
432
433 if (netdev->type == LXC_NET_VETH) {
434 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
435 lxc_list_del(cur);
436 free(cur->elem);
437 free(cur);
438 }
439
440 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
441 lxc_list_del(cur);
442 free(cur->elem);
443 free(cur);
444 }
445
446 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
447 lxc_list_del(cur);
448 free(cur);
449 }
450 }
451
452 free(netdev);
453 }
454
455 define_cleanup_function(struct lxc_netdev *, lxc_free_netdev);
456
457 bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx)
458 {
459 call_cleaner(lxc_free_netdev) struct lxc_netdev *netdev = NULL;
460 struct lxc_list *cur, *next;
461
462 lxc_list_for_each_safe(cur, &conf->network, next) {
463 netdev = cur->elem;
464 if (netdev->idx != idx)
465 continue;
466
467 lxc_list_del(cur);
468 free(cur);
469 return true;
470 }
471
472 return false;
473 }
474
475 void lxc_free_networks(struct lxc_list *networks)
476 {
477 struct lxc_list *cur, *next;
478
479 lxc_list_for_each_safe (cur, networks, next) {
480 struct lxc_netdev *netdev = cur->elem;
481 netdev = cur->elem;
482 lxc_free_netdev(netdev);
483 free(cur);
484 }
485
486 /* prevent segfaults */
487 lxc_list_init(networks);
488 }
489
490 static struct lxc_veth_mode {
491 char *name;
492 int mode;
493 } veth_mode[] = {
494 { "bridge", VETH_MODE_BRIDGE },
495 { "router", VETH_MODE_ROUTER },
496 };
497
498 int lxc_veth_mode_to_flag(int *mode, const char *value)
499 {
500 for (size_t i = 0; i < sizeof(veth_mode) / sizeof(veth_mode[0]); i++) {
501 if (strcmp(veth_mode[i].name, value) != 0)
502 continue;
503
504 *mode = veth_mode[i].mode;
505 return 0;
506 }
507
508 return ret_errno(EINVAL);
509 }
510
511 char *lxc_veth_flag_to_mode(int mode)
512 {
513 for (size_t i = 0; i < sizeof(veth_mode) / sizeof(veth_mode[0]); i++) {
514 if (veth_mode[i].mode != mode)
515 continue;
516
517 return veth_mode[i].name;
518 }
519
520 return ret_set_errno(NULL, EINVAL);
521 }
522
523 static struct lxc_macvlan_mode {
524 char *name;
525 int mode;
526 } macvlan_mode[] = {
527 { "private", MACVLAN_MODE_PRIVATE },
528 { "vepa", MACVLAN_MODE_VEPA },
529 { "bridge", MACVLAN_MODE_BRIDGE },
530 { "passthru", MACVLAN_MODE_PASSTHRU },
531 };
532
533 int lxc_macvlan_mode_to_flag(int *mode, const char *value)
534 {
535 for (size_t i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) {
536 if (strcmp(macvlan_mode[i].name, value))
537 continue;
538
539 *mode = macvlan_mode[i].mode;
540 return 0;
541 }
542
543 return ret_errno(EINVAL);
544 }
545
546 char *lxc_macvlan_flag_to_mode(int mode)
547 {
548 for (size_t i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) {
549 if (macvlan_mode[i].mode != mode)
550 continue;
551
552 return macvlan_mode[i].name;
553 }
554
555 return ret_set_errno(NULL, EINVAL);
556 }
557
558 static struct lxc_ipvlan_mode {
559 char *name;
560 int mode;
561 } ipvlan_mode[] = {
562 { "l3", IPVLAN_MODE_L3 },
563 { "l3s", IPVLAN_MODE_L3S },
564 { "l2", IPVLAN_MODE_L2 },
565 };
566
567 int lxc_ipvlan_mode_to_flag(int *mode, const char *value)
568 {
569 for (size_t i = 0; i < sizeof(ipvlan_mode) / sizeof(ipvlan_mode[0]); i++) {
570 if (strcmp(ipvlan_mode[i].name, value) != 0)
571 continue;
572
573 *mode = ipvlan_mode[i].mode;
574 return 0;
575 }
576
577 return -1;
578 }
579
580 char *lxc_ipvlan_flag_to_mode(int mode)
581 {
582 for (size_t i = 0; i < sizeof(ipvlan_mode) / sizeof(ipvlan_mode[0]); i++) {
583 if (ipvlan_mode[i].mode != mode)
584 continue;
585
586 return ipvlan_mode[i].name;
587 }
588
589 return NULL;
590 }
591
592 static struct lxc_ipvlan_isolation {
593 char *name;
594 int flag;
595 } ipvlan_isolation[] = {
596 { "bridge", IPVLAN_ISOLATION_BRIDGE },
597 { "private", IPVLAN_ISOLATION_PRIVATE },
598 { "vepa", IPVLAN_ISOLATION_VEPA },
599 };
600
601 int lxc_ipvlan_isolation_to_flag(int *flag, const char *value)
602 {
603 for (size_t i = 0; i < sizeof(ipvlan_isolation) / sizeof(ipvlan_isolation[0]); i++) {
604 if (strcmp(ipvlan_isolation[i].name, value) != 0)
605 continue;
606
607 *flag = ipvlan_isolation[i].flag;
608 return 0;
609 }
610
611 return -1;
612 }
613
614 char *lxc_ipvlan_flag_to_isolation(int flag)
615 {
616 for (size_t i = 0; i < sizeof(ipvlan_isolation) / sizeof(ipvlan_isolation[0]); i++) {
617 if (ipvlan_isolation[i].flag != flag)
618 continue;
619
620 return ipvlan_isolation[i].name;
621 }
622
623 return NULL;
624 }
625
626 int set_config_string_item(char **conf_item, const char *value)
627 {
628 char *new_value;
629
630 if (lxc_config_value_empty(value)) {
631 free(*conf_item);
632 *conf_item = NULL;
633 return 0;
634 }
635
636 new_value = strdup(value);
637 if (!new_value) {
638 SYSERROR("Failed to duplicate string \"%s\"", value);
639 return -1;
640 }
641
642 free(*conf_item);
643 *conf_item = new_value;
644 return 0;
645 }
646
647 int set_config_string_item_max(char **conf_item, const char *value, size_t max)
648 {
649 if (strlen(value) >= max) {
650 ERROR("%s is too long (>= %lu)", value, (unsigned long)max);
651 return -1;
652 }
653
654 return set_config_string_item(conf_item, value);
655 }
656
657 int set_config_path_item(char **conf_item, const char *value)
658 {
659 return set_config_string_item_max(conf_item, value, PATH_MAX);
660 }
661
662 int set_config_bool_item(bool *conf_item, const char *value, bool empty_conf_action)
663 {
664 unsigned int val = 0;
665
666 if (lxc_config_value_empty(value)) {
667 *conf_item = empty_conf_action;
668 return 0;
669 }
670
671 if (lxc_safe_uint(value, &val) < 0)
672 return -EINVAL;
673
674 switch (val) {
675 case 0:
676 *conf_item = false;
677 return 0;
678 case 1:
679 *conf_item = true;
680 return 0;
681 }
682
683 return -EINVAL;
684 }
685
686 int config_ip_prefix(struct in_addr *addr)
687 {
688 if (IN_CLASSA(addr->s_addr))
689 return 32 - IN_CLASSA_NSHIFT;
690
691 if (IN_CLASSB(addr->s_addr))
692 return 32 - IN_CLASSB_NSHIFT;
693
694 if (IN_CLASSC(addr->s_addr))
695 return 32 - IN_CLASSC_NSHIFT;
696
697 return 0;
698 }
699
700 int network_ifname(char *valuep, const char *value, size_t size)
701 {
702 size_t retlen;
703
704 if (!valuep || !value)
705 return -1;
706
707 retlen = strlcpy(valuep, value, size);
708 if (retlen >= size)
709 ERROR("Network device name \"%s\" is too long (>= %zu)", value,
710 size);
711
712 return 0;
713 }
714
715 bool lxc_config_net_is_hwaddr(const char *line)
716 {
717 unsigned index;
718 char tmp[7];
719
720 if (strncmp(line, "lxc.net", 7) != 0)
721 return false;
722
723 if (strncmp(line, "lxc.net.hwaddr", 14) == 0)
724 return true;
725
726 if (strncmp(line, "lxc.network.hwaddr", 18) == 0)
727 return true;
728
729 if (sscanf(line, "lxc.net.%u.%6s", &index, tmp) == 2 ||
730 sscanf(line, "lxc.network.%u.%6s", &index, tmp) == 2)
731 return strncmp(tmp, "hwaddr", 6) == 0;
732
733 return false;
734 }
735
736 void rand_complete_hwaddr(char *hwaddr)
737 {
738 const char hex[] = "0123456789abcdef";
739 char *curs = hwaddr;
740 #ifdef HAVE_RAND_R
741 unsigned int seed;
742
743 seed = randseed(false);
744 #else
745
746 (void)randseed(true);
747 #endif
748
749 while (*curs != '\0' && *curs != '\n') {
750 if (*curs == 'x' || *curs == 'X') {
751 if (curs - hwaddr == 1) {
752 /* ensure address is unicast */
753 #ifdef HAVE_RAND_R
754 *curs = hex[rand_r(&seed) & 0x0E];
755 } else {
756 *curs = hex[rand_r(&seed) & 0x0F];
757 #else
758 *curs = hex[rand() & 0x0E];
759 } else {
760 *curs = hex[rand() & 0x0F];
761 #endif
762 }
763 }
764 curs++;
765 }
766 }
767
768 bool new_hwaddr(char *hwaddr)
769 {
770 int ret;
771 #ifdef HAVE_RAND_R
772 unsigned int seed;
773
774 seed = randseed(false);
775
776 ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand_r(&seed) % 255,
777 rand_r(&seed) % 255, rand_r(&seed) % 255);
778 #else
779
780 (void)randseed(true);
781
782 ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
783 rand() % 255, rand() % 255);
784 #endif
785 if (ret < 0 || ret >= 18) {
786 SYSERROR("Failed to call snprintf()");
787 return false;
788 }
789
790 return true;
791 }
792
793 int lxc_get_conf_str(char *retv, int inlen, const char *value)
794 {
795 size_t value_len;
796
797 if (!value)
798 return 0;
799
800 value_len = strlen(value);
801 if (retv && inlen >= value_len + 1)
802 memcpy(retv, value, value_len + 1);
803
804 return value_len;
805 }
806
807 int lxc_get_conf_bool(struct lxc_conf *c, char *retv, int inlen, bool v)
808 {
809 int len;
810 int fulllen = 0;
811
812 if (!retv)
813 inlen = 0;
814 else
815 memset(retv, 0, inlen);
816
817 strprint(retv, inlen, "%d", v);
818
819 return fulllen;
820 }
821
822 int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v)
823 {
824 int len;
825 int fulllen = 0;
826
827 if (!retv)
828 inlen = 0;
829 else
830 memset(retv, 0, inlen);
831
832 strprint(retv, inlen, "%d", v);
833
834 return fulllen;
835 }
836
837 int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v)
838 {
839 int len;
840 int fulllen = 0;
841
842 if (!retv)
843 inlen = 0;
844 else
845 memset(retv, 0, inlen);
846
847 strprint(retv, inlen, "%zu", v);
848
849 return fulllen;
850 }
851
852 int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v)
853 {
854 int len;
855 int fulllen = 0;
856
857 if (!retv)
858 inlen = 0;
859 else
860 memset(retv, 0, inlen);
861
862 strprint(retv, inlen, "%"PRIu64, v);
863
864 return fulllen;
865 }
866
867 static int lxc_container_name_to_pid(const char *lxcname_or_pid,
868 const char *lxcpath)
869 {
870 int ret;
871 signed long int pid;
872 char *err = NULL;
873
874 pid = strtol(lxcname_or_pid, &err, 10);
875 if (*err != '\0' || pid < 1) {
876 struct lxc_container *c;
877
878 c = lxc_container_new(lxcname_or_pid, lxcpath);
879 if (!c) {
880 ERROR("\"%s\" is not a valid pid nor a container name",
881 lxcname_or_pid);
882 return -1;
883 }
884
885 if (!c->may_control(c)) {
886 ERROR("Insufficient privileges to control container "
887 "\"%s\"", c->name);
888 lxc_container_put(c);
889 return -1;
890 }
891
892 pid = c->init_pid(c);
893 if (pid < 1) {
894 ERROR("Container \"%s\" is not running", c->name);
895 lxc_container_put(c);
896 return -1;
897 }
898
899 lxc_container_put(c);
900 }
901
902 ret = kill(pid, 0);
903 if (ret < 0) {
904 SYSERROR("Failed to send signal to pid %d", (int)pid);
905 return -1;
906 }
907
908 return pid;
909 }
910
911 int lxc_inherit_namespace(const char *nsfd_path, const char *lxcpath,
912 const char *namespace)
913 {
914 int fd, pid;
915 char *dup, *lastslash;
916
917 if (nsfd_path[0] == '/') {
918 return open(nsfd_path, O_RDONLY | O_CLOEXEC);
919 }
920
921 lastslash = strrchr(nsfd_path, '/');
922 if (lastslash) {
923 dup = strdup(nsfd_path);
924 if (!dup)
925 return -1;
926
927 dup[lastslash - nsfd_path] = '\0';
928 pid = lxc_container_name_to_pid(lastslash + 1, dup);
929 free(dup);
930 } else {
931 pid = lxc_container_name_to_pid(nsfd_path, lxcpath);
932 }
933
934 if (pid < 0)
935 return -1;
936
937 fd = lxc_preserve_ns(pid, namespace);
938 if (fd < 0)
939 return -1;
940
941 return fd;
942 }
943
944 struct signame {
945 int num;
946 const char *name;
947 };
948
949 static const struct signame signames[] = {
950 { SIGHUP, "HUP" },
951 { SIGINT, "INT" },
952 { SIGQUIT, "QUIT" },
953 { SIGILL, "ILL" },
954 { SIGABRT, "ABRT" },
955 { SIGFPE, "FPE" },
956 { SIGKILL, "KILL" },
957 { SIGSEGV, "SEGV" },
958 { SIGPIPE, "PIPE" },
959 { SIGALRM, "ALRM" },
960 { SIGTERM, "TERM" },
961 { SIGUSR1, "USR1" },
962 { SIGUSR2, "USR2" },
963 { SIGCHLD, "CHLD" },
964 { SIGCONT, "CONT" },
965 { SIGSTOP, "STOP" },
966 { SIGTSTP, "TSTP" },
967 { SIGTTIN, "TTIN" },
968 { SIGTTOU, "TTOU" },
969 #ifdef SIGTRAP
970 { SIGTRAP, "TRAP" },
971 #endif
972 #ifdef SIGIOT
973 { SIGIOT, "IOT" },
974 #endif
975 #ifdef SIGEMT
976 { SIGEMT, "EMT" },
977 #endif
978 #ifdef SIGBUS
979 { SIGBUS, "BUS" },
980 #endif
981 #ifdef SIGSTKFLT
982 { SIGSTKFLT, "STKFLT" },
983 #endif
984 #ifdef SIGCLD
985 { SIGCLD, "CLD" },
986 #endif
987 #ifdef SIGURG
988 { SIGURG, "URG" },
989 #endif
990 #ifdef SIGXCPU
991 { SIGXCPU, "XCPU" },
992 #endif
993 #ifdef SIGXFSZ
994 { SIGXFSZ, "XFSZ" },
995 #endif
996 #ifdef SIGVTALRM
997 { SIGVTALRM, "VTALRM" },
998 #endif
999 #ifdef SIGPROF
1000 { SIGPROF, "PROF" },
1001 #endif
1002 #ifdef SIGWINCH
1003 { SIGWINCH, "WINCH" },
1004 #endif
1005 #ifdef SIGIO
1006 { SIGIO, "IO" },
1007 #endif
1008 #ifdef SIGPOLL
1009 { SIGPOLL, "POLL" },
1010 #endif
1011 #ifdef SIGINFO
1012 { SIGINFO, "INFO" },
1013 #endif
1014 #ifdef SIGLOST
1015 { SIGLOST, "LOST" },
1016 #endif
1017 #ifdef SIGPWR
1018 { SIGPWR, "PWR" },
1019 #endif
1020 #ifdef SIGUNUSED
1021 { SIGUNUSED, "UNUSED" },
1022 #endif
1023 #ifdef SIGSYS
1024 { SIGSYS, "SYS" },
1025 #endif
1026 };
1027
1028 static int sig_num(const char *sig)
1029 {
1030 unsigned int signum;
1031
1032 if (lxc_safe_uint(sig, &signum) < 0)
1033 return -1;
1034
1035 return signum;
1036 }
1037
1038 static int rt_sig_num(const char *signame)
1039 {
1040 int rtmax = 0, sig_n = 0;
1041
1042 if (strncasecmp(signame, "max-", 4) == 0)
1043 rtmax = 1;
1044
1045 signame += 4;
1046 if (!isdigit(*signame))
1047 return -1;
1048
1049 sig_n = sig_num(signame);
1050 sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n;
1051 if (sig_n > SIGRTMAX || sig_n < SIGRTMIN)
1052 return -1;
1053
1054 return sig_n;
1055 }
1056
1057 int sig_parse(const char *signame)
1058 {
1059 size_t n;
1060
1061 if (isdigit(*signame)) {
1062 return sig_num(signame);
1063 } else if (strncasecmp(signame, "sig", 3) == 0) {
1064 signame += 3;
1065 if (strncasecmp(signame, "rt", 2) == 0)
1066 return rt_sig_num(signame + 2);
1067
1068 for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++)
1069 if (strcasecmp(signames[n].name, signame) == 0)
1070 return signames[n].num;
1071 }
1072
1073 return -1;
1074 }