File: apphook.c

package info (click to toggle)
syslog-ng 4.8.1-5%2Bdeb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,452 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,011; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (361 lines) | stat: -rw-r--r-- 8,795 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
/*
 * Copyright (c) 2002-2012 Balabit
 * Copyright (c) 1998-2012 Balázs Scheidler
 *
 * 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 "apphook.h"
#include "messages.h"
#include "children.h"
#include "dnscache.h"
#include "alarms.h"
#include "stats/stats-registry.h"
#include "metrics/metrics.h"
#include "healthcheck/healthcheck-stats.h"
#include "logmsg/logmsg.h"
#include "logsource.h"
#include "logwriter.h"
#include "afinter.h"
#include "template/globals.h"
#include "hostname.h"
#include "mainloop-call.h"
#include "service-management.h"
#include "crypto.h"
#include "value-pairs/value-pairs.h"
#include "scratch-buffers.h"
#include "mainloop.h"
#include "secret-storage/nondumpable-allocator.h"
#include "secret-storage/secret-storage.h"
#include "transport/transport-factory-id.h"
#include "timeutils/timeutils.h"
#include "msg-stats.h"
#include "timeutils/cache.h"
#include "multi-line/multi-line-factory.h"
#include "filterx/filterx-globals.h"

#include <iv.h>
#include <iv_work.h>

typedef struct _ApplicationHookEntry
{
  gint type;
  ApplicationHookRunMode run_mode;
  ApplicationHookFunc func;
  gpointer user_data;
} ApplicationHookEntry;

typedef struct _ApplicationThreadHookEntry
{
  ApplicationThreadHookFunc func;
  gpointer user_data;
} ApplicationThreadHookEntry;

static GList *application_hooks = NULL;
static GList *application_thread_init_hooks = NULL;
static GList *application_thread_deinit_hooks = NULL;
static gint current_state = AH_STARTUP;

gboolean
app_is_starting_up(void)
{
  return current_state < AH_RUNNING;
}

gboolean
app_is_shutting_down(void)
{
  return current_state >= AH_PRE_SHUTDOWN;
}

void
register_application_hook(gint type, ApplicationHookFunc func, gpointer user_data, ApplicationHookRunMode run_mode)
{
  if (type > __AH_STATE_MAX || current_state < type)
    {
      ApplicationHookEntry *entry = g_new0(ApplicationHookEntry, 1);

      entry->type = type;
      entry->func = func;
      entry->user_data = user_data;
      entry->run_mode = run_mode;

      application_hooks = g_list_prepend(application_hooks, entry);
    }
  else
    {
      /* the requested hook has already passed, call the requested function immediately */
      msg_debug("Application hook registered after the given point passed",
                evt_tag_int("current", current_state),
                evt_tag_int("hook", type));
      func(type, user_data);
    }
}

void
register_application_thread_init_hook(ApplicationThreadHookFunc func, gpointer user_data)
{
  ApplicationThreadHookEntry *entry = g_new0(ApplicationThreadHookEntry, 1);

  entry->func = func;
  entry->user_data = user_data;

  application_thread_init_hooks = g_list_prepend(application_thread_init_hooks, entry);
}

void
register_application_thread_deinit_hook(ApplicationThreadHookFunc func, gpointer user_data)
{
  ApplicationThreadHookEntry *entry = g_new0(ApplicationThreadHookEntry, 1);

  entry->func = func;
  entry->user_data = user_data;

  application_thread_deinit_hooks = g_list_prepend(application_thread_deinit_hooks, entry);
}

static void
run_application_thread_init_hooks(void)
{
  for (GList *elem = application_thread_init_hooks; elem; elem = elem->next)
    {
      ApplicationThreadHookEntry *hook = (ApplicationThreadHookEntry *) elem->data;
      hook->func(hook->user_data);
    }
}

static void
run_application_thread_deinit_hooks(void)
{
  for (GList *elem = application_thread_deinit_hooks; elem; elem = elem->next)
    {
      ApplicationThreadHookEntry *hook = (ApplicationThreadHookEntry *) elem->data;
      hook->func(hook->user_data);
    }
}

static void
_update_current_state(gint type)
{
  if (type > __AH_STATE_MAX)
    return;

  g_assert(current_state <= type);
  current_state = type;
}

static void
run_application_hook(gint type)
{
  GList *l, *l_next;

  _update_current_state(type);

  msg_debug("Running application hooks", evt_tag_int("hook", type));
  for (l = application_hooks; l; l = l_next)
    {
      ApplicationHookEntry *e = l->data;

      if (e->type == type)
        {
          l_next = l->next;
          e->func(type, e->user_data);

          if (e->run_mode == AHM_RUN_ONCE)
            {
              application_hooks = g_list_remove_link(application_hooks, l);
              g_free(e);
              g_list_free_1(l);
            }
          else
            {
              g_assert(e->run_mode == AHM_RUN_REPEAT);
            }
        }
      else
        {
          l_next = l->next;
        }
    }

}

static void
app_fatal(const char *msg)
{
  fprintf(stderr, "%s\n", msg);
}

#define construct_nondumpable_logger(logger) \
void \
nondumpable_allocator_ ## logger (gchar *summary, gchar *reason) \
{ \
  logger(summary, evt_tag_str("reason", reason)); \
}

construct_nondumpable_logger(msg_debug);
construct_nondumpable_logger(msg_fatal);

void
app_startup(void)
{
  msg_init(FALSE);
  iv_set_fatal_msg_handler(app_fatal);
  iv_init();
  crypto_init();
  hostname_global_init();
  dns_caching_global_init();
  dns_caching_thread_init();
  afinter_global_init();
  child_manager_init();
  alarm_init();
  main_loop_thread_resource_init();
  stats_init();
  metrics_global_init();
  healthcheck_stats_global_init();
  tzset();
  log_msg_global_init();
  log_source_global_init();
  log_template_global_init();
  value_pairs_global_init();
  service_management_init();
  scratch_buffers_allocator_init();
  nondumpable_setlogger(nondumpable_allocator_msg_debug, nondumpable_allocator_msg_fatal);
  secret_storage_init();
  transport_factory_id_global_init();
  scratch_buffers_global_init();
  msg_stats_init();
  timeutils_global_init();
  multi_line_global_init();
  filterx_global_init();
}

void
app_post_daemonized(void)
{
  run_application_hook(AH_POST_DAEMONIZED);
}

void
app_running(void)
{
  run_application_hook(AH_RUNNING);
}


void
app_pre_shutdown(void)
{
  run_application_hook(AH_PRE_SHUTDOWN);
}

void
app_shutdown(void)
{
  msg_stats_deinit();
  run_application_hook(AH_SHUTDOWN);

  filterx_global_deinit();
  multi_line_global_deinit();
  main_loop_thread_resource_deinit();
  secret_storage_deinit();
  scratch_buffers_allocator_deinit();
  scratch_buffers_global_deinit();
  value_pairs_global_deinit();
  log_template_global_deinit();
  log_msg_global_deinit();

  afinter_global_deinit();
  metrics_global_deinit();
  stats_destroy();
  child_manager_deinit();
  g_list_foreach(application_hooks, (GFunc) g_free, NULL);
  g_list_free(application_hooks);
  g_list_free_full(application_thread_init_hooks, g_free);
  g_list_free_full(application_thread_deinit_hooks, g_free);
  dns_caching_thread_deinit();
  dns_caching_global_deinit();
  hostname_global_deinit();
  crypto_deinit();
  msg_deinit();
  transport_factory_id_global_deinit();


  /* NOTE: the iv_deinit() call should come here, but there's some exit
   * synchronization issue in libivykis that causes use-after-free with the
   * thread-local-state for the main thread and iv_work_pool worker threads.
   * I've dropped a mail to Lennert about the issue, but I'm commenting this
   * out for now to avoid it biting someone. Bazsi, 2013/12/23.
   *
   *

    iv_deinit();

   */
}

void
app_config_pre_pre_init(void)
{
  run_application_hook(AH_CONFIG_PRE_PRE_INIT);
}

void
app_config_pre_init(void)
{
  run_application_hook(AH_CONFIG_PRE_INIT);
}

void
app_config_stopped(void)
{
  run_application_hook(AH_CONFIG_STOPPED);
}

void
app_config_changed(void)
{
  run_application_hook(AH_CONFIG_CHANGED);
}

void
app_reopen_files(void)
{
  run_application_hook(AH_REOPEN_FILES);
}

void
app_thread_start(void)
{
  scratch_buffers_allocator_init();
  dns_caching_thread_init();
  main_loop_call_thread_init();
  run_application_thread_init_hooks();
}

void
app_thread_stop(void)
{
  run_application_thread_deinit_hooks();
  main_loop_call_thread_deinit();
  dns_caching_thread_deinit();
  scratch_buffers_allocator_deinit();
  timeutils_cache_deinit();
}