delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/11/12/11:45:17

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <43761BF8.9030603@t-online.de>
Date: Sat, 12 Nov 2005 17:44:40 +0100
From: Christian Franke <Christian DOT Franke AT t-online DOT de>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b) Gecko/20050217
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: cygrunsrv hangs forever on exec error (fix included)
X-ID: SP7rKTZCoedvFaqnqK-MQMdRU4DgBpBTOGv4AlhAQ3OfXvbU2a4tcZ
X-TOI-MSGID: d636dedc-f0e2-4179-8a65-a1b4862b35de
X-IsSubscribed: yes

--------------080101040103090809000609
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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



--------------080101040103090809000609
Content-Type: text/plain;
 name="cygrunsrv-patch.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="cygrunsrv-patch.txt"

--- 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. */ 


--------------080101040103090809000609
Content-Type: text/plain; charset=us-ascii

--
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/
--------------080101040103090809000609--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019