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)
31 #include <sys/socket.h>
32 #include <sys/types.h>
35 #include "discover/user-event.h"
38 #define DBG(_args...) do {fprintf(stderr, _args); fflush(stderr); } while (0)
40 static inline int __attribute__ ((format (printf, 1, 2))) DBG(
41 __attribute__((unused)) const char *fmt, ...) {return 0; }
44 static void print_version(void)
46 printf("pb-event (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
49 static void print_usage(void)
53 "Usage: pb-event [-h] [event...]\n"
55 " Send a single petitboot user event to the petitboot discover server.\n"
56 " Events can be read from stdin, or provided on the command line.\n"
57 " User events must have the following format:\n"
59 " (add|remove)@device-id [name=value] [image=value] [args=value]\n"
61 " When read from stdin, components are separated by NUL chars\n"
66 " pb-event add@/net/eth0 name=netboot image=tftp://192.168.1.10/vmlinux\n"
67 " pb-event remove@/net/eth0\n"
70 " printf 'add@/net/eth0\\0name=netboot\\0image=tftp://10.0.0.2/vmlinux\\0' \\\n"
72 " printf 'remove@/net/eth0\\0' | pb-event\n"
76 static const char *err_max_size = "pb-event: message too large "
79 static ssize_t parse_event_args(int n, char * const * args,
80 char *buf, size_t max_len)
82 ssize_t arg_len, total_len;
88 for (i = 0; i < n; i++) {
90 arg_len = strlen(arg);
92 if (total_len + (size_t)i + 1 > max_len) {
93 fprintf(stderr, err_max_size, max_len);
97 memcpy(buf + total_len, arg, arg_len);
100 buf[total_len] = '\0';
108 static ssize_t parse_event_file(FILE *filp, char *buf, size_t max_len)
112 len = fread(buf, 1, max_len, filp);
115 fprintf(stderr, err_max_size, max_len);
125 static int send_event(char *buf, ssize_t len)
127 struct sockaddr_un addr;
130 sd = socket(PF_UNIX, SOCK_DGRAM, 0);
132 err(EXIT_FAILURE, "socket");
134 memset(&addr, 0, sizeof(addr));
135 addr.sun_family = AF_UNIX;
136 strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
138 for (i = 10; i; i--) {
139 ssize_t sent = sendto(sd, buf, len, 0,
140 (struct sockaddr *)&addr, SUN_LEN(&addr));
145 DBG("pb-event: waiting for server %d\n", i);
152 err(EXIT_FAILURE, "send");
154 DBG("pb-event: wrote %zu bytes\n", len);
159 int main(int argc, char *argv[])
161 char buf[PBOOT_USER_EVENT_SIZE];
164 if (argc >= 2 && !strcmp(argv[1], "-h")) {
170 len = parse_event_args(argc - 1, argv + 1,
173 len = parse_event_file(stdin, buf, sizeof(buf));
179 send_event(buf, len);