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