aboutsummaryrefslogtreecommitdiff
path: root/tracecmd/trace-agent.c
blob: f0723a6601a4b1d28ba5c9d3f4b5a57dc31bc2ea (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018 VMware Inc, Slavomir Kaslev <kaslevs@vmware.com>
 *
 * based on prior implementation by Yoshihiro Yunomae
 * Copyright (C) 2013 Hitachi, Ltd.
 * Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>
 */

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>

#include "trace-local.h"
#include "trace-msg.h"

#define dprint(fmt, ...)	tracecmd_debug(fmt, ##__VA_ARGS__)

static void make_vsocks(int nr, int *fds, unsigned int *ports)
{
	unsigned int port;
	int i, fd, ret;

	for (i = 0; i < nr; i++) {
		fd = trace_vsock_make_any();
		if (fd < 0)
			die("Failed to open vsocket");

		ret = trace_vsock_get_port(fd, &port);
		if (ret < 0)
			die("Failed to get vsocket address");

		fds[i] = fd;
		ports[i] = port;
	}
}

static void make_net(int nr, int *fds, unsigned int *ports)
{
	int port;
	int i, fd;
	int start_port = START_PORT_SEARCH;

	for (i = 0; i < nr; i++) {
		port = trace_net_search(start_port, &fd, USE_TCP);
		if (port < 0)
			die("Failed to open socket");
		if (listen(fd, 5) < 0)
			die("Failed to listen on port %d\n", port);
		fds[i] = fd;
		ports[i] = port;
		dprint("CPU[%d]: fd:%d port:%d\n", i, fd, port);
		start_port = port + 1;
	}
}

static void make_sockets(int nr, int *fds, unsigned int *ports,
			 const char * network)
{
	if (network)
		return make_net(nr, fds, ports);
	else
		return make_vsocks(nr, fds, ports);
}

static int open_agent_fifos(int nr_cpus, int *fds)
{
	char path[PATH_MAX];
	int i, fd, ret;

	for (i = 0; i < nr_cpus; i++) {
		snprintf(path, sizeof(path), VIRTIO_FIFO_FMT, i);
		fd = open(path, O_WRONLY);
		if (fd < 0) {
			ret = -errno;
			goto cleanup;
		}

		fds[i] = fd;
	}

	return 0;

cleanup:
	while (--i >= 0)
		close(fds[i]);

	return ret;
}

static char *get_clock(int argc, char **argv)
{
	int i;

	if (!argc || !argv)
		return NULL;

	for (i = 0; i < argc - 1; i++) {
		if (!strcmp("-C", argv[i]))
			return argv[i+1];
	}
	return NULL;
}

static void trace_print_connection(int fd, const char *network)
{
	int ret;

	if (network)
		ret = trace_net_print_connection(fd);
	else
		ret = trace_vsock_print_connection(fd);
	if (ret < 0)
		tracecmd_debug("Could not print connection fd:%d\n", fd);
}

static void agent_handle(int sd, int nr_cpus, int page_size, const char *network)
{
	struct tracecmd_tsync_protos *tsync_protos = NULL;
	struct tracecmd_time_sync *tsync = NULL;
	struct tracecmd_msg_handle *msg_handle;
	char *tsync_proto = NULL;
	unsigned long long trace_id;
	unsigned int remote_id;
	unsigned int local_id;
	unsigned int tsync_port = 0;
	unsigned int *ports;
	char **argv = NULL;
	int argc = 0;
	bool use_fifos;
	int *fds;
	int ret;
	int fd;

	fds = calloc(nr_cpus, sizeof(*fds));
	ports = calloc(nr_cpus, sizeof(*ports));
	if (!fds || !ports)
		die("Failed to allocate memory");

	msg_handle = tracecmd_msg_handle_alloc(sd, 0);
	if (!msg_handle)
		die("Failed to allocate message handle");

	ret = tracecmd_msg_recv_trace_req(msg_handle, &argc, &argv,
					  &use_fifos, &trace_id,
					  &tsync_protos);
	if (ret < 0)
		die("Failed to receive trace request");

	if (use_fifos && open_agent_fifos(nr_cpus, fds))
		use_fifos = false;

	if (!use_fifos)
		make_sockets(nr_cpus, fds, ports, network);
	if (tsync_protos && tsync_protos->names) {
		if (network) {
			/* For now just use something */
			remote_id = 2;
			local_id = 1;
			tsync_port = trace_net_search(START_PORT_SEARCH, &fd, USE_TCP);
			if (listen(fd, 5) < 0)
				die("Failed to listen on %d\n", tsync_port);
		} else {
			if (get_vsocket_params(msg_handle->fd, &local_id,
					       &remote_id)) {
				warning("Failed to get local and remote ids");
				/* Just make something up */
				remote_id = -1;
				local_id = -2;
			}
			fd = trace_vsock_make_any();
			if (fd >= 0 &&
			    trace_vsock_get_port(fd, &tsync_port) < 0) {
				close(fd);
				fd = -1;
			}
		}
		if (fd >= 0) {
			tsync = tracecmd_tsync_with_host(fd, tsync_protos,
							 get_clock(argc, argv),
							 remote_id, local_id);
		}
		if (tsync) {
			tracecmd_tsync_get_selected_proto(tsync, &tsync_proto);
		} else {
			warning("Failed to negotiate timestamps synchronization with the host");
			if (fd >= 0)
				close(fd);
		}
	}
	trace_id = tracecmd_generate_traceid();
	ret = tracecmd_msg_send_trace_resp(msg_handle, nr_cpus, page_size,
					   ports, use_fifos, trace_id,
					   tsync_proto, tsync_port);
	if (ret < 0)
		die("Failed to send trace response");

	trace_record_agent(msg_handle, nr_cpus, fds, argc, argv,
			   use_fifos, trace_id, network);

	if (tsync) {
		tracecmd_tsync_with_host_stop(tsync);
		tracecmd_tsync_free(tsync);
	}

	if (tsync_protos) {
		free(tsync_protos->names);
		free(tsync_protos);
	}
	free(argv[0]);
	free(argv);
	free(ports);
	free(fds);
	tracecmd_msg_handle_close(msg_handle);
	exit(0);
}

static volatile pid_t handler_pid;

static void handle_sigchld(int sig)
{
	int wstatus;
	pid_t pid;

	for (;;) {
		pid = waitpid(-1, &wstatus, WNOHANG);
		if (pid <= 0)
			break;

		if (pid == handler_pid)
			handler_pid = 0;
	}
}

static pid_t do_fork()
{
	/* in debug mode, we do not fork off children */
	if (tracecmd_get_debug())
		return 0;

	return fork();
}

static void agent_serve(unsigned int port, bool do_daemon, const char *network)
{
	struct sockaddr_storage net_addr;
	struct sockaddr *addr = NULL;
	socklen_t *addr_len_p = NULL;
	socklen_t addr_len = sizeof(net_addr);
	int sd, cd, nr_cpus;
	unsigned int cid;
	pid_t pid;

	signal(SIGCHLD, handle_sigchld);

	if (network) {
		addr = (struct sockaddr *)&net_addr;
		addr_len_p = &addr_len;
	}

	nr_cpus = tracecmd_count_cpus();
	page_size = getpagesize();

	if (network) {
		sd = trace_net_make(port, USE_TCP);
		if (listen(sd, 5) < 0)
			die("Failed to listen on %d\n", port);
	} else
		sd = trace_vsock_make(port);
	if (sd < 0)
		die("Failed to open socket");
	tracecmd_tsync_init();

	if (!network) {
		cid = trace_vsock_local_cid();
		if (cid >= 0)
			printf("listening on @%u:%u\n", cid, port);
	}

	if (do_daemon && daemon(1, 0))
		die("daemon");

	for (;;) {
		cd = accept(sd, addr, addr_len_p);
		if (cd < 0) {
			if (errno == EINTR)
				continue;
			die("accept");
		}
		if (tracecmd_get_debug())
			trace_print_connection(cd, network);

		if (network && !trace_net_cmp_connection(&net_addr, network)) {
			dprint("Client does not match '%s'\n", network);
			close(cd);
			continue;
		}

		if (handler_pid)
			goto busy;

		pid = do_fork();
		if (pid == 0) {
			close(sd);
			signal(SIGCHLD, SIG_DFL);
			agent_handle(cd, nr_cpus, page_size, network);
		}
		if (pid > 0)
			handler_pid = pid;

busy:
		close(cd);
	}
}

enum {
	OPT_verbose	= 254,
	DO_DEBUG	= 255
};

void trace_agent(int argc, char **argv)
{
	bool do_daemon = false;
	unsigned int port = TRACE_AGENT_DEFAULT_PORT;
	const char *network = NULL;

	if (argc < 2)
		usage(argv);

	if (strcmp(argv[1], "agent") != 0)
		usage(argv);

	for (;;) {
		int c, option_index = 0;
		static struct option long_options[] = {
			{"port", required_argument, NULL, 'p'},
			{"help", no_argument, NULL, '?'},
			{"debug", no_argument, NULL, DO_DEBUG},
			{"verbose", optional_argument, NULL, OPT_verbose},
			{NULL, 0, NULL, 0}
		};

		c = getopt_long(argc-1, argv+1, "+hp:DN:",
				long_options, &option_index);
		if (c == -1)
			break;
		switch (c) {
		case 'h':
			usage(argv);
			break;
		case 'N':
			network = optarg;
			break;
		case 'p':
			port = atoi(optarg);
			break;
		case 'D':
			do_daemon = true;
			break;
		case DO_DEBUG:
			tracecmd_set_debug(true);
			break;
		case OPT_verbose:
			if (trace_set_verbose(optarg) < 0)
				die("invalid verbose level %s", optarg);
			break;
		default:
			usage(argv);
		}
	}

	if (optind < argc-1)
		usage(argv);

	agent_serve(port, do_daemon, network);
}