X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fparser.c;h=3153a657c7528f729e3341972f02fae91783496f;hp=c04a0afca610b31bfb07468824770445cfc94d92;hb=a5db3ced87f5f24500f785b70af8b8630bc1c077;hpb=5e7c90eddd7ac2e4a3b05a7a5f6e29166edfa161 diff --git a/discover/parser.c b/discover/parser.c index c04a0af..3153a65 100644 --- a/discover/parser.c +++ b/discover/parser.c @@ -37,7 +37,7 @@ static int read_file(struct discover_context *ctx, if (len > max_file_size) goto err_close; - buf = talloc_array(ctx, char, len); + buf = talloc_array(ctx, char, len + 1); if (!buf) goto err_close; @@ -55,6 +55,8 @@ static int read_file(struct discover_context *ctx, } + buf[len] = '\0'; + close(fd); *bufp = buf; *lenp = len; @@ -73,6 +75,28 @@ static char *local_path(struct discover_context *ctx, return join_paths(ctx, ctx->device->mount_path, filename); } +static int download_config(struct discover_context *ctx, char **buf, int *len) +{ + unsigned tempfile; + const char *file; + int rc; + + file = load_url(ctx, ctx->conf_url, &tempfile); + if (!file) + return -1; + + rc = read_file(ctx, file, buf, len); + if (rc) + goto out_clean; + + return 0; + +out_clean: + if (tempfile) + unlink(file); + return -1; +} + static void iterate_parser_files(struct discover_context *ctx, const struct parser *parser) { @@ -92,6 +116,8 @@ static void iterate_parser_files(struct discover_context *ctx, rc = read_file(ctx, path, &buf, &len); if (!rc) { + pb_log("Running parser %s on file %s\n", + parser->name, *filename); parser->parse(ctx, buf, len); talloc_free(buf); } @@ -100,11 +126,13 @@ static void iterate_parser_files(struct discover_context *ctx, void iterate_parsers(struct discover_context *ctx, enum conf_method method) { - int i; + int rc, i, len; + char *buf; pb_log("trying parsers for %s\n", ctx->device->device->id); - if (method == CONF_METHOD_LOCAL_FILE) { + switch (method) { + case CONF_METHOD_LOCAL_FILE: for (i = 0; i < n_parsers; i++) { if (parsers[i]->method != CONF_METHOD_LOCAL_FILE) continue; @@ -114,6 +142,29 @@ void iterate_parsers(struct discover_context *ctx, enum conf_method method) iterate_parser_files(ctx, ctx->parser); } ctx->parser = NULL; + break; + + case CONF_METHOD_DHCP: + rc = download_config(ctx, &buf, &len); + if (rc) { + pb_log("\tdownload failed, aborting\n"); + return; + } + + for (i = 0; i < n_parsers; i++) { + if (parsers[i]->method != method) + continue; + + pb_log("\ttrying parser '%s'\n", parsers[i]->name); + ctx->parser = parsers[i]; + parsers[i]->parse(ctx, buf, len); + } + + break; + + case CONF_METHOD_UNKNOWN: + break; + } }