discover: parse boot message from incoming ACTION_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         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_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_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_remove_message(struct discover_server *server,
113                 struct client *client, char *dev_id)
114 {
115         struct pb_protocol_message *message;
116         int len;
117
118         len = strlen(dev_id) + sizeof(uint32_t);
119
120         message = pb_protocol_create_message(client,
121                         PB_PROTOCOL_ACTION_REMOVE, len);
122         if (!message)
123                 return -1;
124
125         pb_protocol_serialise_string(message->payload, dev_id);
126
127         return client_write_message(server, client, message);
128 }
129
130 static int discover_server_process_message(void *arg)
131 {
132         struct pb_protocol_message *message;
133         struct boot_command *boot_command;
134         struct client *client = arg;
135
136         message = pb_protocol_read_message(client, client->fd);
137
138         if (!message)
139                 return 0;
140
141         if (message->action != PB_PROTOCOL_ACTION_BOOT) {
142                 pb_log("%s: invalid action %d\n", __func__, message->action);
143                 return 0;
144         }
145
146         boot_command = pb_protocol_deserialise_boot_command(client, message);
147         if (!boot_command) {
148                 pb_log("%s: no boot command?", __func__);
149                 return 0;
150         }
151
152         /* todo: pass boot_command to client->server->device_handler */
153
154         return 0;
155 }
156
157 static int discover_server_process_connection(void *arg)
158 {
159         struct discover_server *server = arg;
160         struct client *client;
161         int fd, i, n_devices;
162
163         /* accept the incoming connection */
164         fd = accept(server->socket, NULL, 0);
165         if (!fd) {
166                 pb_log("accept: %s\n", strerror(errno));
167                 return 0;
168         }
169
170         /* add to our list of clients */
171         client = talloc(server, struct client);
172         list_add(&server->clients, &client->list);
173
174         talloc_set_destructor(client, client_destructor);
175
176         client->fd = fd;
177         client->server = server;
178
179         /* send existing devices to client */
180         n_devices = device_handler_get_device_count(server->device_handler);
181         for (i = 0; i < n_devices; i++) {
182                 const struct device *device;
183
184                 device = device_handler_get_device(server->device_handler, i);
185                 write_add_message(server, client, device);
186         }
187
188         waiter_register(server->waitset, client->fd, WAIT_IN,
189                         discover_server_process_message, client);
190
191         return 0;
192 }
193
194 void discover_server_notify_add(struct discover_server *server,
195                 struct device *device)
196 {
197         struct client *client;
198
199         list_for_each_entry(&server->clients, client, list)
200                 write_add_message(server, client, device);
201
202 }
203
204 void discover_server_notify_remove(struct discover_server *server,
205                 struct device *device)
206 {
207         struct client *client;
208
209         list_for_each_entry(&server->clients, client, list)
210                 write_remove_message(server, client, device->id);
211
212 }
213
214 void discover_server_set_device_source(struct discover_server *server,
215                 struct device_handler *handler)
216 {
217         server->device_handler = handler;
218 }
219
220 struct discover_server *discover_server_init(struct waitset *waitset)
221 {
222         struct discover_server *server;
223         struct sockaddr_un addr;
224
225         server = talloc(NULL, struct discover_server);
226         if (!server)
227                 return NULL;
228
229         server->waiter = NULL;
230         server->waitset = waitset;
231         list_init(&server->clients);
232
233         unlink(PB_SOCKET_PATH);
234
235         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
236         if (server->socket < 0) {
237                 pb_log("error creating server socket: %s\n", strerror(errno));
238                 goto out_err;
239         }
240
241         talloc_set_destructor(server, server_destructor);
242
243         addr.sun_family = AF_UNIX;
244         strcpy(addr.sun_path, PB_SOCKET_PATH);
245
246         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
247                 pb_log("error binding server socket: %s\n", strerror(errno));
248                 goto out_err;
249         }
250
251         if (listen(server->socket, 8)) {
252                 pb_log("server socket listen: %s\n", strerror(errno));
253                 goto out_err;
254         }
255
256         server->waiter = waiter_register(server->waitset, server->socket,
257                         WAIT_IN, discover_server_process_connection, server);
258
259         return server;
260
261 out_err:
262         talloc_free(server);
263         return NULL;
264 }
265
266 void discover_server_destroy(struct discover_server *server)
267 {
268         talloc_free(server);
269 }
270