aboutsummaryrefslogtreecommitdiff
path: root/nxcomp
diff options
context:
space:
mode:
authorMihai Moldovan <ionic@ionic.de>2017-09-30 15:32:33 +0200
committerMihai Moldovan <ionic@ionic.de>2017-10-27 08:46:39 +0200
commit1829a35cd0413a752bad082967508ffd8b1c7d01 (patch)
treee27f9108219a14f76f9922858a25b1e605186d2f /nxcomp
parent09586d760833dac680ba8837b0b695ed3900c96d (diff)
downloadnx-libs-1829a35cd0413a752bad082967508ffd8b1c7d01.tar.gz
nx-libs-1829a35cd0413a752bad082967508ffd8b1c7d01.tar.bz2
nx-libs-1829a35cd0413a752bad082967508ffd8b1c7d01.zip
nxcomp/src/Log.h: block signals while writing out data.
Prevents race conditions caused by signal handlers while flushing out our log queue.
Diffstat (limited to 'nxcomp')
-rw-r--r--nxcomp/src/Log.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/nxcomp/src/Log.h b/nxcomp/src/Log.h
index ecc6ea9ed..e3b1397d2 100644
--- a/nxcomp/src/Log.h
+++ b/nxcomp/src/Log.h
@@ -35,6 +35,7 @@
#include <map>
#include <stdlib.h>
#include <string.h>
+#include <signal.h>
#include <assert.h>
#include <stack>
@@ -218,6 +219,22 @@ class NXLog
*/
void flush(per_thread_data *pdt)
{
+ /*
+ * Block all signals until we are dong printing data.
+ * Ensures that a signal handler won't interrupt us
+ * and overwrite the buffer data mid-print, leading
+ * to confusing output.
+ */
+ sigset_t orig_signal_mask,
+ tmp_signal_mask;
+ sigemptyset(&orig_signal_mask);
+
+ /* Set up new mask to block all signals. */
+ sigfillset(&tmp_signal_mask);
+
+ /* Block all signals. */
+ pthread_sigmask(SIG_BLOCK, &tmp_signal_mask, &orig_signal_mask);
+
if (!pdt->buffer.empty ()) {
const std::string str = pdt->buffer.top()->str();
@@ -231,6 +248,9 @@ class NXLog
/* Remove from stack. */
pdt->buffer.pop();
}
+
+ /* Restore old signal mask. */
+ pthread_sigmask(SIG_SETMASK, &orig_signal_mask, NULL);
}