]> git.ozlabs.org Git - petitboot/blobdiff - discover/device-handler.c
discover: simplify handler switch statement
[petitboot] / discover / device-handler.c
index 0783181147edab9f0f8c6b7de9fa2ea35c516cb8..e65eb61ebc2e353fb4880b4b59260c1f9c969a73 100644 (file)
@@ -201,7 +201,7 @@ static int mount_device(struct discover_context *ctx)
        argv[4] = "ro";
        argv[5] = NULL;
 
-       if (pb_run_cmd(argv, 1)) {
+       if (pb_run_cmd(argv, 1, 0)) {
 
                /* Retry mount without ro option. */
 
@@ -210,7 +210,7 @@ static int mount_device(struct discover_context *ctx)
                argv[2] = ctx->mount_path;
                argv[3] = NULL;
 
-               if (pb_run_cmd(argv, 1))
+               if (pb_run_cmd(argv, 1, 0))
                        goto out_rmdir;
        }
 
@@ -294,11 +294,7 @@ static int handle_add_udev_event(struct device_handler *handler,
        ctx->id = talloc_strdup(ctx, event->device);
 
        devname = event_get_param(ctx->event, "DEVNAME");
-       if (!devname) {
-               pb_log("no devname for %s?\n", event->device);
-               return 0;
-       }
-
+       assert(devname);
        ctx->device_path = talloc_strdup(ctx, devname);
 
        rc = mount_device(ctx);
@@ -391,46 +387,31 @@ static int handle_remove_user_event(struct device_handler *handler,
        return 0;
 }
 
+typedef int (*event_handler)(struct device_handler *, struct event *);
+
+static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
+       [EVENT_TYPE_UDEV] = {
+               [EVENT_ACTION_ADD]      = handle_add_udev_event,
+               [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
+       },
+       [EVENT_TYPE_USER] = {
+               [EVENT_ACTION_ADD]      = handle_add_user_event,
+               [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
+       }
+};
+
 int device_handler_event(struct device_handler *handler,
                struct event *event)
 {
-       int rc = 0;
-
-       switch (event->type) {
-       case EVENT_TYPE_UDEV:
-               switch (event->action) {
-               case EVENT_ACTION_ADD:
-                       rc = handle_add_udev_event(handler, event);
-                       break;
-               case EVENT_ACTION_REMOVE:
-                       rc = handle_remove_udev_event(handler, event);
-                       break;
-               default:
-                       pb_log("%s unknown action: %d\n", __func__,
-                               event->action);
-                       break;
-               }
-               break;
-       case EVENT_TYPE_USER:
-               switch (event->action) {
-               case EVENT_ACTION_ADD:
-                       rc = handle_add_user_event(handler, event);
-                       break;
-               case EVENT_ACTION_REMOVE:
-                       rc = handle_remove_user_event(handler, event);
-                       break;
-               default:
-                       pb_log("%s unknown action: %d\n", __func__,
-                               event->action);
-                       break;
-               }
-               break;
-       default:
-               pb_log("%s unknown type: %d\n", __func__, event->type);
-               break;
+       if (event->type >= EVENT_TYPE_MAX ||
+                       event->action >= EVENT_ACTION_MAX ||
+                       !handlers[event->type][event->action]) {
+               pb_log("%s unknown type/action: %d/%d\n", __func__,
+                               event->type, event->action);
+               return 0;
        }
 
-       return rc;
+       return handlers[event->type][event->action](handler, event);
 }
 
 struct device_handler *device_handler_init(struct discover_server *server)