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:
73 static void user_event_print_event(struct event __attribute__((unused)) *event)
77 pb_debug("user_event %s event:\n", event_action_name(event->action));
78 pb_debug("\tdevice: %s\n", event->device);
80 for (i = 0; i < event->n_params; i++)
81 pb_debug("\t%-12s => %s\n",
82 event->params[i].name, event->params[i].value);
85 static struct resource *user_event_resource(struct discover_boot_option *opt,
86 struct event *event, bool gen_boot_args_sigfile)
88 const char *siaddr, *boot_file;
93 siaddr = event_get_param(event, "siaddr");
95 pb_log("%s: next server option not found\n", __func__);
99 boot_file = event_get_param(event, "bootfile");
101 pb_log("%s: bootfile not found\n", __func__);
105 if (gen_boot_args_sigfile) {
106 char* args_sigfile_default = talloc_asprintf(opt,
107 "%s.cmdline.sig", boot_file);
108 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
109 args_sigfile_default);
110 talloc_free(args_sigfile_default);
113 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
115 url = pb_url_parse(opt, url_str);
116 talloc_free(url_str);
121 res = create_url_resource(opt, url);
130 static int parse_user_event(struct discover_context *ctx, struct event *event)
132 struct discover_boot_option *d_opt;
133 char *server_ip, *root_dir, *p;
134 struct boot_option *opt;
138 dev = ctx->device->device;
140 d_opt = discover_boot_option_create(ctx, ctx->device);
146 val = event_get_param(event, "name");
149 pb_log("%s: no name found\n", __func__);
153 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
154 opt->name = talloc_strdup(opt, val);
156 d_opt->boot_image = user_event_resource(d_opt, event, false);
157 if (!d_opt->boot_image) {
158 pb_log("%s: no boot image found for %s!\n", __func__,
162 d_opt->args_sig_file = user_event_resource(d_opt, event, true);
164 val = event_get_param(event, "rootpath");
166 server_ip = talloc_strdup(opt, val);
167 p = strchr(server_ip, ':');
169 root_dir = talloc_strdup(opt, p + 1);
171 opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
172 "root=/dev/nfs ip=any nfsroot=",
173 server_ip, root_dir);
175 talloc_free(root_dir);
177 opt->boot_args = talloc_asprintf(opt, "%s",
178 "root=/dev/nfs ip=any nfsroot=");
181 talloc_free(server_ip);
184 opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
185 opt->boot_args ? : "");
187 if (event_get_param(event, "default"))
188 opt->is_default = true;
190 discover_context_add_boot_option(ctx, d_opt);
200 static const char *parse_host_addr(struct event *event)
204 val = event_get_param(event, "tftp");
208 val = event_get_param(event, "siaddr");
212 val = event_get_param(event, "serverid");
219 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
221 unsigned int mac_addr_arr[MAC_ADDR_SIZE];
224 sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
225 mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
228 mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
229 mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
230 mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
235 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
237 unsigned int ip_addr_arr[IP_ADDR_SIZE];
240 sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
241 ip_addr_arr + 2, ip_addr_arr + 3);
243 ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
244 ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
249 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
250 struct event *event, bool *is_complete)
252 const char *conffile, *pathprefix, *host, *bootfile;
253 char *p, *basedir, *url_str;
256 conffile = event_get_param(event, "pxeconffile");
257 pathprefix = event_get_param(event, "pxepathprefix");
258 bootfile = event_get_param(event, "bootfile");
260 /* If we're given a conf file, we're able to generate a complete URL to
261 * the configuration file, and the parser doesn't need to do any
262 * further autodiscovery */
263 *is_complete = !!conffile;
265 /* if conffile is a URL, that's all we need */
266 if (conffile && is_url(conffile)) {
267 url = pb_url_parse(ctx, conffile);
271 /* If we can create a URL from pathprefix (optionally with
272 * conffile appended to create a complete URL), use that */
273 if (pathprefix && is_url(pathprefix)) {
275 url_str = talloc_asprintf(ctx, "%s%s",
276 pathprefix, conffile);
277 url = pb_url_parse(ctx, url_str);
278 talloc_free(url_str);
280 url = pb_url_parse(ctx, pathprefix);
286 host = parse_host_addr(event);
288 pb_log("%s: host address not found\n", __func__);
292 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
294 /* if we have a pathprefix, use that directly.. */
296 /* strip leading slashes */
297 while (pathprefix[0] == '/')
299 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
301 /* ... otherwise, add a path based on the bootfile name, but only
302 * if conffile isn't an absolute path itself */
303 } else if (bootfile && !(conffile && conffile[0] == '/')) {
305 basedir = talloc_strdup(ctx, bootfile);
307 /* strip filename from the bootfile path, leaving only a
309 p = strrchr(basedir, '/');
315 url_str = talloc_asprintf_append(url_str, "%s/",
318 talloc_free(basedir);
321 /* finally, append conffile */
323 url_str = talloc_asprintf_append(url_str, "%s", conffile);
325 url = pb_url_parse(ctx, url_str);
327 talloc_free(url_str);
332 char **user_event_parse_conf_filenames(
333 struct discover_context *ctx, struct event *event)
335 char *mac_addr, *ip_hex;
336 const char *mac, *ip;
340 mac = event_get_param(event, "mac");
342 mac_addr = parse_mac_addr(ctx, mac);
346 ip = event_get_param(event, "ip");
348 ip_hex = parse_ip_addr(ctx, ip);
349 len = strlen(ip_hex);
355 if (!mac_addr && !ip_hex) {
356 pb_log("%s: neither mac nor ip parameter found\n", __func__);
360 /* Filenames as fallback IP's + mac + default */
361 filenames = talloc_array(ctx, char *, len + 3);
365 filenames[index++] = talloc_strdup(filenames, mac_addr);
368 filenames[index++] = talloc_strdup(filenames, ip_hex);
369 ip_hex[--len] = '\0';
372 filenames[index++] = talloc_strdup(filenames, "default");
373 filenames[index++] = NULL;
376 talloc_free(mac_addr);
384 static int user_event_dhcp(struct user_event *uev, struct event *event)
386 struct device_handler *handler = uev->handler;
387 struct discover_device *dev;
389 uint8_t hwaddr[MAC_ADDR_SIZE];
391 sscanf(event_get_param(event, "mac"),
392 "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
393 hwaddr, hwaddr + 1, hwaddr + 2,
394 hwaddr + 3, hwaddr + 4, hwaddr + 5);
396 system_info_set_interface_address(sizeof(hwaddr), hwaddr,
397 event_get_param(event, "ip"));
399 dev = discover_device_create(handler, event_get_param(event, "mac"),
402 device_handler_dhcp(handler, dev, event);
407 static int user_event_add(struct user_event *uev, struct event *event)
409 struct device_handler *handler = uev->handler;
410 struct discover_context *ctx;
411 struct discover_device *dev;
413 /* In case this is a network interface, try to refer to it by UUID */
414 dev = discover_device_create(handler, event_get_param(event, "mac"),
416 dev->device->id = talloc_strdup(dev, event->device);
417 ctx = device_handler_discover_context_create(handler, dev);
419 parse_user_event(ctx, event);
421 device_handler_discover_context_commit(handler, ctx);
428 static int user_event_remove(struct user_event *uev, struct event *event)
430 struct device_handler *handler = uev->handler;
431 struct discover_device *dev;
432 const char *mac = event_get_param(event, "mac");
435 dev = device_lookup_by_uuid(handler, event_get_param(event, "mac"));
437 dev = device_lookup_by_id(handler, event->device);
442 device_handler_remove(handler, dev);
447 static int user_event_url(struct user_event *uev, struct event *event)
449 struct device_handler *handler = uev->handler;
452 url = event_get_param(event, "url");
454 device_handler_process_url(handler, url, NULL, NULL);
459 static int user_event_boot(struct user_event *uev, struct event *event)
461 struct device_handler *handler = uev->handler;
462 struct boot_command *cmd = talloc(handler, struct boot_command);
464 cmd->option_id = talloc_strdup(cmd, event_get_param(event, "id"));
465 cmd->boot_image_file = talloc_strdup(cmd, event_get_param(event, "image"));
466 cmd->initrd_file = talloc_strdup(cmd, event_get_param(event, "initrd"));
467 cmd->dtb_file = talloc_strdup(cmd, event_get_param(event, "dtb"));
468 cmd->boot_args = talloc_strdup(cmd, event_get_param(event, "args"));
470 device_handler_boot(handler, cmd);
477 static int user_event_sync(struct user_event *uev, struct event *event)
479 struct device_handler *handler = uev->handler;
481 if (strncasecmp(event->device, "all", strlen("all")) != 0)
482 device_sync_snapshots(handler, event->device);
484 device_sync_snapshots(handler, NULL);
489 static void user_event_handle_message(struct user_event *uev, char *buf,
495 event = talloc(uev, struct event);
496 event->type = EVENT_TYPE_USER;
498 result = event_parse_ad_message(event, buf, len);
503 user_event_print_event(event);
505 switch (event->action) {
506 case EVENT_ACTION_ADD:
507 result = user_event_add(uev, event);
509 case EVENT_ACTION_REMOVE:
510 result = user_event_remove(uev, event);
512 case EVENT_ACTION_URL:
513 result = user_event_url(uev, event);
515 case EVENT_ACTION_DHCP:
516 result = user_event_dhcp(uev, event);
518 case EVENT_ACTION_BOOT:
519 result = user_event_boot(uev, event);
521 case EVENT_ACTION_SYNC:
522 result = user_event_sync(uev, event);
528 /* user_event_url() and user_event_dhcp() will steal the event context,
529 * but all others still need to free */
535 static int user_event_process(void *arg)
537 struct user_event *uev = arg;
538 char buf[PBOOT_USER_EVENT_SIZE + 1];
541 len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
544 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
549 pb_log("%s: empty", __func__);
555 pb_debug("%s: %u bytes\n", __func__, len);
557 user_event_handle_message(uev, buf, len);
562 static int user_event_destructor(void *arg)
564 struct user_event *uev = arg;
566 pb_debug("%s\n", __func__);
568 if (uev->socket >= 0)
574 struct user_event *user_event_init(struct device_handler *handler,
575 struct waitset *waitset)
577 struct sockaddr_un addr;
578 struct user_event *uev;
580 unlink(PBOOT_USER_EVENT_SOCKET);
582 uev = talloc(handler, struct user_event);
584 uev->handler = handler;
586 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
587 if (uev->socket < 0) {
588 pb_log("%s: Error creating event handler socket: %s\n",
589 __func__, strerror(errno));
593 talloc_set_destructor(uev, user_event_destructor);
595 memset(&addr, 0, sizeof(addr));
596 addr.sun_family = AF_UNIX;
597 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
599 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
600 pb_log("Error binding event handler socket: %s\n",
604 waiter_register_io(waitset, uev->socket, WAIT_IN,
605 user_event_process, uev);
607 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);