if () => if()
[platform/upstream/curl.git] / lib / nwlib.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 #ifdef NETWARE /* Novell NetWare */
25
26 #include <stdlib.h>
27
28 #ifdef __NOVELL_LIBC__
29 /* For native LibC-based NLM we need to register as a real lib. */
30 #include <errno.h>
31 #include <string.h>
32 #include <library.h>
33 #include <netware.h>
34 #include <screen.h>
35 #include <nks/thread.h>
36 #include <nks/synch.h>
37
38 #include "memory.h"
39 #include "memdebug.h"
40
41 typedef struct
42 {
43   int     _errno;
44   void    *twentybytes;
45 } libthreaddata_t;
46
47 typedef struct
48 {
49   int         x;
50   int         y;
51   int         z;
52   void        *tenbytes;
53   NXKey_t     perthreadkey;   /* if -1, no key obtained... */
54   NXMutex_t   *lock;
55 } libdata_t;
56
57 int         gLibId      = -1;
58 void        *gLibHandle = (void *) NULL;
59 rtag_t      gAllocTag   = (rtag_t) NULL;
60 NXMutex_t   *gLibLock   = (NXMutex_t *) NULL;
61
62 /* internal library function prototypes... */
63 int     DisposeLibraryData ( void * );
64 void    DisposeThreadData ( void * );
65 int     GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );
66
67
68 int _NonAppStart( void        *NLMHandle,
69                   void        *errorScreen,
70                   const char  *cmdLine,
71                   const char  *loadDirPath,
72                   size_t      uninitializedDataLength,
73                   void        *NLMFileHandle,
74                   int         (*readRoutineP)( int conn,
75                                                void *fileHandle, size_t offset,
76                                                size_t nbytes,
77                                                size_t *bytesRead,
78                                                void *buffer ),
79                   size_t      customDataOffset,
80                   size_t      customDataSize,
81                   int         messageCount,
82                   const char  **messages )
83 {
84   NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
85
86 #ifndef __GNUC__
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)
96 #endif
97
98 /*
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.
103 */
104   gAllocTag = AllocateResourceTag(NLMHandle,
105                                   "<library-name> memory allocations",
106                                   AllocSignature);
107
108   if(!gAllocTag) {
109     OutputToScreen(errorScreen, "Unable to allocate resource tag for "
110                    "library memory allocations.\n");
111     return -1;
112   }
113
114   gLibId = register_library(DisposeLibraryData);
115
116   if(gLibId < -1) {
117     OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
118     return -1;
119   }
120
121   gLibHandle = NLMHandle;
122
123   gLibLock = NXMutexAlloc(0, 0, &liblock);
124
125   if(!gLibLock) {
126     OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
127     return -1;
128   }
129
130   return 0;
131 }
132
133 /*
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.
136 */
137 void _NonAppStop( void )
138 {
139   (void) unregister_library(gLibId);
140   NXMutexFree(gLibLock);
141 }
142
143 /*
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.
149 **
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
152 ** it.
153 */
154 int _NonAppCheckUnload( void )
155 {
156     return 0;
157 }
158
159 int GetOrSetUpData(int id, libdata_t **appData,
160                    libthreaddata_t **threadData )
161 {
162   int                 err;
163   libdata_t           *app_data;
164   libthreaddata_t *thread_data;
165   NXKey_t             key;
166   NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
167
168   err         = 0;
169   thread_data = (libthreaddata_t *) NULL;
170
171 /*
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.
175 */
176   app_data = (libdata_t *) get_app_data(id);
177
178   if(!app_data) {
179 /*
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.
186 */
187     NXLock(gLibLock);
188
189     if(!(app_data = (libdata_t *) get_app_data(id))) {
190       app_data = (libdata_t *) malloc(sizeof(libdata_t));
191
192       if(app_data) {
193         memset(app_data, 0, sizeof(libdata_t));
194
195         app_data->tenbytes = malloc(10);
196         app_data->lock     = NXMutexAlloc(0, 0, &liblock);
197
198         if(!app_data->tenbytes || !app_data->lock) {
199           if(app_data->lock)
200             NXMutexFree(app_data->lock);
201
202           free(app_data);
203           app_data = (libdata_t *) NULL;
204           err      = ENOMEM;
205         }
206
207         if(app_data) {
208 /*
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.
214 */
215           err = set_app_data(gLibId, app_data);
216
217           if(err) {
218             free(app_data);
219             app_data = (libdata_t *) NULL;
220             err      = ENOMEM;
221           }
222           else {
223             /* create key for thread-specific data... */
224             err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
225
226             if(err)                /* (no more keys left?) */
227               key = -1;
228
229             app_data->perthreadkey = key;
230           }
231         }
232       }
233     }
234
235     NXUnlock(gLibLock);
236   }
237
238   if(app_data) {
239     key = app_data->perthreadkey;
240
241     if(key != -1 /* couldn't create a key? no thread data */
242         && !(err = NXKeyGetValue(key, (void **) &thread_data))
243         && !thread_data) {
244 /*
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.
250 */
251       thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t));
252
253       if(thread_data) {
254         thread_data->_errno      = 0;
255         thread_data->twentybytes = malloc(20);
256
257         if(!thread_data->twentybytes) {
258           free(thread_data);
259           thread_data = (libthreaddata_t *) NULL;
260           err         = ENOMEM;
261         }
262
263         if((err = NXKeySetValue(key, thread_data))) {
264           free(thread_data->twentybytes);
265           free(thread_data);
266           thread_data = (libthreaddata_t *) NULL;
267         }
268       }
269     }
270   }
271
272   if(appData)
273     *appData = app_data;
274
275   if(threadData)
276     *threadData = thread_data;
277
278   return err;
279 }
280
281 int DisposeLibraryData( void *data )
282 {
283   if(data) {
284     void *tenbytes = ((libdata_t *) data)->tenbytes;
285
286     if(tenbytes)
287       free(tenbytes);
288
289     free(data);
290   }
291
292   return 0;
293 }
294
295 void DisposeThreadData( void *data )
296 {
297   if(data) {
298     void *twentybytes = ((libthreaddata_t *) data)->twentybytes;
299
300     if(twentybytes)
301       free(twentybytes);
302
303     free(data);
304   }
305 }
306
307 #else /* __NOVELL_LIBC__ */
308 /* For native CLib-based NLM seems we can do a bit more simple. */
309 #include <nwthread.h>
310
311 int main ( void )
312 {
313     /* initialize any globals here... */
314
315     /* do this if any global initializing was done
316     SynchronizeStart();
317     */
318     ExitThread (TSR_THREAD, 0);
319     return 0;
320 }
321
322 #endif /* __NOVELL_LIBC__ */
323
324 #endif /* NETWARE */