be4543832265a964ceea26124dbcdc1a8d85a2c9
[petitboot] / discover / sysinfo.c
1
2 #include <string.h>
3
4 #include <talloc/talloc.h>
5 #include <process/process.h>
6
7 #include "discover-server.h"
8 #include "sysinfo.h"
9
10 static struct system_info *sysinfo;
11 static struct discover_server *server;
12
13 static const char *sysinfo_helper = PKG_LIBEXEC_DIR "/pb-sysinfo";
14
15 const struct system_info *system_info_get(void)
16 {
17         return sysinfo;
18 }
19
20 void system_info_register_interface(unsigned int hwaddr_size, uint8_t *hwaddr,
21                 const char *name, bool link)
22 {
23         struct interface_info *if_info;
24         unsigned int i;
25
26         for (i = 0; i < sysinfo->n_interfaces; i++) {
27                 bool changed = false;
28
29                 if_info = sysinfo->interfaces[i];
30
31                 if (if_info->hwaddr_size != hwaddr_size)
32                         continue;
33
34                 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
35                         continue;
36
37                 /* Found an existing interface. Notify clients on any name or
38                  * link changes */
39                 if (strcmp(if_info->name, name)) {
40                         talloc_free(if_info->name);
41                         if_info->name = talloc_strdup(if_info, name);
42                         changed = true;
43                 }
44
45                 if (if_info->link != link) {
46                         if_info->link = link;
47                         changed = true;
48                 }
49
50                 if (changed)
51                         discover_server_notify_system_info(server, sysinfo);
52
53                 return;
54         }
55
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);
60         if_info->link = link;
61
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;
67
68         discover_server_notify_system_info(server, sysinfo);
69 }
70
71 void system_info_register_blockdev(const char *name, const char *uuid,
72                 const char *mountpoint)
73 {
74         struct blockdev_info *bd_info;
75         unsigned int i;
76
77         for (i = 0; i < sysinfo->n_blockdevs; i++) {
78                 bd_info = sysinfo->blockdevs[i];
79
80                 if (strcmp(bd_info->name, name))
81                         continue;
82
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);
88                 return;
89         }
90
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);
95
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;
101
102         discover_server_notify_system_info(server, sysinfo);
103 }
104
105 static void system_info_set_identifier(struct system_info *info)
106 {
107         struct process *process;
108         int rc;
109         const char *argv[] = {
110                 sysinfo_helper, NULL, NULL,
111         };
112
113         process = process_create(info);
114         process->path = sysinfo_helper;
115         process->argv = argv;
116         process->keep_stdout = true;
117
118         argv[1] = "--type";
119         rc = process_run_sync(process);
120
121         if (!rc) {
122                 info->type = talloc_strndup(info, process->stdout_buf,
123                                 process->stdout_len);
124         }
125
126         argv[1] = "--id";
127         rc = process_run_sync(process);
128
129         if (!rc) {
130                 info->identifier = talloc_strndup(info, process->stdout_buf,
131                                 process->stdout_len);
132         }
133
134         process_release(process);
135 }
136
137 void system_info_init(struct discover_server *s)
138 {
139         server = s;
140         sysinfo = talloc_zero(server, struct system_info);
141         system_info_set_identifier(sysinfo);
142 }