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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "apr_general.h"
19 #include "apr_pools.h"
20 #include "apr_signal.h"
22 #include "apr_arch_misc.h" /* for WSAHighByte / WSALowByte */
23 #include "apr_arch_proc_mutex.h" /* for apr_proc_mutex_unix_setup_lock() */
24 #include "apr_arch_internal_time.h"
27 ** Resource tag signatures for using NetWare WinSock 2. These will no longer
28 ** be needed by anyone once the new WSAStartupWithNlmHandle() is available
29 ** since WinSock will make the calls to AllocateResourceTag().
31 #define WS_LOAD_ENTRY_SIGNATURE (*(unsigned long *) "WLDE")
32 #define WS_SKT_SIGNATURE (*(unsigned long *) "WSKT")
33 #define WS_LOOKUP_SERVICE_SIGNATURE (*(unsigned long *) "WLUP")
34 #define WS_WSAEVENT_SIGNATURE (*(unsigned long *) "WEVT")
35 #define WS_CPORT_SIGNATURE (*(unsigned long *) "WCPT")
38 int (*WSAStartupWithNLMHandle)( WORD version, LPWSADATA data, void *handle ) = NULL;
39 int (*WSACleanupWithNLMHandle)( void *handle ) = NULL;
41 static int wsa_startup_with_handle (WORD wVersionRequested, LPWSADATA data, void *handle)
45 if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
48 app_data->gs_startup_rtag = AllocateResourceTag(handle, "WinSock Start-up", WS_LOAD_ENTRY_SIGNATURE);
49 app_data->gs_socket_rtag = AllocateResourceTag(handle, "WinSock socket()", WS_SKT_SIGNATURE);
50 app_data->gs_lookup_rtag = AllocateResourceTag(handle, "WinSock Look-up", WS_LOOKUP_SERVICE_SIGNATURE);
51 app_data->gs_event_rtag = AllocateResourceTag(handle, "WinSock Event", WS_WSAEVENT_SIGNATURE);
52 app_data->gs_pcp_rtag = AllocateResourceTag(handle, "WinSock C-Port", WS_CPORT_SIGNATURE);
54 return WSAStartupRTags(wVersionRequested, data,
55 app_data->gs_startup_rtag,
56 app_data->gs_socket_rtag,
57 app_data->gs_lookup_rtag,
58 app_data->gs_event_rtag,
59 app_data->gs_pcp_rtag);
62 static int wsa_cleanup_with_handle (void *handle)
66 if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
69 return WSACleanupRTag(app_data->gs_startup_rtag);
72 static int UnregisterAppWithWinSock (void *nlm_handle)
74 if (!WSACleanupWithNLMHandle)
76 if (!(WSACleanupWithNLMHandle = ImportPublicObject(gLibHandle, "WSACleanupWithNLMHandle")))
77 WSACleanupWithNLMHandle = wsa_cleanup_with_handle;
80 return (*WSACleanupWithNLMHandle)(nlm_handle);
83 static int RegisterAppWithWinSock (void *nlm_handle)
87 WORD wVersionRequested = MAKEWORD(WSAHighByte, WSALowByte);
89 if (!WSAStartupWithNLMHandle)
91 if (!(WSAStartupWithNLMHandle = ImportPublicObject(gLibHandle, "WSAStartupWithNLMHandle")))
92 WSAStartupWithNLMHandle = wsa_startup_with_handle;
95 err = (*WSAStartupWithNLMHandle)(wVersionRequested, &wsaData, nlm_handle);
97 if (LOBYTE(wsaData.wVersion) != WSAHighByte ||
98 HIBYTE(wsaData.wVersion) != WSALowByte) {
100 UnregisterAppWithWinSock (nlm_handle);
107 APR_DECLARE(apr_status_t) apr_app_initialize(int *argc,
108 const char * const * *argv,
109 const char * const * *env)
111 /* An absolute noop. At present, only Win32 requires this stub, but it's
112 * required in order to move command arguments passed through the service
113 * control manager into the process, and it's required to fix the char*
114 * data passed in from win32 unicode into utf-8, win32's apr internal fmt.
116 return apr_initialize();
119 APR_DECLARE(apr_status_t) apr_initialize(void)
123 void *nlmhandle = getnlmhandle();
125 /* Register the NLM as using APR. If it is already
126 registered then just return. */
127 if (register_NLM(nlmhandle) != 0) {
131 /* apr_pool_initialize() is being called from the library
132 startup code since all of the memory resources belong
133 to the library rather than the application. */
135 if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
139 apr_pool_tag(pool, "apr_initilialize");
141 err = RegisterAppWithWinSock (nlmhandle);
147 apr_signal_init(pool);
152 APR_DECLARE_NONSTD(void) apr_terminate(void)
156 /* Get our instance data for shutting down. */
157 if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
160 /* Unregister the NLM. If it is not registered
162 if (unregister_NLM(app_data->gs_nlmhandle) != 0) {
166 /* apr_pool_terminate() is being called from the
167 library shutdown code since the memory resources
168 belong to the library rather than the application */
170 /* Just clean up the memory for the app that is going
172 netware_pool_proc_cleanup ();
174 UnregisterAppWithWinSock (app_data->gs_nlmhandle);
177 APR_DECLARE(void) apr_terminate2(void)