]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
discover/event-parser: correctly populate boot option resources
[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
16 static int n_parsers;
17 static struct parser **parsers;
18
19 static const int max_file_size = 1024 * 1024;
20
21 static int read_file(struct discover_context *ctx,
22                 const char *filename, char **bufp, int *lenp)
23 {
24         struct stat statbuf;
25         int rc, fd, i, len;
26         char *buf;
27
28         fd = open(filename, O_RDONLY);
29         if (fd < 0)
30                 return -1;
31
32         rc = fstat(fd, &statbuf);
33         if (rc < 0)
34                 goto err_close;
35
36         len = statbuf.st_size;
37         if (len > max_file_size)
38                 goto err_close;
39
40         buf = talloc_array(ctx, char, len);
41         if (!buf)
42                 goto err_close;
43
44         for (i = 0; i < len; i += rc) {
45                 rc = read(fd, buf + i, len - i);
46
47                 /* unexpected EOF: trim and return */
48                 if (rc == 0) {
49                         len = i;
50                         break;
51                 }
52
53                 if (rc < 0)
54                         goto err_free;
55
56         }
57
58         close(fd);
59         *bufp = buf;
60         *lenp = len;
61         return 0;
62
63 err_free:
64         talloc_free(buf);
65 err_close:
66         close(fd);
67         return -1;
68 }
69
70 static char *local_path(struct discover_context *ctx,
71                 const char *filename)
72 {
73         return join_paths(ctx, ctx->device->mount_path, filename);
74 }
75
76 static int download_config(struct discover_context *ctx, char **buf, int *len)
77 {
78         unsigned tempfile;
79         const char *file;
80         int rc;
81
82         file = load_url(ctx, ctx->conf_url, &tempfile);
83         if (!file)
84                 return -1;
85
86         rc = read_file(ctx, file, buf, len);
87         if (rc)
88                 goto out_clean;
89
90         return 0;
91
92 out_clean:
93         if (tempfile)
94                 unlink(file);
95         return -1;
96 }
97
98 static void iterate_parser_files(struct discover_context *ctx,
99                 const struct parser *parser)
100 {
101         const char * const *filename;
102         const char *path;
103
104         if (!parser->filenames)
105                 return;
106
107         for (filename = parser->filenames; *filename; filename++) {
108                 int rc, len;
109                 char *buf;
110
111                 path = local_path(ctx, *filename);
112                 if (!path)
113                         continue;
114
115                 rc = read_file(ctx, path, &buf, &len);
116                 if (!rc) {
117                         parser->parse(ctx, buf, len);
118                         talloc_free(buf);
119                 }
120         }
121 }
122
123 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
124 {
125         int rc, i, len;
126         char *buf;
127
128         pb_log("trying parsers for %s\n", ctx->device->device->id);
129
130         switch (method) {
131         case CONF_METHOD_LOCAL_FILE:
132                 for (i = 0; i < n_parsers; i++) {
133                         if (parsers[i]->method != CONF_METHOD_LOCAL_FILE)
134                                 continue;
135
136                         pb_log("\ttrying parser '%s'\n", parsers[i]->name);
137                         ctx->parser = parsers[i];
138                         iterate_parser_files(ctx, ctx->parser);
139                 }
140                 ctx->parser = NULL;
141                 break;
142
143         case CONF_METHOD_DHCP:
144                 rc = download_config(ctx, &buf, &len);
145                 if (rc) {
146                         pb_log("\tdownload failed, aborting\n");
147                         return;
148                 }
149
150                 for (i = 0; i < n_parsers; i++) {
151                         if (parsers[i]->method != method)
152                                 continue;
153
154                         pb_log("\ttrying parser '%s'\n", parsers[i]->name);
155                         ctx->parser = parsers[i];
156                         parsers[i]->parse(ctx, buf, len);
157                 }
158
159                 break;
160
161         case CONF_METHOD_UNKNOWN:
162                 break;
163
164         }
165 }
166
167 void __register_parser(struct parser *parser)
168 {
169         parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
170         parsers[n_parsers] = parser;
171         n_parsers++;
172 }
173
174 void parser_init(void)
175 {
176 }