]> git.ozlabs.org Git - petitboot/blobdiff - discover/discover-server.c
discover/resource: create_url_resource should take ownership of url
[petitboot] / discover / discover-server.c
index 3f08152601a05640b1b72996cc5575d549518e89..29816ee6a3614d4ad4500b2b7d31307e43fb19dd 100644 (file)
@@ -31,6 +31,7 @@ struct discover_server {
 struct client {
        struct discover_server *server;
        struct list_item list;
+       struct waiter *waiter;
        int fd;
 };
 
@@ -55,6 +56,9 @@ static int client_destructor(void *arg)
        if (client->fd >= 0)
                close(client->fd);
 
+       if (client->waiter)
+               waiter_remove(client->waiter);
+
        list_remove(&client->list);
 
        return 0;
@@ -172,23 +176,36 @@ static int discover_server_process_message(void *arg)
 
        message = pb_protocol_read_message(client, client->fd);
 
-       if (!message)
-               return 0;
-
-       if (message->action != PB_PROTOCOL_ACTION_BOOT) {
-               pb_log("%s: invalid action %d\n", __func__, message->action);
+       if (!message) {
+               talloc_free(client);
                return 0;
        }
 
-       boot_command = talloc(client, struct boot_command);
 
-       rc = pb_protocol_deserialise_boot_command(boot_command, message);
-       if (rc) {
-               pb_log("%s: no boot command?", __func__);
+       switch (message->action) {
+       case PB_PROTOCOL_ACTION_BOOT:
+               boot_command = talloc(client, struct boot_command);
+
+               rc = pb_protocol_deserialise_boot_command(boot_command,
+                               message);
+               if (rc) {
+                       pb_log("%s: no boot command?", __func__);
+                       return 0;
+               }
+
+               device_handler_boot(client->server->device_handler,
+                               boot_command);
+               break;
+
+       case PB_PROTOCOL_ACTION_CANCEL_DEFAULT:
+               device_handler_cancel_default(client->server->device_handler);
+               break;
+
+       default:
+               pb_log("%s: invalid action %d\n", __func__, message->action);
                return 0;
        }
 
-       device_handler_boot(client->server->device_handler, boot_command);
 
        return 0;
 }
@@ -196,18 +213,18 @@ static int discover_server_process_message(void *arg)
 static int discover_server_process_connection(void *arg)
 {
        struct discover_server *server = arg;
+       int fd, rc, i, n_devices;
        struct client *client;
-       int fd, i, n_devices;
 
        /* accept the incoming connection */
        fd = accept(server->socket, NULL, 0);
-       if (!fd) {
+       if (fd < 0) {
                pb_log("accept: %s\n", strerror(errno));
                return 0;
        }
 
        /* add to our list of clients */
-       client = talloc(server, struct client);
+       client = talloc_zero(server, struct client);
        list_add(&server->clients, &client->list);
 
        talloc_set_destructor(client, client_destructor);
@@ -222,16 +239,21 @@ static int discover_server_process_connection(void *arg)
                const struct discover_device *device;
 
                device = device_handler_get_device(server->device_handler, i);
-               write_device_add_message(server, client, device->device);
+               rc = write_device_add_message(server, client, device->device);
+               if (rc)
+                       return 0;
 
-               list_for_each_entry(&device->boot_options, opt, list)
-                       discover_server_notify_boot_option_add(server,
+               list_for_each_entry(&device->boot_options, opt, list) {
+                       rc = write_boot_option_add_message(server, client,
                                        opt->option);
-
+                       if (rc)
+                               return 0;
+               }
        }
 
-       waiter_register(server->waitset, client->fd, WAIT_IN,
-                       discover_server_process_message, client);
+       client->waiter = waiter_register_io(server->waitset, client->fd,
+                               WAIT_IN, discover_server_process_message,
+                               client);
 
        return 0;
 }
@@ -316,7 +338,7 @@ struct discover_server *discover_server_init(struct waitset *waitset)
                goto out_err;
        }
 
-       server->waiter = waiter_register(server->waitset, server->socket,
+       server->waiter = waiter_register_io(server->waitset, server->socket,
                        WAIT_IN, discover_server_process_connection, server);
 
        return server;