void apxLogRotateSet(HANDLE hFile, BOOL doRotate) { apx_logfile_st *lf = (apx_logfile_st *)hFile; if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) return; lf->bRotate = doRotate; }
void apxLogLevelSet(HANDLE hFile, DWORD dwLevel) { apx_logfile_st *lf = (apx_logfile_st *)hFile; if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) return; if (dwLevel < 4) lf->dwLogLevel = dwLevel; }
void apxLogClose( HANDLE hFile) { apx_logfile_st *lf = (apx_logfile_st *)hFile; if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) return; FlushFileBuffers(lf->hFile); CloseHandle(lf->hFile); if (lf == _st_sys_loghandle) _st_sys_loghandle = NULL; apxFree(lf); }
static BOOL apx_log_rotate(apx_logfile_st *l, LPSYSTEMTIME lpCtime) { WCHAR sPath[MAX_PATH+1]; /* rotate on daily basis */ if (l->sysTime.wDay == lpCtime->wDay) return TRUE; FlushFileBuffers(l->hFile); CloseHandle(l->hFile); l->sysTime = *lpCtime; wsprintfW(sPath, L"%s\\%s%04d%02d%02d.log", l->szPath, l->szPrefix, l->sysTime.wYear, l->sysTime.wMonth, l->sysTime.wDay); l->hFile = CreateFileW(sPath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL); if (IS_INVALID_HANDLE(l->hFile)) return FALSE; else return TRUE; }
BOOL apxJavaStart(LPAPXJAVA_THREADARGS pArgs) { LPAPXJAVAVM lpJava; lpJava = APXHANDLE_DATA(pArgs->hJava); if (!lpJava) return FALSE; lpJava->dwWorkerStatus = 0; lpJava->hWorkerInit = CreateEvent(NULL, FALSE, FALSE, NULL); lpJava->hWorkerSync = CreateEvent(NULL, FALSE, FALSE, NULL); lpJava->hWorkerThread = CreateThread(NULL, lpJava->szStackSize, __apxJavaWorkerThread, pArgs, CREATE_SUSPENDED, &lpJava->iWorkerThread); if (IS_INVALID_HANDLE(lpJava->hWorkerThread)) { apxLogWrite(APXLOG_MARK_SYSERR); return FALSE; } ResumeThread(lpJava->hWorkerThread); /* Wait until the worker thread initializes */ WaitForSingleObject(lpJava->hWorkerInit, INFINITE); if (lpJava->dwWorkerStatus == 0) return FALSE; SetEvent(lpJava->hWorkerSync); if (lstrcmpA(lpJava->clWorker.sClazz, "java/lang/System")) { /* Give some time to initialize the thread * Unless we are calling System.exit(0). * This will be hanled by _onexit hook. */ Sleep(1000); } return TRUE; }
static BOOL __apxJavaJniCallback(APXHANDLE hObject, UINT uMsg, WPARAM wParam, LPARAM lParam) { LPAPXJAVAVM lpJava; DWORD dwJvmRet = 0; lpJava = APXHANDLE_DATA(hObject); switch (uMsg) { case WM_CLOSE: if (_st_sys_jvm && lpJava->lpJvm) { if (!IS_INVALID_HANDLE(lpJava->hWorkerThread)) { if (GetExitCodeThread(lpJava->hWorkerThread, &dwJvmRet) && dwJvmRet == STILL_ACTIVE) { TerminateThread(lpJava->hWorkerThread, 5); } } SAFE_CLOSE_HANDLE(lpJava->hWorkerThread); __apxJvmAttach(lpJava); JVM_DELETE_CLAZZ(lpJava, clWorker); JVM_DELETE_CLAZZ(lpJava, clString); __apxJvmDetach(lpJava); /* Check if this is the jvm loader */ if (!lpJava->iVmCount && _st_sys_jvmDllHandle) { /* Unload JVM dll */ FreeLibrary(_st_sys_jvmDllHandle); _st_sys_jvmDllHandle = NULL; } lpJava->lpJvm = NULL; } break; default: break; } return TRUE; }
BOOL apxDestroyJvm(DWORD dwTimeout) { if (_st_sys_jvm) { DWORD tid; HANDLE hWaiter; BOOL rv = FALSE; JavaVM *lpJvm = _st_sys_jvm; _st_sys_jvm = NULL; (*lpJvm)->DetachCurrentThread(lpJvm); hWaiter = CreateThread(NULL, 0, __apxJavaDestroyThread, (void *)lpJvm, 0, &tid); if (IS_INVALID_HANDLE(hWaiter)) { apxLogWrite(APXLOG_MARK_SYSERR); return FALSE; } if (WaitForSingleObject(hWaiter, dwTimeout) == WAIT_OBJECT_0) rv = TRUE; CloseHandle(hWaiter); return rv; } else return FALSE; }
DWORD apxMultiSzToArrayA(APXHANDLE hPool, LPCSTR lpString, LPSTR **lppArray) { DWORD i, n, l; char *buff; LPSTR p; l = __apxGetMultiSzLengthA(lpString, &n); if (!n || !l) return 0; if (IS_INVALID_HANDLE(hPool)) buff = apxPoolAlloc(hPool, (n + 2) * sizeof(LPTSTR) + (l + 1)); else buff = apxAlloc((n + 2) * sizeof(LPSTR) + (l + 1) * sizeof(CHAR)); *lppArray = (LPSTR *)buff; p = (LPSTR)(buff + (n + 2) * sizeof(LPSTR)); AplCopyMemory(p, lpString, (l + 1) * sizeof(CHAR)); for (i = 0; i < n; i++) { (*lppArray)[i] = p; while (*p) p++; p++; } (*lppArray)[++i] = NULL; return n; }
void apxLogLevelSetW(HANDLE hFile, LPCWSTR szLevel) { apx_logfile_st *lf = (apx_logfile_st *)hFile; if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) return; if (szLevel) { if (!lstrcmpiW(szLevel, L"error")) lf->dwLogLevel = APXLOG_LEVEL_ERROR; else if (!lstrcmpiW(szLevel, L"warn")) lf->dwLogLevel = APXLOG_LEVEL_WARN; else if (!lstrcmpiW(szLevel, L"info")) lf->dwLogLevel = APXLOG_LEVEL_INFO; else if (!lstrcmpiW(szLevel, L"debug")) lf->dwLogLevel = APXLOG_LEVEL_DEBUG; } }
APXHANDLE apxCreateJava(APXHANDLE hPool, LPCWSTR szJvmDllPath) { APXHANDLE hJava; LPAPXJAVAVM lpJava; jsize iVmCount; JavaVM *lpJvm = NULL; struct APX_JDK1_1InitArgs jArgs1_1; if (!__apxLoadJvmDll(szJvmDllPath)) return NULL; /* */ if (DYNLOAD_FPTR(JNI_GetCreatedJavaVMs)(&lpJvm, 1, &iVmCount) != JNI_OK) { return NULL; } if (iVmCount && !lpJvm) return NULL; hJava = apxHandleCreate(hPool, 0, NULL, sizeof(APXJAVAVM), __apxJavaJniCallback); if (IS_INVALID_HANDLE(hJava)) return NULL; hJava->dwType = APXHANDLE_TYPE_JVM; lpJava = APXHANDLE_DATA(hJava); lpJava->lpJvm = lpJvm; lpJava->iVmCount = iVmCount; /* Guess the stack size */ AplZeroMemory(&jArgs1_1, sizeof(jArgs1_1)); jArgs1_1.version = JNI_VERSION_1_1; DYNLOAD_FPTR(JNI_GetDefaultJavaVMInitArgs)(&jArgs1_1); if (jArgs1_1.javaStackSize < 0 || jArgs1_1.javaStackSize > (2048 * 1024)) jArgs1_1.javaStackSize = 0; lpJava->szStackSize = (SIZE_T)jArgs1_1.javaStackSize; if (!_st_sys_jvm) _st_sys_jvm = lpJvm; return hJava; }
/**************************************************************************** * task_manager_stop ****************************************************************************/ int task_manager_stop(int handle, int timeout) { int status; tm_request_t request_msg; tm_response_t response_msg; if (IS_INVALID_HANDLE(handle) || timeout < TM_RESPONSE_WAIT_INF) { return TM_INVALID_PARAM; } memset(&request_msg, 0, sizeof(tm_request_t)); /* Set the request msg */ request_msg.cmd = TASKMGRCMD_STOP; request_msg.handle = handle; request_msg.caller_pid = getpid(); request_msg.timeout = timeout; if (timeout != TM_NO_RESPONSE) { TM_ASPRINTF(&request_msg.q_name, "%s%d", TM_PRIVATE_MQ, request_msg.caller_pid); if (request_msg.q_name == NULL) { return TM_OUT_OF_MEMORY; } } status = taskmgr_send_request(&request_msg); if (status < 0) { if (request_msg.q_name != NULL) { TM_FREE(request_msg.q_name); } return status; } if (timeout != TM_NO_RESPONSE) { status = taskmgr_receive_response(request_msg.q_name, &response_msg, timeout); TM_FREE(request_msg.q_name); } return status; }
BOOL apxSetEnvironmentVariable(APXHANDLE hPool, LPCTSTR szName, LPCTSTR szValue, BOOL bAppend) { LPTSTR szNew = (LPTSTR)szValue; if (bAppend) { DWORD l = GetEnvironmentVariable(szName, NULL, 0); if (l > 0) { BOOL rv; if (IS_INVALID_HANDLE(hPool)) szNew = apxAlloc(l + lstrlen(szValue) + 3); else szNew = apxPoolAlloc(hPool, l + lstrlen(szValue) + 3); GetEnvironmentVariable(szName, szNew, l + 1); lstrcat(szNew, TEXT(";")); lstrcat(szNew, szValue); rv = SetEnvironmentVariable(szName, szNew); apxFree(szNew); return rv; } } return SetEnvironmentVariable(szName, szNew); }
int apxLogWrite( HANDLE hFile, DWORD dwLevel, BOOL bTimeStamp, LPCSTR szFile, DWORD dwLine, LPCSTR szFormat, ...) { va_list args; CHAR buffer[1024+32]; LPSTR szBp; int len = 0; LPCSTR f = szFile; CHAR sb[MAX_PATH+1]; DWORD wr; DWORD err; BOOL dolock = TRUE; apx_logfile_st *lf = (apx_logfile_st *)hFile; err = GetLastError(); /* save the last Error code */ if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) { lf = &_st_sys_errhandle; lf->hFile = GetStdHandle(STD_ERROR_HANDLE); dolock = FALSE; } if (dwLevel < lf->dwLogLevel) return 0; if (f) { f = (szFile + lstrlenA(szFile) - 1); while(f != szFile && '\\' != *f && '/' != *f) f--; if(f != szFile) f++; } lstrcpyA(buffer, _log_level[dwLevel]); if (!dolock) lstrcatA(buffer, "\n"); szBp = &buffer[lstrlenA(buffer)]; if (!szFormat) { FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), szBp, 1000, NULL); } else { va_start(args, szFormat); wvsprintfA(szBp, szFormat, args); va_end(args); } len = lstrlenA(buffer); if (len > 0) { /* Remove trailing line separator */ if (buffer[len - 1] == '\n') { buffer[len - 1] = '\0'; --len; } if (!IS_INVALID_HANDLE(lf->hFile)) { SYSTEMTIME t; GetLocalTime(&t); if (lf->bRotate) { if (!apx_log_rotate(lf, &t)) return 0; } if (dolock) { APX_LOGLOCK(lf->hFile); } if (bTimeStamp) { wsprintfA(sb, "[%d-%02d-%02d %02d:%02d:%02d] ", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); WriteFile(lf->hFile, sb, lstrlenA(sb), &wr, NULL); } if (f) { wsprintfA(sb, "[%-4d %s] ", dwLine, f); WriteFile(lf->hFile, sb, lstrlenA(sb), &wr, NULL); } WriteFile(lf->hFile, buffer, len, &wr, NULL); /* Terminate the line */ WriteFile(lf->hFile, LINE_SEP, sizeof(LINE_SEP) - 1, &wr, NULL); #ifdef _DEBUG_FULL FlushFileBuffers(lf->hFile); #endif if (dolock) { APX_LOGUNLOCK(lf->hFile); } } #ifdef _DEBUG_FULL { char tid[1024 + 16]; wsprintfA(tid, "[%04X] %s", GetCurrentThreadId(), buffer); OutputDebugStringA(tid); } #endif } /* Restore the last Error code */ SetLastError(err); return len; }
int apxLogWrite( HANDLE hFile, DWORD dwLevel, BOOL bTimeStamp, LPCSTR szFile, DWORD dwLine, LPCSTR szFormat, ...) { va_list args; CHAR buffer[1024+32] = ""; LPSTR szBp; int len = 0; LPCSTR f = szFile; CHAR sb[SIZ_PATHLEN]; DWORD wr; DWORD err; BOOL dolock = TRUE; apx_logfile_st *lf = (apx_logfile_st *)hFile; err = GetLastError(); /* save the last Error code */ if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) { lf = &_st_sys_errhandle; lf->hFile = GetStdHandle(STD_ERROR_HANDLE); dolock = FALSE; } if (dwLevel < lf->dwLogLevel) return 0; if (f && (lf->dwLogLevel == APXLOG_LEVEL_DEBUG || dwLevel == APXLOG_LEVEL_ERROR)) { f = (szFile + lstrlenA(szFile) - 1); while(f != szFile && '\\' != *f && '/' != *f) f--; if(f != szFile) f++; } else f = NULL; szBp = buffer; if (!szFormat) { if (err == 0) { lstrcpyA(szBp, "Unknown error code"); if (dwLevel == APXLOG_LEVEL_ERROR) { szBp += 18; wsprintfA(szBp, " occured in (%s:%d) ", f, dwLine); } } else FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), szBp, 1000, NULL); } else { va_start(args, szFormat); wvsprintfA(szBp, szFormat, args); va_end(args); } len = lstrlenA(buffer); if (len > 0) { /* Remove trailing line separator */ if (buffer[len - 1] == '\n') buffer[--len] = '\0'; if (len > 0 && buffer[len - 1] == '\r') buffer[--len] = '\0'; if (!IS_INVALID_HANDLE(lf->hFile)) { SYSTEMTIME t; GetLocalTime(&t); if (dolock) { APX_LOGLOCK(lf->hFile); logRotate(lf, &t); } if (bTimeStamp) { wsprintfA(sb, "[%d-%02d-%02d %02d:%02d:%02d] ", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); WriteFile(lf->hFile, sb, lstrlenA(sb), &wr, NULL); } WriteFile(lf->hFile, _log_level[dwLevel], lstrlenA(_log_level[dwLevel]), &wr, NULL); if (f && lf->dwLogLevel == APXLOG_LEVEL_DEBUG) { wsprintfA(sb, "(%10s:%-4d) ", f, dwLine); WriteFile(lf->hFile, sb, lstrlenA(sb), &wr, NULL); } if (len) WriteFile(lf->hFile, buffer, len, &wr, NULL); /* Terminate the line */ WriteFile(lf->hFile, LINE_SEP, sizeof(LINE_SEP) - 1, &wr, NULL); #ifdef _DEBUG_FULL FlushFileBuffers(lf->hFile); #endif if (dolock) { APX_LOGUNLOCK(lf->hFile); } } #ifdef _DEBUG_FULL { char tid[1024 + 16]; wsprintfA(tid, "[%04d] %s", GetCurrentThreadId(), buffer); OutputDebugStringA(tid); } #endif } /* Restore the last Error code */ SetLastError(err); if (szFormat && err != 0 && dwLevel == APXLOG_LEVEL_ERROR) { /* Print the System error description */ apxLogWrite(hFile, dwLevel, bTimeStamp, szFile, dwLine, NULL); } return len; }
static BOOL __apxLoadJvmDll(LPCWSTR szJvmDllPath) { UINT errMode; WCHAR jreAltPath[SIZ_PATHLEN]; LPWSTR dllJvmPath = (LPWSTR)szJvmDllPath; DYNLOAD_FPTR_DECLARE(SetDllDirectoryW); if (!IS_INVALID_HANDLE(_st_sys_jvmDllHandle)) return TRUE; /* jvm.dll is already loaded */ if (dllJvmPath && *dllJvmPath) { /* Explicit JVM path. * Check if provided argument is valid */ if (GetFileAttributesW(dllJvmPath) == INVALID_FILE_ATTRIBUTES) { /* DAEMON-247: Invalid RuntimeLib explicitly specified is error. */ apxLogWrite(APXLOG_MARK_DEBUG "Invalid RuntimeLib specified '%S'", dllJvmPath); return FALSE; } } else { dllJvmPath = apxGetJavaSoftRuntimeLib(NULL); if (!dllJvmPath) return FALSE; } if (GetFileAttributesW(dllJvmPath) == INVALID_FILE_ATTRIBUTES) { /* DAEMON-184: RuntimeLib registry key is invalid. * Check from Jre JavaHome directly */ LPWSTR szJreHome = apxGetJavaSoftHome(NULL, TRUE); apxLogWrite(APXLOG_MARK_DEBUG "Invalid RuntimeLib '%S'", dllJvmPath); if (szJreHome) { apxLogWrite(APXLOG_MARK_DEBUG "Using Jre JavaHome '%S'", szJreHome); lstrlcpyW(jreAltPath, SIZ_PATHLEN, szJreHome); lstrlcatW(jreAltPath, SIZ_PATHLEN, L"\\bin\\server\\jvm.dll"); dllJvmPath = jreAltPath; } } /* Suppress the not found system popup message */ errMode = SetErrorMode(SEM_FAILCRITICALERRORS); apxLogWrite(APXLOG_MARK_DEBUG "loading jvm '%S'", dllJvmPath); _st_sys_jvmDllHandle = LoadLibraryExW(dllJvmPath, NULL, 0); if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle) && GetFileAttributesW(dllJvmPath) != INVALID_FILE_ATTRIBUTES) { /* There is a file but cannot be loaded. * Try to load the MSVCRTxx.dll before JVM.dll */ WCHAR jreBinPath[SIZ_PATHLEN]; WCHAR crtBinPath[SIZ_PATHLEN]; DWORD i, l = 0; lstrlcpyW(jreBinPath, SIZ_PATHLEN, dllJvmPath); for (i = lstrlenW(jreBinPath); i > 0, l < 2; i--) { if (jreBinPath[i] == L'\\' || jreBinPath[i] == L'/') { jreBinPath[i] = L'\0'; lstrlcpyW(crtBinPath, SIZ_PATHLEN, jreBinPath); lstrlcatW(crtBinPath, SIZ_PATHLEN, MSVCRT71_DLLNAME); if (GetFileAttributesW(crtBinPath) != INVALID_FILE_ATTRIBUTES) { if (LoadLibraryW(crtBinPath)) { /* Found MSVCRTxx.dll */ apxLogWrite(APXLOG_MARK_DEBUG "preloaded '%S'", crtBinPath); break; } } l++; } } } /* This shuldn't happen, but try to search in %PATH% */ if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle)) _st_sys_jvmDllHandle = LoadLibraryExW(dllJvmPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle)) { WCHAR jreBinPath[SIZ_PATHLEN]; DWORD i, l = 0; lstrlcpyW(jreBinPath, SIZ_PATHLEN, dllJvmPath); DYNLOAD_FPTR_ADDRESS(SetDllDirectoryW, KERNEL32); for (i = lstrlenW(jreBinPath); i > 0, l < 2; i--) { if (jreBinPath[i] == L'\\' || jreBinPath[i] == L'/') { jreBinPath[i] = L'\0'; DYNLOAD_CALL(SetDllDirectoryW)(jreBinPath); apxLogWrite(APXLOG_MARK_DEBUG "Setting DLL search path to '%S'", jreBinPath); l++; } } _st_sys_jvmDllHandle = LoadLibraryExW(dllJvmPath, NULL, 0); if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle)) _st_sys_jvmDllHandle = LoadLibraryExW(dllJvmPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); } /* Restore the error mode signalization */ SetErrorMode(errMode); if (IS_INVALID_HANDLE(_st_sys_jvmDllHandle)) { apxLogWrite(APXLOG_MARK_SYSERR); return FALSE; } DYNLOAD_FPTR_LOAD(JNI_GetDefaultJavaVMInitArgs, _st_sys_jvmDllHandle); DYNLOAD_FPTR_LOAD(JNI_CreateJavaVM, _st_sys_jvmDllHandle); DYNLOAD_FPTR_LOAD(JNI_GetCreatedJavaVMs, _st_sys_jvmDllHandle); DYNLOAD_FPTR_LOAD(JVM_DumpAllStacks, _st_sys_jvmDllHandle); if (!DYNLOAD_FPTR(JNI_GetDefaultJavaVMInitArgs) || !DYNLOAD_FPTR(JNI_CreateJavaVM) || !DYNLOAD_FPTR(JNI_GetCreatedJavaVMs)) { apxLogWrite(APXLOG_MARK_SYSERR); FreeLibrary(_st_sys_jvmDllHandle); _st_sys_jvmDllHandle = NULL; return FALSE; } /* Real voodo ... */ return TRUE; }