]> git.ozlabs.org Git - petitboot/blob - ui/common/discover-client.c
a124f8bee8d9ffd03eb008ca4bae26e801a53699
[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 static void update_status(struct discover_client *client,
116                 struct boot_status *status)
117 {
118         if (client->ops.update_status)
119                 client->ops.update_status(status, client->ops.cb_arg);
120 }
121
122 static void update_sysinfo(struct discover_client *client,
123                 struct system_info *sysinfo)
124 {
125         if (client->ops.update_sysinfo)
126                 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
127 }
128
129 static void update_config(struct discover_client *client,
130                 struct config *config)
131 {
132         if (client->ops.update_config)
133                 client->ops.update_config(config, client->ops.cb_arg);
134 }
135
136 static int discover_client_process(void *arg)
137 {
138         struct discover_client *client = arg;
139         struct pb_protocol_message *message;
140         struct system_info *sysinfo;
141         struct boot_status *status;
142         struct boot_option *opt;
143         struct config *config;
144         struct device *dev;
145         char *dev_id;
146         void *ctx;
147         int rc;
148
149         /* We use a temporary context for processing one message; persistent
150          * data is re-parented to the client in the callbacks. */
151         ctx = talloc_new(client);
152
153         message = pb_protocol_read_message(ctx, client->fd);
154
155         if (!message)
156                 return -1;
157
158         switch (message->action) {
159         case PB_PROTOCOL_ACTION_DEVICE_ADD:
160                 dev = talloc_zero(ctx, struct device);
161                 list_init(&dev->boot_options);
162
163                 rc = pb_protocol_deserialise_device(dev, message);
164                 if (rc) {
165                         pb_log("%s: no device?\n", __func__);
166                         return 0;
167                 }
168
169                 device_add(client, dev);
170                 break;
171         case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
172                 opt = talloc_zero(ctx, struct boot_option);
173
174                 rc = pb_protocol_deserialise_boot_option(opt, message);
175                 if (rc) {
176                         pb_log("%s: no boot_option?\n", __func__);
177                         return 0;
178                 }
179
180                 boot_option_add(client, opt);
181                 break;
182         case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
183                 dev_id = pb_protocol_deserialise_string(ctx, message);
184                 if (!dev_id) {
185                         pb_log("%s: no device id?\n", __func__);
186                         return 0;
187                 }
188                 device_remove(client, dev_id);
189                 break;
190         case PB_PROTOCOL_ACTION_STATUS:
191                 status = talloc_zero(ctx, struct boot_status);
192
193                 rc = pb_protocol_deserialise_boot_status(status, message);
194                 if (rc) {
195                         pb_log("%s: invalid status message?\n", __func__);
196                         return 0;
197                 }
198                 update_status(client, status);
199                 break;
200         case PB_PROTOCOL_ACTION_SYSTEM_INFO:
201                 sysinfo = talloc_zero(ctx, struct system_info);
202
203                 rc = pb_protocol_deserialise_system_info(sysinfo, message);
204                 if (rc) {
205                         pb_log("%s: invalid sysinfo message?\n", __func__);
206                         return 0;
207                 }
208                 update_sysinfo(client, sysinfo);
209                 break;
210         case PB_PROTOCOL_ACTION_CONFIG:
211                 config = talloc_zero(ctx, struct config);
212
213                 rc = pb_protocol_deserialise_config(config, message);
214                 if (rc) {
215                         pb_log("%s: invalid config message?\n", __func__);
216                         return 0;
217                 }
218                 update_config(client, config);
219                 break;
220         default:
221                 pb_log("%s: unknown action %d\n", __func__, message->action);
222         }
223
224         talloc_free(ctx);
225
226         return 0;
227 }
228
229 struct discover_client* discover_client_init(struct waitset *waitset,
230         const struct discover_client_ops *ops, void *cb_arg)
231 {
232         struct discover_client *client;
233         struct sockaddr_un addr;
234
235         client = talloc(NULL, struct discover_client);
236         if (!client)
237                 return NULL;
238
239         memcpy(&client->ops, ops, sizeof(client->ops));
240         client->ops.cb_arg = cb_arg;
241
242         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
243         if (client->fd < 0) {
244                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
245                 goto out_err;
246         }
247
248         talloc_set_destructor(client, discover_client_destructor);
249
250         client->n_devices = 0;
251         client->devices = NULL;
252
253         addr.sun_family = AF_UNIX;
254         strcpy(addr.sun_path, PB_SOCKET_PATH);
255
256         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
257                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
258                 goto out_err;
259         }
260
261         waiter_register_io(waitset, client->fd, WAIT_IN,
262                         discover_client_process, client);
263
264         return client;
265
266 out_err:
267         talloc_free(client);
268         return NULL;
269 }
270
271 /* accessors for discovered devices */
272 int discover_client_device_count(struct discover_client *client)
273 {
274         return client->n_devices;
275 }
276
277 struct device *discover_client_get_device(struct discover_client *client,
278                 int index)
279 {
280         if (index < 0 || index >= client->n_devices)
281                 return NULL;
282
283         return client->devices[index];
284 }
285
286 static void create_boot_command(struct boot_command *command,
287                 const struct device *device __attribute__((unused)),
288                 const struct boot_option *boot_option,
289                 const struct pb_boot_data *data)
290 {
291         command->option_id = boot_option ? boot_option->id : NULL;
292         command->boot_image_file = data->image;
293         command->initrd_file = data->initrd;
294         command->dtb_file = data->dtb;
295         command->boot_args = data->args;
296 }
297
298 int discover_client_boot(struct discover_client *client,
299                 const struct device *device,
300                 const struct boot_option *boot_option,
301                 const struct pb_boot_data *data)
302 {
303         struct pb_protocol_message *message;
304         struct boot_command boot_command;
305         int len, rc;
306
307         create_boot_command(&boot_command, device, boot_option, data);
308
309         len = pb_protocol_boot_len(&boot_command);
310
311         message = pb_protocol_create_message(client,
312                         PB_PROTOCOL_ACTION_BOOT, len);
313
314         if (!message)
315                 return -1;
316
317         pb_protocol_serialise_boot_command(&boot_command,
318                         message->payload, len);
319
320         rc = pb_protocol_write_message(client->fd, message);
321
322         return rc;
323 }
324
325 int discover_client_cancel_default(struct discover_client *client)
326 {
327         struct pb_protocol_message *message;
328
329         message = pb_protocol_create_message(client,
330                         PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
331
332         if (!message)
333                 return -1;
334
335         return pb_protocol_write_message(client->fd, message);
336 }
337
338 int discover_client_send_reinit(struct discover_client *client)
339 {
340         struct pb_protocol_message *message;
341
342         message = pb_protocol_create_message(client,
343                         PB_PROTOCOL_ACTION_REINIT, 0);
344
345         if (!message)
346                 return -1;
347
348         return pb_protocol_write_message(client->fd, message);
349 }
350
351 int discover_client_send_config(struct discover_client *client,
352                 struct config *config)
353 {
354         struct pb_protocol_message *message;
355         int len;
356
357         len = pb_protocol_config_len(config);
358
359         message = pb_protocol_create_message(client,
360                                 PB_PROTOCOL_ACTION_CONFIG, len);
361         if (!message)
362                 return -1;
363
364         pb_protocol_serialise_config(config, message->payload, len);
365
366         return pb_protocol_write_message(client->fd, message);
367 }