File: hook-commands.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 (162 lines) | stat: -rw-r--r-- 4,347 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2002-2018 Balabit
 *
 * 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 "hook-commands.h"
#include "apphook.h"

#include <stdlib.h>

#define HOOK_COMMANDS_PLUGIN "hook-commands"

struct _HookCommandsPlugin
{
  LogDriverPlugin super;
  gchar *startup;
  gchar *setup;
  gchar *teardown;
  gchar *shutdown;
  gboolean (*saved_init)(LogPipe *s);
  gboolean (*saved_deinit)(LogPipe *s);
};

void
hook_commands_plugin_set_startup(HookCommandsPlugin *self, const gchar *startup)
{
  g_free(self->startup);
  self->startup = g_strdup(startup);
}

void
hook_commands_plugin_set_setup(HookCommandsPlugin *self, const gchar *setup)
{
  g_free(self->setup);
  self->setup = g_strdup(setup);
}

void
hook_commands_plugin_set_teardown(HookCommandsPlugin *self, const gchar *teardown)
{
  g_free(self->teardown);
  self->teardown = g_strdup(teardown);
}

void
hook_commands_plugin_set_shutdown(HookCommandsPlugin *self, const gchar *shutdown_)
{
  g_free(self->shutdown);
  self->shutdown = g_strdup(shutdown_);
}

static gboolean
_run_hook(LogDriver *driver, const gchar *hook, const gchar *cmd)
{
  gint rc;

  if (!cmd)
    return TRUE;

  msg_debug("About to execute a hook command",
            evt_tag_str("driver", driver->id),
            log_pipe_location_tag(&driver->super),
            evt_tag_str("hook", hook),
            evt_tag_str("command", cmd));
  rc = system(cmd);
  if (rc != 0)
    {
      msg_error("Hook command returned with failure, aborting initialization",
                evt_tag_str("driver", driver->id),
                log_pipe_location_tag(&driver->super),
                evt_tag_str("hook", hook),
                evt_tag_str("command", cmd),
                evt_tag_int("rc", rc));
    }
  return rc == 0;
}

/* this is running in the context of the LogDriver, e.g.  LogPipe *s points
 * to the driver we were plugged into */
static gboolean
_init_hook(LogPipe *s)
{
  LogDriver *driver = (LogDriver *) s;
  HookCommandsPlugin *self = log_driver_get_plugin(driver, HookCommandsPlugin, HOOK_COMMANDS_PLUGIN);

  if (app_is_starting_up() && !_run_hook(driver, "startup", self->startup))
    return FALSE;

  if (!_run_hook(driver, "setup", self->setup))
    return FALSE;
  return self->saved_init(s);
}

static gboolean
_deinit_hook(LogPipe *s)
{
  LogDriver *driver = (LogDriver *) s;
  HookCommandsPlugin *self = log_driver_get_plugin(driver, HookCommandsPlugin, HOOK_COMMANDS_PLUGIN);

  _run_hook(driver, "teardown", self->teardown);

  if (app_is_shutting_down())
    _run_hook(driver, "shutdown", self->shutdown);

  /* NOTE: we call deinit() even if teardown failed, there's not much we can
   * do apart from reporting the failure, which we already did in
   * _run_hook()
   * */

  return self->saved_deinit(s);
}

static gboolean
_attach(LogDriverPlugin *s, LogDriver *driver)
{
  HookCommandsPlugin *self = (HookCommandsPlugin *) s;

  self->saved_init = driver->super.init;
  driver->super.init = _init_hook;

  self->saved_deinit = driver->super.deinit;
  driver->super.deinit = _deinit_hook;
  return TRUE;
}

static void
_free(LogDriverPlugin *s)
{
  HookCommandsPlugin *self = (HookCommandsPlugin *) s;

  g_free(self->setup);
  g_free(self->teardown);
  log_driver_plugin_free_method(s);
}

HookCommandsPlugin *
hook_commands_plugin_new(void)
{
  HookCommandsPlugin *self = g_new0(HookCommandsPlugin, 1);

  log_driver_plugin_init_instance(&self->super, HOOK_COMMANDS_PLUGIN);
  self->super.attach = _attach;
  self->super.free_fn = _free;
  return self;
}