X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fdiscover-server.c;h=4ba91eeba5526c3ce85bd87e4c181fe542a434af;hp=198793204ef0cd30a68bb7da80858fd3a7791cdd;hb=1435814a67d3c1a0199f84b91246b37eb8fa8b99;hpb=e4f5bd235894c11823ac1befe8c8c43063cad026 diff --git a/discover/discover-server.c b/discover/discover-server.c index 1987932..4ba91ee 100644 --- a/discover/discover-server.c +++ b/discover/discover-server.c @@ -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; @@ -145,6 +149,24 @@ static int write_device_remove_message(struct discover_server *server, return client_write_message(server, client, message); } +static int write_boot_status_message(struct discover_server *server, + struct client *client, const struct boot_status *status) +{ + struct pb_protocol_message *message; + int len; + + len = pb_protocol_boot_status_len(status); + + message = pb_protocol_create_message(client, + PB_PROTOCOL_ACTION_STATUS, len); + if (!message) + return -1; + + pb_protocol_serialise_boot_status(status, message->payload, len); + + return client_write_message(server, client, message); +} + static int discover_server_process_message(void *arg) { struct pb_protocol_message *message; @@ -154,8 +176,11 @@ static int discover_server_process_message(void *arg) message = pb_protocol_read_message(client, client->fd); - if (!message) + if (!message) { + talloc_free(client); return 0; + } + if (message->action != PB_PROTOCOL_ACTION_BOOT) { pb_log("%s: invalid action %d\n", __func__, message->action); @@ -178,18 +203,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); @@ -204,16 +229,20 @@ 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(server->waitset, client->fd, WAIT_IN, + discover_server_process_message, client); return 0; } @@ -247,6 +276,15 @@ void discover_server_notify_device_remove(struct discover_server *server, } +void discover_server_notify_boot_status(struct discover_server *server, + struct boot_status *status) +{ + struct client *client; + + list_for_each_entry(&server->clients, client, list) + write_boot_status_message(server, client, status); +} + void discover_server_set_device_source(struct discover_server *server, struct device_handler *handler) {