start.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_general.h"
  18. #include "apr_pools.h"
  19. #include "apr_signal.h"
  20. #include "ShellAPI.h"
  21. #include "apr_arch_misc.h" /* for WSAHighByte / WSALowByte */
  22. #include "wchar.h"
  23. #include "apr_arch_file_io.h"
  24. #include "crtdbg.h"
  25. #include "assert.h"
  26. /* This symbol is _private_, although it must be exported.
  27. */
  28. int APR_DECLARE_DATA apr_app_init_complete = 0;
  29. /* Used by apr_app_initialize to reprocess the environment
  30. *
  31. * An internal apr function to convert a double-null terminated set
  32. * of single-null terminated strings from wide Unicode to narrow utf-8
  33. * as a list of strings. These are allocated from the MSVCRT's
  34. * _CRT_BLOCK to trick the system into trusting our store.
  35. */
  36. static int warrsztoastr(const char * const * *retarr,
  37. const wchar_t * arrsz, int args)
  38. {
  39. const apr_wchar_t *wch;
  40. apr_size_t totlen;
  41. apr_size_t newlen;
  42. apr_size_t wsize;
  43. char **newarr;
  44. int arg;
  45. if (args < 0) {
  46. for (args = 1, wch = arrsz; wch[0] || wch[1]; ++wch)
  47. if (!*wch)
  48. ++args;
  49. }
  50. wsize = 1 + wch - arrsz;
  51. newarr = _malloc_dbg((args + 1) * sizeof(char *),
  52. _CRT_BLOCK, __FILE__, __LINE__);
  53. /* This is a safe max allocation, we will realloc after
  54. * processing and return the excess to the free store.
  55. * 3 ucs bytes hold any single wchar_t value (16 bits)
  56. * 4 ucs bytes will hold a wchar_t pair value (20 bits)
  57. */
  58. newlen = totlen = wsize * 3 + 1;
  59. newarr[0] = _malloc_dbg(newlen * sizeof(char),
  60. _CRT_BLOCK, __FILE__, __LINE__);
  61. (void)apr_conv_ucs2_to_utf8(arrsz, &wsize,
  62. newarr[0], &newlen);
  63. assert(newlen && !wsize);
  64. /* Return to the free store if the heap realloc is the least bit optimized
  65. */
  66. newarr[0] = _realloc_dbg(newarr[0], totlen - newlen,
  67. _CRT_BLOCK, __FILE__, __LINE__);
  68. for (arg = 1; arg < args; ++arg) {
  69. newarr[arg] = newarr[arg - 1] + 2;
  70. while (*(newarr[arg]++)) {
  71. /* continue */;
  72. }
  73. }
  74. newarr[arg] = NULL;
  75. *retarr = newarr;
  76. return args;
  77. }
  78. /* Reprocess the arguments to main() for a completely apr-ized application
  79. */
  80. APR_DECLARE(apr_status_t) apr_app_initialize(int *argc,
  81. const char * const * *argv,
  82. const char * const * *env)
  83. {
  84. apr_status_t rv = apr_initialize();
  85. if (rv != APR_SUCCESS) {
  86. return rv;
  87. }
  88. #if APR_HAS_UNICODE_FS
  89. IF_WIN_OS_IS_UNICODE
  90. {
  91. apr_wchar_t **wstrs;
  92. apr_wchar_t *sysstr;
  93. int wstrc;
  94. int dupenv;
  95. if (apr_app_init_complete) {
  96. return rv;
  97. }
  98. apr_app_init_complete = 1;
  99. sysstr = GetCommandLineW();
  100. if (sysstr) {
  101. wstrs = CommandLineToArgvW(sysstr, &wstrc);
  102. if (wstrs) {
  103. *argc = apr_wastrtoastr(argv, wstrs, wstrc);
  104. GlobalFree(wstrs);
  105. }
  106. }
  107. sysstr = GetEnvironmentStringsW();
  108. dupenv = warrsztoastr(&_environ, sysstr, -1);
  109. if (env) {
  110. *env = _malloc_dbg((dupenv + 1) * sizeof (char *),
  111. _CRT_BLOCK, __FILE__, __LINE__ );
  112. memcpy((void*)*env, _environ, (dupenv + 1) * sizeof (char *));
  113. }
  114. else {
  115. }
  116. FreeEnvironmentStringsW(sysstr);
  117. /* MSVCRT will attempt to maintain the wide environment calls
  118. * on _putenv(), which is bogus if we've passed a non-ascii
  119. * string to _putenv(), since they use MultiByteToWideChar
  120. * and breaking the implicit utf-8 assumption we've built.
  121. *
  122. * Reset _wenviron for good measure.
  123. */
  124. if (_wenviron) {
  125. apr_wchar_t **wenv = _wenviron;
  126. _wenviron = NULL;
  127. free(wenv);
  128. }
  129. }
  130. #endif
  131. return rv;
  132. }
  133. static int initialized = 0;
  134. /* Provide to win32/thread.c */
  135. extern DWORD tls_apr_thread;
  136. APR_DECLARE(apr_status_t) apr_initialize(void)
  137. {
  138. apr_pool_t *pool;
  139. apr_status_t status;
  140. int iVersionRequested;
  141. WSADATA wsaData;
  142. int err;
  143. apr_oslevel_e osver;
  144. if (initialized++) {
  145. return APR_SUCCESS;
  146. }
  147. /* Initialize apr_os_level global */
  148. if (apr_get_oslevel(&osver) != APR_SUCCESS) {
  149. return APR_EEXIST;
  150. }
  151. tls_apr_thread = TlsAlloc();
  152. if ((status = apr_pool_initialize()) != APR_SUCCESS)
  153. return status;
  154. if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
  155. return APR_ENOPOOL;
  156. }
  157. apr_pool_tag(pool, "apr_initialize");
  158. iVersionRequested = MAKEWORD(WSAHighByte, WSALowByte);
  159. err = WSAStartup((WORD) iVersionRequested, &wsaData);
  160. if (err) {
  161. return err;
  162. }
  163. if (LOBYTE(wsaData.wVersion) != WSAHighByte ||
  164. HIBYTE(wsaData.wVersion) != WSALowByte) {
  165. WSACleanup();
  166. return APR_EEXIST;
  167. }
  168. apr_signal_init(pool);
  169. return APR_SUCCESS;
  170. }
  171. APR_DECLARE_NONSTD(void) apr_terminate(void)
  172. {
  173. initialized--;
  174. if (initialized) {
  175. return;
  176. }
  177. apr_pool_terminate();
  178. WSACleanup();
  179. TlsFree(tls_apr_thread);
  180. }
  181. APR_DECLARE(void) apr_terminate2(void)
  182. {
  183. apr_terminate();
  184. }