/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2019-2022 Samsung Electronics Co., Ltd. All rights reserved.
*
* This file is licensed under the terms of MIT License or the Apache License
* Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
#define DB_FALLBACK_PATH DATA_INSTALL_DIR "/" DB_FILE
-// If database initialization fails, restoration to a fallback snapshot is
-// attempted. If the restoration succeeds, a file flag is created to notify
-// other system components.
+// If database initialization fails, a file flag is created to notify
+// other system components & then, the DB restoration is attempted.
// For database placed in "$f" the filename is ("$f" DB_RECOVERED_SUFFIX).
#define DB_RECOVERED_SUFFIX "-recovered"
#define DB_JOURNAL_SUFFIX "-journal"
/*
- * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2014-2022 Samsung Electronics Co., Ltd. All rights reserved.
*
* This file is licensed under the terms of MIT License or the Apache License
* Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
} //namespace
PrivilegeDb::PrivilegeDb(Offline offline, const std::string &dbPath, const char *okMarkerPath, const char *loaderCmd) {
- bool didFallback = false;
- if (!underlying(offline) && !FS::fileExists(okMarkerPath) && !(didFallback = FS::fileExists(dbPath + DB_RECOVERED_SUFFIX)))
+ if (!underlying(offline) && !FS::fileExists(okMarkerPath) && !FS::fileExists(dbPath + DB_RECOVERED_SUFFIX))
throwDbInitEx("loader failed to initialize db - giving up");
tryCatchDbInit([&]{ mSqlConnection.Connect(dbPath); });
} catch (DB::SqlConnection::Exception::Base &e) {
if (underlying(offline))
throwDbInitEx("failed to initialize db in offline mode - giving up");
- if (didFallback)
- throwDbInitEx("Database initialization error during query preparation on fallback db - giving up");
LogError("Database initialization error during query preparation - attempting fallback");
tryCatchDbInit([&]{
mSqlConnection.Disconnect();
/*
- * Copyright (c) 2018-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2018-2022 Samsung Electronics Co., Ltd. All rights reserved.
*
* This file is licensed under the terms of MIT License or the Apache License
* Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
// skip main database bringup and start applying fallback right away
//
// the option is used by the manager if a fallback has not been applied yet
- // ("database successfully recovered" marker file does not exist)
+ // ("database recovery attempted" marker file does not exist)
// and a schema error has been detected
case 'f':
// exclusive option - cannot be combined w/ anything else
// need the following filenames:
// dbPath : to open the database
- // dbPath + DB_RECOVERED_SUFFIX : to remove/create the "database successfully recovered" marker file
+ // dbPath + DB_RECOVERED_SUFFIX : to remove/create the "database recovery attempted" marker file
// dbPath + DB_JOURNAL_SUFFIX : to potentially truncate the journal file when overwriting database with fallback
//
// the pkgsInfo tape memory is used to store these names (the tape is otherwise unused during database bringup)
// pkgsInfo.t = dbPath (\0-unterminated)
memcpy(pkgsInfo.t, dbPath, dbPathLen);
- // pkgsInfo.t = dbPath + DB_RECOVERED_SUFFIX (\0-terminated)
- acpy(pkgsInfo.t + dbPathLen, DB_RECOVERED_SUFFIX);
- // remove the "database successfully recovered" marker file (it's not supposed to survive reboot but is stored on permanent flash)
- if (unlikely(!unlinkIfExists(pkgsInfo.t)))
- fail("unlink(db" DB_RECOVERED_SUFFIX ") failed");
parseOptions(argc, argv);
if (unlikely(0 > creat(dbOkMarker, 0644)))
fail("creat(dbOkMarker) failed");
} else {
+ // create the "database recovery attempted" marker file
+ acpy(pkgsInfo.t + dbPathLen, DB_RECOVERED_SUFFIX);
+ if (unlikely(creat(pkgsInfo.t, 0644) < 0))
+ fail("creat(.security-manager.db" DB_RECOVERED_SUFFIX ") failed");
+
// main db initialization failed or not attempted, apply fallback
toStderr("overwriting db with fallback");
overwriteDbFileWithFallback(dbPathLen);
if (unlikely(!dbUp(CheckFallback::no)))
fail("fallback db bringup failed");
- // create the "database successfully recovered" marker file
- acpy(pkgsInfo.t + dbPathLen, DB_RECOVERED_SUFFIX);
- if (unlikely(creat(pkgsInfo.t, 0644) < 0))
- fail("creat(.security-manager.db" DB_RECOVERED_SUFFIX ") failed");
-
// try to sync DB_INSTALL_DIR dir
const int dbdirfd = open(DB_INSTALL_DIR, O_RDONLY);
if (unlikely(dbdirfd < 0))
/*
- * Copyright (c) 2016-2020 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2016-2022 Samsung Electronics Co., Ltd. All rights reserved.
*
* This file is licensed under the terms of MIT License or the Apache License
* Version 2.0 of your choice. See the LICENSE.MIT file for MIT license details.
}
}
void checkMarker(PrivilegeDBFixture::Marker marker) {
- testMarkerFile(TEST_DB_OK_MARKER, underlying(marker) & underlying(PrivilegeDBFixture::Marker::standard));
- testMarkerFile(TEST_DB_PATH DB_RECOVERED_SUFFIX, underlying(marker) & underlying(PrivilegeDBFixture::Marker::fallback));
+ const auto broken = marker != PrivilegeDBFixture::Marker::standard;
+ testMarkerFile(TEST_DB_OK_MARKER, !broken);
+ testMarkerFile(TEST_DB_PATH DB_RECOVERED_SUFFIX, broken);
if (underlying(marker)) {
struct stat st;
BOOST_REQUIRE(!lstat(TEST_DB_PATH, &st));