/* Open the log file * TODO: format like standard apache error.log * Add the EventLogger */ HANDLE apxLogOpen( APXHANDLE hPool, LPCWSTR szPath, LPCWSTR szPrefix) { WCHAR sPath[MAX_PATH+1]; WCHAR sName[MAX_PATH+1]; SYSTEMTIME sysTime; apx_logfile_st *h; GetLocalTime(&sysTime); if (!szPath) { if (GetSystemDirectoryW(sPath, MAX_PATH) == 0) return INVALID_HANDLE_VALUE; lstrcatW(sPath, L"\\LogFiles\\"); if (!szPrefix) lstrcatW(sPath, L"Apache"); else lstrcatW(sPath, szPrefix); wsprintfW(sName, L"\\%04d%02d%02d.log", sysTime.wYear, sysTime.wMonth, sysTime.wDay); } else { lstrcpyW(sPath, szPath); if (szPrefix) wsprintfW(sName, L"\\%s", szPrefix); else wsprintfW(sName, L"\\jakarta_service_%04d%02d%02d.log", sysTime.wYear, sysTime.wMonth, sysTime.wDay); } if (!(h = (apx_logfile_st *)apxPoolCalloc(hPool, sizeof(apx_logfile_st)))) return NULL; /* Set default level to info */ h->dwLogLevel = APXLOG_LEVEL_INFO; CreateDirectoryW(sPath, NULL); h->sysTime = sysTime; lstrcpyW(h->szPath, sPath); lstrcatW(sPath, sName); if (szPrefix) lstrcpyW(h->szPrefix, szPrefix); h->hFile = CreateFileW(sPath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL); /* Set this file as system log file */ if (!_st_sys_loghandle) _st_sys_loghandle = h; return (HANDLE)h; }
/* * Environment variables parsing * Each variable is prfixed with PR_ * for example 'set PR_JVM=auto' has a same meaning as providing '--Jvm auto' * on the command line. * Multistring varisables are added to the present conf. */ void apxCmdlineLoadEnvVars( LPAPXCMDLINE lpCmdline) { WCHAR szEnv[64]; int i = 0; if (!lpCmdline || !lpCmdline->lpOptions) return; while (lpCmdline->lpOptions[i].szName) { DWORD l; WCHAR szVar[SIZ_HUGLEN]; lstrlcpyW(szEnv, 64, L"PR_"); lstrlcatW(szEnv, 64, lpCmdline->lpOptions[i].szName); l = GetEnvironmentVariableW(szEnv, szVar, SIZ_HUGMAX); if (l == 0 || l >= SIZ_HUGMAX) { if (l == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) { apxLogWrite(APXLOG_MARK_ERROR "Error geting environment variable %S", szEnv); return; } ++i; continue; } if (lpCmdline->lpOptions[i].dwType & APXCMDOPT_STR) { lpCmdline->lpOptions[i].szValue = apxPoolStrdupW(lpCmdline->hPool, szVar); lpCmdline->lpOptions[i].dwType |= APXCMDOPT_FOUND; } else if (lpCmdline->lpOptions[i].dwType & APXCMDOPT_INT) { lpCmdline->lpOptions[i].dwValue = (DWORD)apxAtoulW(szVar); lpCmdline->lpOptions[i].dwType |= APXCMDOPT_FOUND; } else if (lpCmdline->lpOptions[i].dwType & APXCMDOPT_MSZ) { LPWSTR pp; BOOL insquote = FALSE, indquote = FALSE; DWORD sp = 0; lpCmdline->lpOptions[i].dwValue = (lstrlenW(szVar) + 2) * sizeof(WCHAR); lpCmdline->lpOptions[i].szValue = apxPoolCalloc(lpCmdline->hPool, lpCmdline->lpOptions[i].dwValue); pp = szVar; while(*pp) { if (*pp == L'\'') insquote = !insquote; else if (*pp == L'"') { indquote = !indquote; lpCmdline->lpOptions[i].szValue[sp++] = L'"'; } else if ((*pp == L'#' || *pp == L';') && !insquote && !indquote) lpCmdline->lpOptions[i].szValue[sp++] = L'\0'; else lpCmdline->lpOptions[i].szValue[sp++] = *pp; pp++; } lpCmdline->lpOptions[i].dwType |= APXCMDOPT_FOUND | APXCMDOPT_ADD; } ++i; } }
LPWSTR apxCRLFToMszW(APXHANDLE hPool, LPCWSTR szStr, LPDWORD lpdwBytes) { DWORD l, c, n = 0; LPWSTR rv, b; l = lstrlenW(szStr); b = rv = apxPoolCalloc(hPool, (l + 2) * sizeof(WCHAR)); for (c = 0; c < l; c++) { if (szStr[c] == L'\r') { *b++ = '\0'; n++; } else if (szStr[c] != L'\n') { *b++ = szStr[c]; n++; } } if (lpdwBytes) *lpdwBytes = (n + 2) * sizeof(WCHAR); return rv; }
LPWSTR apxMszToCRLFW(APXHANDLE hPool, LPCWSTR szStr) { DWORD l, c; LPWSTR rv, b; LPCWSTR p = szStr; l = __apxGetMultiSzLengthW(szStr, &c); b = rv = apxPoolCalloc(hPool, (l + c + 2) * sizeof(WCHAR)); while (c > 0) { if (*p) *b++ = *p; else { *b++ = L'\r'; *b++ = L'\n'; c--; } p++; } return rv; }
LPWSTR apxMultiSzCombine(APXHANDLE hPool, LPCWSTR lpStrA, LPCWSTR lpStrB, LPDWORD lpdwLength) { LPWSTR rv; DWORD la = 0, lb = 0; if (!lpStrA && !lpStrB) return NULL; /* Nothing to do if both are NULL */ la = __apxGetMultiSzLengthW(lpStrA, NULL); lb = __apxGetMultiSzLengthW(lpStrB, NULL); rv = apxPoolCalloc(hPool, (la + lb + 1) * sizeof(WCHAR)); if (la) { AplMoveMemory(rv, lpStrA, la * sizeof(WCHAR)); } if (lb) { AplMoveMemory(&rv[la], lpStrB, lb * sizeof(WCHAR)); } if (lpdwLength) *lpdwLength = (la + lb + 1) * sizeof(WCHAR); return rv; }
/* * argv parsing. * Parse the argv[0] and split to ExePath and * Executable name. Strip the extension ('.exe'). * Check for command in argv[1] //CMD//Application * Parse the options --option value or --option==value * break on first argument that doesn't start with '--' */ LPAPXCMDLINE apxCmdlineParse( APXHANDLE hPool, APXCMDLINEOPT *lpOptions, LPCWSTR *lpszCommands, LPCWSTR *lpszAltcmds) { LPAPXCMDLINE lpCmdline = NULL; DWORD l, i, s = 1; LPWSTR p; DWORD match; WCHAR mh[SIZ_HUGLEN]; if (_st_sys_argc < 1) return NULL; if (!(lpCmdline = (LPAPXCMDLINE)apxPoolCalloc(hPool, sizeof(APXCMDLINE)))) return NULL; lpCmdline->hPool = hPool; lpCmdline->lpOptions = lpOptions; if (GetModuleFileNameW(GetModuleHandle(NULL), mh, SIZ_HUGLEN)) { GetLongPathNameW(mh, mh, SIZ_HUGLEN); lpCmdline->szExePath = apxPoolStrdupW(hPool, mh); lpCmdline->szArgv0 = apxPoolStrdupW(hPool, mh); if (lpCmdline->szExePath == NULL || lpCmdline->szArgv0 == NULL) return NULL; if ((p = wcsrchr(lpCmdline->szExePath, L'\\'))) *p++ = L'\0'; else return NULL; } else return NULL; lpCmdline->szExecutable = p; p = wcsrchr(lpCmdline->szExecutable, L'.'); if (p && lstrcmpiW(p, EXE_SUFFIX) == 0) *p = L'\0'; if ((p = wcsrchr(lpCmdline->szExecutable, L'.'))) { if (lstrcmpiW(p, X86_SUFFIX) == 0) { *p = L'\0'; } else if (lstrcmpiW(p, X64_SUFFIX) == 0) { *p = L'\0'; } else if (lstrcmpiW(p, A64_SUFFIX) == 0) { *p = L'\0'; } } if (_st_sys_argc > 1 && lstrlenW(_st_sys_argvw[1]) > 2) { LPWSTR cp = _st_sys_argvw[1]; LPWSTR cn = _st_sys_argc > 2 ? _st_sys_argvw[2] : NULL; LPWSTR ca = cp; i = 0; if (ca[0] == L'/' && ca[1] == L'/') { ca += 2; if ((cn = wcschr(ca, L'/'))) { *cn++ = L'\0'; while (*cn == L'/') cn++; if (*cn == L'\0') cn = NULL; } if (cn == NULL) cn = lpCmdline->szExecutable; while (lpszCommands[i]) { if (lstrcmpW(lpszCommands[i++], ca) == 0) { lpCmdline->dwCmdIndex = i; break; } } if (lpCmdline->dwCmdIndex) { lpCmdline->szApplication = cn; s = 2; } else { apxLogWrite(APXLOG_MARK_ERROR "Unrecognized cmd option %S", cp); return NULL; } } else { while (lpszAltcmds[i]) { if (lstrcmpW(lpszAltcmds[i++], ca) == 0) { lpCmdline->dwCmdIndex = i; break; } } if (lpCmdline->dwCmdIndex) { s = 2; if (cn && iswalnum(*cn)) { s++; lpCmdline->szApplication = cn; } else lpCmdline->szApplication = lpCmdline->szExecutable; } else { apxLogWrite(APXLOG_MARK_ERROR "Unrecognized cmd option %S", cp); return NULL; } } } else { lpCmdline->szApplication = lpCmdline->szExecutable; lpCmdline->dwCmdIndex = 1; return lpCmdline; } for (i = s; i < (DWORD)_st_sys_argc; i++) { LPWSTR e = NULL; LPWSTR a = _st_sys_argvw[i]; BOOL add = FALSE; if (a[0] == L'+' && a[1] == L'+') add = TRUE; else if (a[0] != L'-' || a[1] != L'-') break; p = a + 2; /* Find if the option has '=' char * for --option==value or --option value cases. */ while (*p) { if (*p == L'=') { *p = L'\0'; e = p + 1; break; } else p++; } match = 0; for (l = 0; lpOptions[l].szName; l++) { if (lstrcmpW(lpOptions[l].szName, a + 2) == 0) { LPWSTR val; /* check if arg is needed */ if (e) val = e; else if ((i + 1) < (DWORD)_st_sys_argc) val = _st_sys_argvw[++i]; else { lpOptions[l].dwValue = 0; lpOptions[l].szValue = NULL; lpOptions[l].dwType |= APXCMDOPT_FOUND; break; } if (add) { if (!(lpOptions[l].dwType & APXCMDOPT_FOUND)) { /* Only set add flag in case there was no --option */ lpOptions[l].dwType |= APXCMDOPT_ADD; } } else if (lpOptions[l].dwType & APXCMDOPT_ADD) { /* We have ++option --option ... * Discard earlier values and go over. */ lpOptions[l].dwType &= ~APXCMDOPT_ADD; lpOptions[l].dwValue = 0; lpOptions[l].szValue = 0; } if (lpOptions[l].dwType & APXCMDOPT_STR) lpOptions[l].szValue = val; else if (lpOptions[l].dwType & APXCMDOPT_INT) lpOptions[l].dwValue = (DWORD)apxAtoulW(val); else if (lpOptions[l].dwType & APXCMDOPT_MSZ) { LPWSTR pp; BOOL insquote = FALSE, indquote=FALSE; DWORD sp = 0; LPWSTR ov = lpOptions[l].szValue; if (lpOptions[l].dwValue > 2) { sp = (lpOptions[l].dwValue - sizeof(WCHAR)) / sizeof(WCHAR); } lpOptions[l].dwValue = (sp + lstrlenW(val) + 2) * sizeof(WCHAR); lpOptions[l].szValue = (LPWSTR)apxPoolCalloc(hPool, lpOptions[l].dwValue); if (sp) { AplMoveMemory(lpOptions[l].szValue, ov, sp * sizeof(WCHAR)); apxFree(ov); } pp = val; while(*pp) { if (*pp == L'\'') insquote = !insquote; else if (*pp == L'"') { indquote = !indquote; lpOptions[l].szValue[sp++] = L'"'; } else if ((*pp == L'#' || *pp == L';') && !insquote && !indquote) lpOptions[l].szValue[sp++] = L'\0'; else lpOptions[l].szValue[sp++] = *pp; pp++; } } lpOptions[l].dwType |= APXCMDOPT_FOUND; match = l + 1; break; } } if (match == 0) { /* --unknown option * */ apxLogWrite(APXLOG_MARK_ERROR "Unrecognized program option %S", _st_sys_argvw[i]); return NULL; } } if (i < (DWORD)_st_sys_argc) { lpCmdline->dwArgc = _st_sys_argc - i; lpCmdline->lpArgvw = &_st_sys_argvw[i]; } return lpCmdline; }
/* Open the log file * TODO: format like standard apache error.log * Add the EventLogger */ HANDLE apxLogOpen( APXHANDLE hPool, LPCWSTR szPath, LPCWSTR szPrefix, DWORD dwRotate) { WCHAR sPath[SIZ_PATHLEN]; WCHAR sName[SIZ_PATHLEN]; SYSTEMTIME sysTime; apx_logfile_st *h; GetLocalTime(&sysTime); if (!szPath) { if (GetSystemDirectoryW(sPath, MAX_PATH) == 0) return INVALID_HANDLE_VALUE; lstrlcatW(sPath, MAX_PATH, L"\\LogFiles"); if (!CreateDirectoryW(sPath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) { if (!CreateDirectoryW(sPath, NULL)) return INVALID_HANDLE_VALUE; } lstrlcatW(sPath, MAX_PATH, L"\\Apache"); if (!CreateDirectoryW(sPath, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) { if (!CreateDirectoryW(sPath, NULL)) return INVALID_HANDLE_VALUE; } } else { lstrlcpyW(sPath, MAX_PATH, szPath); } if (!szPrefix) szPrefix = L"commons-daemon"; if (dwRotate != 0 && dwRotate < 86400) wsprintfW(sName, L"\\%s" LOGF_EXR, szPrefix, sysTime.wYear, sysTime.wMonth, sysTime.wDay, 0, 0, 0); else wsprintfW(sName, L"\\%s" LOGF_EXT, szPrefix, sysTime.wYear, sysTime.wMonth, sysTime.wDay); if (!(h = (apx_logfile_st *)apxPoolCalloc(hPool, sizeof(apx_logfile_st)))) return INVALID_HANDLE_VALUE; /* Set default level to info */ h->dwLogLevel = APXLOG_LEVEL_INFO; CreateDirectoryW(sPath, NULL); h->sysTime = sysTime; lstrlcpyW(h->szPath, MAX_PATH, sPath); lstrlcpyW(h->szFile, MAX_PATH, sPath); lstrlcatW(h->szFile, MAX_PATH, sName); lstrlcpyW(h->szPrefix, MAX_PATH, szPrefix); h->hFile = CreateFileW(h->szFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (h->hFile == INVALID_HANDLE_VALUE) { /* Make sure we write somewhere */ h = &_st_sys_errhandle; apxDisplayError(FALSE, NULL, 0, "Unable to create logger at '%S'\n", h->szFile); return (HANDLE)h; } else { h->dwRotate = dwRotate; } /* Set this file as system log file */ if (!_st_sys_loghandle) _st_sys_loghandle = h; return (HANDLE)h; }