discover/grub2: Add default option parsing
[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         if (message->action != PB_PROTOCOL_ACTION_BOOT) {
186                 pb_log("%s: invalid action %d\n", __func__, message->action);
187                 return 0;
188         }
189
190         boot_command = talloc(client, struct boot_command);
191
192         rc = pb_protocol_deserialise_boot_command(boot_command, message);
193         if (rc) {
194                 pb_log("%s: no boot command?", __func__);
195                 return 0;
196         }
197
198         device_handler_boot(client->server->device_handler, boot_command);
199
200         return 0;
201 }
202
203 static int discover_server_process_connection(void *arg)
204 {
205         struct discover_server *server = arg;
206         int fd, rc, i, n_devices;
207         struct client *client;
208
209         /* accept the incoming connection */
210         fd = accept(server->socket, NULL, 0);
211         if (fd < 0) {
212                 pb_log("accept: %s\n", strerror(errno));
213                 return 0;
214         }
215
216         /* add to our list of clients */
217         client = talloc_zero(server, struct client);
218         list_add(&server->clients, &client->list);
219
220         talloc_set_destructor(client, client_destructor);
221
222         client->fd = fd;
223         client->server = server;
224
225         /* send existing devices to client */
226         n_devices = device_handler_get_device_count(server->device_handler);
227         for (i = 0; i < n_devices; i++) {
228                 const struct discover_boot_option *opt;
229                 const struct discover_device *device;
230
231                 device = device_handler_get_device(server->device_handler, i);
232                 rc = write_device_add_message(server, client, device->device);
233                 if (rc)
234                         return 0;
235
236                 list_for_each_entry(&device->boot_options, opt, list) {
237                         rc = write_boot_option_add_message(server, client,
238                                         opt->option);
239                         if (rc)
240                                 return 0;
241                 }
242         }
243
244         client->waiter = waiter_register(server->waitset, client->fd, WAIT_IN,
245                                 discover_server_process_message, client);
246
247         return 0;
248 }
249
250 void discover_server_notify_device_add(struct discover_server *server,
251                 struct device *device)
252 {
253         struct client *client;
254
255         list_for_each_entry(&server->clients, client, list)
256                 write_device_add_message(server, client, device);
257
258 }
259
260 void discover_server_notify_boot_option_add(struct discover_server *server,
261                 struct boot_option *boot_option)
262 {
263         struct client *client;
264
265         list_for_each_entry(&server->clients, client, list)
266                 write_boot_option_add_message(server, client, boot_option);
267 }
268
269 void discover_server_notify_device_remove(struct discover_server *server,
270                 struct device *device)
271 {
272         struct client *client;
273
274         list_for_each_entry(&server->clients, client, list)
275                 write_device_remove_message(server, client, device->id);
276
277 }
278
279 void discover_server_notify_boot_status(struct discover_server *server,
280                 struct boot_status *status)
281 {
282         struct client *client;
283
284         list_for_each_entry(&server->clients, client, list)
285                 write_boot_status_message(server, client, status);
286 }
287
288 void discover_server_set_device_source(struct discover_server *server,
289                 struct device_handler *handler)
290 {
291         server->device_handler = handler;
292 }
293
294 struct discover_server *discover_server_init(struct waitset *waitset)
295 {
296         struct discover_server *server;
297         struct sockaddr_un addr;
298
299         server = talloc(NULL, struct discover_server);
300         if (!server)
301                 return NULL;
302
303         server->waiter = NULL;
304         server->waitset = waitset;
305         list_init(&server->clients);
306
307         unlink(PB_SOCKET_PATH);
308
309         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
310         if (server->socket < 0) {
311                 pb_log("error creating server socket: %s\n", strerror(errno));
312                 goto out_err;
313         }
314
315         talloc_set_destructor(server, server_destructor);
316
317         addr.sun_family = AF_UNIX;
318         strcpy(addr.sun_path, PB_SOCKET_PATH);
319
320         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
321                 pb_log("error binding server socket: %s\n", strerror(errno));
322                 goto out_err;
323         }
324
325         if (listen(server->socket, 8)) {
326                 pb_log("server socket listen: %s\n", strerror(errno));
327                 goto out_err;
328         }
329
330         server->waiter = waiter_register(server->waitset, server->socket,
331                         WAIT_IN, discover_server_process_connection, server);
332
333         return server;
334
335 out_err:
336         talloc_free(server);
337         return NULL;
338 }
339
340 void discover_server_destroy(struct discover_server *server)
341 {
342         talloc_free(server);
343 }
344