Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from MusicPlayerDaemon:master #24

Merged
merged 10 commits into from
Jan 29, 2025
6 changes: 3 additions & 3 deletions src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ MainConfigured(const CommandLineOptions &options,
Instance instance;
global_instance = &instance;

instance.io_thread.Start();
instance.rtio_thread.Start();

#ifdef ENABLE_NEIGHBOR_PLUGINS
instance.neighbors = std::make_unique<NeighborGlue>();
instance.neighbors->Init(raw_config,
Expand Down Expand Up @@ -421,9 +424,6 @@ MainConfigured(const CommandLineOptions &options,
};
#endif

instance.io_thread.Start();
instance.rtio_thread.Start();

#ifdef ENABLE_NEIGHBOR_PLUGINS
if (instance.neighbors != nullptr)
instance.neighbors->Open();
Expand Down
10 changes: 6 additions & 4 deletions src/client/Client.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>

class SocketAddress;
class SocketPeerCredentials;
class UniqueSocketDescriptor;
class EventLoop;
class Path;
Expand All @@ -38,6 +39,8 @@ class Client final
friend struct ClientPerPartitionListHook;
friend class ClientList;

const std::string name;

IntrusiveListHook<> list_siblings, partition_siblings;

CoarseTimerEvent timeout_event;
Expand All @@ -51,8 +54,6 @@ class Client final

CommandListBuilder cmd_list;

const unsigned int num; /* client number */

/** is this client waiting for an "idle" response? */
bool idle_waiting = false;

Expand Down Expand Up @@ -121,7 +122,7 @@ public:
Client(EventLoop &loop, Partition &partition,
UniqueSocketDescriptor fd, int uid,
unsigned _permission,
int num) noexcept;
std::string &&_name) noexcept;

~Client() noexcept;

Expand Down Expand Up @@ -300,5 +301,6 @@ struct ClientPerPartitionListHook

void
client_new(EventLoop &loop, Partition &partition,
UniqueSocketDescriptor fd, SocketAddress address, int uid,
UniqueSocketDescriptor fd, SocketAddress address,
SocketPeerCredentials cred,
unsigned permission) noexcept;
2 changes: 1 addition & 1 deletion src/client/Event.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
void
Client::OnSocketError(std::exception_ptr ep) noexcept
{
FmtError(client_domain, "error on client {}: {}", num, ep);
FmtError(client_domain, "[{}] error: {}", name, ep);

SetExpired();
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/Expire.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Client::OnTimeout() noexcept
assert(!idle_waiting);
assert(!background_command);

FmtDebug(client_domain, "[{}] timeout", num);
FmtDebug(client_domain, "[{}] timeout", name);
}

Close();
Expand Down
12 changes: 7 additions & 5 deletions src/client/Listener.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#include "Listener.hxx"
#include "Client.hxx"
#include "Permission.hxx"
#include "net/PeerCredentials.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "net/SocketAddress.hxx"
#include "config.h"

static unsigned
GetPermissions(SocketAddress address, int uid) noexcept
GetPermissions(SocketAddress address, const SocketPeerCredentials cred) noexcept
{
(void)uid; // TODO: implement option to derive permissions from uid
(void)cred; // TODO: implement option to derive permissions from uid

#ifdef HAVE_UN
if (address.GetFamily() == AF_LOCAL)
Expand All @@ -29,10 +30,11 @@ GetPermissions(SocketAddress address, int uid) noexcept

void
ClientListener::OnAccept(UniqueSocketDescriptor fd,
SocketAddress address, int uid) noexcept
SocketAddress address) noexcept
{
const auto cred = fd.GetPeerCredentials();

client_new(GetEventLoop(), partition,
std::move(fd), address, uid,
GetPermissions(address, uid));
std::move(fd), address, cred,
GetPermissions(address, cred));
}
2 changes: 1 addition & 1 deletion src/client/Listener.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public:

private:
void OnAccept(UniqueSocketDescriptor fd,
SocketAddress address, int uid) noexcept override;
SocketAddress address) noexcept override;
};

#endif
41 changes: 29 additions & 12 deletions src/client/New.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#include "Partition.hxx"
#include "Instance.hxx"
#include "lib/fmt/SocketAddressFormatter.hxx"
#include "net/PeerCredentials.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "net/SocketAddress.hxx"
#include "net/ToString.hxx"
#include "util/SpanCast.hxx"
#include "Log.hxx"
#include "Version.h"

#include <fmt/core.h>

#include <cassert>

using std::string_view_literals::operator""sv;
Expand All @@ -24,26 +28,41 @@ static constexpr auto GREETING = "OK MPD " PROTOCOL_VERSION "\n"sv;
Client::Client(EventLoop &_loop, Partition &_partition,
UniqueSocketDescriptor _fd,
int _uid, unsigned _permission,
int _num) noexcept
std::string &&_name) noexcept
:FullyBufferedSocket(_fd.Release(), _loop,
16384, client_max_output_buffer_size),
name(std::move(_name)),
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)),
partition(&_partition),
permission(_permission),
uid(_uid),
num(_num),
last_album_art(_loop)
{
FmtInfo(client_domain, "[{}] client connected", name);

timeout_event.Schedule(client_timeout);
}

[[gnu::pure]]
static std::string
MakeClientName(SocketAddress address, const SocketPeerCredentials &cred) noexcept
{
if (cred.IsDefined()) {
if (cred.GetPid() > 0)
return fmt::format("pid={} uid={}", cred.GetPid(), cred.GetUid());

return fmt::format("uid={}", cred.GetUid());
}

return ToString(address);
}

void
client_new(EventLoop &loop, Partition &partition,
UniqueSocketDescriptor fd, SocketAddress remote_address, int uid,
UniqueSocketDescriptor fd, SocketAddress remote_address,
SocketPeerCredentials cred,
unsigned permission) noexcept
{
static unsigned int next_client_num;

assert(fd.IsDefined());

ClientList &client_list = *partition.instance.client_list;
Expand All @@ -54,16 +73,14 @@ client_new(EventLoop &loop, Partition &partition,

(void)fd.WriteNoWait(AsBytes(GREETING));

const unsigned num = next_client_num++;
const int uid = cred.IsDefined() ? static_cast<int>(cred.GetUid()) : -1;

auto *client = new Client(loop, partition, std::move(fd), uid,
permission,
num);
permission,
MakeClientName(remote_address, cred));

client_list.Add(*client);
partition.clients.push_back(*client);

FmtInfo(client_domain, "[{}] opened from {}",
num, remote_address);
}

void
Expand All @@ -75,6 +92,6 @@ Client::Close() noexcept
if (FullyBufferedSocket::IsDefined())
FullyBufferedSocket::Close();

FmtInfo(client_domain, "[{}] closed", num);
FmtInfo(client_domain, "[{}] disconnected", name);
delete this;
}
20 changes: 8 additions & 12 deletions src/client/Process.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ Client::ProcessLine(char *line) noexcept
request */
FmtWarning(client_domain,
"[{}] malformed command {:?}",
num, line);
name, line);
return CommandResult::CLOSE;
}

if (cmd_list.IsActive() && IsAsyncCommmand(line)) {
FmtWarning(client_domain,
"[{}] not possible in comand list: {:?}",
num, line);
name, line);
return CommandResult::CLOSE;
}

Expand All @@ -82,17 +82,15 @@ Client::ProcessLine(char *line) noexcept
except "noidle" */
FmtWarning(client_domain,
"[{}] command {:?} during idle",
num, line);
name, line);
return CommandResult::CLOSE;
}

if (cmd_list.IsActive()) {
if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
const unsigned id = num;

FmtDebug(client_domain,
"[{}] process command list",
id);
name);

const bool ok_mode = cmd_list.IsOKMode();
auto list = cmd_list.Commit();
Expand All @@ -102,7 +100,7 @@ Client::ProcessLine(char *line) noexcept
std::move(list));
FmtDebug(client_domain,
"[{}] process command "
"list returned {}", id, unsigned(ret));
"list returned {}", name, unsigned(ret));

if (ret == CommandResult::OK)
WriteOK();
Expand All @@ -113,7 +111,7 @@ Client::ProcessLine(char *line) noexcept
FmtWarning(client_domain,
"[{}] command list size "
"is larger than the max ({})",
num, client_max_command_list_size);
name, client_max_command_list_size);
return CommandResult::CLOSE;
}

Expand All @@ -127,15 +125,13 @@ Client::ProcessLine(char *line) noexcept
cmd_list.Begin(true);
return CommandResult::OK;
} else {
const unsigned id = num;

FmtDebug(client_domain,
"[{}] process command {:?}",
id, line);
name, line);
auto ret = command_process(*this, 0, line);
FmtDebug(client_domain,
"[{}] command returned {}",
id, unsigned(ret));
name, unsigned(ret));

if (IsExpired())
return CommandResult::CLOSE;
Expand Down
5 changes: 3 additions & 2 deletions src/event/Loop.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#endif

#ifdef HAVE_URING
#include "UringManager.hxx"
#include "uring/Manager.hxx"
#include "util/PrintException.hxx"
#include <stdio.h>
#endif
Expand Down Expand Up @@ -61,7 +61,8 @@ EventLoop::GetUring() noexcept
if (!uring_initialized) {
uring_initialized = true;
try {
uring = std::make_unique<Uring::Manager>(*this);
uring = std::make_unique<Uring::Manager>(*this, 1024,
IORING_SETUP_SINGLE_ISSUER);
} catch (...) {
fprintf(stderr, "Failed to initialize io_uring: ");
PrintException(std::current_exception());
Expand Down
27 changes: 1 addition & 26 deletions src/event/ServerSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,6 @@ class ServerSocket::OneServerSocket final {

static constexpr Domain server_socket_domain("server_socket");

static int
get_remote_uid(SocketDescriptor s) noexcept
{
#ifdef HAVE_STRUCT_UCRED
const auto cred = s.GetPeerCredentials();
if (cred.pid < 0)
return -1;

return cred.uid;
#else
#ifdef HAVE_GETPEEREID
uid_t euid;
gid_t egid;

if (getpeereid(s.Get(), &euid, &egid) == 0)
return euid;
#else
(void)s;
#endif
return -1;
#endif
}

inline void
ServerSocket::OneServerSocket::Accept() noexcept
{
Expand All @@ -143,9 +120,7 @@ ServerSocket::OneServerSocket::Accept() noexcept
(const char *)msg);
}

const auto uid = get_remote_uid(peer_fd);

parent.OnAccept(std::move(peer_fd), peer_address, uid);
parent.OnAccept(std::move(peer_fd), peer_address);
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/event/ServerSocket.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public:

protected:
virtual void OnAccept(UniqueSocketDescriptor fd,
SocketAddress address, int uid) noexcept = 0;
SocketAddress address) noexcept = 0;
};

#endif
39 changes: 0 additions & 39 deletions src/event/UringManager.cxx

This file was deleted.

Loading