2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #if defined(HAVE_CONFIG_H)
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <types/types.h>
33 #include <talloc/talloc.h>
34 #include <waiter/waiter.h>
36 #include "device-handler.h"
39 #include "user-event.h"
42 #define MAC_ADDR_SIZE 6
43 #define IP_ADDR_SIZE 4
46 struct device_handler *handler;
50 static const char *event_action_name(enum event_action action)
53 case EVENT_ACTION_ADD:
55 case EVENT_ACTION_REMOVE:
57 case EVENT_ACTION_DHCP:
66 static void user_event_print_event(struct event __attribute__((unused)) *event)
70 pb_debug("user_event %s event:\n", event_action_name(event->action));
71 pb_debug("\tdevice: %s\n", event->device);
73 for (i = 0; i < event->n_params; i++)
74 pb_debug("\t%-12s => %s\n",
75 event->params[i].name, event->params[i].value);
78 static struct resource *user_event_resource(struct discover_boot_option *opt,
81 const char *siaddr, *boot_file;
86 siaddr = event_get_param(event, "siaddr");
88 pb_log("%s: next server option not found\n", __func__);
92 boot_file = event_get_param(event, "bootfile");
94 pb_log("%s: bootfile not found\n", __func__);
98 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, boot_file);
99 url = pb_url_parse(opt, url_str);
100 talloc_free(url_str);
105 res = create_url_resource(opt, url);
114 static int parse_user_event(struct discover_context *ctx, struct event *event)
116 struct discover_boot_option *d_opt;
117 char *server_ip, *root_dir, *p;
118 struct boot_option *opt;
122 dev = ctx->device->device;
124 d_opt = discover_boot_option_create(ctx, ctx->device);
130 val = event_get_param(event, "name");
133 pb_log("%s: no name found\n", __func__);
137 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
138 opt->name = talloc_strdup(opt, val);
140 d_opt->boot_image = user_event_resource(d_opt, event);
141 if (!d_opt->boot_image) {
142 pb_log("%s: no boot image found for %s!\n", __func__,
147 val = event_get_param(event, "rootpath");
149 server_ip = talloc_strdup(opt, val);
150 p = strchr(server_ip, ':');
152 root_dir = talloc_strdup(opt, p + 1);
154 opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
155 "root=/dev/nfs ip=any nfsroot=",
156 server_ip, root_dir);
158 talloc_free(root_dir);
160 opt->boot_args = talloc_asprintf(opt, "%s",
161 "root=/dev/nfs ip=any nfsroot=");
164 talloc_free(server_ip);
167 opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
168 opt->boot_args ? : "");
170 if (event_get_param(event, "default"))
171 opt->is_default = true;
173 discover_context_add_boot_option(ctx, d_opt);
183 static const char *parse_host_addr(struct event *event)
187 val = event_get_param(event, "tftp");
191 val = event_get_param(event, "siaddr");
195 val = event_get_param(event, "serverid");
202 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
204 unsigned int mac_addr_arr[MAC_ADDR_SIZE];
207 sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
208 mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
211 mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
212 mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
213 mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
218 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
220 unsigned int ip_addr_arr[IP_ADDR_SIZE];
223 sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
224 ip_addr_arr + 2, ip_addr_arr + 3);
226 ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
227 ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
232 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
233 struct event *event, bool *is_complete)
235 const char *conffile, *pathprefix, *host, *bootfile;
236 char *p, *basedir, *url_str;
239 conffile = event_get_param(event, "pxeconffile");
240 pathprefix = event_get_param(event, "pxepathprefix");
241 bootfile = event_get_param(event, "bootfile");
243 /* If we're given a conf file, we're able to generate a complete URL to
244 * the configuration file, and the parser doesn't need to do any
245 * further autodiscovery */
246 *is_complete = !!conffile;
248 /* if conffile is a URL, that's all we need */
249 if (conffile && is_url(conffile)) {
250 url = pb_url_parse(ctx, conffile);
254 /* If we can create a URL from pathprefix (optionally with
255 * conffile appended to create a complete URL), use that */
256 if (pathprefix && is_url(pathprefix)) {
258 url_str = talloc_asprintf(ctx, "%s%s",
259 pathprefix, conffile);
260 url = pb_url_parse(ctx, url_str);
261 talloc_free(url_str);
263 url = pb_url_parse(ctx, pathprefix);
269 host = parse_host_addr(event);
271 pb_log("%s: host address not found\n", __func__);
275 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
277 /* if we have a pathprefix, use that directly.. */
279 /* strip leading slashes */
280 while (pathprefix[0] == '/')
282 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
284 /* ... otherwise, add a path based on the bootfile name, but only
285 * if conffile isn't an absolute path itself */
286 } else if (bootfile && !(conffile && conffile[0] == '/')) {
288 basedir = talloc_strdup(ctx, bootfile);
290 /* strip filename from the bootfile path, leaving only a
292 p = strrchr(basedir, '/');
297 url_str = talloc_asprintf_append(url_str, "%s/",
300 talloc_free(basedir);
303 /* finally, append conffile */
305 url_str = talloc_asprintf_append(url_str, "%s", conffile);
307 url = pb_url_parse(ctx, url_str);
309 talloc_free(url_str);
314 char **user_event_parse_conf_filenames(
315 struct discover_context *ctx, struct event *event)
317 char *mac_addr, *ip_hex;
318 const char *mac, *ip;
322 mac = event_get_param(event, "mac");
324 mac_addr = parse_mac_addr(ctx, mac);
328 ip = event_get_param(event, "ip");
330 ip_hex = parse_ip_addr(ctx, ip);
331 len = strlen(ip_hex);
337 if (!mac_addr && !ip_hex) {
338 pb_log("%s: neither mac nor ip parameter found\n", __func__);
342 /* Filenames as fallback IP's + mac + default */
343 filenames = talloc_array(ctx, char *, len + 3);
347 filenames[index++] = talloc_strdup(filenames, mac_addr);
350 filenames[index++] = talloc_strdup(filenames, ip_hex);
351 ip_hex[--len] = '\0';
354 filenames[index++] = talloc_strdup(filenames, "default");
355 filenames[index++] = NULL;
358 talloc_free(mac_addr);
366 static int user_event_dhcp(struct user_event *uev, struct event *event)
368 struct device_handler *handler = uev->handler;
369 struct discover_device *dev;
371 dev = discover_device_create(handler, event->device);
373 device_handler_dhcp(handler, dev, event);
378 static int user_event_conf(struct user_event *uev, struct event *event)
380 struct device_handler *handler = uev->handler;
381 struct discover_device *dev;
385 val = event_get_param(event, "url");
389 url = pb_url_parse(event, val);
393 dev = discover_device_create(handler, event->device);
395 device_handler_conf(handler, dev, url);
400 static int user_event_add(struct user_event *uev, struct event *event)
402 struct device_handler *handler = uev->handler;
403 struct discover_context *ctx;
404 struct discover_device *dev;
406 dev = discover_device_create(handler, event->device);
407 ctx = device_handler_discover_context_create(handler, dev);
409 parse_user_event(ctx, event);
411 device_handler_discover_context_commit(handler, ctx);
418 static int user_event_remove(struct user_event *uev, struct event *event)
420 struct device_handler *handler = uev->handler;
421 struct discover_device *dev;
423 dev = device_lookup_by_id(handler, event->device);
427 device_handler_remove(handler, dev);
432 static void user_event_handle_message(struct user_event *uev, char *buf,
438 event = talloc(uev, struct event);
439 event->type = EVENT_TYPE_USER;
441 result = event_parse_ad_message(event, buf, len);
446 user_event_print_event(event);
448 switch (event->action) {
449 case EVENT_ACTION_ADD:
450 result = user_event_add(uev, event);
452 case EVENT_ACTION_REMOVE:
453 result = user_event_remove(uev, event);
455 case EVENT_ACTION_CONF:
456 result = user_event_conf(uev, event);
458 case EVENT_ACTION_DHCP:
459 result = user_event_dhcp(uev, event);
470 static int user_event_process(void *arg)
472 struct user_event *uev = arg;
473 char buf[PBOOT_USER_EVENT_SIZE];
476 len = recvfrom(uev->socket, buf, sizeof(buf), 0, NULL, NULL);
479 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
484 pb_log("%s: empty", __func__);
488 pb_debug("%s: %u bytes\n", __func__, len);
490 user_event_handle_message(uev, buf, len);
495 static int user_event_destructor(void *arg)
497 struct user_event *uev = arg;
499 pb_debug("%s\n", __func__);
501 if (uev->socket >= 0)
507 struct user_event *user_event_init(struct waitset *waitset,
508 struct device_handler *handler)
510 struct sockaddr_un addr;
511 struct user_event *uev;
513 unlink(PBOOT_USER_EVENT_SOCKET);
515 uev = talloc(NULL, struct user_event);
517 uev->handler = handler;
519 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
520 if (uev->socket < 0) {
521 pb_log("%s: Error creating event handler socket: %s\n",
522 __func__, strerror(errno));
526 talloc_set_destructor(uev, user_event_destructor);
528 memset(&addr, 0, sizeof(addr));
529 addr.sun_family = AF_UNIX;
530 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
532 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
533 pb_log("Error binding event handler socket: %s\n",
537 waiter_register_io(waitset, uev->socket, WAIT_IN,
538 user_event_process, uev);
540 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
549 void user_event_destroy(struct user_event *uev)