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