| 1001 | } |
| 1002 | |
| 1003 | void CryptoManager::cryptThreadRoutine() |
| 1004 | { |
| 1005 | FbLocalStatus status_vector; |
| 1006 | bool lckRelease = false; |
| 1007 | |
| 1008 | try |
| 1009 | { |
| 1010 | // Try to take crypt mutex |
| 1011 | // If can't take that mutex - nothing to do, cryptThread already runs in our process |
| 1012 | MutexEnsureUnlock guard(cryptThreadMtx, FB_FUNCTION); |
| 1013 | if (!guard.tryEnter()) |
| 1014 | { |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | // Establish temp context needed to take crypt thread lock |
| 1019 | ThreadContextHolder tempDbb(&dbb, nullptr, &status_vector); |
| 1020 | |
| 1021 | // Take exclusive threadLock |
| 1022 | // If can't take that lock - nothing to do, cryptThread already runs somewhere |
| 1023 | if (!LCK_lock(tempDbb, threadLock, LCK_EX, LCK_NO_WAIT)) |
| 1024 | return; |
| 1025 | |
| 1026 | try |
| 1027 | { |
| 1028 | // Set running flag |
| 1029 | AutoSetRestore<bool> runFlag(&run, true); |
| 1030 | |
| 1031 | // Establish context |
| 1032 | // Need real attachment in order to make classic mode happy |
| 1033 | ClumpletWriter writer(ClumpletReader::dpbList, MAX_DPB_SIZE); |
| 1034 | writer.insertString(isc_dpb_user_name, DBA_USER_NAME); |
| 1035 | writer.insertByte(isc_dpb_no_db_triggers, TRUE); |
| 1036 | writer.insertInt(isc_dpb_worker_attach, 1); |
| 1037 | |
| 1038 | // Avoid races with release_attachment() in jrd.cpp |
| 1039 | XThreadEnsureUnlock releaseGuard(dbb.dbb_thread_mutex, FB_FUNCTION); |
| 1040 | releaseGuard.enter(); |
| 1041 | |
| 1042 | if (!down()) |
| 1043 | { |
| 1044 | AutoPlugin<JProvider> jInstance(JProvider::getInstance()); |
| 1045 | jInstance->setDbCryptCallback(&status_vector, dbb.dbb_callback); |
| 1046 | check(&status_vector); |
| 1047 | |
| 1048 | RefPtr<JAttachment> jAtt(REF_NO_INCR, jInstance->attachDatabase(&status_vector, |
| 1049 | dbb.dbb_database_name.c_str(), writer.getBufferLength(), writer.getBuffer())); |
| 1050 | check(&status_vector); |
| 1051 | |
| 1052 | AttSyncLockGuard attGuard(*(jAtt->getStable()->getSync()), FB_FUNCTION); |
| 1053 | Attachment* att = jAtt->getHandle(); |
| 1054 | if (!att) |
| 1055 | Arg::Gds(isc_att_shutdown).raise(); |
| 1056 | att->att_flags |= ATT_from_thread; |
| 1057 | releaseGuard.leave(); |
| 1058 | |
| 1059 | ThreadContextHolder tdbb(att->att_database, att, &status_vector); |
| 1060 | tdbb->markAsSweeper(); |
no test coverage detected