Skip to content

Commit c090ffe

Browse files
m-blahajan-kolarik
authored andcommitted
dnfdaemon: Limit number of simultaneously active sessions
1 parent bf98b14 commit c090ffe

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

dnf5daemon-server/session_manager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
2626
#include <sdbus-c++/sdbus-c++.h>
2727

2828
#include <iostream>
29+
#include <numeric>
2930
#include <random>
3031
#include <sstream>
3132
#include <string>
3233
#include <thread>
3334

35+
// TODO(mblaha): Make this constant configurable
36+
const int MAX_SESSIONS = 3;
37+
3438
SessionManager::SessionManager() {
3539
connection = sdbus::createSystemBusConnection(dnfdaemon::DBUS_NAME);
3640
dbus_register();
@@ -98,6 +102,14 @@ sdbus::MethodReply SessionManager::open_session(sdbus::MethodCall & call) {
98102
if (!active) {
99103
throw sdbus::Error(dnfdaemon::ERROR, "Cannot open new session.");
100104
}
105+
// limit number of simultaneously opened sessions
106+
const int num_sessions = std::accumulate(
107+
sessions.begin(), sessions.end(), 0, [](int sum, const auto & sender) { return sum + sender.second.size(); });
108+
if (num_sessions >= MAX_SESSIONS) {
109+
auto reply = call.createErrorReply(sdbus::Error(
110+
dnfdaemon::ERROR, "Cannot open new session - maximal number of simultaneously opened sessions achieved."));
111+
return reply;
112+
}
101113

102114
auto sender = call.getSender();
103115
dnfdaemon::KeyValueMap configuration;

0 commit comments

Comments
 (0)