Author: mladen.turk(a)jboss.com
Date: 2011-04-28 05:20:16 -0400 (Thu, 28 Apr 2011)
New Revision: 2756
Modified:
trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.c
trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.h
trunk/utils/windows/native/service/procrun/apps/prunsrv/prunsrv.c
Log:
Sync code with commons-daemon 1.0.5
Modified: trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.c
===================================================================
--- trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.c 2011-04-28 09:13:19
UTC (rev 2755)
+++ trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.c 2011-04-28 09:20:16
UTC (rev 2756)
@@ -1514,7 +1514,7 @@
break;
case WM_COMMAND:
- switch(LOWORD(wParam)) {
+ switch (LOWORD(wParam)) {
case IDM_TM_CONFIG:
ShowServiceProperties(hWnd);
break;
@@ -1564,7 +1564,7 @@
}
break;
case WM_TRAYMESSAGE:
- switch(lParam) {
+ switch (lParam) {
case WM_LBUTTONDBLCLK:
ShowServiceProperties(hWnd);
break;
@@ -1701,11 +1701,18 @@
if (!_options[0].dwValue) {
mutex = CreateMutex(NULL, FALSE, _gui_store->szWndMutex);
if ((mutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS)) {
- /* Skip sytem error message */
- SetLastError(ERROR_SUCCESS);
- if (!quiet)
- apxDisplayError(TRUE, NULL, 0, apxLoadResourceA(IDS_ALREAY_RUNING, 0),
- lpCmdline->szApplication);
+ HANDLE hOther = FindWindow(_gui_store->szWndClass, NULL);
+ if (hOther) {
+ SetForegroundWindow(hOther);
+ SendMessage(hOther, WM_COMMAND, MAKEWPARAM(IDM_TM_CONFIG, 0), 0);
+ }
+ else {
+ /* Skip sytem error message */
+ SetLastError(ERROR_SUCCESS);
+ if (!quiet)
+ apxDisplayError(TRUE, NULL, 0, apxLoadResourceA(IDS_ALREAY_RUNING,
0),
+ lpCmdline->szApplication);
+ }
goto cleanup;
}
}
Modified: trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.h
===================================================================
--- trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.h 2011-04-28 09:13:19
UTC (rev 2755)
+++ trunk/utils/windows/native/service/procrun/apps/prunmgr/prunmgr.h 2011-04-28 09:20:16
UTC (rev 2756)
@@ -15,7 +15,6 @@
*/
/* ====================================================================
- *
* Contributed by Mladen Turk <mturk(a)apache.org>
* 05 Aug 2003
* ====================================================================
@@ -24,7 +23,6 @@
#ifndef _PRUNMGR_H
#define _PRUNMGR_H
-#undef PRG_VERSION
#define PRG_VERSION "2.0.5.1"
#define CSV_VERSION 2,0,5,1
#define PRG_REGROOT L"JBoss\\JBoss Web 2"
@@ -75,10 +73,11 @@
#define IDC_PPLGPATH 2642
#define IDC_PPLGBPATH 2643
#define IDC_PPLGPREFIX 2644
-#define IDC_PPLGSTDOUT 2645
-#define IDC_PPLGBSTDOUT 2646
-#define IDC_PPLGSTDERR 2647
-#define IDC_PPLGBSTDERR 2648
+#define IDC_PPLGPIDFILE 2645
+#define IDC_PPLGSTDOUT 2646
+#define IDC_PPLGBSTDOUT 2647
+#define IDC_PPLGSTDERR 2648
+#define IDC_PPLGBSTDERR 2649
#define IDD_PROPPAGE_JVM 2660
#define IDC_PPJAUTO 2661
@@ -139,3 +138,4 @@
#define IDS_NOTIMPLEMENTED 3199
#endif /* _PRUNMGR_H */
+
Modified: trunk/utils/windows/native/service/procrun/apps/prunsrv/prunsrv.c
===================================================================
--- trunk/utils/windows/native/service/procrun/apps/prunsrv/prunsrv.c 2011-04-28 09:13:19
UTC (rev 2755)
+++ trunk/utils/windows/native/service/procrun/apps/prunsrv/prunsrv.c 2011-04-28 09:20:16
UTC (rev 2756)
@@ -41,10 +41,12 @@
#define STDERR_FILENO 2
#define ONE_MINUTE (60 * 1000)
-#ifdef WIN64
+#ifdef _WIN64
#define KREG_WOW6432 KEY_WOW64_32KEY
+#define PRG_BITS 64
#else
#define KREG_WOW6432 0
+#define PRG_BITS 32
#endif
typedef struct APX_STDWRAP {
@@ -59,8 +61,11 @@
static LPCWSTR PRSRV_AUTO = L"auto";
static LPCWSTR PRSRV_JAVA = L"java";
static LPCWSTR PRSRV_JVM = L"jvm";
+static LPCWSTR PRSRV_JDK = L"jdk";
+static LPCWSTR PRSRV_JRE = L"jre";
static LPCWSTR PRSRV_MANUAL = L"manual";
static LPCWSTR PRSRV_JBIN = L"\\bin\\java.exe";
+static LPCWSTR PRSRV_PBIN = L"\\bin";
static LPCWSTR PRSRV_SIGNAL = L"SIGNAL";
static LPCWSTR STYPE_INTERACTIVE = L"interactive";
@@ -229,7 +234,10 @@
static HANDLE gPidfileHandle = NULL;
static LPWSTR gPidfileName = NULL;
static BOOL gSignalValid = TRUE;
+static APXJAVA_THREADARGS gRargs;
+static APXJAVA_THREADARGS gSargs;
+
DWORD WINAPI eventThread(LPVOID lpParam)
{
for (;;) {
@@ -251,7 +259,7 @@
* If stderrfile is not specified it will
* go to stdoutfile.
*/
-static BOOL redirectStdStreams(APX_STDWRAP *lpWrapper)
+static BOOL redirectStdStreams(APX_STDWRAP *lpWrapper, LPAPXCMDLINE lpCmdline)
{
BOOL aErr = FALSE;
BOOL aOut = FALSE;
@@ -261,10 +269,14 @@
/* redirect to file or console */
if (lpWrapper->szStdOutFilename) {
if (lstrcmpiW(lpWrapper->szStdOutFilename, PRSRV_AUTO) == 0) {
+ WCHAR lsn[1024];
aOut = TRUE;
+ lstrcpyW(lsn, lpCmdline->szApplication);
+ lstrlocaseW(lsn);
+ lstrcatW(lsn, L"-stdout");
lpWrapper->szStdOutFilename = apxLogFile(gPool,
lpWrapper->szLogPath,
- L"service-stdout",
+ lsn,
NULL, TRUE);
}
/* Delete the file if not in append mode
@@ -278,13 +290,19 @@
*stdout = *lpWrapper->fpStdOutFile;
setvbuf(stdout, NULL, _IONBF, 0);
}
+ else
+ lpWrapper->szStdOutFilename = NULL;
}
if (lpWrapper->szStdErrFilename) {
if (lstrcmpiW(lpWrapper->szStdErrFilename, PRSRV_AUTO) == 0) {
+ WCHAR lsn[1024];
aErr = TRUE;
+ lstrcpyW(lsn, lpCmdline->szApplication);
+ lstrlocaseW(lsn);
+ lstrcatW(lsn, L"-stderr");
lpWrapper->szStdErrFilename = apxLogFile(gPool,
lpWrapper->szLogPath,
- L"service-stderr",
+ lsn,
NULL, TRUE);
}
if (!aErr)
@@ -295,6 +313,8 @@
*stderr = *lpWrapper->fpStdErrFile;
setvbuf(stderr, NULL, _IONBF, 0);
}
+ else
+ lpWrapper->szStdOutFilename = NULL;
}
else if (lpWrapper->fpStdOutFile) {
_dup2(_fileno(lpWrapper->fpStdOutFile), 2);
@@ -330,14 +350,11 @@
static void printVersion(void)
{
-#ifdef _WIN64
- int b = 64;
-#else
- int b = 32;
-#endif
fwprintf(stderr, L"Commons Daemon Service Runner version %S/Win%d (%S)\n",
- PRG_VERSION, b, __DATE__);
- fwprintf(stderr, L"Copyright (c) 2000-2010 The Apache Software
Foundation.\n");
+ PRG_VERSION, PRG_BITS, __DATE__);
+ fwprintf(stderr, L"Copyright (c) 2000-2011 The Apache Software
Foundation.\n\n"
+ L"For bug reporting instructions, please see:\n"
+
L"<URL:https://issues.apache.org/jira/browse/DAEMON>.");
}
/* Display configuration parameters */
@@ -381,6 +398,11 @@
APXHANDLE hRegistry;
int i = 0;
+ if (!lpCmdline->szApplication) {
+ /* Handle empty service names */
+ apxLogWrite(APXLOG_MARK_WARN "No service name provided");
+ return FALSE;
+ }
SetLastError(ERROR_SUCCESS);
hRegistry = apxCreateRegistryW(gPool, KEY_READ | KREG_WOW6432,
PRG_REGROOT,
@@ -786,6 +808,13 @@
return TRUE;
}
+static int onExitHook(void)
+{
+ apxLogWrite(APXLOG_MARK_DEBUG "On exit hook called ...");
+ reportServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
+ return 0;
+}
+
/* Executed when the service receives stop event */
static DWORD WINAPI serviceStop(LPVOID lpParameter)
{
@@ -813,24 +842,32 @@
apxLogWrite(APXLOG_MARK_ERROR "Failed creating java %S",
_jni_jvmpath);
return 1;
}
- if (!apxJavaInitialize(hWorker, _jni_classpath, _jni_jvmoptions,
- SO_JVMMS, SO_JVMMX, SO_JVMSS, SO_JNIVFPRINTF)) {
- rv = 2;
- apxLogWrite(APXLOG_MARK_ERROR "Failed initializing java %s",
_jni_classpath);
- goto cleanup;
- }
- if (!apxJavaLoadMainClass(hWorker, _jni_sclass, _jni_smethod, _jni_sparam)) {
- rv = 2;
- apxLogWrite(APXLOG_MARK_ERROR "Failed loading main %s class %s",
- _jni_rclass, _jni_classpath);
- goto cleanup;
- }
+ gSargs.hJava = hWorker;
+ gSargs.szClassPath = _jni_classpath;
+ gSargs.lpOptions = _jni_jvmoptions;
+ gSargs.dwMs = SO_JVMMS;
+ gSargs.dwMx = SO_JVMMX;
+ gSargs.dwSs = SO_JVMSS;
+ gSargs.bJniVfprintf = SO_JNIVFPRINTF;
+ gSargs.szClassName = _jni_sclass;
+ gSargs.szMethodName = _jni_smethod;
+ gSargs.lpArguments = _jni_sparam;
+ gSargs.szStdErrFilename = NULL;
+ gSargs.szStdOutFilename = NULL;
+
+ if (lstrcmpA(_jni_sclass, "java/lang/System") == 0)
+ _onexit(onExitHook);
/* Create sutdown event */
gShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (!apxJavaStart(hWorker)) {
+ if (!apxJavaStart(&gSargs)) {
apxLogWrite(APXLOG_MARK_ERROR "Failed starting java");
rv = 3;
}
+ else if (lstrcmpA(_jni_sclass, "java/lang/System") == 0) {
+ reportServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 5000);
+ apxLogWrite(APXLOG_MARK_DEBUG "Forcing java jni System.exit worker to
finish...");
+ return 0;
+ }
else {
apxLogWrite(APXLOG_MARK_DEBUG "Waiting for java jni stop worker to
finish...");
apxJavaWait(hWorker, INFINITE, FALSE);
@@ -841,6 +878,14 @@
else if (SO_STOPMODE) { /* Only in case we have a stop mode */
DWORD nArgs;
LPWSTR *pArgs;
+
+ if (!SO_STOPIMAGE) {
+ apxLogWrite(APXLOG_MARK_ERROR "Missing service ImageFile");
+ if (!_service_mode)
+ apxDisplayError(FALSE, NULL, 0, "Service '%S' is missing the
ImageFile",
+ _service_name ? _service_name : L"unknown");
+ return 1;
+ }
/* Redirect process */
hWorker = apxCreateProcessW(gPool,
0,
@@ -971,8 +1016,12 @@
gPidfileName = apxLogFile(gPool, SO_LOGPATH, SO_PIDFILE, NULL, FALSE);
if (GetFileAttributesW(gPidfileName) != INVALID_FILE_ATTRIBUTES) {
/* Pid file exists */
- apxLogWrite(APXLOG_MARK_ERROR "Pid file '%S' exists",
gPidfileName);
- return 1;
+ if (!DeleteFileW(gPidfileName)) {
+ /* Delete failed. Either no access or opened */
+ apxLogWrite(APXLOG_MARK_ERROR "Pid file '%S' exists",
+ gPidfileName);
+ return 1;
+ }
}
}
GetSystemTimeAsFileTime(&fts);
@@ -989,20 +1038,20 @@
apxLogWrite(APXLOG_MARK_ERROR "Failed creating java %S",
_jni_jvmpath);
return 1;
}
- if (!apxJavaInitialize(gWorker, _jni_classpath, _jni_jvmoptions,
- SO_JVMMS, SO_JVMMX, SO_JVMSS, SO_JNIVFPRINTF)) {
- rv = 2;
- apxLogWrite(APXLOG_MARK_ERROR "Failed initializing java %s",
_jni_classpath);
- goto cleanup;
- }
- if (!apxJavaLoadMainClass(gWorker, _jni_rclass, _jni_rmethod, _jni_rparam)) {
- rv = 3;
- apxLogWrite(APXLOG_MARK_ERROR "Failed loading main %s class %s",
_jni_rclass, _jni_classpath);
- goto cleanup;
- }
- apxJavaSetOut(gWorker, TRUE, gStdwrap.szStdErrFilename);
- apxJavaSetOut(gWorker, FALSE, gStdwrap.szStdOutFilename);
- if (!apxJavaStart(gWorker)) {
+ gRargs.hJava = gWorker;
+ gRargs.szClassPath = _jni_classpath;
+ gRargs.lpOptions = _jni_jvmoptions;
+ gRargs.dwMs = SO_JVMMS;
+ gRargs.dwMx = SO_JVMMX;
+ gRargs.dwSs = SO_JVMSS;
+ gRargs.bJniVfprintf = SO_JNIVFPRINTF;
+ gRargs.szClassName = _jni_rclass;
+ gRargs.szMethodName = _jni_rmethod;
+ gRargs.lpArguments = _jni_rparam;
+ gRargs.szStdErrFilename = gStdwrap.szStdErrFilename;
+ gRargs.szStdOutFilename = gStdwrap.szStdOutFilename;
+
+ if (!apxJavaStart(&gRargs)) {
rv = 4;
apxLogWrite(APXLOG_MARK_ERROR "Failed to start Java");
goto cleanup;
@@ -1010,6 +1059,13 @@
apxLogWrite(APXLOG_MARK_DEBUG "Java started %s", _jni_rclass);
}
else {
+ if (!SO_STARTIMAGE) {
+ apxLogWrite(APXLOG_MARK_ERROR "Missing service ImageFile");
+ if (!_service_mode)
+ apxDisplayError(FALSE, NULL, 0, "Service '%S' is missing the
ImageFile",
+ _service_name ? _service_name : L"unknown");
+ return 1;
+ }
/* Redirect process */
gWorker = apxCreateProcessW(gPool,
0,
@@ -1072,7 +1128,8 @@
FILE_SHARE_READ,
NULL,
CREATE_NEW,
- FILE_ATTRIBUTE_NORMAL,
+ FILE_ATTRIBUTE_NORMAL |
+ FILE_FLAG_DELETE_ON_CLOSE,
NULL);
if (gPidfileHandle != INVALID_HANDLE_VALUE) {
@@ -1221,11 +1278,25 @@
LPWSTR jx = NULL, szJH = SO_JAVAHOME;
if (!szJH)
szJH = apxGetJavaSoftHome(gPool, FALSE);
+ else if (!lstrcmpiW(szJH, PRSRV_JDK)) {
+ /* Figure out the JDK JavaHome */
+ szJH = apxGetJavaSoftHome(gPool, FALSE);
+ }
+ else if (!lstrcmpiW(szJH, PRSRV_JRE)) {
+ /* Figure out the JRE JavaHome */
+ szJH = apxGetJavaSoftHome(gPool, TRUE);
+ }
if (szJH) {
jx = apxPoolAlloc(gPool, (lstrlenW(szJH) + 16) * sizeof(WCHAR));
lstrcpyW(jx, szJH);
lstrcatW(jx, PRSRV_JBIN);
- SO_STARTPATH = szJH;
+ if (!SO_STARTPATH) {
+ /* Use JAVA_HOME/bin as start path */
+ LPWSTR szJP = apxPoolAlloc(gPool, (lstrlenW(szJH) + 8) *
sizeof(WCHAR));
+ lstrcpyW(szJP, szJH);
+ lstrcatW(szJP, PRSRV_PBIN);
+ SO_STARTPATH = szJP;
+ }
}
else {
apxLogWrite(APXLOG_MARK_ERROR "Unable to find Java Runtime
Environment.");
@@ -1248,11 +1319,25 @@
LPWSTR jx = NULL, szJH = SO_JAVAHOME;
if (!szJH)
szJH = apxGetJavaSoftHome(gPool, FALSE);
+ else if (!lstrcmpiW(szJH, PRSRV_JDK)) {
+ /* Figure out the JDK JavaHome */
+ szJH = apxGetJavaSoftHome(gPool, FALSE);
+ }
+ else if (!lstrcmpiW(szJH, PRSRV_JRE)) {
+ /* Figure out the JRE JavaHome */
+ szJH = apxGetJavaSoftHome(gPool, TRUE);
+ }
if (szJH) {
jx = apxPoolAlloc(gPool, (lstrlenW(szJH) + 16) * sizeof(WCHAR));
lstrcpyW(jx, szJH);
lstrcatW(jx, PRSRV_JBIN);
- SO_STOPPATH = szJH;
+ if (!SO_STOPPATH) {
+ LPWSTR szJP = apxPoolAlloc(gPool, (lstrlenW(szJH) + 8) *
sizeof(WCHAR));
+ lstrcpyW(szJP, szJH);
+ lstrcatW(szJP, PRSRV_PBIN);
+ /* Use JAVA_HOME/bin as stop path */
+ SO_STOPPATH = szJP;
+ }
}
else {
apxLogWrite(APXLOG_MARK_ERROR "Unable to find Java Runtime
Environment.");
@@ -1340,7 +1425,7 @@
/* Run the service in the debug mode */
BOOL docmdDebugService(LPAPXCMDLINE lpCmdline)
{
- BOOL rv = FALSE;
+ BOOL rv = TRUE;
_service_mode = FALSE;
_service_name = lpCmdline->szApplication;
@@ -1348,8 +1433,6 @@
serviceMain(0, NULL);
apxLogWrite(APXLOG_MARK_INFO "Debug service finished.");
SAFE_CLOSE_HANDLE(gPidfileHandle);
- if (gPidfileName)
- DeleteFileW(gPidfileName);
return rv;
}
@@ -1365,11 +1448,22 @@
rv = (StartServiceCtrlDispatcherW(_service_table) != 0);
apxLogWrite(APXLOG_MARK_INFO "Run service finished.");
SAFE_CLOSE_HANDLE(gPidfileHandle);
- if (gPidfileName)
- DeleteFileW(gPidfileName);
return rv;
}
+static const char *gSzProc[] = {
+ "",
+ "parse command line arguments",
+ "load configuration",
+ "run service as console application",
+ "run service",
+ "stop service",
+ "update service parameters",
+ "install service",
+ "delete service",
+ NULL
+};
+
void __cdecl main(int argc, char **argv)
{
UINT rv = 0;
@@ -1411,7 +1505,8 @@
apxLogOpen(gPool, SO_LOGPATH, SO_LOGPREFIX);
apxLogLevelSetW(NULL, SO_LOGLEVEL);
apxLogWrite(APXLOG_MARK_DEBUG "Commons Daemon procrun log initialized");
- apxLogWrite(APXLOG_MARK_INFO "Commons Daemon procrun (%s) started",
PRG_VERSION);
+ apxLogWrite(APXLOG_MARK_INFO "Commons Daemon procrun (%s %d-bit) started",
+ PRG_VERSION, PRG_BITS);
AplZeroMemory(&gStdwrap, sizeof(APX_STDWRAP));
@@ -1421,7 +1516,7 @@
gStdwrap.szStdOutFilename = SO_STDOUTPUT;
gStdwrap.szStdErrFilename = SO_STDERROR;
}
- redirectStdStreams(&gStdwrap);
+ redirectStdStreams(&gStdwrap, lpCmdline);
if (lpCmdline->dwCmdIndex == 2) {
SYSTEMTIME t;
GetLocalTime(&t);
@@ -1474,9 +1569,18 @@
}
cleanup:
- if (rv)
+ if (rv) {
+ int ipx = 0;
+ if (rv > 0 && rv < 7)
+ ipx = rv;
apxLogWrite(APXLOG_MARK_ERROR "Commons Daemon procrun failed "
- "with exit value: %d", rv);
+ "with exit value: %d (Failed to %s)",
+ rv, gSzProc[ipx]);
+ if (ipx > 2 && !_service_mode) {
+ /* Print something to the user console */
+ apxDisplayError(FALSE, NULL, 0, "Failed to %s", gSzProc[ipx]);
+ }
+ }
else
apxLogWrite(APXLOG_MARK_INFO "Commons Daemon procrun finished");
if (lpCmdline)