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 void system_info_set_interface_address(unsigned int hwaddr_size,
22 uint8_t *hwaddr, const char *address)
24 struct interface_info *if_info;
26 char mac[20], **if_addr, *new_addr, *subnet;
28 for (i = 0; i < sysinfo->n_interfaces; i++) {
29 if_info = sysinfo->interfaces[i];
31 if (if_info->hwaddr_size != hwaddr_size)
34 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
38 * Don't include the subnet from a static config, and check if
39 * we're updating the IPv4 or IPv6 address.
41 if ((subnet = strchr(address, '/')))
42 new_addr = talloc_strndup(if_info, address, subnet - address);
44 new_addr = talloc_strdup(if_info, address);
45 if (addr_scheme(new_addr) == AF_INET)
46 if_addr = &if_info->address;
48 if_addr = &if_info->address_v6;
50 /* Found an existing interface. Notify clients if a new address
52 if (!*if_addr || strcmp(*if_addr, address)) {
53 talloc_free(*if_addr);
55 discover_server_notify_system_info(server, sysinfo);
60 mac_str(hwaddr, hwaddr_size, mac, sizeof(mac));
61 pb_log("Couldn't find interface matching %s\n", mac);
64 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
65 const char *name, bool link)
67 struct interface_info *if_info;
70 for (i = 0; i < sysinfo->n_interfaces; i++) {
73 if_info = sysinfo->interfaces[i];
75 if (if_info->hwaddr_size != hwaddr_size)
78 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
81 /* Found an existing interface. Notify clients on any name or
83 if (strcmp(if_info->name, name)) {
84 talloc_free(if_info->name);
85 if_info->name = talloc_strdup(if_info, name);
89 if (if_info->link != link) {
95 discover_server_notify_system_info(server, sysinfo);
100 if_info = talloc_zero(sysinfo, struct interface_info);
101 if_info->hwaddr_size = hwaddr_size;
102 if_info->hwaddr = talloc_memdup(if_info, hwaddr, hwaddr_size);
103 if_info->name = talloc_strdup(if_info, name);
104 if_info->link = link;
106 sysinfo->n_interfaces++;
107 sysinfo->interfaces = talloc_realloc(sysinfo, sysinfo->interfaces,
108 struct interface_info *,
109 sysinfo->n_interfaces);
110 sysinfo->interfaces[sysinfo->n_interfaces - 1] = if_info;
112 discover_server_notify_system_info(server, sysinfo);
115 void system_info_register_blockdev(const char *name, const char *uuid,
116 const char *mountpoint)
118 struct blockdev_info *bd_info;
121 for (i = 0; i < sysinfo->n_blockdevs; i++) {
122 bd_info = sysinfo->blockdevs[i];
124 if (strcmp(bd_info->name, name))
127 /* update the mountpoint and UUID, and we're done */
128 talloc_free(bd_info->mountpoint);
129 bd_info->uuid = talloc_strdup(bd_info, uuid);
130 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
131 discover_server_notify_system_info(server, sysinfo);
135 bd_info = talloc_zero(sysinfo, struct blockdev_info);
136 bd_info->name = talloc_strdup(bd_info, name);
137 bd_info->uuid = talloc_strdup(bd_info, uuid);
138 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
140 sysinfo->n_blockdevs++;
141 sysinfo->blockdevs = talloc_realloc(sysinfo, sysinfo->blockdevs,
142 struct blockdev_info *,
143 sysinfo->n_blockdevs);
144 sysinfo->blockdevs[sysinfo->n_blockdevs - 1] = bd_info;
146 discover_server_notify_system_info(server, sysinfo);
149 void system_info_init(struct discover_server *s)
152 sysinfo = talloc_zero(server, struct system_info);
153 platform_get_sysinfo(sysinfo);
156 /* Only reset device information. Platform information is static */
157 void system_info_reinit(void)
161 for (i = 0; i < sysinfo->n_blockdevs; i++)
162 talloc_free(sysinfo->blockdevs[i]);
163 talloc_free(sysinfo->blockdevs);
164 sysinfo->blockdevs = NULL;
165 sysinfo->n_blockdevs = 0;
167 for (i = 0; i < sysinfo->n_interfaces; i++)
168 talloc_free(sysinfo->interfaces[i]);
169 talloc_free(sysinfo->interfaces);
170 sysinfo->interfaces = NULL;
171 sysinfo->n_interfaces = 0;