ui/ncurses: Add nc-subset selection screen
[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         char *url;
220         int rc;
221
222         message = pb_protocol_read_message(client, client->fd);
223
224         if (!message) {
225                 talloc_free(client);
226                 return 0;
227         }
228
229
230         switch (message->action) {
231         case PB_PROTOCOL_ACTION_BOOT:
232                 boot_command = talloc(client, struct boot_command);
233
234                 rc = pb_protocol_deserialise_boot_command(boot_command,
235                                 message);
236                 if (rc) {
237                         pb_log("%s: no boot command?", __func__);
238                         return 0;
239                 }
240
241                 device_handler_boot(client->server->device_handler,
242                                 boot_command);
243                 break;
244
245         case PB_PROTOCOL_ACTION_CANCEL_DEFAULT:
246                 device_handler_cancel_default(client->server->device_handler);
247                 break;
248
249         case PB_PROTOCOL_ACTION_REINIT:
250                 device_handler_reinit(client->server->device_handler);
251                 break;
252
253         case PB_PROTOCOL_ACTION_CONFIG:
254                 config = talloc_zero(client, struct config);
255
256                 rc = pb_protocol_deserialise_config(config, message);
257                 if (rc) {
258                         pb_log("%s: no config?", __func__);
259                         return 0;
260                 }
261
262                 device_handler_update_config(client->server->device_handler,
263                                 config);
264                 break;
265
266         case PB_PROTOCOL_ACTION_ADD_URL:
267                 url = pb_protocol_deserialise_string((void *) client, message);
268
269                 device_handler_process_url(client->server->device_handler, url);
270                 break;
271
272         default:
273                 pb_log("%s: invalid action %d\n", __func__, message->action);
274                 return 0;
275         }
276
277
278         return 0;
279 }
280
281 static int discover_server_process_connection(void *arg)
282 {
283         struct discover_server *server = arg;
284         int fd, rc, i, n_devices;
285         struct client *client;
286
287         /* accept the incoming connection */
288         fd = accept(server->socket, NULL, 0);
289         if (fd < 0) {
290                 pb_log("accept: %s\n", strerror(errno));
291                 return 0;
292         }
293
294         /* add to our list of clients */
295         client = talloc_zero(server, struct client);
296         list_add(&server->clients, &client->list);
297
298         talloc_set_destructor(client, client_destructor);
299
300         client->fd = fd;
301         client->server = server;
302         client->waiter = waiter_register_io(server->waitset, client->fd,
303                                 WAIT_IN, discover_server_process_message,
304                                 client);
305
306         /* send sysinfo to client */
307         rc = write_system_info_message(server, client, system_info_get());
308         if (rc)
309                 return 0;
310
311         /* send config to client */
312         rc = write_config_message(server, client, config_get());
313         if (rc)
314                 return 0;
315
316         /* send existing devices to client */
317         n_devices = device_handler_get_device_count(server->device_handler);
318         for (i = 0; i < n_devices; i++) {
319                 const struct discover_boot_option *opt;
320                 const struct discover_device *device;
321
322                 device = device_handler_get_device(server->device_handler, i);
323                 rc = write_device_add_message(server, client, device->device);
324                 if (rc)
325                         return 0;
326
327                 list_for_each_entry(&device->boot_options, opt, list) {
328                         rc = write_boot_option_add_message(server, client,
329                                         opt->option);
330                         if (rc)
331                                 return 0;
332                 }
333         }
334
335         return 0;
336 }
337
338 void discover_server_notify_device_add(struct discover_server *server,
339                 struct device *device)
340 {
341         struct client *client;
342
343         list_for_each_entry(&server->clients, client, list)
344                 write_device_add_message(server, client, device);
345
346 }
347
348 void discover_server_notify_boot_option_add(struct discover_server *server,
349                 struct boot_option *boot_option)
350 {
351         struct client *client;
352
353         list_for_each_entry(&server->clients, client, list)
354                 write_boot_option_add_message(server, client, boot_option);
355 }
356
357 void discover_server_notify_device_remove(struct discover_server *server,
358                 struct device *device)
359 {
360         struct client *client;
361
362         list_for_each_entry(&server->clients, client, list)
363                 write_device_remove_message(server, client, device->id);
364
365 }
366
367 void discover_server_notify_boot_status(struct discover_server *server,
368                 struct boot_status *status)
369 {
370         struct client *client;
371
372         list_for_each_entry(&server->clients, client, list)
373                 write_boot_status_message(server, client, status);
374 }
375
376 void discover_server_notify_system_info(struct discover_server *server,
377                 const struct system_info *sysinfo)
378 {
379         struct client *client;
380
381         list_for_each_entry(&server->clients, client, list)
382                 write_system_info_message(server, client, sysinfo);
383 }
384
385 void discover_server_notify_config(struct discover_server *server,
386                 const struct config *config)
387 {
388         struct client *client;
389
390         list_for_each_entry(&server->clients, client, list)
391                 write_config_message(server, client, config);
392 }
393
394 void discover_server_set_device_source(struct discover_server *server,
395                 struct device_handler *handler)
396 {
397         server->device_handler = handler;
398 }
399
400 struct discover_server *discover_server_init(struct waitset *waitset)
401 {
402         struct discover_server *server;
403         struct sockaddr_un addr;
404
405         server = talloc(NULL, struct discover_server);
406         if (!server)
407                 return NULL;
408
409         server->waiter = NULL;
410         server->waitset = waitset;
411         list_init(&server->clients);
412
413         unlink(PB_SOCKET_PATH);
414
415         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
416         if (server->socket < 0) {
417                 pb_log("error creating server socket: %s\n", strerror(errno));
418                 goto out_err;
419         }
420
421         talloc_set_destructor(server, server_destructor);
422
423         addr.sun_family = AF_UNIX;
424         strcpy(addr.sun_path, PB_SOCKET_PATH);
425
426         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
427                 pb_log("error binding server socket: %s\n", strerror(errno));
428                 goto out_err;
429         }
430
431         if (listen(server->socket, 8)) {
432                 pb_log("server socket listen: %s\n", strerror(errno));
433                 goto out_err;
434         }
435
436         server->waiter = waiter_register_io(server->waitset, server->socket,
437                         WAIT_IN, discover_server_process_connection, server);
438
439         return server;
440
441 out_err:
442         talloc_free(server);
443         return NULL;
444 }
445
446 void discover_server_destroy(struct discover_server *server)
447 {
448         talloc_free(server);
449 }
450