Hi,

if the exec in cygrunsrv fails or the command exits to early, cygrunsrv will hang forever.
The service can no longer be controlled until cygrunsrv has been kill(-9)ed.
Auto restart of service does not work in this case.


Steps to reproduce (at least on XP SP2):

$ cygrunsrv -I test -p /bin/true

$ cygrunsrv -S test
cygrunsrv: Error starting a service: ... Win32 error 1053:
...

$ sleep 3600 #;-)

$ cygrunsrv -S test
Service             : test
Current State       : Start Pending
Controls Accepted   : Stop
Command             : /bin/true

cygrunsrv process has to be killed manually.

Same result occurs if the command's exe is removed.


This is due to the following (IMO undocumented and "interesting";-) behavior of the windows SCM:

The StartServiceControlDispatcher() routine does not return unless some thread (no necessarily service_main()) sets SERVICE_STOPPED.

The service_main() thread started by SCM is left alone.
Exiting service_main() does nothing, in particular it does not end StartServiceControlDispatcher().

But, if SERVICE_STOPPED is set, StartServiceControlDispatcher() immediately returns, again without any care about service_main(). Because usually now the program exit()'s and kills all threads, code following SERVICE_STOPPED may not be executed at all.


The attached patch for cygrunsrv-1.10.1 demonstrates this behavior via LOG_DEBUG messages.

With the _exit() at the end of service_main(), cygrunsrv does no longer hang and auto-restart of services works.

Note that the messages at the end of service_main() will never be written if a service exits normally
(Try with: cygrunsrv -I ... -p /bin/sleep -a 30)

Thanks for any comment

Christian


--- cygrunsrv.cc.orig   2005-05-22 18:34:55.001000000 +0200
+++ cygrunsrv.cc        2005-11-12 17:33:50.546875000 +0100
@@ -1591,6 +1591,10 @@
                svcname, WTERMSIG (status));
       break;
     }
+  syslog(LOG_DEBUG,"service_main(): sleep(10)");
+  sleep(10);
+  syslog(LOG_DEBUG,"service_main(): _exit(%d)", service_main_exitval);
+  _exit(service_main_exitval);
 }
 
 int
@@ -1635,6 +1639,7 @@
       ste[1].lpServiceProc = NULL;
       if (!StartServiceCtrlDispatcherA(ste))
        return error (StartAsSvcErr);
+      syslog(LOG_DEBUG,"main(): exit(%d)", service_main_exitval);
       return service_main_exitval;
     }
   /* Started with parameters: Install, deinstall, start or stop a service. */ 

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply via email to