1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, 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 https://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.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef NETWARE /* Novell NetWare */
27 #ifdef __NOVELL_LIBC__
28 /* For native LibC-based NLM we need to register as a real lib. */
32 #include <nks/thread.h>
33 #include <nks/synch.h>
35 #include "curl_memory.h"
36 /* The last #include file should be: */
39 struct libthreaddata {
49 NXKey_t perthreadkey; /* if -1, no key obtained... */
54 void *gLibHandle = (void *) NULL;
55 rtag_t gAllocTag = (rtag_t) NULL;
56 NXMutex_t *gLibLock = (NXMutex_t *) NULL;
58 /* internal library function prototypes... */
59 int DisposeLibraryData(void *);
60 void DisposeThreadData(void *);
61 int GetOrSetUpData(int id, struct libdata **data,
62 struct libthreaddata **threaddata);
65 int _NonAppStart(void *NLMHandle,
68 const char *loadDirPath,
69 size_t uninitializedDataLength,
71 int (*readRoutineP)(int conn,
72 void *fileHandle, size_t offset,
76 size_t customDataOffset,
77 size_t customDataSize,
79 const char **messages)
81 NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
84 #pragma unused(cmdLine)
85 #pragma unused(loadDirPath)
86 #pragma unused(uninitializedDataLength)
87 #pragma unused(NLMFileHandle)
88 #pragma unused(readRoutineP)
89 #pragma unused(customDataOffset)
90 #pragma unused(customDataSize)
91 #pragma unused(messageCount)
92 #pragma unused(messages)
96 * Here we process our command line, post errors (to the error screen),
97 * perform initializations and anything else we need to do before being able
98 * to accept calls into us. If we succeed, we return non-zero and the NetWare
99 * Loader will leave us up, otherwise we fail to load and get dumped.
101 gAllocTag = AllocateResourceTag(NLMHandle,
102 "<library-name> memory allocations",
106 OutputToScreen(errorScreen, "Unable to allocate resource tag for "
107 "library memory allocations.\n");
111 gLibId = register_library(DisposeLibraryData);
114 OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
118 gLibHandle = NLMHandle;
120 gLibLock = NXMutexAlloc(0, 0, &liblock);
123 OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
131 * Here we clean up any resources we allocated. Resource tags is a big part
132 * of what we created, but NetWare doesn't ask us to free those.
134 void _NonAppStop(void)
136 (void) unregister_library(gLibId);
137 NXMutexFree(gLibLock);
141 * This function cannot be the first in the file for if the file is linked
142 * first, then the check-unload function's offset will be nlmname.nlm+0
143 * which is how to tell that there isn't one. When the check function is
144 * first in the linked objects, it is ambiguous. For this reason, we will
145 * put it inside this file after the stop function.
147 * Here we check to see if it's alright to ourselves to be unloaded. If not,
148 * we return a non-zero value. Right now, there isn't any reason not to allow
151 int _NonAppCheckUnload(void)
156 int GetOrSetUpData(int id, struct libdata **appData,
157 struct libthreaddata **threadData)
160 struct libdata *app_data;
161 struct libthreaddata *thread_data;
163 NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
166 thread_data = (struct libthreaddata_t *) NULL;
169 * Attempt to get our data for the application calling us. This is where we
170 * store whatever application-specific information we need to carry in
171 * support of calling applications.
173 app_data = (struct libdata *) get_app_data(id);
177 * This application hasn't called us before; set up application AND
178 * per-thread data. Of course, just in case a thread from this same
179 * application is calling us simultaneously, we better lock our application
180 * data-creation mutex. We also need to recheck for data after we acquire
181 * the lock because WE might be that other thread that was too late to
182 * create the data and the first thread in will have created it.
186 app_data = (struct libdata *) get_app_data(id);
188 app_data = calloc(1, sizeof(struct libdata));
191 app_data->tenbytes = malloc(10);
192 app_data->lock = NXMutexAlloc(0, 0, &liblock);
194 if(!app_data->tenbytes || !app_data->lock) {
196 NXMutexFree(app_data->lock);
197 free(app_data->tenbytes);
199 app_data = (libdata_t *) NULL;
205 * Here we burn in the application data that we were trying to get
206 * by calling get_app_data(). Next time we call the first function,
207 * we'll get this data we're just now setting. We also go on here to
208 * establish the per-thread data for the calling thread, something
209 * we'll have to do on each application thread the first time
212 err = set_app_data(gLibId, app_data);
216 NXMutexFree(app_data->lock);
217 free(app_data->tenbytes);
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
246 * whether there was already application data or not, this may be the
247 * first call by a new thread. The fact that we allocation 20 bytes on
248 * a pointer is not very important, this just helps to demonstrate that
249 * we can have arbitrarily complex per-thread data.
251 thread_data = malloc(sizeof(struct libthreaddata));
254 thread_data->_errno = 0;
255 thread_data->twentybytes = malloc(20);
257 if(!thread_data->twentybytes) {
259 thread_data = (struct libthreaddata *) NULL;
263 err = NXKeySetValue(key, thread_data);
265 free(thread_data->twentybytes);
267 thread_data = (struct libthreaddata *) NULL;
277 *threadData = thread_data;
282 int DisposeLibraryData(void *data)
285 void *tenbytes = ((libdata_t *) data)->tenbytes;
294 void DisposeThreadData(void *data)
297 void *twentybytes = ((struct libthreaddata *) data)->twentybytes;
304 #else /* __NOVELL_LIBC__ */
305 /* For native CLib-based NLM seems we can do a bit more simple. */
306 #include <nwthread.h>
310 /* initialize any globals here... */
312 /* do this if any global initializing was done
315 ExitThread(TSR_THREAD, 0);
319 #endif /* __NOVELL_LIBC__ */
324 # pragma warn(disable:2024) /* Disable warning #2024: Empty input file */