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