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