File: journald-subsystem.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 (201 lines) | stat: -rw-r--r-- 5,760 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
/*
 * Copyright (c) 2010-2014 Balabit
 * Copyright (c) 2010-2014 Viktor Juhasz <viktor.juhasz@balabit.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; 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 "journald-subsystem.h"
#include "messages.h"

#include <glib.h>
#include <gmodule.h>

#if SYSLOG_NG_SYSTEMD_JOURNAL_MODE == SYSLOG_NG_JOURNALD_SYSTEM



#else

int (*sd_journal_open)(sd_journal **ret, int flags);
#if SYSLOG_NG_HAVE_JOURNAL_NAMESPACES
int (*sd_journal_open_namespace)(sd_journal **ret, const char *namespace, int flags);
#endif
void (*sd_journal_close)(sd_journal *j);
int (*sd_journal_seek_head)(sd_journal *j);
int (*sd_journal_seek_tail)(sd_journal *j);
int (*sd_journal_get_cursor)(sd_journal *j, char **cursor);
int (*sd_journal_next)(sd_journal *j);
void (*sd_journal_restart_data)(sd_journal *j);
int (*sd_journal_enumerate_data)(sd_journal *j, const void **data, size_t *length);
int (*sd_journal_seek_cursor)(sd_journal *j, const char *cursor);
int (*sd_journal_test_cursor)(sd_journal *j, const char *cursor);
int (*sd_journal_get_fd)(sd_journal *j);
int (*sd_journal_process)(sd_journal *j);
int (*sd_journal_get_realtime_usec)(sd_journal *j, uint64_t *usec);
int (*sd_journal_get_realtime_usec)(sd_journal *j, uint64_t *usec);
int (*sd_journal_add_match)(sd_journal *j, const void *data, size_t size);
char *(*sd_id128_to_string)(sd_id128_t id, char s[SD_ID128_STRING_MAX]);
int (*sd_id128_get_boot)(sd_id128_t *ret);


#define LOAD_SYMBOL(library, symbol) _load_module_symbols(library, #symbol, (gpointer *)&symbol)

#define JOURNAL_LIBRARY_NAME "libsystemd-journal.so.0"
#define SYSTEMD_LIBRARY_NAME "libsystemd.so.0"
#define ID128_LIBRARY_NAME "libsystemd-id128.so.0"

const char *journald_libraries[] = {JOURNAL_LIBRARY_NAME, SYSTEMD_LIBRARY_NAME, NULL};
const char *journald_ext_libraries[] = {ID128_LIBRARY_NAME, JOURNAL_LIBRARY_NAME, SYSTEMD_LIBRARY_NAME, NULL};

static GModule *journald_module;
static GModule *journald_ext_module;

typedef struct sd_journal sd_journal;

static GModule *
_journald_module_open(const char *const *libraries)
{
  GModule *module = NULL;

  for (gint i = 0; (libraries[i] != NULL) && !module; i++)
    {
      module = g_module_open(libraries[i], 0);
      if (!module)
        msg_debug("No module by name", evt_tag_str("module", libraries[i]));
    }

  return module;
}

static gboolean
_load_module_symbols(GModule *library, gchar *symbol_name, gpointer *symbol)
{
  if (!g_module_symbol(library, symbol_name, symbol))
    {
      msg_error("Could not find symbol", evt_tag_str("symbol", symbol_name));
      return FALSE;
    }
  return TRUE;
}

static gboolean
_load_journald_symbols(void)
{
  if (!LOAD_SYMBOL(journald_module, sd_journal_open))
    return FALSE;

#if SYSLOG_NG_HAVE_JOURNAL_NAMESPACES
  if (!LOAD_SYMBOL(journald_module, sd_journal_open_namespace))
    return FALSE;
#endif

  if (!LOAD_SYMBOL(journald_module, sd_journal_close))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_seek_head))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_seek_tail))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_get_cursor))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_next))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_restart_data))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_enumerate_data))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_seek_cursor))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_get_fd))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_process))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_get_realtime_usec))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_test_cursor))
    return FALSE;

  if (!LOAD_SYMBOL(journald_module, sd_journal_add_match))
    return FALSE;

  if (!LOAD_SYMBOL(journald_ext_module, sd_id128_to_string))
    return FALSE;

  if (!LOAD_SYMBOL(journald_ext_module, sd_id128_get_boot))
    return FALSE;

  return TRUE;
}

void
unload_journald_subsystem(void)
{
  if (journald_module)
    {
      g_module_close(journald_module);
      journald_module = NULL;
    }

  if (journald_ext_module)
    {
      g_module_close(journald_ext_module);
      journald_module = NULL;
    }
}

int
load_journald_subsystem(void)
{
  if (!journald_module && !journald_ext_module)
    {
      journald_module = _journald_module_open(journald_libraries);
      if (!journald_module)
        {
          msg_error("Could not find journald_module");
          return FALSE;
        }

      journald_ext_module = _journald_module_open(journald_ext_libraries);
      if (!journald_ext_module)
        {
          msg_error("Could not find journald_ext_module");
          return FALSE;
        }

      if (!_load_journald_symbols())
        {
          unload_journald_subsystem();
          return FALSE;
        }
    }
  return TRUE;
}

#endif