[pacman-dev] [PATCH] Use sigaction instead of signal.
From signal man page : "The behavior of signal() varies across Unix versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below."
The code was taken from there : http://www.gnu.org/software/libtool/manual/libc/Sigaction-Function-Example.h... Signed-off-by: Chantry Xavier <shiningxc@gmail.com> --- src/pacman/pacman.c | 25 ++++++++++++++++++++----- 1 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index f46b71c..9c56eae 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -212,7 +212,7 @@ static void cleanup(int ret) { * in a consistant state. * @param signum the thrown signal */ -static void handler(int signum) +static void termination_handler(int signum) { if(signum==SIGSEGV) { @@ -752,6 +752,7 @@ static int parseconfig(const char *file) int main(int argc, char *argv[]) { int ret = 0; + struct sigaction new_action, old_action; #if defined(HAVE_GETEUID) /* geteuid undefined in CYGWIN */ uid_t myuid = geteuid(); @@ -762,10 +763,24 @@ int main(int argc, char *argv[]) mtrace(); #endif - /* set signal handlers */ - signal(SIGINT, handler); - signal(SIGTERM, handler); - signal(SIGSEGV, handler); + /* Set signal handlers */ + /* Set up the structure to specify the new action. */ + new_action.sa_handler = termination_handler; + sigemptyset(&new_action.sa_mask); + new_action.sa_flags = 0; + + sigaction(SIGINT, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGINT, &new_action, NULL); + } + sigaction(SIGTERM, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGTERM, &new_action, NULL); + } + sigaction(SIGSEGV, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGSEGV, &new_action, NULL); + } /* i18n init */ #if defined(ENABLE_NLS) -- 1.5.4.2
On Sun, Mar 9, 2008 at 7:15 AM, Chantry Xavier <shiningxc@gmail.com> wrote:
From signal man page : "The behavior of signal() varies across Unix versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below."
The code was taken from there : http://www.gnu.org/software/libtool/manual/libc/Sigaction-Function-Example.h...
Signed-off-by: Chantry Xavier <shiningxc@gmail.com> --- src/pacman/pacman.c | 25 ++++++++++++++++++++----- 1 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index f46b71c..9c56eae 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -212,7 +212,7 @@ static void cleanup(int ret) { * in a consistant state. * @param signum the thrown signal */ -static void handler(int signum) +static void termination_handler(int signum) Any real reason to rename the function?
{ if(signum==SIGSEGV) { @@ -752,6 +752,7 @@ static int parseconfig(const char *file) int main(int argc, char *argv[]) { int ret = 0; + struct sigaction new_action, old_action; #if defined(HAVE_GETEUID) /* geteuid undefined in CYGWIN */ uid_t myuid = geteuid(); @@ -762,10 +763,24 @@ int main(int argc, char *argv[]) mtrace(); #endif
- /* set signal handlers */ - signal(SIGINT, handler); - signal(SIGTERM, handler); - signal(SIGSEGV, handler); + /* Set signal handlers */ + /* Set up the structure to specify the new action. */ + new_action.sa_handler = termination_handler; + sigemptyset(&new_action.sa_mask); + new_action.sa_flags = 0; + + sigaction(SIGINT, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGINT, &new_action, NULL); + } + sigaction(SIGTERM, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGTERM, &new_action, NULL); + } + sigaction(SIGSEGV, NULL, &old_action); + if(old_action.sa_handler != SIG_IGN) { + sigaction(SIGSEGV, &new_action, NULL); + }
/* i18n init */ #if defined(ENABLE_NLS) -- 1.5.4.2
Looks good, pulled locally and I just made the small change where we don't rename the handler function.
On Sun, Mar 09, 2008 at 12:00:59PM -0500, Dan McGee wrote:
On Sun, Mar 9, 2008 at 7:15 AM, Chantry Xavier <shiningxc@gmail.com> wrote:
From signal man page : "The behavior of signal() varies across Unix versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead. See Portability below."
The code was taken from there : http://www.gnu.org/software/libtool/manual/libc/Sigaction-Function-Example.h...
Signed-off-by: Chantry Xavier <shiningxc@gmail.com> --- src/pacman/pacman.c | 25 ++++++++++++++++++++----- 1 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index f46b71c..9c56eae 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -212,7 +212,7 @@ static void cleanup(int ret) { * in a consistant state. * @param signum the thrown signal */ -static void handler(int signum) +static void termination_handler(int signum) Any real reason to rename the function?
Not at all, I just made up the handler name by my previous commit. Then I just copied the code from the link above, which was using termination_handler :) I didn't know which one was better.
participants (3)
-
Chantry Xavier
-
Dan McGee
-
Xavier