summaryrefslogtreecommitdiffstats
path: root/utils/pb-event.c
blob: 4a167192c7a99c34a172b7a9240e8b1dfbd9d37e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  Copyright (C) 2009 Sony Computer Entertainment Inc.
 *  Copyright 2009 Sony Corp.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#define _GNU_SOURCE
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include "discover/user-event.h"

#if defined(DEBUG)
#define DBG(_args...) do {fprintf(stderr, _args); fflush(stderr); } while (0)
#else
static inline int __attribute__ ((format (printf, 1, 2))) DBG(
	__attribute__((unused)) const char *fmt, ...) {return 0; }
#endif

static void print_version(void)
{
	printf("pb-event (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
}

static void print_usage(void)
{
	print_version();
	printf(
"Usage: pb-event [-h] [event...]\n"
"\n"
"       Send a single petitboot user event to the petitboot discover server.\n"
"       Events can be read from stdin, or provided on the command line.\n"
"       User events must have the following format:\n"
"\n"
"         (add|remove)@device-id [name=value] [image=value] [args=value]\n"
"\n"
"       When read from stdin, components are separated by NUL chars\n"
"\n"
"Examples:\n"
"\n"
"    args:\n"
"       pb-event add@/net/eth0 name=netboot image=tftp://192.168.1.10/vmlinux\n"
"       pb-event remove@/net/eth0\n"
"\n"
"    stdin:\n"
"       printf 'add@/net/eth0\\0name=netboot\\0image=tftp://10.0.0.2/vmlinux\\0' \\\n"
"           | pb-event\n"
"       printf 'remove@/net/eth0\\0' | pb-event\n"
"\n");
}

static const char *err_max_size = "pb-event: message too large "
					"(%zu byte max)\n";

static ssize_t parse_event_args(int n, char * const * args,
		char *buf, size_t max_len)
{
	ssize_t arg_len, total_len;
	const char *arg;
	int i;

	total_len = 0;

	for (i = 0; i < n; i++) {
		arg = args[i];
		arg_len = strlen(arg);

		if (total_len + (size_t)i + 1 > max_len) {
			fprintf(stderr, err_max_size, max_len);
			return -1;
		}

		memcpy(buf + total_len, arg, arg_len);
		total_len += arg_len;

		buf[total_len] = '\0';
		total_len++;
	}

	return total_len;

}

static ssize_t parse_event_file(FILE *filp, char *buf, size_t max_len)
{
	int len;

	len = fread(buf, 1, max_len, filp);

	if (!feof(filp)) {
		fprintf(stderr, err_max_size, max_len);
		return -1;
	}

	if (!len)
		return -1;

	return len;
}

static int send_event(char *buf, ssize_t len)
{
	struct sockaddr_un addr;
	int sd, i;

	sd = socket(PF_UNIX, SOCK_DGRAM, 0);
	if (sd < 0)
		err(EXIT_FAILURE, "socket");

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);

	for (i = 10; i; i--) {
		ssize_t sent = sendto(sd, buf, len, 0,
				(struct sockaddr *)&addr, SUN_LEN(&addr));

		if (sent == len)
			break;

		DBG("pb-event: waiting for server %d\n", i);
		sleep(1);
	}

	close(sd);

	if (!i)
		err(EXIT_FAILURE, "send");

	DBG("pb-event: wrote %zu bytes\n", len);

	return 0;
}

int main(int argc, char *argv[])
{
	char buf[PBOOT_USER_EVENT_SIZE];
	ssize_t len;

	if (argc >= 2 && !strcmp(argv[1], "-h")) {
		print_usage();
		return EXIT_SUCCESS;
	}

	if (argc > 1) {
		len = parse_event_args(argc - 1, argv + 1,
					buf, sizeof(buf));
	} else {
		len = parse_event_file(stdin, buf, sizeof(buf));
	}

	if (len < 0)
		return EXIT_FAILURE;

	send_event(buf, len);

	return EXIT_SUCCESS;
}
OpenPOWER on IntegriCloud