discover/network: In static config mode, only configure defined interfaces
[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 void system_info_register_blockdev(const char *name, const char *uuid,
57                 const char *mountpoint)
58 {
59         struct blockdev_info *bd_info;
60         unsigned int i;
61
62         for (i = 0; i < sysinfo->n_blockdevs; i++) {
63                 bd_info = sysinfo->blockdevs[i];
64
65                 if (strcmp(bd_info->name, name))
66                         continue;
67
68                 /* update the mountpoint and UUID, and we're done */
69                 talloc_free(bd_info->mountpoint);
70                 bd_info->uuid = talloc_strdup(bd_info, uuid);
71                 bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
72                 discover_server_notify_system_info(server, sysinfo);
73                 return;
74         }
75
76         bd_info = talloc_zero(sysinfo, struct blockdev_info);
77         bd_info->name = talloc_strdup(bd_info, name);
78         bd_info->uuid = talloc_strdup(bd_info, uuid);
79         bd_info->mountpoint = talloc_strdup(bd_info, mountpoint);
80
81         sysinfo->n_blockdevs++;
82         sysinfo->blockdevs = talloc_realloc(sysinfo, sysinfo->blockdevs,
83                                                 struct blockdev_info *,
84                                                 sysinfo->n_blockdevs);
85         sysinfo->blockdevs[sysinfo->n_blockdevs - 1] = bd_info;
86
87         discover_server_notify_system_info(server, sysinfo);
88 }
89
90 static void system_info_set_identifier(struct system_info *info)
91 {
92         struct process *process;
93         int rc;
94         const char *argv[] = {
95                 sysinfo_helper, NULL, NULL,
96         };
97
98         process = process_create(info);
99         process->path = sysinfo_helper;
100         process->argv = argv;
101         process->keep_stdout = true;
102
103         argv[1] = "--type";
104         rc = process_run_sync(process);
105
106         if (!rc) {
107                 info->type = talloc_strndup(info, process->stdout_buf,
108                                 process->stdout_len);
109         }
110
111         argv[1] = "--id";
112         rc = process_run_sync(process);
113
114         if (!rc) {
115                 info->identifier = talloc_strndup(info, process->stdout_buf,
116                                 process->stdout_len);
117         }
118
119         process_release(process);
120 }
121
122 void system_info_init(struct discover_server *s)
123 {
124         server = s;
125         sysinfo = talloc_zero(server, struct system_info);
126         system_info_set_identifier(sysinfo);
127 }