summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/load_tester/load_tester.c
blob: b7b971ee8445b2b8f8a580ba942dac1b7bc9fd53 (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
/*
 * Copyright (C) 2012 Martin Willi
 * Copyright (C) 2012 revosec AG
 *
 * 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; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include "load_tester_control.h"

#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

/**
 * Connect to the daemon, return stream
 */
static FILE* make_connection()
{
	struct sockaddr_un addr;
	FILE *stream;
	int fd;

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, LOAD_TESTER_SOCKET);

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd < 0)
	{
		fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
		return NULL;
	}
	if (connect(fd, (struct sockaddr *)&addr,
			offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)) < 0)
	{
		fprintf(stderr, "connecting to %s failed: %s\n",
				LOAD_TESTER_SOCKET, strerror(errno));
		close(fd);
		return NULL;
	}
	stream = fdopen(fd, "r+");
	if (!stream)
	{
		close(fd);
		return NULL;
	}
	return stream;
}

/**
 * Initiate load-tests
 */
static int initiate(unsigned int count, unsigned int delay)
{
	FILE *stream;
	char c;

	stream = make_connection();
	if (!stream)
	{
		return 1;
	}

	fprintf(stream, "%u %u\n", count, delay);

	while (1)
	{
		fflush(stream);
		c = fgetc(stream);
		if (c == EOF)
		{
			break;
		}
		if (fputc(c, stdout) == EOF)
		{
			break;
		}
		fflush(stdout);
	}
	fclose(stream);
	return 0;
}

int main(int argc, char *argv[])
{
	if (argc >= 3 && strcmp(argv[1], "initiate") == 0)
	{
		return initiate(atoi(argv[2]), argc > 3 ? atoi(argv[3]) : 0);
	}
	fprintf(stderr, "Usage:\n");
	fprintf(stderr, "  %s initiate <count> [<delay in ms>]\n", argv[0]);
	return 1;
}