File: filterx-scope.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (408 lines) | stat: -rw-r--r-- 11,595 bytes parent folder | download | duplicates (2)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "filterx/filterx-scope.h"
#include "scratch-buffers.h"

#define FILTERX_HANDLE_FLOATING_BIT (1UL << 31)
#define FILTERX_SCOPE_MAX_GENERATION ((1UL << 20) - 1)

struct _FilterXVariable
{
  /* the MSB indicates that the variable is a floating one */
  FilterXVariableHandle handle;
  /*
   * assigned -- Indicates that the variable was assigned to a new value
   *
   * declared -- this variable is declared (e.g. retained for the entire input pipeline)
   */
  guint32 assigned:1,
          declared:1,
          generation:20;
  FilterXObject *value;
};

gboolean
filterx_variable_handle_is_floating(FilterXVariableHandle handle)
{
  return !!(handle & FILTERX_HANDLE_FLOATING_BIT);
}

gboolean
filterx_variable_is_floating(FilterXVariable *v)
{
  return filterx_variable_handle_is_floating(v->handle);
}

static NVHandle
filterx_variable_get_nv_handle(FilterXVariable *v)
{
  return v->handle & ~FILTERX_HANDLE_FLOATING_BIT;
}

FilterXObject *
filterx_variable_get_value(FilterXVariable *v)
{
  return filterx_object_ref(v->value);
}

void
filterx_variable_set_value(FilterXVariable *v, FilterXObject *new_value)
{
  filterx_object_unref(v->value);
  v->value = filterx_object_ref(new_value);
  v->assigned = TRUE;
}

void
filterx_variable_unset_value(FilterXVariable *v)
{
  filterx_variable_set_value(v, NULL);
}

gboolean
filterx_variable_is_set(FilterXVariable *v)
{
  return v->value != NULL;
}


static void
_variable_free(FilterXVariable *v)
{
  filterx_object_unref(v->value);
}

struct _FilterXScope
{
  GAtomicCounter ref_cnt;
  GArray *variables;
  guint32 generation:20, write_protected, dirty, syncable;
};

static gboolean
_lookup_variable(FilterXScope *self, FilterXVariableHandle handle, FilterXVariable **v_slot)
{
  gint l, h, m;

  /* open-coded binary search */
  l = 0;
  h = self->variables->len - 1;
  while (l <= h)
    {
      m = (l + h) >> 1;

      FilterXVariable *m_elem = &g_array_index(self->variables, FilterXVariable, m);

      FilterXVariableHandle mv = m_elem->handle;
      if (mv == handle)
        {
          *v_slot = m_elem;
          return TRUE;
        }
      else if (mv > handle)
        {
          h = m - 1;
        }
      else
        {
          l = m + 1;
        }
    }
  *v_slot = &g_array_index(self->variables, FilterXVariable, l);
  return FALSE;
}

void
filterx_scope_set_dirty(FilterXScope *self)
{
  self->dirty = TRUE;
}

gboolean
filterx_scope_is_dirty(FilterXScope *self)
{
  return self->dirty;
}

FilterXVariableHandle
filterx_scope_map_variable_to_handle(const gchar *name, FilterXVariableType type)
{
  NVHandle nv_handle = log_msg_get_value_handle(name);

  if (type == FX_VAR_MESSAGE)
    return (FilterXVariableHandle) nv_handle;
  return (FilterXVariableHandle) nv_handle | FILTERX_HANDLE_FLOATING_BIT;
}

FilterXVariable *
filterx_scope_lookup_variable(FilterXScope *self, FilterXVariableHandle handle)
{
  FilterXVariable *v;

  if (_lookup_variable(self, handle, &v))
    {
      if (filterx_variable_handle_is_floating(handle) &&
          !v->declared && v->generation != self->generation)
        return NULL;
      return v;
    }
  return NULL;
}

static FilterXVariable *
_register_variable(FilterXScope *self,
                   FilterXVariableHandle handle,
                   FilterXObject *initial_value)
{
  FilterXVariable v, *v_slot;

  if (_lookup_variable(self, handle, &v_slot))
    {
      /* already present */
      if (v_slot->generation != self->generation)
        {
          /* existing value is from a previous generation, override it as if
           * it was a new value */

          v_slot->generation = self->generation;
          filterx_variable_set_value(v_slot, initial_value);
          /* consider this to be unset just as an initial registration is */
          v_slot->assigned = FALSE;
        }
      return v_slot;
    }
  /* turn v_slot into an index */
  gsize v_index = ((guint8 *) v_slot - (guint8 *) self->variables->data) / sizeof(FilterXVariable);
  g_assert(v_index <= self->variables->len);
  g_assert(&g_array_index(self->variables, FilterXVariable, v_index) == v_slot);

  v.handle = handle;
  v.assigned = FALSE;
  v.value = filterx_object_ref(initial_value);
  v.generation = self->generation;
  g_array_insert_val(self->variables, v_index, v);

  return &g_array_index(self->variables, FilterXVariable, v_index);
}

FilterXVariable *
filterx_scope_register_variable(FilterXScope *self,
                                FilterXVariableHandle handle,
                                FilterXObject *initial_value)
{
  FilterXVariable *v = _register_variable(self, handle, initial_value);
  v->declared = FALSE;

  /* the scope needs to be synced with the message if it holds a
   * message-tied variable (e.g.  $MSG) */
  if (!filterx_variable_handle_is_floating(handle))
    self->syncable = TRUE;
  return v;
}

FilterXVariable *
filterx_scope_register_declared_variable(FilterXScope *self,
                                         FilterXVariableHandle handle,
                                         FilterXObject *initial_value)

{
  g_assert(filterx_variable_handle_is_floating(handle));

  FilterXVariable *v = _register_variable(self, handle, initial_value);
  v->declared = TRUE;
  return v;
}

/*
 * 1) sync objects to message
 * 2) drop undeclared objects
 */
void
filterx_scope_sync(FilterXScope *self, LogMessage *msg)
{

  if (!self->dirty)
    {
      msg_trace("Filterx sync: not syncing as scope is not dirty",
                evt_tag_printf("scope", "%p", self));
      return;
    }
  if (!self->syncable)
    {
      msg_trace("Filterx sync: not syncing as there are no variables to sync",
                evt_tag_printf("scope", "%p", self));
      self->dirty = FALSE;
      return;
    }

  GString *buffer = scratch_buffers_alloc();

  for (gint i = 0; i < self->variables->len; i++)
    {
      FilterXVariable *v = &g_array_index(self->variables, FilterXVariable, i);

      /* we don't need to sync the value if:
       *
       *  1) this is a floating variable; OR
       *
       *  2) the value was extracted from the message but was not changed in
       *     place (for mutable objects), and was not assigned to.
       *
       */
      if (filterx_variable_is_floating(v))
        {
          /* we could unset undeclared, floating values here as we are
           * transitioning to the next filterx block.  But this is also
           * addressed by the variable generation counter mechanism.  This
           * means that we will unset those if some expr actually refers to
           * it.  Also, we skip the entire sync exercise, unless we have
           * message tied values, so we need to work even if floating
           * variables are not synced.
           *
           * With that said, let's not clear these.
           */
        }
      else if (v->value == NULL)
        {
          msg_trace("Filterx sync: whiteout variable, unsetting in message",
                    evt_tag_str("variable", log_msg_get_value_name(filterx_variable_get_nv_handle(v), NULL)));
          /* we need to unset */
          log_msg_unset_value(msg, v->handle);
          v->assigned = FALSE;
        }
      else if (v->assigned || v->value->modified_in_place)
        {
          LogMessageValueType t;

          msg_trace("Filterx sync: changed variable in scope, overwriting in message",
                    evt_tag_str("variable", log_msg_get_value_name(filterx_variable_get_nv_handle(v), NULL)));

          g_string_truncate(buffer, 0);
          if (!filterx_object_marshal(v->value, buffer, &t))
            g_assert_not_reached();
          log_msg_set_value_with_type(msg, v->handle, buffer->str, buffer->len, t);
          v->value->modified_in_place = FALSE;
          v->assigned = FALSE;
        }
      else
        {
          msg_trace("Filterx sync: variable in scope and message in sync, not doing anything",
                    evt_tag_str("variable", log_msg_get_value_name(filterx_variable_get_nv_handle(v), NULL)));
        }
    }
  self->dirty = FALSE;
}

FilterXScope *
filterx_scope_new(void)
{
  FilterXScope *self = g_new0(FilterXScope, 1);

  g_atomic_counter_set(&self->ref_cnt, 1);
  self->variables = g_array_sized_new(FALSE, TRUE, sizeof(FilterXVariable), 16);
  g_array_set_clear_func(self->variables, (GDestroyNotify) _variable_free);
  return self;
}

static FilterXScope *
filterx_scope_clone(FilterXScope *other)
{
  FilterXScope *self = filterx_scope_new();

  for (gint src_index = 0, dst_index = 0; src_index < other->variables->len; src_index++)
    {
      FilterXVariable *v = &g_array_index(other->variables, FilterXVariable, src_index);

      if (v->declared || !filterx_variable_is_floating(v))
        {
          g_array_append_val(self->variables, *v);
          FilterXVariable *v_clone = &g_array_index(self->variables, FilterXVariable, dst_index);

          v_clone->generation = 0;
          if (v->value)
            v_clone->value = filterx_object_clone(v->value);
          else
            v_clone->value = NULL;
          dst_index++;
          msg_trace("Filterx scope, cloning scope variable",
                    evt_tag_str("variable", log_msg_get_value_name((v->handle & ~FILTERX_HANDLE_FLOATING_BIT), NULL)));
        }
    }

  if (other->variables->len > 0)
    self->dirty = other->dirty;
  self->syncable = other->syncable;
  msg_trace("Filterx clone finished",
            evt_tag_printf("scope", "%p", self),
            evt_tag_printf("other", "%p", other),
            evt_tag_int("dirty", self->dirty),
            evt_tag_int("syncable", self->syncable),
            evt_tag_int("write_protected", self->write_protected));
  /* NOTE: we don't clone weak references, those only relate to mutable
   * objects, which we are cloning anyway */
  return self;
}

void
filterx_scope_write_protect(FilterXScope *self)
{
  self->write_protected = TRUE;
}

FilterXScope *
filterx_scope_make_writable(FilterXScope **pself)
{
  if ((*pself)->write_protected)
    {
      FilterXScope *new;

      new = filterx_scope_clone(*pself);
      filterx_scope_unref(*pself);
      *pself = new;
    }
  (*pself)->generation++;
  g_assert((*pself)->generation < FILTERX_SCOPE_MAX_GENERATION);
  return *pself;
}

static void
_free(FilterXScope *self)
{
  g_array_free(self->variables, TRUE);
  g_free(self);
}

FilterXScope *
filterx_scope_ref(FilterXScope *self)
{
  if (self)
    g_atomic_counter_inc(&self->ref_cnt);
  return self;
}

void
filterx_scope_unref(FilterXScope *self)
{
  if (self && (g_atomic_counter_dec_and_test(&self->ref_cnt)))
    _free(self);
}