| 1 |
/* setsig.c - Set a signal handler, trying to turning on the SA_RESTART bit |
| 2 |
Copyright (C) 1997-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* Written by Miles Bader. */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include <signal.h> |
| 26 |
|
| 27 |
/* This is exactly like the traditional signal function, but turns on the |
| 28 |
SA_RESTART bit where possible. */ |
| 29 |
sighandler_t |
| 30 |
setsig (int sig, sighandler_t handler) |
| 31 |
{ |
| 32 |
#ifdef HAVE_SIGACTION |
| 33 |
struct sigaction sa, osa; |
| 34 |
sigemptyset (&sa.sa_mask); |
| 35 |
sigemptyset (&osa.sa_mask); |
| 36 |
# ifdef SA_RESTART |
| 37 |
sa.sa_flags |= SA_RESTART; |
| 38 |
# endif |
| 39 |
sa.sa_handler = handler; |
| 40 |
if (sigaction (sig, &sa, &osa) < 0) |
| 41 |
return SIG_ERR; |
| 42 |
return osa.sa_handler; |
| 43 |
#else /* !HAVE_SIGACTION */ |
| 44 |
# ifdef HAVE_SIGVEC |
| 45 |
struct sigvec sv, osv; |
| 46 |
sigemptyset (&sv.sv_mask); |
| 47 |
sigemptyset (&osv.sv_mask); |
| 48 |
sv.sv_handler = handler; |
| 49 |
if (sigvec (sig, &sv, &osv) < 0) |
| 50 |
return SIG_ERR; |
| 51 |
return osv.sv_handler; |
| 52 |
# else/* !HAVE_SIGVEC */ |
| 53 |
return signal (sig, handler); |
| 54 |
# endif/* HAVE_SIGVEC */ |
| 55 |
#endif /* HAVE_SIGACTION */ |
| 56 |
} |