/*
* setsig_func.h

Copyright (C) 2021 Alessandro Vesely

This file is part of Ipqbdb.

Ipqbdb is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Ipqbdb 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 Ipqbdb.  If not, see <http://www.gnu.org/licenses/>.

*/

#if !defined SETSIG_FUNC_H_INCLUDED

/*
* app_private and err_prefix must be defined in the main file
* before including this.
*/

#include <signal.h>
#include <syslog.h>

static volatile int caught_signal;

static void sig_catcher(int sig)
{
	if (sig == SIGABRT)
	{
		syslog(LOG_CRIT, "aborting %s\n", err_prefix);
		abort();
	}
	caught_signal = sig;
}

static int setsigs(void)
{
	int rtc;
	struct sigaction act;
	memset(&act, 0, sizeof act);
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = sig_catcher;

	rtc =
#if defined(SETSIG_FUNC_CATCH_SIGUSR1)
		sigaction(SIGUSR1, &act, NULL) ||
#endif
		sigaction(SIGPIPE, &act, NULL) ||
		sigaction(SIGILL, &act, NULL)  ||
		sigaction(SIGHUP, &act, NULL)  ||
		sigaction(SIGINT, &act, NULL)  ||
		sigaction(SIGQUIT, &act, NULL) ||
		sigaction(SIGPWR,  &act, NULL) ||
		sigaction(SIGABRT, &act, NULL) ||
		sigaction(SIGTERM, &act, NULL);

	act.sa_handler = SIG_IGN;
	rtc |=
		sigaction(SIGALRM, &act, NULL) ||
		sigaction(SIGIO,   &act, NULL) ||
#if !defined(SETSIG_FUNC_CATCH_SIGUSR1)
		sigaction(SIGUSR1, &act, NULL) ||
#endif
		sigaction(SIGUSR2, &act, NULL);
//		sigaction(SIGEMT,  &act, NULL);
	return rtc;
}

#define SETSIG_FUNC_H_INCLUDED
#endif
