start.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "fspr.h"
  17. #include "fspr_general.h"
  18. #include "fspr_pools.h"
  19. #include "fspr_signal.h"
  20. #include "fspr_arch_misc.h" /* for WSAHighByte / WSALowByte */
  21. #include "fspr_arch_proc_mutex.h" /* for fspr_proc_mutex_unix_setup_lock() */
  22. #include "fspr_arch_internal_time.h"
  23. #ifdef USE_WINSOCK
  24. /*
  25. ** Resource tag signatures for using NetWare WinSock 2. These will no longer
  26. ** be needed by anyone once the new WSAStartupWithNlmHandle() is available
  27. ** since WinSock will make the calls to AllocateResourceTag().
  28. */
  29. #define WS_LOAD_ENTRY_SIGNATURE (*(unsigned long *) "WLDE")
  30. #define WS_SKT_SIGNATURE (*(unsigned long *) "WSKT")
  31. #define WS_LOOKUP_SERVICE_SIGNATURE (*(unsigned long *) "WLUP")
  32. #define WS_WSAEVENT_SIGNATURE (*(unsigned long *) "WEVT")
  33. #define WS_CPORT_SIGNATURE (*(unsigned long *) "WCPT")
  34. int (*WSAStartupWithNLMHandle)( WORD version, LPWSADATA data, void *handle ) = NULL;
  35. int (*WSACleanupWithNLMHandle)( void *handle ) = NULL;
  36. static int wsa_startup_with_handle (WORD wVersionRequested, LPWSADATA data, void *handle)
  37. {
  38. APP_DATA *app_data;
  39. if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
  40. return APR_EGENERAL;
  41. app_data->gs_startup_rtag = AllocateResourceTag(handle, "WinSock Start-up", WS_LOAD_ENTRY_SIGNATURE);
  42. app_data->gs_socket_rtag = AllocateResourceTag(handle, "WinSock socket()", WS_SKT_SIGNATURE);
  43. app_data->gs_lookup_rtag = AllocateResourceTag(handle, "WinSock Look-up", WS_LOOKUP_SERVICE_SIGNATURE);
  44. app_data->gs_event_rtag = AllocateResourceTag(handle, "WinSock Event", WS_WSAEVENT_SIGNATURE);
  45. app_data->gs_pcp_rtag = AllocateResourceTag(handle, "WinSock C-Port", WS_CPORT_SIGNATURE);
  46. return WSAStartupRTags(wVersionRequested, data,
  47. app_data->gs_startup_rtag,
  48. app_data->gs_socket_rtag,
  49. app_data->gs_lookup_rtag,
  50. app_data->gs_event_rtag,
  51. app_data->gs_pcp_rtag);
  52. }
  53. static int wsa_cleanup_with_handle (void *handle)
  54. {
  55. APP_DATA *app_data;
  56. if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
  57. return APR_EGENERAL;
  58. return WSACleanupRTag(app_data->gs_startup_rtag);
  59. }
  60. static int UnregisterAppWithWinSock (void *nlm_handle)
  61. {
  62. if (!WSACleanupWithNLMHandle)
  63. {
  64. if (!(WSACleanupWithNLMHandle = ImportPublicObject(gLibHandle, "WSACleanupWithNLMHandle")))
  65. WSACleanupWithNLMHandle = wsa_cleanup_with_handle;
  66. }
  67. return (*WSACleanupWithNLMHandle)(nlm_handle);
  68. }
  69. static int RegisterAppWithWinSock (void *nlm_handle)
  70. {
  71. int err;
  72. WSADATA wsaData;
  73. WORD wVersionRequested = MAKEWORD(WSAHighByte, WSALowByte);
  74. if (!WSAStartupWithNLMHandle)
  75. {
  76. if (!(WSAStartupWithNLMHandle = ImportPublicObject(gLibHandle, "WSAStartupWithNLMHandle")))
  77. WSAStartupWithNLMHandle = wsa_startup_with_handle;
  78. }
  79. err = (*WSAStartupWithNLMHandle)(wVersionRequested, &wsaData, nlm_handle);
  80. if (LOBYTE(wsaData.wVersion) != WSAHighByte ||
  81. HIBYTE(wsaData.wVersion) != WSALowByte) {
  82. UnregisterAppWithWinSock (nlm_handle);
  83. return APR_EEXIST;
  84. }
  85. return err;
  86. }
  87. #endif
  88. APR_DECLARE(fspr_status_t) fspr_app_initialize(int *argc,
  89. const char * const * *argv,
  90. const char * const * *env)
  91. {
  92. /* An absolute noop. At present, only Win32 requires this stub, but it's
  93. * required in order to move command arguments passed through the service
  94. * control manager into the process, and it's required to fix the char*
  95. * data passed in from win32 unicode into utf-8, win32's apr internal fmt.
  96. */
  97. return fspr_initialize();
  98. }
  99. APR_DECLARE(fspr_status_t) fspr_initialize(void)
  100. {
  101. fspr_pool_t *pool;
  102. int err;
  103. void *nlmhandle = getnlmhandle();
  104. /* Register the NLM as using APR. If it is already
  105. registered then just return. */
  106. if (register_NLM(nlmhandle) != 0) {
  107. return APR_SUCCESS;
  108. }
  109. /* fspr_pool_initialize() is being called from the library
  110. startup code since all of the memory resources belong
  111. to the library rather than the application. */
  112. if (fspr_pool_create(&pool, NULL) != APR_SUCCESS) {
  113. return APR_ENOPOOL;
  114. }
  115. fspr_pool_tag(pool, "fspr_initilialize");
  116. #ifdef USE_WINSOCK
  117. err = RegisterAppWithWinSock (nlmhandle);
  118. if (err) {
  119. return err;
  120. }
  121. #endif
  122. fspr_signal_init(pool);
  123. return APR_SUCCESS;
  124. }
  125. APR_DECLARE_NONSTD(void) fspr_terminate(void)
  126. {
  127. APP_DATA *app_data;
  128. /* Get our instance data for shutting down. */
  129. if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
  130. return;
  131. /* Unregister the NLM. If it is not registered
  132. then just return. */
  133. if (unregister_NLM(app_data->gs_nlmhandle) != 0) {
  134. return;
  135. }
  136. /* fspr_pool_terminate() is being called from the
  137. library shutdown code since the memory resources
  138. belong to the library rather than the application */
  139. /* Just clean up the memory for the app that is going
  140. away. */
  141. netware_pool_proc_cleanup ();
  142. #ifdef USE_WINSOCK
  143. UnregisterAppWithWinSock (app_data->gs_nlmhandle);
  144. #endif
  145. }
  146. APR_DECLARE(void) fspr_terminate2(void)
  147. {
  148. fspr_terminate();
  149. }