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