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