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 <netware.h>
17 #include <library.h>
18 #include <nks/synch.h>
19 
20 #include "apr_pools.h"
21 #include "apr_private.h"
22 #include "apr_arch_internal_time.h"
23 
24 
25 /* library-private data...*/
26 int          gLibId = -1;
27 void         *gLibHandle = (void *) NULL;
28 NXMutex_t    *gLibLock = (NXMutex_t *) NULL;
29 
30 /* internal library function prototypes...*/
31 int DisposeLibraryData(void *);
32 
_NonAppStart(void * NLMHandle,void * errorScreen,const char * cmdLine,const char * loadDirPath,size_t uninitializedDataLength,void * NLMFileHandle,int (* readRoutineP)(int conn,void * fileHandle,size_t offset,size_t nbytes,size_t * bytesRead,void * buffer),size_t customDataOffset,size_t customDataSize,int messageCount,const char ** messages)33 int _NonAppStart
34 (
35     void        *NLMHandle,
36     void        *errorScreen,
37     const char  *cmdLine,
38     const char  *loadDirPath,
39     size_t      uninitializedDataLength,
40     void        *NLMFileHandle,
41     int         (*readRoutineP)( int conn, void *fileHandle, size_t offset,
42                     size_t nbytes, size_t *bytesRead, void *buffer ),
43     size_t      customDataOffset,
44     size_t      customDataSize,
45     int         messageCount,
46     const char  **messages
47 )
48 {
49 #ifdef USE_WINSOCK
50     WSADATA wsaData;
51 #endif
52     apr_status_t status;
53 
54     NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
55 
56 #pragma unused(cmdLine)
57 #pragma unused(loadDirPath)
58 #pragma unused(uninitializedDataLength)
59 #pragma unused(NLMFileHandle)
60 #pragma unused(readRoutineP)
61 #pragma unused(customDataOffset)
62 #pragma unused(customDataSize)
63 #pragma unused(messageCount)
64 #pragma unused(messages)
65 
66     gLibId = register_library(DisposeLibraryData);
67 
68     if (gLibId < -1)
69     {
70         OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
71         return -1;
72     }
73 
74     gLibHandle = NLMHandle;
75 
76     gLibLock = NXMutexAlloc(0, 0, &liblock);
77 
78     if (!gLibLock)
79     {
80         OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
81         return -1;
82     }
83 
84     apr_netware_setup_time();
85 
86     if ((status = apr_pool_initialize()) != APR_SUCCESS)
87         return status;
88 
89 #ifdef USE_WINSOCK
90     return WSAStartup((WORD) MAKEWORD(2, 0), &wsaData);
91 #else
92     return 0;
93 #endif
94 }
95 
_NonAppStop(void)96 void _NonAppStop( void )
97 {
98     apr_pool_terminate();
99 
100 #ifdef USE_WINSOCK
101     WSACleanup();
102 #endif
103 
104     unregister_library(gLibId);
105     NXMutexFree(gLibLock);
106 }
107 
_NonAppCheckUnload(void)108 int  _NonAppCheckUnload( void )
109 {
110     return 0;
111 }
112 
register_NLM(void * NLMHandle)113 int register_NLM(void *NLMHandle)
114 {
115     APP_DATA *app_data = (APP_DATA*) get_app_data(gLibId);
116 
117     NXLock(gLibLock);
118     if (!app_data) {
119         app_data = (APP_DATA*)library_malloc(gLibHandle, sizeof(APP_DATA));
120 
121         if (app_data) {
122             memset (app_data, 0, sizeof(APP_DATA));
123             set_app_data(gLibId, app_data);
124             app_data->gs_nlmhandle = NLMHandle;
125         }
126     }
127 
128     if (app_data && (!app_data->initialized)) {
129         app_data->initialized = 1;
130         NXUnlock(gLibLock);
131         return 0;
132     }
133 
134     NXUnlock(gLibLock);
135     return 1;
136 }
137 
unregister_NLM(void * NLMHandle)138 int unregister_NLM(void *NLMHandle)
139 {
140     APP_DATA *app_data = (APP_DATA*) get_app_data(gLibId);
141 
142     NXLock(gLibLock);
143     if (app_data) {
144         app_data->initialized = 0;
145         NXUnlock(gLibLock);
146         return 0;
147     }
148     NXUnlock(gLibLock);
149     return 1;
150 }
151 
DisposeLibraryData(void * data)152 int DisposeLibraryData(void *data)
153 {
154     if (data)
155     {
156         library_free(data);
157     }
158 
159     return 0;
160 }
161 
setGlobalPool(void * data)162 int setGlobalPool(void *data)
163 {
164     APP_DATA *app_data = (APP_DATA*) get_app_data(gLibId);
165 
166     NXLock(gLibLock);
167 
168     if (app_data && !app_data->gPool) {
169         app_data->gPool = data;
170     }
171 
172     NXUnlock(gLibLock);
173     return 1;
174 }
175 
getGlobalPool()176 void* getGlobalPool()
177 {
178     APP_DATA *app_data = (APP_DATA*) get_app_data(gLibId);
179 
180     if (app_data) {
181         return app_data->gPool;
182     }
183 
184     return NULL;
185 }
186 
187