1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "offline.hpp"
#include "dbus.hpp"
#include "utils/string.hpp"
#include <libdnf5/transaction/offline.hpp>
#include <sdbus-c++/sdbus-c++.h>
#include <exception>
#include <filesystem>
const char * const ERR_ANOTHER_TOOL = "Offline transaction was initiated by another tool.";
std::filesystem::path Offline::get_datadir() {
auto base = session.get_base();
const auto & installroot = base->get_config().get_installroot_option().get_value();
return installroot / libdnf5::offline::DEFAULT_DATADIR.relative_path();
}
std::filesystem::path Offline::get_destdir() {
auto base = session.get_base();
const auto & installroot = base->get_config().get_installroot_option().get_value();
return installroot / libdnf5::offline::DEFAULT_DESTDIR.relative_path();
}
Offline::Scheduled Offline::offline_transaction_scheduled() {
std::error_code ec;
// magic symlink exists
if (std::filesystem::exists(libdnf5::offline::MAGIC_SYMLINK, ec)) {
// and points to dnf5 location
if (std::filesystem::equivalent(libdnf5::offline::MAGIC_SYMLINK, get_datadir())) {
return Scheduled::SCHEDULED;
} else {
return Scheduled::ANOTHER_TOOL;
}
}
return Scheduled::NOT_SCHEDULED;
}
void Offline::dbus_register() {
auto dbus_object = session.get_dbus_object();
#ifdef SDBUS_CPP_VERSION_2
dbus_object
->addVTable(
sdbus::MethodVTableItem{
sdbus::MethodName{"cancel_with_options"},
sdbus::Signature{"a{sv}"},
{"options"},
sdbus::Signature{"bs"},
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::cancel_with_options, call, session.session_locale);
},
{}},
sdbus::MethodVTableItem{
sdbus::MethodName{"cancel"},
{},
{},
sdbus::Signature{"bs"},
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Offline::cancel, call, session.session_locale);
},
{}},
sdbus::MethodVTableItem{
sdbus::MethodName{"get_status"},
{},
{},
sdbus::Signature{"ba{sv}"},
{"is_pending", "transaction_status"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::get_status, call, session.session_locale);
},
{}},
sdbus::MethodVTableItem{
sdbus::MethodName{"clean_with_options"},
sdbus::Signature{"a{sv}"},
{"options"},
sdbus::Signature{"bs"},
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::clean_with_options, call, session.session_locale);
},
{}},
sdbus::MethodVTableItem{
sdbus::MethodName{"clean"},
{},
{},
sdbus::Signature{"bs"},
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Offline::clean, call, session.session_locale);
},
{}},
sdbus::MethodVTableItem{
sdbus::MethodName{"set_finish_action_with_options"},
sdbus::Signature{"sa{sv}"},
{"action", "options"},
sdbus::Signature{"bs"},
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::set_finish_action_with_options, call, session.session_locale);
},
{}},
sdbus::MethodVTableItem{
sdbus::MethodName{"set_finish_action"},
sdbus::Signature{"s"},
{"action"},
sdbus::Signature{"bs"},
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::set_finish_action, call, session.session_locale);
},
{}})
.forInterface(dnfdaemon::INTERFACE_OFFLINE);
#else
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"cancel_with_options",
{"a{sv}"},
{"options"},
"bs",
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::cancel_with_options, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"cancel",
{},
{},
"bs",
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Offline::cancel, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"get_status",
{},
{},
"ba{sv}",
{"is_pending", "transaction_status"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Offline::get_status, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"clean_with_options",
{"a{sv}"},
{"options"},
"bs",
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::clean_with_options, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"clean",
{},
{},
"bs",
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(*this, &Offline::clean, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"set_finish_action_with_options",
"sa{sv}",
{"action", "options"},
"bs",
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::set_finish_action_with_options, call, session.session_locale);
});
dbus_object->registerMethod(
dnfdaemon::INTERFACE_OFFLINE,
"set_finish_action",
"s",
{"action"},
"bs",
{"success", "error_msg"},
[this](sdbus::MethodCall call) -> void {
session.get_threads_manager().handle_method(
*this, &Offline::set_finish_action, call, session.session_locale);
});
#endif
}
sdbus::MethodReply Offline::get_status(sdbus::MethodCall & call) {
dnfdaemon::KeyValueMap transaction_state;
const std::filesystem::path state_path{get_datadir() / libdnf5::offline::TRANSACTION_STATE_FILENAME};
// try load the offline transaction state
libdnf5::offline::OfflineTransactionState state{state_path};
if (!state.get_read_exception()) {
const auto & state_data = state.get_data();
transaction_state["status"] = sdbus::Variant(state_data.get_status());
transaction_state["cachedir"] = sdbus::Variant(state_data.get_cachedir());
transaction_state["target_releasever"] = sdbus::Variant(state_data.get_target_releasever());
transaction_state["system_releasever"] = sdbus::Variant(state_data.get_system_releasever());
transaction_state["verb"] = sdbus::Variant(state_data.get_verb());
transaction_state["cmd_line"] = sdbus::Variant(state_data.get_cmd_line());
transaction_state["poweroff_after"] = sdbus::Variant(state_data.get_poweroff_after());
transaction_state["module_platform_id"] = sdbus::Variant(state_data.get_module_platform_id());
}
auto reply = call.createReply();
reply << (offline_transaction_scheduled() == Scheduled::SCHEDULED);
reply << transaction_state;
return reply;
}
sdbus::MethodReply Offline::impl_cancel(sdbus::MethodCall & call, const dnfdaemon::KeyValueMap & options) {
bool interactive = dnfdaemon::key_value_map_get<bool>(options, "interactive", true);
if (!session.check_authorization(
dnfdaemon::POLKIT_EXECUTE_RPM_TRUSTED_TRANSACTION, call.getSender(), interactive)) {
throw std::runtime_error("Not authorized");
}
bool success = true;
std::string error_msg;
switch (offline_transaction_scheduled()) {
case Scheduled::SCHEDULED: {
std::error_code ec;
if (!std::filesystem::remove(libdnf5::offline::MAGIC_SYMLINK, ec) && ec) {
success = false;
error_msg = ec.message();
}
} break;
case Scheduled::ANOTHER_TOOL:
success = false;
error_msg = ERR_ANOTHER_TOOL;
break;
case Scheduled::NOT_SCHEDULED:
break;
}
auto reply = call.createReply();
reply << success;
reply << error_msg;
return reply;
}
sdbus::MethodReply Offline::cancel_with_options(sdbus::MethodCall & call) {
dnfdaemon::KeyValueMap options;
call >> options;
return impl_cancel(call, options);
}
sdbus::MethodReply Offline::cancel(sdbus::MethodCall & call) {
dnfdaemon::KeyValueMap options{};
return impl_cancel(call, options);
}
sdbus::MethodReply Offline::impl_clean(sdbus::MethodCall & call, const dnfdaemon::KeyValueMap & options) {
bool interactive = dnfdaemon::key_value_map_get<bool>(options, "interactive", true);
if (!session.check_authorization(
dnfdaemon::POLKIT_EXECUTE_RPM_TRUSTED_TRANSACTION, call.getSender(), interactive)) {
throw std::runtime_error("Not authorized");
}
std::vector<std::string> error_msgs;
bool success = true;
if (offline_transaction_scheduled() == Scheduled::SCHEDULED) {
// remove the magic symlink if it was created by dnf5
std::error_code ec;
if (!std::filesystem::remove(libdnf5::offline::MAGIC_SYMLINK, ec) && ec) {
success = false;
error_msgs.push_back(ec.message());
}
}
// clean dnf5 offline transaction files
for (const auto & entry : std::filesystem::directory_iterator(get_datadir())) {
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
if (ec) {
success = false;
error_msgs.push_back(ec.message());
}
}
// clean dnf5 offline packages destdir
for (const auto & entry : std::filesystem::directory_iterator(get_destdir())) {
std::error_code ec;
std::filesystem::remove_all(entry.path(), ec);
if (ec) {
success = false;
error_msgs.push_back(ec.message());
}
}
auto reply = call.createReply();
reply << success;
reply << libdnf5::utils::string::join(error_msgs, ", ");
return reply;
}
sdbus::MethodReply Offline::clean_with_options(sdbus::MethodCall & call) {
dnfdaemon::KeyValueMap options;
call >> options;
return impl_clean(call, options);
}
sdbus::MethodReply Offline::clean(sdbus::MethodCall & call) {
dnfdaemon::KeyValueMap options{};
return impl_clean(call, options);
}
sdbus::MethodReply Offline::impl_set_finish_action(
sdbus::MethodCall & call, const std::string & action, const dnfdaemon::KeyValueMap & options) {
bool interactive = dnfdaemon::key_value_map_get<bool>(options, "interactive", true);
if (!session.check_authorization(
dnfdaemon::POLKIT_EXECUTE_RPM_TRUSTED_TRANSACTION, call.getSender(), interactive)) {
throw std::runtime_error("Not authorized");
}
bool success{false};
std::string error_msg{};
// check finish_action validity
if (action != "poweroff" && action != "reboot") {
error_msg =
fmt::format("Unsupported finish action \"{}\". Valid options are \"reboot\", or \"poweroff\".", action);
} else {
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(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();
reply << success;
reply << error_msg;
return reply;
}
sdbus::MethodReply Offline::set_finish_action_with_options(sdbus::MethodCall & call) {
std::string action;
dnfdaemon::KeyValueMap options;
call >> action >> options;
return impl_set_finish_action(call, action, options);
}
sdbus::MethodReply Offline::set_finish_action(sdbus::MethodCall & call) {
std::string action;
dnfdaemon::KeyValueMap options{};
call >> action;
return impl_set_finish_action(call, action, options);
}
|