6#include <cosmos/error/UsageError.hxx>
7#include <cosmos/fs/FileDescriptor.hxx>
8#include <cosmos/net/unix_aux.hxx>
12static_assert(
sizeof(UnixCredentials) ==
sizeof(
struct ucred),
"size mismatch between UnixCredentials and struct ucred!");
15 pid = to_integral(cosmos::proc::get_own_pid());
16 uid = to_integral(cosmos::proc::get_effective_user_id());
17 gid = to_integral(cosmos::proc::get_effective_group_id());
21 this->checkMsg(msg, UnixMessage::RIGHTS);
25 const uint8_t *data =
reinterpret_cast<const uint8_t*
>(msg.
data());
30 while (left >=
sizeof(fd)) {
31 std::memcpy(&fd, data,
sizeof(fd));
44 cosmos_throw (
UsageError(
"Attempt to serialize empty vector of FileNum"));
47 auto ret = this->createMsg(UnixMessage::RIGHTS,
sizeof(
FileNum) * m_fds.size());
49 auto data = this->data(ret);
51 for (
auto fd: m_fds) {
52 std::memcpy(data, &fd,
sizeof(fd));
59void UnixRightsMessage::closeUnclaimed() {
61 for (
auto fd: m_fds) {
71void UnixCredentialsMessage::deserialize(
const ReceiveMessageHeader::ControlMessage &msg) {
72 checkMsg(msg, UnixMessage::CREDENTIALS);
74 const uint8_t *data =
reinterpret_cast<const uint8_t*
>(msg.data());
75 const auto bytes = msg.dataLength();
77 if (bytes !=
sizeof(m_creds)) {
78 cosmos_throw (RuntimeError(
"SCM_CREDS message with mismatching length encountered"));
81 std::memcpy(&m_creds, data, bytes);
84SendMessageHeader::ControlMessage UnixCredentialsMessage::serialize()
const {
85 auto ret = this->createMsg(UnixMessage::CREDENTIALS,
sizeof(m_creds));
87 auto data = this->data(ret);
89 std::memcpy(data, &m_creds,
sizeof(m_creds));
Thin Wrapper around OS file descriptors.
void close()
Explicitly close the contained FD.
bool m_unclaimed_fds
Flag whether "live" FDs in m_fds have not yet been collected.
SendMessageHeader::ControlMessage serialize() const
Serialize a ControlMessage for passing file descriptors.
void deserialize(const ReceiveMessageHeader::ControlMessage &msg)
Parse received file descriptors from the given ControlMessage.
Exception type for logical usage errors within the application.
FileNum
Primitive file descriptor.
void setCurrentCreds()
Fill in the credentials from the current process context.