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"
43 #define MAC_ADDR_SIZE 6
44 #define IP_ADDR_SIZE 4
47 struct device_handler *handler;
51 static const char *event_action_name(enum event_action action)
54 case EVENT_ACTION_ADD:
56 case EVENT_ACTION_REMOVE:
58 case EVENT_ACTION_URL:
60 case EVENT_ACTION_DHCP:
62 case EVENT_ACTION_BOOT:
64 case EVENT_ACTION_SYNC:
66 case EVENT_ACTION_PLUGIN:
75 static void user_event_print_event(struct event __attribute__((unused)) *event)
79 pb_debug("user_event %s event:\n", event_action_name(event->action));
80 pb_debug("\tdevice: %s\n", event->device);
82 for (i = 0; i < event->n_params; i++)
83 pb_debug("\t%-12s => %s\n",
84 event->params[i].name, event->params[i].value);
87 static struct resource *user_event_resource(struct discover_boot_option *opt,
88 struct event *event, bool gen_boot_args_sigfile)
90 const char *siaddr, *boot_file;
95 siaddr = event_get_param(event, "siaddr");
97 pb_log_fn("next server option not found\n");
101 boot_file = event_get_param(event, "bootfile");
103 pb_log_fn("bootfile not found\n");
107 if (gen_boot_args_sigfile) {
108 char* args_sigfile_default = talloc_asprintf(opt,
109 "%s.cmdline.sig", boot_file);
110 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
111 args_sigfile_default);
112 talloc_free(args_sigfile_default);
115 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
117 url = pb_url_parse(opt, url_str);
118 talloc_free(url_str);
123 res = create_url_resource(opt, url);
132 static int parse_user_event(struct discover_context *ctx, struct event *event)
134 struct discover_boot_option *d_opt;
135 char *server_ip, *root_dir, *p;
136 struct boot_option *opt;
140 dev = ctx->device->device;
142 d_opt = discover_boot_option_create(ctx, ctx->device);
148 val = event_get_param(event, "name");
151 pb_log_fn("no name found\n");
155 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
156 opt->name = talloc_strdup(opt, val);
158 d_opt->boot_image = user_event_resource(d_opt, event, false);
159 if (!d_opt->boot_image) {
160 pb_log_fn("no boot image found for %s!\n",
164 d_opt->args_sig_file = user_event_resource(d_opt, event, true);
166 val = event_get_param(event, "rootpath");
168 server_ip = talloc_strdup(opt, val);
169 p = strchr(server_ip, ':');
171 root_dir = talloc_strdup(opt, p + 1);
173 opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
174 "root=/dev/nfs ip=any nfsroot=",
175 server_ip, root_dir);
177 talloc_free(root_dir);
179 opt->boot_args = talloc_asprintf(opt, "%s",
180 "root=/dev/nfs ip=any nfsroot=");
183 talloc_free(server_ip);
186 opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
187 opt->boot_args ? : "");
189 if (event_get_param(event, "default"))
190 opt->is_default = true;
192 discover_context_add_boot_option(ctx, d_opt);
202 static const char *parse_host_addr(struct event *event)
206 val = event_get_param(event, "tftp");
210 val = event_get_param(event, "siaddr");
214 val = event_get_param(event, "serverid");
221 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
223 unsigned int mac_addr_arr[MAC_ADDR_SIZE];
226 sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
227 mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
230 mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
231 mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
232 mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
237 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
239 unsigned int ip_addr_arr[IP_ADDR_SIZE];
242 sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
243 ip_addr_arr + 2, ip_addr_arr + 3);
245 ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
246 ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
251 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
252 struct event *event, bool *is_complete)
254 const char *conffile, *pathprefix, *host, *bootfile, *bootfile_url;
255 char *p, *basedir, *url_str;
258 conffile = event_get_param(event, "pxeconffile");
259 pathprefix = event_get_param(event, "pxepathprefix");
260 bootfile = event_get_param(event, "bootfile");
261 bootfile_url = event_get_param(event, "bootfile_url");
263 /* If we're given a conf file, we're able to generate a complete URL to
264 * the configuration file, and the parser doesn't need to do any
265 * further autodiscovery */
266 *is_complete = !!conffile;
268 /* if conffile is a URL, that's all we need */
269 if (conffile && is_url(conffile)) {
270 url = pb_url_parse(ctx, conffile);
274 /* If we can create a URL from pathprefix (optionally with
275 * conffile appended to create a complete URL), use that */
276 if (pathprefix && is_url(pathprefix)) {
278 url_str = talloc_asprintf(ctx, "%s%s",
279 pathprefix, conffile);
280 url = pb_url_parse(ctx, url_str);
281 talloc_free(url_str);
283 url = pb_url_parse(ctx, pathprefix);
289 host = parse_host_addr(event);
291 pb_log_fn("host address not found\n");
293 /* No full URLs and no host address? Check for DHCPv6 options */
294 if (bootfile_url && is_url(bootfile_url)) {
296 return pb_url_parse(ctx, bootfile_url);
301 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
303 /* if we have a pathprefix, use that directly.. */
305 /* strip leading slashes */
306 while (pathprefix[0] == '/')
308 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
310 /* ... otherwise, add a path based on the bootfile name, but only
311 * if conffile isn't an absolute path itself */
312 } else if (bootfile && !(conffile && conffile[0] == '/')) {
314 basedir = talloc_strdup(ctx, bootfile);
316 /* strip filename from the bootfile path, leaving only a
318 p = strrchr(basedir, '/');
324 url_str = talloc_asprintf_append(url_str, "%s/",
327 talloc_free(basedir);
330 /* finally, append conffile */
332 url_str = talloc_asprintf_append(url_str, "%s", conffile);
334 url = pb_url_parse(ctx, url_str);
336 talloc_free(url_str);
341 char **user_event_parse_conf_filenames(
342 struct discover_context *ctx, struct event *event)
344 char *mac_addr, *ip_hex;
345 const char *mac, *ip;
349 mac = event_get_param(event, "mac");
351 mac_addr = parse_mac_addr(ctx, mac);
355 ip = event_get_param(event, "ip");
357 ip_hex = parse_ip_addr(ctx, ip);
358 len = strlen(ip_hex);
364 if (!mac_addr && !ip_hex) {
365 pb_log_fn("neither mac nor ip parameter found\n");
369 /* Filenames as fallback IP's + mac + default */
370 filenames = talloc_array(ctx, char *, len + 3);
374 filenames[index++] = talloc_strdup(filenames, mac_addr);
377 filenames[index++] = talloc_strdup(filenames, ip_hex);
378 ip_hex[--len] = '\0';
381 filenames[index++] = talloc_strdup(filenames, "default");
382 filenames[index++] = NULL;
385 talloc_free(mac_addr);
393 static int user_event_dhcp(struct user_event *uev, struct event *event)
395 struct device_handler *handler = uev->handler;
396 struct discover_device *dev;
398 uint8_t hwaddr[MAC_ADDR_SIZE];
400 if (!event_get_param(event, "mac"))
402 if (!event_get_param(event, "ip") && !event_get_param(event, "ipv6"))
405 sscanf(event_get_param(event, "mac"),
406 "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
407 hwaddr, hwaddr + 1, hwaddr + 2,
408 hwaddr + 3, hwaddr + 4, hwaddr + 5);
410 if (event_get_param(event, "ipv6"))
411 system_info_set_interface_address(sizeof(hwaddr), hwaddr,
412 event_get_param(event, "ipv6"));
414 system_info_set_interface_address(sizeof(hwaddr), hwaddr,
415 event_get_param(event, "ip"));
417 dev = discover_device_create(handler, event_get_param(event, "mac"),
420 device_handler_dhcp(handler, dev, event);
425 static int user_event_add(struct user_event *uev, struct event *event)
427 struct device_handler *handler = uev->handler;
428 struct discover_context *ctx;
429 struct discover_device *dev;
432 /* In case this is a network interface, try to refer to it by UUID */
433 dev = discover_device_create(handler, event_get_param(event, "mac"),
435 dev->device->id = talloc_strdup(dev, event->device);
436 ctx = device_handler_discover_context_create(handler, dev);
438 rc = parse_user_event(ctx, event);
440 pb_log("parse_user_event returned %d\n", rc);
444 device_handler_discover_context_commit(handler, ctx);
451 static int user_event_remove(struct user_event *uev, struct event *event)
453 struct device_handler *handler = uev->handler;
454 struct discover_device *dev;
455 const char *mac = event_get_param(event, "mac");
458 dev = device_lookup_by_uuid(handler, event_get_param(event, "mac"));
460 dev = device_lookup_by_id(handler, event->device);
465 device_handler_remove(handler, dev);
470 static int user_event_url(struct user_event *uev, struct event *event)
472 struct device_handler *handler = uev->handler;
475 url = event_get_param(event, "url");
477 device_handler_process_url(handler, url, NULL, NULL);
482 static int user_event_boot(struct user_event *uev, struct event *event)
484 struct device_handler *handler = uev->handler;
485 struct boot_command *cmd = talloc(handler, struct boot_command);
487 cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
488 cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
489 cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
490 cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
491 cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
493 device_handler_boot(handler, cmd);
500 static int user_event_sync(struct user_event *uev, struct event *event)
502 struct device_handler *handler = uev->handler;
504 if (strncasecmp(event->device, "all", strlen("all")) != 0)
505 device_sync_snapshots(handler, event->device);
507 device_sync_snapshots(handler, NULL);
512 static int process_uninstalled_plugin(struct user_event *uev,
515 struct device_handler *handler = uev->handler;
516 struct discover_boot_option *file_opt;
517 struct discover_device *device;
518 struct discover_context *ctx;
520 struct resource *res;
522 if (!event_get_param(event, "path")) {
523 pb_log("Uninstalled pb-plugin event missing path param\n");
527 device = device_lookup_by_name(handler, event->device);
529 pb_log("Couldn't find device matching %s for plugin\n",
534 ctx = device_handler_discover_context_create(handler, device);
535 file_opt = discover_boot_option_create(ctx, device);
536 file_opt->option->name = talloc_strdup(file_opt,
537 event_get_param(event, "name"));
538 file_opt->option->id = talloc_asprintf(file_opt, "%s@%p",
539 device->device->id, file_opt);
540 file_opt->option->type = DISCOVER_PLUGIN_OPTION;
543 path = event_get_param(event, "path");
544 /* path may be relative to root */
545 if (strncmp(device->mount_path, path, strlen(device->mount_path)) == 0) {
546 path += strlen(device->mount_path) + 1;
549 res = talloc(file_opt, struct resource);
550 resolve_resource_against_device(res, device, path);
551 file_opt->boot_image = res;
553 discover_context_add_boot_option(ctx, file_opt);
554 device_handler_discover_context_commit(handler, ctx);
560 * Notification of a plugin event. This can either be for an uninstalled plugin
561 * that pb-plugin has scanned, or the result of a plugin that pb-plugin has
564 static int user_event_plugin(struct user_event *uev, struct event *event)
566 struct device_handler *handler = uev->handler;
567 char *executable, *executables, *saveptr;
568 struct plugin_option *opt;
569 const char *installed;
571 installed = event_get_param(event, "installed");
572 if (!installed || strncmp(installed, "no", strlen("no")) == 0)
573 return process_uninstalled_plugin(uev, event);
575 opt = talloc_zero(handler, struct plugin_option);
578 opt->name = talloc_strdup(opt, event_get_param(event, "name"));
579 opt->id = talloc_strdup(opt, event_get_param(event, "id"));
580 opt->version = talloc_strdup(opt, event_get_param(event, "version"));
581 opt->vendor = talloc_strdup(opt, event_get_param(event, "vendor"));
582 opt->vendor_id = talloc_strdup(opt, event_get_param(event, "vendor_id"));
583 opt->date = talloc_strdup(opt, event_get_param(event, "date"));
584 opt->plugin_file = talloc_strdup(opt,
585 event_get_param(event, "source_file"));
587 executables = talloc_strdup(opt, event_get_param(event, "executables"));
594 * The 'executables' parameter is a space-delimited list of installed
597 executable = strtok_r(executables, " ", &saveptr);
599 opt->executables = talloc_realloc(opt, opt->executables,
600 char *, opt->n_executables + 1);
601 if (!opt->executables) {
605 opt->executables[opt->n_executables++] = talloc_strdup(opt,
607 executable = strtok_r(NULL, " ", &saveptr);
610 device_handler_add_plugin_option(handler, opt);
612 talloc_free(executables);
617 static void user_event_handle_message(struct user_event *uev, char *buf,
623 event = talloc(uev, struct event);
624 event->type = EVENT_TYPE_USER;
626 result = event_parse_ad_message(event, buf, len);
631 user_event_print_event(event);
633 switch (event->action) {
634 case EVENT_ACTION_ADD:
635 result = user_event_add(uev, event);
637 case EVENT_ACTION_REMOVE:
638 result = user_event_remove(uev, event);
640 case EVENT_ACTION_URL:
641 result = user_event_url(uev, event);
643 case EVENT_ACTION_DHCP:
644 result = user_event_dhcp(uev, event);
646 case EVENT_ACTION_BOOT:
647 result = user_event_boot(uev, event);
649 case EVENT_ACTION_SYNC:
650 result = user_event_sync(uev, event);
652 case EVENT_ACTION_PLUGIN:
653 result = user_event_plugin(uev, event);
659 /* user_event_url() and user_event_dhcp() will steal the event context,
660 * but all others still need to free */
666 static int user_event_process(void *arg)
668 struct user_event *uev = arg;
669 char buf[PBOOT_USER_EVENT_SIZE + 1];
672 len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
675 pb_log_fn("socket read failed: %s\n", strerror(errno));
680 pb_log_fn("empty\n");
686 pb_debug("%s: %u bytes\n", __func__, len);
688 user_event_handle_message(uev, buf, len);
693 static int user_event_destructor(void *arg)
695 struct user_event *uev = arg;
697 pb_debug("%s\n", __func__);
699 if (uev->socket >= 0)
705 struct user_event *user_event_init(struct device_handler *handler,
706 struct waitset *waitset)
708 struct sockaddr_un addr;
709 struct user_event *uev;
711 unlink(PBOOT_USER_EVENT_SOCKET);
713 uev = talloc(handler, struct user_event);
715 uev->handler = handler;
717 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
718 if (uev->socket < 0) {
719 pb_log("%s: Error creating event handler socket: %s\n",
720 __func__, strerror(errno));
724 talloc_set_destructor(uev, user_event_destructor);
726 memset(&addr, 0, sizeof(addr));
727 addr.sun_family = AF_UNIX;
728 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
730 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
731 pb_log("Error binding event handler socket: %s\n",
735 waiter_register_io(waitset, uev->socket, WAIT_IN,
736 user_event_process, uev);
738 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);