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