]> git.ozlabs.org Git - petitboot/blob - discover/event.c
lib/process: Don't abort stdout reads on EINTR
[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, "conf"))
51                 *action = EVENT_ACTION_CONF;
52         else if (streq(buf, "dhcp"))
53                 *action = EVENT_ACTION_DHCP;
54         else {
55                 pb_log("%s: unknown action: %s\n", __func__, buf);
56                 return -1;
57         }
58
59         if (!*(sep + 1)) {
60                 pb_log("%s: bad device: %s\n", __func__, buf);
61                 return -1;
62         }
63
64         *device = sep + 1;
65         return headerlen;
66 }
67
68 /**
69  * event_parse_params - Parse a <name>=<value> buffer.
70  *
71  * The buffer is not modified.
72  */
73
74 static void event_parse_params(struct event *event, const char *buf, int len)
75 {
76         int param_len, name_len, value_len;
77         struct param *param;
78         char *sep;
79
80         for (; len > 0; len -= param_len + 1, buf += param_len + 1) {
81
82                 /* find the length of the whole parameter */
83                 param_len = strnlen(buf, len);
84                 if (!param_len) {
85                         /* multiple NULs? skip over */
86                         param_len = 1;
87                         continue;
88                 }
89
90                 /* update the params array */
91                 event->params = talloc_realloc(event, event->params,
92                                         struct param, ++event->n_params);
93                 param = &event->params[event->n_params - 1];
94
95                 sep = memchr(buf, '=', param_len);
96                 if (!sep) {
97                         name_len = param_len;
98                         value_len = 0;
99                         param->value = "";
100                 } else {
101                         name_len = sep - buf;
102                         value_len = param_len - name_len - 1;
103                         param->value = talloc_strndup(event, sep + 1,
104                                         value_len);
105                 }
106                 param->name = talloc_strndup(event, buf, name_len);
107         }
108 }
109
110 int event_parse_ad_message(struct event *event, char *buf, int len)
111 {
112         enum event_action action;
113         int headerlen;
114         char *device;
115
116         headerlen = event_parse_ad_header(buf, len, &action, &device);
117
118         if (headerlen <= 0)
119                 return -1;
120
121         /* now we have an action and a device, we can construct the event */
122         event->action = action;
123         event->device = talloc_strdup(event, device);
124         event->n_params = 0;
125         event->params = NULL;
126
127         len -= headerlen + 1;
128         buf += headerlen + 1;
129         event_parse_params(event, buf, len);
130
131         return 0;
132 }
133
134 const char *event_get_param(const struct event *event, const char *name)
135 {
136         int i;
137
138         for (i = 0; i < event->n_params; i++)
139                 if (!strcasecmp(event->params[i].name, name))
140                         return event->params[i].value;
141
142         return NULL;
143 }
144
145 void event_set_param(struct event *event, const char *name, const char *value)
146 {
147         struct param *param;
148         int i;
149
150         /* if it's already present, replace the value of the old param */
151         for (i = 0; i < event->n_params; i++) {
152                 param = &event->params[i];
153                 if (!strcasecmp(param->name, name)) {
154                         talloc_free(param->value);
155                         param->value = talloc_strdup(event, value);
156                         return;
157                 }
158         }
159
160         /* not found - create a new param */
161         event->params = talloc_realloc(event, event->params,
162                                 struct param, ++event->n_params);
163         param = &event->params[event->n_params - 1];
164
165         param->name = talloc_strdup(event, name);
166         param->value = talloc_strdup(event, value);
167 }