apiutil.c

来自「IBM的Linux上的PKCS#11实现」· C语言 代码 · 共 1,239 行 · 第 1/3 页

C
1,239
字号
}// Register the process with PKCSSLOTD in the// shared memory.// This call must be made with the API Global Mutex Locked// and the Anchor control block initialized with the // shared memory.  No checking for shared memory validity is doneint API_Register(){   long int       reuse=-1,free=-1;   Slot_Mgr_Shr_t *shm;#ifdef PKCS64   Slot_Mgr_Proc_t_64  *procp;#else   Slot_Mgr_Proc_t  *procp;#endif   uint16         indx;   // Grab the Shared Memory MUTEX to prevent other updates to the   // SHM Process    // The registration is done to allow for future handling of   // the Slot Event List.  Which is maintained by the Slotd.      shm = Anchor->SharedMemP;   XProcLock(&(shm->slt_mutex));   procp = shm->proc_table;   for (indx=0;indx< NUMBER_PROCESSES_ALLOWED; indx++,procp++){      // Is the entry in use      if (procp->inuse == TRUE) {         // Handle the weird case of the process terminating without         // un-registering, and restarting with exactly the same PID          // before the slot manager garbage collection can performed.         // To eliminate the race condition between garbage collection         // the shm-slt_mutex will protect us.         // This should be a VERY rare (if ever) occurance, given the         // way AIX deals with re-allocation of PID;s, however if this         // ever gets ported over to another platform we want to deal         // with this accordingly since it may re-use pids differently          // (Linux appears to re-use pids more rapidly).         if ( procp->proc_id == getpid()) {            if ( reuse == -1 ) {               reuse = indx;            }         }      }else {         //Already found the first free         if (free == -1 ) {            free = indx;         }      }   }   // If we did not find a free entry then we fail the routine   if ( (reuse == -1) && (free == -1 ) ){      XProcUnLock(&(shm->slt_mutex));      return FALSE;   }   // check if we are reusing a control block or taking the first free.   // Since th mutex is helt, we don;t have to worry about some other   // process grabbing the slot...  Garbage collection from   // the slotd should not affect this since it will grab the mutex   // before doing its thing.   if (reuse != -1 ){      procp = &(shm->proc_table[reuse]);      indx = reuse;   } else {      procp = &(shm->proc_table[free]);      indx = free;   }#ifdef PKCS64   bzero((char *)procp,sizeof(Slot_Mgr_Proc_t_64));#else   bzero((char *)procp,sizeof(Slot_Mgr_Proc_t));#endif   procp->inuse = TRUE;   procp->proc_id = getpid();   procp->reg_time = time(NULL);   Anchor->MgrProcIndex=indx;   LOGIT(LOG_DEBUG,"API_Register MgrProcIndc %d  pid %d \n",procp->proc_id,Anchor->MgrProcIndex);   //??? What to do about the Mutex and cond variable   //Does initializing them in the slotd allow for them to not be    //initialized in the application.      XProcUnLock(&(shm->slt_mutex));   return TRUE;}// DeRegister the process with PKCSSLOTD in the// shared memory.// This call must be made with the API Global Mutex Locked// and the Anchor control block initialized with the // shared memory.  No checking for shared memory validity is donevoid API_UnRegister(){      Slot_Mgr_Shr_t *shm;#ifdef PKCS64   Slot_Mgr_Proc_t_64  *procp;#else   Slot_Mgr_Proc_t  *procp;#endif   uint16         indx;   // Grab the Shared Memory MUTEX to prevent other updates to the   // SHM Process    // The registration is done to allow for future handling of   // the Slot Event List.  Which is maintained by the Slotd.      shm = Anchor->SharedMemP;   XProcLock(&(shm->slt_mutex));   procp = &(shm->proc_table[Anchor->MgrProcIndex]);#ifdef PKCS64   bzero((char *)procp,sizeof(Slot_Mgr_Proc_t_64));#else   bzero((char *)procp,sizeof(Slot_Mgr_Proc_t));#endif   Anchor->MgrProcIndex=0;   //??? What to do about the Mutex and cond variable   //Does initializing them in the slotd allow for them to not be    //initialized in the application.   XProcUnLock(&(shm->slt_mutex));}voidDL_UnLoad( sltp,slotID )   API_Slot_t  *sltp;   CK_SLOT_ID  slotID;{   Slot_Mgr_Shr_t  *shm;#ifdef PKCS64   Slot_Info_t_64  *sinfp;#else   Slot_Info_t     *sinfp;#endif   shm = Anchor->SharedMemP;   sinfp = &(shm->slot_info[slotID]);   if ( sinfp->present == FALSE ){      return;   }   if (! sltp->dlop_p ){      return ;   }   // Call the routine to properly unload the DLL   DL_Unload(sltp);   return ;}intDL_Loaded( location, dllload)   char *location;   DLL_Load_t  *dllload;{   int i;   for (i=0;i< NUMBER_SLOTS_MANAGED;i++){      if ( dllload[i].dll_name != NULL){	 LOGIT(LOG_DEBUG,"DL_LOADED Looking for index %d name %s",i,dllload[i].dll_name);         if (strcmp(location,dllload[i].dll_name)== 0) {            return i; // Return the index of the dll         }      }   }   return -1; // Indicate failure to find the dll}intDL_Load( sinfp,sltp,dllload)#ifdef PKCS64   Slot_Info_t_64 *sinfp;#else   Slot_Info_t *sinfp;#endif   API_Slot_t  *sltp;   DLL_Load_t  *dllload;{   int i;   char  buffer[2048];   char  *path,*path2;   char *dname;   int plen; LOGIT(LOG_DEBUG,"DL_LOAD");   for (i=0;i<NUMBER_SLOTS_MANAGED;i++){      if (dllload[i].dll_name == NULL){         LOGIT(LOG_DEBUG,"Empty slot at %d ", i);         break;      }   }   if (i == NUMBER_SLOTS_MANAGED){  	LOGIT(LOG_DEBUG,"No empty slots ");	return 0; // Failed to find it..   }   dllload[i].dll_name = sinfp->dll_location;  // Point to the location   // allocate off the stack   path = alloca(strlen(sinfp->dll_location)+20);   path2 = alloca(strlen(sinfp->dll_location)+20);   if ( !path ) {        sltp->dlop_p = NULL;        return 0; // allocation failed   }   // Check for the name specified to be corre   sprintf(path,"%s",sinfp->dll_location);  // make a copy since dir name can change the memory   sprintf(path2,"%s/stdll",LIBLOCATION);  // make a copy since dir name can change the memory   dname = dirname(path);   if (strcmp(dname,path2) != 0 ) {        // Not in the location we expect        sltp->dlop_p = NULL;        return 0;   }   if ( sizeof(long) == 4 ) {      dllload[i].dlop_p = dlopen(sinfp->dll_location,RTLD_NOW);   } else {     // 64 bit env       sprintf(buffer,"%s64",sinfp->dll_location);      dllload[i].dlop_p = dlopen(buffer,RTLD_NOW);   }   if (dllload[i].dlop_p != NULL ){      sltp->dlop_p = dllload[i].dlop_p;      sltp->dll_information = &dllload[i];      dllload[i].dll_load_count=1;;   } else {      LOGIT(LOG_DEBUG,"\tDL_Load of %s failed, dlerror: %s",sinfp->dll_location,dlerror());      sltp->dlop_p = NULL;      return 0;   }   return 1;}int DL_Unload(sltp)   API_Slot_t  *sltp;{   DLL_Load_t  *dllload;   // Decrement the count of loads.  When 0 then unloadthis thing;   //   dllload = sltp->dll_information;   dllload->dll_load_count --;   if (dllload->dll_load_count == 0 ){// bug in 64bit causes crash of system...// check here for 64bit app	if (sizeof(long) == 4){	      dlclose(dllload->dlop_p);        }      dllload->dll_name = NULL;   }   // Clear out the slot information   sltp->DLLoaded = FALSE;   sltp->dlop_p = NULL;   sltp->pSTfini = NULL;   sltp->pSTcloseall = NULL;}intDL_Load_and_Init(sltp,slotID )   API_Slot_t  *sltp;   CK_SLOT_ID  slotID;{   Slot_Mgr_Shr_t  *shm;#ifdef PKCS64   Slot_Info_t_64  *sinfp;#else   Slot_Info_t     *sinfp;#endif   int            (*pSTinit)();   void            (*pSTfini)();   void            (*pSTcloseall)();   void             *dlp;   CK_RV            rv;   int            dll_len,dl_index;   DLL_Load_t  *dllload;   // Get pointer to shared memory from the anchor block   //   shm = Anchor->SharedMemP;   sinfp = &(shm->slot_info[slotID]);   dllload = Anchor->DLLs;   // list of dll's in the system   if ( sinfp->present == FALSE ){      return FALSE;   }   if ( (dll_len = strlen(sinfp->dll_location))){      // Check if this DLL has been loaded already.. If so, just increment      // the counter in the dllload structure and copy the data to      // the slot pointer.      if ( (dl_index = DL_Loaded(sinfp->dll_location,dllload)) != -1 ){         dllload[dl_index].dll_load_count++;         sltp->dll_information = &dllload[dl_index];         sltp->dlop_p = dllload[dl_index].dlop_p;      } else {         LOGIT(LOG_DEBUG,"DL_Load_and_Init dll_location %s",sinfp->dll_location);         DL_Load(sinfp,sltp,dllload);      }   } else {      return FALSE;   }   if (! sltp->dlop_p ){      LOGIT(LOG_DEBUG,"DL_Load_and_Init pointer %x",sltp->dlop_p);         return FALSE;   }   if (strlen(sinfp->slot_init_fcn)){      pSTinit = (int (*)())dlsym( sltp->dlop_p, sinfp->slot_init_fcn);   } else {      DL_Unload(sltp);      // dlclose(sltp->dlop_p);      return FALSE;   }   if (!pSTinit ){      // Unload the DLL      DL_Unload(sltp);      //dlclose(sltp->dlop_p);      return FALSE;   }   // Returns true or false   rv = pSTinit(&(sltp->FcnList),slotID,sinfp->correlator);   LOGIT(LOG_DEBUG,"return from STDDLL Init = %x",rv);      if (rv != CKR_OK ){      // clean up and unload      DL_Unload(sltp);      //dlclose(sltp->dlop_p);       sltp->DLLoaded=FALSE;      return FALSE;   } else {      sltp->DLLoaded=TRUE;      // Check if a SC_Finalize function has been exported      pSTfini = (void (*)())dlsym(sltp->dlop_p,"SC_Finalize");      sltp->pSTfini = pSTfini;      sltp->pSTcloseall = (void (*)())dlsym(sltp->dlop_p,"SC_CloseAllSessions");      return TRUE;   }   return TRUE;}voidst_err_log(int num, ...){      int n;      va_list pvar;      char *env;      char buffer[4096*4];   if (!enabled)  loginit();   if ( enabled ){         va_start(pvar, num);         vsprintf(buffer,err_msg[num].msg,pvar);         va_end(pvar);#if defined(AIX)         syslog_r(LOG_ERR,&log_data,buffer);#else         syslog(LOG_ERR,buffer);#endif   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?