discover: Allow discover server to receive 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 list_item list;
33         int fd;
34 };
35
36
37 static int server_destructor(void *arg)
38 {
39         struct discover_server *server = arg;
40
41         if (server->waiter)
42                 waiter_remove(server->waiter);
43
44         if (server->socket >= 0)
45                 close(server->socket);
46
47         return 0;
48 }
49
50 static int client_destructor(void *arg)
51 {
52         struct client *client = arg;
53
54         if (client->fd >= 0)
55                 close(client->fd);
56
57         list_remove(&client->list);
58
59         return 0;
60
61 }
62
63 static void print_clients(struct discover_server *server)
64         __attribute__((unused));
65
66 static void print_clients(struct discover_server *server)
67 {
68         struct client *client;
69
70         pb_log("current clients [%p,%p,%p]:\n",
71                         &server->clients.head,
72                         server->clients.head.prev,
73                         server->clients.head.next);
74         list_for_each_entry(&server->clients, client, list)
75                 pb_log("\t[%p,%p,%p] client: %d\n", &client->list,
76                                 client->list.prev, client->list.next,
77                                 client->fd);
78 }
79
80 static int client_write_message(
81                 struct discover_server *server __attribute__((unused)),
82                 struct client *client, struct pb_protocol_message *message)
83 {
84         int rc;
85
86         rc = pb_protocol_write_message(client->fd, message);
87         if (rc)
88                 talloc_free(client);
89
90         return rc;
91 }
92
93 static int write_add_message(struct discover_server *server,
94                 struct client *client, const struct device *dev)
95 {
96         struct pb_protocol_message *message;
97         int len;
98
99         len = pb_protocol_device_len(dev);
100
101         message = pb_protocol_create_message(client,
102                         PB_PROTOCOL_ACTION_ADD, len);
103         if (!message)
104                 return -1;
105
106         pb_protocol_serialise_device(dev, message->payload, len);
107
108         return client_write_message(server, client, message);
109 }
110
111 static int write_remove_message(struct discover_server *server,
112                 struct client *client, char *dev_id)
113 {
114         struct pb_protocol_message *message;
115         int len;
116
117         len = strlen(dev_id) + sizeof(uint32_t);
118
119         message = pb_protocol_create_message(client,
120                         PB_PROTOCOL_ACTION_REMOVE, len);
121         if (!message)
122                 return -1;
123
124         pb_protocol_serialise_string(message->payload, dev_id);
125
126         return client_write_message(server, client, message);
127 }
128
129 static int discover_server_process_message(void *arg)
130 {
131         struct pb_protocol_message *message;
132         struct client *client = arg;
133
134         message = pb_protocol_read_message(client, client->fd);
135
136         if (!message)
137                 return 0;
138
139         if (message->action != PB_PROTOCOL_ACTION_BOOT) {
140                 pb_log("%s: invalid action %d\n", __func__, message->action);
141                 return 0;
142         }
143
144         /* todo: process boot message */
145         return 0;
146 }
147
148 static int discover_server_process_connection(void *arg)
149 {
150         struct discover_server *server = arg;
151         struct client *client;
152         int fd, i, n_devices;
153
154         /* accept the incoming connection */
155         fd = accept(server->socket, NULL, 0);
156         if (!fd) {
157                 pb_log("accept: %s\n", strerror(errno));
158                 return 0;
159         }
160
161         /* add to our list of clients */
162         client = talloc(server, struct client);
163         list_add(&server->clients, &client->list);
164
165         talloc_set_destructor(client, client_destructor);
166
167         client->fd = fd;
168
169         /* send existing devices to client */
170         n_devices = device_handler_get_device_count(server->device_handler);
171         for (i = 0; i < n_devices; i++) {
172                 const struct device *device;
173
174                 device = device_handler_get_device(server->device_handler, i);
175                 write_add_message(server, client, device);
176         }
177
178         waiter_register(server->waitset, client->fd, WAIT_IN,
179                         discover_server_process_message, client);
180
181         return 0;
182 }
183
184 void discover_server_notify_add(struct discover_server *server,
185                 struct device *device)
186 {
187         struct client *client;
188
189         list_for_each_entry(&server->clients, client, list)
190                 write_add_message(server, client, device);
191
192 }
193
194 void discover_server_notify_remove(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_remove_message(server, client, device->id);
201
202 }
203
204 void discover_server_set_device_source(struct discover_server *server,
205                 struct device_handler *handler)
206 {
207         server->device_handler = handler;
208 }
209
210 struct discover_server *discover_server_init(struct waitset *waitset)
211 {
212         struct discover_server *server;
213         struct sockaddr_un addr;
214
215         server = talloc(NULL, struct discover_server);
216         if (!server)
217                 return NULL;
218
219         server->waiter = NULL;
220         server->waitset = waitset;
221         list_init(&server->clients);
222
223         unlink(PB_SOCKET_PATH);
224
225         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
226         if (server->socket < 0) {
227                 pb_log("error creating server socket: %s\n", strerror(errno));
228                 goto out_err;
229         }
230
231         talloc_set_destructor(server, server_destructor);
232
233         addr.sun_family = AF_UNIX;
234         strcpy(addr.sun_path, PB_SOCKET_PATH);
235
236         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
237                 pb_log("error binding server socket: %s\n", strerror(errno));
238                 goto out_err;
239         }
240
241         if (listen(server->socket, 8)) {
242                 pb_log("server socket listen: %s\n", strerror(errno));
243                 goto out_err;
244         }
245
246         server->waiter = waiter_register(server->waitset, server->socket,
247                         WAIT_IN, discover_server_process_connection, server);
248
249         return server;
250
251 out_err:
252         talloc_free(server);
253         return NULL;
254 }
255
256 void discover_server_destroy(struct discover_server *server)
257 {
258         talloc_free(server);
259 }
260