4 #include <talloc/talloc.h>
5 #include <process/process.h>
9 #include "discover-server.h"
13 static struct system_info *sysinfo;
14 static struct discover_server *server;
16 const struct system_info *system_info_get(void)
21 bool system_info_network_available(void)
25 for (i = 0; i < sysinfo->n_interfaces; i++)
26 if (sysinfo->interfaces[i]->address)
31 void system_info_set_interface_address(unsigned int hwaddr_size,
32 uint8_t *hwaddr, const char *address)
34 struct interface_info *if_info;
36 char mac[20], **if_addr, *new_addr, *subnet;
38 for (i = 0; i < sysinfo->n_interfaces; i++) {
39 if_info = sysinfo->interfaces[i];
41 if (if_info->hwaddr_size != hwaddr_size)
44 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
48 * Don't include the subnet from a static config, and check if
49 * we're updating the IPv4 or IPv6 address.
51 if ((subnet = strchr(address, '/')))
52 new_addr = talloc_strndup(if_info, address, subnet - address);
54 new_addr = talloc_strdup(if_info, address);
55 if (addr_scheme(new_addr) == AF_INET)
56 if_addr = &if_info->address;
58 if_addr = &if_info->address_v6;
60 /* Found an existing interface. Notify clients if a new address
62 if (!*if_addr || strcmp(*if_addr, address)) {
63 talloc_free(*if_addr);
65 discover_server_notify_system_info(server, sysinfo);
70 mac_str(hwaddr, hwaddr_size, mac, sizeof(mac));
71 pb_log("Couldn't find interface matching %s\n", mac);
74 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
75 const char *name, bool link)
77 struct interface_info *if_info;
80 for (i = 0; i < sysinfo->n_interfaces; i++) {
83 if_info = sysinfo->interfaces[i];
85 if (if_info->hwaddr_size != hwaddr_size)
88 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
91 /* Found an existing interface. Notify clients on any name or
93 if (strcmp(if_info->name, name)) {
94 talloc_free(if_info->name);
95 if_info->name = talloc_strdup(if_info, name);
99 if (if_info->link != link) {
100 if_info->link = link;
105 discover_server_notify_system_info(server, sysinfo);
110 if_info = talloc_zero(sysinfo, struct interface_info);
111 if_info->hwaddr_size = hwaddr_size;
112 if_info->hwaddr = talloc_memdup(if_info, hwaddr, hwaddr_size);
113 if_info->name = talloc_strdup(if_info, name);
114 if_info->link = link;
116 sysinfo->n_interfaces++;
117 sysinfo->interfaces = talloc_realloc(sysinfo, sysinfo->interfaces,
118 struct interface_info *,
119 sysinfo->n_interfaces);
120 sysinfo->interfaces[sysinfo->n_interfaces - 1] = if_info;
122 discover_server_notify_system_info(server, sysinfo);
125 void system_info_register_blockdev(const char *name, const char *uuid,
126 const char *mountpoint)
128 struct blockdev_info *bd_info;
131 for (i = 0; i < sysinfo->n_blockdevs; i++) {
132 bd_info = sysinfo->blockdevs[i];
134 if (strcmp(bd_info->name, name))
137 /* update the mountpoint and UUID, and we're done */
138 talloc_free(bd_info->mountpoint);
139 bd_info->uuid = talloc_strdup(bd_info, uuid);
140 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
141 discover_server_notify_system_info(server, sysinfo);
145 bd_info = talloc_zero(sysinfo, struct blockdev_info);
146 bd_info->name = talloc_strdup(bd_info, name);
147 bd_info->uuid = talloc_strdup(bd_info, uuid);
148 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
150 sysinfo->n_blockdevs++;
151 sysinfo->blockdevs = talloc_realloc(sysinfo, sysinfo->blockdevs,
152 struct blockdev_info *,
153 sysinfo->n_blockdevs);
154 sysinfo->blockdevs[sysinfo->n_blockdevs - 1] = bd_info;
156 discover_server_notify_system_info(server, sysinfo);
159 void system_info_init(struct discover_server *s)
162 sysinfo = talloc_zero(server, struct system_info);
163 platform_get_sysinfo(sysinfo);
166 /* Only reset device information. Platform information is static */
167 void system_info_reinit(void)
171 for (i = 0; i < sysinfo->n_blockdevs; i++)
172 talloc_free(sysinfo->blockdevs[i]);
173 talloc_free(sysinfo->blockdevs);
174 sysinfo->blockdevs = NULL;
175 sysinfo->n_blockdevs = 0;
177 for (i = 0; i < sysinfo->n_interfaces; i++)
178 talloc_free(sysinfo->interfaces[i]);
179 talloc_free(sysinfo->interfaces);
180 sysinfo->interfaces = NULL;
181 sysinfo->n_interfaces = 0;