]> git.ozlabs.org Git - petitboot/blob - discover/event-parser.c
discover: Don't free URL in load_url
[petitboot] / discover / event-parser.c
1 #define _GNU_SOURCE
2
3 #include <assert.h>
4
5 #include "log/log.h"
6 #include "talloc/talloc.h"
7 #include "url/url.h"
8
9 #include "resource.h"
10 #include "event.h"
11 #include "parser-utils.h"
12 #include "device-handler.h"
13
14 static struct resource *user_event_resource(struct discover_boot_option *opt,
15                 struct event *event, const char *param_name)
16 {
17         struct resource *res;
18         struct pb_url *url;
19         const char *val;
20
21         val = event_get_param(event, param_name);
22         if (!val)
23                 return NULL;
24
25         url = pb_url_parse(opt, val);
26         if (!url)
27                 return NULL;
28
29         res = create_url_resource(opt, url);
30         if (!res) {
31                 talloc_free(url);
32                 return NULL;
33         }
34
35         return res;
36 }
37
38 /**
39  * parse_user_event - Parse a user event.
40  *
41  * Understands params: name, image, args.
42  */
43
44 int parse_user_event(struct discover_context *ctx, struct event *event)
45 {
46         struct discover_boot_option *d_opt;
47         struct boot_option *opt;
48         struct device *dev;
49         const char *p;
50
51         dev = ctx->device->device;
52
53         d_opt = discover_boot_option_create(ctx, ctx->device);
54         opt = d_opt->option;
55
56         if (!d_opt)
57                 goto fail;
58
59         p = event_get_param(event, "name");
60
61         if (!p) {
62                 pb_log("%s: no name found\n", __func__);
63                 goto fail;
64         }
65
66         opt->id = talloc_asprintf(opt, "%s#%s", dev->id, p);
67         opt->name = talloc_strdup(opt, p);
68
69         d_opt->boot_image = user_event_resource(d_opt, event, "image");
70         if (!d_opt->boot_image) {
71                 pb_log("%s: no boot image found for %s!\n", __func__,
72                                 opt->name);
73                 goto fail;
74         }
75
76         d_opt->initrd = user_event_resource(d_opt, event, "initrd");
77
78         p = event_get_param(event, "args");
79
80         if (p)
81                 opt->boot_args = talloc_strdup(opt, p);
82
83         opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
84                 opt->boot_args ? : "");
85
86         if (event_get_param(event, "default"))
87                 opt->is_default = true;
88
89         discover_context_add_boot_option(ctx, d_opt);
90
91         return 0;
92
93 fail:
94         talloc_free(d_opt);
95         return -1;
96 }