void InMemoryStorageBackend::save(void) {
    std::string checksumFilename = m_dbPath + m_chsFilename;
    auto chsStream = std::make_shared<std::ofstream>();
    openDumpFileStream<std::ofstream>(*chsStream, checksumFilename + m_backupFilenameSuffix);

    dumpDatabase(chsStream);

    m_integrity.syncDatabase(buckets(), true);
    m_integrity.createBackupGuard();
    m_integrity.revalidatePrimaryDatabase(buckets());
    //guard is removed during revalidation
}
Exemple #2
0
DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent )
    : QObject( (QObject*) parent )
    , m_lastartid( 0 )
    , m_lastalbid( 0 )
    , m_lasttrkid( 0 )
{
    QTime t;
    t.start();

    bool schemaUpdated = openDatabase( dbname );
    tDebug( LOGVERBOSE ) << "Opened database:" << t.elapsed();

    TomahawkSqlQuery query = newquery();
    query.exec( "SELECT v FROM settings WHERE k='dbid'" );
    if ( query.next() )
    {
        m_dbid = query.value( 0 ).toString();
    }
    else
    {
        m_dbid = uuid();
        query.exec( QString( "INSERT INTO settings(k,v) VALUES('dbid','%1')" ).arg( m_dbid ) );
    }
    tLog() << "Database ID:" << m_dbid;

     // make sqlite behave how we want:
    query.exec( "PRAGMA auto_vacuum = FULL" );
    query.exec( "PRAGMA synchronous  = ON" );
    query.exec( "PRAGMA foreign_keys = ON" );
    //query.exec( "PRAGMA temp_store = MEMORY" );
    tDebug( LOGVERBOSE ) << "Tweaked db pragmas:" << t.elapsed();

    // in case of unclean shutdown last time:
    query.exec( "UPDATE source SET isonline = 'false'" );

//    schemaUpdated = true; // REMOVE ME
    m_fuzzyIndex = new FuzzyIndex( *this, schemaUpdated );
    if ( schemaUpdated )
        QTimer::singleShot( 0, this, SLOT( updateIndex() ) );

    tDebug( LOGVERBOSE ) << "Loaded index:" << t.elapsed();

    if ( qApp->arguments().contains( "--dumpdb" ) )
    {
        dumpDatabase();
        ::exit( 0 );
    }
}
Exemple #3
0
Tomahawk::DatabaseImpl::DatabaseImpl( const QString& dbname )
{
    QTime t;
    t.start();

    // Signals for splash screen must be connected here
    connect( this, SIGNAL( schemaUpdateStarted() ),
             qApp, SLOT( onSchemaUpdateStarted() ) );
    connect( this, SIGNAL( schemaUpdateStatus( QString ) ),
             qApp, SLOT( onSchemaUpdateStatus( QString ) ) );
    connect( this, SIGNAL( schemaUpdateDone() ),
             qApp, SLOT( onSchemaUpdateDone() ) );

    bool schemaUpdated = openDatabase( dbname );
    tDebug( LOGVERBOSE ) << "Opened database:" << t.elapsed();

    TomahawkSqlQuery query = newquery();
    query.exec( "SELECT v FROM settings WHERE k='dbid'" );
    if ( query.next() )
    {
        m_dbid = query.value( 0 ).toString();
    }
    else
    {
        m_dbid = uuid();
        query.exec( QString( "INSERT INTO settings(k,v) VALUES('dbid','%1')" ).arg( m_dbid ) );
    }

    tLog() << "Database ID:" << m_dbid;
    init();
    query.exec( "PRAGMA auto_vacuum = FULL" );
    query.exec( "PRAGMA synchronous = NORMAL" );

    tDebug( LOGVERBOSE ) << "Tweaked db pragmas:" << t.elapsed();

    // in case of unclean shutdown last time:
    query.exec( "UPDATE source SET isonline = 'false'" );
    query.exec( "DELETE FROM oplog WHERE source IS NULL AND singleton = 'true'" );

    m_fuzzyIndex = new Tomahawk::DatabaseFuzzyIndex( this, schemaUpdated );

    tDebug( LOGVERBOSE ) << "Loaded index:" << t.elapsed();
    if ( qApp->arguments().contains( "--dumpdb" ) )
    {
        dumpDatabase();
        ::exit( 0 );
    }
}
Exemple #4
0
void Initializer::start() const {
  // Load registry/extension modules before extensions.
  osquery::loadModules();

  // Pre-extension manager initialization options checking.
  // If the shell or daemon does not need extensions and it will exit quickly,
  // prefer to disable the extension manager.
  if ((FLAGS_config_check || FLAGS_config_dump) &&
      !Watcher::hasManagedExtensions()) {
    FLAGS_disable_extensions = true;
  }

  // A watcher should not need access to the backing store.
  // If there are spurious access then warning logs will be emitted since the
  // set-allow-open will never be called.
  if (!isWatcher()) {
    DatabasePlugin::setAllowOpen(true);
    // A daemon must always have R/W access to the database.
    DatabasePlugin::setRequireWrite(tool_ == OSQUERY_TOOL_DAEMON);
    if (!DatabasePlugin::initPlugin()) {
      LOG(ERROR) << RLOG(1629) << binary_
                 << " initialize failed: Could not initialize database";
      auto retcode = (isWorker()) ? EXIT_CATASTROPHIC : EXIT_FAILURE;
      requestShutdown(retcode);
    }
  }

  // Bind to an extensions socket and wait for registry additions.
  // After starting the extension manager, osquery MUST shutdown using the
  // internal 'shutdown' method.
  osquery::startExtensionManager();

  // Then set the config plugin, which uses a single/active plugin.
  initActivePlugin("config", FLAGS_config_plugin);

  // Run the setup for all lazy registries (tables, SQL).
  Registry::setUp();

  if (FLAGS_config_check) {
    // The initiator requested an initialization and config check.
    auto s = Config::getInstance().load();
    if (!s.ok()) {
      std::cerr << "Error reading config: " << s.toString() << "\n";
    }
    // A configuration check exits the application.
    // Make sure to request a shutdown as plugins may have created services.
    requestShutdown(s.getCode());
  }

  if (FLAGS_database_dump) {
    dumpDatabase();
    requestShutdown();
  }

  // Load the osquery config using the default/active config plugin.
  auto s = Config::getInstance().load();
  if (!s.ok()) {
    auto message = "Error reading config: " + s.toString();
    if (tool_ == OSQUERY_TOOL_DAEMON) {
      LOG(WARNING) << message;
    } else {
      LOG(INFO) << message;
    }
  }

  // Initialize the status and result plugin logger.
  if (!FLAGS_disable_logging) {
    initActivePlugin("logger", FLAGS_logger_plugin);
  }
  initLogger(binary_);

  // Initialize the distributed plugin, if necessary
  if (!FLAGS_disable_distributed) {
    if (Registry::exists("distributed", FLAGS_distributed_plugin)) {
      initActivePlugin("distributed", FLAGS_distributed_plugin);
    }
  }

  // Start event threads.
  osquery::attachEvents();
  EventFactory::delay();
}
Exemple #5
0
	~OracleTestDatabaseInitializer()
	{
		dumpDatabase();
	}
	~PostgreSQLTestDatabaseInitializer()
	{
		dumpDatabase();
	}
Exemple #7
0
static void _doOnExit(void) {
	programHeader();
	printf("\nSaving database before exit...\n");
	dumpDatabase(DB_HANDLE);
}
Exemple #8
0
void Initializer::start() {
  // Load registry/extension modules before extensions.
  osquery::loadModules();

  // Pre-extension manager initialization options checking.
  if (FLAGS_config_check && !Watcher::hasManagedExtensions()) {
    FLAGS_disable_extensions = true;
  }

  // A daemon must always have R/W access to the database.
  DBHandle::setAllowOpen(true);
  DBHandle::setRequireWrite(tool_ == OSQUERY_TOOL_DAEMON);
  if (!DBHandle::checkDB()) {
    LOG(ERROR) << RLOG(1629) << binary_
               << " initialize failed: Could not open RocksDB";
    if (isWorker()) {
      ::exit(EXIT_CATASTROPHIC);
    } else {
      ::exit(EXIT_FAILURE);
    }
  }

  // Bind to an extensions socket and wait for registry additions.
  osquery::startExtensionManager();

  // Then set the config plugin, which uses a single/active plugin.
  initActivePlugin("config", FLAGS_config_plugin);

  // Run the setup for all lazy registries (tables, SQL).
  Registry::setUp();

  if (FLAGS_config_check) {
    // The initiator requested an initialization and config check.
    auto s = Config::getInstance().load();
    if (!s.ok()) {
      std::cerr << "Error reading config: " << s.toString() << "\n";
    }
    // A configuration check exits the application.
    ::exit(s.getCode());
  }

  if (FLAGS_database_dump) {
    dumpDatabase();
    ::exit(EXIT_SUCCESS);
  }

  // Load the osquery config using the default/active config plugin.
  auto s = Config::getInstance().load();
  if (!s.ok()) {
    auto message = "Error reading config: " + s.toString();
    if (tool_ == OSQUERY_TOOL_DAEMON) {
      LOG(WARNING) << message;
    } else {
      LOG(INFO) << message;
    }
  }

  // Initialize the status and result plugin logger.
  initActivePlugin("logger", FLAGS_logger_plugin);
  initLogger(binary_);

  // Initialize the distributed plugin, if necessary
  if (!FLAGS_disable_distributed) {
    if (Registry::exists("distributed", FLAGS_distributed_plugin)) {
      initActivePlugin("distributed", FLAGS_distributed_plugin);
    }
  }

  // Start event threads.
  osquery::attachEvents();
  EventFactory::delay();
}
Exemple #9
0
	~SQLiteTestDatabaseInitializer()
	{
		dumpDatabase();
	}