]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile_utils.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "confile_utils.h"
34 lxc_log_define(lxc_confile_utils
, lxc
);
36 int parse_idmaps(const char *idmap
, char *type
, unsigned long *nsid
,
37 unsigned long *hostid
, unsigned long *range
)
40 unsigned long tmp_hostid
, tmp_nsid
, tmp_range
;
45 /* Duplicate string. */
50 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
55 slide
+= strspn(slide
, " \t\r");
56 if (slide
!= window
&& *slide
== '\0')
60 if (*slide
!= 'u' && *slide
!= 'g')
65 /* move beyond type */
69 /* Validate that only whitespace follows. */
70 slide
+= strspn(slide
, " \t\r");
71 /* There must be whitespace. */
75 /* Mark beginning of nsuid. */
77 /* Validate that non-whitespace follows. */
78 slide
+= strcspn(slide
, " \t\r");
79 /* There must be non-whitespace. */
80 if (slide
== window
|| *slide
== '\0')
82 /* Mark end of nsuid. */
86 if (lxc_safe_ulong(window
, &tmp_nsid
) < 0)
93 /* Validate that only whitespace follows. */
94 slide
+= strspn(slide
, " \t\r");
95 /* If there was only one whitespace then we whiped it with our \0 above.
96 * So only ensure that we're not at the end of the string.
101 /* Mark beginning of hostid. */
103 /* Validate that non-whitespace follows. */
104 slide
+= strcspn(slide
, " \t\r");
105 /* There must be non-whitespace. */
106 if (slide
== window
|| *slide
== '\0')
108 /* Mark end of nsuid. */
112 if (lxc_safe_ulong(window
, &tmp_hostid
) < 0)
115 /* Move beyond \0. */
119 /* Validate that only whitespace follows. */
120 slide
+= strspn(slide
, " \t\r");
121 /* If there was only one whitespace then we whiped it with our \0 above.
122 * So only ensure that we're not at the end of the string.
127 /* Mark beginning of range. */
129 /* Validate that non-whitespace follows. */
130 slide
+= strcspn(slide
, " \t\r");
131 /* There must be non-whitespace. */
135 /* The range is the last valid entry we expect. So make sure that there
136 * is not trailing garbage and if there is, error out.
138 if (*(slide
+ strspn(slide
, " \t\r\n")) != '\0')
140 /* Mark end of range. */
144 if (lxc_safe_ulong(window
, &tmp_range
) < 0)
149 *hostid
= tmp_hostid
;
152 /* Yay, we survived. */
161 bool lxc_config_value_empty(const char *value
)
163 if (value
&& strlen(value
) > 0)
169 /* Takes care of finding the correct netdev struct in the networks list or
170 * allocates a new one if it couldn't be found.
172 struct lxc_netdev
*lxc_get_netdev_by_idx(struct lxc_conf
*conf
,
173 unsigned int idx
, bool allocate
)
175 struct lxc_list
*newlist
;
176 struct lxc_netdev
*netdev
= NULL
;
177 struct lxc_list
*networks
= &conf
->network
;
178 struct lxc_list
*insert
= networks
;
181 if (!lxc_list_empty(networks
)) {
182 lxc_list_for_each(insert
, networks
) {
183 netdev
= insert
->elem
;
184 if (netdev
->idx
>= idx
)
188 /* network already exists */
189 if (netdev
->idx
== idx
)
196 /* network does not exist */
197 netdev
= malloc(sizeof(*netdev
));
201 memset(netdev
, 0, sizeof(*netdev
));
202 lxc_list_init(&netdev
->ipv4
);
203 lxc_list_init(&netdev
->ipv6
);
205 /* give network a unique index */
208 /* prepare new list */
209 newlist
= malloc(sizeof(*newlist
));
215 lxc_list_init(newlist
);
216 newlist
->elem
= netdev
;
218 /* Insert will now point to the correct position to insert the new
221 lxc_list_add_tail(insert
, newlist
);
226 void lxc_log_configured_netdevs(const struct lxc_conf
*conf
)
228 struct lxc_netdev
*netdev
;
229 struct lxc_list
*it
= (struct lxc_list
*)&conf
->network
;;
231 if ((conf
->loglevel
!= LXC_LOG_LEVEL_TRACE
) &&
232 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE
))
235 if (lxc_list_empty(it
)) {
236 TRACE("container has no networks configured");
240 lxc_list_for_each(it
, &conf
->network
) {
243 TRACE("index: %d", netdev
->idx
);
244 switch (netdev
->type
) {
248 case LXC_NET_MACVLAN
:
249 TRACE("type: macvlan");
258 TRACE("type: empty");
264 ERROR("invalid network type %d", netdev
->type
);
268 TRACE("flags: %s", netdev
->flags
== IFF_UP
? "up" : "none");
270 TRACE("link: %s", netdev
->link
);
272 TRACE("name: %s", netdev
->name
);
274 TRACE("hwaddr: %s", netdev
->hwaddr
);
276 TRACE("mtu: %s", netdev
->mtu
);
277 if (netdev
->upscript
)
278 TRACE("upscript: %s", netdev
->upscript
);
279 if (netdev
->downscript
)
280 TRACE("downscript: %s", netdev
->downscript
);
284 static void lxc_free_netdev(struct lxc_netdev
*netdev
)
286 struct lxc_list
*cur
, *next
;
290 if (netdev
->type
== LXC_NET_VETH
)
291 free(netdev
->priv
.veth_attr
.pair
);
292 free(netdev
->upscript
);
293 free(netdev
->downscript
);
294 free(netdev
->hwaddr
);
297 free(netdev
->ipv4_gateway
);
298 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
304 free(netdev
->ipv6_gateway
);
305 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
314 bool lxc_remove_nic_by_idx(struct lxc_conf
*conf
, unsigned int idx
)
316 struct lxc_list
*cur
, *next
;
317 struct lxc_netdev
*netdev
;
320 lxc_list_for_each_safe(cur
, &conf
->network
, next
) {
322 if (netdev
->idx
!= idx
)
333 lxc_free_netdev(netdev
);
339 void lxc_free_networks(struct lxc_conf
*conf
)
341 struct lxc_list
*cur
, *next
;
342 struct lxc_netdev
*netdev
;
344 lxc_list_for_each_safe(cur
, &conf
->network
, next
) {
346 lxc_free_netdev(netdev
);
350 /* prevent segfaults */
351 lxc_list_init(&conf
->network
);