From 0df6272cf9676afc230a934dfdd078d72ab469b1 Mon Sep 17 00:00:00 2001 From: fyodor Date: Thu, 13 Aug 2009 21:54:55 +0000 Subject: [PATCH] Signal handling fixes: fixed nbase configure to actually set HAVE_SIGNAL properly so our signal handing code actually activates. But our signal handling code was a bit questionable in many respects, so I removed most of it under the theory that it hasn't been working for years and nobody seemed to notice/care, so we presumably don't really need it. And if we do decide to add it again, we basically will need to do it over because we now want to do it safely from a reentrancy perspective. In particular, I removed sigdie() and reaper(). We still have a few signal() calls. In particular, we ignore SIGPIPE and we set SIGINT, SIGTERM, SIGHUP, and SIGSEGV to the defaults (SIG_DFL) explicitly. Thanks to Ithilgore, Luis, and Solar Designer in particular for fidning or commenting on the issue. --- main.cc | 10 --------- nmap.cc | 69 +-------------------------------------------------------- nmap.h | 1 - 3 files changed, 1 insertion(+), 79 deletions(-) diff --git a/main.cc b/main.cc index 68a3452a6..a7ffa6688 100644 --- a/main.cc +++ b/main.cc @@ -170,16 +170,6 @@ int main(int argc, char *argv[]) { mtrace(); #endif - /* Trap these sigs for cleanup */ -#if HAVE_SIGNAL - signal(SIGINT, sigdie); - signal(SIGTERM, sigdie); -#ifndef WIN32 - signal(SIGHUP, sigdie); - signal(SIGCHLD, reaper); -#endif -#endif - if ((cptr = getenv("NMAP_ARGS"))) { if (Snprintf(command, sizeof(command), "nmap %s", cptr) >= (int) sizeof(command)) { error("Warning: NMAP_ARGS variable is too long, truncated"); diff --git a/nmap.cc b/nmap.cc index 50c1871d2..aae608cc0 100644 --- a/nmap.cc +++ b/nmap.cc @@ -1326,11 +1326,6 @@ int nmap_main(int argc, char *argv[]) { tty_init(); // Put the keyboard in raw mode -#if HAVE_SIGNAL - if (!o.debugging) - signal(SIGSEGV, sigdie); -#endif - // After the arguments are fully processed we now make any of the timing // tweaks the user might've specified: if (pre_max_parallelism != -1) o.max_parallelism = pre_max_parallelism; @@ -1614,7 +1609,7 @@ int nmap_main(int argc, char *argv[]) { #if defined(HAVE_SIGNAL) && defined(SIGPIPE) signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE so our program doesn't crash because - of it, but we really shouldn't get an unsuspected + of it, but we really shouldn't get an unexpected SIGPIPE */ #endif @@ -2738,68 +2733,6 @@ int ftp_anon_connect(struct ftpinfo *ftp) { return sd; } -#ifndef WIN32 - -void reaper(int signo) { - int status; - pid_t pid; - - if ((pid = wait(&status)) == -1) { - gh_perror("waiting to reap child"); - } else { - fprintf(stderr, "\n[%d finished status=%d (%s)]\nnmap> ", (int) pid, status, (status == 0)? "success" : "failure"); - } -} -#endif - -void sigdie(int signo) { - int abt = 0; - - fflush(stdout); - - switch(signo) { - case SIGINT: - error("caught SIGINT signal, cleaning up"); - break; - -#ifdef SIGTERM - case SIGTERM: - error("caught SIGTERM signal, cleaning up"); - break; -#endif - -#ifdef SIGHUP - case SIGHUP: - error("caught SIGHUP signal, cleaning up"); - break; -#endif - -#ifdef SIGSEGV - case SIGSEGV: - error("caught SIGSEGV signal, cleaning up"); - abt = 1; - break; -#endif - -#ifdef SIGBUS - case SIGBUS: - error("caught SIGBUS signal, cleaning up"); - abt = 1; - break; -#endif - - default: - error("caught signal %d, cleaning up", signo); - abt = 1; - break; - } - - fflush(stderr); - log_close(LOG_MACHINE|LOG_NORMAL|LOG_SKID); - if (abt) abort(); - exit(1); -} - /* Returns one if the file pathname given exists, is not a directory and * is readable by the executing process. Returns two if it is readable * and is a directory. Otherwise returns 0. diff --git a/nmap.h b/nmap.h index 3c69a4347..7046596d9 100644 --- a/nmap.h +++ b/nmap.h @@ -439,7 +439,6 @@ void nmap_free_mem(); /* general helper functions */ const char *statenum2str(int state); const char *scantype2str(stype scantype); -void sigdie(int signo); void reaper(int signo); char *seqreport(struct seq_info *seq); const char *ipidclass2ascii(int seqclass);