if (crv == CKR_SESSION_COUNT) { PK11_GETTAB(slot)->C_CloseSession(slot->session); slot->session = CK_INVALID_SESSION; crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID, CKF_RW_SESSION|CKF_SERIAL_SESSION, slot,pk11_notify,&rwsession); } if (crv != CKR_OK) {
PORT_Assert(rwsession != CK_INVALID_SESSION || crv != CKR_OK); if (crv != CKR_OK || rwsession == CK_INVALID_SESSION) { if (crv == CKR_OK) crv = CKR_DEVICE_ERROR; if (haveMonitor) PK11_ExitSlotMonitor(slot);
CK_SESSION_HANDLE PK11_GetRWSession(PK11SlotInfo *slot){    CK_SESSION_HANDLE rwsession;    CK_RV crv;    if (!slot->isThreadSafe || slot->defRWSession) PK11_EnterSlotMonitor(slot);    if (slot->defRWSession) return slot->session;    crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID,				CKF_RW_SESSION|CKF_SERIAL_SESSION,				  	  slot, pk11_notify,&rwsession);    if (crv == CKR_SESSION_COUNT) {	PK11_GETTAB(slot)->C_CloseSession(slot->session);	slot->session = CK_INVALID_SESSION;    	crv = PK11_GETTAB(slot)->C_OpenSession(slot->slotID,				CKF_RW_SESSION|CKF_SERIAL_SESSION,				  	  slot,pk11_notify,&rwsession);    }    if (crv != CKR_OK) {	PORT_SetError(PK11_MapError(crv));	if (slot->session == CK_INVALID_SESSION) {    	    PK11_GETTAB(slot)->C_OpenSession(slot->slotID,CKF_SERIAL_SESSION,				  	 slot,pk11_notify,&slot->session);	}	if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot);	return CK_INVALID_SESSION;    }    return rwsession;}