Author: mladen.turk(a)jboss.com
Date: 2008-03-16 15:19:58 -0400 (Sun, 16 Mar 2008)
New Revision: 1438
Modified:
trunk/utils/windows/native/service/examples/service.bat
trunk/utils/windows/native/service/jbosssch/sch.rc
trunk/utils/windows/native/service/jbosssvc/jboss.c
trunk/utils/windows/native/service/jbosssvc/jboss.rc
Log:
Update JBoss AS service runner
Modified: trunk/utils/windows/native/service/examples/service.bat
===================================================================
--- trunk/utils/windows/native/service/examples/service.bat 2008-03-14 16:01:08 UTC (rev
1437)
+++ trunk/utils/windows/native/service/examples/service.bat 2008-03-16 19:19:58 UTC (rev
1438)
@@ -24,12 +24,12 @@
REM Figure out the running mode
-@if "%1" == "install" goto cmdInstall
-@if "%1" == "uninstall" goto cmdUninstall
-@if "%1" == "start" goto cmdStart
-@if "%1" == "stop" goto cmdStop
-@if "%1" == "restart" goto cmdRestart
-@if "%1" == "signal" goto cmdSignal
+if /I "%1" == "install" goto cmdInstall
+if /I "%1" == "uninstall" goto cmdUninstall
+if /I "%1" == "start" goto cmdStart
+if /I "%1" == "stop" goto cmdStop
+if /I "%1" == "restart" goto cmdRestart
+if /I "%1" == "signal" goto cmdSignal
echo Usage: service install^|uninstall^|start^|stop^|restart^|signal
goto cmdEnd
@@ -42,39 +42,62 @@
REM ERR_RET_MODE 6
:errExplain
-@if errorlevel 1 echo Invalid command line parameters
-@if errorlevel 2 echo Failed installing %SVCDISP%
-@if errorlevel 4 echo Failed removing %SVCDISP%
-@if errorlevel 6 echo Unknown service mode for %SVCDISP%
+if errorlevel 1 echo Invalid command line parameters
+if errorlevel 2 echo Failed installing %SVCDISP%
+if errorlevel 4 echo Failed removing %SVCDISP%
+if errorlevel 6 echo Unknown service mode for %SVCDISP%
goto cmdEnd
:cmdInstall
jbosssvc.exe -iwdc %SVCNAME% "%DIRNAME%" "%SVCDISP%"
"%SVCDESC%" service.bat
-@if not errorlevel 0 goto errExplain
+if not errorlevel 0 goto errExplain
echo Service %SVCDISP% installed
goto cmdEnd
:cmdUninstall
jbosssvc.exe -u %SVCNAME%
-@if not errorlevel 0 goto errExplain
+if not errorlevel 0 goto errExplain
echo Service %SVCDISP% removed
goto cmdEnd
:cmdStart
REM Executed on service start
-call run.bat >run.log
+del .r.lock 2>&1 | findstr /C:"being used" > nul
+if not errorlevel 1 (
+ echo Could not continue. Locking file already in use.
+ goto cmdEnd
+)
+echo Y > .r.lock
+echo Starting %SVCDISP% > run.log
+call run.bat < .r.lock >> run.log 2>&1
+echo Shutdown service >> run.log
+del .r.lock
goto cmdEnd
:cmdStop
REM Executed on service stop
-call shutdown -S >shutdown.log
+echo Y > .s.lock
+call shutdown -S < .s.lock > shutdown.log 2>&1
+del .s.lock
goto cmdEnd
:cmdRestart
REM Executed on service restart
REM Note. We can only stop and start
-call shutdown -S >>shutdown.log
-call run.bat >>run.log
+echo Y > .s.lock
+call shutdown -S < .s.lock >> shutdown.log 2>&1
+del .s.lock
+:waitRun
+REM Delete lock file
+del .r.lock > nul 2>&1
+REM Wait one second if lock file exist
+jbosssvc.exe -s 1
+if exist ".r.lock" goto waitRun
+echo Y > .r.lock
+echo Restarting %SVCDISP% >> run.log
+call run.bat < .r.lock >> run.log 2>&1
+echo Shutdown service >> run.log
+del .r.lock
goto cmdEnd
:cmdSignal
Modified: trunk/utils/windows/native/service/jbosssch/sch.rc
===================================================================
--- trunk/utils/windows/native/service/jbosssch/sch.rc 2008-03-14 16:01:08 UTC (rev 1437)
+++ trunk/utils/windows/native/service/jbosssch/sch.rc 2008-03-16 19:19:58 UTC (rev 1438)
@@ -38,8 +38,8 @@
#define STR_COMPANY "Red Hat�, Inc."
#define STR_TRADEMARK "� Red Hat Inc."
#define STR_PRODUCT "JBoss Service helper"
-#define STR_VERISON "1.2.2.0"
-#define CSV_VERISON 1,2,2,0
+#define STR_VERISON "1.2.3.0"
+#define CSV_VERISON 1,2,3,0
#define PRIVATE_BUILD 0
#define PRERELEASE_BUILD 0
Modified: trunk/utils/windows/native/service/jbosssvc/jboss.c
===================================================================
--- trunk/utils/windows/native/service/jbosssvc/jboss.c 2008-03-14 16:01:08 UTC (rev
1437)
+++ trunk/utils/windows/native/service/jbosssvc/jboss.c 2008-03-16 19:19:58 UTC (rev
1438)
@@ -127,6 +127,8 @@
static enum _service_mode_e _service_mode = mode_none;
static HANDLE _service_run_event = NULL;
+static HANDLE _service_run_hproc = NULL;
+static DWORD _service_run_pid = 0;
static BOOL IsWindowsNT()
{
@@ -265,11 +267,13 @@
" service.bat\n");
fprintf(stderr, " -u service\n");
fprintf(stderr, " -t service\n");
+ fprintf(stderr, " -s\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -d Service display name\n");
fprintf(stderr, " -c Service description\n");
fprintf(stderr, " -w Service working path\n");
fprintf(stderr, " -l Turn info logging On\n");
+ fprintf(stderr, " -s Sleep 1 second and exit\n");
ExitProcess(ERR_RET_USAGE);
}
@@ -649,11 +653,42 @@
return bResult;
}
+static BOOL TerminateProcessGroup(HANDLE hProcess, DWORD dwProcessId, UINT retCode)
+{
+ HANDLE hProcessSnap;
+ PROCESSENTRY32 pe32;
+ hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+ if (hProcessSnap == INVALID_HANDLE_VALUE) {
+ return FALSE;
+ }
+
+ pe32.dwSize = sizeof(PROCESSENTRY32);
+
+ if (!Process32First(hProcessSnap, &pe32)) {
+ CloseHandle(hProcessSnap);
+ return FALSE;
+ }
+
+ do {
+ if (pe32.th32ParentProcessID == dwProcessId) {
+ HANDLE hP = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
+ if (hP) {
+ TerminateProcess(hP, retCode);
+ CloseHandle(hP);
+ }
+ }
+ } while (Process32Next(hProcessSnap, &pe32));
+
+ CloseHandle(hProcessSnap);
+ return TerminateProcess(hProcess, retCode);
+
+}
+
/* Executed when the service receives stop event */
static DWORD ServiceStop()
{
- DWORD rv;
+ int i = 0;
PROCESS_INFORMATION prInfo;
char cmd[MAX_CMDLINE + 1];
@@ -663,24 +698,26 @@
}
BuildCommandLine(cmd, "stop");
AddToMessageLog(FALSE, "Stopping service %s", _service_name);
+
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
- if (RunChildProcess(_cmd_exe, cmd, &prInfo)) {
- rv = WaitForSingleObject(prInfo.hProcess, INFINITE);
+ while (RunChildProcess(_cmd_exe, cmd, &prInfo)) {
+ WaitForSingleObject(prInfo.hProcess, INFINITE);
AddToMessageLog(FALSE, "Stopped service %s", _service_name);
CloseHandle(prInfo.hProcess);
CloseHandle(prInfo.hThread);
- rv = WaitForSingleObject(_service_run_event, INFINITE);
- AddToMessageLog(FALSE, "Stopped service child %s", _service_name);
-
- ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
- return 0;
+ ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
+ if (WaitForSingleObject(_service_run_event, 10000) == WAIT_OBJECT_0)
+ break;
+ if (i++ > 4) {
+ ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
+ TerminateProcessGroup(_service_run_hproc, _service_run_pid, 1);
+ AddToMessageLog(FALSE, "Service terminated %s", _service_name);
+ }
}
- else {
- ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
- AddToMessageLog(TRUE, "Stopping service %s", _service_name);
- return 1;
- }
+ ReportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
+ AddToMessageLog(FALSE, "Stopping service %s", _service_name);
+ return 0;
}
/* Executed when the service receives restart event */
@@ -778,7 +815,12 @@
ReportServiceStatus(SERVICE_RUNNING, NO_ERROR, 0);
SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE);
+ _service_run_hproc = prInfo.hProcess;
+ _service_run_pid = prInfo.dwProcessId;
rv = WaitForSingleObject(prInfo.hProcess, INFINITE);
+ _service_run_hproc = NULL;
+ _service_run_pid = 0;
+
AddToMessageLog(FALSE, "Finished service %s", _service_name);
ReportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
SetEvent(_service_run_event);
@@ -800,8 +842,7 @@
DWORD rc;
_service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
_service_status.dwCurrentState = SERVICE_START_PENDING;
- _service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
- SERVICE_ACCEPT_PAUSE_CONTINUE;
+ _service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
_service_status.dwWin32ExitCode = 0;
_service_status.dwCheckPoint = 0;
_service_status.dwWaitHint = 0;
@@ -834,7 +875,7 @@
{
UINT rv = 0;
char *arg;
- int i;
+ int i, l;
int args_left = 1;
int need_desc = 0;
int need_disp = 0;
@@ -857,6 +898,12 @@
while (*++arg != '\0') {
switch (*arg) {
+ case 's':
+ l = atoi(argv[i+1]);
+ Sleep(l * 1000);
+ ExitProcess(0);
+ return;
+ break;
case 'i':
_service_mode = mode_install;
args_left++;
@@ -902,8 +949,12 @@
if ((argc - i) < args_left)
ErrorUsage();
lstrcpy(_service_name, argv[i++]);
+ for (l = 0; l < lstrlen(_service_name); l++) {
+ /* Make service name uppercase */
+ _service_name[l] = toupper((unsigned char)_service_name[l]);
+ }
if (need_path) {
- int l = lstrlen(argv[i]);
+ l = lstrlen(argv[i]);
lstrcpy(_working_path, argv[i++]);
/* Remove trailing backslash */
if (l > 0 && _working_path[l - 1] == '\\')
Modified: trunk/utils/windows/native/service/jbosssvc/jboss.rc
===================================================================
--- trunk/utils/windows/native/service/jbosssvc/jboss.rc 2008-03-14 16:01:08 UTC (rev
1437)
+++ trunk/utils/windows/native/service/jbosssvc/jboss.rc 2008-03-16 19:19:58 UTC (rev
1438)
@@ -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.2.0"
-#define CSV_VERISON 1,2,2,0
+#define STR_VERISON "1.2.3.0"
+#define CSV_VERISON 1,2,3,0
#define PRIVATE_BUILD 0
#define PRERELEASE_BUILD 0