-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext.c
262 lines (218 loc) · 5.25 KB
/
context.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
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
/* $Id$ */
/*
* Copyright (c) 2004 Nicholas Marriott <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "logfmon.h"
void free_context(struct context *);
struct context *
add_context(struct file *file, char *key, struct rule *rule, char *entry,
regmatch_t match[10])
{
struct context *context;
context = xmalloc(sizeof (struct context));
TAILQ_INIT(&context->msgs);
context->rule = rule;
context->expiry = time(NULL) + rule->params.exp_time;
context->key = xstrdup(key);
context->line = xstrdup(entry);
memcpy(&context->match, match, sizeof context->match);
log_debug("added context: key=%s", key);
TAILQ_INSERT_TAIL(&file->contexts, context, entry);
return (context);
}
void
free_context(struct context *context)
{
reset_context(context);
xfree(context->line);
xfree(context->key);
xfree(context);
}
void
free_contexts(struct file *file)
{
struct context *context;
while (!TAILQ_EMPTY(&file->contexts)) {
context = TAILQ_FIRST(&file->contexts);
TAILQ_REMOVE(&file->contexts, context, entry);
free_context(context);
}
}
void
reset_context(struct context *context)
{
struct msg *msg;
while (!TAILQ_EMPTY(&context->msgs)) {
msg = TAILQ_FIRST(&context->msgs);
TAILQ_REMOVE(&context->msgs, msg, entry);
xfree(msg->str);
xfree(msg);
}
}
void
delete_context(struct file *file, struct context *context)
{
log_debug("removed context: key=%s", context->key);
TAILQ_REMOVE(&file->contexts, context, entry);
free_context(context);
}
struct context *
find_context_by_key(struct file *file, char *key)
{
struct context *context;
TAILQ_FOREACH(context, &file->contexts, entry) {
if (strcmp(context->key, key) == 0)
return (context);
}
return (NULL);
}
unsigned int
count_msgs(struct context *context)
{
struct msg *msg;
unsigned int n;
n = 0;
TAILQ_FOREACH(msg, &context->msgs, entry)
n++;
return (n);
}
void
expire_contexts(struct file *file)
{
struct context *context;
TAILQ_HEAD(, context) exp_contexts;
time_t now;
now = time(NULL);
TAILQ_INIT(&exp_contexts);
TAILQ_FOREACH(context, &file->contexts, entry) {
if (now >= context->expiry) {
log_debug("expired context: key=%s", context->key);
act_context(context, context->rule->params.exp_act,
context->rule->params.exp_str, 1);
TAILQ_INSERT_HEAD(&exp_contexts, context, exp_entry);
}
}
while (!TAILQ_EMPTY(&exp_contexts)) {
context = TAILQ_FIRST(&exp_contexts);
TAILQ_REMOVE(&exp_contexts, context, exp_entry);
delete_context(file, context);
}
}
void
act_context(struct context *context, enum action act, char *str, int repl)
{
if (repl)
str = repl_matches(context->line, str, context->match);
log_debug("acting on context: action=%s, target=%s",
actions[act], str);
switch (act) {
case ACT_IGNORE:
break;
case ACT_EXEC:
exec_context(str);
break;
case ACT_PIPE:
pipe_context(context, str);
break;
case ACT_WRITE:
write_context(context, str, 0);
break;
case ACT_WRITEAPPEND:
write_context(context, str, 1);
break;
case ACT_OPEN:
case ACT_APPEND:
case ACT_CLOSE:
log_warnx("action invalid here: %s", actions[act]);
break;
default:
log_warnx("unknown action: %d\n", act);
break;
}
if (repl && str != NULL)
xfree(str);
}
void
pipe_context(struct context *context, char *cmd)
{
FILE *fd;
struct msg *msg;
pthread_t thread;
if (cmd == NULL || *cmd == '\0') {
log_warnx("empty pipe command");
return;
}
fd = popen(cmd, "w");
if (fd == NULL) {
log_warn("%s", cmd);
return;
}
TAILQ_FOREACH(msg, &context->msgs, entry) {
if (fwrite(msg->str, strlen(msg->str), 1, fd) != 1) {
log_warn("fwrite");
break;
}
if (fputc('\n', fd) == EOF) {
log_warn("fputc");
break;
}
}
CREATE_THREAD(&thread, pclose_thread, fd);
}
void
exec_context(char *cmd)
{
pthread_t thread;
if (cmd == NULL || *cmd == '\0') {
log_warnx("empty exec command");
return;
}
CREATE_THREAD(&thread, exec_thread, xstrdup(cmd));
}
void
write_context(struct context *context, char *path, int append)
{
FILE *fd;
struct msg *msg;
if (path == NULL || *path == '\0') {
log_warnx("empty write path");
return;
}
fd = fopen(path, append ? "a" : "w");
if (fd == NULL) {
log_warn("%s", path);
return;
}
TAILQ_FOREACH(msg, &context->msgs, entry) {
if (fwrite(msg->str, strlen(msg->str), 1, fd) != 1) {
log_warn("fwrite");
break;
}
if (fputc('\n', fd) == EOF) {
log_warn("fputc");
break;
}
}
fclose(fd);
}