]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
test/parser: Allow checks for NULL boot_args
[petitboot] / discover / parser.c
1
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6
7 #include "types/types.h"
8 #include <log/log.h>
9 #include <talloc/talloc.h>
10
11 #include "device-handler.h"
12 #include "parser.h"
13 #include "parser-utils.h"
14 #include "paths.h"
15 #include "file.h"
16
17 struct p_item {
18         struct list_item list;
19         struct parser *parser;
20 };
21
22 STATIC_LIST(parsers);
23
24 static char *local_path(struct discover_context *ctx,
25                 const char *filename)
26 {
27         return join_paths(ctx, ctx->device->mount_path, filename);
28 }
29
30 static int download_config(struct discover_context *ctx, char **buf, int *len)
31 {
32         unsigned tempfile;
33         const char *file;
34         int rc;
35
36         file = load_url(ctx, ctx->conf_url, &tempfile);
37         if (!file)
38                 return -1;
39
40         rc = read_file(ctx, file, buf, len);
41         if (rc)
42                 goto out_clean;
43
44         return 0;
45
46 out_clean:
47         if (tempfile)
48                 unlink(file);
49         return -1;
50 }
51
52 static void iterate_parser_files(struct discover_context *ctx,
53                 const struct parser *parser)
54 {
55         const char * const *filename;
56         const char *path;
57
58         if (!parser->filenames)
59                 return;
60
61         for (filename = parser->filenames; *filename; filename++) {
62                 int rc, len;
63                 char *buf;
64
65                 path = local_path(ctx, *filename);
66                 if (!path)
67                         continue;
68
69                 rc = read_file(ctx, path, &buf, &len);
70                 if (!rc) {
71                         pb_log("Running parser %s on file %s\n",
72                                         parser->name, *filename);
73                         parser->parse(ctx, buf, len);
74                         talloc_free(buf);
75                 }
76         }
77 }
78
79 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
80 {
81         struct p_item* i;
82         int rc, len;
83         char *buf;
84
85         pb_log("trying parsers for %s\n", ctx->device->device->id);
86
87         switch (method) {
88         case CONF_METHOD_LOCAL_FILE:
89                 list_for_each_entry(&parsers, i, list) {
90                         if (i->parser->method != CONF_METHOD_LOCAL_FILE)
91                                 continue;
92
93                         pb_log("\ttrying parser '%s'\n", i->parser->name);
94                         ctx->parser = i->parser;
95                         iterate_parser_files(ctx, ctx->parser);
96                 }
97                 ctx->parser = NULL;
98                 break;
99
100         case CONF_METHOD_DHCP:
101                 rc = download_config(ctx, &buf, &len);
102                 if (rc) {
103                         pb_log("\tdownload failed, aborting\n");
104                         return;
105                 }
106
107                 list_for_each_entry(&parsers, i, list) {
108                         if (i->parser->method != method)
109                                 continue;
110
111                         pb_log("\ttrying parser '%s'\n", i->parser->name);
112                         ctx->parser = i->parser;
113                         i->parser->parse(ctx, buf, len);
114                 }
115
116                 break;
117
118         case CONF_METHOD_UNKNOWN:
119                 break;
120
121         }
122 }
123
124 void __register_parser(struct parser *parser)
125 {
126         struct p_item* i = talloc(NULL, struct p_item);
127
128         printf("%s: %s\n", __func__, parser->name);
129
130         i->parser = parser;
131         list_add(&parsers, &i->list);
132 }
133
134 void parser_init(void)
135 {
136 }