From 986f6c98244610da6bba88d0c92895b9758973ee Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 22 Nov 2009 00:44:31 +0000 Subject: [PATCH] notmuch-new: Only install SIGALRM if not running under gdb I felt sorry for Carl trying to step through an exception from xapian and suffering from the SIGALARMs.. We can detect if the user launched notmuch under a debugger by either checking our cmdline for the presence of the gdb string or querying if valgrind is controlling our process. For the latter we need to add a compile time check for the valgrind development library, and so add the initial support to build Makefile.config from configure. Signed-off-by: Chris Wilson Reviewed-by: Carl Worth [ickle: And do not install the timer when under the debugger] --- Makefile.config | 1 + Makefile.local | 3 ++- configure | 21 +++++++++++++++++---- debugger.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ notmuch-client.h | 3 +++ notmuch-new.c | 49 +++++++++++++++++++++++++++--------------------- 6 files changed, 98 insertions(+), 26 deletions(-) create mode 100644 debugger.c diff --git a/Makefile.config b/Makefile.config index d72a39ef..ddc74365 100644 --- a/Makefile.config +++ b/Makefile.config @@ -1,2 +1,3 @@ prefix = /usr/local bash_completion_dir = /etc/bash_completion.d +CFLAGS += -DHAVE_VALGRIND diff --git a/Makefile.local b/Makefile.local index b6d3db3c..28286597 100644 --- a/Makefile.local +++ b/Makefile.local @@ -3,6 +3,8 @@ all: notmuch notmuch.1.gz emacs: notmuch.elc notmuch_client_srcs = \ + debugger.c \ + gmime-filter-reply.c \ notmuch.c \ notmuch-config.c \ notmuch-dump.c \ @@ -14,7 +16,6 @@ notmuch_client_srcs = \ notmuch-show.c \ notmuch-tag.c \ notmuch-time.c \ - gmime-filter-reply.c \ query-string.c \ show-message.c diff --git a/configure b/configure index fe46c8e3..b4770ec9 100755 --- a/configure +++ b/configure @@ -53,6 +53,14 @@ else errors=$((errors + 1)) fi +if pkg-config --modversion valgrind > /dev/null 2>&1; then + echo "Checking for valgrind development files... Yes." + have_valgrind=-DHAVE_VALGRIND +else + echo "Checking for valgrind development files... No." + have_valgrind= +fi + if [ $errors -gt 0 ]; then cat < Makefile.config < + */ + +#include "notmuch-client.h" + +#include + +#if HAVE_VALGRIND +#include +#else +#define RUNNING_ON_VALGRIND 0 +#endif + +notmuch_bool_t +debugger_is_active (void) +{ + char buf[1024]; + + if (RUNNING_ON_VALGRIND) + return TRUE; + + sprintf (buf, "/proc/%d/exe", getppid ()); + if (readlink (buf, buf, sizeof (buf)) != -1 && + strncmp (basename (buf), "gdb", 3) == 0) + { + return TRUE; + } + + return FALSE; +} diff --git a/notmuch-client.h b/notmuch-client.h index b65aa777..ea776860 100644 --- a/notmuch-client.h +++ b/notmuch-client.h @@ -179,4 +179,7 @@ notmuch_config_set_user_other_email (notmuch_config_t *config, const char *other_email[], size_t length); +notmuch_bool_t +debugger_is_active (void); + #endif diff --git a/notmuch-new.c b/notmuch-new.c index 43cc4fb7..1616ee96 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -259,6 +259,7 @@ add_files (notmuch_database_t *notmuch, notmuch_status_t status; struct sigaction action; struct itimerval timerval; + notmuch_bool_t timer_is_active = FALSE; if (stat (path, &st)) { fprintf (stderr, "Error reading directory %s: %s\n", @@ -272,31 +273,37 @@ add_files (notmuch_database_t *notmuch, } /* Setup our handler for SIGALRM */ - memset (&action, 0, sizeof (struct sigaction)); - action.sa_handler = handle_sigalrm; - sigemptyset (&action.sa_mask); - action.sa_flags = SA_RESTART; - sigaction (SIGALRM, &action, NULL); - - /* Then start a timer to send SIGALRM once per second. */ - timerval.it_interval.tv_sec = 1; - timerval.it_interval.tv_usec = 0; - timerval.it_value.tv_sec = 1; - timerval.it_value.tv_usec = 0; - setitimer (ITIMER_REAL, &timerval, NULL); + if (! debugger_is_active ()) { + memset (&action, 0, sizeof (struct sigaction)); + action.sa_handler = handle_sigalrm; + sigemptyset (&action.sa_mask); + action.sa_flags = SA_RESTART; + sigaction (SIGALRM, &action, NULL); + + /* Then start a timer to send SIGALRM once per second. */ + timerval.it_interval.tv_sec = 1; + timerval.it_interval.tv_usec = 0; + timerval.it_value.tv_sec = 1; + timerval.it_value.tv_usec = 0; + setitimer (ITIMER_REAL, &timerval, NULL); + + timer_is_active = TRUE; + } status = add_files_recursive (notmuch, path, &st, state); /* Now stop the timer. */ - timerval.it_interval.tv_sec = 0; - timerval.it_interval.tv_usec = 0; - timerval.it_value.tv_sec = 0; - timerval.it_value.tv_usec = 0; - setitimer (ITIMER_REAL, &timerval, NULL); - - /* And disable the signal handler. */ - action.sa_handler = SIG_IGN; - sigaction (SIGALRM, &action, NULL); + if (timer_is_active) { + timerval.it_interval.tv_sec = 0; + timerval.it_interval.tv_usec = 0; + timerval.it_value.tv_sec = 0; + timerval.it_value.tv_usec = 0; + setitimer (ITIMER_REAL, &timerval, NULL); + + /* And disable the signal handler. */ + action.sa_handler = SIG_IGN; + sigaction (SIGALRM, &action, NULL); + } return status; } -- 2.26.2