]> git.ozlabs.org Git - petitboot/blob - ui/common/discover-client.c
discover: populate sysinfo with block devices
[petitboot] / ui / common / discover-client.c
1
2 #include <assert.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdint.h>
8
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <asm/byteorder.h>
12
13 #include <talloc/talloc.h>
14 #include <log/log.h>
15
16 #include "discover-client.h"
17 #include "pb-protocol/pb-protocol.h"
18
19 struct discover_client {
20         int fd;
21         struct discover_client_ops ops;
22         int n_devices;
23         struct device **devices;
24 };
25
26 static int discover_client_destructor(void *arg)
27 {
28         struct discover_client *client = arg;
29
30         if (client->fd >= 0)
31                 close(client->fd);
32
33         return 0;
34 }
35
36 void discover_client_destroy(struct discover_client *client)
37 {
38         talloc_free(client);
39 }
40
41 static struct device *find_device(struct discover_client *client,
42                 const char *id)
43 {
44         int i;
45
46         for (i = 0; i < client->n_devices; i++) {
47                 struct device *dev = client->devices[i];
48                 if (!strcmp(dev->id, id))
49                         return dev;
50         }
51
52         return NULL;
53 }
54
55 static void device_add(struct discover_client *client, struct device *device)
56 {
57         client->n_devices++;
58         client->devices = talloc_realloc(client, client->devices,
59                         struct device *, client->n_devices);
60
61         client->devices[client->n_devices - 1] = device;
62         talloc_steal(client, device);
63
64         if (client->ops.device_add)
65                 client->ops.device_add(device, client->ops.cb_arg);
66 }
67
68 static void boot_option_add(struct discover_client *client,
69                 struct boot_option *opt)
70 {
71         struct device *dev;
72
73         dev = find_device(client, opt->device_id);
74
75         /* we require that devices are already present before any boot options
76          * are added */
77         assert(dev);
78
79         talloc_steal(dev, opt);
80
81         if (client->ops.boot_option_add)
82                 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
83 }
84
85 static void device_remove(struct discover_client *client, const char *id)
86 {
87         struct device *device = NULL;
88         int i;
89
90         for (i = 0; i < client->n_devices; i++) {
91                 if (!strcmp(client->devices[i]->id, id)) {
92                         device = client->devices[i];
93                         break;
94                 }
95         }
96
97         if (!device)
98                 return;
99
100         /* remove the device from the client's device array */
101         client->n_devices--;
102         memmove(&client->devices[i], &client->devices[i+1],
103                         (client->n_devices - i) * sizeof(client->devices[0]));
104         client->devices = talloc_realloc(client, client->devices,
105                         struct device *, client->n_devices);
106
107         /* notify the UI */
108         client->ops.device_remove(device, client->ops.cb_arg);
109
110         talloc_free(device);
111 }
112
113 static void update_status(struct discover_client *client,
114                 struct boot_status *status)
115 {
116         if (client->ops.update_status)
117                 client->ops.update_status(status, client->ops.cb_arg);
118 }
119
120 static void update_sysinfo(struct discover_client *client,
121                 struct system_info *sysinfo)
122 {
123         if (client->ops.update_sysinfo)
124                 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
125 }
126
127 static void update_config(struct discover_client *client,
128                 struct config *config)
129 {
130         if (client->ops.update_config)
131                 client->ops.update_config(config, client->ops.cb_arg);
132 }
133
134 static int discover_client_process(void *arg)
135 {
136         struct discover_client *client = arg;
137         struct pb_protocol_message *message;
138         struct system_info *sysinfo;
139         struct boot_status *status;
140         struct boot_option *opt;
141         struct config *config;
142         struct device *dev;
143         char *dev_id;
144         void *ctx;
145         int rc;
146
147         /* We use a temporary context for processing one message; persistent
148          * data is re-parented to the client in the callbacks. */
149         ctx = talloc_new(client);
150
151         message = pb_protocol_read_message(ctx, client->fd);
152
153         if (!message)
154                 return -1;
155
156         switch (message->action) {
157         case PB_PROTOCOL_ACTION_DEVICE_ADD:
158                 dev = talloc_zero(ctx, struct device);
159                 list_init(&dev->boot_options);
160
161                 rc = pb_protocol_deserialise_device(dev, message);
162                 if (rc) {
163                         pb_log("%s: no device?\n", __func__);
164                         return 0;
165                 }
166
167                 device_add(client, dev);
168                 break;
169         case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
170                 opt = talloc_zero(ctx, struct boot_option);
171
172                 rc = pb_protocol_deserialise_boot_option(opt, message);
173                 if (rc) {
174                         pb_log("%s: no boot_option?\n", __func__);
175                         return 0;
176                 }
177
178                 boot_option_add(client, opt);
179                 break;
180         case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
181                 dev_id = pb_protocol_deserialise_string(ctx, message);
182                 if (!dev_id) {
183                         pb_log("%s: no device id?\n", __func__);
184                         return 0;
185                 }
186                 device_remove(client, dev_id);
187                 break;
188         case PB_PROTOCOL_ACTION_STATUS:
189                 status = talloc_zero(ctx, struct boot_status);
190
191                 rc = pb_protocol_deserialise_boot_status(status, message);
192                 if (rc) {
193                         pb_log("%s: invalid status message?\n", __func__);
194                         return 0;
195                 }
196                 update_status(client, status);
197                 break;
198         case PB_PROTOCOL_ACTION_SYSTEM_INFO:
199                 sysinfo = talloc_zero(ctx, struct system_info);
200
201                 rc = pb_protocol_deserialise_system_info(sysinfo, message);
202                 if (rc) {
203                         pb_log("%s: invalid sysinfo message?\n", __func__);
204                         return 0;
205                 }
206                 update_sysinfo(client, sysinfo);
207                 break;
208         case PB_PROTOCOL_ACTION_CONFIG:
209                 config = talloc_zero(ctx, struct config);
210
211                 rc = pb_protocol_deserialise_config(config, message);
212                 if (rc) {
213                         pb_log("%s: invalid config message?\n", __func__);
214                         return 0;
215                 }
216                 update_config(client, config);
217                 break;
218         default:
219                 pb_log("%s: unknown action %d\n", __func__, message->action);
220         }
221
222         talloc_free(ctx);
223
224         return 0;
225 }
226
227 struct discover_client* discover_client_init(struct waitset *waitset,
228         const struct discover_client_ops *ops, void *cb_arg)
229 {
230         struct discover_client *client;
231         struct sockaddr_un addr;
232
233         client = talloc(NULL, struct discover_client);
234         if (!client)
235                 return NULL;
236
237         memcpy(&client->ops, ops, sizeof(client->ops));
238         client->ops.cb_arg = cb_arg;
239
240         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
241         if (client->fd < 0) {
242                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
243                 goto out_err;
244         }
245
246         talloc_set_destructor(client, discover_client_destructor);
247
248         client->n_devices = 0;
249         client->devices = NULL;
250
251         addr.sun_family = AF_UNIX;
252         strcpy(addr.sun_path, PB_SOCKET_PATH);
253
254         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
255                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
256                 goto out_err;
257         }
258
259         waiter_register_io(waitset, client->fd, WAIT_IN,
260                         discover_client_process, client);
261
262         return client;
263
264 out_err:
265         talloc_free(client);
266         return NULL;
267 }
268
269 /* accessors for discovered devices */
270 int discover_client_device_count(struct discover_client *client)
271 {
272         return client->n_devices;
273 }
274
275 struct device *discover_client_get_device(struct discover_client *client,
276                 int index)
277 {
278         if (index < 0 || index >= client->n_devices)
279                 return NULL;
280
281         return client->devices[index];
282 }
283
284 static void create_boot_command(struct boot_command *command,
285                 const struct device *device __attribute__((unused)),
286                 const struct boot_option *boot_option,
287                 const struct pb_boot_data *data)
288 {
289         command->option_id = boot_option ? boot_option->id : NULL;
290         command->boot_image_file = data->image;
291         command->initrd_file = data->initrd;
292         command->dtb_file = data->dtb;
293         command->boot_args = data->args;
294 }
295
296 int discover_client_boot(struct discover_client *client,
297                 const struct device *device,
298                 const struct boot_option *boot_option,
299                 const struct pb_boot_data *data)
300 {
301         struct pb_protocol_message *message;
302         struct boot_command boot_command;
303         int len, rc;
304
305         create_boot_command(&boot_command, device, boot_option, data);
306
307         len = pb_protocol_boot_len(&boot_command);
308
309         message = pb_protocol_create_message(client,
310                         PB_PROTOCOL_ACTION_BOOT, len);
311
312         if (!message)
313                 return -1;
314
315         pb_protocol_serialise_boot_command(&boot_command,
316                         message->payload, len);
317
318         rc = pb_protocol_write_message(client->fd, message);
319
320         return rc;
321 }
322
323 int discover_client_cancel_default(struct discover_client *client)
324 {
325         struct pb_protocol_message *message;
326
327         message = pb_protocol_create_message(client,
328                         PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
329
330         if (!message)
331                 return -1;
332
333         return pb_protocol_write_message(client->fd, message);
334 }
335
336 int discover_client_send_config(struct discover_client *client,
337                 struct config *config)
338 {
339         struct pb_protocol_message *message;
340         int len;
341
342         len = pb_protocol_config_len(config);
343
344         message = pb_protocol_create_message(client,
345                                 PB_PROTOCOL_ACTION_CONFIG, len);
346         if (!message)
347                 return -1;
348
349         pb_protocol_serialise_config(config, message->payload, len);
350
351         return pb_protocol_write_message(client->fd, message);
352 }