Skip to content

Commit

Permalink
dnfdaemon: Strict set_finish_action() value check
Browse files Browse the repository at this point in the history
Check that only one of "poweroff", or "reboot" value was passed to
set_finish_action() call.
  • Loading branch information
m-blaha committed Jul 10, 2024
1 parent 61487cf commit 95f589a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

<!--
set_finish_action:
@action: string, if set to "poweroff", the system will be powered off after applying the offline transaction. Otherwise the system will reboot.
@action: string, one of "poweroff", or "reboot". If set to "poweroff", the system will be powered off after applying the offline transaction. Otherwise the system will reboot.
@success: boolean, true if the action was successfully set
@error_msg: string, contains error encountered while setting the action
Expand Down
46 changes: 29 additions & 17 deletions dnf5daemon-server/services/offline/offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,36 @@ sdbus::MethodReply Offline::set_finish_action(sdbus::MethodCall & call) {
}
bool success{false};
std::string error_msg{};
// try load the offline transaction state
const std::filesystem::path state_path{
libdnf5::offline::MAGIC_SYMLINK / libdnf5::offline::TRANSACTION_STATE_FILENAME};
libdnf5::offline::OfflineTransactionState state{state_path};
const auto & read_exception = state.get_read_exception();
if (read_exception == nullptr) {
// set the poweroff_after item accordingly
std::string finish_action;
call >> finish_action;
state.get_data().set_poweroff_after(finish_action == "poweroff");
// write the new state
state.write();
success = true;

std::string finish_action;
call >> finish_action;
// check finish_action validity
if (finish_action != "poweroff" && finish_action != "reboot") {
error_msg = fmt::format(
"Unsupported finish action \"{}\". Valid options are \"reboot\", or \"poweroff\".", finish_action);
} else {
try {
std::rethrow_exception(read_exception);
} catch (const std::exception & ex) {
error_msg = ex.what();
const std::filesystem::path state_path{get_datadir() / libdnf5::offline::TRANSACTION_STATE_FILENAME};
std::error_code ec;
// check presence of transaction state file
if (!std::filesystem::exists(state_path, ec)) {
error_msg = "No offline transaction is configured. Cannot set the finish action.";
} else {
// try load the offline transaction state
libdnf5::offline::OfflineTransactionState state{state_path};
const auto & read_exception = state.get_read_exception();
if (read_exception == nullptr) {
// set the poweroff_after item accordingly
state.get_data().set_poweroff_after(finish_action == "poweroff");
// write the new state
state.write();
success = true;
} else {
try {
std::rethrow_exception(read_exception);
} catch (const std::exception & ex) {
error_msg = ex.what();
}
}
}
}
auto reply = call.createReply();
Expand Down

0 comments on commit 95f589a

Please sign in to comment.