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