]> git.ozlabs.org Git - petitboot/blob - discover/event.c
discover/platform: Add finalise_config_hook
[petitboot] / discover / event.c
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #include <string.h>
6
7 #include <log/log.h>
8 #include <talloc/talloc.h>
9
10 #include "event.h"
11
12 #define streq(a, b) (!strcasecmp((a), (b)))
13
14 /**
15  * event_parse_ad_header - Parse an <action>@<device> event header.
16  *
17  * The buffer is modified in place.
18  * Returns zero on success.
19  */
20
21 static int event_parse_ad_header(char *buf, int len, enum event_action *action,
22         char **device)
23 {
24         int headerlen;
25         char *sep;
26
27         *action = 0;
28         *device = NULL;
29         headerlen = strnlen(buf, len);
30
31         if (!headerlen) {
32                 pb_log("%s: bad header, no data\n", __func__);
33                 return -1;
34         }
35
36         /* we should see an <action>@<device>\0 at the head of the buffer */
37         sep = strchr(buf, '@');
38         if (!sep) {
39                 pb_log("%s: bad header: %s\n", __func__, buf);
40                 return -1;
41         }
42
43         /* terminate the action string */
44         *sep = '\0';
45
46         if (streq(buf, "add"))
47                 *action = EVENT_ACTION_ADD;
48         else if (streq(buf, "remove"))
49                 *action = EVENT_ACTION_REMOVE;
50         else if (streq(buf, "url"))
51                 *action = EVENT_ACTION_URL;
52         else if (streq(buf, "conf"))
53                 *action = EVENT_ACTION_CONF;
54         else if (streq(buf, "dhcp"))
55                 *action = EVENT_ACTION_DHCP;
56         else {
57                 pb_log("%s: unknown action: %s\n", __func__, buf);
58                 return -1;
59         }
60
61         if (!*(sep + 1)) {
62                 pb_log("%s: bad device: %s\n", __func__, buf);
63                 return -1;
64         }
65
66         *device = sep + 1;
67         return headerlen;
68 }
69
70 /**
71  * event_parse_params - Parse a <name>=<value> buffer.
72  *
73  * The buffer is not modified.
74  */
75
76 static void event_parse_params(struct event *event, const char *buf, int len)
77 {
78         int param_len, name_len, value_len;
79         struct param *param;
80         char *sep;
81
82         for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
83
84                 /* find the length of the whole parameter */
85                 param_len = strnlen(buf, len);
86                 if (!param_len) {
87                         /* multiple NULs? skip over */
88                         param_len = 1;
89                         continue;
90                 }
91
92                 /* update the params array */
93                 event->params = talloc_realloc(event, event->params,
94                                         struct param, ++event->n_params);
95                 param = &event->params[event->n_params - 1];
96
97                 sep = memchr(buf, '=', param_len);
98                 if (!sep) {
99                         name_len = param_len;
100                         value_len = 0;
101                         param->value = "";
102                 } else {
103                         name_len = sep - buf;
104                         value_len = param_len - name_len - 1;
105                         param->value = talloc_strndup(event, sep + 1,
106                                         value_len);
107                 }
108                 param->name = talloc_strndup(event, buf, name_len);
109         }
110 }
111
112 int event_parse_ad_message(struct event *event, char *buf, int len)
113 {
114         enum event_action action;
115         int headerlen;
116         char *device;
117
118         headerlen = event_parse_ad_header(buf, len, &action, &device);
119
120         if (headerlen <= 0)
121                 return -1;
122
123         /* now we have an action and a device, we can construct the event */
124         event->action = action;
125         event->device = talloc_strdup(event, device);
126         event->n_params = 0;
127         event->params = NULL;
128
129         len -= headerlen + 1;
130         buf += headerlen + 1;
131         event_parse_params(event, buf, len);
132
133         return 0;
134 }
135
136 const char *event_get_param(const struct event *event, const char *name)
137 {
138         int i;
139
140         for (i = 0; i < event->n_params; i++)
141                 if (!strcasecmp(event->params[i].name, name))
142                         return event->params[i].value;
143
144         return NULL;
145 }
146
147 void event_set_param(struct event *event, const char *name, const char *value)
148 {
149         struct param *param;
150         int i;
151
152         /* if it's already present, replace the value of the old param */
153         for (i = 0; i < event->n_params; i++) {
154                 param = &event->params[i];
155                 if (!strcasecmp(param->name, name)) {
156                         talloc_free(param->value);
157                         param->value = talloc_strdup(event, value);
158                         return;
159                 }
160         }
161
162         /* not found - create a new param */
163         event->params = talloc_realloc(event, event->params,
164                                 struct param, ++event->n_params);
165         param = &event->params[event->n_params - 1];
166
167         param->name = talloc_strdup(event, name);
168         param->value = talloc_strdup(event, value);
169 }