Patchwork [05/18] petitboot: Discover add current device report

login
register
mail settings
Submitter Geoff Levand
Date March 25, 2009, 10:35 p.m.
Message ID <20090325223547.186301418@am.sony.com>>
Download mbox | patch
Permalink /patch/25111/
State Accepted
Delegated to: Jeremy Kerr
Headers show

Comments

Geoff Levand - March 25, 2009, 10:35 p.m.
Fillout the missing discover code that reports current devices.

Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
---
 discover/device-handler.c  |   54 ++++++++++++++++++---------------------------
 discover/device-handler.h  |    2 -
 discover/discover-server.c |    6 +++--
 discover/udev.c            |   10 ++++++++
 4 files changed, 37 insertions(+), 35 deletions(-)
Jeremy Kerr - March 30, 2009, 8:30 a.m.
Hi Geoff,

>  struct device_handler {
>  	struct discover_server *server;
> -
> -	struct device *devices;
>  	int n_devices;
> -
>  	struct list contexts;
>  };

The idea here is that the contexts are around for the duration of the 
discovery. After the discovery has completed, we move the struct devices 
from the context over to the 'devices' member here.

This makes accessing the devices much easier, as we don't have to 
iterate through the contexts. This isn't completely implemented at the 
moment, but is where I'd like to be headed.

> --- a/discover/device-handler.h
> +++ b/discover/device-handler.h
> @@ -25,7 +25,7 @@ struct device_handler *device_handler_in
>  void device_handler_destroy(struct device_handler *devices);
>
>  int device_handler_get_current_devices(struct device_handler
> *handler,
> -		const struct device **devices);
> +		const struct device ***devices);
>

I don't like this triple pointer API, do we really need it?

We could either expose the array of devices directly, or have a 
get_device(int index) / get_device_count() type accessors.

Regards,


Jeremy

Patch

--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -23,10 +23,7 @@ 
 
 struct device_handler {
 	struct discover_server *server;
-
-	struct device *devices;
 	int n_devices;
-
 	struct list contexts;
 };
 
@@ -36,30 +33,29 @@  struct mount_map {
 };
 
 
-static struct boot_option options[] = {
-	{
-		.id = "1.1",
-		.name = "meep one",
-		.description = "meep description one",
-		.icon_file = "meep.one.png",
-		.boot_args = "root=/dev/sda1",
-	},
-};
+int device_handler_get_current_devices(
+		struct device_handler *handler,
+		const struct device ***devices)
+{
+	struct discover_context *ctx;
+	int i;
 
-static struct device device = {
-	.id = "1",
-	.name = "meep",
-	.description = "meep description",
-	.icon_file = "meep.png",
-};
+	if (!handler->n_devices) {
+		*devices = NULL;
+		return 0;
+	}
 
-int device_handler_get_current_devices(
-		struct device_handler *handler __attribute__((unused)),
-		const struct device **devices)
+	*devices = (const struct device **)talloc_array(NULL, struct device *,
+		handler->n_devices);
 
-{
-	*devices = &device;
-	return 1;
+	if (!*devices)
+		return 0;
+
+	i = 0;
+	list_for_each_entry(&handler->contexts, ctx, list)
+		(*devices)[i++] = ctx->device;
+
+	return handler->n_devices;
 }
 
 static int mkdir_recursive(const char *dir)
@@ -325,6 +321,7 @@  static int handle_add_event(struct devic
 		return 0;
 	}
 
+	handler->n_devices++;
 	list_add(&handler->contexts, &ctx->list);
 	talloc_set_destructor(ctx, destroy_context);
 
@@ -350,6 +347,7 @@  static int handle_remove_event(struct de
 	if (!ctx)
 		return 0;
 
+	handler->n_devices--;
 	discover_server_notify_remove(handler->server, ctx->device);
 
 	talloc_free(ctx);
@@ -378,10 +376,8 @@  int device_handler_event(struct device_h
 struct device_handler *device_handler_init(struct discover_server *server)
 {
 	struct device_handler *handler;
-	unsigned int i;
 
 	handler = talloc(NULL, struct device_handler);
-	handler->devices = NULL;
 	handler->n_devices = 0;
 	handler->server = server;
 
@@ -390,12 +386,6 @@  struct device_handler *device_handler_in
 	/* set up our mount point base */
 	mkdir_recursive(mount_base());
 
-	/* setup out test objects */
-	list_init(&device.boot_options);
-
-	for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
-		list_add(&device.boot_options, &options[i].list);
-
 	parser_init();
 
 	return handler;
--- a/discover/device-handler.h
+++ b/discover/device-handler.h
@@ -25,7 +25,7 @@  struct device_handler *device_handler_in
 void device_handler_destroy(struct device_handler *devices);
 
 int device_handler_get_current_devices(struct device_handler *handler,
-		const struct device **devices);
+		const struct device ***devices);
 
 int device_handler_event(struct device_handler *handler,
 		struct udev_event *event);
--- a/discover/discover-server.c
+++ b/discover/discover-server.c
@@ -128,7 +128,7 @@  static int write_remove_message(struct d
 static int discover_server_process(void *arg)
 {
 	struct discover_server *server = arg;
-	const struct device *devices;
+	const struct device **devices = NULL;
 	struct client *client;
 	int fd, i, n_devices;
 
@@ -151,7 +151,9 @@  static int discover_server_process(void 
 	n_devices = device_handler_get_current_devices(server->device_handler,
 			&devices);
 	for (i = 0; i < n_devices; i++)
-		write_add_message(server, client, &devices[i]);
+		write_add_message(server, client, devices[i]);
+
+	talloc_free(devices);
 
 	return 0;
 }
--- a/discover/udev.c
+++ b/discover/udev.c
@@ -147,6 +147,8 @@  static int udev_process(void *arg)
 	char buf[4096];
 	int len;
 
+	pb_log("%s\n", __func__);
+
 	len = recvfrom(udev->socket, buf, sizeof(buf), 0, NULL, NULL);
 
 	if (len < 0) {
@@ -176,6 +178,9 @@  struct udev *udev_init(struct device_han
 {
 	struct sockaddr_un addr;
 	struct udev *udev;
+	int rc;
+
+	pb_log("%s\n", __func__);
 
 	unlink(PBOOT_DEVICE_SOCKET);
 
@@ -201,6 +206,11 @@  struct udev *udev_init(struct device_han
 
 	waiter_register(udev->socket, WAIT_IN, udev_process, udev);
 
+	rc = system("/sbin/udevadm trigger --subsystem-match=block");
+
+	if (rc)
+		pb_log("udev trigger failed: %d (%d)\n", rc, WEXITSTATUS(rc));
+
 	return udev;
 
 out_err: