discover/parser: free parsers on exit
[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                 struct discover_device *dev,
26                 const char *filename)
27 {
28         return join_paths(ctx, dev->mount_path, filename);
29 }
30
31 int parser_request_file(struct discover_context *ctx,
32                 struct discover_device *dev, const char *filename,
33                 char **buf, int *len)
34
35 {
36         char *path;
37         int rc;
38
39         /* we only support local files at present */
40         if (!dev->mount_path)
41                 return -1;
42
43         path = local_path(ctx, dev, filename);
44
45         rc = read_file(ctx, path, buf, len);
46
47         talloc_free(path);
48
49         return rc;
50 }
51
52 int parser_replace_file(struct discover_context *ctx,
53                 struct discover_device *dev, const char *filename,
54                 char *buf, int len)
55 {
56         bool release;
57         char *path;
58         int rc;
59
60         if (!dev->mounted)
61                 return -1;
62
63         rc = device_request_write(dev, &release);
64         if (rc) {
65                 pb_log("Can't write file %s: device doesn't allow write\n",
66                                 dev->device_path);
67                 return -1;
68         }
69
70         path = local_path(ctx, dev, filename);
71
72         rc = replace_file(path, buf, len);
73
74         talloc_free(path);
75
76         device_release_write(dev, release);
77
78         return rc;
79 }
80
81 static int download_config(struct discover_context *ctx, char **buf, int *len)
82 {
83         struct load_url_result *result;
84         int rc;
85
86         result = load_url(ctx, ctx->conf_url);
87         if (!result)
88                 return -1;
89
90         rc = read_file(ctx, result->local, buf, len);
91         if (rc)
92                 goto out_clean;
93
94         return 0;
95
96 out_clean:
97         if (result->cleanup_local)
98                 unlink(result->local);
99         return -1;
100 }
101
102 static void iterate_parser_files(struct discover_context *ctx,
103                 const struct parser *parser)
104 {
105         const char * const *filename;
106         const char *path;
107
108         if (!parser->filenames)
109                 return;
110
111         for (filename = parser->filenames; *filename; filename++) {
112                 int rc, len;
113                 char *buf;
114
115                 path = local_path(ctx, ctx->device, *filename);
116                 if (!path)
117                         continue;
118
119                 rc = read_file(ctx, path, &buf, &len);
120                 if (!rc) {
121                         pb_log("Running parser %s on file %s\n",
122                                         parser->name, *filename);
123                         parser->parse(ctx, buf, len);
124                         talloc_free(buf);
125                 }
126         }
127 }
128
129 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
130 {
131         struct p_item* i;
132         int rc, len;
133         char *buf;
134
135         pb_log("trying parsers for %s\n", ctx->device->device->id);
136
137         switch (method) {
138         case CONF_METHOD_LOCAL_FILE:
139                 list_for_each_entry(&parsers, i, list) {
140                         if (i->parser->method != CONF_METHOD_LOCAL_FILE)
141                                 continue;
142
143                         pb_log("\ttrying parser '%s'\n", i->parser->name);
144                         ctx->parser = i->parser;
145                         iterate_parser_files(ctx, ctx->parser);
146                 }
147                 ctx->parser = NULL;
148                 break;
149
150         case CONF_METHOD_DHCP:
151                 rc = download_config(ctx, &buf, &len);
152                 if (rc) {
153                         pb_log("\tdownload failed, aborting\n");
154                         return;
155                 }
156
157                 list_for_each_entry(&parsers, i, list) {
158                         if (i->parser->method != method)
159                                 continue;
160
161                         pb_log("\ttrying parser '%s'\n", i->parser->name);
162                         ctx->parser = i->parser;
163                         i->parser->parse(ctx, buf, len);
164                 }
165
166                 break;
167
168         case CONF_METHOD_UNKNOWN:
169                 break;
170
171         }
172 }
173
174 static void *parsers_ctx;
175
176 void __register_parser(struct parser *parser)
177 {
178         struct p_item *i;
179
180         if (!parsers_ctx)
181                 parsers_ctx = talloc_new(NULL);
182
183         i = talloc(parsers_ctx, struct p_item);
184         i->parser = parser;
185         list_add(&parsers, &i->list);
186 }
187
188 static __attribute__((destructor)) void cleanup_parsers(void)
189 {
190         talloc_free(parsers_ctx);
191 }
192
193 void parser_init(void)
194 {
195 }