2
0

misc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_private.h"
  17. #include "apr_arch_misc.h"
  18. #include "crtdbg.h"
  19. #include "apr_arch_file_io.h"
  20. #include "assert.h"
  21. #include "apr_lib.h"
  22. APR_DECLARE_DATA apr_oslevel_e apr_os_level = APR_WIN_UNK;
  23. apr_status_t apr_get_oslevel(apr_oslevel_e *level)
  24. {
  25. if (apr_os_level == APR_WIN_UNK)
  26. {
  27. static OSVERSIONINFO oslev;
  28. oslev.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  29. GetVersionEx(&oslev);
  30. if (oslev.dwPlatformId == VER_PLATFORM_WIN32_NT)
  31. {
  32. static unsigned int servpack = 0;
  33. char *pservpack;
  34. if (pservpack = oslev.szCSDVersion) {
  35. while (*pservpack && !apr_isdigit(*pservpack)) {
  36. pservpack++;
  37. }
  38. if (*pservpack)
  39. servpack = atoi(pservpack);
  40. }
  41. if (oslev.dwMajorVersion < 3) {
  42. apr_os_level = APR_WIN_UNSUP;
  43. }
  44. else if (oslev.dwMajorVersion == 3) {
  45. if (oslev.dwMajorVersion < 50) {
  46. apr_os_level = APR_WIN_UNSUP;
  47. }
  48. else if (oslev.dwMajorVersion == 50) {
  49. apr_os_level = APR_WIN_NT_3_5;
  50. }
  51. else {
  52. apr_os_level = APR_WIN_NT_3_51;
  53. }
  54. }
  55. else if (oslev.dwMajorVersion == 4) {
  56. if (servpack < 2)
  57. apr_os_level = APR_WIN_NT_4;
  58. else if (servpack <= 2)
  59. apr_os_level = APR_WIN_NT_4_SP2;
  60. else if (servpack <= 3)
  61. apr_os_level = APR_WIN_NT_4_SP3;
  62. else if (servpack <= 4)
  63. apr_os_level = APR_WIN_NT_4_SP4;
  64. else if (servpack <= 5)
  65. apr_os_level = APR_WIN_NT_4_SP5;
  66. else
  67. apr_os_level = APR_WIN_NT_4_SP6;
  68. }
  69. else if (oslev.dwMajorVersion == 5) {
  70. if (oslev.dwMinorVersion == 0) {
  71. if (servpack == 0)
  72. apr_os_level = APR_WIN_2000;
  73. else if (servpack == 1)
  74. apr_os_level = APR_WIN_2000_SP1;
  75. else
  76. apr_os_level = APR_WIN_2000_SP2;
  77. }
  78. else if (oslev.dwMinorVersion == 2) {
  79. apr_os_level = APR_WIN_2003;
  80. }
  81. else {
  82. if (servpack < 1)
  83. apr_os_level = APR_WIN_XP;
  84. else if (servpack == 1)
  85. apr_os_level = APR_WIN_XP_SP1;
  86. else
  87. apr_os_level = APR_WIN_XP_SP2;
  88. }
  89. }
  90. else {
  91. apr_os_level = APR_WIN_XP;
  92. }
  93. }
  94. #ifndef WINNT
  95. else if (oslev.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
  96. char *prevision;
  97. if (prevision = oslev.szCSDVersion) {
  98. while (*prevision && !apr_isupper(*prevision)) {
  99. prevision++;
  100. }
  101. }
  102. else prevision = "";
  103. if (oslev.dwMinorVersion < 10) {
  104. if (*prevision < 'C')
  105. apr_os_level = APR_WIN_95;
  106. else
  107. apr_os_level = APR_WIN_95_OSR2;
  108. }
  109. else if (oslev.dwMinorVersion < 90) {
  110. if (*prevision < 'A')
  111. apr_os_level = APR_WIN_98;
  112. else
  113. apr_os_level = APR_WIN_98_SE;
  114. }
  115. else {
  116. apr_os_level = APR_WIN_ME;
  117. }
  118. }
  119. #endif
  120. #ifdef _WIN32_WCE
  121. else if (oslev.dwPlatformId == VER_PLATFORM_WIN32_CE)
  122. {
  123. if (oslev.dwMajorVersion < 3) {
  124. apr_os_level = APR_WIN_UNSUP;
  125. }
  126. else {
  127. apr_os_level = APR_WIN_CE_3;
  128. }
  129. }
  130. #endif
  131. else {
  132. apr_os_level = APR_WIN_UNSUP;
  133. }
  134. }
  135. *level = apr_os_level;
  136. if (apr_os_level < APR_WIN_UNSUP) {
  137. return APR_EGENERAL;
  138. }
  139. return APR_SUCCESS;
  140. }
  141. /* This is the helper code to resolve late bound entry points
  142. * missing from one or more releases of the Win32 API
  143. */
  144. static const char* const lateDllName[DLL_defined] = {
  145. "kernel32", "advapi32", "mswsock", "ws2_32", "shell32", "ntdll.dll" };
  146. static HMODULE lateDllHandle[DLL_defined] = {
  147. NULL, NULL, NULL, NULL, NULL, NULL };
  148. FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char* fnName, int ordinal)
  149. {
  150. if (!lateDllHandle[fnLib]) {
  151. lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
  152. if (!lateDllHandle[fnLib])
  153. return NULL;
  154. }
  155. if (ordinal)
  156. return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
  157. else
  158. return GetProcAddress(lateDllHandle[fnLib], fnName);
  159. }
  160. /* Declared in include/arch/win32/apr_dbg_win32_handles.h
  161. */
  162. APR_DECLARE_NONSTD(HANDLE) apr_dbg_log(char* fn, HANDLE ha, char* fl, int ln,
  163. int nh, /* HANDLE hv, char *dsc */...)
  164. {
  165. static DWORD tlsid = 0xFFFFFFFF;
  166. static HANDLE fh = NULL;
  167. static long ctr = 0;
  168. static CRITICAL_SECTION cs;
  169. long seq;
  170. DWORD wrote;
  171. char *sbuf;
  172. seq = (InterlockedIncrement)(&ctr);
  173. if (tlsid == 0xFFFFFFFF) {
  174. tlsid = (TlsAlloc)();
  175. }
  176. sbuf = (TlsGetValue)(tlsid);
  177. if (!fh || !sbuf) {
  178. sbuf = (malloc)(1024);
  179. (TlsSetValue)(tlsid, sbuf);
  180. sbuf[1023] = '\0';
  181. if (!fh) {
  182. (GetModuleFileName)(NULL, sbuf, 250);
  183. sprintf(strchr(sbuf, '\0'), ".%d",
  184. (GetCurrentProcessId)());
  185. fh = (CreateFile)(sbuf, GENERIC_WRITE, 0, NULL,
  186. CREATE_ALWAYS, 0, NULL);
  187. (InitializeCriticalSection)(&cs);
  188. }
  189. }
  190. if (!nh) {
  191. (sprintf)(sbuf, "%08x %08x %08x %s() %s:%d\n",
  192. (DWORD)ha, seq, GetCurrentThreadId(), fn, fl, ln);
  193. (EnterCriticalSection)(&cs);
  194. (WriteFile)(fh, sbuf, (DWORD)strlen(sbuf), &wrote, NULL);
  195. (LeaveCriticalSection)(&cs);
  196. }
  197. else {
  198. va_list a;
  199. va_start(a,nh);
  200. (EnterCriticalSection)(&cs);
  201. do {
  202. HANDLE *hv = va_arg(a, HANDLE*);
  203. char *dsc = va_arg(a, char*);
  204. if (strcmp(dsc, "Signaled") == 0) {
  205. if ((DWORD)ha >= STATUS_WAIT_0
  206. && (DWORD)ha < STATUS_ABANDONED_WAIT_0) {
  207. hv += (DWORD)ha;
  208. }
  209. else if ((DWORD)ha >= STATUS_ABANDONED_WAIT_0
  210. && (DWORD)ha < STATUS_USER_APC) {
  211. hv += (DWORD)ha - STATUS_ABANDONED_WAIT_0;
  212. dsc = "Abandoned";
  213. }
  214. else if ((DWORD)ha == WAIT_TIMEOUT) {
  215. dsc = "Timed Out";
  216. }
  217. }
  218. (sprintf)(sbuf, "%08x %08x %08x %s(%s) %s:%d\n",
  219. (DWORD*)*hv, seq, GetCurrentThreadId(),
  220. fn, dsc, fl, ln);
  221. (WriteFile)(fh, sbuf, (DWORD)strlen(sbuf), &wrote, NULL);
  222. } while (--nh);
  223. (LeaveCriticalSection)(&cs);
  224. va_end(a);
  225. }
  226. return ha;
  227. }