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