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