File: writelog.c

package info (click to toggle)
gatling 0.13-6
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 1,196 kB
  • ctags: 1,115
  • sloc: ansic: 23,805; makefile: 143; sh: 71; perl: 30
file content (291 lines) | stat: -rw-r--r-- 6,746 bytes parent folder | download | duplicates (3)
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
/*
 * writelog.c  -  writes output of a daemon to a logfile, logrotatable
 *
 * Copyright (C) 2011 Roland Stigge <stigge@antcom.de>
 *
 * Writelog 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 3 of the License, or
 * (at your option) any later version.
 *
 * Writelog 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 Writelog; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <error.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/stat.h>

#define WITH_TIMESTAMP
#define BUFSIZE 4096

int terminate_flag = 0;
int reopen_flag = 0;
int sighup_flag = 0;

/* Handle signals:
   SIGTERM (exit) 
   SIGUSR1 (reopen logfile by writelog)
   SIGHUP (delegate to gatling - close listening sockets and exit after serving)
 */

void sigterm_handler(int signum) {
	terminate_flag = 1;
}

struct sigaction sigterm = {
	.sa_handler = sigterm_handler,
	.sa_flags = SA_RESTART,
};

void sigusr1_handler(int signum) {
	reopen_flag = 1;
}

struct sigaction sigusr1 = {
	.sa_handler = sigusr1_handler,
	.sa_flags = SA_RESTART,
};

void sighup_handler(int signum) {
	sighup_flag = 1;
}

struct sigaction sighup = {
	.sa_handler = sighup_handler,
	.sa_flags = SA_RESTART,
};

void usage(void) {
	printf("usage: writelog <logfile> <commandline>\n");
}

static void write_block(int fd, char* buffer, int size) {
	int offset = 0;
	int written;
	
	do {
		written = write(fd, &buffer[offset], size - offset);
		if (written == -1) {
			fprintf(stderr, "Error on write(): %s\n", strerror(errno));
			exit(1);
		}
		offset += written;
	} while (offset != size);
}
				
#ifdef WITH_TIMESTAMP
static void write_timestamped(int fd, char* buffer, int size) {
	int offset = 0;
	time_t t;
	int t_len;
	char t_string[100];
	static int write_timestamp = 1; // flag; initially, write timestamp

	t = time(NULL); // current time
	if (ctime_r(&t, t_string) != &t_string[0]) { // format it
		fprintf(stderr, "Error on ctime_r(): %s\n", strerror(errno));
		exit(1);
	}

	t_len = strlen(t_string);
	t_string[t_len - 1] = ' '; // replace trailing newline with space

	while (offset != size) {
		int chunksize = 0;

		// find end of line
		while (offset + chunksize < size && buffer[offset + chunksize] != '\n') {
			chunksize++;
		}
		if (offset + chunksize < size && buffer[offset + chunksize] == '\n') {
			chunksize++;
		}

		if (write_timestamp)
			write_block(fd, t_string, t_len);
		write_block(fd, &buffer[offset], chunksize);
		write_timestamp = 1;
		offset += chunksize;
	}

	if (size) {
		if (buffer[size - 1] == '\n') // write timestamp next time?
			write_timestamp = 1;
		else
			write_timestamp = 0;
	}
}
#endif

int main(int argc, char* argv[]) {
	int pipefds[2]; // read fd, write fd
	int pid;
	int res;
	char buffer[BUFSIZE];
	int logfd;

	if (argc < 3) {
		usage();
		exit(1);
	}

	if (pipe(&pipefds[0])) {
		fprintf(stderr, "Error on pipe(): %s\n", strerror(errno));
		exit(1);
	}

	pid = fork();
	if (pid == (pid_t)0) { // child process
		if (close(pipefds[0])) {
			fprintf(stderr, "Error on close(): %s\n", strerror(errno));
			exit(1);
		}

		res = dup2(pipefds[1], 1); // stdout of child redirects to pipe
		if (res < 0) {
			fprintf(stderr, "Error on dup2(): %s\n", strerror(errno));
			exit(1);
		}
		res = dup2(pipefds[1], 2); // stderr of child redirects to pipe
		if (res < 0) {
			fprintf(stderr, "Error on dup2(): %s\n", strerror(errno));
			exit(1);
		}

		res = execvp(argv[2], &argv[2]); // normally, doesn't return
		if (res == -1) {
			fprintf(stderr, "Error on execvp(): %s\n", strerror(errno));
		} else {
			fprintf(stderr, "Error: execvp() returned %d\n", res);
		}
		exit(1);
	} else if (pid < 0) { // error
		fprintf(stderr, "Error on fork(): %s\n", strerror(errno));
		exit(1);
	}
	
	// parent
	if (close(pipefds[1])) {
		fprintf(stderr, "Error on close(): %s\n", strerror(errno));
		exit(1);
	}

	// register signal handlers
	res = sigaction(SIGTERM, &sigterm, NULL);
	if (res == -1) {
		fprintf(stderr, "Error on sigaction(): %s\n", strerror(errno));
		exit(1);
	}

	res = sigaction(SIGUSR1, &sigusr1, NULL);
	if (res == -1) {
		fprintf(stderr, "Error on sigaction(): %s\n", strerror(errno));
		exit(1);
	}

	res = sigaction(SIGHUP, &sighup, NULL);
	if (res == -1) {
		fprintf(stderr, "Error on sigaction(): %s\n", strerror(errno));
		exit(1);
	}

	logfd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT,
			S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
	if (logfd < 0) {
		fprintf(stderr, "Error on logfile open(): %s\n", strerror(errno));
		exit(1);
	}

	// main loop
	do {
		fd_set set;

		FD_ZERO(&set);
		FD_SET(pipefds[0], &set);

		res = select(FD_SETSIZE, &set, NULL, NULL, NULL);
		if (res == 1) { // data
			int got;

			got = read(pipefds[0], buffer, BUFSIZE);
			if (got == -1) {
				fprintf(stderr, "Error on read(): %s\n", strerror(errno));
				exit(1);
			} else if (got > 0) {
#ifdef WITH_TIMESTAMP
				write_timestamped(logfd, buffer, got);
#else
				write_block(logfd, buffer, got);
#endif
			} else {
				fprintf(stderr, "Warning: Unhandled return value %d from read()\n", got);
			}
		} else if (res == -1) { // error
			if (errno != EINTR) { // EINTR for signal is OK
				fprintf(stderr, "Error on signal(): %s\n", strerror(errno));
				exit(1);
			}
		} else { // 
			fprintf(stderr, "Unknown return from on signal(): %d\n", res);
			exit(1);
		}

		if (terminate_flag) {
			kill(pid, SIGTERM);
		}

		if (sighup_flag) {
			kill(pid, SIGHUP);
			terminate_flag = 1;
		}

		if (reopen_flag) {
			if (close(logfd)) {
				fprintf(stderr, "Error on logfd close(): %s\n", strerror(errno));
				exit(1);
			}
			logfd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT,
					S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
			if (logfd < 0) {
				fprintf(stderr, "Error on logfile open(): %s\n", strerror(errno));
				exit(1);
			}

			reopen_flag = 0;
		}
	} while (!terminate_flag);

	if (close(logfd)) {
		fprintf(stderr, "Error on logfd close(): %s\n", strerror(errno));
		exit(1);
	}

	if (!sighup_flag) { // on sighup, just leave old gatling process alone
		res = wait(NULL); // wait for the child to terminate
		if (res == -1) {
			fprintf(stderr, "Error on wait(): %s\n", strerror(errno));
			exit(1);
		}
	}

	return 0;
}