8d7283811eb0af721404f85c90a7891cb522a157
[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
15 #include "pb-protocol/pb-protocol.h"
16 #include "list/list.h"
17
18 #include "log.h"
19 #include "waiter.h"
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         printf("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                 printf("\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(struct discover_server *server,
80                 struct client *client, struct pb_protocol_message *message)
81 {
82         int rc;
83
84         rc = pb_protocol_write_message(client->fd, message);
85         if (rc)
86                 talloc_free(client);
87
88         return rc;
89 }
90
91 static int write_add_message(struct discover_server *server,
92                 struct client *client, struct device *dev)
93 {
94         struct pb_protocol_message *message;
95         int len;
96
97         len = pb_protocol_device_len(dev);
98
99         message = pb_protocol_create_message(client,
100                         PB_PROTOCOL_ACTION_ADD, len);
101         if (!message)
102                 return -1;
103
104         pb_protocol_serialise_device(dev, message->payload, len);
105
106         return client_write_message(server, client, message);
107 }
108
109 static int write_remove_message(struct discover_server *server,
110                 struct client *client, char *dev_id)
111 {
112         struct pb_protocol_message *message;
113         int len;
114
115         len = strlen(dev_id) + sizeof(uint32_t);
116
117         message = pb_protocol_create_message(client,
118                         PB_PROTOCOL_ACTION_REMOVE, len);
119         if (!message)
120                 return -1;
121
122         pb_protocol_serialise_string(message->payload, dev_id);
123
124         return client_write_message(server, client, message);
125 }
126
127 static int discover_server_process(void *arg)
128 {
129         struct discover_server *server = arg;
130         struct device *devices;
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_current_devices(server->device_handler,
151                         &devices);
152         for (i = 0; i < n_devices; i++)
153                 write_add_message(server, client, &devices[i]);
154
155         return 0;
156 }
157
158 void discover_server_notify_add(struct discover_server *server,
159                 struct device *device)
160 {
161         struct client *client;
162
163         list_for_each_entry(&server->clients, client, list)
164                 write_add_message(server, client, device);
165
166 }
167
168 void discover_server_notify_remove(struct discover_server *server,
169                 struct device *device)
170 {
171         struct client *client;
172
173         list_for_each_entry(&server->clients, client, list)
174                 write_remove_message(server, client, device->id);
175
176 }
177
178 void discover_server_set_device_source(struct discover_server *server,
179                 struct device_handler *handler)
180 {
181         server->device_handler = handler;
182 }
183
184 struct discover_server *discover_server_init(void)
185 {
186         struct discover_server *server;
187         struct sockaddr_un addr;
188
189         server = talloc(NULL, struct discover_server);
190         if (!server)
191                 return NULL;
192
193         server->waiter = NULL;
194         list_init(&server->clients);
195
196         unlink(PB_SOCKET_PATH);
197
198         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
199         if (server->socket < 0) {
200                 pb_log("error creating server socket: %s\n", strerror(errno));
201                 goto out_err;
202         }
203
204         talloc_set_destructor(server, server_destructor);
205
206         addr.sun_family = AF_UNIX;
207         strcpy(addr.sun_path, PB_SOCKET_PATH);
208
209         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
210                 pb_log("error binding server socket: %s\n", strerror(errno));
211                 goto out_err;
212         }
213
214         if (listen(server->socket, 8)) {
215                 pb_log("server socket listen: %s\n", strerror(errno));
216                 goto out_err;
217         }
218
219         server->waiter = waiter_register(server->socket, WAIT_IN,
220                         discover_server_process, server);
221
222         return server;
223
224 out_err:
225         talloc_free(server);
226         return NULL;
227 }
228
229 void discover_server_destroy(struct discover_server *server)
230 {
231         talloc_free(server);
232 }
233