Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[Patch] Fix race with signals in signal_block()



Hi !

Here is patch to fix a bug where zsh can become unkillable (except of course with SIGKILL), I use Linux-2.6/i386 so the POSIX_SIGNALS macro is defined.

signal_block() and signal_unblock() use two global variables dummy_sigset1 and dummy_sigset2. I observed a race between
child_block() and
oldmask = signal_block(newmask); /* Block all signals temporarily */
called in the zhandler() signal handler.

The attached patch fixes the problem by getting rid of the global variables, and instead using the signal_block() and signal_unblock() functions.

Hopefully I did not break the NO_SIGNAL_BLOCKING, SYSV_SIGNALS and BSD_SIGNALS cases.

Cheers.

--
Guillaume
--- zsh-4.3.4/Src/signals.c
+++ zsh-4.3.4/Src/signals.c
@@ -200,15 +200,6 @@ signal_mask(int sig)
  * set. Return the old signal set.       */
 
 /**/
-#ifdef POSIX_SIGNALS
-
-/**/
-mod_export sigset_t dummy_sigset1, dummy_sigset2;
-
-/**/
-#else
-
-/**/
 #ifndef BSD_SIGNALS
 
 sigset_t
@@ -216,7 +207,11 @@ signal_block(sigset_t set)
 {
     sigset_t oset;
  
-#ifdef SYSV_SIGNALS
+#ifdef POSIX_SIGNALS
+    sigprocmask(SIG_BLOCK, &set, &oset);
+
+#else
+# ifdef SYSV_SIGNALS
     int i;
  
     oset = blocked_set;
@@ -226,7 +221,7 @@ signal_block(sigset_t set)
             sighold(i);
         }
     }
-#else  /* NO_SIGNAL_BLOCKING */
+# else  /* NO_SIGNAL_BLOCKING */
 /* We will just ignore signals if the system doesn't have *
  * the ability to block them.                             */
     int i;
@@ -238,7 +233,8 @@ signal_block(sigset_t set)
             signal_ignore(i);
         }
    }
-#endif /* SYSV_SIGNALS  */
+# endif /* SYSV_SIGNALS */
+#endif /* POSIX_SIGNALS */
  
     return oset;
 }
@@ -246,19 +242,17 @@ signal_block(sigset_t set)
 /**/
 #endif /* BSD_SIGNALS */
 
-/**/
-#endif /* POSIX_SIGNALS */
-
 /* Unblock the signals in the given signal *
  * set. Return the old signal set.         */
 
-#ifndef POSIX_SIGNALS
-
 sigset_t
 signal_unblock(sigset_t set)
 {
     sigset_t oset;
- 
+
+#ifdef POSIX_SIGNALS
+    sigprocmask(SIG_UNBLOCK, &set, &oset);
+#else
 # ifdef BSD_SIGNALS
     sigfillset(&oset);
     oset = sigsetmask(oset);
@@ -288,12 +282,11 @@ signal_unblock(sigset_t set)
    }
 #  endif /* SYSV_SIGNALS  */
 # endif  /* BSD_SIGNALS   */
+#endif   /* POSIX_SIGNALS */
  
     return oset;
 }
 
-#endif   /* POSIX_SIGNALS */
-
 /* set the process signal mask to *
  * be the given signal mask       */
 
--- zsh-4.3.4/Src/signals.h
+++ zsh-4.3.4/Src/signals.h
@@ -100,26 +100,10 @@
 
 #define restore_queue_signals(q) (queueing_enabled = (q))
 
-/* Make some signal functions faster. */
-
-#ifdef POSIX_SIGNALS
-#define signal_block(S) \
-    ((dummy_sigset1 = (S)), \
-     sigprocmask(SIG_BLOCK, &dummy_sigset1, &dummy_sigset2), \
-     dummy_sigset2)
-#else
-# ifdef BSD_SIGNALS
+#ifdef BSD_SIGNALS
 #define signal_block(S) sigblock(S)
-# else
+#else
 extern sigset_t signal_block _((sigset_t));
-# endif  /* BSD_SIGNALS   */
-#endif   /* POSIX_SIGNALS */
+#endif  /* BSD_SIGNALS   */
 
-#ifdef POSIX_SIGNALS
-#define signal_unblock(S) \
-    ((dummy_sigset1 = (S)), \
-     sigprocmask(SIG_UNBLOCK, &dummy_sigset1, &dummy_sigset2), \
-     dummy_sigset2)
-#else
 extern sigset_t signal_unblock _((sigset_t));
-#endif   /* POSIX_SIGNALS */


Messages sorted by: Reverse Date, Date, Thread, Author