Blob Blame History Raw
To: vim-dev@vim.org
Subject: Patch 7.1.279
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
------------

Patch 7.1.279
Problem:    When using cscope temporary files are left behind.
Solution:   Send the quit command to cscope and give it two seconds to exit
	    nicely before killing it. (partly by Dominique Pelle)
Files:	    src/if_cscope.c


*** ../vim-7.1.278/src/if_cscope.c	Fri Sep 14 19:56:18 2007
--- src/if_cscope.c	Sat Mar 15 12:38:12 2008
***************
*** 2096,2101 ****
--- 2096,2113 ----
      return CSCOPE_SUCCESS;
  }
  
+ #if defined(UNIX) && defined(SIGALRM)
+ /*
+  * Used to catch and ignore SIGALRM below.
+  */
+ /* ARGSUSED */
+     static RETSIGTYPE
+ sig_handler SIGDEFARG(sigarg)
+ {
+     /* do nothing */
+     SIGRETURN;
+ }
+ #endif
  
  /*
   * PRIVATE: cs_release_csp
***************
*** 2108,2116 ****
      int i;
      int freefnpp;
  {
- #if defined(UNIX)
-     int pstat;
- #else
      /*
       * Trying to exit normally (not sure whether it is fit to UNIX cscope
       */
--- 2120,2125 ----
***************
*** 2119,2124 ****
--- 2128,2179 ----
  	(void)fputs("q\n", csinfo[i].to_fp);
  	(void)fflush(csinfo[i].to_fp);
      }
+ #if defined(UNIX)
+     {
+ 	int pstat;
+ 	pid_t pid;
+ 
+ # if defined(HAVE_SIGACTION)
+ 	struct sigaction sa, old;
+ 
+         /* Use sigaction() to limit the waiting time to two seconds. */
+ 	sa.sa_handler = sig_handler;
+ 	sa.sa_flags = SA_NODEFER;
+ 	sigaction(SIGALRM, &sa, &old);
+ 	alarm(2); /* 2 sec timeout */
+ 
+ 	/* Block until cscope exits or until timer expires */
+ 	pid = waitpid(csinfo[i].pid, &pstat, 0);
+ 
+ 	/* cancel pending alarm if still there and restore signal */
+ 	alarm(0);
+ 	sigaction(SIGALRM, &old, NULL);
+ # else
+ 	int waited;
+ 
+ 	/* Can't use sigaction(), loop for two seconds.  First yield the CPU
+ 	 * to give cscope a chance to exit quickly. */
+ 	sleep(0);
+ 	for (waited = 0; waited < 40; ++waited)
+ 	{
+ 	    pid = waitpid(csinfo[i].pid, &pstat, WNOHANG);
+ 	    if (pid != 0)
+ 		break;  /* break unless the process is still running */
+ 	    mch_delay(50, FALSE); /* sleep 50 ms */
+ 	}
+ # endif
+ 	/*
+ 	 * If the cscope process is still running: kill it.
+ 	 * Safety check: If the PID would be zero here, the entire X session
+ 	 * would be killed.  -1 and 1 are dangerous as well.
+ 	 */
+ 	if (pid < 0 && csinfo[i].pid > 1)
+ 	{
+ 	    kill(csinfo[i].pid, SIGTERM);
+ 	    (void)waitpid(csinfo[i].pid, &pstat, 0);
+ 	}
+     }
+ #else  /* !UNIX */
      if (csinfo[i].hProc != NULL)
      {
  	/* Give cscope a chance to exit normally */
***************
*** 2133,2150 ****
      if (csinfo[i].to_fp != NULL)
  	(void)fclose(csinfo[i].to_fp);
  
-     /*
-      * Safety check: If the PID would be zero here, the entire X session would
-      * be killed.  -1 and 1 are dangerous as well.
-      */
- #if defined(UNIX)
-     if (csinfo[i].pid > 1)
-     {
- 	kill(csinfo[i].pid, SIGTERM);
- 	(void)waitpid(csinfo[i].pid, &pstat, 0);
-     }
- #endif
- 
      if (freefnpp)
      {
  	vim_free(csinfo[i].fname);
--- 2188,2193 ----
*** ../vim-7.1.278/src/version.c	Wed Mar 12 21:47:31 2008
--- src/version.c	Sat Mar 15 12:38:58 2008
***************
*** 668,669 ****
--- 668,671 ----
  {   /* Add new patch number below this line */
+ /**/
+     279,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
130. You can't get out of your desk even if it's time to eat or time
     to go to the bathroom.

 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///