X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fpaths.c;h=0d414ebaac0833a075b0f65532734e1620e46649;hp=ef8a45f924c8fa658fd4c39330f40eaf485761b4;hb=e1f6ffb9ca9e35738385a3524ba46896a0b519e6;hpb=c62667e5c78ea212e5ac49244e9792954a1d8f71 diff --git a/discover/paths.c b/discover/paths.c index ef8a45f..0d414eb 100644 --- a/discover/paths.c +++ b/discover/paths.c @@ -1,11 +1,15 @@ -#define _GNU_SOURCE +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif +#include #include #include #include #include #include +#include #include #include @@ -13,135 +17,20 @@ #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt") -struct mount_map { - char *dev, *mnt; +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; }; -static struct mount_map *mount_map; -static int mount_map_size; - -static int is_prefix(const char *str, const char *prefix) -{ - return !strncmp(str, prefix, strlen(prefix)); -} - -static int is_prefix_ignorecase(const char *str, const char *prefix) -{ - return !strncasecmp(str, prefix, strlen(prefix)); -} - const char *mount_base(void) { return DEVICE_MOUNT_BASE; } -char *encode_label(void *alloc_ctx, const char *label) -{ - char *str, *c; - unsigned int i; - - /* the label can be expanded by up to four times */ - str = talloc_size(alloc_ctx, strlen(label) * 4 + 1); - c = str; - - for (i = 0; i < strlen(label); i++) { - - if (label[i] == '/' || label[i] == '\\') { - sprintf(c, "\\x%02x", label[i]); - c += 4; - continue; - } - - *(c++) = label[i]; - } - - *c = '\0'; - - return str; -} - -char *parse_device_path(void *alloc_ctx, - const char *dev_str, const char __attribute__((unused)) *cur_dev) -{ - char *dev, *enc; - - if (is_prefix_ignorecase(dev_str, "uuid=")) { - dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-uuid/%s", - dev_str + strlen("uuid=")); - return dev; - } - - if (is_prefix_ignorecase(dev_str, "label=")) { - enc = encode_label(NULL, dev_str + strlen("label=")); - dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-label/%s", enc); - talloc_free(enc); - return dev; - } - - /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand - * back before returning. - */ - if (is_prefix(dev_str, "/dev/")) - dev_str += strlen("/dev/"); - - return join_paths(alloc_ctx, "/dev", dev_str); -} - -const char *mountpoint_for_device(const char *dev) -{ - int i; - - if (is_prefix(dev, "/dev/")) - dev += strlen("/dev/"); - - /* check existing entries in the map */ - for (i = 0; i < mount_map_size; i++) - if (!strcmp(mount_map[i].dev, dev)) - return mount_map[i].mnt; - - /* no existing entry, create a new one */ - i = mount_map_size++; - mount_map = talloc_realloc(NULL, mount_map, - struct mount_map, mount_map_size); - - mount_map[i].dev = talloc_strdup(mount_map, dev); - mount_map[i].mnt = join_paths(mount_map, DEVICE_MOUNT_BASE, dev); - return mount_map[i].mnt; -} - -char *resolve_path(void *alloc_ctx, const char *path, const char *current_dev) -{ - static const char s_file[] = "file://"; - char *ret; - const char *devpath, *sep; - - /* test for urls */ - - if (!strncasecmp(path, s_file, sizeof(s_file) - 1)) - path += sizeof(s_file) - 1; - else if (strstr(path, "://")) - return talloc_strdup(alloc_ctx, path); - - sep = strchr(path, ':'); - if (!sep) { - devpath = mountpoint_for_device(current_dev); - ret = join_paths(alloc_ctx, devpath, path); - } else { - /* parse just the device name into dev */ - char *tmp, *dev; - tmp = talloc_strndup(NULL, path, sep - path); - dev = parse_device_path(NULL, tmp, current_dev); - - devpath = mountpoint_for_device(dev); - ret = join_paths(alloc_ctx, devpath, sep + 1); - - talloc_free(dev); - talloc_free(tmp); - } - - return ret; -} - char *join_paths(void *alloc_ctx, const char *a, const char *b) { char *full_path; @@ -159,172 +48,248 @@ char *join_paths(void *alloc_ctx, const char *a, const char *b) static char *local_name(void *ctx) { - char *tmp, *ret; + char *ret, tmp[] = "/tmp/pb-XXXXXX"; + int fd; - tmp = tempnam(NULL, "pb-"); + fd = mkstemp(tmp); - if (!tmp) + if (fd < 0) return NULL; + close(fd); + ret = talloc_strdup(ctx, tmp); - free(tmp); return ret; } -/** - * 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. - */ -static char *load_nfs(void *ctx, struct pb_url *url) +static void load_url_result_cleanup_local(struct load_url_result *result) { - int result; - const char *argv[8]; - const char **p; - char *local; - char *opts; - - local = local_name(ctx); - - if (!local) - return NULL; - - result = pb_mkdir_recursive(local); - - if (result) - goto fail; - - opts = talloc_strdup(NULL, "ro,nolock,nodiratime"); + if (result->cleanup_local) + unlink(result->local); +} - if (url->port) - opts = talloc_asprintf_append(opts, ",port=%s", url->port); +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); + } - p = argv; - *p++ = pb_system_apps.mount; /* 1 */ - *p++ = "-t"; /* 2 */ - *p++ = "nfs"; /* 3 */ - *p++ = opts; /* 4 */ - *p++ = url->host; /* 5 */ - *p++ = url->dir; /* 6 */ - *p++ = local; /* 7 */ - *p++ = NULL; /* 8 */ + /* 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; - result = pb_run_cmd(argv, 1, 0); + cb(result, data); +} - talloc_free(opts); +static void load_process_to_local_file(struct load_task *task, + const char **argv, int argv_local_idx) +{ + int rc; - if (result) - goto fail; + task->result->local = local_name(task->result); + if (!task->result->local) { + task->result->status = LOAD_ERROR; + return; + } + task->result->cleanup_local = true; - local = talloc_asprintf_append(local, "/%s", url->path); - pb_log("%s: local '%s'\n", __func__, local); + if (argv_local_idx) + argv[argv_local_idx] = task->result->local; - return local; + task->process->argv = argv; + task->process->path = argv[0]; -fail: - pb_rmdir_recursive("/", local); - talloc_free(local); - return NULL; + 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) -{ - int result; - const char *argv[4]; - const char **p; - char *local; - local = local_name(ctx); +static void load_nfs(struct load_task *task) +{ + char *mountpoint, *opts; + int rc; + const char *argv[] = { + pb_system_apps.mount, + "-t", "nfs", + NULL, /* 3: opts */ + task->url->host, + task->url->dir, + NULL, /* 6: mountpoint */ + NULL, + }; + + 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; - if (!local) - return NULL; + opts = talloc_strdup(NULL, "ro,nolock,nodiratime"); + argv[3] = opts; - p = argv; - *p++ = pb_system_apps.sftp; /* 1 */ - *p++ = talloc_asprintf(local, "%s:%s", url->host, url->path); /* 2 */ - *p++ = local; /* 3 */ - *p++ = NULL; /* 4 */ + if (task->url->port) + opts = talloc_asprintf_append(opts, ",port=%s", + task->url->port); - result = pb_run_cmd(argv, 1, 0); + task->result->local = talloc_asprintf(task->result, "%s/%s", + mountpoint, + task->url->path); - if (result) - goto fail; + task->process->path = pb_system_apps.mount; + task->process->argv = argv; - return local; + 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); + task->result->status = rc ? LOAD_ERROR : LOAD_OK; + process_release(task->process); + task->process = NULL; + } -fail: - talloc_free(local); - return NULL; + talloc_free(opts); } -/** - * 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 void load_sftp(struct load_task *task) +{ + const char *argv[] = { + pb_system_apps.sftp, + NULL, /* 1: host:path */ + NULL, /* 2: local file */ + NULL, + }; + + argv[1] = talloc_asprintf(task, "%s:%s", + task->url->host, task->url->path); + load_process_to_local_file(task, argv, 2); +} -static char *load_tftp(void *ctx, struct pb_url *url) +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; + + process = process_create(ctx); + process->path = pb_system_apps.tftp; + process->argv = argv; + process->keep_stdout = true; + process->add_stderr = true; + process_run_sync(process); + + if (!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); + } else { + pb_log("Unknown TFTP client type!\n"); + type = TFTP_TYPE_BROKEN; + } - if (!local) - return NULL; + process_release(process); + return type; +} - /* 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 */ - - result = pb_run_cmd(argv, 1, 0); - - 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 */ - - result = pb_run_cmd(argv, 1, 0); - - if (!result) - return local; - - talloc_free(local); - return NULL; +static void load_tftp(struct load_task *task) +{ + const char *port = "69"; + const char *argv[10] = { + pb_system_apps.tftp, + }; + + 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 { @@ -339,94 +304,142 @@ enum wget_flags { * or NULL on error. */ -static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags) +static void load_wget(struct load_task *task, int flags) { - int result; - const char *argv[7]; - const char **p; - char *local; - - local = local_name(ctx); - - if (!local) - return NULL; + 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; - p = argv; - *p++ = pb_system_apps.wget; /* 1 */ + i = 3; #if !defined(DEBUG) - *p++ = "--quiet"; /* 2 */ + argv[i++] = "--quiet"; #endif - *p++ = "-O"; /* 3 */ - *p++ = local; /* 4 */ - *p++ = url->full; /* 5 */ if (flags & wget_no_check_certificate) - *p++ = "--no-check-certificate"; /* 6 */ - *p++ = NULL; /* 7 */ + argv[i++] = "--no-check-certificate"; - result = pb_run_cmd(argv, 1, 0); + argv[i] = task->url->full; - if (result) - goto fail; + load_process_to_local_file(task, argv, 2); +} - return local; +/* 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) +{ + int rc; -fail: - talloc_free(local); - return NULL; + rc = access(task->url->path, F_OK); + if (rc) { + task->result->status = LOAD_ERROR; + } else { + task->result->local = talloc_strdup(task->result, + task->url->path); + task->result->status = LOAD_OK; + } } /** - * pb_load_file - Loads a (possibly) remote file and returns the local file + * load_url - Loads a (possibly) remote URL and returns the local file * path. * @ctx: The talloc context to associate with the returned string. - * @remote: The remote file URL. + * @url: The remote file URL. * @tempfile: An optional variable pointer to be set when a temporary local * file is created. + * @url_cb: An optional callback pointer if the caller wants to load url + * asynchronously. * * Returns the local file path in a talloc'ed character string on success, * or NULL on error. */ -char *load_file(void *ctx, const char *remote, unsigned int *tempfile) +struct load_url_result *load_url_async(void *ctx, struct pb_url *url, + load_url_complete async_cb, void *async_data) { - struct pb_url *url = pb_url_parse(ctx, remote); - char *local; - int tmp = 0; + struct load_url_result *result; + struct load_task *task; if (!url) return 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->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; + } + switch (url->scheme) { case pb_url_ftp: case pb_url_http: - local = load_wget(ctx, url, 0); - tmp = !!local; + load_wget(task, 0); break; case pb_url_https: - local = load_wget(ctx, url, wget_no_check_certificate); - tmp = !!local; + load_wget(task, wget_no_check_certificate); break; case pb_url_nfs: - local = load_nfs(ctx, url); - tmp = !!local; + load_nfs(task); break; case pb_url_sftp: - local = load_sftp(ctx, url); - tmp = !!local; + load_sftp(task); break; case pb_url_tftp: - local = load_tftp(ctx, url); - 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) + talloc_free(task); + + return result; +} + +struct load_url_result *load_url(void *ctx, struct pb_url *url) +{ + return load_url_async(ctx, url, 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); - talloc_free(url); - return local; + res->status = LOAD_CANCELLED; + process_stop_async(task->process); }