discover: Enable 'url' pb-events
[petitboot] / discover / discover-server.c
1
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <errno.h>
7 #include <assert.h>
8
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <asm/byteorder.h>
12
13 #include <pb-config/pb-config.h>
14 #include <talloc/talloc.h>
15 #include <waiter/waiter.h>
16 #include <log/log.h>
17
18 #include "pb-protocol/pb-protocol.h"
19 #include "list/list.h"
20
21 #include "device-handler.h"
22 #include "discover-server.h"
23 #include "platform.h"
24 #include "sysinfo.h"
25
26 struct discover_server {
27         int socket;
28         struct waitset *waitset;
29         struct waiter *waiter;
30         struct list clients;
31         struct device_handler *device_handler;
32 };
33
34 struct client {
35         struct discover_server *server;
36         struct list_item list;
37         struct waiter *waiter;
38         int fd;
39         bool remote_closed;
40 };
41
42
43 static int server_destructor(void *arg)
44 {
45         struct discover_server *server = arg;
46
47         if (server->waiter)
48                 waiter_remove(server->waiter);
49
50         if (server->socket >= 0)
51                 close(server->socket);
52
53         return 0;
54 }
55
56 static int client_destructor(void *arg)
57 {
58         struct client *client = arg;
59
60         if (client->fd >= 0)
61                 close(client->fd);
62
63         if (client->waiter)
64                 waiter_remove(client->waiter);
65
66         list_remove(&client->list);
67
68         return 0;
69
70 }
71
72 static void print_clients(struct discover_server *server)
73         __attribute__((unused));
74
75 static void print_clients(struct discover_server *server)
76 {
77         struct client *client;
78
79         pb_debug("current clients [%p,%p,%p]:\n",
80                         &server->clients.head,
81                         server->clients.head.prev,
82                         server->clients.head.next);
83         list_for_each_entry(&server->clients, client, list)
84                 pb_debug("\t[%p,%p,%p] client: %d\n", &client->list,
85                                 client->list.prev, client->list.next,
86                                 client->fd);
87 }
88
89 static int client_write_message(
90                 struct discover_server *server __attribute__((unused)),
91                 struct client *client, struct pb_protocol_message *message)
92 {
93         int rc;
94
95         if (client->remote_closed)
96                 return -1;
97
98         rc = pb_protocol_write_message(client->fd, message);
99         if (rc)
100                 client->remote_closed = true;
101
102         return rc;
103 }
104
105 static int write_device_add_message(struct discover_server *server,
106                 struct client *client, const struct device *dev)
107 {
108         struct pb_protocol_message *message;
109         int len;
110
111         len = pb_protocol_device_len(dev);
112
113         message = pb_protocol_create_message(client,
114                         PB_PROTOCOL_ACTION_DEVICE_ADD, len);
115         if (!message)
116                 return -1;
117
118         pb_protocol_serialise_device(dev, message->payload, len);
119
120         return client_write_message(server, client, message);
121 }
122
123 static int write_boot_option_add_message(struct discover_server *server,
124                 struct client *client, const struct boot_option *opt)
125 {
126         struct pb_protocol_message *message;
127         int len;
128
129         len = pb_protocol_boot_option_len(opt);
130
131         message = pb_protocol_create_message(client,
132                         PB_PROTOCOL_ACTION_BOOT_OPTION_ADD, len);
133         if (!message)
134                 return -1;
135
136         pb_protocol_serialise_boot_option(opt, message->payload, len);
137
138         return client_write_message(server, client, message);
139 }
140
141 static int write_device_remove_message(struct discover_server *server,
142                 struct client *client, char *dev_id)
143 {
144         struct pb_protocol_message *message;
145         int len;
146
147         len = strlen(dev_id) + sizeof(uint32_t);
148
149         message = pb_protocol_create_message(client,
150                         PB_PROTOCOL_ACTION_DEVICE_REMOVE, len);
151         if (!message)
152                 return -1;
153
154         pb_protocol_serialise_string(message->payload, dev_id);
155
156         return client_write_message(server, client, message);
157 }
158
159 static int write_boot_status_message(struct discover_server *server,
160                 struct client *client, const struct boot_status *status)
161 {
162         struct pb_protocol_message *message;
163         int len;
164
165         len = pb_protocol_boot_status_len(status);
166
167         message = pb_protocol_create_message(client,
168                         PB_PROTOCOL_ACTION_STATUS, len);
169         if (!message)
170                 return -1;
171
172         pb_protocol_serialise_boot_status(status, message->payload, len);
173
174         return client_write_message(server, client, message);
175 }
176
177 static int write_system_info_message(struct discover_server *server,
178                 struct client *client, const struct system_info *sysinfo)
179 {
180         struct pb_protocol_message *message;
181         int len;
182
183         len = pb_protocol_system_info_len(sysinfo);
184
185         message = pb_protocol_create_message(client,
186                         PB_PROTOCOL_ACTION_SYSTEM_INFO, len);
187         if (!message)
188                 return -1;
189
190         pb_protocol_serialise_system_info(sysinfo, message->payload, len);
191
192         return client_write_message(server, client, message);
193 }
194
195 static int write_config_message(struct discover_server *server,
196                 struct client *client, const struct config *config)
197 {
198         struct pb_protocol_message *message;
199         int len;
200
201         len = pb_protocol_config_len(config);
202
203         message = pb_protocol_create_message(client,
204                         PB_PROTOCOL_ACTION_CONFIG, len);
205         if (!message)
206                 return -1;
207
208         pb_protocol_serialise_config(config, message->payload, len);
209
210         return client_write_message(server, client, message);
211 }
212
213 static int discover_server_process_message(void *arg)
214 {
215         struct pb_protocol_message *message;
216         struct boot_command *boot_command;
217         struct client *client = arg;
218         struct config *config;
219         int rc;
220
221         message = pb_protocol_read_message(client, client->fd);
222
223         if (!message) {
224                 talloc_free(client);
225                 return 0;
226         }
227
228
229         switch (message->action) {
230         case PB_PROTOCOL_ACTION_BOOT:
231                 boot_command = talloc(client, struct boot_command);
232
233                 rc = pb_protocol_deserialise_boot_command(boot_command,
234                                 message);
235                 if (rc) {
236                         pb_log("%s: no boot command?", __func__);
237                         return 0;
238                 }
239
240                 device_handler_boot(client->server->device_handler,
241                                 boot_command);
242                 break;
243
244         case PB_PROTOCOL_ACTION_CANCEL_DEFAULT:
245                 device_handler_cancel_default(client->server->device_handler);
246                 break;
247
248         case PB_PROTOCOL_ACTION_REINIT:
249                 device_handler_reinit(client->server->device_handler);
250                 break;
251
252         case PB_PROTOCOL_ACTION_CONFIG:
253                 config = talloc_zero(client, struct config);
254
255                 rc = pb_protocol_deserialise_config(config, message);
256                 if (rc) {
257                         pb_log("%s: no config?", __func__);
258                         return 0;
259                 }
260
261                 device_handler_update_config(client->server->device_handler,
262                                 config);
263                 break;
264
265         default:
266                 pb_log("%s: invalid action %d\n", __func__, message->action);
267                 return 0;
268         }
269
270
271         return 0;
272 }
273
274 static int discover_server_process_connection(void *arg)
275 {
276         struct discover_server *server = arg;
277         int fd, rc, i, n_devices;
278         struct client *client;
279
280         /* accept the incoming connection */
281         fd = accept(server->socket, NULL, 0);
282         if (fd < 0) {
283                 pb_log("accept: %s\n", strerror(errno));
284                 return 0;
285         }
286
287         /* add to our list of clients */
288         client = talloc_zero(server, struct client);
289         list_add(&server->clients, &client->list);
290
291         talloc_set_destructor(client, client_destructor);
292
293         client->fd = fd;
294         client->server = server;
295         client->waiter = waiter_register_io(server->waitset, client->fd,
296                                 WAIT_IN, discover_server_process_message,
297                                 client);
298
299         /* send sysinfo to client */
300         rc = write_system_info_message(server, client, system_info_get());
301         if (rc)
302                 return 0;
303
304         /* send config to client */
305         rc = write_config_message(server, client, config_get());
306         if (rc)
307                 return 0;
308
309         /* send existing devices to client */
310         n_devices = device_handler_get_device_count(server->device_handler);
311         for (i = 0; i < n_devices; i++) {
312                 const struct discover_boot_option *opt;
313                 const struct discover_device *device;
314
315                 device = device_handler_get_device(server->device_handler, i);
316                 rc = write_device_add_message(server, client, device->device);
317                 if (rc)
318                         return 0;
319
320                 list_for_each_entry(&device->boot_options, opt, list) {
321                         rc = write_boot_option_add_message(server, client,
322                                         opt->option);
323                         if (rc)
324                                 return 0;
325                 }
326         }
327
328         return 0;
329 }
330
331 void discover_server_notify_device_add(struct discover_server *server,
332                 struct device *device)
333 {
334         struct client *client;
335
336         list_for_each_entry(&server->clients, client, list)
337                 write_device_add_message(server, client, device);
338
339 }
340
341 void discover_server_notify_boot_option_add(struct discover_server *server,
342                 struct boot_option *boot_option)
343 {
344         struct client *client;
345
346         list_for_each_entry(&server->clients, client, list)
347                 write_boot_option_add_message(server, client, boot_option);
348 }
349
350 void discover_server_notify_device_remove(struct discover_server *server,
351                 struct device *device)
352 {
353         struct client *client;
354
355         list_for_each_entry(&server->clients, client, list)
356                 write_device_remove_message(server, client, device->id);
357
358 }
359
360 void discover_server_notify_boot_status(struct discover_server *server,
361                 struct boot_status *status)
362 {
363         struct client *client;
364
365         list_for_each_entry(&server->clients, client, list)
366                 write_boot_status_message(server, client, status);
367 }
368
369 void discover_server_notify_system_info(struct discover_server *server,
370                 const struct system_info *sysinfo)
371 {
372         struct client *client;
373
374         list_for_each_entry(&server->clients, client, list)
375                 write_system_info_message(server, client, sysinfo);
376 }
377
378 void discover_server_notify_config(struct discover_server *server,
379                 const struct config *config)
380 {
381         struct client *client;
382
383         list_for_each_entry(&server->clients, client, list)
384                 write_config_message(server, client, config);
385 }
386
387 void discover_server_set_device_source(struct discover_server *server,
388                 struct device_handler *handler)
389 {
390         server->device_handler = handler;
391 }
392
393 struct discover_server *discover_server_init(struct waitset *waitset)
394 {
395         struct discover_server *server;
396         struct sockaddr_un addr;
397
398         server = talloc(NULL, struct discover_server);
399         if (!server)
400                 return NULL;
401
402         server->waiter = NULL;
403         server->waitset = waitset;
404         list_init(&server->clients);
405
406         unlink(PB_SOCKET_PATH);
407
408         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
409         if (server->socket < 0) {
410                 pb_log("error creating server socket: %s\n", strerror(errno));
411                 goto out_err;
412         }
413
414         talloc_set_destructor(server, server_destructor);
415
416         addr.sun_family = AF_UNIX;
417         strcpy(addr.sun_path, PB_SOCKET_PATH);
418
419         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
420                 pb_log("error binding server socket: %s\n", strerror(errno));
421                 goto out_err;
422         }
423
424         if (listen(server->socket, 8)) {
425                 pb_log("server socket listen: %s\n", strerror(errno));
426                 goto out_err;
427         }
428
429         server->waiter = waiter_register_io(server->waitset, server->socket,
430                         WAIT_IN, discover_server_process_connection, server);
431
432         return server;
433
434 out_err:
435         talloc_free(server);
436         return NULL;
437 }
438
439 void discover_server_destroy(struct discover_server *server)
440 {
441         talloc_free(server);
442 }
443