summaryrefslogtreecommitdiffstats
path: root/tests/inputfilegen.c
blob: b4e9c2dd8e0c91c2eee9d0aa50fc58f864badeac (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
/* generate an input file suitable for use by the testbench
 * Copyright (C) 2018-2022 by Rainer Gerhards and Adiscon GmbH.
 * Copyright (C) 2016-2018 by Pascal Withopf and Adiscon GmbH.
 *
 * usage: ./inputfilegen num-lines > file
 * -m number of messages
 * -i start number of message
 * -d extra data to add (all 'X' after the msgnum)
 * -s size of file to generate
 *  cannot be used together with -m
 *  size may be slightly smaller in order to be able to write
 *  the last complete line.
 * -M "message count file" - contains number of messages to be written
 *  This is especially useful with -s, as the testbench otherwise does
 *  not know how to do a seq_check. To keep things flexible, can also be
 *  used with -m (this may aid testbench framework generalization).
 * -f outputfile
 *  Permits to write data to file "outputfile" instead of stdout. Also
 *  enables support for SIGHUP.
 * -S sleep time
 *  ms to sleep between sending message bulks (bulks size given by -B)
 * -B number of messages in bulk
 *  number of messages to send without sleeping as specified in -S.
 *  IGNORED IF -S is not also given!
 * Part of rsyslog, licensed under ASL 2.0
 */
#include "config.h"
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#if defined(_AIX)
	#include  <unistd.h>
#else
	#include <getopt.h>
#endif
#if defined(__FreeBSD__)
#include <sys/time.h>
#else
#include <time.h>
#endif
#if defined(HAVE_SYS_SELECT_H)
#include <sys/select.h>
#endif

#define DEFMSGS 5
#define NOEXTRADATA -1

static volatile int bHadHUP = 0;
static void
hdlr_sighup(int sig)
{
	fprintf(stderr, "inputfilegen: had hup, sig %d\n", sig);
	bHadHUP = 1;
}
static void
sighup_enable()
{
	struct sigaction sigAct;
	memset(&sigAct, 0, sizeof (sigAct));
	sigemptyset(&sigAct.sa_mask);
	sigAct.sa_handler = hdlr_sighup;
	sigaction(SIGHUP, &sigAct, NULL);
}

void msleep(const int sleepTime)
{
	struct timeval tvSelectTimeout;

	tvSelectTimeout.tv_sec = sleepTime / 1000;
	tvSelectTimeout.tv_usec = (sleepTime % 1000) * 1000; /* micro seconds */
	if(select(0, NULL, NULL, NULL, &tvSelectTimeout) == -1) {
		if(errno != EINTR) {
			perror("select");
			exit(1);
		}
	}
}

static FILE *
open_output(const char *fn)
{
	FILE *fh_output = fopen(fn, "w");
	if(fh_output == NULL) {
		perror(fn);
		exit(1);
	}
	return fh_output;
}

int main(int argc, char* argv[])
{
	int c, i;
	long long nmsgs = DEFMSGS;
	long long nmsgstart = 0;
	int nfinishedidle = 0;
	int nchars = NOEXTRADATA;
	int errflg = 0;
	long long filesize = -1;
	char *extradata = NULL;
	const char *msgcntfile = NULL;
	const char *outputfile = "-";
	FILE *fh_output;
	int sleep_ms = 0;		/* How long to sleep between message writes */
	int sleep_msgs = 0;		/* messages to xmit between sleeps (if configured) */
	int sleep_hubreopen_ms = 5;	/* Wait until new file is being reopened, logrotate may need some time */
	int ctr = 0;

	while((c=getopt(argc, argv, "m:M:i:I:h:d:s:f:S:B:")) != -1) {
		switch(c) {
		case 'm':
			nmsgs = atoi(optarg);
			break;
		case 'M':
			msgcntfile = optarg;
			break;
		case 'i':
			nmsgstart = atoi(optarg);
			break;
		case 'I':
			nfinishedidle = atoi(optarg);
			break;
		case 'd':
			nchars = atoi(optarg);
			break;
		case 's':
			filesize = atoll(optarg);
			break;
		case 'S':
			sleep_ms = atoi(optarg);
			break;
		case 'B':
			sleep_msgs = atoi(optarg);
			break;
		case 'h':
			sleep_hubreopen_ms = atoi(optarg);
			break;
		case 'f':
			outputfile = optarg;
			sighup_enable();
			break;
		case ':':
			fprintf(stderr, "Option -%c requires an operand\n", optopt);
			errflg++;
			break;
		case '?':
			fprintf(stderr, "inputfilegen: Unrecognized option: -%c\n", optopt);
			errflg++;
			break;
		}
	}
	if(errflg) {
		fprintf(stderr, "invalid call\n");
		exit(2);
	}

	if(filesize != -1) {
		const int linesize = (17 + nchars); /* 17 is std line size! */
		nmsgs = filesize / linesize;
		fprintf(stderr, "inputfilegen: file size requested %lld, actual %lld with "
			"%lld lines, %lld bytes less\n",
			filesize, nmsgs * linesize, nmsgs, filesize - nmsgs * linesize);
		if(nmsgs > 100000000) {
			fprintf(stderr, "inputfilegen: number of lines exhaust 8-digit line numbers "
				"which are standard inside the testbench.\n"
				"Use -d switch to add extra data (e.g. -d111 for "
				"128 byte lines or -d47 for 64 byte lines)\n");
			exit(1);
		}
	}

	if(msgcntfile != NULL) {
		FILE *const fh = fopen(msgcntfile, "w");
		if(fh == NULL) {
			perror(msgcntfile);
			exit(1);
		}
		fprintf(fh, "%lld", nmsgs);
		fclose(fh);
	}

	if(strcmp(outputfile, "-")) {
		fh_output = open_output(outputfile);
	} else {
		fh_output = stdout;
	}

	if(nchars != NOEXTRADATA) {
		extradata = (char *)malloc(nchars + 1);
		memset(extradata, 'X', nchars);
		extradata[nchars] = '\0';
	}
	for(i = nmsgstart; i < (nmsgs+nmsgstart); ++i) {
		if(	sleep_ms > 0 &&
			ctr++ >= sleep_msgs) {
			msleep(sleep_ms);
			ctr = 0;
		}
		if(bHadHUP) {
			fclose(fh_output);
			if(sleep_hubreopen_ms > 0) {
				/* Extra Sleep so logrotate or whatever can take place */
				msleep(sleep_hubreopen_ms);
			}
			fh_output = open_output(outputfile);
			fprintf(stderr, "inputfilegen: %s reopened\n", outputfile);
			fprintf(stderr, "inputfilegen: current message number %d\n", i);
			bHadHUP = 0;
		}
		fprintf(fh_output, "msgnum:%8.8d:", i);
		if(nchars != NOEXTRADATA) {
			fprintf(fh_output, "%s", extradata);
		}
		fprintf(fh_output, "\n");
	}
	if (nfinishedidle > 0) {
		/* Keep process open for nfinishedidle ms */
		for(int x = 0; x < nfinishedidle; ++x) {
			if(bHadHUP) {
				/* Create empty file */
				fh_output = open_output(outputfile);
				fclose(fh_output);
				fprintf(stderr, "inputfilegen: last message number was %d\n", i);
				bHadHUP = 0;
			}
			msleep(1);
		}
	}

	free(extradata);
	return 0;
}