]> git.ozlabs.org Git - petitboot/blob - lib/pb-config/pb-config.c
lib/pb-config: Properly initialise interface_config
[petitboot] / lib / pb-config / pb-config.c
1
2 #include <string.h>
3
4 #include <log/log.h>
5 #include <types/types.h>
6 #include <talloc/talloc.h>
7
8 #include "pb-config.h"
9
10 static struct interface_config *config_copy_interface(struct config *ctx,
11                 struct interface_config *src)
12 {
13         struct interface_config *dest = talloc_zero(ctx,
14                                                 struct interface_config);
15
16         memcpy(dest->hwaddr, src->hwaddr, sizeof(src->hwaddr));
17         dest->ignore = src->ignore;
18
19         if (dest->ignore)
20                 return dest;
21
22         dest->method = src->method;
23
24         switch (src->method) {
25         case CONFIG_METHOD_DHCP:
26                 break;
27         case CONFIG_METHOD_STATIC:
28                 dest->static_config.address =
29                         talloc_strdup(dest, src->static_config.address);
30                 dest->static_config.gateway =
31                         talloc_strdup(dest, src->static_config.gateway);
32                 dest->static_config.url =
33                         talloc_strdup(dest, src->static_config.url);
34                 break;
35         }
36
37         return dest;
38 }
39
40 struct config *config_copy(void *ctx, const struct config *src)
41 {
42         struct config *dest;
43         unsigned int i;
44
45         dest = talloc(ctx, struct config);
46         dest->autoboot_enabled = src->autoboot_enabled;
47         dest->autoboot_timeout_sec = src->autoboot_timeout_sec;
48         dest->safe_mode = src->safe_mode;
49
50         dest->network.n_interfaces = src->network.n_interfaces;
51         dest->network.interfaces = talloc_array(dest, struct interface_config *,
52                                         dest->network.n_interfaces);
53         dest->network.n_dns_servers = src->network.n_dns_servers;
54         dest->network.dns_servers = talloc_array(dest, const char *,
55                                         dest->network.n_dns_servers);
56
57         for (i = 0; i < src->network.n_interfaces; i++)
58                 dest->network.interfaces[i] = config_copy_interface(dest,
59                                 src->network.interfaces[i]);
60
61         for (i = 0; i < src->network.n_dns_servers; i++)
62                 dest->network.dns_servers[i] = talloc_strdup(dest,
63                                 src->network.dns_servers[i]);
64
65         dest->n_autoboot_opts = src->n_autoboot_opts;
66         dest->autoboot_opts = talloc_array(dest, struct autoboot_option,
67                                         dest->n_autoboot_opts);
68
69         for (i = 0; i < src->n_autoboot_opts; i++) {
70                 dest->autoboot_opts[i].boot_type =
71                         src->autoboot_opts[i].boot_type;
72                 if (src->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
73                         dest->autoboot_opts[i].type =
74                                 src->autoboot_opts[i].type;
75                 else
76                         dest->autoboot_opts[i].uuid =
77                                 talloc_strdup(dest, src->autoboot_opts[i].uuid);
78         }
79
80         dest->ipmi_bootdev = src->ipmi_bootdev;
81         dest->ipmi_bootdev_persistent = src->ipmi_bootdev_persistent;
82
83         dest->allow_writes = src->allow_writes;
84
85         if (src->lang && strlen(src->lang))
86                 dest->lang = talloc_strdup(dest, src->lang);
87         else
88                 dest->lang = NULL;
89
90         return dest;
91 }