Subject: Re: Cygwin service doesn't get NT shutdown notification
From: "Paul Garceau" <[email-masked]>
To: [email-masked]
Date: Thu, 26 Apr 2001 13:23:42 -0700

Hi folks,

On 26 Apr 2001, at 11:35, the Illustrious Fred Yankowski wrote:

> I'm trying to write a wrapper for the Cygwin port of PostgreSQL so
> that it can run as an NT service and stop cleanly when the service is
> stopped or shutdown due to system shutdown.  I've got it working well
> when the service is stopped manually, but when I shutdown/restart my NT
> system the service process seems to get killed without being notified of
> the shutdown.
> 
> I've written a simple NT service that can be built for both Cygwin and
> native NT (based on code from Corinna Vinschen, Chuck Wilson, and Jason
> Tishler).  The native Win32 version *does* catch and handle the shutdown
> event, but the same code built for Cygwin does not seem to get the
> event.

	Just guessing, but are you including a specific message handling 
subroutine/module geared to interface between the Cygwin (bash) shell 
and the NT4 (comspec) shell?  Without being able to draw from 
experience (another guess  -- hopefully an educated one), there is a 
possibility that Cygwin registry settings are overwriting PostGreSQL 
registry settings, thereby corrupting your NT4 registry when it comes 
to shut down (system shut down/restart).

	When I think about what you said re: manual shutdown of service, the 
corrupt registry makes the most sense to me, given that the NT4 Task 
Manager Application can over-ride any existing service settings on 
command via manual launch of Task Manager (CTRL-ALT-DEL).

	In simple terms, NT4 Task Manager allows you to manually shut down any 
run-away process or application when you have the proper user-
privileges (e.g. default NT4 Administrator privileges).

	It may be that Cygwin doesn't have sufficient Access on your system 
(user privelege) to actually launch a shut-down event from Cygwin 
system task (bash).

	Again, the above are just guesses based on the following elements:

	NT4 thinks of two different shells as two different processes/tasks
	NT4 has a Posix Subsystem.
	NT4 Messaging may require specific code in order to go through ACL 
(Access Control Level) and not have the specific code required 
("Developer forgot something").

	NT4 is very picky about NT4 Services (any tasks launchable from
		 Control Panel|Services Applet) of any kind.

	NT4 security levels are even higher if we are talking about services
		 that	have been launched as System (user) tasks.

	Some things I might try:
	
	Setting up service using Win32api (ie. srvany.exe) from a NTVDM shell 
(NT Virtual DOS Machine) apart from Cygwin (thereby placing service 
directly under NT4 specific task/process controls).

[Caveat: "srvany.exe" is a Resource Kit Utility and, afaik, does not 
exist out-of-the-box for NT4 OS, unless of course, you have already 
purchased the NT4 Resource Kit.]

	Check NT4 registry to be certain that it is not being corrupted or 
overwritten by some other process/task/service.

	Insight 5.0 might be able to help you winnow out the problem as well.

	This email is an example of what is largely educated guessing on my 
part (been using NT4 Workstation since 1994 or 1995; installed latest 
version -- relatively speaking, of Cygwin v17? about that time as 
well).

	Hope it helps.

	Peace,

		Paul G.

	
	
> 
> I'm very interested in solving this problem so that the Cygwin version
> too can catch and handle the shutdown event.  Can anyone suggest some
> way to attack the problem?  Is there some way to trace what's going on
> that might shed some light?  Unfortunately, much of the action is
> internal to the Windows NT mechanism for communicating from the Service
> Control Manager to the supervisory thread in the service process itself,
> all of which is opague.  In particular, I don't know what form of IPC is
> used to signal the shutdown event.  It's interesting that the Cygwin
> service process does catch and handle the event for a manual stop of the
> service.
> 
> Does something happen during NT shutdown that disables or kills all
> running Cygwin processes, that might kill a service process before it
> gets the SHUTDOWN notification?  Does something happen to/within the
> shared Cygwin DLL at shutdown?  Will calls to functions provided by the
> Cygwin DLL fail during shutdown? 
> 
> The source code I'm using for this test is available at
> <http://ontosys.com/src/testcygservice.tgz>.  I would be most grateful
> for any help in inspecting the code and testing the execution.  I've
> attached a copy of commentary from the code that explains how to build
> and run it.
> 
> -- 
> Fred Yankowski           [email-masked]      tel: +1.630.879.1312
> Principal Consultant     www.OntoSys.com       fax: +1.630.879.1370
> OntoSys, Inc             38W242 Deerpath Rd, Batavia, IL 60510, USA
> 
> --
> 
> This file provides all the code needed to create a simple NT service,
> including command-line functions to install and remove the service. The
> service itself simply prints a status message every few seconds to a
> logfile.
> 
> This code can be built using Cygwin/gcc, or using MS VC++ for a
> native/non-Cygwin build.  The point is to track down a problem where NT
> services built with Cygwin don't get a chance to shutdown cleanly when
> the system is shutdown.
> 
> To build for Cygwin, simply run 'make'.
> 
> To build using VC++, create a new 'Win32 Application' project.  Add
> service.c to the project.  In the Link/Output settings, set the
> entry-point symbol to 'mainCRTStartup' (no quotes, of course).  Build as
> normal.
> 
> To install the service, do the following from a shell:
> 
>    $ ./service.exe --install-as-service
> 
> That creates a service named testcygservice or testwinservice for the
> Cygwin or non-Cygwin build, respectively.  Look in the generated
> testcygservice.log (or testwinservice.log) file to check for any error
> reports.
> 
> To run the service, just do
> 
>    $ net start testcygservice   # or testwinservice
> 
> When run as a service the log file will be created in the
> $SYSTEMROOT/system32 folder.
> 
> To test operation over a restart, start the service as above, verify
> that it's operating by checking that the log file is getting new "count
> = NN" messages every few seconds, then restart the system.
> 
> Check the logfile again after the restart finishes.  If the service was
> able to catch the shutdown event the logfile will contain messages like
> this:
> 
> If the service does catch the shutdown event then the logfile will
> include a section like this:
> 
>  13:32:39 count = 26
>  13:32:44 count = 27
>  13:32:48 service_handler(5) begins
>  13:32:49 count = 28
>  13:32:49 work_main() ends
>  13:32:49 work process stopping
>  13:35:40 service_main(1,) begins
>  13:35:40 work_main() begins
>  13:35:45 count = 0
>  13:35:50 count = 1
> 
> The "service_handler(5)" message shows it catching the SHUTDOWN
> notification from the NT Service Control Manager, and then doing a
> clean shutdown.
> 
> If the service *doesn't* catch the shutdown event the logfile will
> have a section like this, where the "service_main" message is the
> first message logged after restart:
> 
>       14:06:14 count = 19
>       14:06:19 count = 20
>       14:09:08 service_main(1,) begins
>       14:09:08 work_main() begins
>       14:09:13 count = 0
>       14:09:18 count = 1
> 
> To uninstall the service, do
> 
>    $ net stop testcygservice
>    $ ./service.exe --remove-as-service
> 
> 
> --
> Want to unsubscribe from this list?
> Check out: http://cygwin.com/ml/#unsubscribe-simple
> 
> 

Nothing real can be threatened.
    Nothing unreal exists.

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple