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:
61 case EVENT_ACTION_BOOT:
63 case EVENT_ACTION_SYNC:
72 static void user_event_print_event(struct event __attribute__((unused)) *event)
76 pb_debug("user_event %s event:\n", event_action_name(event->action));
77 pb_debug("\tdevice: %s\n", event->device);
79 for (i = 0; i < event->n_params; i++)
80 pb_debug("\t%-12s => %s\n",
81 event->params[i].name, event->params[i].value);
84 static struct resource *user_event_resource(struct discover_boot_option *opt,
85 struct event *event, bool gen_boot_args_sigfile)
87 const char *siaddr, *boot_file;
92 siaddr = event_get_param(event, "siaddr");
94 pb_log("%s: next server option not found\n", __func__);
98 boot_file = event_get_param(event, "bootfile");
100 pb_log("%s: bootfile not found\n", __func__);
104 if (gen_boot_args_sigfile) {
105 char* args_sigfile_default = talloc_asprintf(opt,
106 "%s.cmdline.sig", boot_file);
107 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
108 args_sigfile_default);
109 talloc_free(args_sigfile_default);
112 url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr,
114 url = pb_url_parse(opt, url_str);
115 talloc_free(url_str);
120 res = create_url_resource(opt, url);
129 static int parse_user_event(struct discover_context *ctx, struct event *event)
131 struct discover_boot_option *d_opt;
132 char *server_ip, *root_dir, *p;
133 struct boot_option *opt;
137 dev = ctx->device->device;
139 d_opt = discover_boot_option_create(ctx, ctx->device);
145 val = event_get_param(event, "name");
148 pb_log("%s: no name found\n", __func__);
152 opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
153 opt->name = talloc_strdup(opt, val);
155 d_opt->boot_image = user_event_resource(d_opt, event, false);
156 if (!d_opt->boot_image) {
157 pb_log("%s: no boot image found for %s!\n", __func__,
161 d_opt->args_sig_file = user_event_resource(d_opt, event, true);
163 val = event_get_param(event, "rootpath");
165 server_ip = talloc_strdup(opt, val);
166 p = strchr(server_ip, ':');
168 root_dir = talloc_strdup(opt, p + 1);
170 opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
171 "root=/dev/nfs ip=any nfsroot=",
172 server_ip, root_dir);
174 talloc_free(root_dir);
176 opt->boot_args = talloc_asprintf(opt, "%s",
177 "root=/dev/nfs ip=any nfsroot=");
180 talloc_free(server_ip);
183 opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
184 opt->boot_args ? : "");
186 if (event_get_param(event, "default"))
187 opt->is_default = true;
189 discover_context_add_boot_option(ctx, d_opt);
199 static const char *parse_host_addr(struct event *event)
203 val = event_get_param(event, "tftp");
207 val = event_get_param(event, "siaddr");
211 val = event_get_param(event, "serverid");
218 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
220 unsigned int mac_addr_arr[MAC_ADDR_SIZE];
223 sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
224 mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
227 mac_addr = talloc_asprintf(ctx, "01-%02x-%02x-%02x-%02x-%02x-%02x",
228 mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
229 mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
234 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
236 unsigned int ip_addr_arr[IP_ADDR_SIZE];
239 sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
240 ip_addr_arr + 2, ip_addr_arr + 3);
242 ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
243 ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
248 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
249 struct event *event, bool *is_complete)
251 const char *conffile, *pathprefix, *host, *bootfile;
252 char *p, *basedir, *url_str;
255 conffile = event_get_param(event, "pxeconffile");
256 pathprefix = event_get_param(event, "pxepathprefix");
257 bootfile = event_get_param(event, "bootfile");
259 /* If we're given a conf file, we're able to generate a complete URL to
260 * the configuration file, and the parser doesn't need to do any
261 * further autodiscovery */
262 *is_complete = !!conffile;
264 /* if conffile is a URL, that's all we need */
265 if (conffile && is_url(conffile)) {
266 url = pb_url_parse(ctx, conffile);
270 /* If we can create a URL from pathprefix (optionally with
271 * conffile appended to create a complete URL), use that */
272 if (pathprefix && is_url(pathprefix)) {
274 url_str = talloc_asprintf(ctx, "%s%s",
275 pathprefix, conffile);
276 url = pb_url_parse(ctx, url_str);
277 talloc_free(url_str);
279 url = pb_url_parse(ctx, pathprefix);
285 host = parse_host_addr(event);
287 pb_log("%s: host address not found\n", __func__);
291 url_str = talloc_asprintf(ctx, "tftp://%s/", host);
293 /* if we have a pathprefix, use that directly.. */
295 /* strip leading slashes */
296 while (pathprefix[0] == '/')
298 url_str = talloc_asprintf_append(url_str, "%s", pathprefix);
300 /* ... otherwise, add a path based on the bootfile name, but only
301 * if conffile isn't an absolute path itself */
302 } else if (bootfile && !(conffile && conffile[0] == '/')) {
304 basedir = talloc_strdup(ctx, bootfile);
306 /* strip filename from the bootfile path, leaving only a
308 p = strrchr(basedir, '/');
314 url_str = talloc_asprintf_append(url_str, "%s/",
317 talloc_free(basedir);
320 /* finally, append conffile */
322 url_str = talloc_asprintf_append(url_str, "%s", conffile);
324 url = pb_url_parse(ctx, url_str);
326 talloc_free(url_str);
331 char **user_event_parse_conf_filenames(
332 struct discover_context *ctx, struct event *event)
334 char *mac_addr, *ip_hex;
335 const char *mac, *ip;
339 mac = event_get_param(event, "mac");
341 mac_addr = parse_mac_addr(ctx, mac);
345 ip = event_get_param(event, "ip");
347 ip_hex = parse_ip_addr(ctx, ip);
348 len = strlen(ip_hex);
354 if (!mac_addr && !ip_hex) {
355 pb_log("%s: neither mac nor ip parameter found\n", __func__);
359 /* Filenames as fallback IP's + mac + default */
360 filenames = talloc_array(ctx, char *, len + 3);
364 filenames[index++] = talloc_strdup(filenames, mac_addr);
367 filenames[index++] = talloc_strdup(filenames, ip_hex);
368 ip_hex[--len] = '\0';
371 filenames[index++] = talloc_strdup(filenames, "default");
372 filenames[index++] = NULL;
375 talloc_free(mac_addr);
383 static int user_event_dhcp(struct user_event *uev, struct event *event)
385 struct device_handler *handler = uev->handler;
386 struct discover_device *dev;
388 dev = discover_device_create(handler, event->device);
390 device_handler_dhcp(handler, dev, event);
395 static int user_event_conf(struct user_event *uev, struct event *event)
397 struct device_handler *handler = uev->handler;
398 struct discover_device *dev;
402 val = event_get_param(event, "url");
406 url = pb_url_parse(event, val);
410 dev = discover_device_create(handler, event->device);
412 device_handler_conf(handler, dev, url);
417 static int user_event_add(struct user_event *uev, struct event *event)
419 struct device_handler *handler = uev->handler;
420 struct discover_context *ctx;
421 struct discover_device *dev;
423 dev = discover_device_create(handler, event->device);
424 ctx = device_handler_discover_context_create(handler, dev);
426 parse_user_event(ctx, event);
428 device_handler_discover_context_commit(handler, ctx);
435 static int user_event_remove(struct user_event *uev, struct event *event)
437 struct device_handler *handler = uev->handler;
438 struct discover_device *dev;
440 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 void user_event_handle_message(struct user_event *uev, char *buf,
497 event = talloc(uev, struct event);
498 event->type = EVENT_TYPE_USER;
500 result = event_parse_ad_message(event, buf, len);
505 user_event_print_event(event);
507 switch (event->action) {
508 case EVENT_ACTION_ADD:
509 result = user_event_add(uev, event);
511 case EVENT_ACTION_REMOVE:
512 result = user_event_remove(uev, event);
514 case EVENT_ACTION_URL:
515 result = user_event_url(uev, event);
517 case EVENT_ACTION_CONF:
518 result = user_event_conf(uev, event);
520 case EVENT_ACTION_DHCP:
521 result = user_event_dhcp(uev, event);
523 case EVENT_ACTION_BOOT:
524 result = user_event_boot(uev, event);
526 case EVENT_ACTION_SYNC:
527 result = user_event_sync(uev, event);
538 static int user_event_process(void *arg)
540 struct user_event *uev = arg;
541 char buf[PBOOT_USER_EVENT_SIZE + 1];
544 len = recvfrom(uev->socket, buf, PBOOT_USER_EVENT_SIZE, 0, NULL, NULL);
547 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
552 pb_log("%s: empty", __func__);
558 pb_debug("%s: %u bytes\n", __func__, len);
560 user_event_handle_message(uev, buf, len);
565 static int user_event_destructor(void *arg)
567 struct user_event *uev = arg;
569 pb_debug("%s\n", __func__);
571 if (uev->socket >= 0)
577 struct user_event *user_event_init(struct device_handler *handler,
578 struct waitset *waitset)
580 struct sockaddr_un addr;
581 struct user_event *uev;
583 unlink(PBOOT_USER_EVENT_SOCKET);
585 uev = talloc(handler, struct user_event);
587 uev->handler = handler;
589 uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
590 if (uev->socket < 0) {
591 pb_log("%s: Error creating event handler socket: %s\n",
592 __func__, strerror(errno));
596 talloc_set_destructor(uev, user_event_destructor);
598 memset(&addr, 0, sizeof(addr));
599 addr.sun_family = AF_UNIX;
600 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
602 if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
603 pb_log("Error binding event handler socket: %s\n",
607 waiter_register_io(waitset, uev->socket, WAIT_IN,
608 user_event_process, uev);
610 pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);