upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / misc / netware / start.c
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
17 #include "apr.h"
18 #include "apr_general.h"
19 #include "apr_pools.h"
20 #include "apr_signal.h"
21
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"
25
26 /*
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().
30 */
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")
36
37
38 int (*WSAStartupWithNLMHandle)( WORD version, LPWSADATA data, void *handle ) = NULL;
39 int (*WSACleanupWithNLMHandle)( void *handle ) = NULL;
40
41 static int wsa_startup_with_handle (WORD wVersionRequested, LPWSADATA data, void *handle)
42 {
43     APP_DATA *app_data;
44     
45     if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
46         return APR_EGENERAL;
47
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);
53
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);
60 }
61
62 static int wsa_cleanup_with_handle (void *handle)
63 {
64     APP_DATA *app_data;
65     
66     if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
67         return APR_EGENERAL;
68
69     return WSACleanupRTag(app_data->gs_startup_rtag);
70 }
71
72 static int UnregisterAppWithWinSock (void *nlm_handle)
73 {
74     if (!WSACleanupWithNLMHandle)
75     {
76         if (!(WSACleanupWithNLMHandle = ImportPublicObject(gLibHandle, "WSACleanupWithNLMHandle")))
77             WSACleanupWithNLMHandle = wsa_cleanup_with_handle;
78     }
79
80     return (*WSACleanupWithNLMHandle)(nlm_handle);
81 }
82
83 static int RegisterAppWithWinSock (void *nlm_handle)
84 {
85     int err;
86     WSADATA wsaData;
87     WORD wVersionRequested = MAKEWORD(WSAHighByte, WSALowByte);
88
89     if (!WSAStartupWithNLMHandle)
90     {
91         if (!(WSAStartupWithNLMHandle = ImportPublicObject(gLibHandle, "WSAStartupWithNLMHandle")))
92             WSAStartupWithNLMHandle = wsa_startup_with_handle;
93     }
94
95     err = (*WSAStartupWithNLMHandle)(wVersionRequested, &wsaData, nlm_handle);
96
97     if (LOBYTE(wsaData.wVersion) != WSAHighByte ||
98         HIBYTE(wsaData.wVersion) != WSALowByte) {
99         
100         UnregisterAppWithWinSock (nlm_handle);
101         return APR_EEXIST;
102     }
103
104     return err;
105 }
106
107 APR_DECLARE(apr_status_t) apr_app_initialize(int *argc, 
108                                              const char * const * *argv, 
109                                              const char * const * *env)
110 {
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.
115      */
116     return apr_initialize();
117 }
118
119 APR_DECLARE(apr_status_t) apr_initialize(void)
120 {
121     apr_pool_t *pool;
122     int err;
123     void *nlmhandle = getnlmhandle();
124
125     /* Register the NLM as using APR. If it is already
126         registered then just return. */
127     if (register_NLM(nlmhandle) != 0) {
128         return APR_SUCCESS;
129     }
130
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. */
134     
135     if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
136         return APR_ENOPOOL;
137     }
138
139     apr_pool_tag(pool, "apr_initilialize");
140
141     err = RegisterAppWithWinSock (nlmhandle);
142     
143     if (err) {
144         return err;
145     }
146
147     apr_signal_init(pool);
148
149     return APR_SUCCESS;
150 }
151
152 APR_DECLARE_NONSTD(void) apr_terminate(void)
153 {
154     APP_DATA *app_data;
155
156     /* Get our instance data for shutting down. */
157     if (!(app_data = (APP_DATA*) get_app_data(gLibId)))
158         return;
159
160     /* Unregister the NLM. If it is not registered
161         then just return. */
162     if (unregister_NLM(app_data->gs_nlmhandle) != 0) {
163         return;
164     }
165
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 */
169
170     /* Just clean up the memory for the app that is going
171         away. */
172     netware_pool_proc_cleanup ();
173
174     UnregisterAppWithWinSock (app_data->gs_nlmhandle);
175 }
176
177 APR_DECLARE(void) apr_terminate2(void)
178 {
179     apr_terminate();
180 }