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