diff --git a/dnf5daemon-server/session_manager.cpp b/dnf5daemon-server/session_manager.cpp
index a5e1c14f7..b8439cf37 100644
--- a/dnf5daemon-server/session_manager.cpp
+++ b/dnf5daemon-server/session_manager.cpp
@@ -26,11 +26,15 @@ along with libdnf. If not, see .
#include
#include
+#include
#include
#include
#include
#include
+// TODO(mblaha): Make this constant configurable
+const int MAX_SESSIONS = 3;
+
SessionManager::SessionManager() {
connection = sdbus::createSystemBusConnection(dnfdaemon::DBUS_NAME);
dbus_register();
@@ -98,6 +102,14 @@ sdbus::MethodReply SessionManager::open_session(sdbus::MethodCall & call) {
if (!active) {
throw sdbus::Error(dnfdaemon::ERROR, "Cannot open new session.");
}
+ // limit number of simultaneously opened sessions
+ const int num_sessions = std::accumulate(
+ sessions.begin(), sessions.end(), 0, [](int sum, const auto & sender) { return sum + sender.second.size(); });
+ if (num_sessions >= MAX_SESSIONS) {
+ auto reply = call.createErrorReply(sdbus::Error(
+ dnfdaemon::ERROR, "Cannot open new session - maximal number of simultaneously opened sessions achieved."));
+ return reply;
+ }
auto sender = call.getSender();
dnfdaemon::KeyValueMap configuration;