📄 apiutil.c
字号:
int n; va_list pvar; char *env; char buffer[2048]; if ( enabled){ // Verify that the requested logging level is less than or // equal to that which is set at compile time. if ( type <= logging ){ va_start(pvar, fmt); vsprintf(buffer,fmt,pvar); va_end(pvar);#if defined(AIX) || defined(PTHREAD_SYSLOG) syslog_r(type,&log_data,buffer);#elif defined(LINUX) syslog(type,buffer);#endif } }}#if SYSVSEM#include <sys/sem.h>int Shm_Sem=-1; // system 5 shared memory semaphore...pthread_mutex_t semmtx = PTHREAD_MUTEX_INITIALIZER; // local mutex for semaphore functions...static struct sembuf xlock_lock[2]={ 0,0,0, 0,1,SEM_UNDO};static struct sembuf xlock_unlock[1] = { 0,-1,(IPC_NOWAIT | SEM_UNDO)};#endifintXProcLock(void *x){#if (AIX)#if PKCS64 return msem_lock(x,0);#else return pthread_mutex_lock(x);#endif#elif PTHREADXPL return pthread_mutex_lock(x);#elif POSIXSEM#error "posix semaphores need to be defined"#elif SYSVSEM#error "LINUX Code for sysvsem xproc lock needs to be done" #elif NOXPROCLOCK return CKR_OK;#elif SPINXPL if (xplfd == -1 ) { xplfd = open(XPL_FILE,O_CREAT|O_RDWR,S_IRWXU|S_IRWXG|S_IRWXO); } flock(xplfd,LOCK_EX); return CKR_OK;#else#error "XProcess locking needs to be defined"#endif}int XProcUnLock(void *x){#if (AIX)#if PKCS64 return msem_unlock(x,0);#else return pthread_mutex_unlock(x);#endif#elif PTHREADXPL return pthread_mutex_unlock(x);#elif POSIXSEM#error "posix semaphores need to be defined"#elif SYSVSEM#error "LINUX Code for sysvsem xproc lock needs to be done" #elif NOXPROCLOCK return CKR_OK;#elif SPINXPL flock(xplfd,LOCK_UN); return CKR_OK;#else#error "XProcess locking needs to be definec"#endif}voidAddToSessionList(pSess) Session_Struct_t *pSess;{ Session_Struct_t *pCur; pthread_mutex_lock(&(Anchor->SessListMutex)); //LOGIT(LOG_DEBUG,"AddToSessionList %x",pSess); pCur = Anchor->SessListBeg; if (!pCur ) { // first time to add //LOGIT(LOG_DEBUG,"\t\tFirst Addition"); pthread_mutex_lock(&(Anchor->ProcMutex)); Anchor->SessListBeg = pSess; pthread_mutex_unlock(&(Anchor->ProcMutex)); pSess->Previous = pSess->Next = NULL; } else { // Go to the end of the list.. while (pCur->Next != NULL ){ //LOGIT(LOG_DEBUG,"\t\tPcur = %x Next = %x",pCur,pCur->Next); pCur = pCur->Next; } // Append this one pCur->Next = pSess; pSess->Previous = pCur; pSess->Next = NULL; } pthread_mutex_unlock(&(Anchor->SessListMutex));}voidRemoveFromSessionList(pSess) Session_Struct_t *pSess;{ Session_Struct_t *pCur,*pTmp; pthread_mutex_lock(&(Anchor->SessListMutex)); pCur = Anchor->SessListBeg; // Just in case check that there really is a list although // the call to ValidSession should have caught this already. // But someone may have removed the session already // while we were validating the call. if ( pCur) { // Are we the beginning of the list if (pCur == pSess){ pthread_mutex_lock(&(Anchor->ProcMutex)); pTmp = pSess->Next; Anchor->SessListBeg = pSess->Next; if (pTmp){ pTmp->Previous = NULL; } free(pSess); pthread_mutex_unlock(&(Anchor->ProcMutex)); pCur = NULL; // Force out of the loop } else { // First check for a Null element then check next against // the desired element. This will allow the loop to terminate // at the end of the list even if the desired element is not in // the list (should not happen, but be defensive). while (pCur && pCur->Next != pSess) { pCur = pCur->Next; } // We did not hit the end of the list without finding // our element so we can continue to remove it if (pCur != NULL ){ pTmp = pSess->Next; pCur->Next = pTmp; if (pTmp) { pTmp->Previous = pCur; } free(pSess); } } } pthread_mutex_unlock(&(Anchor->SessListMutex));}intValid_Session(pSession,rSession) Session_Struct_t *pSession; ST_SESSION_T *rSession;{ int rv=FALSE; // Assume that it is not on the list Session_Struct_t *cSessionp; if ( !pSession ) return FALSE; // Walk the Anchor block session linked list // return TRUE if the pointer is on the list // False if it is not pthread_mutex_lock(&(Anchor->SessListMutex)); cSessionp = Anchor->SessListBeg; while (cSessionp) { if (cSessionp == pSession){ rv = TRUE; rSession->sessionh = pSession->RealHandle; rSession->slotID = pSession->SltId; break; } cSessionp = (Session_Struct_t *)cSessionp->Next; } pthread_mutex_unlock(&(Anchor->SessListMutex)); return rv;}int API_Initialized(){ if ( Anchor == NULL ) return FALSE;#if !(LINUX) if ( Anchor->Pid == getpid() ){ return TRUE; } else { return FALSE; }#endif return TRUE;}intslot_present(id) CK_SLOT_ID id;{ Slot_Mgr_Shr_t *shm;#ifdef PKCS64 Slot_Info_t_64 *sinfp;#else Slot_Info_t *sinfp;#endif // Get pointer to shared memory from the anchor block // shm = Anchor->SharedMemP; sinfp = &(shm->slot_info[id]); if ( sinfp->present == FALSE ){ return FALSE; } return TRUE;}intincr_sess_counts(slotID) CK_SLOT_ID slotID;{ Slot_Mgr_Shr_t *shm;#ifdef PKCS64 Slot_Info_t_64 *sinfp; Slot_Mgr_Proc_t_64 *procp;#else Slot_Info_t *sinfp; Slot_Mgr_Proc_t *procp;#endif // Get the slot mutex shm = Anchor->SharedMemP; XProcLock(&(shm->slt_mutex)); sinfp = &(shm->slot_info[slotID]); sinfp->global_sessions++; procp = &shm->proc_table[Anchor->MgrProcIndex]; procp->slot_session_count[slotID]++; XProcUnLock(&(shm->slt_mutex)); }intdecr_sess_counts(slotID) CK_SLOT_ID slotID;{ Slot_Mgr_Shr_t *shm;#ifdef PKCS64 Slot_Info_t_64 *sinfp; Slot_Mgr_Proc_t_64 *procp;#else Slot_Info_t *sinfp; Slot_Mgr_Proc_t *procp;#endif // Get the slot mutex shm = Anchor->SharedMemP; XProcLock(&(shm->slt_mutex)); sinfp = &(shm->slot_info[slotID]); if (sinfp->global_sessions > 0){ sinfp->global_sessions--; } procp = &shm->proc_table[Anchor->MgrProcIndex]; if (procp->slot_session_count[slotID] > 0){ procp->slot_session_count[slotID]++; } XProcUnLock(&(shm->slt_mutex));}// Check if any sessions from other applicaitons exist on this particular// token.... This will also validate our own sessions as well.// There might be an issue with the fact that a session is created but the// number is not incremented until the session allocation is completed by// the token. The API may need to lock the shared memory prior to creating// the session and then unlock when the stdll has completed its work.// Closing sessions should probably behave the same way.intsessions_exist(slotID) CK_SLOT_ID slotID;{ Slot_Mgr_Shr_t *shm;#ifdef PKCS64 Slot_Info_t_64 *sinfp; Slot_Mgr_Proc_t_64 *procp;#else Slot_Info_t *sinfp; Slot_Mgr_Proc_t *procp;#endif // Get the slot mutex shm = Anchor->SharedMemP;#ifdef PKCS64 XProcLock(&(shm->slt_mutex)); sinfp = &(shm->slot_info[slotID]); if (sinfp->global_sessions == 0) { XProcUnLock(&(shm->slt_mutex)); return FALSE; } XProcUnLock(&(shm->slt_mutex));#else XProcLock(&(shm->slt_mutex)); sinfp = &(shm->slot_info[slotID]); if (sinfp->global_sessions == 0) { XProcUnLock(&(shm->slt_mutex)); return FALSE; } XProcUnLock(&(shm->slt_mutex));#endif return TRUE;}voidunlock_shm(){ Slot_Mgr_Shr_t *shm; shm = Anchor->SharedMemP; XProcUnLock(&(shm->slt_mutex));}voidlock_shm(){ Slot_Mgr_Shr_t *shm; shm = Anchor->SharedMemP; XProcLock(&(shm->slt_mutex));}// Terminates all sessions associated with a given process// this cleans up any lingering sessions with the process// and does not //// It is only called from the C_Finalize routinevoidTerminate_All_Process_Sessions(){ CK_SLOT_ID id; CK_RV rv; LOGIT(LOG_DEBUG,"Terminate_All_Process_Sessions"); for (id=0;id<NUMBER_SLOTS_MANAGED;id++){ // Check if the slot is present in the slot manager // if not just skip it... if (slot_present(id) == TRUE) { rv = C_CloseAllSessions(id); } else { continue; } // If the return code is not OK, we are really hosed // since we are terminating the session. // For now we will just log it if (rv != CKR_OK){ LOGIT(LOG_DEBUG,"Terminate_All_Process_Sessions RV %x",rv); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -