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