9 #include <sys/socket.h>
11 #include <asm/byteorder.h>
13 #include <pb-config/pb-config.h>
14 #include <talloc/talloc.h>
15 #include <waiter/waiter.h>
18 #include "pb-protocol/pb-protocol.h"
19 #include "list/list.h"
21 #include "device-handler.h"
22 #include "discover-server.h"
25 struct discover_server {
27 struct waitset *waitset;
28 struct waiter *waiter;
30 struct device_handler *device_handler;
34 struct discover_server *server;
35 struct list_item list;
36 struct waiter *waiter;
42 static int server_destructor(void *arg)
44 struct discover_server *server = arg;
47 waiter_remove(server->waiter);
49 if (server->socket >= 0)
50 close(server->socket);
55 static int client_destructor(void *arg)
57 struct client *client = arg;
63 waiter_remove(client->waiter);
65 list_remove(&client->list);
71 static void print_clients(struct discover_server *server)
72 __attribute__((unused));
74 static void print_clients(struct discover_server *server)
76 struct client *client;
78 pb_debug("current clients [%p,%p,%p]:\n",
79 &server->clients.head,
80 server->clients.head.prev,
81 server->clients.head.next);
82 list_for_each_entry(&server->clients, client, list)
83 pb_debug("\t[%p,%p,%p] client: %d\n", &client->list,
84 client->list.prev, client->list.next,
88 static int client_write_message(
89 struct discover_server *server __attribute__((unused)),
90 struct client *client, struct pb_protocol_message *message)
94 if (client->remote_closed)
97 rc = pb_protocol_write_message(client->fd, message);
99 client->remote_closed = true;
104 static int write_device_add_message(struct discover_server *server,
105 struct client *client, const struct device *dev)
107 struct pb_protocol_message *message;
110 len = pb_protocol_device_len(dev);
112 message = pb_protocol_create_message(client,
113 PB_PROTOCOL_ACTION_DEVICE_ADD, len);
117 pb_protocol_serialise_device(dev, message->payload, len);
119 return client_write_message(server, client, message);
122 static int write_boot_option_add_message(struct discover_server *server,
123 struct client *client, const struct boot_option *opt)
125 struct pb_protocol_message *message;
128 len = pb_protocol_boot_option_len(opt);
130 message = pb_protocol_create_message(client,
131 PB_PROTOCOL_ACTION_BOOT_OPTION_ADD, len);
135 pb_protocol_serialise_boot_option(opt, message->payload, len);
137 return client_write_message(server, client, message);
140 static int write_device_remove_message(struct discover_server *server,
141 struct client *client, char *dev_id)
143 struct pb_protocol_message *message;
146 len = strlen(dev_id) + sizeof(uint32_t);
148 message = pb_protocol_create_message(client,
149 PB_PROTOCOL_ACTION_DEVICE_REMOVE, len);
153 pb_protocol_serialise_string(message->payload, dev_id);
155 return client_write_message(server, client, message);
158 static int write_boot_status_message(struct discover_server *server,
159 struct client *client, const struct boot_status *status)
161 struct pb_protocol_message *message;
164 len = pb_protocol_boot_status_len(status);
166 message = pb_protocol_create_message(client,
167 PB_PROTOCOL_ACTION_STATUS, len);
171 pb_protocol_serialise_boot_status(status, message->payload, len);
173 return client_write_message(server, client, message);
176 static int write_system_info_message(struct discover_server *server,
177 struct client *client, const struct system_info *sysinfo)
179 struct pb_protocol_message *message;
182 len = pb_protocol_system_info_len(sysinfo);
184 message = pb_protocol_create_message(client,
185 PB_PROTOCOL_ACTION_SYSTEM_INFO, len);
189 pb_protocol_serialise_system_info(sysinfo, message->payload, len);
191 return client_write_message(server, client, message);
194 static int write_config_message(struct discover_server *server,
195 struct client *client, const struct config *config)
197 struct pb_protocol_message *message;
200 len = pb_protocol_config_len(config);
202 message = pb_protocol_create_message(client,
203 PB_PROTOCOL_ACTION_CONFIG, len);
207 pb_protocol_serialise_config(config, message->payload, len);
209 return client_write_message(server, client, message);
212 static int discover_server_process_message(void *arg)
214 struct pb_protocol_message *message;
215 struct boot_command *boot_command;
216 struct client *client = arg;
219 message = pb_protocol_read_message(client, client->fd);
227 switch (message->action) {
228 case PB_PROTOCOL_ACTION_BOOT:
229 boot_command = talloc(client, struct boot_command);
231 rc = pb_protocol_deserialise_boot_command(boot_command,
234 pb_log("%s: no boot command?", __func__);
238 device_handler_boot(client->server->device_handler,
242 case PB_PROTOCOL_ACTION_CANCEL_DEFAULT:
243 device_handler_cancel_default(client->server->device_handler);
247 pb_log("%s: invalid action %d\n", __func__, message->action);
255 static int discover_server_process_connection(void *arg)
257 struct discover_server *server = arg;
258 int fd, rc, i, n_devices;
259 struct client *client;
261 /* accept the incoming connection */
262 fd = accept(server->socket, NULL, 0);
264 pb_log("accept: %s\n", strerror(errno));
268 /* add to our list of clients */
269 client = talloc_zero(server, struct client);
270 list_add(&server->clients, &client->list);
272 talloc_set_destructor(client, client_destructor);
275 client->server = server;
276 client->waiter = waiter_register_io(server->waitset, client->fd,
277 WAIT_IN, discover_server_process_message,
280 /* send sysinfo to client */
281 rc = write_system_info_message(server, client, system_info_get());
285 /* send config to client */
286 rc = write_config_message(server, client, config_get());
290 /* send existing devices to client */
291 n_devices = device_handler_get_device_count(server->device_handler);
292 for (i = 0; i < n_devices; i++) {
293 const struct discover_boot_option *opt;
294 const struct discover_device *device;
296 device = device_handler_get_device(server->device_handler, i);
297 rc = write_device_add_message(server, client, device->device);
301 list_for_each_entry(&device->boot_options, opt, list) {
302 rc = write_boot_option_add_message(server, client,
312 void discover_server_notify_device_add(struct discover_server *server,
313 struct device *device)
315 struct client *client;
317 list_for_each_entry(&server->clients, client, list)
318 write_device_add_message(server, client, device);
322 void discover_server_notify_boot_option_add(struct discover_server *server,
323 struct boot_option *boot_option)
325 struct client *client;
327 list_for_each_entry(&server->clients, client, list)
328 write_boot_option_add_message(server, client, boot_option);
331 void discover_server_notify_device_remove(struct discover_server *server,
332 struct device *device)
334 struct client *client;
336 list_for_each_entry(&server->clients, client, list)
337 write_device_remove_message(server, client, device->id);
341 void discover_server_notify_boot_status(struct discover_server *server,
342 struct boot_status *status)
344 struct client *client;
346 list_for_each_entry(&server->clients, client, list)
347 write_boot_status_message(server, client, status);
350 void discover_server_notify_system_info(struct discover_server *server,
351 const struct system_info *sysinfo)
353 struct client *client;
355 list_for_each_entry(&server->clients, client, list)
356 write_system_info_message(server, client, sysinfo);
359 void discover_server_notify_config(struct discover_server *server,
360 const struct config *config)
362 struct client *client;
364 list_for_each_entry(&server->clients, client, list)
365 write_config_message(server, client, config);
368 void discover_server_set_device_source(struct discover_server *server,
369 struct device_handler *handler)
371 server->device_handler = handler;
374 struct discover_server *discover_server_init(struct waitset *waitset)
376 struct discover_server *server;
377 struct sockaddr_un addr;
379 server = talloc(NULL, struct discover_server);
383 server->waiter = NULL;
384 server->waitset = waitset;
385 list_init(&server->clients);
387 unlink(PB_SOCKET_PATH);
389 server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
390 if (server->socket < 0) {
391 pb_log("error creating server socket: %s\n", strerror(errno));
395 talloc_set_destructor(server, server_destructor);
397 addr.sun_family = AF_UNIX;
398 strcpy(addr.sun_path, PB_SOCKET_PATH);
400 if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
401 pb_log("error binding server socket: %s\n", strerror(errno));
405 if (listen(server->socket, 8)) {
406 pb_log("server socket listen: %s\n", strerror(errno));
410 server->waiter = waiter_register_io(server->waitset, server->socket,
411 WAIT_IN, discover_server_process_connection, server);
420 void discover_server_destroy(struct discover_server *server)