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