4 #include <talloc/talloc.h>
5 #include <process/process.h>
7 #include "discover-server.h"
10 static struct system_info *sysinfo;
11 static struct discover_server *server;
13 static const char *sysinfo_helper = PKG_LIBEXEC_DIR "/pb-sysinfo";
15 const struct system_info *system_info_get(void)
20 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
21 const char *name, bool link)
23 struct interface_info *if_info;
26 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))
37 /* Found an existing interface. Notify clients on any name or
39 if (strcmp(if_info->name, name)) {
40 talloc_free(if_info->name);
41 if_info->name = talloc_strdup(if_info, name);
45 if (if_info->link != link) {
51 discover_server_notify_system_info(server, sysinfo);
56 if_info = talloc_zero(sysinfo, struct interface_info);
57 if_info->hwaddr_size = hwaddr_size;
58 if_info->hwaddr = talloc_memdup(if_info, hwaddr, hwaddr_size);
59 if_info->name = talloc_strdup(if_info, name);
62 sysinfo->n_interfaces++;
63 sysinfo->interfaces = talloc_realloc(sysinfo, sysinfo->interfaces,
64 struct interface_info *,
65 sysinfo->n_interfaces);
66 sysinfo->interfaces[sysinfo->n_interfaces - 1] = if_info;
68 discover_server_notify_system_info(server, sysinfo);
71 void system_info_register_blockdev(const char *name, const char *uuid,
72 const char *mountpoint)
74 struct blockdev_info *bd_info;
77 for (i = 0; i < sysinfo->n_blockdevs; i++) {
78 bd_info = sysinfo->blockdevs[i];
80 if (strcmp(bd_info->name, name))
83 /* update the mountpoint and UUID, and we're done */
84 talloc_free(bd_info->mountpoint);
85 bd_info->uuid = talloc_strdup(bd_info, uuid);
86 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
87 discover_server_notify_system_info(server, sysinfo);
91 bd_info = talloc_zero(sysinfo, struct blockdev_info);
92 bd_info->name = talloc_strdup(bd_info, name);
93 bd_info->uuid = talloc_strdup(bd_info, uuid);
94 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
96 sysinfo->n_blockdevs++;
97 sysinfo->blockdevs = talloc_realloc(sysinfo, sysinfo->blockdevs,
98 struct blockdev_info *,
99 sysinfo->n_blockdevs);
100 sysinfo->blockdevs[sysinfo->n_blockdevs - 1] = bd_info;
102 discover_server_notify_system_info(server, sysinfo);
105 static void system_info_set_identifier(struct system_info *info)
107 struct process *process;
109 const char *argv[] = {
110 sysinfo_helper, NULL, NULL,
113 process = process_create(info);
114 process->path = sysinfo_helper;
115 process->argv = argv;
116 process->keep_stdout = true;
119 rc = process_run_sync(process);
122 info->type = talloc_strndup(info, process->stdout_buf,
123 process->stdout_len);
127 rc = process_run_sync(process);
130 info->identifier = talloc_strndup(info, process->stdout_buf,
131 process->stdout_len);
134 process_release(process);
137 void system_info_init(struct discover_server *s)
140 sysinfo = talloc_zero(server, struct system_info);
141 system_info_set_identifier(sysinfo);