discover: Enable 'url' pb-events
authorSamuel Mendoza-Jonas <sam.mj@au1.ibm.com>
Thu, 24 Jul 2014 05:05:36 +0000 (15:05 +1000)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 28 Jul 2014 08:02:43 +0000 (16:02 +0800)
Adds a new option to pb-event;
./pb-event url@dev url=scheme://path/to/petitboot.conf

Specifies a remote conf file to parse for boot options

Signed-off-by: Samuel Mendoza-Jonas <sam.mj@au1.ibm.com>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/device-handler.c
discover/device-handler.h
discover/event.c
discover/event.h
discover/user-event.c

index 5a5210e7956f6a5c707d12f13dff43b28220e756..191ec33e52a153f8323f95f10c7278756fa1bf9d 100644 (file)
 #include <url/url.h>
 #include <i18n/i18n.h>
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
 #include "device-handler.h"
 #include "discover-server.h"
 #include "user-event.h"
@@ -831,6 +836,135 @@ void device_handler_update_config(struct device_handler *handler,
        device_handler_reinit(handler);
 }
 
+static char *device_from_addr(void *ctx, struct pb_url *url)
+{
+       char *ipaddr, *buf, *tok, *dev = NULL;
+       const char *delim = " ";
+       struct sockaddr_in *ip;
+       struct sockaddr_in si;
+       struct addrinfo *res;
+       struct process *p;
+       int rc;
+
+       /* Note: IPv4 only */
+       rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
+       if (rc > 0) {
+               ipaddr = url->host;
+       } else {
+               /* need to turn hostname into a valid IP */
+               rc = getaddrinfo(url->host, NULL, NULL, &res);
+               if (rc) {
+                       pb_debug("%s: Invalid URL\n",__func__);
+                       return NULL;
+               }
+               ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
+               ip = (struct sockaddr_in *) res->ai_addr;
+               inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
+               freeaddrinfo(res);
+       }
+
+       const char *argv[] = {
+               pb_system_apps.ip,
+               "route", "show", "to", "match",
+               ipaddr,
+               NULL
+       };
+
+       p = process_create(ctx);
+
+       p->path = pb_system_apps.ip;
+       p->argv = argv;
+       p->keep_stdout = true;
+
+       rc = process_run_sync(p);
+
+       if (rc) {
+               /* ip has complained for some reason; most likely
+                * there is no route to the host - bail out */
+               pb_debug("%s: No route to %s\n",__func__,url->host);
+               return NULL;
+       }
+
+       buf = p->stdout_buf;
+       /* If a route is found, ip-route output will be of the form
+        * "... dev DEVNAME ... " */
+       tok = strtok(buf, delim);
+       while (tok) {
+               if (!strcmp(tok, "dev")) {
+                       tok = strtok(NULL, delim);
+                       dev = talloc_strdup(ctx, tok);
+                       break;
+               }
+               tok = strtok(NULL, delim);
+       }
+
+       process_release(p);
+       if (dev)
+               pb_debug("%s: Found interface '%s'\n", __func__,dev);
+       return dev;
+}
+
+
+void device_handler_process_url(struct device_handler *handler,
+               const char *url)
+{
+       struct discover_context *ctx;
+       struct discover_device *dev;
+       struct boot_status *status;
+       struct pb_url *pb_url;
+       struct event *event;
+       struct param *param;
+
+       status = talloc(handler, struct boot_status);
+
+       status->type = BOOT_STATUS_ERROR;
+       status->progress = 0;
+       status->detail = talloc_asprintf(status,
+                       _("Received config URL %s"), url);
+
+       event = talloc(handler, struct event);
+       event->type = EVENT_TYPE_USER;
+       event->action = EVENT_ACTION_CONF;
+
+       event->params = talloc_array(event, struct param, 1);
+       param = &event->params[0];
+       param->name = talloc_strdup(event, "pxeconffile");
+       param->value = talloc_strdup(event, url);
+       event->n_params = 1;
+
+       pb_url = pb_url_parse(event, event->params->value);
+       if (!pb_url || !pb_url->host) {
+               status->message = talloc_asprintf(handler,
+                                       _("Invalid config URL!"));
+               goto msg;
+       }
+
+       event->device = device_from_addr(event, pb_url);
+       if (!event->device) {
+               status->message = talloc_asprintf(status,
+                                       _("Unable to route to host %s"),
+                                       pb_url->host);
+               goto msg;
+       }
+
+       dev = discover_device_create(handler, event->device);
+       ctx = device_handler_discover_context_create(handler, dev);
+       ctx->event = event;
+
+       iterate_parsers(ctx);
+
+       device_handler_discover_context_commit(handler, ctx);
+
+       talloc_free(ctx);
+
+       status->type = BOOT_STATUS_INFO;
+       status->message = talloc_asprintf(status, _("Config file %s parsed"),
+                                       pb_url->file);
+msg:
+       boot_status(handler, status);
+       talloc_free(status);
+}
+
 #ifndef PETITBOOT_TEST
 
 static void device_handler_update_lang(const char *lang)
index 246abf94657600e9916b196ec6ab1e4e1ee8d484..e8e71ad9eedf4fd2e38c63e3c8722d64fa9cbedc 100644 (file)
@@ -117,6 +117,8 @@ void device_handler_boot(struct device_handler *handler,
 void device_handler_cancel_default(struct device_handler *handler);
 void device_handler_update_config(struct device_handler *handler,
                struct config *config);
+void device_handler_process_url(struct device_handler *handler,
+               const char *url);
 void device_handler_reinit(struct device_handler *handler);
 
 int device_request_write(struct discover_device *dev, bool *release);
index 6434b40d39cd944c90cd93ab8681617b3b721ba6..79323788368d8c20b5f4f67702bd68337a9fe371 100644 (file)
@@ -47,6 +47,8 @@ static int event_parse_ad_header(char *buf, int len, enum event_action *action,
                *action = EVENT_ACTION_ADD;
        else if (streq(buf, "remove"))
                *action = EVENT_ACTION_REMOVE;
+       else if (streq(buf, "url"))
+               *action = EVENT_ACTION_URL;
        else if (streq(buf, "conf"))
                *action = EVENT_ACTION_CONF;
        else if (streq(buf, "dhcp"))
index 98ece11c73a8bd3bcda0362b8789d30baa3cb452..8dee13b560f689cacc46aeb3cbe5cdcc1da233b9 100644 (file)
@@ -10,6 +10,7 @@ enum event_type {
 enum event_action {
        EVENT_ACTION_ADD = 20,
        EVENT_ACTION_REMOVE,
+       EVENT_ACTION_URL,
        EVENT_ACTION_CONF,
        EVENT_ACTION_DHCP,
        EVENT_ACTION_MAX,
index 1f7004509041123091ff85b9234c7e8b08133fed..69defa34e341b22a9441b4b233f39d7b532eaa94 100644 (file)
@@ -54,6 +54,8 @@ static const char *event_action_name(enum event_action action)
                return "add";
        case EVENT_ACTION_REMOVE:
                return "remove";
+       case EVENT_ACTION_URL:
+               return "url";
        case EVENT_ACTION_DHCP:
                return "dhcp";
        default:
@@ -430,6 +432,18 @@ static int user_event_remove(struct user_event *uev, struct event *event)
        return 0;
 }
 
+static int user_event_url(struct user_event *uev, struct event *event)
+{
+       struct device_handler *handler = uev->handler;
+       const char *url;
+
+       url = event_get_param(event, "url");
+       if (url)
+               device_handler_process_url(handler, url);
+
+       return 0;
+}
+
 static void user_event_handle_message(struct user_event *uev, char *buf,
        int len)
 {
@@ -453,6 +467,9 @@ static void user_event_handle_message(struct user_event *uev, char *buf,
        case EVENT_ACTION_REMOVE:
                result = user_event_remove(uev, event);
                break;
+       case EVENT_ACTION_URL:
+               result = user_event_url(uev, event);
+               break;
        case EVENT_ACTION_CONF:
                result = user_event_conf(uev, event);
                break;