Author: mladen.turk(a)jboss.com
Date: 2008-03-20 12:39:04 -0400 (Thu, 20 Mar 2008)
New Revision: 1451
Modified:
trunk/utils/windows/native/service/jbosssvc/jboss.c
Log:
Read service default shutdown timeout
Modified: trunk/utils/windows/native/service/jbosssvc/jboss.c
===================================================================
--- trunk/utils/windows/native/service/jbosssvc/jboss.c 2008-03-20 16:13:24 UTC (rev
1450)
+++ trunk/utils/windows/native/service/jbosssvc/jboss.c 2008-03-20 16:39:04 UTC (rev
1451)
@@ -70,6 +70,7 @@
#include <stdlib.h>
#include <string.h>
#include <process.h>
+#include <time.h>
/* Custom return error values */
#define ERR_RET_USAGE 1
@@ -152,6 +153,46 @@
return rv;
}
+static DWORD WaitToKillServiceTimeout()
+{
+ static DWORD dwTimeout = 0;
+
+ if (dwTimeout) {
+ return dwTimeout;
+ }
+ else {
+ DWORD err;
+ HKEY hKey;
+ CHAR wsBuf[MAX_PATH];
+ DWORD dwLen, dwType = REG_SZ;
+
+ dwTimeout = 20000;
+ if ((err = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
+ "SYSTEM\\CurrentControlSet\\Control",
+ 0,
+ KEY_READ,
+ &hKey)) != ERROR_SUCCESS) {
+ return dwTimeout;
+ }
+
+ dwLen = sizeof(wsBuf);
+ err = RegQueryValueExA(hKey,
+ "WaitToKillServiceTimeout",
+ NULL,
+ &dwType,
+ (LPBYTE)wsBuf,
+ &dwLen);
+ RegCloseKey(hKey);
+ if (err == ERROR_SUCCESS) {
+ dwTimeout = (DWORD)atoi(wsBuf);
+ if (!dwTimeout)
+ dwTimeout = 20000;
+ }
+
+ return dwTimeout;
+ }
+}
+
/* To share the semaphores with other processes, we need a NULL ACL
* Code from MS KB Q106387
*/
@@ -679,8 +720,9 @@
}
}
} while (Process32Next(hProcessSnap, &pe32));
-
+
CloseHandle(hProcessSnap);
+ Sleep(500);
return TerminateProcess(hProcess, retCode);
}
@@ -688,28 +730,38 @@
/* Executed when the service receives stop event */
static DWORD ServiceStop()
{
- int i = 0;
+ int step = 14000;
PROCESS_INFORMATION prInfo;
char cmd[MAX_CMDLINE + 1];
+ time_t s;
+ double t;
if (!IsServiceRunning(_service_name)) {
AddToMessageLog(FALSE, "Service %s is already stopped",
_service_name);
return 0;
}
+
+ t = WaitToKillServiceTimeout() / 1000 - 2;
+ time(&s);
BuildCommandLine(cmd, "stop");
AddToMessageLog(FALSE, "Stopping service %s", _service_name);
- ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
+ ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR,
+ WaitToKillServiceTimeout());
+
while (RunChildProcess(_cmd_exe, cmd, &prInfo)) {
WaitForSingleObject(prInfo.hProcess, INFINITE);
AddToMessageLog(FALSE, "Stopped service %s", _service_name);
CloseHandle(prInfo.hProcess);
CloseHandle(prInfo.hThread);
-
+
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
- if (WaitForSingleObject(_service_run_event, 10000) == WAIT_OBJECT_0)
+ if (WaitForSingleObject(_service_run_event, step) == WAIT_OBJECT_0)
break;
- if (i++ > 4) {
+
+ if (step > 2000)
+ step = 2000;
+ if (difftime(time(NULL), s) > t) {
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
TerminateProcessGroup(_service_run_hproc, _service_run_pid, 1);
AddToMessageLog(FALSE, "Service terminated %s", _service_name);