71ef70b81fdb3f146e498ba4a38f9c0a438494bb
[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         list_init(&device->boot_options);
64
65         if (client->ops.device_add)
66                 client->ops.device_add(device, client->ops.cb_arg);
67 }
68
69 static void boot_option_add(struct discover_client *client,
70                 struct boot_option *opt)
71 {
72         struct device *dev;
73
74         dev = find_device(client, opt->device_id);
75
76         /* we require that devices are already present before any boot options
77          * are added */
78         assert(dev);
79
80         talloc_steal(dev, opt);
81         list_add(&dev->boot_options, &opt->list);
82
83         if (client->ops.boot_option_add)
84                 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
85 }
86
87 static void device_remove(struct discover_client *client, const char *id)
88 {
89         struct device *device = NULL;
90         int i;
91
92         for (i = 0; i < client->n_devices; i++) {
93                 if (!strcmp(client->devices[i]->id, id)) {
94                         device = client->devices[i];
95                         break;
96                 }
97         }
98
99         if (!device)
100                 return;
101
102         /* remove the device from the client's device array */
103         client->n_devices--;
104         memmove(&client->devices[i], &client->devices[i+1],
105                         (client->n_devices - i) * sizeof(client->devices[0]));
106         client->devices = talloc_realloc(client, client->devices,
107                         struct device *, client->n_devices);
108
109         /* notify the UI */
110         client->ops.device_remove(device, client->ops.cb_arg);
111
112         talloc_free(device);
113 }
114
115 void discover_client_enumerate(struct discover_client *client)
116 {
117         struct boot_option *opt;
118         struct device *device;
119         int i;
120
121         for (i = 0; i < client->n_devices; i++) {
122                 device = client->devices[i];
123                 if (client->ops.device_add)
124                         client->ops.device_add(device, client->ops.cb_arg);
125
126                 list_for_each_entry(&device->boot_options, opt, list)
127                         if (client->ops.boot_option_add)
128                                 client->ops.boot_option_add(device, opt,
129                                                 client->ops.cb_arg);
130         }
131 }
132
133 static void update_status(struct discover_client *client,
134                 struct boot_status *status)
135 {
136         if (client->ops.update_status)
137                 client->ops.update_status(status, client->ops.cb_arg);
138 }
139
140 static void update_sysinfo(struct discover_client *client,
141                 struct system_info *sysinfo)
142 {
143         if (client->ops.update_sysinfo)
144                 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
145 }
146
147 static void update_config(struct discover_client *client,
148                 struct config *config)
149 {
150         if (client->ops.update_config)
151                 client->ops.update_config(config, client->ops.cb_arg);
152 }
153
154 static int discover_client_process(void *arg)
155 {
156         struct discover_client *client = arg;
157         struct pb_protocol_message *message;
158         struct system_info *sysinfo;
159         struct boot_status *status;
160         struct boot_option *opt;
161         struct config *config;
162         struct device *dev;
163         char *dev_id;
164         void *ctx;
165         int rc;
166
167         /* We use a temporary context for processing one message; persistent
168          * data is re-parented to the client in the callbacks. */
169         ctx = talloc_new(client);
170
171         message = pb_protocol_read_message(ctx, client->fd);
172
173         if (!message)
174                 return -1;
175
176         switch (message->action) {
177         case PB_PROTOCOL_ACTION_DEVICE_ADD:
178                 dev = talloc_zero(ctx, struct device);
179                 list_init(&dev->boot_options);
180
181                 rc = pb_protocol_deserialise_device(dev, message);
182                 if (rc) {
183                         pb_log("%s: no device?\n", __func__);
184                         goto out;
185                 }
186
187                 device_add(client, dev);
188                 break;
189         case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
190                 opt = talloc_zero(ctx, struct boot_option);
191
192                 rc = pb_protocol_deserialise_boot_option(opt, message);
193                 if (rc) {
194                         pb_log("%s: no boot_option?\n", __func__);
195                         goto out;
196                 }
197
198                 boot_option_add(client, opt);
199                 break;
200         case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
201                 dev_id = pb_protocol_deserialise_string(ctx, message);
202                 if (!dev_id) {
203                         pb_log("%s: no device id?\n", __func__);
204                         goto out;
205                 }
206                 device_remove(client, dev_id);
207                 break;
208         case PB_PROTOCOL_ACTION_STATUS:
209                 status = talloc_zero(ctx, struct boot_status);
210
211                 rc = pb_protocol_deserialise_boot_status(status, message);
212                 if (rc) {
213                         pb_log("%s: invalid status message?\n", __func__);
214                         goto out;
215                 }
216                 update_status(client, status);
217                 break;
218         case PB_PROTOCOL_ACTION_SYSTEM_INFO:
219                 sysinfo = talloc_zero(ctx, struct system_info);
220
221                 rc = pb_protocol_deserialise_system_info(sysinfo, message);
222                 if (rc) {
223                         pb_log("%s: invalid sysinfo message?\n", __func__);
224                         goto out;
225                 }
226                 update_sysinfo(client, sysinfo);
227                 break;
228         case PB_PROTOCOL_ACTION_CONFIG:
229                 config = talloc_zero(ctx, struct config);
230
231                 rc = pb_protocol_deserialise_config(config, message);
232                 if (rc) {
233                         pb_log("%s: invalid config message?\n", __func__);
234                         goto out;
235                 }
236                 update_config(client, config);
237                 break;
238         default:
239                 pb_log("%s: unknown action %d\n", __func__, message->action);
240         }
241
242 out:
243         talloc_free(ctx);
244
245         return 0;
246 }
247
248 struct discover_client* discover_client_init(struct waitset *waitset,
249         const struct discover_client_ops *ops, void *cb_arg)
250 {
251         struct discover_client *client;
252         struct sockaddr_un addr;
253
254         client = talloc(NULL, struct discover_client);
255         if (!client)
256                 return NULL;
257
258         memcpy(&client->ops, ops, sizeof(client->ops));
259         client->ops.cb_arg = cb_arg;
260
261         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
262         if (client->fd < 0) {
263                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
264                 goto out_err;
265         }
266
267         talloc_set_destructor(client, discover_client_destructor);
268
269         client->n_devices = 0;
270         client->devices = NULL;
271
272         addr.sun_family = AF_UNIX;
273         strcpy(addr.sun_path, PB_SOCKET_PATH);
274
275         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
276                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
277                 goto out_err;
278         }
279
280         waiter_register_io(waitset, client->fd, WAIT_IN,
281                         discover_client_process, client);
282
283         return client;
284
285 out_err:
286         talloc_free(client);
287         return NULL;
288 }
289
290 /* accessors for discovered devices */
291 int discover_client_device_count(struct discover_client *client)
292 {
293         return client->n_devices;
294 }
295
296 struct device *discover_client_get_device(struct discover_client *client,
297                 int index)
298 {
299         if (index < 0 || index >= client->n_devices)
300                 return NULL;
301
302         return client->devices[index];
303 }
304
305 static void create_boot_command(struct boot_command *command,
306                 const struct device *device __attribute__((unused)),
307                 const struct boot_option *boot_option,
308                 const struct pb_boot_data *data)
309 {
310         command->option_id = boot_option ? boot_option->id : NULL;
311         command->boot_image_file = data->image;
312         command->initrd_file = data->initrd;
313         command->dtb_file = data->dtb;
314         command->boot_args = data->args;
315 }
316
317 int discover_client_boot(struct discover_client *client,
318                 const struct device *device,
319                 const struct boot_option *boot_option,
320                 const struct pb_boot_data *data)
321 {
322         struct pb_protocol_message *message;
323         struct boot_command boot_command;
324         int len, rc;
325
326         create_boot_command(&boot_command, device, boot_option, data);
327
328         len = pb_protocol_boot_len(&boot_command);
329
330         message = pb_protocol_create_message(client,
331                         PB_PROTOCOL_ACTION_BOOT, len);
332
333         if (!message)
334                 return -1;
335
336         pb_protocol_serialise_boot_command(&boot_command,
337                         message->payload, len);
338
339         rc = pb_protocol_write_message(client->fd, message);
340
341         return rc;
342 }
343
344 int discover_client_cancel_default(struct discover_client *client)
345 {
346         struct pb_protocol_message *message;
347
348         message = pb_protocol_create_message(client,
349                         PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
350
351         if (!message)
352                 return -1;
353
354         return pb_protocol_write_message(client->fd, message);
355 }
356
357 int discover_client_send_reinit(struct discover_client *client)
358 {
359         struct pb_protocol_message *message;
360
361         message = pb_protocol_create_message(client,
362                         PB_PROTOCOL_ACTION_REINIT, 0);
363
364         if (!message)
365                 return -1;
366
367         return pb_protocol_write_message(client->fd, message);
368 }
369
370 int discover_client_send_config(struct discover_client *client,
371                 struct config *config)
372 {
373         struct pb_protocol_message *message;
374         int len;
375
376         len = pb_protocol_config_len(config);
377
378         message = pb_protocol_create_message(client,
379                                 PB_PROTOCOL_ACTION_CONFIG, len);
380         if (!message)
381                 return -1;
382
383         pb_protocol_serialise_config(config, message->payload, len);
384
385         return pb_protocol_write_message(client->fd, message);
386 }
387
388 int discover_client_send_url(struct discover_client *client,
389                 char *url)
390 {
391         struct pb_protocol_message *message;
392         int len;
393
394         len = pb_protocol_url_len(url);
395
396         message = pb_protocol_create_message(client,
397                                 PB_PROTOCOL_ACTION_ADD_URL, len);
398         if (!message)
399                 return -1;
400
401         pb_protocol_serialise_url(url, message->payload, len);
402
403         return pb_protocol_write_message(client->fd, message);
404 }