discover/pb-discover: #include <locale.h> for musl libc
[petitboot] / discover / sysinfo.c
1
2 #include <string.h>
3
4 #include <talloc/talloc.h>
5 #include <process/process.h>
6 #include <log/log.h>
7
8 #include "discover-server.h"
9 #include "platform.h"
10 #include "sysinfo.h"
11
12 static struct system_info *sysinfo;
13 static struct discover_server *server;
14
15 const struct system_info *system_info_get(void)
16 {
17         return sysinfo;
18 }
19
20 bool system_info_network_available(void)
21 {
22         unsigned int i;
23
24         for (i = 0; i < sysinfo->n_interfaces; i++)
25                 if (sysinfo->interfaces[i]->address)
26                         return true;
27         return false;
28 }
29
30 void system_info_set_interface_address(unsigned int hwaddr_size,
31                 uint8_t *hwaddr, const char *address)
32 {
33         struct interface_info *if_info;
34         unsigned int i;
35         char mac[20];
36
37         for (i = 0; i < sysinfo->n_interfaces; i++) {
38                 if_info = sysinfo->interfaces[i];
39
40                 if (if_info->hwaddr_size != hwaddr_size)
41                         continue;
42
43                 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
44                         continue;
45
46                 /* Found an existing interface. Notify clients if a new address
47                  * is set */
48                 if (!if_info->address || strcmp(if_info->address, address)) {
49                         talloc_free(if_info->address);
50                         if_info->address = talloc_strdup(if_info, address);
51                         discover_server_notify_system_info(server, sysinfo);
52                         return;
53                 }
54         }
55
56         mac_str(hwaddr, hwaddr_size, mac, sizeof(mac));
57         pb_log("Couldn't find interface matching %s\n", mac);
58 }
59
60 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
61                 const char *name, bool link)
62 {
63         struct interface_info *if_info;
64         unsigned int i;
65
66         for (i = 0; i < sysinfo->n_interfaces; i++) {
67                 bool changed = false;
68
69                 if_info = sysinfo->interfaces[i];
70
71                 if (if_info->hwaddr_size != hwaddr_size)
72                         continue;
73
74                 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
75                         continue;
76
77                 /* Found an existing interface. Notify clients on any name or
78                  * link changes */
79                 if (strcmp(if_info->name, name)) {
80                         talloc_free(if_info->name);
81                         if_info->name = talloc_strdup(if_info, name);
82                         changed = true;
83                 }
84
85                 if (if_info->link != link) {
86                         if_info->link = link;
87                         changed = true;
88                 }
89
90                 if (changed)
91                         discover_server_notify_system_info(server, sysinfo);
92
93                 return;
94         }
95
96         if_info = talloc_zero(sysinfo, struct interface_info);
97         if_info->hwaddr_size = hwaddr_size;
98         if_info->hwaddr = talloc_memdup(if_info, hwaddr, hwaddr_size);
99         if_info->name = talloc_strdup(if_info, name);
100         if_info->link = link;
101
102         sysinfo->n_interfaces++;
103         sysinfo->interfaces = talloc_realloc(sysinfo, sysinfo->interfaces,
104                                                 struct interface_info *,
105                                                 sysinfo->n_interfaces);
106         sysinfo->interfaces[sysinfo->n_interfaces - 1] = if_info;
107
108         discover_server_notify_system_info(server, sysinfo);
109 }
110
111 void system_info_register_blockdev(const char *name, const char *uuid,
112                 const char *mountpoint)
113 {
114         struct blockdev_info *bd_info;
115         unsigned int i;
116
117         for (i = 0; i < sysinfo->n_blockdevs; i++) {
118                 bd_info = sysinfo->blockdevs[i];
119
120                 if (strcmp(bd_info->name, name))
121                         continue;
122
123                 /* update the mountpoint and UUID, and we're done */
124                 talloc_free(bd_info->mountpoint);
125                 bd_info->uuid = talloc_strdup(bd_info, uuid);
126                 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
127                 discover_server_notify_system_info(server, sysinfo);
128                 return;
129         }
130
131         bd_info = talloc_zero(sysinfo, struct blockdev_info);
132         bd_info->name = talloc_strdup(bd_info, name);
133         bd_info->uuid = talloc_strdup(bd_info, uuid);
134         bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
135
136         sysinfo->n_blockdevs++;
137         sysinfo->blockdevs = talloc_realloc(sysinfo, sysinfo->blockdevs,
138                                                 struct blockdev_info *,
139                                                 sysinfo->n_blockdevs);
140         sysinfo->blockdevs[sysinfo->n_blockdevs - 1] = bd_info;
141
142         discover_server_notify_system_info(server, sysinfo);
143 }
144
145 void system_info_init(struct discover_server *s)
146 {
147         server = s;
148         sysinfo = talloc_zero(server, struct system_info);
149         platform_get_sysinfo(sysinfo);
150 }
151
152 /* Only reset device information. Platform information is static */
153 void system_info_reinit(void)
154 {
155         unsigned int i;
156
157         for (i = 0; i < sysinfo->n_blockdevs; i++)
158                 talloc_free(sysinfo->blockdevs[i]);
159         talloc_free(sysinfo->blockdevs);
160         sysinfo->blockdevs = NULL;
161         sysinfo->n_blockdevs = 0;
162
163         for (i = 0; i < sysinfo->n_interfaces; i++)
164                 talloc_free(sysinfo->interfaces[i]);
165         talloc_free(sysinfo->interfaces);
166         sysinfo->interfaces = NULL;
167         sysinfo->n_interfaces = 0;
168 }