You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by "William A. Rowe, Jr." <wr...@lnd.com> on 2000/05/16 17:45:23 UTC

Win32 Process Window Termination

Win32 Folks;

  [those easily annoyed by Wintel can ignore this thread - 
it's another good example of the M$ boneheaded cr*p :-]

  I've attacked and substantially conquered the process termination
issues under WinNT.  The code I'll be posting handles all manners of
ending the server, including Ctrl+C, Ctrl+Break, the close window
button and system shutdown.  None of the following applies to WinNT...

  Using JJK's submission as a base, I've also conquered starting
Apache as a service under Win9x.  When not as-service, it also handles
the Ctrl+C and Ctrl+Break situations well.  When as-service, I've tweaked
the code to hide, rather than destroy, the console window.  But I don't
care for the 'hidden window' approach if I can avoid it... since the
issue goes hand in hand with the following not-as-service issue:

  Win9x uses a different Console wrapper, the window class tty rather
than the NT ConsoleWindowClass, and has no provision to send the various
shutdown and close events to the executable.  Kermit 5.0, several other
compatibility thunks and other console apps seem to share the same issues.

  I've tried (unsuccessfully) to handle these events by subclassing the 
tty window.  This doesn't succeed, but indicates no error code upon 
failure (other than returning 0 from SetWindowLong, which is unexpected, 
since the WNDPROC does have a value.)  I'm assuming that another process 
owns this tty window, even though it appears to be our own (per spy).

  I've got only a few other options, none of which I like.  The obvious
is to create a hidden window (I -hate- this), implement a third party
console emulator (yes, a few exist), change to a GUI, poll via the VM
calls (not calls, but int 2F!?!) etc, etc, etc.

  If anyone has a good suggestion to offer, that does not break the
common code base (meaning, don't start in terms of the Win32 rewrite!)
I'm all ears.

Bill

RE: Win32 Process Window Termination

Posted by "William A. Rowe, Jr." <wr...@lnd.com>.
> From: Brian Moon [mailto:brianm@dealnews.com]
> Sent: Tuesday, May 16, 2000 11:29 AM
> 
> It seems that the easiest way to go would be to make the app 
> a window-less
> gui app.  I hope that makes sense.  In windows you can have 
> an app that is
> not console but at the same time does not have a window.  
> Now, I do not know
> the code inside and out, but it seems that should work.

The biggest agent against this is the command line style of
Apache.  I am not sure how the process could recapture sharing
of the same console after it has been launched as a windowed app.

I don't want to go to the obvious, one .exe for Apache9x services
(as you described) and one for everything else.  But I suspect 
Win9x will require a Window message loop of it's own, within a
hidden window.  I have found no other realistic mechanism (non-
polled) to detect that the system is shutting down.  But this
solution does nothing to help detect that the user clicked the 
close button of the console window.
 
> I believe the console windows are owned by a process named
> C:\WINDOWS\SYSTEM\conagent.exe.  If you look at the 
> properties of a console
> window in Win98 you will see that they all show that as the 
> program that is
> running and not the process itself.  Kind of strange.  Now, I 
> do not have
> 2.0 working on 98 yet, but for 1.3, I know that winoldap is 
> fired up when
> you start apache.  Not sure why.  Welcome to MS.

I know the conagent plays some very strange games, especially
compared to WinNT.  All for compatibility with the broad market
of 16 bit dos apps :-P  Unfortunately it's entirely undocumented.

> From: "William A. Rowe, Jr." <wr...@lnd.com>
> To: <ne...@apache.org>
> 
> > Win32 Folks;
> >
> >   [those easily annoyed by Wintel can ignore this thread -
> > it's another good example of the M$ boneheaded cr*p :-]
> >
> >   I've attacked and substantially conquered the process termination
> > issues under WinNT.  The code I'll be posting handles all manners of
> > ending the server, including Ctrl+C, Ctrl+Break, the close window
> > button and system shutdown.  None of the following applies 
> to WinNT...
> >
> >   Using JJK's submission as a base, I've also conquered starting
> > Apache as a service under Win9x.  When not as-service, it 
> also handles
> > the Ctrl+C and Ctrl+Break situations well.  When 
> as-service, I've tweaked
> > the code to hide, rather than destroy, the console window.  
> But I don't
> > care for the 'hidden window' approach if I can avoid it... since the
> > issue goes hand in hand with the following not-as-service issue:
> >
> >   Win9x uses a different Console wrapper, the window class 
> tty rather
> > than the NT ConsoleWindowClass, and has no provision to 
> send the various
> > shutdown and close events to the executable.  Kermit 5.0, 
> several other
> > compatibility thunks and other console apps seem to share 
> the same issues.
> >
> >   I've tried (unsuccessfully) to handle these events by 
> subclassing the
> > tty window.  This doesn't succeed, but indicates no error code upon
> > failure (other than returning 0 from SetWindowLong, which 
> is unexpected,
> > since the WNDPROC does have a value.)  I'm assuming that 
> another process
> > owns this tty window, even though it appears to be our own 
> (per spy).
> >
> >   I've got only a few other options, none of which I like.  
> The obvious
> > is to create a hidden window (I -hate- this), implement a 
> third party
> > console emulator (yes, a few exist), change to a GUI, poll 
> via the VM
> > calls (not calls, but int 2F!?!) etc, etc, etc.
> >
> >   If anyone has a good suggestion to offer, that does not break the
> > common code base (meaning, don't start in terms of the 
> Win32 rewrite!)
> > I'm all ears.
> >
> > Bill
> >
> 


Re: Win32 Process Window Termination

Posted by Brian Moon <br...@dealnews.com>.
It seems that the easiest way to go would be to make the app a window-less
gui app.  I hope that makes sense.  In windows you can have an app that is
not console but at the same time does not have a window.  Now, I do not know
the code inside and out, but it seems that should work.

I believe the console windows are owned by a process named
C:\WINDOWS\SYSTEM\conagent.exe.  If you look at the properties of a console
window in Win98 you will see that they all show that as the program that is
running and not the process itself.  Kind of strange.  Now, I do not have
2.0 working on 98 yet, but for 1.3, I know that winoldap is fired up when
you start apache.  Not sure why.  Welcome to MS.

Brian Moon
----------------------------------------------------------------------
dealnews.com, Inc.
Makers of dealnews, dealmac
http://dealnews.com/ | http://dealmac.com/


----- Original Message -----
From: "William A. Rowe, Jr." <wr...@lnd.com>
To: <ne...@apache.org>
Sent: Tuesday, May 16, 2000 10:45 AM
Subject: Win32 Process Window Termination


> Win32 Folks;
>
>   [those easily annoyed by Wintel can ignore this thread -
> it's another good example of the M$ boneheaded cr*p :-]
>
>   I've attacked and substantially conquered the process termination
> issues under WinNT.  The code I'll be posting handles all manners of
> ending the server, including Ctrl+C, Ctrl+Break, the close window
> button and system shutdown.  None of the following applies to WinNT...
>
>   Using JJK's submission as a base, I've also conquered starting
> Apache as a service under Win9x.  When not as-service, it also handles
> the Ctrl+C and Ctrl+Break situations well.  When as-service, I've tweaked
> the code to hide, rather than destroy, the console window.  But I don't
> care for the 'hidden window' approach if I can avoid it... since the
> issue goes hand in hand with the following not-as-service issue:
>
>   Win9x uses a different Console wrapper, the window class tty rather
> than the NT ConsoleWindowClass, and has no provision to send the various
> shutdown and close events to the executable.  Kermit 5.0, several other
> compatibility thunks and other console apps seem to share the same issues.
>
>   I've tried (unsuccessfully) to handle these events by subclassing the
> tty window.  This doesn't succeed, but indicates no error code upon
> failure (other than returning 0 from SetWindowLong, which is unexpected,
> since the WNDPROC does have a value.)  I'm assuming that another process
> owns this tty window, even though it appears to be our own (per spy).
>
>   I've got only a few other options, none of which I like.  The obvious
> is to create a hidden window (I -hate- this), implement a third party
> console emulator (yes, a few exist), change to a GUI, poll via the VM
> calls (not calls, but int 2F!?!) etc, etc, etc.
>
>   If anyone has a good suggestion to offer, that does not break the
> common code base (meaning, don't start in terms of the Win32 rewrite!)
> I'm all ears.
>
> Bill
>


Re: Win32 Process Window Termination

Posted by Bill Stoddard <st...@raleigh.ibm.com>.
+1. This is something I've wanted to do for awhile, just never got around to it.

BTW, please feel free to modify winnt.c. The only favor I ask is to not change the accept
and thread dispatch flows w/o bouncing it off me first (unless it is someting that is
obviously broken).  There are a lot of things to keep in mind when changing the
AcceptEx/Completion port stuff, especially with regard to shutting down the server. I
forget some of them myself and I wrote the stuff :-) And I'm still not happy with shutdown
(specifically restart)

I am against creating seperate MPMs for 95/99 and NT/2000. One MPM should dynamically
handle both. If you want to take a shot at breaking up winnt.c into seperate files
(common, nt/2000, 95/98 pieces), be my guest :-)

I expect to be out of the development loop for 2 weeks or so starting in a day or so.

Cheers,

Bill

> I'm -really- hesitant to touch this, seeing as you own
> it at this moment, Bill.  Here's the 2.0 equivilant of
> the new 1.3.x code for clean process termination.
>
> Feel free to test and criticize.  This -does-not- solve
> the Win9x hung shutdown issue, which is it's own beast.
>
> Index: src/modules/mpm/winnt/winnt.c
> ===================================================================
> RCS file: /home/cvs/apache-2.0/src/modules/mpm/winnt/winnt.c,v
> retrieving revision 1.65
> diff -u -r1.65 winnt.c
> --- src/modules/mpm/winnt/winnt.c 2000/05/12 00:43:43 1.65
> +++ src/modules/mpm/winnt/winnt.c 2000/05/16 20:00:45
>      return 0;
>  }
>
> +static BOOL CALLBACK master_control_handler(DWORD ctrl_type)
> +{
> +    switch (ctrl_type)
> +    {
> +        case CTRL_C_EVENT:
> +        case CTRL_BREAK_EVENT:
> +            fprintf(stderr, "Apache server interrupted...\n");
> +            /* for Interrupt signals, shut down the server.
> +             * Tell the system we have dealt with the signal
> +             * without waiting for Apache to terminate.
> +             */
> +            ap_start_shutdown();
> +            return TRUE;
> +
> +        case CTRL_CLOSE_EVENT:
> +        case CTRL_LOGOFF_EVENT:
> +        case CTRL_SHUTDOWN_EVENT:
> +            /* for Terminate signals, shut down the server.
> +             * Wait for Apache to terminate, but respond
> +             * after a reasonable time to tell the system
> +             * that we did attempt to shut ourself down.
> +             * THESE EVENTS WILL NOT OCCUR UNDER WIN9x!
> +             */
> +            fprintf(stderr, "Apache server shutdown initiated...\n");
> +            ap_start_shutdown();
> +            Sleep(30000);
> +            return TRUE;
> +    }
> +
> +    /* We should never get here, but this is (mostly) harmless */
> +    return FALSE;
> +}
> +
>  static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
>  {
>      int remaining_children_to_start = ap_daemons_to_start;
> @@ -1548,6 +1581,8 @@
>
>      HANDLE process_handles[MAX_PROCESSES];
>      HANDLE process_kill_events[MAX_PROCESSES];
> +
> +    SetConsoleCtrlHandler(master_control_handler, TRUE);
>
>      setup_listeners(s);
>      bind_listeners_to_completion_port();


RE: Win32 Process Window Termination

Posted by "William A. Rowe, Jr." <wr...@lnd.com>.
I'm -really- hesitant to touch this, seeing as you own 
it at this moment, Bill.  Here's the 2.0 equivilant of
the new 1.3.x code for clean process termination.

Feel free to test and criticize.  This -does-not- solve
the Win9x hung shutdown issue, which is it's own beast.

Index: src/modules/mpm/winnt/winnt.c
===================================================================
RCS file: /home/cvs/apache-2.0/src/modules/mpm/winnt/winnt.c,v
retrieving revision 1.65
diff -u -r1.65 winnt.c
--- src/modules/mpm/winnt/winnt.c	2000/05/12 00:43:43	1.65
+++ src/modules/mpm/winnt/winnt.c	2000/05/16 20:00:45
     return 0;
 }
 
+static BOOL CALLBACK master_control_handler(DWORD ctrl_type)
+{
+    switch (ctrl_type)
+    {
+        case CTRL_C_EVENT:
+        case CTRL_BREAK_EVENT:
+            fprintf(stderr, "Apache server interrupted...\n");
+            /* for Interrupt signals, shut down the server.
+             * Tell the system we have dealt with the signal
+             * without waiting for Apache to terminate.
+             */
+            ap_start_shutdown();            
+            return TRUE;
+
+        case CTRL_CLOSE_EVENT:
+        case CTRL_LOGOFF_EVENT:
+        case CTRL_SHUTDOWN_EVENT:
+            /* for Terminate signals, shut down the server.
+             * Wait for Apache to terminate, but respond
+             * after a reasonable time to tell the system
+             * that we did attempt to shut ourself down.
+             * THESE EVENTS WILL NOT OCCUR UNDER WIN9x!
+             */
+            fprintf(stderr, "Apache server shutdown initiated...\n");
+            ap_start_shutdown();
+            Sleep(30000);
+            return TRUE;
+    }
+ 
+    /* We should never get here, but this is (mostly) harmless */
+    return FALSE;
+}
+
 static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
 {
     int remaining_children_to_start = ap_daemons_to_start;
@@ -1548,6 +1581,8 @@
 
     HANDLE process_handles[MAX_PROCESSES];
     HANDLE process_kill_events[MAX_PROCESSES];
+
+    SetConsoleCtrlHandler(master_control_handler, TRUE);
 
     setup_listeners(s);
     bind_listeners_to_completion_port();