File: session_manager.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (220 lines) | stat: -rw-r--r-- 7,872 bytes parent folder | download
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
// 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 "session_manager.hpp"

#include "dbus.hpp"
#include "session.hpp"

#include <libdnf5/logger/stream_logger.hpp>
#include <sdbus-c++/sdbus-c++.h>

#include <iostream>
#include <numeric>
#include <random>
#include <sstream>
#include <string>
#include <thread>

// TODO(mblaha): Make this constant configurable
const unsigned int MAX_SESSIONS = 3;

SessionManager::SessionManager() {
    connection = sdbus::createSystemBusConnection(dnfdaemon::DBUS_NAME);
    dbus_register();
}

SessionManager::~SessionManager() {
    dbus_object->unregister();
    threads_manager.finish();
}

void SessionManager::dbus_register() {
    dbus_object = sdbus::createObject(*connection, dnfdaemon::DBUS_OBJECT_PATH);
#ifdef SDBUS_CPP_VERSION_2
    dbus_object
        ->addVTable(
            sdbus::MethodVTableItem{
                sdbus::MethodName{"open_session"},
                sdbus::Signature{"a{sv}"},
                {"options"},
                sdbus::Signature{"o"},
                {"session_object_path"},
                [this](sdbus::MethodCall call) -> void {
                    // open_session method manages sessions and does not call any libdnf5 API. Do not use the mutex.
                    threads_manager.handle_method<SessionManager, false>(*this, &SessionManager::open_session, call);
                },
                {}},
            sdbus::MethodVTableItem{
                sdbus::MethodName{"close_session"},
                sdbus::Signature{"o"},
                {"session_object_path"},
                sdbus::Signature{"b"},
                {"success"},
                [this](sdbus::MethodCall call) -> void {
                    // close_session method manages sessions and does not call any libdnf5 API. Do not use the mutex.
                    threads_manager.handle_method<SessionManager, false>(*this, &SessionManager::close_session, call);
                },
                {}})
        .forInterface(dnfdaemon::INTERFACE_SESSION_MANAGER);
#else
    dbus_object->registerMethod(
        dnfdaemon::INTERFACE_SESSION_MANAGER,
        "open_session",
        "a{sv}",
        {"options"},
        "o",
        {"session_object_path"},
        [this](sdbus::MethodCall call) -> void {
            // open_session method manages sessions and does not call any libdnf5 API. Do not use the mutex.
            threads_manager.handle_method<SessionManager, false>(*this, &SessionManager::open_session, call);
        });
    dbus_object->registerMethod(
        dnfdaemon::INTERFACE_SESSION_MANAGER,
        "close_session",
        "o",
        {"session_object_path"},
        "b",
        {"success"},
        [this](sdbus::MethodCall call) -> void {
            // close_session method manages sessions and does not call any libdnf5 API. Do not use the mutex.
            threads_manager.handle_method<SessionManager, false>(*this, &SessionManager::close_session, call);
        });
    dbus_object->finishRegistration();

#endif

    // register signal handler for NameOwnerChanged
    name_changed_proxy = sdbus::createProxy(
        *connection, SDBUS_SERVICE_NAME_TYPE{"org.freedesktop.DBus"}, sdbus::ObjectPath{"/org/freedesktop/DBus"});
    name_changed_proxy->registerSignalHandler(
        SDBUS_INTERFACE_NAME_TYPE{"org.freedesktop.DBus"},
        SDBUS_SIGNAL_NAME_TYPE{"NameOwnerChanged"},
        [this](sdbus::Signal signal) -> void {
            threads_manager.handle_signal(*this, &SessionManager::on_name_owner_changed, signal);
        });
#ifndef SDBUS_CPP_VERSION_2
    name_changed_proxy->finishRegistration();
#endif
}


std::string gen_session_id() {
    static std::random_device rd;
    static std::uniform_int_distribution<> dist(0, 15);

    std::stringstream ss;
    ss << std::hex;
    for (int i = 0; i < 32; i++) {
        ss << dist(rd);
    }
    return ss.str();
}


void SessionManager::on_name_owner_changed(sdbus::Signal & signal) {
    std::string name;
    std::string old_owner;
    std::string new_owner;
    signal >> name >> old_owner >> new_owner;
    if (new_owner.empty() && sessions.count(old_owner) > 0) {
        std::map<std::string, std::map<std::string, std::unique_ptr<Session>>> to_be_erased;
        {
            std::lock_guard<std::mutex> lock(sessions_mutex);
            // the sender name disappeared from the dbus, erase all its sessions
            to_be_erased[old_owner] = std::move(sessions.at(old_owner));
            sessions.erase(old_owner);
        }
        to_be_erased.erase(old_owner);
    }
}

sdbus::MethodReply SessionManager::open_session(sdbus::MethodCall & call) {
    std::lock_guard<std::mutex> lock(active_mutex);
    if (!active) {
        throw sdbus::Error(dnfdaemon::ERROR, "Cannot open new session.");
    }
    // limit number of simultaneously opened sessions
    const auto num_sessions =
        std::accumulate(sessions.begin(), sessions.end(), 0U, [](unsigned 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;
    call >> configuration;

    // generate UUID-like session id
    const sdbus::ObjectPath sessionid{dnfdaemon::DBUS_OBJECT_PATH + std::string("/") + gen_session_id()};
    // store newly created session
    {
        std::lock_guard<std::mutex> lock(sessions_mutex);
        sessions[sender].emplace(
            sessionid, std::make_unique<Session>(*connection, std::move(configuration), sessionid, sender));
    }

    auto reply = call.createReply();
    reply << sdbus::ObjectPath{sessionid};
    return reply;
}


sdbus::MethodReply SessionManager::close_session(sdbus::MethodCall & call) {
    auto sender = call.getSender();
    sdbus::ObjectPath session_id;
    call >> session_id;

    bool retval = false;
    // find sessions created by the same sender
    auto sender_it = sessions.find(sender);
    if (sender_it != sessions.end()) {
        std::lock_guard<std::mutex> lock(sessions_mutex);
        // delete session with given session_id
        if (sender_it->second.erase(session_id) > 0) {
            retval = true;
        }
    }

    auto reply = call.createReply();
    reply << retval;
    return reply;
}

void SessionManager::start_event_loop() {
    connection->enterEventLoop();
};

void SessionManager::shut_down() {
    // TODO (mblaha): log info like "Shutdown signal received, waiting for sessions to finish..."
    std::lock_guard<std::mutex> lock(active_mutex);
    if (active) {
        // prevent opening a new session
        active = false;
        std::lock_guard<std::mutex> lock(sessions_mutex);
        // wait for current sessions to finish and delete them
        sessions.clear();
        // leave the main event loop
        connection->leaveEventLoop();
    }
}