#include <sys/types.h>
namespace vist {
-namespace rmi {
-namespace impl {
struct Credentials {
- pid_t pid;
- uid_t uid;
- gid_t gid;
-
- static Credentials GetPeers(int fd)
+ static Credentials Peer(int fd)
{
struct ucred cred;
socklen_t credsz = sizeof(cred);
return {cred.pid, cred.uid, cred.gid};
}
+
+ pid_t pid;
+ uid_t uid;
+ gid_t gid;
};
-} // namespace impl
-} // namespace rmi
} // namespace vist
this->pImpl->stop();
}
+/// Credentials exists per thread.
+std::shared_ptr<Credentials> GetPeerCredentials()
+{
+ return Server::GetPeerCredentials();
+}
+
} // namespace rmi
} // namespace vist
#define EXPOSE(gateway, object, function) gateway.expose(object, #function, function)
+#include <vist/credentials.hpp>
#include <vist/klass/functor.hpp>
#include <vist/macro.hpp>
template<typename O, typename F>
void expose(O& object, const std::string& name, F&& func);
+ static std::shared_ptr<Credentials> GetPeerCredentials();
+
private:
class Impl;
namespace rmi {
namespace impl {
+thread_local std::shared_ptr<Credentials> Server::peer = nullptr;
+
Server::Server(const std::string& path, const Task& task, ServiceType type) : worker(5)
{
switch (type) {
/// process task per thread
this->worker.submit([this, connection, task]{
auto onRead = [connection, task]() {
+ Server::peer.reset(new Credentials(Credentials::Peer(connection->getFd())));
+
Message request = connection->recv();
DEBUG(VIST) << "Session header: " << request.signature;
this->mainloop.removeHandler(connection->getFd());
};
- this->mainloop.addHandler(connection->getFd(),
- std::move(onRead), std::move(onClose));
+ this->mainloop.addHandler(connection->getFd(), std::move(onRead), std::move(onClose));
});
};
#pragma once
+#include <vist/credentials.hpp>
#include <vist/rmi/gateway.hpp>
-#include <vist/rmi/message.hpp>
#include <vist/rmi/impl/mainloop.hpp>
#include <vist/rmi/impl/socket.hpp>
+#include <vist/rmi/message.hpp>
#include <vist/thread-pool.hpp>
#include <memory>
void run(int timeout = -1, Stopper stopper = nullptr);
void stop(void);
+ static std::shared_ptr<Credentials> GetPeerCredentials()
+ {
+ return peer;
+ }
+
private:
void accept(const Task& task);
+ static thread_local std::shared_ptr<Credentials> peer;
+
std::unique_ptr<Socket> socket;
Mainloop mainloop;
ThreadPool worker;
/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
TEST(ServerClientTests, not_ondemand)
{
- std::string sockPath = "./vist-test.sock";
+ std::string sockPath = "@vist-test.sock";
auto task = [&](Message& message) -> Message {
EXPECT_EQ(message.signature, requestSignature);
if (serverThread.joinable())
serverThread.join();
}
+
+TEST(ServerClientTests, peer_pid)
+{
+ std::string sockPath = "@vist-test.sock";
+ auto task = [](Message& message) -> Message {
+ EXPECT_EQ(message.signature, requestSignature);
+
+ auto peer = Server::GetPeerCredentials();
+ EXPECT_TRUE(peer != nullptr);
+
+ int recv1;
+ message.disclose(recv1);
+ EXPECT_EQ(request1, recv1);
+
+ Message reply(Message::Type::Reply, responseSignature);
+ reply.enclose(response1);
+ return reply;
+ };
+
+ Server server(sockPath, task);
+ auto serverThread = std::thread([&]() {
+ server.run();
+ });
+
+ { /// Client configuration
+ auto clientClosure = [&]() {
+ Client client(sockPath);
+
+ Message message(Message::Type::MethodCall, requestSignature);
+ message.enclose(request1);
+
+ auto reply = client.request(message);
+ EXPECT_EQ(reply.signature, responseSignature);
+
+ int recv1;
+ reply.disclose(recv1);
+ EXPECT_EQ(response1, recv1);
+ };
+
+ clientClosure();
+ }
+
+ server.stop();
+
+ if (serverThread.joinable())
+ serverThread.join();
+}
* limitations under the License
*/
-#include <vist/rmi/impl/credentials.hpp>
+#include <vist/credentials.hpp>
#include <vist/rmi/impl/socket.hpp>
#include <cstring>
Socket accepted = socket.accept();
- auto cred = Credentials::GetPeers(accepted.getFd());
+ auto cred = vist::Credentials::Peer(accepted.getFd());
EXPECT_TRUE(cred.pid > 0);
// Recv input from client.
std::string getName(void)
{
+ auto peer = Gateway::GetPeerCredentials();
+ EXPECT_TRUE(peer != nullptr);
+
return this->name;
}