]> git.ozlabs.org Git - petitboot/blobdiff - discover/paths.c
Various fixups and checks to make scan-build happy
[petitboot] / discover / paths.c
index a1f65ec10a03dfa68a7c6f7765b9f5e97d8cc4b5..16fdd597444d19856f1aad00cb810aa3ee68657e 100644 (file)
@@ -1,23 +1,46 @@
-#define _GNU_SOURCE
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
 
+#include <assert.h>
+#include <netdb.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include <talloc/talloc.h>
 #include <system/system.h>
 #include <process/process.h>
 #include <url/url.h>
 #include <log/log.h>
+#include "i18n/i18n.h"
 
 #include "paths.h"
+#include "device-handler.h"
+#include "sysinfo.h"
 
 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
 
-struct load_url_async_data {
-       load_url_callback url_cb;
-       void *ctx;
-       int status;
+
+struct list    pending_network_jobs;
+
+struct network_job {
+       struct load_task        *task;
+       int                     flags;
+
+       struct list_item        list;
+};
+
+struct load_task {
+       struct pb_url           *url;
+       struct process          *process;
+       struct load_url_result  *result;
+       bool                    async;
+       load_url_complete       async_cb;
+       void                    *async_data;
 };
 
 const char *mount_base(void)
@@ -39,239 +62,336 @@ char *join_paths(void *alloc_ctx, const char *a, const char *b)
        return full_path;
 }
 
+#ifndef PETITBOOT_TEST
+
+#ifdef WITH_BUSYBOX
+static inline bool have_busybox(void) { return true; }
+#else
+static inline bool have_busybox(void) { return false; }
+#endif
 
 static char *local_name(void *ctx)
 {
-       char *tmp, *ret;
+       char *ret, tmp[] = "/tmp/pb-XXXXXX";
+       mode_t oldmask;
+       int fd;
 
-       tmp = tempnam(NULL, "pb-");
+       oldmask = umask(0644);
+       fd = mkstemp(tmp);
+       umask(oldmask);
 
-       if (!tmp)
+       if (fd < 0)
                return NULL;
 
+       close(fd);
+
        ret = talloc_strdup(ctx, tmp);
-       free(tmp);
 
        return ret;
 }
 
-static void load_url_exit_cb(struct process *process)
+static void load_url_result_cleanup_local(struct load_url_result *result)
 {
-       struct load_url_async_data *url_data = process->data;
+       if (result->cleanup_local)
+               unlink(result->local);
+}
 
-       pb_log("The download client '%s' [pid %d] exited, rc %d\n",
-                       process->path, process->pid, process->exit_status);
+static void load_url_process_exit(struct process *process)
+{
+       struct load_task *task = process->data;
+       struct load_url_result *result;
+       load_url_complete cb;
+       void *data;
+
+       pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
+                       process->path, process->pid, task->url->full,
+                       process->exit_status);
+
+       result = task->result;
+       data = task->async_data;
+       cb = task->async_cb;
+
+       if (result->status == LOAD_CANCELLED) {
+               load_url_result_cleanup_local(result);
+       } else if (process_exit_ok(process)) {
+               result->status = LOAD_OK;
+       } else {
+               result->status = LOAD_ERROR;
+               load_url_result_cleanup_local(result);
+               pb_debug("Download client stdout buffer:\n%s\n",
+                               process->stdout_buf);
+       }
 
-       if (!url_data->status)
-               url_data->url_cb(url_data->ctx, &(url_data->status));
+       if (result->status == LOAD_OK && process->stdout_data)
+               device_handler_status_info(process->stdout_data,
+                               _("Download complete: %s"), task->url->file);
 
+       /* The load callback may well free the ctx, which was the
+        * talloc parent of the task. Therefore, we want to do our cleanup
+        * before invoking it
+        */
        process_release(process);
+       talloc_free(task);
+       result->task = NULL;
+
+       cb(result, data);
 }
 
-/**
- * pb_load_nfs - Mounts the NFS export and returns the local file path.
- *
- * Returns the local file path in a talloc'ed character string on success,
- * or NULL on error.
+/*
+ * Callback to retrieve progress information from Busybox utilities.
+ * Busybox utilities use a common progress bar format which progress percentage
+ * and current size can be can be parsed from.
  */
-static char *load_nfs(void *ctx, struct pb_url *url,
-               struct load_url_async_data *url_data)
+static int busybox_progress_cb(void *arg)
 {
-       char *local, *opts;
-       int result;
-       struct process *process;
-       const char *argv[] = {
-                       pb_system_apps.mount,
-                       "-t", "nfs",
-                       NULL,
-                       url->host,
-                       url->dir,
-                       NULL,
-                       NULL,
-       };
-
-       local = local_name(ctx);
-       if (!local)
-               return NULL;
-       argv[6] = local;
+       const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
+       struct process_info *procinfo = arg;
+       char *n, *s, suffix, *line = NULL;
+       struct device_handler *handler;
+       unsigned int percentage, size;
+       struct process *p;
+       int rc;
 
-       result = pb_mkdir_recursive(local);
-       if (result)
-               goto fail;
+       if (!arg)
+               return -1;
 
-       opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
-       argv[3] = opts;
+       p = procinfo_get_process(procinfo);
+       handler = p->stdout_data;
 
-       if (url->port)
-               opts = talloc_asprintf_append(opts, ",port=%s", url->port);
+       rc = process_process_stdout(procinfo, &line);
 
-       if (url_data) {
-               process = process_create(ctx);
+       if (rc) {
+               /* Unregister ourselves from progress tracking */
+               device_handler_status_download_remove(handler, procinfo);
+       }
 
-               process->path = pb_system_apps.mount;
-               process->argv = argv;
-               process->exit_cb = load_url_exit_cb;
-               process->data = url_data;
+       if (rc || !line)
+               return rc;
+
+       rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
+
+       /*
+        * Many unrecognised lines are partial updates. If we see a partial
+        * line with a newline character, see if we can match a valid line
+        * at the end of stdout_buf
+        */
+       if (rc != 3) {
+               n = strchr(line, '\n');
+               if (n)
+                       for (s = n - 1; s >= p->stdout_buf; s--)
+                               if (*s == '\n') {
+                                       rc = sscanf(s + 1, busybox_fmt,
+                                               &percentage, &size, &suffix);
+                                       break;
+                               }
+       }
 
-               result = process_run_async(process);
-               if (result)
-                       process_release(process);
-       } else {
-               result = process_run_simple_argv(ctx, argv);
+       if (rc != 3) {
+               percentage = size = 0;
+               suffix = ' ';
        }
 
-       talloc_free(opts);
+       device_handler_status_download(handler, procinfo,
+                       percentage, size, suffix);
+
+       return 0;
+}
+
 
-       if (result)
-               goto fail;
 
-       local = talloc_asprintf_append(local,  "/%s", url->path);
-       pb_log("%s: local '%s'\n", __func__, local);
+static void load_process_to_local_file(struct load_task *task,
+               const char **argv, int argv_local_idx)
+{
+       int rc;
+
+       task->result->local = local_name(task->result);
+       if (!task->result->local) {
+               task->result->status = LOAD_ERROR;
+               return;
+       }
+       task->result->cleanup_local = true;
 
-       return local;
+       if (argv_local_idx)
+               argv[argv_local_idx] = task->result->local;
 
-fail:
-       pb_rmdir_recursive("/", local);
-       talloc_free(local);
-       return NULL;
+       task->process->argv = argv;
+       task->process->path = argv[0];
+
+       if (task->async) {
+               rc = process_run_async(task->process);
+               if (rc) {
+                       process_release(task->process);
+                       task->process = NULL;
+               }
+               task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
+       } else {
+               rc = process_run_sync(task->process);
+               if (rc || !process_exit_ok(task->process))
+                       task->result->status = LOAD_ERROR;
+               else
+                       task->result->status = LOAD_OK;
+               process_release(task->process);
+               task->process = NULL;
+       }
 }
 
 /**
- * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
- *
- * Returns the local file path in a talloc'ed character string on success,
- * or NULL on error.
+ * pb_load_nfs - Create a mountpoint, set the local file within that
+ * mountpoint, and run the appropriate mount command
  */
-static char *load_sftp(void *ctx, struct pb_url *url,
-               struct load_url_async_data *url_data)
+
+static void load_nfs(struct load_task *task)
 {
-       char *host_path, *local;
-       int result;
-       struct process *process;
+       char *mountpoint, *opts;
+       int rc;
        const char *argv[] = {
-                       pb_system_apps.sftp,
-                       NULL,
-                       NULL,
+                       pb_system_apps.mount,
+                       "-t", "nfs",
+                       NULL,                   /* 3: opts */
+                       task->url->host,
+                       task->url->dir,
+                       NULL,                   /* 6: mountpoint */
                        NULL,
        };
 
-       local = local_name(ctx);
-       if (!local)
-               return NULL;
-       argv[2] = local;
+       task->result->status = LOAD_ERROR;
+       mountpoint = local_name(task->result);
+       if (!mountpoint)
+               return;
+       task->result->cleanup_local = true;
+       argv[6] = mountpoint;
+
+       rc = pb_mkdir_recursive(mountpoint);
+       if (rc)
+               return;
 
-       host_path = talloc_asprintf(local, "%s:%s", url->host, url->path);
-       argv[1] = host_path;
+       opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
+       argv[3] = opts;
+
+       if (task->url->port)
+               opts = talloc_asprintf_append(opts, ",port=%s",
+                                               task->url->port);
 
-       if (url_data) {
-               process = process_create(ctx);
+       task->result->local = talloc_asprintf(task->result, "%s/%s",
+                                                       mountpoint,
+                                                       task->url->path);
 
-               process->path = pb_system_apps.sftp;
-               process->argv = argv;
-               process->exit_cb = load_url_exit_cb;
-               process->data = url_data;
+       task->process->path = pb_system_apps.mount;
+       task->process->argv = argv;
 
-               result = process_run_async(process);
-               if (result)
-                       process_release(process);
+       if (task->async) {
+               rc = process_run_async(task->process);
+               if (rc) {
+                       process_release(task->process);
+                       task->process = NULL;
+               }
+               task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
        } else {
-               result = process_run_simple_argv(ctx, argv);
+               rc = process_run_sync(task->process);
+               task->result->status = rc ? LOAD_ERROR : LOAD_OK;
+               process_release(task->process);
+               task->process = NULL;
        }
 
-       if (result)
-               goto fail;
+       talloc_free(opts);
+}
 
-       return local;
+static void load_sftp(struct load_task *task)
+{
+       const char *argv[] = {
+                       pb_system_apps.sftp,
+                       NULL,           /* 1: host:path */
+                       NULL,           /* 2: local file */
+                       NULL,
+       };
 
-fail:
-       talloc_free(local);
-       return NULL;
+       argv[1] = talloc_asprintf(task, "%s:%s",
+                               task->url->host, task->url->path);
+       load_process_to_local_file(task, argv, 2);
 }
 
-/**
- * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
- *
- * Returns the local file path in a talloc'ed character string on success,
- * or NULL on error.
- */
-
-static char *load_tftp(void *ctx, struct pb_url *url,
-               struct load_url_async_data *url_data)
+static enum tftp_type check_tftp_type(void *ctx)
 {
-       int result;
-       const char *argv[10];
-       const char **p;
-       char *local;
+       const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
        struct process *process;
+       enum tftp_type type;
+       int rc;
+
+       process = process_create(ctx);
+       process->path = pb_system_apps.tftp;
+       process->argv = argv;
+       process->keep_stdout = true;
+       process->add_stderr = true;
+       rc = process_run_sync(process);
+
+       if (rc || !process->stdout_buf || process->stdout_len == 0) {
+               pb_log("Can't check TFTP client type!\n");
+               type = TFTP_TYPE_BROKEN;
+
+       } else if (memmem(process->stdout_buf, process->stdout_len,
+                               "tftp-hpa", strlen("tftp-hpa"))) {
+               pb_debug("Found TFTP client type: tftp-hpa\n");
+               type = TFTP_TYPE_HPA;
+
+       } else if (memmem(process->stdout_buf, process->stdout_len,
+                               "BusyBox", strlen("BusyBox"))) {
+               pb_debug("Found TFTP client type: BusyBox tftp\n");
+               type = TFTP_TYPE_BUSYBOX;
 
-       local = local_name(ctx);
-
-       if (!local)
-               return NULL;
-
-       /* first try busybox tftp args */
-
-       p = argv;
-       *p++ = pb_system_apps.tftp;     /* 1 */
-       *p++ = "-g";                    /* 2 */
-       *p++ = "-l";                    /* 3 */
-       *p++ = local;                   /* 4 */
-       *p++ = "-r";                    /* 5 */
-       *p++ = url->path;               /* 6 */
-       *p++ = url->host;               /* 7 */
-       if (url->port)
-               *p++ = url->port;       /* 8 */
-       *p++ = NULL;                    /* 9 */
-
-       if (url_data) {
-               process = process_create(ctx);
-
-               process->path = pb_system_apps.tftp;
-               process->argv = argv;
-               process->exit_cb = load_url_exit_cb;
-               process->data = url_data;
-
-               result = process_run_async(process);
        } else {
-               result = process_run_simple_argv(ctx, argv);
+               pb_log("Unknown TFTP client type!\n");
+               type = TFTP_TYPE_BROKEN;
        }
 
-       if (!result)
-               return local;
-
-       /* next try tftp-hpa args */
-       p = argv;
-       *p++ = pb_system_apps.tftp;     /* 1 */
-       *p++ = "-m";                    /* 2 */
-       *p++ = "binary";                /* 3 */
-       *p++ = url->host;               /* 4 */
-       if (url->port)
-               *p++ = url->port;       /* 5 */
-       *p++ = "-c";                    /* 6 */
-       *p++ = "get";                   /* 7 */
-       *p++ = url->path;               /* 8 */
-       *p++ = local;                   /* 9 */
-       *p++ = NULL;                    /* 10 */
-
-       if (url_data) {
-               process->argv = argv;
-               result = process_run_async(process);
-               if (result)
-                       process_release(process);
-       } else {
-               result = process_run_simple_argv(ctx, argv);
-       }
+       process_release(process);
+       return type;
+}
 
-       if (!result)
-               return local;
+static void load_tftp(struct load_task *task)
+{
+       const char *port = "69";
+       const char *argv[10] = {
+               pb_system_apps.tftp,
+       };
 
-       talloc_free(local);
-       return NULL;
+       if (task->url->port)
+               port = task->url->port;
+
+       if (tftp_type == TFTP_TYPE_UNKNOWN)
+               tftp_type = check_tftp_type(task);
+
+       if (tftp_type == TFTP_TYPE_BUSYBOX) {
+               argv[1] = "-g";
+               argv[2] = "-l";
+               argv[3] = NULL; /* 3: local file */
+               argv[4] = "-r";
+               argv[5] = task->url->path;
+               argv[6] = task->url->host;
+               argv[7] = port;
+               argv[8] = NULL;
+
+               load_process_to_local_file(task, argv, 3);
+
+       } else if (tftp_type == TFTP_TYPE_HPA) {
+               argv[1] = "-m";
+               argv[2] = "binary";
+               argv[3] = task->url->host;
+               argv[4] = port;
+               argv[5] = "-c";
+               argv[6] = "get";
+               argv[7] = task->url->path;
+               argv[8] = NULL; /* 8: local file */
+               argv[9] = NULL;
+               load_process_to_local_file(task, argv, 8);
+
+       } else
+               task->result->status = LOAD_ERROR;
 }
 
 enum wget_flags {
-       wget_empty = 0,
-       wget_no_check_certificate = 1,
+       wget_empty                      = 0x1,
+       wget_no_check_certificate       = 0x2,
+       wget_verbose                    = 0x4,
 };
 
 /**
@@ -281,57 +401,139 @@ enum wget_flags {
  * or NULL on error.
  */
 
-static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags,
-               struct load_url_async_data *url_data)
+static void load_wget(struct load_task *task, int flags)
 {
-       int result;
-       const char *argv[7];
-       const char **p;
-       char *local;
-       struct process *process;
-
-       local = local_name(ctx);
+       const char *argv[] = {
+               pb_system_apps.wget,
+               "-O",
+               NULL, /* 2: local file */
+               NULL, /* 3 (optional): --quiet */
+               NULL, /* 4 (optional): --no-check-certificate */
+               NULL, /* 5: URL */
+               NULL,
+       };
+       int i;
 
-       if (!local)
-               return NULL;
+       if (task->process->stdout_cb)
+               flags |= wget_verbose;
 
-       p = argv;
-       *p++ = pb_system_apps.wget;                     /* 1 */
-#if !defined(DEBUG)
-       *p++ = "--quiet";                               /* 2 */
+       i = 3;
+#if defined(DEBUG)
+       flags |= wget_verbose;
 #endif
-       *p++ = "-O";                                    /* 3 */
-       *p++ = local;                                   /* 4 */
-       *p++ = url->full;                               /* 5 */
+       if ((flags & wget_verbose) == 0)
+               argv[i++] = "--quiet";
+
        if (flags & wget_no_check_certificate)
-               *p++ = "--no-check-certificate";        /* 6 */
-       *p++ = NULL;                                    /* 7 */
+               argv[i++] = "--no-check-certificate";
 
-       if (url_data) {
-               process = process_create(ctx);
+       argv[i] = task->url->full;
 
-               process->path = pb_system_apps.wget;
-               process->argv = argv;
-               process->exit_cb = load_url_exit_cb;
-               process->data = url_data;
+       load_process_to_local_file(task, argv, 2);
+}
+
+/* Although we don't need to load anything for a local path (we just return
+ * the path from the file:// URL), the other load helpers will error-out on
+ * non-existant files. So, do the same here with an access() check on the local
+ * filename.
+ */
+static void load_local(struct load_task *task)
+{
+       struct load_url_result *result = task->result;
+       int rc;
 
-               result = process_run_async(process);
-               if (result)
-                       process_release(process);
+       rc = access(task->url->path, F_OK);
+       if (rc) {
+               result->status = LOAD_ERROR;
        } else {
-               result = process_run_simple_argv(ctx, argv);
+               result->local = talloc_strdup(task->result, task->url->path);
+               result->status = LOAD_OK;
        }
 
-       if (result)
-               goto fail;
+       if (task->async_cb)
+               task->async_cb(task->result, task->async_data);
+}
+
+static void load_url_async_start_pending(struct load_task *task, int flags)
+{
+       pb_log("Starting pending job for %s\n", task->url->full);
+
+       switch (task->url->scheme) {
+       case pb_url_ftp:
+       case pb_url_http:
+               load_wget(task, flags);
+               break;
+       case pb_url_https:
+               flags |= wget_no_check_certificate;
+               load_wget(task, flags);
+               break;
+       case pb_url_nfs:
+               load_nfs(task);
+               break;
+       case pb_url_sftp:
+               load_sftp(task);
+               break;
+       case pb_url_tftp:
+               load_tftp(task);
+               break;
+       default:
+               /* Shouldn't be a need via this path but.. */
+               load_local(task);
+               break;
+       }
+
+       if (task->result->status == LOAD_ERROR) {
+               pb_log("Pending job failed for %s\n", task->url->full);
+               load_url_result_cleanup_local(task->result);
+               talloc_free(task->result);
+               talloc_free(task);
+       }
+}
+
+void pending_network_jobs_start(void)
+{
+       struct network_job *job, *tmp;
+
+       if (!pending_network_jobs.head.next)
+               return;
+
+       list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
+               load_url_async_start_pending(job->task, job->flags);
+               list_remove(&job->list);
+       }
+}
+
+void pending_network_jobs_cancel(void)
+{
+       struct network_job *job, *tmp;
+
+       if (!pending_network_jobs.head.next)
+               return;
+
+       list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
+               talloc_free(job);
+       list_init(&pending_network_jobs);
+}
+
+static void pending_network_jobs_add(struct load_task *task, int flags)
+{
+       struct network_job *job;
+
+       if (!pending_network_jobs.head.next)
+               list_init(&pending_network_jobs);
 
-       return local;
+       job = talloc(task, struct network_job);
+       if (!job) {
+               pb_log("Failed to allocate space for pending job\n");
+               return;
+       }
 
-fail:
-       talloc_free(local);
-       return NULL;
+       job->task = task;
+       job->flags = flags;
+       list_add_tail(&pending_network_jobs, &job->list);
 }
 
+
 /**
  * load_url - Loads a (possibly) remote URL and returns the local file
  * path.
@@ -346,61 +548,142 @@ fail:
  * or NULL on error.
  */
 
-char *load_url_async(void *ctx, struct pb_url *url, unsigned int *tempfile,
-               load_url_callback url_cb)
+struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
+               load_url_complete async_cb, void *async_data,
+               waiter_cb stdout_cb, void *stdout_data)
 {
-       char *local;
-       int tmp = 0;
-       struct load_url_async_data *url_data;
+       struct load_url_result *result;
+       struct load_task *task;
+       struct addrinfo *res;
+       int flags = 0;
 
        if (!url)
                return NULL;
 
-       url_data = NULL;
+       task = talloc_zero(ctx, struct load_task);
+       task->url = url;
+       task->async = async_cb != NULL;
+       task->result = talloc_zero(ctx, struct load_url_result);
+       task->result->task = task;
+       task->result->url = url;
+       task->process = process_create(task);
+       if (task->async) {
+               task->async_cb = async_cb;
+               task->async_data = async_data;
+               task->process->exit_cb = load_url_process_exit;
+               task->process->data = task;
+               task->process->stdout_cb = stdout_cb;
+               task->process->stdout_data = stdout_data;
+       }
+
+       if (!stdout_cb && stdout_data && have_busybox())
+               task->process->stdout_cb = busybox_progress_cb;
+
+       /* Make sure we save output for any task that has a custom handler */
+       if (task->process->stdout_cb) {
+               task->process->add_stderr = true;
+               task->process->keep_stdout = true;
+       }
 
-       if (url_cb) {
-               url_data = talloc_zero(ctx, struct load_url_async_data);
-               url_data->url_cb = url_cb;
-               url_data->ctx = ctx;
-               url_data->status = 0;
+       /* If the url is remote but network is not yet available queue up this
+        * load for later */
+       if (url->scheme != pb_url_file) {
+               if (getaddrinfo(url->host, NULL, NULL, &res) != 0) {
+                       pb_log("load task for %s queued pending network\n", url->full);
+                       pending_network_jobs_add(task, flags);
+                       task->result->status = LOAD_ASYNC;
+                       return task->result;
+               }
+               freeaddrinfo(res);
        }
 
        switch (url->scheme) {
        case pb_url_ftp:
        case pb_url_http:
-               local = load_wget(ctx, url, 0, url_data);
-               tmp = !!local;
+               load_wget(task, flags);
                break;
        case pb_url_https:
-               local = load_wget(ctx, url, wget_no_check_certificate,
-                               url_data);
-               tmp = !!local;
+               flags |= wget_no_check_certificate;
+               load_wget(task, flags);
                break;
        case pb_url_nfs:
-               local = load_nfs(ctx, url, url_data);
-               tmp = !!local;
+               load_nfs(task);
                break;
        case pb_url_sftp:
-               local = load_sftp(ctx, url, url_data);
-               tmp = !!local;
+               load_sftp(task);
                break;
        case pb_url_tftp:
-               local = load_tftp(ctx, url, url_data);
-               tmp = !!local;
+               load_tftp(task);
                break;
        default:
-               local = talloc_strdup(ctx, url->full);
-               tmp = 0;
+               load_local(task);
                break;
        }
 
-       if (tempfile)
-               *tempfile = tmp;
+       result = task->result;
+       if (result->status == LOAD_ERROR) {
+               load_url_result_cleanup_local(task->result);
+               talloc_free(result);
+               talloc_free(task);
+               return NULL;
+       }
+
+       if (!task->async || result->status == LOAD_OK)
+               talloc_free(task);
 
-       return local;
+       return result;
 }
 
-char *load_url(void *ctx, struct pb_url *url, unsigned int *tempfile)
+struct load_url_result *load_url(void *ctx, struct pb_url *url)
 {
-       return load_url_async(ctx, url, tempfile, NULL);
+       return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
 }
+
+void load_url_async_cancel(struct load_url_result *res)
+{
+       struct load_task *task = res->task;
+
+       /* the completion callback may have already been called; this clears
+        * res->task */
+       if (!task)
+               return;
+
+       if (res->status == LOAD_CANCELLED)
+               return;
+
+       assert(task->async);
+       assert(task->process);
+
+       res->status = LOAD_CANCELLED;
+       process_stop_async(task->process);
+}
+
+#else
+
+static void __attribute__((unused)) load_local(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_wget(
+               struct load_task *task __attribute__((unused)),
+               int flags __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_tftp(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_sftp(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_nfs(
+               struct load_task *task __attribute__((unused)))
+{
+}
+static void __attribute__((unused)) load_url_process_exit(
+               struct process *process __attribute__((unused)))
+{
+}
+
+#endif