-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifologger.c
230 lines (198 loc) · 5.47 KB
/
fifologger.c
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
/*
* fifologger $Id: fifologger.c,v 1.11 2013/01/23 22:08:16 project Exp project $
*
* Reads input from a FIFO and writes it into a file specified with strftime(3)
* syntax.
*
* Outfile is reopened hourly and fflush():ed at intervals determined by
* OUT_SYNC_INTERVAL.
*
* Suitable format: xferlog.%Y%m%d (xferlog-20011027)
*
* -- stric 2001-10-27 - Initial implementation.
* -- nikke 2004-09-18 - Added functionality to not fopen/fclose outfile on
* each fgets.
*/
/*
* Copyright (C) 2001 Tomas Forsman (né Ögren)
* Copyright (C) 2004 Niklas Edmundsson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Enable large file API, it's needed */
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE 1
#define _LARGE_FILES 1
#include <stdio.h>
#include <signal.h>
#include <limits.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#define STRSIZE 32768
/* Interval between forced fflush() of output file */
#define OUT_SYNC_INTERVAL 10
/* Wait this long for more input, then flush output file */
#define OUT_SYNC_DELAY 10
/* RCS version strings and stuff, to be used with in help text or when running
ident on the binary */
static const char rcsid[] = "$Id: fifologger.c,v 1.11 2013/01/23 22:08:16 project Exp project $";
FILE *fifo;
char *fifoname;
char *outformat;
int dolog = 1;
void
exithandler(int signum) {
fflush(NULL);
exit(1);
}
void
alarmhandler(int signum) {
fflush(NULL);
}
void
error(int lvl, char *str, char *arg) {
char buf[STRSIZE];
snprintf(buf, STRSIZE, "[%%s] %s: %s", str, strerror(errno));
syslog(lvl, buf, fifoname, arg);
if (dolog) {
strcat(buf, "\n");
fprintf(stderr, buf, fifoname, arg);
}
}
FILE *
openfifo(char *name) {
FILE *f;
fifoname = name;
f = fopen(name, "r");
if (f == NULL) {
error(LOG_ERR, "Unable to open fifo %s", name);
closelog();
exit(1);
}
return f;
}
int
writeline(char *line) {
time_t t;
static FILE *outf = NULL;
static time_t outfclosetime = 0;
static time_t outflastflush = 0;
t = time(NULL);
/* Close outfile if it has been open too long */
if( outf && t>=outfclosetime) {
fclose(outf);
outf=NULL;
}
/* Open outfile if not already open */
if(!outf) {
char buf[PATH_MAX+1];
struct tm *tim = localtime(&t);
struct tm hrtim;
strftime(buf, PATH_MAX, outformat, tim);
outf = fopen(buf, "a");
if (!outf) {
error(LOG_CRIT, "Unable to open outfile %s", buf);
return 1;
}
/* Figure out when it's time to close it (when the next hour starts) */
memcpy(&hrtim, tim, sizeof(struct tm));
hrtim.tm_sec=0;
hrtim.tm_min=0;
outfclosetime = mktime(&hrtim)+3600;
}
if (fputs(line, outf) == EOF) {
error(LOG_CRIT, "Unable to write to outfile %s", outformat);
fclose(outf);
outf=NULL;
return 2;
}
if(outflastflush+OUT_SYNC_INTERVAL < t) {
fflush(outf);
outflastflush=t;
}
return 0;
}
void
mainloop(void) {
char *buf = malloc(STRSIZE+1);
char *s;
while (1) {
alarm(OUT_SYNC_DELAY);
s = fgets(buf, STRSIZE, fifo);
alarm(0);
if (s == 0) {
sleep(1);
} else {
/* Doesn't handle return values, don't know if they're worth much
though.. */
writeline(buf);
}
}
}
int
main(int argc, char *argv[]) {
struct sigaction sa;
if (argc != 3) {
printf("fifologger %s\n", rcsid);
printf("Usage: %s: <fifo> <outformat>\n\n", argv[0]);
printf("Don't use any relative paths (will cd to /).\n");
exit(0);
}
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
/* Trap some standard signals so we can fflush() the outfile on exit */
sa.sa_handler = exithandler;
if(sigaction(SIGHUP, &sa, NULL)) {
perror("sigaction");
exit(1);
}
if(sigaction(SIGINT, &sa, NULL)) {
perror("sigaction");
exit(1);
}
if(sigaction(SIGTERM, &sa, NULL)) {
perror("sigaction");
exit(1);
}
if(sigaction(SIGQUIT, &sa, NULL)) {
perror("sigaction");
exit(1);
}
/* An alarm handler that flushes the output stream if we get no
action for a while */
sa.sa_handler = alarmhandler;
sa.sa_flags = SA_RESTART; /* To avoid packet loss in fgets() */
if(sigaction(SIGALRM, &sa, NULL)) {
perror("sigaction");
exit(1);
}
if(chdir("/") < 0) {
perror("chdir /");
}
openlog("fifologger", LOG_PID, LOG_DAEMON);
fifo = openfifo(argv[1]);
outformat = argv[2];
fclose(stdin);
fclose(stdout);
fclose(stderr);
dolog = 0;
mainloop();
closelog();
return 0;
}