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