]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
test/parser: Add ubuntu grub tests
[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 + 1);
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         buf[len] = '\0';
59
60         close(fd);
61         *bufp = buf;
62         *lenp = len;
63         return 0;
64
65 err_free:
66         talloc_free(buf);
67 err_close:
68         close(fd);
69         return -1;
70 }
71
72 static char *local_path(struct discover_context *ctx,
73                 const char *filename)
74 {
75         return join_paths(ctx, ctx->device->mount_path, filename);
76 }
77
78 static int download_config(struct discover_context *ctx, char **buf, int *len)
79 {
80         unsigned tempfile;
81         const char *file;
82         int rc;
83
84         file = load_url(ctx, ctx->conf_url, &tempfile);
85         if (!file)
86                 return -1;
87
88         rc = read_file(ctx, file, buf, len);
89         if (rc)
90                 goto out_clean;
91
92         return 0;
93
94 out_clean:
95         if (tempfile)
96                 unlink(file);
97         return -1;
98 }
99
100 static void iterate_parser_files(struct discover_context *ctx,
101                 const struct parser *parser)
102 {
103         const char * const *filename;
104         const char *path;
105
106         if (!parser->filenames)
107                 return;
108
109         for (filename = parser->filenames; *filename; filename++) {
110                 int rc, len;
111                 char *buf;
112
113                 path = local_path(ctx, *filename);
114                 if (!path)
115                         continue;
116
117                 rc = read_file(ctx, path, &buf, &len);
118                 if (!rc) {
119                         pb_log("Running parser %s on file %s\n",
120                                         parser->name, *filename);
121                         parser->parse(ctx, buf, len);
122                         talloc_free(buf);
123                 }
124         }
125 }
126
127 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
128 {
129         int rc, i, len;
130         char *buf;
131
132         pb_log("trying parsers for %s\n", ctx->device->device->id);
133
134         switch (method) {
135         case CONF_METHOD_LOCAL_FILE:
136                 for (i = 0; i < n_parsers; i++) {
137                         if (parsers[i]->method != CONF_METHOD_LOCAL_FILE)
138                                 continue;
139
140                         pb_log("\ttrying parser '%s'\n", parsers[i]->name);
141                         ctx->parser = parsers[i];
142                         iterate_parser_files(ctx, ctx->parser);
143                 }
144                 ctx->parser = NULL;
145                 break;
146
147         case CONF_METHOD_DHCP:
148                 rc = download_config(ctx, &buf, &len);
149                 if (rc) {
150                         pb_log("\tdownload failed, aborting\n");
151                         return;
152                 }
153
154                 for (i = 0; i < n_parsers; i++) {
155                         if (parsers[i]->method != method)
156                                 continue;
157
158                         pb_log("\ttrying parser '%s'\n", parsers[i]->name);
159                         ctx->parser = parsers[i];
160                         parsers[i]->parse(ctx, buf, len);
161                 }
162
163                 break;
164
165         case CONF_METHOD_UNKNOWN:
166                 break;
167
168         }
169 }
170
171 void __register_parser(struct parser *parser)
172 {
173         parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
174         parsers[n_parsers] = parser;
175         n_parsers++;
176 }
177
178 void parser_init(void)
179 {
180 }