You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by me...@future.co.jp on 2001/06/08 10:17:25 UTC

stop_tomcat( ) : TerminateProcess( )->Safe_TerminateProcess( )

*	

 I think, based on http://www.wdj.com/articles/1999/9907/9907c/9907c.htm
 article, the call of TerminateProcess( ) in stop_tomcat( )
 should better be replaced with SafeTerminateProcess( ).


  Megh Raj Nepal
    Tokyo, Japan.

static void stop_tomcat(short port, 
                        const char *protocol,
                        HANDLE hTomcat)
{
    struct sockaddr_in in;
    
    if(jk_resolve("localhost", port, &in)) {
        int sd = jk_open_socket(&in, JK_TRUE, NULL);
        if(sd >0) {
            int rc = JK_FALSE;
            if(!strcasecmp(protocol, "ajp13")) {
                jk_pool_t pool;
                jk_msg_buf_t *msg = NULL;
                jk_pool_atom_t buf[TINY_POOL_SIZE];

                jk_open_pool(&pool, buf, sizeof(buf));

                msg = jk_b_new(&pool);
                jk_b_set_buffer_size(msg, 512); 

                rc = ajp13_marshal_shutdown_into_msgb(msg, 
                                                      &pool,
                                                      NULL);
                if(rc) {
                    jk_b_end(msg);
    
                    if(0 > jk_tcp_socket_sendfull(sd, 
                                                  jk_b_get_buff(msg),
                                                  jk_b_get_len(msg))) {
                        rc = JK_FALSE;
                    }
                }                                                    
            } else {
                char b[] = {(char)254, (char)15};
                rc = send(sd, b, 2, 0);
                if(2 == rc) {
                    rc = JK_TRUE;
                }
            }
            jk_close_socket(sd);
            if(JK_TRUE == rc) {
                if(WAIT_OBJECT_0 == WaitForSingleObject(hTomcat, 30*1000)) {
                    return;
                }
            }            
        }
    }

    //TerminateProcess(hTomcat, 0);    
   SafeTerminateProcess(hTomcat, 0);    
}

BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode)
{
    DWORD dwTID, dwCode, dwErr = 0;
    HANDLE hProcessDup = INVALID_HANDLE_VALUE;
    HANDLE hRT = NULL;
    HINSTANCE hKernel = GetModuleHandle("Kernel32");
    BOOL bSuccess = FALSE;

    BOOL bDup = DuplicateHandle(GetCurrentProcess(), 
                                hProcess, 
                                GetCurrentProcess(), 
                                &hProcessDup, 
                                PROCESS_ALL_ACCESS, 
                                FALSE, 
                                0);

    // Detect the special case where the process is 
    // already dead...
    if ( GetExitCodeProcess((bDup) ? hProcessDup : hProcess, &dwCode) && 
         (dwCode == STILL_ACTIVE) ) 
    {
        FARPROC pfnExitProc;
           
        pfnExitProc = GetProcAddress(hKernel, "ExitProcess");

        hRT = CreateRemoteThread((bDup) ? hProcessDup : hProcess, 
                                 NULL, 
                                 0, 
                                 (LPTHREAD_START_ROUTINE)pfnExitProc,
                                 (PVOID)uExitCode, 0, &dwTID);

        if ( hRT == NULL )
            dwErr = GetLastError();
    }
    else
    {
        dwErr = ERROR_PROCESS_ABORTED;
    }


    if ( hRT )
    {
        // Must wait process to terminate to 
        // guarantee that it has exited...
        WaitForSingleObject((bDup) ? hProcessDup : hProcess, 
                            INFINITE);

        CloseHandle(hRT);
        bSuccess = TRUE;
    }

    if ( bDup )
        CloseHandle(hProcessDup);

    if ( !bSuccess )
        SetLastError(dwErr);

    return bSuccess;
}



To: tomcat-dev@jakarta.apache.org <ma...@jakarta.apache.org> 
*	Subject: Re: [PATCH] jk_nt_service shutdown 
*	From: "Craig R. McClanahan" < Craig.McClanahan@eng.sun.com
<ma...@eng.sun.com>> 
*	Date: Sat, 04 Nov 2000 20:53:12 -0800 
*	Delivered-To: mailing list tomcat-dev@jakarta.apache.org 
*	list-help: < <ma...@jakarta.apache.org>> 
*	list-post: < <ma...@jakarta.apache.org>> 
*	list-unsubscribe: <
<ma...@jakarta.apache.org>> 
*	Mailing-List: contact tomcat-dev-help@jakarta.apache.org; run by
ezmlm 
*	References:
<D1...@natasha.auslabs.avaya.com> 
*	Reply-To: tomcat-dev@jakarta.apache.org
<ma...@jakarta.apache.org> 

  _____  

Luke,

I apologize for not giving you credit for this patch -- it was included
(along
with other patches) by someone else.  But thanks!!!

Craig McClanahan


"Kirby, Luke" wrote:

> Hi!
>
> It would seem that the Jakarta NT Service fails to properly shutdown
Tomcat.
> Examining the code shows that it does the right thing and sends the
ajp1[23]
> shutdown message but then happily proceeds to terminate the Tomcat process
> before it has a chance to shutdown gracefully. Oops!
>
> The patch to fix it is below. This patch also includes a fix to the .dsp
> file, required by the mod_jk reorganisation.
>
> Enjoy!
>
>  Luke
>
> Index: jk_nt_service.c
> ===================================================================
> RCS file:
> /home/cvspublic/jakarta-tomcat/src/native/mod_jk/nt_service/jk_nt_service.
c,
> v
> retrieving revision 1.1
> diff -u -r1.1 jk_nt_service.c
> --- jk_nt_service.c     2000/08/26 02:03:53     1.1
> +++ jk_nt_service.c     2000/10/25 04:19:10
> @@ -639,9 +639,10 @@
>  static void stop_tomcat(short port,
>                          const char *protocol,
>                          HANDLE hTomcat)
> -{
> +{
> +
>      struct sockaddr_in in;
> -
> +
>      if(jk_resolve("localhost", port, &in)) {
>          int sd = jk_open_socket(&in, JK_TRUE, NULL);
>          if(sd >0) {
> @@ -659,7 +660,7 @@
>                  rc = ajp13_marshal_shutdown_into_msgb(msg,
>                                                        &pool,
>                                                        NULL);
> -                if(rc) {
> +                if (JK_TRUE == rc) {
>                      jk_b_end(msg);
>
>                      if(0 > jk_tcp_socket_sendfull(sd,
> @@ -670,13 +671,13 @@
>                  }
>              } else {
>                  char b[] = {(char)254, (char)15};
> -                int rc = send(sd, b, 2, 0);
> -                if(2 == rc) {
> +                if(2 == send(sd, b, 2, 0)) {
>                      rc = JK_TRUE;
>                  }
>              }
>              jk_close_socket(sd);
> -            if(2 == rc) {
> +            if(JK_TRUE == rc) {
> +                // shutdown message sent - give it a moment to finish.
>                  if(WAIT_OBJECT_0 == WaitForSingleObject(hTomcat,
30*1000))
> {
>                      return;
>                  }
> Index: nt_service.dsp
> ===================================================================
> RCS file:
>
/home/cvspublic/jakarta-tomcat/src/native/mod_jk/nt_service/nt_service.dsp,v
> retrieving revision 1.1
> diff -u -r1.1 nt_service.dsp
> --- nt_service.dsp      2000/08/26 02:03:53     1.1
> +++ nt_service.dsp      2000/10/25 04:19:10
> @@ -42,7 +42,7 @@
>  # PROP Ignore_Export_Lib 0
>  # PROP Target_Dir ""
>  # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"
/D
> "_MBCS" /YX /FD /c
> -# ADD CPP /nologo /W3 /GX /O2 /I "../jk" /D "WIN32" /D "NDEBUG" /D
> "_CONSOLE" /D "_MBCS" /YX /FD /c
> +# ADD CPP /nologo /W3 /GX /O2 /I "../common" /D "WIN32" /D "NDEBUG" /D
> "_CONSOLE" /D "_MBCS" /YX /FD /c
>  # ADD BASE RSC /l 0x409 /d "NDEBUG"
>  # ADD RSC /l 0x409 /d "NDEBUG"
>  BSC32=bscmake.exe
> @@ -66,7 +66,7 @@
>  # PROP Ignore_Export_Lib 0
>  # PROP Target_Dir ""
>  # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D
> "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
> -# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../jk" /D "WIN32" /D "_DEBUG"
/D
> "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
> +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../common" /D "WIN32" /D
"_DEBUG"
> /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
>  # ADD BASE RSC /l 0x409 /d "_DEBUG"
>  # ADD RSC /l 0x409 /d "_DEBUG"
>  BSC32=bscmake.exe
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org

  _____  


*	Prev by Date: Re: I think that it is a bug. 
*	Next by Date: RE: Why tomcat 3.x makes redirects (302) absolute ? 
*	Prev by thread: Re: I think that it is a bug. 
*	Next by thread: Absolute URLs on redirections 
*	Index(es): 

*	Date 
*	Thread