16
19
* You should have received a copy of the GNU General Public License
17
20
* along with this program; if not, write to the Free Software
18
21
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
* Inspired by nsyslog, originally written by Darren Reed.
22
* $Id: afinter.c,v 1.14 2003/01/31 14:26:48 bazsi Exp $
24
* Log source for internal messages of syslog-ng
26
***************************************************************************/
28
24
#include "afinter.h"
30
#include "syslog-names.h"
36
struct syslog_config *assigned_configuration = NULL;
38
static int write_internal_message(int level, UINT32 length, UINT8 *data)
43
if (assigned_configuration && assigned_configuration->internal) {
46
pri = LOG_SYSLOG | LOG_ERR;
49
pri = LOG_SYSLOG | LOG_CRIT;
52
pri = LOG_SYSLOG | LOG_DEBUG;
57
pri = LOG_SYSLOG | LOG_NOTICE;
59
while (data[length - 1] == '\n')
61
msg = make_internal_message(pri, length, data);
62
msg->flags |= LF_LOCAL;
63
HANDLE_LOG(assigned_configuration->internal, msg);
69
void set_error_internal(void)
71
error_write = write_internal_message;
74
void set_internal_cfg(struct syslog_config *cfg)
76
assigned_configuration = cfg;
79
static int do_init_afinter(struct log_handler *c,
80
struct syslog_config *cfg,
81
struct persistent_config *persistent)
83
CAST(log_source_driver, self, c);
84
cfg->internal = self->super.next;
85
if (assigned_configuration == NULL) {
86
assigned_configuration = cfg;
88
return ST_OK | ST_GOON;
92
do_destroy_afinter(struct log_handler *c,
93
struct syslog_config *cfg,
94
struct persistent_config *persistent)
99
struct log_source_driver *make_afinter_source(void)
101
NEW(log_source_driver, self);
103
self->super.super.init = do_init_afinter;
104
self->super.super.destroy = do_destroy_afinter;
25
#include "logreader.h"
30
extern GQueue *internal_msg_queue;
31
static gint next_mark_target = -1;
34
afinter_postpone_mark(gint mark_freq)
40
g_get_current_time(&tv);
41
next_mark_target = tv.tv_sec + mark_freq;
45
typedef struct _AFInterWatch
52
afinter_source_prepare(GSource *source, gint *timeout)
54
AFInterWatch *self = (AFInterWatch *) source;
59
if (self->mark_freq > 0 && next_mark_target == -1)
61
g_source_get_current_time(source, &tv);
62
next_mark_target = tv.tv_sec + self->mark_freq;
65
if (next_mark_target != -1)
67
g_source_get_current_time(source, &tv);
68
*timeout = MAX((next_mark_target - tv.tv_sec) * 1000, 0);
74
return !g_queue_is_empty(internal_msg_queue);
78
afinter_source_check(GSource *source)
82
g_source_get_current_time(source, &tv);
84
if (next_mark_target != -1 && next_mark_target <= tv.tv_sec)
86
return !g_queue_is_empty(internal_msg_queue);
90
afinter_source_dispatch(GSource *source,
98
g_source_get_current_time(source, &tv);
100
if (next_mark_target != -1 && next_mark_target <= tv.tv_sec)
102
msg = log_msg_new_mark();
103
path_flags = PF_FLOW_CTL_OFF;
107
msg = g_queue_pop_head(internal_msg_queue);
112
((void (*)(LogPipe *, LogMessage *, gint))callback) ((LogPipe *) user_data, msg, path_flags);
117
afinter_source_finalize(GSource *source)
121
GSourceFuncs afinter_source_watch_funcs =
123
afinter_source_prepare,
124
afinter_source_check,
125
afinter_source_dispatch,
126
afinter_source_finalize
130
afinter_source_dispatch_msg(LogPipe *pipe, LogMessage *msg, gint path_flags)
132
log_pipe_queue(pipe, msg, path_flags);
135
static inline GSource *
136
afinter_source_watch_new(LogPipe *pipe, gint mark_freq)
138
AFInterWatch *self = (AFInterWatch *) g_source_new(&afinter_source_watch_funcs, sizeof(AFInterWatch));
140
self->mark_freq = mark_freq;
141
g_source_set_callback(&self->super, (GSourceFunc) afinter_source_dispatch_msg, log_pipe_ref(pipe), (GDestroyNotify) log_pipe_unref);
145
typedef struct _AFInterSource
152
afinter_source_init(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
154
AFInterSource *self = (AFInterSource *) s;
156
/* the source added below references this logreader, it will be unref'd
157
when the source is destroyed */
158
self->watch = afinter_source_watch_new(&self->super.super, cfg->mark_freq);
159
g_source_attach(self->watch, NULL);
164
afinter_source_deinit(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
166
AFInterSource *self = (AFInterSource *) s;
170
g_source_destroy(self->watch);
171
g_source_unref(self->watch);
178
afinter_source_new(LogSourceOptions *options)
180
AFInterSource *self = g_new0(AFInterSource, 1);
182
log_source_init_instance(&self->super, options);
183
self->super.super.init = afinter_source_init;
184
self->super.super.deinit = afinter_source_deinit;
188
typedef struct _AFInterSourceDriver
192
LogSourceOptions source_options;
193
} AFInterSourceDriver;
196
afinter_sd_init(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
198
AFInterSourceDriver *self = (AFInterSourceDriver *) s;
200
log_source_options_init(&self->source_options, cfg);
201
self->source = afinter_source_new(&self->source_options);
202
log_pipe_append(&self->source->super, s);
203
log_pipe_init(&self->source->super, cfg, NULL);
208
afinter_sd_deinit(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
210
AFInterSourceDriver *self = (AFInterSourceDriver *) s;
214
log_pipe_deinit(&self->source->super, cfg, NULL);
215
/* break circular reference created during _init */
216
log_pipe_unref(&self->source->super);
223
afinter_sd_free(LogPipe *s)
225
AFInterSourceDriver *self = (AFInterSourceDriver *) s;
227
g_assert(!self->source);
228
log_drv_free_instance(&self->super);
235
AFInterSourceDriver *self = g_new0(AFInterSourceDriver, 1);
237
log_drv_init_instance(&self->super);
238
self->super.super.init = afinter_sd_init;
239
self->super.super.deinit = afinter_sd_deinit;
240
self->super.super.free_fn = afinter_sd_free;
241
log_source_options_defaults(&self->source_options);