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("%s: next server option not found\n", __func__);
101 boot_file = event_get_param(event, "bootfile");
103 pb_log("%s: bootfile not found\n", __func__);
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("%s: no name found\n", __func__);
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("%s: no boot image found for %s!\n", __func__,
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;
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");
262 /* If we're given a conf file, we're able to generate a complete URL to
263 * the configuration file, and the parser doesn't need to do any
264 * further autodiscovery */
265 *is_complete = !!conffile;
267 /* if conffile is a URL, that's all we need */
268 if (conffile && is_url(conffile)) {
269 url = pb_url_parse(ctx, conffile);
273 /* If we can create a URL from pathprefix (optionally with
274 * conffile appended to create a complete URL), use that */
275 if (pathprefix && is_url(pathprefix)) {
277 url_str = talloc_asprintf(ctx, "%s%s",
278 pathprefix, conffile);
279 url = pb_url_parse(ctx, url_str);
280 talloc_free(url_str);
282 url = pb_url_parse(ctx, pathprefix);
288 host = parse_host_addr(event);
290 pb_log("%s: host address not found\n", __func__);
294 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
296 /* if we have a pathprefix, use that directly.. */
298 /* strip leading slashes */
299 while (pathprefix[0] == '/')
301 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
303 /* ... otherwise, add a path based on the bootfile name, but only
304 * if conffile isn't an absolute path itself */
305 } else if (bootfile && !(conffile && conffile[0] == '/')) {
307 basedir = talloc_strdup(ctx, bootfile);
309 /* strip filename from the bootfile path, leaving only a
311 p = strrchr(basedir, '/');
317 url_str = talloc_asprintf_append(url_str, "%s/",
320 talloc_free(basedir);
323 /* finally, append conffile */
325 url_str = talloc_asprintf_append(url_str, "%s", conffile);
327 url = pb_url_parse(ctx, url_str);
329 talloc_free(url_str);
334 char **user_event_parse_conf_filenames(
335 struct discover_context *ctx, struct event *event)
337 char *mac_addr, *ip_hex;
338 const char *mac, *ip;
342 mac = event_get_param(event, "mac");
344 mac_addr = parse_mac_addr(ctx, mac);
348 ip = event_get_param(event, "ip");
350 ip_hex = parse_ip_addr(ctx, ip);
351 len = strlen(ip_hex);
357 if (!mac_addr && !ip_hex) {
358 pb_log("%s: neither mac nor ip parameter found\n", __func__);
362 /* Filenames as fallback IP's + mac + default */
363 filenames = talloc_array(ctx, char *, len + 3);
367 filenames[index++] = talloc_strdup(filenames, mac_addr);
370 filenames[index++] = talloc_strdup(filenames, ip_hex);
371 ip_hex[--len] = '\0';
374 filenames[index++] = talloc_strdup(filenames, "default");
375 filenames[index++] = NULL;
378 talloc_free(mac_addr);
386 static int user_event_dhcp(struct user_event *uev, struct event *event)
388 struct device_handler *handler = uev->handler;
389 struct discover_device *dev;
391 uint8_t hwaddr[MAC_ADDR_SIZE];
393 sscanf(event_get_param(event, "mac"),
394 "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
395 hwaddr, hwaddr + 1, hwaddr + 2,
396 hwaddr + 3, hwaddr + 4, hwaddr + 5);
398 system_info_set_interface_address(sizeof(hwaddr), hwaddr,
399 event_get_param(event, "ip"));
401 dev = discover_device_create(handler, event_get_param(event, "mac"),
404 device_handler_dhcp(handler, dev, event);
409 static int user_event_add(struct user_event *uev, struct event *event)
411 struct device_handler *handler = uev->handler;
412 struct discover_context *ctx;
413 struct discover_device *dev;
415 /* In case this is a network interface, try to refer to it by UUID */
416 dev = discover_device_create(handler, event_get_param(event, "mac"),
418 dev->device->id = talloc_strdup(dev, event->device);
419 ctx = device_handler_discover_context_create(handler, dev);
421 parse_user_event(ctx, event);
423 device_handler_discover_context_commit(handler, ctx);
430 static int user_event_remove(struct user_event *uev, struct event *event)
432 struct device_handler *handler = uev->handler;
433 struct discover_device *dev;
434 const char *mac = event_get_param(event, "mac");
437 dev = device_lookup_by_uuid(handler, event_get_param(event, "mac"));
439 dev = device_lookup_by_id(handler, event->device);
444 device_handler_remove(handler, dev);
449 static int user_event_url(struct user_event *uev, struct event *event)
451 struct device_handler *handler = uev->handler;
454 url = event_get_param(event, "url");
456 device_handler_process_url(handler, url, NULL, NULL);
461 static int user_event_boot(struct user_event *uev, struct event *event)
463 struct device_handler *handler = uev->handler;
464 struct boot_command *cmd = talloc(handler, struct boot_command);
466 cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
467 cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
468 cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
469 cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
470 cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
472 device_handler_boot(handler, cmd);
479 static int user_event_sync(struct user_event *uev, struct event *event)
481 struct device_handler *handler = uev->handler;
483 if (strncasecmp(event->device, "all", strlen("all")) != 0)
484 device_sync_snapshots(handler, event->device);
486 device_sync_snapshots(handler, NULL);
491 static int process_uninstalled_plugin(struct user_event *uev,
494 struct device_handler *handler = uev->handler;
495 struct discover_boot_option *file_opt;
496 struct discover_device *device;
497 struct discover_context *ctx;
499 struct resource *res;
501 if (!event_get_param(event, "path")) {
502 pb_log("Uninstalled pb-plugin event missing path param\n");
506 device = device_lookup_by_name(handler, event->device);
508 pb_log("Couldn't find device matching %s for plugin\n",
513 ctx = device_handler_discover_context_create(handler, device);
514 file_opt = discover_boot_option_create(ctx, device);
515 file_opt->option->name = talloc_strdup(file_opt,
516 event_get_param(event, "name"));
517 file_opt->option->id = talloc_asprintf(file_opt, "%s@%p",
518 device->device->id, file_opt);
519 file_opt->option->type = DISCOVER_PLUGIN_OPTION;
522 path = event_get_param(event, "path");
523 /* path may be relative to root */
524 if (strncmp(device->mount_path, path, strlen(device->mount_path)) == 0) {
525 path += strlen(device->mount_path) + 1;
528 res = talloc(file_opt, struct resource);
529 resolve_resource_against_device(res, device, path);
530 file_opt->boot_image = res;
532 discover_context_add_boot_option(ctx, file_opt);
533 device_handler_discover_context_commit(handler, ctx);
539 * Notification of a plugin event. This can either be for an uninstalled plugin
540 * that pb-plugin has scanned, or the result of a plugin that pb-plugin has
543 static int user_event_plugin(struct user_event *uev, struct event *event)
545 struct device_handler *handler = uev->handler;
546 char *executable, *executables, *saveptr;
547 struct plugin_option *opt;
548 const char *installed;
550 installed = event_get_param(event, "installed");
551 if (!installed || strncmp(installed, "no", strlen("no")) == 0)
552 return process_uninstalled_plugin(uev, event);
554 opt = talloc_zero(handler, struct plugin_option);
557 opt->name = talloc_strdup(opt, event_get_param(event, "name"));
558 opt->id = talloc_strdup(opt, event_get_param(event, "id"));
559 opt->version = talloc_strdup(opt, event_get_param(event, "version"));
560 opt->vendor = talloc_strdup(opt, event_get_param(event, "vendor"));
561 opt->vendor_id = talloc_strdup(opt, event_get_param(event, "vendor_id"));
562 opt->date = talloc_strdup(opt, event_get_param(event, "date"));
563 opt->plugin_file = talloc_strdup(opt,
564 event_get_param(event, "source_file"));
566 executables = talloc_strdup(opt, event_get_param(event, "executables"));
573 * The 'executables' parameter is a space-delimited list of installed
576 executable = strtok_r(executables, " ", &saveptr);
578 opt->executables = talloc_realloc(opt, opt->executables,
579 char *, opt->n_executables + 1);
580 if (!opt->executables) {
584 opt->executables[opt->n_executables++] = talloc_strdup(opt,
586 executable = strtok_r(NULL, " ", &saveptr);
589 device_handler_add_plugin_option(handler, opt);
591 talloc_free(executables);
596 static void user_event_handle_message(struct user_event *uev, char *buf,
602 event = talloc(uev, struct event);
603 event->type = EVENT_TYPE_USER;
605 result = event_parse_ad_message(event, buf, len);
610 user_event_print_event(event);
612 switch (event->action) {
613 case EVENT_ACTION_ADD:
614 result = user_event_add(uev, event);
616 case EVENT_ACTION_REMOVE:
617 result = user_event_remove(uev, event);
619 case EVENT_ACTION_URL:
620 result = user_event_url(uev, event);
622 case EVENT_ACTION_DHCP:
623 result = user_event_dhcp(uev, event);
625 case EVENT_ACTION_BOOT:
626 result = user_event_boot(uev, event);
628 case EVENT_ACTION_SYNC:
629 result = user_event_sync(uev, event);
631 case EVENT_ACTION_PLUGIN:
632 result = user_event_plugin(uev, event);
638 /* user_event_url() and user_event_dhcp() will steal the event context,
639 * but all others still need to free */
645 static int user_event_process(void *arg)
647 struct user_event *uev = arg;
648 char buf[PBOOT_USER_EVENT_SIZE + 1];
651 len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
654 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
659 pb_log("%s: empty", __func__);
665 pb_debug("%s: %u bytes\n", __func__, len);
667 user_event_handle_message(uev, buf, len);
672 static int user_event_destructor(void *arg)
674 struct user_event *uev = arg;
676 pb_debug("%s\n", __func__);
678 if (uev->socket >= 0)
684 struct user_event *user_event_init(struct device_handler *handler,
685 struct waitset *waitset)
687 struct sockaddr_un addr;
688 struct user_event *uev;
690 unlink(PBOOT_USER_EVENT_SOCKET);
692 uev = talloc(handler, struct user_event);
694 uev->handler = handler;
696 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
697 if (uev->socket < 0) {
698 pb_log("%s: Error creating event handler socket: %s\n",
699 __func__, strerror(errno));
703 talloc_set_destructor(uev, user_event_destructor);
705 memset(&addr, 0, sizeof(addr));
706 addr.sun_family = AF_UNIX;
707 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
709 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
710 pb_log("Error binding event handler socket: %s\n",
714 waiter_register_io(waitset, uev->socket, WAIT_IN,
715 user_event_process, uev);
717 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);