inline void MySqlDataStore::Open() { if(m_openState.SetOpening()) { return; } try { Beam::Threading::With(m_readerDatabaseConnection, [&] (mysqlpp::Connection& connection) { OpenDatabaseConnection(connection); if(!Details::LoadTables(connection, m_schema)) { BOOST_THROW_EXCEPTION(IO::ConnectException{ "Unable to load database tables."}); } }); Beam::Threading::With(m_writerDatabaseConnection, [&] (mysqlpp::Connection& connection) { OpenDatabaseConnection(connection); if(!Details::LoadTables(connection, m_schema)) { BOOST_THROW_EXCEPTION(IO::ConnectException{ "Unable to load database tables."}); } }); for(std::size_t i = 0; i <= boost::thread::hardware_concurrency(); ++i) { auto connection = std::make_unique<mysqlpp::Connection>(false); OpenDatabaseConnection(*connection); m_readerDatabaseConnectionPool.Add(std::move(connection)); } } catch(const std::exception&) { m_openState.SetOpenFailure(); Shutdown(); } m_openState.SetOpen(); }
//TODO: Document me int InsertModulesToMeasureForPolling(vector<string> * v){ char query[200]; char * fixedInsertStr; unsigned int len; int holder; MYSQL * conn; if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("InsertModulesToMeasureForPolling: Couldn't acquire mutex. Returning\n"); __asm{int 3}; //free outstanding resources v->clear(); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("InsertModulesToMeasureForPolling: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// for(unsigned int i=0; i < v->size(); i++){ string temp = v->at(i); fixedInsertStr = "INSERT INTO ModulesToMeasure(moduleName) VALUES ('%s')"; len = sprintf_s(query, 200, fixedInsertStr, temp.c_str()); if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); if(holder != INSERT_ERROR_DUPLICATE){ //allow duplicates v->clear(); ReleaseMutex(mysqlMutex); CloseDatabaseConnection(conn); printf("InsertModulesToMeasureForPolling: Couldn't release mutex. Returning\n"); return GENERIC_ERROR; } else{ printf("\n\n%s is already in ModulesToMeasure and we don't insert duplicates.\n\n", temp.c_str()); } } } ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("InsertModulesToMeasureForPolling: Couldn't release mutex. Returning\n"); //free outstanding resources v->clear(); __asm{int 3}; return GENERIC_ERROR; } //We don't want the vector to have any entries when we're done with it. v->clear(); return GENERIC_SUCCESS; }
int InsertExports(unsigned int loadedModuleID, vector<export_t *> * v){ char query[300]; char * fixedStr; unsigned int len, holder; export_t * tmp; MYSQL * conn; if(WaitForSingleObject(mysqlMutex,INFINITE) != WAIT_OBJECT_0){ printf("InsertExports: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffTiresiasDBName); if(conn == NULL){ printf("InsertExports: OpenDatabaseConnection(gffTiresiasDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex,GENERIC_ERROR); } ////////////////////////////////// for(unsigned int i=0; i < v->size(); i++){ tmp = v->at(i); //Skip anything that's a special case and therefore the rva was set to 0 //(like the VideoPortCheckForDeviceExistance->VIDEOPRT.VideoPortCheckForDeviceExistance) if(tmp->rva == 0) continue; fixedStr = "INSERT INTO Exports(rva,name,ordinal,loadedModuleID) VALUES (%u,'%s',%u,%u)"; len = sprintf_s(query, 300, fixedStr, tmp->rva, tmp->name, tmp->ordinal, loadedModuleID); if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); if(holder != INSERT_ERROR_DUPLICATE){ //allow duplicates CloseDatabaseConnection(conn); ReleaseMutex(mysqlMutex); printf("InsertModulesToMeasureForPolling: Couldn't release mutex. Returning\n"); return GENERIC_ERROR; } } } ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("InsertModulesToMeasureForPolling: Couldn't release mutex. Returning\n"); //free outstanding resources //v->clear(); __asm{int 3}; return GENERIC_ERROR; } return GENERIC_SUCCESS; }
int InsertServerPrerequisiteForMessageType(int msgType, vector<string> * v){ char query[200]; char * fixedInsertStr; unsigned int len; int holder; char * cTemp; MYSQL * conn; if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("InsertServerPrerequisiteForMessageType: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("InsertServerPrerequisiteForMessageType: OpenDatabaseConnection(gffServerDBName) failed\n"); v->clear(); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } //For each host to measure in the vector for(unsigned int i=0; i < v->size(); i++){ string temp = v->at(i); cTemp = (char *)temp.c_str(); fixedInsertStr = "INSERT INTO TableVerificationPrerequisiteModules VALUES(NULL,%i,'%s')"; len = sprintf_s(query, 200, fixedInsertStr, msgType, cTemp); ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); // if error code is 1062, the entry already exists, so it's ok if(holder != 1062){ CloseDatabaseConnection(conn); v->clear(); return ReleaseMutexAndReturnError(mysqlMutex, holder); } } } CloseDatabaseConnection(conn); v->clear(); ////////////////////////////////// if(!ReleaseMutex(mysqlMutex)){ printf("InsertServerPrerequisiteForMessageType: Couldn't release mutex. Returning\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
nsresult DOMStorageDBThread::InitDatabase() { Telemetry::AutoTimer<Telemetry::LOCALDOMSTORAGE_INIT_DATABASE_MS> timer; nsresult rv; // Here we are on the worker thread. This opens the worker connection. MOZ_ASSERT(!NS_IsMainThread()); rv = OpenDatabaseConnection(); NS_ENSURE_SUCCESS(rv, rv); rv = TryJournalMode(); NS_ENSURE_SUCCESS(rv, rv); // Create a read-only clone (void)mWorkerConnection->Clone(true, getter_AddRefs(mReaderConnection)); NS_ENSURE_TRUE(mReaderConnection, NS_ERROR_FAILURE); rv = DOMStorageDBUpdater::Update(mWorkerConnection); NS_ENSURE_SUCCESS(rv, rv); // Database open and all initiation operation are done. Switching this flag // to true allow main thread to read directly from the database. // If we would allow this sooner, we would have opened a window where main thread // read might operate on a totaly broken and incosistent database. mDBReady = true; // List scopes having any stored data nsCOMPtr<mozIStorageStatement> stmt; // Note: result of this select must match DOMStorageManager::CreateOrigin() rv = mWorkerConnection->CreateStatement(NS_LITERAL_CSTRING( "SELECT DISTINCT originAttributes || ':' || originKey FROM webappsstore2"), getter_AddRefs(stmt)); NS_ENSURE_SUCCESS(rv, rv); mozStorageStatementScoper scope(stmt); bool exists; while (NS_SUCCEEDED(rv = stmt->ExecuteStep(&exists)) && exists) { nsAutoCString foundOrigin; rv = stmt->GetUTF8String(0, foundOrigin); NS_ENSURE_SUCCESS(rv, rv); MonitorAutoLock monitor(mThreadObserver->GetMonitor()); mOriginsHavingData.PutEntry(foundOrigin); } return NS_OK; }
nsresult StorageDBThread::OpenAndUpdateDatabase() { nsresult rv; // Here we are on the worker thread. This opens the worker connection. MOZ_ASSERT(!NS_IsMainThread()); rv = OpenDatabaseConnection(); NS_ENSURE_SUCCESS(rv, rv); rv = TryJournalMode(); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; }
int InsertExpectedBinarySymbols(vector<expected_symbol_t> * v){ char query[300]; char * fixedStr; unsigned int len, holder; expected_symbol_t tmp; MYSQL * conn; if(WaitForSingleObject(mysqlMutex,INFINITE) != WAIT_OBJECT_0){ printf("InsertExpectedBinarySymbols: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffTiresiasDBName); if(conn == NULL){ printf("InsertExpectedBinarySymbols: OpenDatabaseConnection(gffTiresiasDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex,GENERIC_ERROR); } ////////////////////////////////// for(unsigned int i=0; i < v->size(); i++){ tmp = v->at(i); fixedStr = "INSERT INTO ExpectedBinarySymbols(symbolName,moduleName) VALUES ('%s','%s')"; len = sprintf_s(query, 300, fixedStr, tmp.symbolName, tmp.moduleName); if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); if(holder != INSERT_ERROR_DUPLICATE){ //allow duplicates CloseDatabaseConnection(conn); ReleaseMutex(mysqlMutex); printf("InsertExpectedBinarySymbols: Couldn't release mutex. Returning\n"); return GENERIC_ERROR; } } } ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("InsertExpectedBinarySymbols: Couldn't release mutex. Returning\n"); __asm{int 3}; return GENERIC_ERROR; } return GENERIC_SUCCESS; }
int InsertResolvedBinarySymbols(vector<resolved_symbol_t *> * v){ char query[300]; char * fixedStr; unsigned int len, holder; resolved_symbol_t *resolved_entry; MYSQL * conn; if(WaitForSingleObject(mysqlMutex,INFINITE) != WAIT_OBJECT_0){ printf("InsertResolvedBinarySymbols: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffTiresiasDBName); if(conn == NULL){ printf("InsertResolvedBinarySymbols: OpenDatabaseConnection(gffTiresiasDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex,GENERIC_ERROR); } ////////////////////////////////// for(unsigned int i=0; i < v->size(); i++){ resolved_entry = v->at(i); fixedStr = "INSERT INTO ResolvedBinarySymbols(name,rva,binID,hostID) VALUES ('%s',%u,%u,%u)"; len = sprintf_s(query, 300, fixedStr, resolved_entry->symbolName, resolved_entry->rva, resolved_entry->binID, resolved_entry->hostID); if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); if(holder != INSERT_ERROR_DUPLICATE){ //allow duplicates CloseDatabaseConnection(conn); ReleaseMutex(mysqlMutex); printf("InsertResolvedBinarySymbols: Mysql Error code: %d\n",holder); return GENERIC_ERROR; } } } ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("InsertResolvedBinarySymbols: Couldn't release mutex. Returning\n"); __asm{int 3}; return GENERIC_ERROR; } return GENERIC_SUCCESS; }
int SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp(self_check_t * selfCheckInput, self_check_t * selfCheckTiresias){ MYSQL_RES * results; MYSQL_ROW row; //really a char ** unsigned int numRows = 0, numFields = 0; char query[200]; unsigned int len; char * fixedSelectStr; MYSQL * conn; fixedSelectStr = "SELECT * FROM Checksums WHERE nonce = %#x AND timeDateStamp = %#x AND baseVA = %#x"; len = sprintf_s(query, 200, fixedSelectStr, selfCheckInput->nonce, selfCheckInput->timeDateStamp, selfCheckInput->baseVA); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffTiresiasDBName); if(conn == NULL){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: OpenDatabaseConnection(gffTiresiasDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: mysql_real_query failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){//Will fail here if an empty set is returned mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } //There should only ever be one correct checksum value //for a given combination of nonce/timeDateStamp/baseVA if(numRows > 1){ mysql_free_result(results); printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: More than one checksum found for the nonce/timeDateStamp/baseVA combination. Check tiresias database\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, TOO_MANY_RESULTS_FOUND); } row = mysql_fetch_row(results); if(row == NULL){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numFields = mysql_num_fields(results); if(numFields != (4 + SELF_CHECKSUM_SIZE)){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: mysql_num_fields says we have the wrong number of fields\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } selfCheckTiresias->nonce = selfCheckInput->nonce; selfCheckTiresias->timeDateStamp = selfCheckInput->timeDateStamp; selfCheckTiresias->baseVA = selfCheckInput->baseVA; for(unsigned int i = 0; i < SELF_CHECKSUM_SIZE; i++){ selfCheckTiresias->checksum[i] = strtoul(row[4+i], NULL, 10); } mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectTiresiasChecksumByNonceAndBaseAndTimedatestamp: Couldn't release mutex. Exiting thread\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
//TODO: Document me int SelectAllHostsToMeasureForPolling(int tolerance, queue<int> * q){ int hostID; MYSQL_RES * results; MYSQL_ROW row; unsigned int len; int holder; unsigned int numRows = 0, numFields = 0; char query[200]; MYSQL * conn; time_t rawtime; time (&rawtime); unsigned int tempTimeStamp = (unsigned int)rawtime; tempTimeStamp = tempTimeStamp - (unsigned int)tolerance; if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectAllHostsToMeasureForPolling: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("SelectAllHostsToMeasureForPolling: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } len = sprintf_s(query, 200, "SELECT hostID FROM HostsToMeasure WHERE readiness = %u and lastTimeMeasurementRequested < %u",READY, tempTimeStamp); ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, holder); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectAllHostsToMeasureForPolling: mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){//Will fail here if an empty set is returned mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } //This function expects that the results will be a single column of integers (IDs) for(unsigned int i = 0; i < numRows; i++){ row = mysql_fetch_row(results); if(row == NULL){ printf("SelectAllHostsToMeasureForPolling: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } hostID = strtoul(row[0], NULL, 10); q->push(hostID); } mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectAllHostsToMeasureForPolling: Couldn't release mutex. Exiting thread\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
//Document me int SelectModulesToMeasure(vector<string> * v){ MYSQL_RES * results; MYSQL_ROW row; char * fixedInsertStr; unsigned int len; int holder; unsigned int numRows = 0, numFields = 0; MYSQL * conn; fixedInsertStr = "SELECT moduleName FROM ModulesToMeasure"; len = strlen(fixedInsertStr); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectModulesToMeasure: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("SelectModulesToMeasure: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, fixedInsertStr, len)){ holder = CheckError(conn,mysql_errno(conn)); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, holder); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectModulesToMeasure: mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){ mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } //This function expects that the results will be a single column of wstrings for(unsigned int i = 0; i < numRows; i++){ row = mysql_fetch_row(results); if(row == NULL){ printf("SelectModulesToMeasure: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } string temp = (string)row[0]; v->push_back(temp); } mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectModulesToMeasure: Couldn't release mutex. Exiting thread\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
int SelectAllHostsToMeasure(queue<string> * q, queue<int> * r){ MYSQL_RES * results; MYSQL_ROW row; //really a char ** string hostIP; int hostID; char * endptr = ""; char * fixedInsertStr; unsigned int len; int holder; unsigned int numRows = 0, numFields = 0; MYSQL * conn; fixedInsertStr = "SELECT Hosts.id, Hosts.ip_str FROM Hosts INNER JOIN HostsToMeasure WHERE Hosts.id = HostsToMeasure.hostID"; len = strlen(fixedInsertStr); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectAllHostsToMeasure: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("SelectAllHostsToMeasure: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, fixedInsertStr, len)){ holder = CheckError(conn,mysql_errno(conn)); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, holder); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectAllHostsToMeasure: mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){//Will fail here if an empty set is returned mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } //This function expects that the results will be a single column of integers (IDs) for(unsigned int i = 0; i < numRows; i++){ row = mysql_fetch_row(results); if(row == NULL){ printf("SelectAllHostsToMeasure: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } hostID = strtoul(row[0], &endptr, 10); r->push(hostID); q->push(row[1]); } holder = GENERIC_SUCCESS; mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectAllHostsToMeasure: Couldn't release mutex. Exiting thread\n"); return GENERIC_ERROR; } return holder; }
int SelectDataSetForLimitCalculations(unsigned int countFromTimeInSeconds, unsigned int count, queue<unsigned int> * q){ MYSQL_RES * results; MYSQL_ROW row; char query[300]; char * fixedInsertStr; unsigned int len; MYSQL * conn; unsigned int counter = 0; fixedInsertStr = "SELECT meRTTInMicroseconds FROM MeasurementEvents WHERE type = %d and timeInSeconds > %d"; len = sprintf_s(query, 300, fixedInsertStr, SELF_ATTESTATION, countFromTimeInSeconds); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectDataSetForLimitCalculations: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("SelectDataSetForLimitCalculations: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ printf("mysql_real_query() failed in SelectDataSetForLimitCalculations\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectDataSetForLimitCalculations: mysql_store_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } if(mysql_num_rows(results) == 0){ mysql_free_result(results); printf("SelectDataSetForLimitCalculations: mysql_num_rows returned no results\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } while(counter < count){ row = mysql_fetch_row(results); if(row == NULL){ printf("SelectDataSetForLimitCalculations: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } q->push((unsigned int)(atoi(row[0]))); counter++; } ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectDataSetForLimitCalculations: Couldn't release mutex.\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
int SelectUnfinishedPendingSelfAttestationMeIds(unsigned int timeLimit, vector<int> * v){ MYSQL_RES * results; MYSQL_ROW row; unsigned int len; int holder; unsigned int numRows = 0; MYSQL * conn; char query[400]; len = sprintf_s(query, 400, "SELECT id FROM pendingmeasurementevents WHERE timeInSeconds < %u AND type = 0", timeLimit); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectUnfinishedPendingSelfAttestationMeIds: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("SelectUnfinishedPendingSelfAttestationMeIds: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, holder); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectUnfinishedPendingSelfAttestationMeIds: mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){ mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } for(unsigned int i = 0; i < numRows; i++){ row = mysql_fetch_row(results); if(row == NULL){ printf("SelectUnfinishedPendingSelfAttestationMeIds: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } unsigned int temp = strtoul(row[0], NULL, 10); v->push_back(temp); } mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectUnfinishedPendingSelfAttestationMeIds: Couldn't release mutex. Exiting thread\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
int CleanUpStaleMatchingPendingEvents(me_t * pendingME){ char query[256]; char * fixedInsertStr; unsigned int len; unsigned int numRows; int holder; MYSQL * conn; MYSQL_RES * results; MYSQL_ROW row; fixedInsertStr = "SELECT id FROM PendingMeasurementEvents WHERE type = %d AND timeInSeconds = %d AND hostID = %d AND overloadedForeignID = %d AND id <> %d AND timeInSeconds < %d AND nonce = %d"; len = sprintf_s(query, 256, fixedInsertStr, pendingME->type, pendingME->timeInSeconds, pendingME->hostID,pendingME->overloadedForeignID,pendingME->id,pendingME->timeInSeconds,pendingME->nonce); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("CleanUpStaleMatchingPendingEvents: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("CleanUpStaleMatchingPendingEvents: OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, holder); } results = mysql_store_result(conn); if(results == NULL){ printf("CleanUpStaleMatchingPendingEvents: mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){ mysql_free_result(results); CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("CleanUpStaleMatchingPendingEvents: Couldn't release mutex.\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; } for(unsigned int i = 0; i < numRows; i++){ row = mysql_fetch_row(results); if(row == NULL){ printf("CleanUpStaleMatchingPendingEvents: mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } unsigned int temp = (unsigned int)row[0]; fixedInsertStr = "DELETE FROM PendingMeasurementEvents WHERE id = %d"; len = sprintf_s(query, 100, fixedInsertStr, temp); //if(0 != mysql_real_query(conn, query, len)){ // holder = CheckError(conn,mysql_errno(conn)); // CloseDatabaseConnection(conn); // return ReleaseMutexAndReturnError(mysqlMutex, holder); //} printf("CleanUpStaleMatchingPendingEvents: delete sql = %s\n",query); } mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("CleanUpStaleMatchingPendingEvents: Couldn't release mutex.\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }
nsresult DOMStorageDBThread::InitDatabase() { Telemetry::AutoTimer<Telemetry::LOCALDOMSTORAGE_INIT_DATABASE_MS> timer; nsresult rv; // Here we are on the worker thread. This opens the worker connection. MOZ_ASSERT(!NS_IsMainThread()); rv = OpenDatabaseConnection(); NS_ENSURE_SUCCESS(rv, rv); rv = TryJournalMode(); NS_ENSURE_SUCCESS(rv, rv); // Create a read-only clone (void)mWorkerConnection->Clone(true, getter_AddRefs(mReaderConnection)); NS_ENSURE_TRUE(mReaderConnection, NS_ERROR_FAILURE); mozStorageTransaction transaction(mWorkerConnection, false); // Ensure Goanna 1.9.1 storage table rv = mWorkerConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE TABLE IF NOT EXISTS webappsstore2 (" "scope TEXT, " "key TEXT, " "value TEXT, " "secure INTEGER, " "owner TEXT)")); NS_ENSURE_SUCCESS(rv, rv); rv = mWorkerConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING( "CREATE UNIQUE INDEX IF NOT EXISTS scope_key_index" " ON webappsstore2(scope, key)")); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<mozIStorageFunction> function1(new nsReverseStringSQLFunction()); NS_ENSURE_TRUE(function1, NS_ERROR_OUT_OF_MEMORY); rv = mWorkerConnection->CreateFunction(NS_LITERAL_CSTRING("REVERSESTRING"), 1, function1); NS_ENSURE_SUCCESS(rv, rv); bool exists; // Check if there is storage of Goanna 1.9.0 and if so, upgrade that storage // to actual webappsstore2 table and drop the obsolete table. First process // this newer table upgrade to priority potential duplicates from older // storage table. rv = mWorkerConnection->TableExists(NS_LITERAL_CSTRING("webappsstore"), &exists); NS_ENSURE_SUCCESS(rv, rv); if (exists) { rv = mWorkerConnection->ExecuteSimpleSQL( NS_LITERAL_CSTRING("INSERT OR IGNORE INTO " "webappsstore2(scope, key, value, secure, owner) " "SELECT REVERSESTRING(domain) || '.:', key, value, secure, owner " "FROM webappsstore")); NS_ENSURE_SUCCESS(rv, rv); rv = mWorkerConnection->ExecuteSimpleSQL( NS_LITERAL_CSTRING("DROP TABLE webappsstore")); NS_ENSURE_SUCCESS(rv, rv); } // Check if there is storage of Goanna 1.8 and if so, upgrade that storage // to actual webappsstore2 table and drop the obsolete table. Potential // duplicates will be ignored. rv = mWorkerConnection->TableExists(NS_LITERAL_CSTRING("moz_webappsstore"), &exists); NS_ENSURE_SUCCESS(rv, rv); if (exists) { rv = mWorkerConnection->ExecuteSimpleSQL( NS_LITERAL_CSTRING("INSERT OR IGNORE INTO " "webappsstore2(scope, key, value, secure, owner) " "SELECT REVERSESTRING(domain) || '.:', key, value, secure, domain " "FROM moz_webappsstore")); NS_ENSURE_SUCCESS(rv, rv); rv = mWorkerConnection->ExecuteSimpleSQL( NS_LITERAL_CSTRING("DROP TABLE moz_webappsstore")); NS_ENSURE_SUCCESS(rv, rv); } rv = transaction.Commit(); NS_ENSURE_SUCCESS(rv, rv); // Database open and all initiation operation are done. Switching this flag // to true allow main thread to read directly from the database. // If we would allow this sooner, we would have opened a window where main thread // read might operate on a totaly broken and incosistent database. mDBReady = true; // List scopes having any stored data nsCOMPtr<mozIStorageStatement> stmt; rv = mWorkerConnection->CreateStatement(NS_LITERAL_CSTRING("SELECT DISTINCT scope FROM webappsstore2"), getter_AddRefs(stmt)); NS_ENSURE_SUCCESS(rv, rv); mozStorageStatementScoper scope(stmt); while (NS_SUCCEEDED(rv = stmt->ExecuteStep(&exists)) && exists) { nsAutoCString foundScope; rv = stmt->GetUTF8String(0, foundScope); NS_ENSURE_SUCCESS(rv, rv); MonitorAutoLock monitor(mThreadObserver->GetMonitor()); mScopesHavingData.PutEntry(foundScope); } return NS_OK; }
int SelectHostIPsByHostName(char * hostName, list<string> * v){ char query[300]; char * fixedInsertStr; unsigned int len; int holder; MYSQL_RES * results; MYSQL_ROW row; unsigned int numRows = 0, numFields = 0; MYSQL * conn; if(hostName == NULL || strlen(hostName) == 0){ printf("SelectHostIPsByHostName(): hostName passed was null\n"); return GENERIC_ERROR; } fixedInsertStr = "SELECT ip_str FROM Hosts WHERE hostName like '%s'"; len = sprintf_s(query, 300, fixedInsertStr, hostName); if(WaitForSingleObject(mysqlMutex, INFINITE) != WAIT_OBJECT_0){ printf("SelectHostIPsByHostName: Couldn't acquire mutex. Returning\n"); return GENERIC_ERROR; } conn = OpenDatabaseConnection(gffServerDBName); if(conn == NULL){ printf("SelectHostIPsByHostName(): OpenDatabaseConnection(gffServerDBName) failed\n"); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } ////////////////////////////////// if(0 != mysql_real_query(conn, query, len)){ holder = CheckError(conn,mysql_errno(conn)); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, holder); } results = mysql_store_result(conn); if(results == NULL){ printf("SelectHostIPsByHostName(): mysql_use_result failed\n"); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } numRows = (unsigned int)mysql_num_rows(results); if(numRows == 0){ mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, NO_RESULTS_FOUND); } //This function expects that the results will be a single column of strings for(unsigned int i = 0; i < numRows; i++){ row = mysql_fetch_row(results); if(row == NULL){ printf("SelectHostIPsByHostName(): mysql_fetch_row failed\n"); mysql_free_result(results); CloseDatabaseConnection(conn); return ReleaseMutexAndReturnError(mysqlMutex, GENERIC_ERROR); } string temp = (string)row[0]; v->push_back(temp); } mysql_free_result(results); ////////////////////////////////// CloseDatabaseConnection(conn); if(!ReleaseMutex(mysqlMutex)){ printf("SelectHostIPsByHostName(): Couldn't release mutex. Exiting thread\n"); return GENERIC_ERROR; } return GENERIC_SUCCESS; }