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