nwlib.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #ifdef NETWARE /* Novell NetWare */
00026 
00027 #ifdef __NOVELL_LIBC__
00028 /* For native LibC-based NLM we need to register as a real lib. */
00029 #include <library.h>
00030 #include <netware.h>
00031 #include <screen.h>
00032 #include <nks/thread.h>
00033 #include <nks/synch.h>
00034 
00035 #include "curl_memory.h"
00036 /* The last #include file should be: */
00037 #include "memdebug.h"
00038 
00039 typedef struct
00040 {
00041   int     _errno;
00042   void    *twentybytes;
00043 } libthreaddata_t;
00044 
00045 typedef struct
00046 {
00047   int         x;
00048   int         y;
00049   int         z;
00050   void        *tenbytes;
00051   NXKey_t     perthreadkey;   /* if -1, no key obtained... */
00052   NXMutex_t   *lock;
00053 } libdata_t;
00054 
00055 int         gLibId      = -1;
00056 void        *gLibHandle = (void *) NULL;
00057 rtag_t      gAllocTag   = (rtag_t) NULL;
00058 NXMutex_t   *gLibLock   = (NXMutex_t *) NULL;
00059 
00060 /* internal library function prototypes... */
00061 int  DisposeLibraryData(void *);
00062 void DisposeThreadData(void *);
00063 int  GetOrSetUpData(int id, libdata_t **data, libthreaddata_t **threaddata);
00064 
00065 
00066 int _NonAppStart(void        *NLMHandle,
00067                  void        *errorScreen,
00068                  const char  *cmdLine,
00069                  const char  *loadDirPath,
00070                  size_t      uninitializedDataLength,
00071                  void        *NLMFileHandle,
00072                  int         (*readRoutineP)(int conn,
00073                                              void *fileHandle, size_t offset,
00074                                              size_t nbytes,
00075                                              size_t *bytesRead,
00076                                              void *buffer),
00077                   size_t      customDataOffset,
00078                   size_t      customDataSize,
00079                   int         messageCount,
00080                   const char  **messages)
00081 {
00082   NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
00083 
00084 #ifndef __GNUC__
00085 #pragma unused(cmdLine)
00086 #pragma unused(loadDirPath)
00087 #pragma unused(uninitializedDataLength)
00088 #pragma unused(NLMFileHandle)
00089 #pragma unused(readRoutineP)
00090 #pragma unused(customDataOffset)
00091 #pragma unused(customDataSize)
00092 #pragma unused(messageCount)
00093 #pragma unused(messages)
00094 #endif
00095 
00096   /*
00097    * Here we process our command line, post errors (to the error screen),
00098    * perform initializations and anything else we need to do before being able
00099    * to accept calls into us. If we succeed, we return non-zero and the NetWare
00100    * Loader will leave us up, otherwise we fail to load and get dumped.
00101    */
00102   gAllocTag = AllocateResourceTag(NLMHandle,
00103                                   "<library-name> memory allocations",
00104                                   AllocSignature);
00105 
00106   if(!gAllocTag) {
00107     OutputToScreen(errorScreen, "Unable to allocate resource tag for "
00108                    "library memory allocations.\n");
00109     return -1;
00110   }
00111 
00112   gLibId = register_library(DisposeLibraryData);
00113 
00114   if(gLibId < -1) {
00115     OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
00116     return -1;
00117   }
00118 
00119   gLibHandle = NLMHandle;
00120 
00121   gLibLock = NXMutexAlloc(0, 0, &liblock);
00122 
00123   if(!gLibLock) {
00124     OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
00125     return -1;
00126   }
00127 
00128   return 0;
00129 }
00130 
00131 /*
00132  * Here we clean up any resources we allocated. Resource tags is a big part
00133  * of what we created, but NetWare doesn't ask us to free those.
00134  */
00135 void _NonAppStop(void)
00136 {
00137   (void) unregister_library(gLibId);
00138   NXMutexFree(gLibLock);
00139 }
00140 
00141 /*
00142  * This function cannot be the first in the file for if the file is linked
00143  * first, then the check-unload function's offset will be nlmname.nlm+0
00144  * which is how to tell that there isn't one. When the check function is
00145  * first in the linked objects, it is ambiguous. For this reason, we will
00146  * put it inside this file after the stop function.
00147  *
00148  * Here we check to see if it's alright to ourselves to be unloaded. If not,
00149  * we return a non-zero value. Right now, there isn't any reason not to allow
00150  * it.
00151  */
00152 int _NonAppCheckUnload(void)
00153 {
00154     return 0;
00155 }
00156 
00157 int GetOrSetUpData(int id, libdata_t **appData,
00158                    libthreaddata_t **threadData)
00159 {
00160   int                 err;
00161   libdata_t           *app_data;
00162   libthreaddata_t *thread_data;
00163   NXKey_t             key;
00164   NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
00165 
00166   err         = 0;
00167   thread_data = (libthreaddata_t *) NULL;
00168 
00169   /*
00170    * Attempt to get our data for the application calling us. This is where we
00171    * store whatever application-specific information we need to carry in
00172    * support of calling applications.
00173    */
00174   app_data = (libdata_t *) get_app_data(id);
00175 
00176   if(!app_data) {
00177     /*
00178      * This application hasn't called us before; set up application AND
00179      * per-thread data. Of course, just in case a thread from this same
00180      * application is calling us simultaneously, we better lock our application
00181      * data-creation mutex. We also need to recheck for data after we acquire
00182      * the lock because WE might be that other thread that was too late to
00183      * create the data and the first thread in will have created it.
00184      */
00185     NXLock(gLibLock);
00186 
00187     app_data = (libdata_t *) get_app_data(id);
00188     if(!app_data) {
00189       app_data = malloc(sizeof(libdata_t));
00190 
00191       if(app_data) {
00192         memset(app_data, 0, sizeof(libdata_t));
00193 
00194         app_data->tenbytes = malloc(10);
00195         app_data->lock     = NXMutexAlloc(0, 0, &liblock);
00196 
00197         if(!app_data->tenbytes || !app_data->lock) {
00198           if(app_data->lock)
00199             NXMutexFree(app_data->lock);
00200 
00201           free(app_data);
00202           app_data = (libdata_t *) NULL;
00203           err      = ENOMEM;
00204         }
00205 
00206         if(app_data) {
00207           /*
00208            * Here we burn in the application data that we were trying to get
00209            * by calling get_app_data(). Next time we call the first function,
00210            * we'll get this data we're just now setting. We also go on here to
00211            * establish the per-thread data for the calling thread, something
00212            * we'll have to do on each application thread the first time
00213            * it calls us.
00214            */
00215           err = set_app_data(gLibId, app_data);
00216 
00217           if(err) {
00218             free(app_data);
00219             app_data = (libdata_t *) NULL;
00220             err      = ENOMEM;
00221           }
00222           else {
00223             /* create key for thread-specific data... */
00224             err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
00225 
00226             if(err)                /* (no more keys left?) */
00227               key = -1;
00228 
00229             app_data->perthreadkey = key;
00230           }
00231         }
00232       }
00233     }
00234 
00235     NXUnlock(gLibLock);
00236   }
00237 
00238   if(app_data) {
00239     key = app_data->perthreadkey;
00240 
00241     if(key != -1 /* couldn't create a key? no thread data */
00242         && !(err = NXKeyGetValue(key, (void **) &thread_data))
00243         && !thread_data) {
00244       /*
00245        * Allocate the per-thread data for the calling thread. Regardless of
00246        * whether there was already application data or not, this may be the
00247        * first call by a new thread. The fact that we allocation 20 bytes on
00248        * a pointer is not very important, this just helps to demonstrate that
00249        * we can have arbitrarily complex per-thread data.
00250        */
00251       thread_data = malloc(sizeof(libthreaddata_t));
00252 
00253       if(thread_data) {
00254         thread_data->_errno      = 0;
00255         thread_data->twentybytes = malloc(20);
00256 
00257         if(!thread_data->twentybytes) {
00258           free(thread_data);
00259           thread_data = (libthreaddata_t *) NULL;
00260           err         = ENOMEM;
00261         }
00262 
00263         err = NXKeySetValue(key, thread_data);
00264         if(err) {
00265           free(thread_data->twentybytes);
00266           free(thread_data);
00267           thread_data = (libthreaddata_t *) NULL;
00268         }
00269       }
00270     }
00271   }
00272 
00273   if(appData)
00274     *appData = app_data;
00275 
00276   if(threadData)
00277     *threadData = thread_data;
00278 
00279   return err;
00280 }
00281 
00282 int DisposeLibraryData(void *data)
00283 {
00284   if(data) {
00285     void *tenbytes = ((libdata_t *) data)->tenbytes;
00286 
00287     free(tenbytes);
00288     free(data);
00289   }
00290 
00291   return 0;
00292 }
00293 
00294 void DisposeThreadData(void *data)
00295 {
00296   if(data) {
00297     void *twentybytes = ((libthreaddata_t *) data)->twentybytes;
00298 
00299     free(twentybytes);
00300     free(data);
00301   }
00302 }
00303 
00304 #else /* __NOVELL_LIBC__ */
00305 /* For native CLib-based NLM seems we can do a bit more simple. */
00306 #include <nwthread.h>
00307 
00308 int main(void)
00309 {
00310   /* initialize any globals here... */
00311 
00312   /* do this if any global initializing was done
00313   SynchronizeStart();
00314   */
00315   ExitThread(TSR_THREAD, 0);
00316   return 0;
00317 }
00318 
00319 #endif /* __NOVELL_LIBC__ */
00320 
00321 #else /* NETWARE */
00322 
00323 #ifdef __POCC__
00324 #  pragma warn(disable:2024)  /* Disable warning #2024: Empty input file */
00325 #endif
00326 
00327 #endif /* NETWARE */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05