File: rcptid.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 (185 lines) | stat: -rw-r--r-- 4,524 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2002-2013 Balabit
 * Copyright (c) 2013 Viktor Juhasz
 * Copyright (c) 2013 Viktor Tusa
 *
 * 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 "rcptid.h"
#include "messages.h"
#include "str-format.h"

static struct _RcptidService
{
  PersistState *persist_state;
  PersistEntryHandle persist_handle;
  GMutex lock;
} rcptid_service;

/* NOTE: RcptIdInstance is a singleton, so we don't pass self around as an argument */

#define self (&rcpt_instance)

static RcptidState *
rcptid_map_state(void)
{
  return (RcptidState *) persist_state_map_entry(rcptid_service.persist_state, rcptid_service.persist_handle);
}

static void
rcptid_unmap_state(void)
{
  persist_state_unmap_entry(rcptid_service.persist_state, rcptid_service.persist_handle);
}

static gboolean
rcptid_is_initialized(void)
{
  return rcptid_service.persist_state != NULL;
}

static gboolean
rcptid_restore_entry(void)
{
  RcptidState *data = rcptid_map_state();

  if (data->header.version > 0)
    {
      msg_error("Internal error restoring log reader state, stored data is too new",
                evt_tag_int("version", data->header.version));
      rcptid_unmap_state();
      return FALSE;
    }

  if ((data->header.big_endian && G_BYTE_ORDER == G_LITTLE_ENDIAN) ||
      (!data->header.big_endian && G_BYTE_ORDER == G_BIG_ENDIAN))
    {
      data->header.big_endian = !data->header.big_endian;
      data->g_rcptid = GUINT64_SWAP_LE_BE(data->g_rcptid);
    }

  rcptid_unmap_state();
  return TRUE;
}

static gboolean
rcptid_create_new_entry(void)
{
  RcptidState *data;

  rcptid_service.persist_handle = persist_state_alloc_entry(rcptid_service.persist_state, "next.rcptid",
                                                            sizeof(RcptidState));
  if (!rcptid_service.persist_handle)
    {
      msg_error("Error allocating RCPTID structure in persist-state");
      return FALSE;
    }

  data = rcptid_map_state();
  data->header.version = 0;
  data->header.big_endian = (G_BYTE_ORDER == G_BIG_ENDIAN);
  data->g_rcptid = 1;
  rcptid_unmap_state();
  return TRUE;
}

void
rcptid_append_formatted_id(GString *result, guint64 rcptid)
{
  if (rcptid)
    {
      format_uint64_padded(result, 0, 0, 10, rcptid);
    }
}

void
rcptid_set_id(guint64 id)
{
  RcptidState *data;

  if (!rcptid_is_initialized())
    return;

  g_mutex_lock(&rcptid_service.lock);

  data = rcptid_map_state();
  data->g_rcptid = id;

  rcptid_unmap_state();

  g_mutex_unlock(&rcptid_service.lock);
}

guint64
rcptid_generate_id(void)
{
  RcptidState *data;
  guint64 new_id;

  if (!rcptid_is_initialized())
    return 0;

  g_mutex_lock(&rcptid_service.lock);

  data = rcptid_map_state();

  new_id = data->g_rcptid++;
  if (data->g_rcptid == 0)
    data->g_rcptid = 1;

  rcptid_unmap_state();

  g_mutex_unlock(&rcptid_service.lock);

  return new_id;
}

/*restore RCTPID from persist file, if possible, else
  create new entry point with "next.rcptid" name*/
gboolean
rcptid_init(PersistState *state, gboolean use_rcptid)
{
  gsize size;
  guint8 version;

  g_assert(rcptid_service.persist_state == NULL);
  if (!use_rcptid)
    return TRUE;

  rcptid_service.persist_state = state;
  rcptid_service.persist_handle = persist_state_lookup_entry(state, "next.rcptid", &size, &version);

  if (rcptid_service.persist_handle && size == sizeof(RcptidState))
    {
      return rcptid_restore_entry();
    }
  else
    {
      if (rcptid_service.persist_handle)
        msg_warning("rcpt-id: persist state: invalid size, allocating a new one");
      return rcptid_create_new_entry();
    }
}

void
rcptid_deinit(void)
{
  rcptid_service.persist_state = NULL;
}