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_URL:
59 case EVENT_ACTION_DHCP:
68 static void user_event_print_event(struct event __attribute__((unused)) *event)
72 pb_debug("user_event %s event:\n", event_action_name(event->action));
73 pb_debug("\tdevice: %s\n", event->device);
75 for (i = 0; i < event->n_params; i++)
76 pb_debug("\t%-12s => %s\n",
77 event->params[i].name, event->params[i].value);
80 static struct resource *user_event_resource(struct discover_boot_option *opt,
83 const char *siaddr, *boot_file;
88 siaddr = event_get_param(event, "siaddr");
90 pb_log("%s: next server option not found\n", __func__);
94 boot_file = event_get_param(event, "bootfile");
96 pb_log("%s: bootfile not found\n", __func__);
100 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, boot_file);
101 url = pb_url_parse(opt, url_str);
102 talloc_free(url_str);
107 res = create_url_resource(opt, url);
116 static int parse_user_event(struct discover_context *ctx, struct event *event)
118 struct discover_boot_option *d_opt;
119 char *server_ip, *root_dir, *p;
120 struct boot_option *opt;
124 dev = ctx->device->device;
126 d_opt = discover_boot_option_create(ctx, ctx->device);
132 val = event_get_param(event, "name");
135 pb_log("%s: no name found\n", __func__);
139 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
140 opt->name = talloc_strdup(opt, val);
142 d_opt->boot_image = user_event_resource(d_opt, event);
143 if (!d_opt->boot_image) {
144 pb_log("%s: no boot image found for %s!\n", __func__,
149 val = event_get_param(event, "rootpath");
151 server_ip = talloc_strdup(opt, val);
152 p = strchr(server_ip, ':');
154 root_dir = talloc_strdup(opt, p + 1);
156 opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
157 "root=/dev/nfs ip=any nfsroot=",
158 server_ip, root_dir);
160 talloc_free(root_dir);
162 opt->boot_args = talloc_asprintf(opt, "%s",
163 "root=/dev/nfs ip=any nfsroot=");
166 talloc_free(server_ip);
169 opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
170 opt->boot_args ? : "");
172 if (event_get_param(event, "default"))
173 opt->is_default = true;
175 discover_context_add_boot_option(ctx, d_opt);
185 static const char *parse_host_addr(struct event *event)
189 val = event_get_param(event, "tftp");
193 val = event_get_param(event, "siaddr");
197 val = event_get_param(event, "serverid");
204 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
206 unsigned int mac_addr_arr[MAC_ADDR_SIZE];
209 sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
210 mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
213 mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
214 mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
215 mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
220 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
222 unsigned int ip_addr_arr[IP_ADDR_SIZE];
225 sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
226 ip_addr_arr + 2, ip_addr_arr + 3);
228 ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
229 ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
234 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
235 struct event *event, bool *is_complete)
237 const char *conffile, *pathprefix, *host, *bootfile;
238 char *p, *basedir, *url_str;
241 conffile = event_get_param(event, "pxeconffile");
242 pathprefix = event_get_param(event, "pxepathprefix");
243 bootfile = event_get_param(event, "bootfile");
245 /* If we're given a conf file, we're able to generate a complete URL to
246 * the configuration file, and the parser doesn't need to do any
247 * further autodiscovery */
248 *is_complete = !!conffile;
250 /* if conffile is a URL, that's all we need */
251 if (conffile && is_url(conffile)) {
252 url = pb_url_parse(ctx, conffile);
256 /* If we can create a URL from pathprefix (optionally with
257 * conffile appended to create a complete URL), use that */
258 if (pathprefix && is_url(pathprefix)) {
260 url_str = talloc_asprintf(ctx, "%s%s",
261 pathprefix, conffile);
262 url = pb_url_parse(ctx, url_str);
263 talloc_free(url_str);
265 url = pb_url_parse(ctx, pathprefix);
271 host = parse_host_addr(event);
273 pb_log("%s: host address not found\n", __func__);
277 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
279 /* if we have a pathprefix, use that directly.. */
281 /* strip leading slashes */
282 while (pathprefix[0] == '/')
284 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
286 /* ... otherwise, add a path based on the bootfile name, but only
287 * if conffile isn't an absolute path itself */
288 } else if (bootfile && !(conffile && conffile[0] == '/')) {
290 basedir = talloc_strdup(ctx, bootfile);
292 /* strip filename from the bootfile path, leaving only a
294 p = strrchr(basedir, '/');
300 url_str = talloc_asprintf_append(url_str, "%s/",
303 talloc_free(basedir);
306 /* finally, append conffile */
308 url_str = talloc_asprintf_append(url_str, "%s", conffile);
310 url = pb_url_parse(ctx, url_str);
312 talloc_free(url_str);
317 char **user_event_parse_conf_filenames(
318 struct discover_context *ctx, struct event *event)
320 char *mac_addr, *ip_hex;
321 const char *mac, *ip;
325 mac = event_get_param(event, "mac");
327 mac_addr = parse_mac_addr(ctx, mac);
331 ip = event_get_param(event, "ip");
333 ip_hex = parse_ip_addr(ctx, ip);
334 len = strlen(ip_hex);
340 if (!mac_addr && !ip_hex) {
341 pb_log("%s: neither mac nor ip parameter found\n", __func__);
345 /* Filenames as fallback IP's + mac + default */
346 filenames = talloc_array(ctx, char *, len + 3);
350 filenames[index++] = talloc_strdup(filenames, mac_addr);
353 filenames[index++] = talloc_strdup(filenames, ip_hex);
354 ip_hex[--len] = '\0';
357 filenames[index++] = talloc_strdup(filenames, "default");
358 filenames[index++] = NULL;
361 talloc_free(mac_addr);
369 static int user_event_dhcp(struct user_event *uev, struct event *event)
371 struct device_handler *handler = uev->handler;
372 struct discover_device *dev;
374 dev = discover_device_create(handler, event->device);
376 device_handler_dhcp(handler, dev, event);
381 static int user_event_conf(struct user_event *uev, struct event *event)
383 struct device_handler *handler = uev->handler;
384 struct discover_device *dev;
388 val = event_get_param(event, "url");
392 url = pb_url_parse(event, val);
396 dev = discover_device_create(handler, event->device);
398 device_handler_conf(handler, dev, url);
403 static int user_event_add(struct user_event *uev, struct event *event)
405 struct device_handler *handler = uev->handler;
406 struct discover_context *ctx;
407 struct discover_device *dev;
409 dev = discover_device_create(handler, event->device);
410 ctx = device_handler_discover_context_create(handler, dev);
412 parse_user_event(ctx, event);
414 device_handler_discover_context_commit(handler, ctx);
421 static int user_event_remove(struct user_event *uev, struct event *event)
423 struct device_handler *handler = uev->handler;
424 struct discover_device *dev;
426 dev = device_lookup_by_id(handler, event->device);
430 device_handler_remove(handler, dev);
435 static int user_event_url(struct user_event *uev, struct event *event)
437 struct device_handler *handler = uev->handler;
440 url = event_get_param(event, "url");
442 device_handler_process_url(handler, url);
447 static void user_event_handle_message(struct user_event *uev, char *buf,
453 event = talloc(uev, struct event);
454 event->type = EVENT_TYPE_USER;
456 result = event_parse_ad_message(event, buf, len);
461 user_event_print_event(event);
463 switch (event->action) {
464 case EVENT_ACTION_ADD:
465 result = user_event_add(uev, event);
467 case EVENT_ACTION_REMOVE:
468 result = user_event_remove(uev, event);
470 case EVENT_ACTION_URL:
471 result = user_event_url(uev, event);
473 case EVENT_ACTION_CONF:
474 result = user_event_conf(uev, event);
476 case EVENT_ACTION_DHCP:
477 result = user_event_dhcp(uev, event);
488 static int user_event_process(void *arg)
490 struct user_event *uev = arg;
491 char buf[PBOOT_USER_EVENT_SIZE];
494 len = recvfrom(uev->socket, buf, sizeof(buf), 0, NULL, NULL);
497 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
502 pb_log("%s: empty", __func__);
506 pb_debug("%s: %u bytes\n", __func__, len);
508 user_event_handle_message(uev, buf, len);
513 static int user_event_destructor(void *arg)
515 struct user_event *uev = arg;
517 pb_debug("%s\n", __func__);
519 if (uev->socket >= 0)
525 struct user_event *user_event_init(struct device_handler *handler,
526 struct waitset *waitset)
528 struct sockaddr_un addr;
529 struct user_event *uev;
531 unlink(PBOOT_USER_EVENT_SOCKET);
533 uev = talloc(handler, struct user_event);
535 uev->handler = handler;
537 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
538 if (uev->socket < 0) {
539 pb_log("%s: Error creating event handler socket: %s\n",
540 __func__, strerror(errno));
544 talloc_set_destructor(uev, user_event_destructor);
546 memset(&addr, 0, sizeof(addr));
547 addr.sun_family = AF_UNIX;
548 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
550 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
551 pb_log("Error binding event handler socket: %s\n",
555 waiter_register_io(waitset, uev->socket, WAIT_IN,
556 user_event_process, uev);
558 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);