discover: log unresolved boot options
[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 <talloc/talloc.h>
14 #include <waiter/waiter.h>
15 #include <log/log.h>
16
17 #include "pb-protocol/pb-protocol.h"
18 #include "list/list.h"
19
20 #include "device-handler.h"
21 #include "discover-server.h"
22
23 struct discover_server {
24         int socket;
25         struct waitset *waitset;
26         struct waiter *waiter;
27         struct list clients;
28         struct device_handler *device_handler;
29 };
30
31 struct client {
32         struct discover_server *server;
33         struct list_item list;
34         int fd;
35 };
36
37
38 static int server_destructor(void *arg)
39 {
40         struct discover_server *server = arg;
41
42         if (server->waiter)
43                 waiter_remove(server->waiter);
44
45         if (server->socket >= 0)
46                 close(server->socket);
47
48         return 0;
49 }
50
51 static int client_destructor(void *arg)
52 {
53         struct client *client = arg;
54
55         if (client->fd >= 0)
56                 close(client->fd);
57
58         list_remove(&client->list);
59
60         return 0;
61
62 }
63
64 static void print_clients(struct discover_server *server)
65         __attribute__((unused));
66
67 static void print_clients(struct discover_server *server)
68 {
69         struct client *client;
70
71         pb_log("current clients [%p,%p,%p]:\n",
72                         &server->clients.head,
73                         server->clients.head.prev,
74                         server->clients.head.next);
75         list_for_each_entry(&server->clients, client, list)
76                 pb_log("\t[%p,%p,%p] client: %d\n", &client->list,
77                                 client->list.prev, client->list.next,
78                                 client->fd);
79 }
80
81 static int client_write_message(
82                 struct discover_server *server __attribute__((unused)),
83                 struct client *client, struct pb_protocol_message *message)
84 {
85         int rc;
86
87         rc = pb_protocol_write_message(client->fd, message);
88         if (rc)
89                 talloc_free(client);
90
91         return rc;
92 }
93
94 static int write_device_add_message(struct discover_server *server,
95                 struct client *client, const struct device *dev)
96 {
97         struct pb_protocol_message *message;
98         int len;
99
100         len = pb_protocol_device_len(dev);
101
102         message = pb_protocol_create_message(client,
103                         PB_PROTOCOL_ACTION_DEVICE_ADD, len);
104         if (!message)
105                 return -1;
106
107         pb_protocol_serialise_device(dev, message->payload, len);
108
109         return client_write_message(server, client, message);
110 }
111
112 static int write_boot_option_add_message(struct discover_server *server,
113                 struct client *client, const struct boot_option *opt)
114 {
115         struct pb_protocol_message *message;
116         int len;
117
118         len = pb_protocol_boot_option_len(opt);
119
120         message = pb_protocol_create_message(client,
121                         PB_PROTOCOL_ACTION_BOOT_OPTION_ADD, len);
122         if (!message)
123                 return -1;
124
125         pb_protocol_serialise_boot_option(opt, message->payload, len);
126
127         return client_write_message(server, client, message);
128 }
129
130 static int write_device_remove_message(struct discover_server *server,
131                 struct client *client, char *dev_id)
132 {
133         struct pb_protocol_message *message;
134         int len;
135
136         len = strlen(dev_id) + sizeof(uint32_t);
137
138         message = pb_protocol_create_message(client,
139                         PB_PROTOCOL_ACTION_DEVICE_REMOVE, len);
140         if (!message)
141                 return -1;
142
143         pb_protocol_serialise_string(message->payload, dev_id);
144
145         return client_write_message(server, client, message);
146 }
147
148 static int write_boot_status_message(struct discover_server *server,
149                 struct client *client, const struct boot_status *status)
150 {
151         struct pb_protocol_message *message;
152         int len;
153
154         len = pb_protocol_boot_status_len(status);
155
156         message = pb_protocol_create_message(client,
157                         PB_PROTOCOL_ACTION_STATUS, len);
158         if (!message)
159                 return -1;
160
161         pb_protocol_serialise_boot_status(status, message->payload, len);
162
163         return client_write_message(server, client, message);
164 }
165
166 static int discover_server_process_message(void *arg)
167 {
168         struct pb_protocol_message *message;
169         struct boot_command *boot_command;
170         struct client *client = arg;
171         int rc;
172
173         message = pb_protocol_read_message(client, client->fd);
174
175         if (!message)
176                 return 0;
177
178         if (message->action != PB_PROTOCOL_ACTION_BOOT) {
179                 pb_log("%s: invalid action %d\n", __func__, message->action);
180                 return 0;
181         }
182
183         boot_command = talloc(client, struct boot_command);
184
185         rc = pb_protocol_deserialise_boot_command(boot_command, message);
186         if (rc) {
187                 pb_log("%s: no boot command?", __func__);
188                 return 0;
189         }
190
191         device_handler_boot(client->server->device_handler, boot_command);
192
193         return 0;
194 }
195
196 static int discover_server_process_connection(void *arg)
197 {
198         struct discover_server *server = arg;
199         struct client *client;
200         int fd, i, n_devices;
201
202         /* accept the incoming connection */
203         fd = accept(server->socket, NULL, 0);
204         if (!fd) {
205                 pb_log("accept: %s\n", strerror(errno));
206                 return 0;
207         }
208
209         /* add to our list of clients */
210         client = talloc(server, struct client);
211         list_add(&server->clients, &client->list);
212
213         talloc_set_destructor(client, client_destructor);
214
215         client->fd = fd;
216         client->server = server;
217
218         /* send existing devices to client */
219         n_devices = device_handler_get_device_count(server->device_handler);
220         for (i = 0; i < n_devices; i++) {
221                 const struct discover_boot_option *opt;
222                 const struct discover_device *device;
223
224                 device = device_handler_get_device(server->device_handler, i);
225                 write_device_add_message(server, client, device->device);
226
227                 list_for_each_entry(&device->boot_options, opt, list)
228                         discover_server_notify_boot_option_add(server,
229                                         opt->option);
230
231         }
232
233         waiter_register(server->waitset, client->fd, WAIT_IN,
234                         discover_server_process_message, client);
235
236         return 0;
237 }
238
239 void discover_server_notify_device_add(struct discover_server *server,
240                 struct device *device)
241 {
242         struct client *client;
243
244         list_for_each_entry(&server->clients, client, list)
245                 write_device_add_message(server, client, device);
246
247 }
248
249 void discover_server_notify_boot_option_add(struct discover_server *server,
250                 struct boot_option *boot_option)
251 {
252         struct client *client;
253
254         list_for_each_entry(&server->clients, client, list)
255                 write_boot_option_add_message(server, client, boot_option);
256 }
257
258 void discover_server_notify_device_remove(struct discover_server *server,
259                 struct device *device)
260 {
261         struct client *client;
262
263         list_for_each_entry(&server->clients, client, list)
264                 write_device_remove_message(server, client, device->id);
265
266 }
267
268 void discover_server_notify_boot_status(struct discover_server *server,
269                 struct boot_status *status)
270 {
271         struct client *client;
272
273         list_for_each_entry(&server->clients, client, list)
274                 write_boot_status_message(server, client, status);
275 }
276
277 void discover_server_set_device_source(struct discover_server *server,
278                 struct device_handler *handler)
279 {
280         server->device_handler = handler;
281 }
282
283 struct discover_server *discover_server_init(struct waitset *waitset)
284 {
285         struct discover_server *server;
286         struct sockaddr_un addr;
287
288         server = talloc(NULL, struct discover_server);
289         if (!server)
290                 return NULL;
291
292         server->waiter = NULL;
293         server->waitset = waitset;
294         list_init(&server->clients);
295
296         unlink(PB_SOCKET_PATH);
297
298         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
299         if (server->socket < 0) {
300                 pb_log("error creating server socket: %s\n", strerror(errno));
301                 goto out_err;
302         }
303
304         talloc_set_destructor(server, server_destructor);
305
306         addr.sun_family = AF_UNIX;
307         strcpy(addr.sun_path, PB_SOCKET_PATH);
308
309         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
310                 pb_log("error binding server socket: %s\n", strerror(errno));
311                 goto out_err;
312         }
313
314         if (listen(server->socket, 8)) {
315                 pb_log("server socket listen: %s\n", strerror(errno));
316                 goto out_err;
317         }
318
319         server->waiter = waiter_register(server->waitset, server->socket,
320                         WAIT_IN, discover_server_process_connection, server);
321
322         return server;
323
324 out_err:
325         talloc_free(server);
326         return NULL;
327 }
328
329 void discover_server_destroy(struct discover_server *server)
330 {
331         talloc_free(server);
332 }
333