1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/templates/signals.c Fri Jan 25 23:12:44 2019 +0100
1.3 @@ -0,0 +1,45 @@
1.4 +/* Signal handling.
1.5 +
1.6 +Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
1.7 +
1.8 +This program is free software; you can redistribute it and/or modify it under
1.9 +the terms of the GNU General Public License as published by the Free Software
1.10 +Foundation; either version 3 of the License, or (at your option) any later
1.11 +version.
1.12 +
1.13 +This program is distributed in the hope that it will be useful, but WITHOUT
1.14 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.15 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.16 +details.
1.17 +
1.18 +You should have received a copy of the GNU General Public License along with
1.19 +this program. If not, see <http://www.gnu.org/licenses/>.
1.20 +*/
1.21 +
1.22 +#include <setjmp.h>
1.23 +#include <signal.h>
1.24 +#include <stdlib.h>
1.25 +
1.26 +#include "signals.h"
1.27 +
1.28 +void __signals_install_handlers()
1.29 +{
1.30 + struct sigaction context;
1.31 +
1.32 + context.sa_flags = SA_SIGINFO;
1.33 + context.sa_sigaction = __signals_fpe_handler;
1.34 + sigemptyset(&context.sa_mask);
1.35 +
1.36 + /* NOTE: Should test for -1 and errno. */
1.37 +
1.38 + sigaction(SIGFPE, &context, NULL);
1.39 +}
1.40 +
1.41 +jmp_buf __fpe_env;
1.42 +
1.43 +void __signals_fpe_handler(int signum, siginfo_t *siginfo, void *context)
1.44 +{
1.45 + /* Return from setjmp with the signal number. */
1.46 +
1.47 + longjmp(__fpe_env, siginfo->si_code);
1.48 +}