1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 ***************************************************************************/
24 #ifdef NETWARE /* Novell NetWare */
28 #ifdef __NOVELL_LIBC__
29 /* For native LibC-based NLM we need to register as a real lib. */
35 #include <nks/thread.h>
36 #include <nks/synch.h>
53 NXKey_t perthreadkey; /* if -1, no key obtained... */
58 void *gLibHandle = (void *) NULL;
59 rtag_t gAllocTag = (rtag_t) NULL;
60 NXMutex_t *gLibLock = (NXMutex_t *) NULL;
62 /* internal library function prototypes... */
63 int DisposeLibraryData ( void * );
64 void DisposeThreadData ( void * );
65 int GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );
68 int _NonAppStart( void *NLMHandle,
71 const char *loadDirPath,
72 size_t uninitializedDataLength,
74 int (*readRoutineP)( int conn,
75 void *fileHandle, size_t offset,
79 size_t customDataOffset,
80 size_t customDataSize,
82 const char **messages )
84 NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
87 #pragma unused(cmdLine)
88 #pragma unused(loadDirPath)
89 #pragma unused(uninitializedDataLength)
90 #pragma unused(NLMFileHandle)
91 #pragma unused(readRoutineP)
92 #pragma unused(customDataOffset)
93 #pragma unused(customDataSize)
94 #pragma unused(messageCount)
95 #pragma unused(messages)
99 ** Here we process our command line, post errors (to the error screen),
100 ** perform initializations and anything else we need to do before being able
101 ** to accept calls into us. If we succeed, we return non-zero and the NetWare
102 ** Loader will leave us up, otherwise we fail to load and get dumped.
104 gAllocTag = AllocateResourceTag(NLMHandle,
105 "<library-name> memory allocations",
109 OutputToScreen(errorScreen, "Unable to allocate resource tag for "
110 "library memory allocations.\n");
114 gLibId = register_library(DisposeLibraryData);
117 OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
121 gLibHandle = NLMHandle;
123 gLibLock = NXMutexAlloc(0, 0, &liblock);
126 OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
134 ** Here we clean up any resources we allocated. Resource tags is a big part
135 ** of what we created, but NetWare doesn't ask us to free those.
137 void _NonAppStop( void )
139 (void) unregister_library(gLibId);
140 NXMutexFree(gLibLock);
144 ** This function cannot be the first in the file for if the file is linked
145 ** first, then the check-unload function's offset will be nlmname.nlm+0
146 ** which is how to tell that there isn't one. When the check function is
147 ** first in the linked objects, it is ambiguous. For this reason, we will
148 ** put it inside this file after the stop function.
150 ** Here we check to see if it's alright to ourselves to be unloaded. If not,
151 ** we return a non-zero value. Right now, there isn't any reason not to allow
154 int _NonAppCheckUnload( void )
159 int GetOrSetUpData(int id, libdata_t **appData,
160 libthreaddata_t **threadData )
164 libthreaddata_t *thread_data;
166 NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
169 thread_data = (libthreaddata_t *) NULL;
172 ** Attempt to get our data for the application calling us. This is where we
173 ** store whatever application-specific information we need to carry in support
174 ** of calling applications.
176 app_data = (libdata_t *) get_app_data(id);
180 ** This application hasn't called us before; set up application AND per-thread
181 ** data. Of course, just in case a thread from this same application is calling
182 ** us simultaneously, we better lock our application data-creation mutex. We
183 ** also need to recheck for data after we acquire the lock because WE might be
184 ** that other thread that was too late to create the data and the first thread
185 ** in will have created it.
189 if(!(app_data = (libdata_t *) get_app_data(id))) {
190 app_data = (libdata_t *) malloc(sizeof(libdata_t));
193 memset(app_data, 0, sizeof(libdata_t));
195 app_data->tenbytes = malloc(10);
196 app_data->lock = NXMutexAlloc(0, 0, &liblock);
198 if(!app_data->tenbytes || !app_data->lock) {
200 NXMutexFree(app_data->lock);
203 app_data = (libdata_t *) NULL;
209 ** Here we burn in the application data that we were trying to get by calling
210 ** get_app_data(). Next time we call the first function, we'll get this data
211 ** we're just now setting. We also go on here to establish the per-thread data
212 ** for the calling thread, something we'll have to do on each application
213 ** thread the first time it calls us.
215 err = set_app_data(gLibId, app_data);
219 app_data = (libdata_t *) NULL;
223 /* create key for thread-specific data... */
224 err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
226 if(err) /* (no more keys left?) */
229 app_data->perthreadkey = key;
239 key = app_data->perthreadkey;
241 if(key != -1 /* couldn't create a key? no thread data */
242 && !(err = NXKeyGetValue(key, (void **) &thread_data))
245 ** Allocate the per-thread data for the calling thread. Regardless of whether
246 ** there was already application data or not, this may be the first call by a
247 ** a new thread. The fact that we allocation 20 bytes on a pointer is not very
248 ** important, this just helps to demonstrate that we can have arbitrarily
249 ** complex per-thread data.
251 thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t));
254 thread_data->_errno = 0;
255 thread_data->twentybytes = malloc(20);
257 if(!thread_data->twentybytes) {
259 thread_data = (libthreaddata_t *) NULL;
263 if((err = NXKeySetValue(key, thread_data))) {
264 free(thread_data->twentybytes);
266 thread_data = (libthreaddata_t *) NULL;
276 *threadData = thread_data;
281 int DisposeLibraryData( void *data )
284 void *tenbytes = ((libdata_t *) data)->tenbytes;
295 void DisposeThreadData( void *data )
298 void *twentybytes = ((libthreaddata_t *) data)->twentybytes;
307 #else /* __NOVELL_LIBC__ */
308 /* For native CLib-based NLM seems we can do a bit more simple. */
309 #include <nwthread.h>
313 /* initialize any globals here... */
315 /* do this if any global initializing was done
318 ExitThread (TSR_THREAD, 0);
322 #endif /* __NOVELL_LIBC__ */