nwlib.c 9.2 KB

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