Author: mladen.turk(a)jboss.com
Date: 2014-01-08 02:10:51 -0500 (Wed, 08 Jan 2014)
New Revision: 3165
Modified:
trunk/utils/windows/native/service/jbosssvc/jboss.c
trunk/utils/windows/native/service/jbosssvc/jboss.rc
Log:
Fix JBNATIVE-80
Modified: trunk/utils/windows/native/service/jbosssvc/jboss.c
===================================================================
--- trunk/utils/windows/native/service/jbosssvc/jboss.c 2013-11-22 15:12:52 UTC (rev
3164)
+++ trunk/utils/windows/native/service/jbosssvc/jboss.c 2014-01-08 07:10:51 UTC (rev
3165)
@@ -1,7 +1,7 @@
/*
* JBOSSSVC - JBoss Service helper
*
- * Copyright(c) 2013 Red Hat Inc.,
+ * Copyright(c) 2014 Red Hat Inc.,
* and individual contributors as indicated by the @authors tag.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
@@ -133,6 +133,7 @@
static enum _service_mode_e _service_mode = mode_none;
static HANDLE _service_run_event = NULL;
static HANDLE _service_run_hproc = NULL;
+static HANDLE _service_run_job = NULL;
static DWORD _service_run_pid = 0;
static BOOL IsWindowsNT()
@@ -656,7 +657,7 @@
}
static BOOL RunChildProcess(LPCSTR szApplication, LPSTR szCmdLine,
- LPPROCESS_INFORMATION lpprInfo)
+ LPPROCESS_INFORMATION lpprInfo, HANDLE hJob)
{
STARTUPINFO stInfo;
BOOL bResult;
@@ -679,14 +680,41 @@
lpprInfo);
CleanNullACL(sa);
+ if (bResult) {
+ if (hJob) {
+ bResult = AssignProcessToJobObject(hJob, lpprInfo->hProcess);
+ }
+ ResumeThread(lpprInfo->hThread);
+ }
return bResult;
}
-static BOOL TerminateProcessGroup(DWORD dwProcessId, UINT retCode)
+static HANDLE CreateLimitedJob()
{
+ JOBOBJECT_EXTENDED_LIMIT_INFORMATION jbi;
+ HANDLE pJob;
+ DWORD dwSz = (DWORD)sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION);
+
+ pJob = CreateJobObject(NULL, NULL);
+ if (pJob == NULL)
+ return NULL;
+
+ memset(&jbi, 0, dwSz);
+ jbi.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
+ if (!SetInformationJobObject(pJob, JobObjectExtendedLimitInformation, &jbi,
dwSz)) {
+ CloseHandle(pJob);
+ return NULL;
+ }
+ return pJob;
+}
+
+static BOOL TerminateProcessGroup(DWORD dwProcessId, UINT retCode, HANDLE hJob)
+{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
+ if (hJob == NULL)
+ return TRUE;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
return FALSE;
@@ -701,9 +729,12 @@
do {
if (pe32.th32ParentProcessID == dwProcessId) {
+ BOOL r;
HANDLE hP = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
if (hP) {
- TerminateProcess(hP, retCode);
+ if (IsProcessInJob(hP, hJob, &r) && r) {
+ TerminateProcess(hP, retCode);
+ }
CloseHandle(hP);
}
}
@@ -717,29 +748,31 @@
static DWORD ServiceStop()
{
PROCESS_INFORMATION prInfo;
+ HANDLE hJob;
char cmd[MAX_CMDLINE + 1];
if (!IsServiceRunning(_service_name)) {
AddToMessageLog(FALSE, "Service %s is already stopped",
_service_name);
return 0;
}
-
+ hJob = CreateLimitedJob();
BuildCommandLine(cmd, "stop");
AddToMessageLog(FALSE, "Stopping service %s", _service_name);
- ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, _service_shtm);
+ ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, _service_shtm * 2 + 3);
- if (RunChildProcess(_cmd_exe, cmd, &prInfo)) {
+ if (RunChildProcess(_cmd_exe, cmd, &prInfo, hJob)) {
if (WaitForSingleObject(prInfo.hProcess, _service_shtm * 1000) == WAIT_OBJECT_0)
AddToMessageLog(FALSE, "Service stop executed %s", _service_name);
else
- TerminateProcessGroup(prInfo.dwProcessId, 1);
+ TerminateProcessGroup(prInfo.dwProcessId, 1, hJob);
CloseHandle(prInfo.hProcess);
CloseHandle(prInfo.hThread);
+ CloseHandle(hJob);
- ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, _service_shtm);
+ ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, _service_shtm + 3);
if (WaitForSingleObject(_service_run_event, _service_shtm * 1000) !=
WAIT_OBJECT_0) {
- ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 2);
- TerminateProcessGroup(_service_run_pid, 1);
+ ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 3);
+ TerminateProcessGroup(_service_run_pid, 1, _service_run_job);
if (WaitForSingleObject(_service_run_event, 2000) != WAIT_OBJECT_0) {
if (_service_run_hproc != NULL)
TerminateProcess(_service_run_hproc, 1);
@@ -761,7 +794,7 @@
BuildCommandLine(cmd, "restart");
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
- if (RunChildProcess(_cmd_exe, cmd, &prInfo)) {
+ if (RunChildProcess(_cmd_exe, cmd, &prInfo, NULL)) {
ReportServiceStatus(SERVICE_RUNNING, NO_ERROR, 0);
rv = WaitForSingleObject(prInfo.hProcess, INFINITE);
printf("Restart Wait %d %d\n", rv, WAIT_OBJECT_0);
@@ -835,23 +868,30 @@
{
DWORD rv;
PROCESS_INFORMATION prInfo;
+ HANDLE hJob;
char cmd[MAX_CMDLINE + 1];
BuildCommandLine(cmd, "start");
DumpParams();
+ if ((hJob = CreateLimitedJob()) == NULL) {
+ AddToMessageLog(TRUE, "Creating job for service %s", _service_name);
+ return 1;
+ }
AddToMessageLog(FALSE, "Starting service %s", _service_name);
ResetEvent(_service_run_event);
- if (RunChildProcess(_cmd_exe, cmd, &prInfo)) {
+ if (RunChildProcess(_cmd_exe, cmd, &prInfo, hJob)) {
AddToMessageLog(FALSE, "Started service %s", _service_name);
ReportServiceStatus(SERVICE_RUNNING, NO_ERROR, 0);
SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE);
_service_run_hproc = prInfo.hProcess;
_service_run_pid = prInfo.dwProcessId;
+ _service_run_job = hJob;
rv = WaitForSingleObject(prInfo.hProcess, INFINITE);
_service_run_hproc = NULL;
+ _service_run_job = NULL;
_service_run_pid = 0;
AddToMessageLog(FALSE, "Finished service %s", _service_name);
@@ -861,6 +901,7 @@
CloseHandle(prInfo.hProcess);
CloseHandle(prInfo.hThread);
+ CloseHandle(hJob);
return 0;
}
else {
Modified: trunk/utils/windows/native/service/jbosssvc/jboss.rc
===================================================================
--- trunk/utils/windows/native/service/jbosssvc/jboss.rc 2013-11-22 15:12:52 UTC (rev
3164)
+++ trunk/utils/windows/native/service/jbosssvc/jboss.rc 2014-01-08 07:10:51 UTC (rev
3165)
@@ -25,7 +25,7 @@
*/
#include <windows.h>
-#define STR_COPYRIGHT "Copyright � 2013 Red Hat Inc. " \
+#define STR_COPYRIGHT "Copyright � 2014 Red Hat Inc. " \
"or its licensors, as applicable."
#define STR_LICENSE "Distributable under LGPL license. " \
@@ -34,8 +34,8 @@
#define STR_COMPANY "Red Hat�, Inc."
#define STR_TRADEMARK "� Red Hat Inc."
#define STR_PRODUCT "JBoss Service wrapper"
-#define STR_VERISON "1.2.5.0"
-#define CSV_VERISON 1,2,5,0
+#define STR_VERISON "1.2.6.0"
+#define CSV_VERISON 1,2,6,0
#define PRIVATE_BUILD 0
#define PRERELEASE_BUILD 0