nwlib.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. #ifdef NETWARE /* Novell NetWare */
  24. #include <stdlib.h>
  25. #ifdef __NOVELL_LIBC__
  26. /* For native LibC-based NLM we need to register as a real lib. */
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <library.h>
  30. #include <netware.h>
  31. #include <screen.h>
  32. #include <nks/thread.h>
  33. #include <nks/synch.h>
  34. typedef struct
  35. {
  36. int _errno;
  37. void *twentybytes;
  38. } libthreaddata_t;
  39. typedef struct
  40. {
  41. int x;
  42. int y;
  43. int z;
  44. void *tenbytes;
  45. NXKey_t perthreadkey; /* if -1, no key obtained... */
  46. NXMutex_t *lock;
  47. } libdata_t;
  48. int gLibId = -1;
  49. void *gLibHandle = (void *) NULL;
  50. rtag_t gAllocTag = (rtag_t) NULL;
  51. NXMutex_t *gLibLock = (NXMutex_t *) NULL;
  52. /* internal library function prototypes... */
  53. int DisposeLibraryData ( void * );
  54. void DisposeThreadData ( void * );
  55. int GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );
  56. int _NonAppStart( void *NLMHandle,
  57. void *errorScreen,
  58. const char *cmdLine,
  59. const char *loadDirPath,
  60. size_t uninitializedDataLength,
  61. void *NLMFileHandle,
  62. int (*readRoutineP)( int conn,
  63. void *fileHandle, size_t offset,
  64. size_t nbytes,
  65. size_t *bytesRead,
  66. void *buffer ),
  67. size_t customDataOffset,
  68. size_t customDataSize,
  69. int messageCount,
  70. const char **messages )
  71. {
  72. NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
  73. #ifndef __GNUC__
  74. #pragma unused(cmdLine)
  75. #pragma unused(loadDirPath)
  76. #pragma unused(uninitializedDataLength)
  77. #pragma unused(NLMFileHandle)
  78. #pragma unused(readRoutineP)
  79. #pragma unused(customDataOffset)
  80. #pragma unused(customDataSize)
  81. #pragma unused(messageCount)
  82. #pragma unused(messages)
  83. #endif
  84. /*
  85. ** Here we process our command line, post errors (to the error screen),
  86. ** perform initializations and anything else we need to do before being able
  87. ** to accept calls into us. If we succeed, we return non-zero and the NetWare
  88. ** Loader will leave us up, otherwise we fail to load and get dumped.
  89. */
  90. gAllocTag = AllocateResourceTag(NLMHandle,
  91. "<library-name> memory allocations",
  92. AllocSignature);
  93. if(!gAllocTag) {
  94. OutputToScreen(errorScreen, "Unable to allocate resource tag for "
  95. "library memory allocations.\n");
  96. return -1;
  97. }
  98. gLibId = register_library(DisposeLibraryData);
  99. if(gLibId < -1) {
  100. OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
  101. return -1;
  102. }
  103. gLibHandle = NLMHandle;
  104. gLibLock = NXMutexAlloc(0, 0, &liblock);
  105. if(!gLibLock) {
  106. OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. /*
  112. ** Here we clean up any resources we allocated. Resource tags is a big part
  113. ** of what we created, but NetWare doesn't ask us to free those.
  114. */
  115. void _NonAppStop( void )
  116. {
  117. (void) unregister_library(gLibId);
  118. NXMutexFree(gLibLock);
  119. }
  120. /*
  121. ** This function cannot be the first in the file for if the file is linked
  122. ** first, then the check-unload function's offset will be nlmname.nlm+0
  123. ** which is how to tell that there isn't one. When the check function is
  124. ** first in the linked objects, it is ambiguous. For this reason, we will
  125. ** put it inside this file after the stop function.
  126. **
  127. ** Here we check to see if it's alright to ourselves to be unloaded. If not,
  128. ** we return a non-zero value. Right now, there isn't any reason not to allow
  129. ** it.
  130. */
  131. int _NonAppCheckUnload( void )
  132. {
  133. return 0;
  134. }
  135. int GetOrSetUpData(int id, libdata_t **appData,
  136. libthreaddata_t **threadData )
  137. {
  138. int err;
  139. libdata_t *app_data;
  140. libthreaddata_t *thread_data;
  141. NXKey_t key;
  142. NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
  143. err = 0;
  144. thread_data = (libthreaddata_t *) NULL;
  145. /*
  146. ** Attempt to get our data for the application calling us. This is where we
  147. ** store whatever application-specific information we need to carry in support
  148. ** of calling applications.
  149. */
  150. app_data = (libdata_t *) get_app_data(id);
  151. if(!app_data) {
  152. /*
  153. ** This application hasn't called us before; set up application AND per-thread
  154. ** data. Of course, just in case a thread from this same application is calling
  155. ** us simultaneously, we better lock our application data-creation mutex. We
  156. ** also need to recheck for data after we acquire the lock because WE might be
  157. ** that other thread that was too late to create the data and the first thread
  158. ** in will have created it.
  159. */
  160. NXLock(gLibLock);
  161. if(!(app_data = (libdata_t *) get_app_data(id))) {
  162. app_data = malloc(sizeof(libdata_t));
  163. if(app_data) {
  164. memset(app_data, 0, sizeof(libdata_t));
  165. app_data->tenbytes = malloc(10);
  166. app_data->lock = NXMutexAlloc(0, 0, &liblock);
  167. if(!app_data->tenbytes || !app_data->lock) {
  168. if(app_data->lock)
  169. NXMutexFree(app_data->lock);
  170. free(app_data);
  171. app_data = (libdata_t *) NULL;
  172. err = ENOMEM;
  173. }
  174. if(app_data) {
  175. /*
  176. ** Here we burn in the application data that we were trying to get by calling
  177. ** get_app_data(). Next time we call the first function, we'll get this data
  178. ** we're just now setting. We also go on here to establish the per-thread data
  179. ** for the calling thread, something we'll have to do on each application
  180. ** thread the first time it calls us.
  181. */
  182. err = set_app_data(gLibId, app_data);
  183. if(err) {
  184. free(app_data);
  185. app_data = (libdata_t *) NULL;
  186. err = ENOMEM;
  187. }
  188. else {
  189. /* create key for thread-specific data... */
  190. err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
  191. if(err) /* (no more keys left?) */
  192. key = -1;
  193. app_data->perthreadkey = key;
  194. }
  195. }
  196. }
  197. }
  198. NXUnlock(gLibLock);
  199. }
  200. if(app_data) {
  201. key = app_data->perthreadkey;
  202. if(key != -1 /* couldn't create a key? no thread data */
  203. && !(err = NXKeyGetValue(key, (void **) &thread_data))
  204. && !thread_data) {
  205. /*
  206. ** Allocate the per-thread data for the calling thread. Regardless of whether
  207. ** there was already application data or not, this may be the first call by a
  208. ** a new thread. The fact that we allocation 20 bytes on a pointer is not very
  209. ** important, this just helps to demonstrate that we can have arbitrarily
  210. ** complex per-thread data.
  211. */
  212. thread_data = malloc(sizeof(libthreaddata_t));
  213. if(thread_data) {
  214. thread_data->_errno = 0;
  215. thread_data->twentybytes = malloc(20);
  216. if(!thread_data->twentybytes) {
  217. free(thread_data);
  218. thread_data = (libthreaddata_t *) NULL;
  219. err = ENOMEM;
  220. }
  221. if((err = NXKeySetValue(key, thread_data))) {
  222. free(thread_data->twentybytes);
  223. free(thread_data);
  224. thread_data = (libthreaddata_t *) NULL;
  225. }
  226. }
  227. }
  228. }
  229. if(appData)
  230. *appData = app_data;
  231. if(threadData)
  232. *threadData = thread_data;
  233. return err;
  234. }
  235. int DisposeLibraryData( void *data )
  236. {
  237. if(data) {
  238. void *tenbytes = ((libdata_t *) data)->tenbytes;
  239. if(tenbytes)
  240. free(tenbytes);
  241. free(data);
  242. }
  243. return 0;
  244. }
  245. void DisposeThreadData( void *data )
  246. {
  247. if(data) {
  248. void *twentybytes = ((libthreaddata_t *) data)->twentybytes;
  249. if(twentybytes)
  250. free(twentybytes);
  251. free(data);
  252. }
  253. }
  254. #else /* __NOVELL_LIBC__ */
  255. /* For native CLib-based NLM seems we can do a bit more simple. */
  256. #include <nwthread.h>
  257. int main ( void )
  258. {
  259. /* initialize any globals here... */
  260. /* do this if any global initializing was done
  261. SynchronizeStart();
  262. */
  263. ExitThread (TSR_THREAD, 0);
  264. return 0;
  265. }
  266. #endif /* __NOVELL_LIBC__ */
  267. #else /* NETWARE */
  268. #ifdef __POCC__
  269. # pragma warn(disable:2024) /* Disable warning #2024: Empty input file */
  270. #endif
  271. #endif /* NETWARE */