~ubuntu-branches/ubuntu/hardy/syslog-ng/hardy

« back to all changes in this revision

Viewing changes to src/afinter.c

  • Committer: Bazaar Package Importer
  • Author(s): SZALAY Attila
  • Date: 2006-05-25 11:21:50 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060525112150-b18srkxcrz980xi9
Tags: 1.9.11-1
* New upstream version
  - Fixed log facility and priority detecting. (Closes: #350120, #350344, #357071, #367256)
* Added bison to Build-Dependency. (Closes: #368765)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *
3
 
 * Copyright (c) 1999 Bal�zs Scheidler
4
 
 * Copyright (c) 1999-2001 BalaBit IT Ltd.
5
 
 * 
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
 
1
/*
 
2
 * Copyright (c) 2002, 2003, 2004 BalaBit IT Ltd, Budapest, Hungary
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 2 as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * Note that this permission is granted for only version 2 of the GPL.
 
9
 *
 
10
 * As an additional exemption you are allowed to compile & link against the
 
11
 * OpenSSL libraries as published by the OpenSSL project. See the file
 
12
 * COPYING for details.
10
13
 *
11
14
 * This program is distributed in the hope that it will be useful,
12
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
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.
19
 
 *
20
 
 * Inspired by nsyslog, originally written by Darren Reed.
21
 
 *
22
 
 * $Id: afinter.c,v 1.14 2003/01/31 14:26:48 bazsi Exp $
23
 
 *
24
 
 * Log source for internal messages of syslog-ng
25
 
 *
26
 
 ***************************************************************************/
 
22
 */
27
23
 
28
24
#include "afinter.h"
29
 
#include "werror.h"
30
 
#include "syslog-names.h"
31
 
#include "cfgfile.h"
32
 
#include "log.h"
33
 
 
34
 
#include <assert.h>
35
 
 
36
 
struct syslog_config *assigned_configuration = NULL;
37
 
 
38
 
static int write_internal_message(int level, UINT32 length, UINT8 *data)
39
 
{
40
 
        struct log_info *msg;
41
 
        int pri;
42
 
 
43
 
        if (assigned_configuration && assigned_configuration->internal) {
44
 
                switch (level) {
45
 
                case MSG_ERROR:
46
 
                        pri = LOG_SYSLOG | LOG_ERR;
47
 
                        break;
48
 
                case MSG_FATAL:
49
 
                        pri = LOG_SYSLOG | LOG_CRIT;
50
 
                        break;
51
 
                case MSG_DEBUG:
52
 
                        pri = LOG_SYSLOG | LOG_DEBUG;
53
 
                        break;
54
 
                case MSG_VERBOSE:
55
 
                case MSG_NOTICE:
56
 
                default:
57
 
                        pri = LOG_SYSLOG | LOG_NOTICE;
58
 
                }
59
 
                while (data[length - 1] == '\n') 
60
 
                        length--;
61
 
                msg = make_internal_message(pri, length, data);
62
 
                msg->flags |= LF_LOCAL;
63
 
                HANDLE_LOG(assigned_configuration->internal, msg);
64
 
                return 0;
65
 
        }
66
 
        return 0;
67
 
}
68
 
 
69
 
void set_error_internal(void)
70
 
{
71
 
        error_write = write_internal_message;
72
 
}
73
 
 
74
 
void set_internal_cfg(struct syslog_config *cfg)
75
 
{
76
 
        assigned_configuration = cfg;
77
 
}
78
 
 
79
 
static int do_init_afinter(struct log_handler *c, 
80
 
                           struct syslog_config *cfg,
81
 
                           struct persistent_config *persistent)
82
 
{
83
 
        CAST(log_source_driver, self, c);
84
 
        cfg->internal = self->super.next;
85
 
        if (assigned_configuration == NULL) {
86
 
                assigned_configuration = cfg;
87
 
        }
88
 
        return ST_OK | ST_GOON;
89
 
}
90
 
 
91
 
static void 
92
 
do_destroy_afinter(struct log_handler *c, 
93
 
                   struct syslog_config *cfg, 
94
 
                   struct persistent_config *persistent)
95
 
{
96
 
        cfg->internal = NULL;
97
 
}
98
 
 
99
 
struct log_source_driver *make_afinter_source(void)
100
 
{
101
 
        NEW(log_source_driver, self);
102
 
 
103
 
        self->super.super.init = do_init_afinter;
104
 
        self->super.super.destroy = do_destroy_afinter;
105
 
        return self;
106
 
}
 
25
#include "logreader.h"
 
26
 
 
27
#include "messages.h"
 
28
 
 
29
 
 
30
extern GQueue *internal_msg_queue;
 
31
static gint next_mark_target = -1;
 
32
 
 
33
void 
 
34
afinter_postpone_mark(gint mark_freq)
 
35
{
 
36
  if (mark_freq > 0)
 
37
    {
 
38
      GTimeVal tv;
 
39
      
 
40
      g_get_current_time(&tv);
 
41
      next_mark_target = tv.tv_sec + mark_freq;
 
42
    }
 
43
}
 
44
 
 
45
typedef struct _AFInterWatch
 
46
{
 
47
  GSource super;
 
48
  gint mark_freq;
 
49
} AFInterWatch;
 
50
 
 
51
static gboolean
 
52
afinter_source_prepare(GSource *source, gint *timeout)
 
53
{
 
54
  AFInterWatch *self = (AFInterWatch *) source;
 
55
  GTimeVal tv;
 
56
  
 
57
  *timeout = -1;
 
58
 
 
59
  if (self->mark_freq > 0 && next_mark_target == -1)
 
60
    {
 
61
      g_source_get_current_time(source, &tv);
 
62
      next_mark_target = tv.tv_sec + self->mark_freq;
 
63
    }
 
64
    
 
65
  if (next_mark_target != -1)
 
66
    {
 
67
      g_source_get_current_time(source, &tv);
 
68
      *timeout = MAX((next_mark_target - tv.tv_sec) * 1000, 0);
 
69
    }
 
70
  else
 
71
    {
 
72
      *timeout = -1;
 
73
    }
 
74
  return !g_queue_is_empty(internal_msg_queue);
 
75
}
 
76
 
 
77
static gboolean
 
78
afinter_source_check(GSource *source)
 
79
{
 
80
  GTimeVal tv;
 
81
 
 
82
  g_source_get_current_time(source, &tv);
 
83
  
 
84
  if (next_mark_target != -1 && next_mark_target <= tv.tv_sec)
 
85
    return TRUE;
 
86
  return !g_queue_is_empty(internal_msg_queue);
 
87
}
 
88
 
 
89
static gboolean
 
90
afinter_source_dispatch(GSource *source,
 
91
                        GSourceFunc callback,
 
92
                        gpointer user_data)
 
93
{
 
94
  LogMessage *msg;
 
95
  gint path_flags = 0;
 
96
  GTimeVal tv;
 
97
  
 
98
  g_source_get_current_time(source, &tv);
 
99
  
 
100
  if (next_mark_target != -1 && next_mark_target <= tv.tv_sec)
 
101
    {
 
102
      msg = log_msg_new_mark();
 
103
      path_flags = PF_FLOW_CTL_OFF;
 
104
    }
 
105
  else
 
106
    {
 
107
      msg = g_queue_pop_head(internal_msg_queue);
 
108
    }
 
109
 
 
110
 
 
111
  if (msg)
 
112
    ((void (*)(LogPipe *, LogMessage *, gint))callback) ((LogPipe *) user_data, msg, path_flags);
 
113
  return TRUE;
 
114
}
 
115
 
 
116
static void
 
117
afinter_source_finalize(GSource *source)
 
118
{
 
119
}
 
120
 
 
121
GSourceFuncs afinter_source_watch_funcs =
 
122
{
 
123
  afinter_source_prepare,
 
124
  afinter_source_check,
 
125
  afinter_source_dispatch,
 
126
  afinter_source_finalize
 
127
};
 
128
 
 
129
static void
 
130
afinter_source_dispatch_msg(LogPipe *pipe, LogMessage *msg, gint path_flags)
 
131
{
 
132
  log_pipe_queue(pipe, msg, path_flags);
 
133
}
 
134
 
 
135
static inline GSource *
 
136
afinter_source_watch_new(LogPipe *pipe, gint mark_freq)
 
137
{
 
138
  AFInterWatch *self = (AFInterWatch *) g_source_new(&afinter_source_watch_funcs, sizeof(AFInterWatch));
 
139
  
 
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);
 
142
  return &self->super;
 
143
}
 
144
 
 
145
typedef struct _AFInterSource
 
146
{
 
147
  LogSource super;
 
148
  GSource *watch;
 
149
} AFInterSource;
 
150
 
 
151
static gboolean
 
152
afinter_source_init(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
 
153
{
 
154
  AFInterSource *self = (AFInterSource *) s;
 
155
  
 
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);
 
160
  return TRUE;
 
161
}
 
162
 
 
163
static gboolean
 
164
afinter_source_deinit(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
 
165
{
 
166
  AFInterSource *self = (AFInterSource *) s;
 
167
  
 
168
  if (self->watch)
 
169
    {
 
170
      g_source_destroy(self->watch);
 
171
      g_source_unref(self->watch);
 
172
      self->watch = NULL;
 
173
    }
 
174
  return TRUE;
 
175
}
 
176
 
 
177
static LogSource *
 
178
afinter_source_new(LogSourceOptions *options)
 
179
{
 
180
  AFInterSource *self = g_new0(AFInterSource, 1);
 
181
  
 
182
  log_source_init_instance(&self->super, options);
 
183
  self->super.super.init = afinter_source_init;
 
184
  self->super.super.deinit = afinter_source_deinit;
 
185
  return &self->super;
 
186
}
 
187
 
 
188
typedef struct _AFInterSourceDriver
 
189
{
 
190
  LogDriver super;
 
191
  LogSource *source;
 
192
  LogSourceOptions source_options;
 
193
} AFInterSourceDriver;
 
194
 
 
195
static gboolean
 
196
afinter_sd_init(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
 
197
{
 
198
  AFInterSourceDriver *self = (AFInterSourceDriver *) s;
 
199
 
 
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);
 
204
  return TRUE;
 
205
}
 
206
 
 
207
static gboolean
 
208
afinter_sd_deinit(LogPipe *s, GlobalConfig *cfg, PersistentConfig *persist)
 
209
{
 
210
  AFInterSourceDriver *self = (AFInterSourceDriver *) s;
 
211
  
 
212
  if (self->source)
 
213
    {
 
214
      log_pipe_deinit(&self->source->super, cfg, NULL);
 
215
      /* break circular reference created during _init */
 
216
      log_pipe_unref(&self->source->super);
 
217
      self->source = NULL;
 
218
    }
 
219
  return TRUE;
 
220
}
 
221
 
 
222
static void
 
223
afinter_sd_free(LogPipe *s)
 
224
{
 
225
  AFInterSourceDriver *self = (AFInterSourceDriver *) s;
 
226
  
 
227
  g_assert(!self->source);
 
228
  log_drv_free_instance(&self->super);
 
229
  g_free(self);
 
230
}
 
231
 
 
232
LogDriver *
 
233
afinter_sd_new(void)
 
234
{
 
235
  AFInterSourceDriver *self = g_new0(AFInterSourceDriver, 1);
 
236
 
 
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);
 
242
  return &self->super;
 
243
}
 
244