chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] replace signal with sigaction


From: Alan Post
Subject: Re: [Chicken-users] replace signal with sigaction
Date: Thu, 29 Sep 2011 16:42:35 -0600

On Thu, Sep 29, 2011 at 07:25:31AM -0600, Alan Post wrote:
> This patch replaces signal with sigaction for registering signals.
> sigaction is a newer API for signal processing that fixes some
> deficiencies of the original signal API.  One fix can be seen in
> this patch: we don't have to reregister the signal handler after
> a signal is sent.
> 
> That prevents a race condition whereby a signal is delivered, our
> signal handler is reset, and before we can reregister our signal
> handler, another signal is sent (which we then miss).
> 
> I'm not sure even signal() behaves this way these days.
> 
> It used to be one tested for sigaction in the same way you might
> test for other features.  I'm not sure if chicken runs on a
> platform that doesn't have sigaction--do I need to add a feature
> test for this and preserve the existing capability on platforms
> without sigaction?
> 
> -Alan
> 

I have ammended this patch to include a HAVE_SIGACTION define,
to preserve the existing functionality on w32 systems.

diff --git a/Makefile.bsd b/Makefile.bsd
index 5eab203..98e44fd 100644
--- a/Makefile.bsd
+++ b/Makefile.bsd
@@ -83,6 +83,7 @@ chicken-config.h: chicken-defaults.h
        echo "#define HAVE_LONG_LONG 1" >>$@
        echo "#define HAVE_MEMMOVE 1" >>$@
        echo "#define HAVE_MEMORY_H 1" >>$@
+       echo "#define HAVE_SIGACTION 1" >>$@
        echo "#define HAVE_STDINT_H 1" >>$@
        echo "#define HAVE_STDLIB_H 1" >>$@
        echo "#define HAVE_STRERROR 1" >>$@
diff --git a/Makefile.cygwin b/Makefile.cygwin
index f56bc29..cee6e74 100644
--- a/Makefile.cygwin
+++ b/Makefile.cygwin
@@ -95,6 +95,7 @@ chicken-config.h: chicken-defaults.h
        echo "#define HAVE_LONG_LONG 1" >>$@
        echo "#define HAVE_MEMMOVE 1" >>$@
        echo "#define HAVE_MEMORY_H 1" >>$@
+       echo "#define HAVE_SIGACTION 1" >>$@
        echo "#define HAVE_STDINT_H 1" >>$@
        echo "#define HAVE_STDLIB_H 1" >>$@
        echo "#define HAVE_STRERROR 1" >>$@
diff --git a/Makefile.haiku b/Makefile.haiku
index 1f86bc3..54634a2 100644
--- a/Makefile.haiku
+++ b/Makefile.haiku
@@ -71,6 +71,7 @@ chicken-config.h: chicken-defaults.h
        echo "#define HAVE_LONG_LONG 1" >>$@
        echo "#define HAVE_MEMMOVE 1" >>$@
        echo "#define HAVE_MEMORY_H 1" >>$@
+       echo "#define HAVE_SIGACTION 1" >>$@
        echo "#define HAVE_STDINT_H 1" >>$@
        echo "#define HAVE_STDLIB_H 1" >>$@
        echo "#define HAVE_STRERROR 1" >>$@
diff --git a/Makefile.linux b/Makefile.linux
index c713b45..6e5116a 100644
--- a/Makefile.linux
+++ b/Makefile.linux
@@ -72,6 +72,7 @@ chicken-config.h: chicken-defaults.h
        echo "#define HAVE_LONG_LONG 1" >>$@
        echo "#define HAVE_MEMMOVE 1" >>$@
        echo "#define HAVE_MEMORY_H 1" >>$@
+       echo "#define HAVE_SIGACTION 1" >>$@
        echo "#define HAVE_STDINT_H 1" >>$@
        echo "#define HAVE_STDLIB_H 1" >>$@
        echo "#define HAVE_STRERROR 1" >>$@
diff --git a/Makefile.macosx b/Makefile.macosx
index b4a44d9..da612a4 100644
--- a/Makefile.macosx
+++ b/Makefile.macosx
@@ -96,6 +96,7 @@ chicken-config.h: chicken-defaults.h
        echo "#define HAVE_LONG_LONG 1" >>$@
        echo "#define HAVE_MEMMOVE 1" >>$@
        echo "#define HAVE_MEMORY_H 1" >>$@
+       echo "#define HAVE_SIGACTION 1" >>$@
        echo "#define HAVE_STDINT_H 1" >>$@
        echo "#define HAVE_STDLIB_H 1" >>$@
        echo "#define HAVE_STRERROR 1" >>$@
diff --git a/Makefile.solaris b/Makefile.solaris
index f2d4dee..84dc433 100644
--- a/Makefile.solaris
+++ b/Makefile.solaris
@@ -102,6 +102,7 @@ chicken-config.h: chicken-defaults.h
        echo "#define HAVE_LONG_LONG 1" >>$@
        echo "#define HAVE_MEMMOVE 1" >>$@
        echo "#define HAVE_MEMORY_H 1" >>$@
+       echo "#define HAVE_SIGACTION 1" >>$@
        echo "#define HAVE_STDINT_H 1" >>$@
        echo "#define HAVE_STDLIB_H 1" >>$@
        echo "#define HAVE_STRERROR 1" >>$@
diff --git a/chicken.h b/chicken.h
index 1739fb9..a154fbe 100644
--- a/chicken.h
+++ b/chicken.h
@@ -859,7 +859,11 @@ DECL_C_PROC_p0 (128,  1,0,0,0,0,0,0,0)
 # define C_isatty                   isatty
 # define C_fileno                   fileno
 # define C_select                   select
+# if defined(HAVE_SIGACTION)
+# define C_sigaction                sigaction
+# else
 # define C_signal                   signal
+# endif
 # define C_getrusage                getrusage
 # define C_tolower                  tolower
 # define C_toupper                  toupper
diff --git a/runtime.c b/runtime.c
index 980f303..68e3c90 100644
--- a/runtime.c
+++ b/runtime.c
@@ -982,7 +982,9 @@ void initialize_symbol_table(void)
 void global_signal_handler(int signum)
 {
   C_raise_interrupt(signal_mapping_table[ signum ]);
+#if !defined(HAVE_SIGACTION)
   signal(signum, global_signal_handler);
+#endif
 }
 
 
@@ -4259,11 +4261,28 @@ C_regparm void C_fcall C_raise_interrupt(int reason)
 C_regparm C_word C_fcall C_establish_signal_handler(C_word signum, C_word 
reason)
 {
   int sig = C_unfix(signum);
+#if defined(HAVE_SIGACTION)
+  struct sigaction new, old;
 
-  if(reason == C_SCHEME_FALSE) C_signal(sig, SIG_IGN);
-  else {
+  new.sa_flags = 0;
+  sigemptyset(&new.sa_mask);
+#endif
+
+  if(reason == C_SCHEME_FALSE) {
+#if defined(HAVE_SIGACTION)
+    new.sa_handler = SIG_IGN;
+    C_sigaction(sig, &new, &old);
+#else
+    C_signal(sig, SIG_IGN);
+#endif
+  } else {
     signal_mapping_table[ sig ] = C_unfix(reason);
+#if defined(HAVE_SIGACTION)
+    new.sa_handler = global_signal_handler;
+    C_sigaction(sig, &new, &old);
+#else
     C_signal(sig, global_signal_handler);
+#endif
   }
 
   return C_SCHEME_UNDEFINED;
-- 
.i ma'a lo bradi cu penmi gi'e du



reply via email to

[Prev in Thread] Current Thread [Next in Thread]