Move log to library
[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         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(
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, 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 device *devices;
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_current_devices(server->device_handler,
152                         &devices);
153         for (i = 0; i < n_devices; i++)
154                 write_add_message(server, client, &devices[i]);
155
156         return 0;
157 }
158
159 void discover_server_notify_add(struct discover_server *server,
160                 struct device *device)
161 {
162         struct client *client;
163
164         list_for_each_entry(&server->clients, client, list)
165                 write_add_message(server, client, device);
166
167 }
168
169 void discover_server_notify_remove(struct discover_server *server,
170                 struct device *device)
171 {
172         struct client *client;
173
174         list_for_each_entry(&server->clients, client, list)
175                 write_remove_message(server, client, device->id);
176
177 }
178
179 void discover_server_set_device_source(struct discover_server *server,
180                 struct device_handler *handler)
181 {
182         server->device_handler = handler;
183 }
184
185 struct discover_server *discover_server_init(void)
186 {
187         struct discover_server *server;
188         struct sockaddr_un addr;
189
190         server = talloc(NULL, struct discover_server);
191         if (!server)
192                 return NULL;
193
194         server->waiter = NULL;
195         list_init(&server->clients);
196
197         unlink(PB_SOCKET_PATH);
198
199         server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
200         if (server->socket < 0) {
201                 pb_log("error creating server socket: %s\n", strerror(errno));
202                 goto out_err;
203         }
204
205         talloc_set_destructor(server, server_destructor);
206
207         addr.sun_family = AF_UNIX;
208         strcpy(addr.sun_path, PB_SOCKET_PATH);
209
210         if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
211                 pb_log("error binding server socket: %s\n", strerror(errno));
212                 goto out_err;
213         }
214
215         if (listen(server->socket, 8)) {
216                 pb_log("server socket listen: %s\n", strerror(errno));
217                 goto out_err;
218         }
219
220         server->waiter = waiter_register(server->socket, WAIT_IN,
221                         discover_server_process, server);
222
223         return server;
224
225 out_err:
226         talloc_free(server);
227         return NULL;
228 }
229
230 void discover_server_destroy(struct discover_server *server)
231 {
232         talloc_free(server);
233 }
234