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
|
/*
* Copyright (c) 2018 Balabit
* Copyright (c) 2018 Kokan
*
* 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 "syslog-ng.h"
#include <signal.h>
#include <string.h>
#if defined(NSIG)
# define SIGNAL_HANDLER_ARRAY_SIZE NSIG
#elif defined(_NSIG)
# define SIGNAL_HANDLER_ARRAY_SIZE _NSIG
#else
# define SIGNAL_HANDLER_ARRAY_SIZE 128
#endif
static struct sigaction external_sigactions[SIGNAL_HANDLER_ARRAY_SIZE];
static gboolean internal_sigaction_registered[SIGNAL_HANDLER_ARRAY_SIZE];
static const struct sigaction *
_get_external_sigaction(gint signum)
{
g_assert(signum < SIGNAL_HANDLER_ARRAY_SIZE);
return &external_sigactions[signum];
}
static void
_set_external_sigaction(gint signum, const struct sigaction *external_sigaction)
{
g_assert(signum < SIGNAL_HANDLER_ARRAY_SIZE);
memcpy(&external_sigactions[signum], external_sigaction, sizeof(struct sigaction));
}
static gboolean
_is_internal_sigaction_registered(gint signum)
{
g_assert(signum < SIGNAL_HANDLER_ARRAY_SIZE);
return internal_sigaction_registered[signum];
}
static void
_set_internal_sigaction_registered(gint signum)
{
g_assert(signum < SIGNAL_HANDLER_ARRAY_SIZE);
internal_sigaction_registered[signum] = TRUE;
}
void
signal_handler_exec_external_handler(gint signum)
{
const struct sigaction *external_sigaction = _get_external_sigaction(signum);
if (!external_sigaction->sa_handler)
return;
external_sigaction->sa_handler(signum);
}
#if SYSLOG_NG_HAVE_DLFCN_H
#include <dlfcn.h>
static int
_original_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
{
static int (*real_sa)(int, const struct sigaction *, struct sigaction *);
if (real_sa == NULL)
real_sa = dlsym(RTLD_NEXT, "sigaction");
return real_sa(signum, act, oldact);
}
static gint
_register_internal_sigaction(gint signum, const struct sigaction *act, struct sigaction *oldact)
{
gint result = _original_sigaction(signum, act, oldact);
if (result == 0)
_set_internal_sigaction_registered(signum);
return result;
}
static gboolean
_need_to_save_external_sigaction_handler(gint signum)
{
/* We need to save external sigaction handlers for signums we internally set a handler to. See lib/mainloop.c */
switch (signum)
{
case SIGCHLD:
case SIGINT:
return TRUE;
default:
return FALSE;
}
}
static void
_save_external_sigaction_handler(gint signum, const struct sigaction *external_sigaction)
{
if (!external_sigaction)
return;
_set_external_sigaction(signum, external_sigaction);
}
static void
_fill_oldact_with_previous_external_sigaction_handler(gint signum, struct sigaction *oldact)
{
if (!oldact)
return;
memcpy(oldact, _get_external_sigaction(signum), sizeof(struct sigaction));
}
/* This should be as defined in the <signal.h> */
int
sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
{
if (!_need_to_save_external_sigaction_handler(signum))
return _original_sigaction(signum, act, oldact);
/* Internal sigactions are always the first one to arrive to this function. */
if (!_is_internal_sigaction_registered(signum))
return _register_internal_sigaction(signum, act, oldact);
_fill_oldact_with_previous_external_sigaction_handler(signum, oldact);
_save_external_sigaction_handler(signum, act);
return 0;
}
#endif
|