discover/sysinfo: Fix talloc against uninitialised server var
[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)
22 {
23         struct interface_info *if_info;
24         unsigned int i;
25
26         for (i = 0; i < sysinfo->n_interfaces; i++) {
27                 if_info = sysinfo->interfaces[i];
28
29                 if (if_info->hwaddr_size != hwaddr_size)
30                         continue;
31
32                 if (memcmp(if_info->hwaddr, hwaddr, hwaddr_size))
33                         continue;
34
35                 /* update the name and we're done */
36                 talloc_free(if_info->name);
37                 if_info->name = talloc_strdup(if_info, name);
38                 discover_server_notify_system_info(server, sysinfo);
39                 return;
40         }
41
42         if_info = talloc_zero(sysinfo, struct interface_info);
43         if_info->hwaddr_size = hwaddr_size;
44         if_info->hwaddr = talloc_memdup(if_info, hwaddr, hwaddr_size);
45         if_info->name = talloc_strdup(if_info, name);
46
47         sysinfo->n_interfaces++;
48         sysinfo->interfaces = talloc_realloc(sysinfo, sysinfo->interfaces,
49                                                 struct interface_info *,
50                                                 sysinfo->n_interfaces);
51         sysinfo->interfaces[sysinfo->n_interfaces - 1] = if_info;
52
53         discover_server_notify_system_info(server, sysinfo);
54 }
55
56 static void system_info_set_identifier(struct system_info *info)
57 {
58         struct process *process;
59         int rc;
60         const char *argv[] = {
61                 sysinfo_helper, NULL, NULL,
62         };
63
64         process = process_create(info);
65         process->path = sysinfo_helper;
66         process->argv = argv;
67         process->keep_stdout = true;
68
69         argv[1] = "--type";
70         rc = process_run_sync(process);
71
72         if (!rc) {
73                 info->type = talloc_strndup(info, process->stdout_buf,
74                                 process->stdout_len);
75         }
76
77         argv[1] = "--id";
78         rc = process_run_sync(process);
79
80         if (!rc) {
81                 info->identifier = talloc_strndup(info, process->stdout_buf,
82                                 process->stdout_len);
83         }
84
85         process_release(process);
86 }
87
88 void system_info_init(struct discover_server *s)
89 {
90         server = s;
91         sysinfo = talloc_zero(server, struct system_info);
92         system_info_set_identifier(sysinfo);
93 }