waiter: Don't rely on global variables to keep waiter state
[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(void *arg)
130 {
131         struct discover_server *server = arg;
132         struct client *client;
133         int fd, i, n_devices;
134
135         /* accept the incoming connection */
136         fd = accept(server->socket, NULL, 0);
137         if (!fd) {
138                 pb_log("accept: %s\n", strerror(errno));
139                 return 0;
140         }
141
142         /* add to our list of clients */
143         client = talloc(server, struct client);
144         list_add(&server->clients, &client->list);
145
146         talloc_set_destructor(client, client_destructor);
147
148         client->fd = fd;
149
150         /* send existing devices to client */
151         n_devices = device_handler_get_device_count(server->device_handler);
152         for (i = 0; i < n_devices; i++) {
153                 const struct device *device;
154
155                 device = device_handler_get_device(server->device_handler, i);
156                 write_add_message(server, client, device);
157         }
158
159         return 0;
160 }
161
162 void discover_server_notify_add(struct discover_server *server,
163                 struct device *device)
164 {
165         struct client *client;
166
167         list_for_each_entry(&server->clients, client, list)
168                 write_add_message(server, client, device);
169
170 }
171
172 void discover_server_notify_remove(struct discover_server *server,
173                 struct device *device)
174 {
175         struct client *client;
176
177         list_for_each_entry(&server->clients, client, list)
178                 write_remove_message(server, client, device->id);
179
180 }
181
182 void discover_server_set_device_source(struct discover_server *server,
183                 struct device_handler *handler)
184 {
185         server->device_handler = handler;
186 }
187
188 struct discover_server *discover_server_init(struct waitset *waitset)
189 {
190         struct discover_server *server;
191         struct sockaddr_un addr;
192
193         server = talloc(NULL, struct discover_server);
194         if (!server)
195                 return NULL;
196
197         server->waiter = NULL;
198         server->waitset = waitset;
199         list_init(&server->clients);
200
201         unlink(PB_SOCKET_PATH);
202
203         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
204         if (server->socket < 0) {
205                 pb_log("error creating server socket: %s\n", strerror(errno));
206                 goto out_err;
207         }
208
209         talloc_set_destructor(server, server_destructor);
210
211         addr.sun_family = AF_UNIX;
212         strcpy(addr.sun_path, PB_SOCKET_PATH);
213
214         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
215                 pb_log("error binding server socket: %s\n", strerror(errno));
216                 goto out_err;
217         }
218
219         if (listen(server->socket, 8)) {
220                 pb_log("server socket listen: %s\n", strerror(errno));
221                 goto out_err;
222         }
223
224         server->waiter = waiter_register(server->waitset, server->socket,
225                         WAIT_IN, discover_server_process, server);
226
227         return server;
228
229 out_err:
230         talloc_free(server);
231         return NULL;
232 }
233
234 void discover_server_destroy(struct discover_server *server)
235 {
236         talloc_free(server);
237 }
238