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