summaryrefslogtreecommitdiffstats
path: root/discover/user-event.c
blob: 4b56de48177c0561f6f78b223e52599c7879cb5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 *  Copyright (C) 2009 Sony Computer Entertainment Inc.
 *  Copyright 2009 Sony Corp.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include <log/log.h>
#include <url/url.h>
#include <types/types.h>
#include <talloc/talloc.h>
#include <waiter/waiter.h>

#include "device-handler.h"
#include "resource.h"
#include "event.h"
#include "user-event.h"


struct user_event {
	struct device_handler *handler;
	int socket;
};

static const char *event_action_name(enum event_action action)
{
	switch (action) {
	case EVENT_ACTION_ADD:
		return "add";
	case EVENT_ACTION_REMOVE:
		return "remove";
	case EVENT_ACTION_CONF:
		return "conf";
	default:
		break;
	}

	return "unknown";
}

static void user_event_print_event(struct event __attribute__((unused)) *event)
{
	int i;

	pb_debug("user_event %s event:\n", event_action_name(event->action));
	pb_debug("\tdevice: %s\n", event->device);

	for (i = 0; i < event->n_params; i++)
		pb_debug("\t%-12s => %s\n",
			event->params[i].name, event->params[i].value);
}

static enum conf_method parse_conf_method(const char *str)
{

	if (!strcasecmp(str, "dhcp")) {
		return CONF_METHOD_DHCP;
	}
	return CONF_METHOD_UNKNOWN;
}

static struct resource *user_event_resource(struct discover_boot_option *opt,
		struct event *event, const char *param_name)
{
	struct resource *res;
	struct pb_url *url;
	const char *val;

	val = event_get_param(event, param_name);
	if (!val)
		return NULL;

	url = pb_url_parse(opt, val);
	if (!url)
		return NULL;

	res = create_url_resource(opt, url);
	if (!res) {
		talloc_free(url);
		return NULL;
	}

	return res;
}

static int parse_user_event(struct discover_context *ctx, struct event *event)
{
	struct discover_boot_option *d_opt;
	struct boot_option *opt;
	struct device *dev;
	const char *p;

	dev = ctx->device->device;

	d_opt = discover_boot_option_create(ctx, ctx->device);
	opt = d_opt->option;

	if (!d_opt)
		goto fail;

	p = event_get_param(event, "name");

	if (!p) {
		pb_log("%s: no name found\n", __func__);
		goto fail;
	}

	opt->id = talloc_asprintf(opt, "%s#%s", dev->id, p);
	opt->name = talloc_strdup(opt, p);

	d_opt->boot_image = user_event_resource(d_opt, event, "image");
	if (!d_opt->boot_image) {
		pb_log("%s: no boot image found for %s!\n", __func__,
				opt->name);
		goto fail;
	}

	d_opt->initrd = user_event_resource(d_opt, event, "initrd");

	p = event_get_param(event, "args");

	if (p)
		opt->boot_args = talloc_strdup(opt, p);

	opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
		opt->boot_args ? : "");

	if (event_get_param(event, "default"))
		opt->is_default = true;

	discover_context_add_boot_option(ctx, d_opt);

	return 0;

fail:
	talloc_free(d_opt);
	return -1;
}

static int user_event_conf(struct user_event *uev, struct event *event)
{
	struct device_handler *handler = uev->handler;
	struct discover_device *dev;
	enum conf_method method;
	struct pb_url *url;
	const char *val;

	val = event_get_param(event, "url");
	if (!val)
		return 0;

	url = pb_url_parse(event, val);
	if (!url)
		return 0;

	val = event_get_param(event, "method");
	if (!val)
		return 0;

	method = parse_conf_method(val);
	if (method == CONF_METHOD_UNKNOWN)
		return 0;

	dev = discover_device_create(handler, event->device);

	device_handler_conf(handler, dev, url, method);

	return 0;
}

static int user_event_add(struct user_event *uev, struct event *event)
{
	struct device_handler *handler = uev->handler;
	struct discover_context *ctx;
	struct discover_device *dev;

	dev = discover_device_create(handler, event->device);
	ctx = device_handler_discover_context_create(handler, dev);

	parse_user_event(ctx, event);

	device_handler_discover_context_commit(handler, ctx);

	talloc_free(ctx);

	return 0;
}

static int user_event_remove(struct user_event *uev, struct event *event)
{
	struct device_handler *handler = uev->handler;
	struct discover_device *dev;

	dev = device_lookup_by_id(handler, event->device);
	if (!dev)
		return 0;

	device_handler_remove(handler, dev);

	return 0;
}

static void user_event_handle_message(struct user_event *uev, char *buf,
	int len)
{
	int result;
	struct event *event;

	event = talloc(uev, struct event);
	event->type = EVENT_TYPE_USER;

	result = event_parse_ad_message(event, buf, len);

	if (result)
		return;

	user_event_print_event(event);

	switch (event->action) {
	case EVENT_ACTION_ADD:
		result = user_event_add(uev, event);
		break;
	case EVENT_ACTION_REMOVE:
		result = user_event_remove(uev, event);
		break;
	case EVENT_ACTION_CONF:
		result = user_event_conf(uev, event);
		break;
	default:
		break;
	}

	talloc_free(event);

	return;
}

static int user_event_process(void *arg)
{
	struct user_event *uev = arg;
	char buf[PBOOT_USER_EVENT_SIZE];
	int len;

	len = recvfrom(uev->socket, buf, sizeof(buf), 0, NULL, NULL);

	if (len < 0) {
		pb_log("%s: socket read failed: %s", __func__, strerror(errno));
		return 0;
	}

	if (len == 0) {
		pb_log("%s: empty", __func__);
		return 0;
	}

	pb_debug("%s: %u bytes\n", __func__, len);

	user_event_handle_message(uev, buf, len);

	return 0;
}

static int user_event_destructor(void *arg)
{
	struct user_event *uev = arg;

	pb_debug("%s\n", __func__);

	if (uev->socket >= 0)
		close(uev->socket);

	return 0;
}

struct user_event *user_event_init(struct waitset *waitset,
		struct device_handler *handler)
{
	struct sockaddr_un addr;
	struct user_event *uev;

	unlink(PBOOT_USER_EVENT_SOCKET);

	uev = talloc(NULL, struct user_event);

	uev->handler = handler;

	uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (uev->socket < 0) {
		pb_log("%s: Error creating event handler socket: %s\n",
			__func__, strerror(errno));
		goto out_err;
	}

	talloc_set_destructor(uev, user_event_destructor);

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);

	if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
		pb_log("Error binding event handler socket: %s\n",
			strerror(errno));
	}

	waiter_register_io(waitset, uev->socket, WAIT_IN,
			user_event_process, uev);

	pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);

	return uev;

out_err:
	talloc_free(uev);
	return NULL;
}

void user_event_destroy(struct user_event *uev)
{
	talloc_free(uev);
}
OpenPOWER on IntegriCloud