X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fpaths.c;h=82a82b15125c7b2317d8d95d54fc784783af8d01;hp=72d07b20c4cee09b354510558e4558e405e4b828;hb=ca5a62c1d04a3eea1d1c307d4fa4f0b1559140d2;hpb=5eb7f7bcd3431cd8f634b02b71bd78f6162c2af3 diff --git a/discover/paths.c b/discover/paths.c index 72d07b2..82a82b1 100644 --- a/discover/paths.c +++ b/discover/paths.c @@ -5,151 +5,428 @@ #include #include +#include +#include +#include +#include #include "paths.h" #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt") -struct mount_map { - char *dev, *mnt; +struct load_url_async_data { + load_url_callback url_cb; + void *ctx; }; -static struct mount_map *mount_map; -static int mount_map_size; - -static int is_prefix(const char *str, const char *prefix) +const char *mount_base(void) { - return !strncmp(str, prefix, strlen(prefix)); + return DEVICE_MOUNT_BASE; } -static int is_prefix_ignorecase(const char *str, const char *prefix) +char *join_paths(void *alloc_ctx, const char *a, const char *b) { - return !strncasecmp(str, prefix, strlen(prefix)); + char *full_path; + + full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2); + + strcpy(full_path, a); + if (b[0] != '/' && a[strlen(a) - 1] != '/') + strcat(full_path, "/"); + strcat(full_path, b); + + return full_path; } -const char *mount_base(void) + +static char *local_name(void *ctx) { - return DEVICE_MOUNT_BASE; + char *tmp, *ret; + + tmp = tempnam(NULL, "pb-"); + + if (!tmp) + return NULL; + + ret = talloc_strdup(ctx, tmp); + free(tmp); + + return ret; } -char *encode_label(void *alloc_ctx, const char *label) +static void load_url_exit_cb(struct process *process) { - char *str, *c; - int i; + struct load_url_async_data *url_data = process->data; - /* the label can be expanded by up to four times */ - str = talloc_size(alloc_ctx, strlen(label) * 4 + 1); - c = str; + pb_log("The download client '%s' [pid %d] exited, rc %d\n", + process->path, process->pid, process->exit_status); - for (i = 0; i < strlen(label); i++) { + url_data->url_cb(url_data->ctx, process->exit_status); - if (label[i] == '/' || label[i] == '\\') { - sprintf(c, "\\x%02x", label[i]); - c += 4; - continue; - } + process_release(process); +} - *(c++) = label[i]; +/** + * 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, + struct load_url_async_data *url_data) +{ + 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; + + result = pb_mkdir_recursive(local); + if (result) + goto fail; + + opts = talloc_strdup(NULL, "ro,nolock,nodiratime"); + argv[3] = opts; + + if (url->port) + opts = talloc_asprintf_append(opts, ",port=%s", url->port); + + if (url_data) { + process = process_create(ctx); + + process->path = pb_system_apps.mount; + process->argv = argv; + process->exit_cb = load_url_exit_cb; + process->data = url_data; + + result = process_run_async(process); + if (result) + process_release(process); + } else { + result = process_run_simple_argv(ctx, argv); } - *c = '\0'; + talloc_free(opts); - return str; + if (result) + goto fail; + + local = talloc_asprintf_append(local, "/%s", url->path); + pb_log("%s: local '%s'\n", __func__, local); + + return local; + +fail: + pb_rmdir_recursive("/", local); + talloc_free(local); + return NULL; } -char *parse_device_path(void *alloc_ctx, - const char *dev_str, const char *cur_dev) +/** + * 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. + */ +static char *load_sftp(void *ctx, struct pb_url *url, + struct load_url_async_data *url_data) { - char *dev, tmp[256], *enc; - - if (is_prefix_ignorecase(dev_str, "uuid=")) { - dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-uuid/%s", - dev_str + strlen("uuid=")); - return dev; + char *host_path, *local; + int result; + struct process *process; + const char *argv[] = { + pb_system_apps.sftp, + NULL, + NULL, + NULL, + }; + + local = local_name(ctx); + if (!local) + return NULL; + argv[2] = local; + + host_path = talloc_asprintf(local, "%s:%s", url->host, url->path); + argv[1] = host_path; + + if (url_data) { + process = process_create(ctx); + + process->path = pb_system_apps.sftp; + process->argv = argv; + process->exit_cb = load_url_exit_cb; + process->data = url_data; + + result = process_run_async(process); + if (result) + process_release(process); + } else { + result = process_run_simple_argv(ctx, argv); } - 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; - } + if (result) + goto fail; - /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand - * back before returning. - */ - if (is_prefix(dev_str, "/dev/")) - dev_str += strlen("/dev/"); - - /* PS3 hack: if we're reading from a ps3dx device, and we refer to - * a sdx device, remap to ps3dx */ - if (cur_dev && is_prefix(cur_dev, "/dev/ps3d") - && is_prefix(dev_str, "sd")) { - snprintf(tmp, 255, "ps3d%s", dev_str + 2); - dev_str = tmp; + return local; + +fail: + talloc_free(local); + return NULL; +} + +static enum tftp_type check_tftp_type(void *ctx) +{ + 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_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; + + } else { + pb_log("Unknown TFTP client type!\n"); + type = TFTP_TYPE_BROKEN; } - return join_paths(alloc_ctx, "/dev", dev_str); + process_release(process); + return type; } -const char *mountpoint_for_device(const char *dev) +/** + * 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) { - int i; + int result; + const char *argv[10]; + const char **p; + char *local; + struct process *process; + + if (tftp_type == TFTP_TYPE_UNKNOWN) + tftp_type = check_tftp_type(ctx); + + if (tftp_type == TFTP_TYPE_BROKEN) + return NULL; + + local = local_name(ctx); + if (!local) + return NULL; + + if (tftp_type == TFTP_TYPE_BUSYBOX) { + /* 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 */ + } else { + 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 = process_create(ctx); - if (is_prefix(dev, "/dev/")) - dev += strlen("/dev/"); + process->path = pb_system_apps.tftp; + process->argv = argv; + process->exit_cb = load_url_exit_cb; + process->data = url_data; - /* 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; + result = process_run_async(process); + } else { + result = process_run_simple_argv(ctx, argv); + } - /* no existing entry, create a new one */ - i = mount_map_size++; - mount_map = talloc_realloc(NULL, mount_map, - struct mount_map, mount_map_size); + if (!result) + return local; - 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; + talloc_free(local); + return NULL; } -char *resolve_path(void *alloc_ctx, const char *path, const char *current_dev) -{ - char *ret; - const char *devpath, *sep; +enum wget_flags { + wget_empty = 0, + wget_no_check_certificate = 1, +}; - sep = strchr(path, ':'); - if (!sep) { - devpath = mountpoint_for_device(current_dev); - ret = join_paths(alloc_ctx, devpath, path); +/** + * pb_load_wget - Loads a remote file via wget 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_wget(void *ctx, struct pb_url *url, enum wget_flags flags, + struct load_url_async_data *url_data) +{ + int result; + const char *argv[7]; + const char **p; + char *local; + struct process *process; + + local = local_name(ctx); + + if (!local) + return NULL; + + p = argv; + *p++ = pb_system_apps.wget; /* 1 */ +#if !defined(DEBUG) + *p++ = "--quiet"; /* 2 */ +#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 */ + + if (url_data) { + process = process_create(ctx); + + process->path = pb_system_apps.wget; + process->argv = argv; + process->exit_cb = load_url_exit_cb; + process->data = url_data; + + result = process_run_async(process); + if (result) + process_release(process); } 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); + result = process_run_simple_argv(ctx, argv); + } - devpath = mountpoint_for_device(dev); - ret = join_paths(alloc_ctx, devpath, sep + 1); + if (result) + goto fail; - talloc_free(dev); - talloc_free(tmp); - } + return local; - return ret; +fail: + talloc_free(local); + return NULL; } -char *join_paths(void *alloc_ctx, const char *a, const char *b) +/** + * load_url - Loads a (possibly) remote URL and returns the local file + * path. + * @ctx: The talloc context to associate with the returned string. + * @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_url_async(void *ctx, struct pb_url *url, unsigned int *tempfile, + load_url_callback url_cb) { - char *full_path; + char *local; + int tmp = 0; + struct load_url_async_data *url_data; - full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2); + if (!url) + return NULL; - strcpy(full_path, a); - if (b[0] != '/' && a[strlen(a) - 1] != '/') - strcat(full_path, "/"); - strcat(full_path, b); + url_data = NULL; - return full_path; + if (url_cb) { + url_data = talloc_zero(ctx, struct load_url_async_data); + url_data->url_cb = url_cb; + url_data->ctx = ctx; + } + + switch (url->scheme) { + case pb_url_ftp: + case pb_url_http: + local = load_wget(ctx, url, 0, url_data); + tmp = !!local; + break; + case pb_url_https: + local = load_wget(ctx, url, wget_no_check_certificate, + url_data); + tmp = !!local; + break; + case pb_url_nfs: + local = load_nfs(ctx, url, url_data); + tmp = !!local; + break; + case pb_url_sftp: + local = load_sftp(ctx, url, url_data); + tmp = !!local; + break; + case pb_url_tftp: + local = load_tftp(ctx, url, url_data); + tmp = !!local; + break; + default: + local = talloc_strdup(ctx, url->full); + tmp = 0; + break; + } + + if (tempfile) + *tempfile = tmp; + + return local; } +char *load_url(void *ctx, struct pb_url *url, unsigned int *tempfile) +{ + return load_url_async(ctx, url, tempfile, NULL); +}