nwlib.c 9.3 KB

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