libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
unix_aux.cxx
1// C++
2#include <cassert>
3#include <cstring>
4
5// Cosmos
6#include <cosmos/error/UsageError.hxx>
7#include <cosmos/fs/FileDescriptor.hxx>
8#include <cosmos/net/unix_aux.hxx>
9
10namespace cosmos {
11
12static_assert(sizeof(UnixCredentials) == sizeof(struct ucred), "size mismatch between UnixCredentials and struct ucred!");
13
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());
18}
19
21 this->checkMsg(msg, UnixMessage::RIGHTS);
22
23 clearFDs();
24
25 const uint8_t *data = reinterpret_cast<const uint8_t*>(msg.data());
26
27 auto left = msg.dataLength();
28 FileNum fd;
29
30 while (left >= sizeof(fd)) {
31 std::memcpy(&fd, data, sizeof(fd));
32 data += sizeof(fd);
33 left -= sizeof(fd);
34 m_fds.push_back(fd);
35 }
36
37 if (!m_fds.empty()) {
38 m_unclaimed_fds = true;
39 }
40}
41
43 if (m_fds.empty()) {
44 cosmos_throw (UsageError("Attempt to serialize empty vector of FileNum"));
45 }
46
47 auto ret = this->createMsg(UnixMessage::RIGHTS, sizeof(FileNum) * m_fds.size());
48
49 auto data = this->data(ret);
50
51 for (auto fd: m_fds) {
52 std::memcpy(data, &fd, sizeof(fd));
53 data += sizeof(fd);
54 }
55
56 return ret;
57}
58
59void UnixRightsMessage::closeUnclaimed() {
60 if (m_unclaimed_fds) {
61 for (auto fd: m_fds) {
63 }
64
65 m_fds.clear();
66
67 m_unclaimed_fds = false;
68 }
69}
70
71void UnixCredentialsMessage::deserialize(const ReceiveMessageHeader::ControlMessage &msg) {
72 checkMsg(msg, UnixMessage::CREDENTIALS);
73
74 const uint8_t *data = reinterpret_cast<const uint8_t*>(msg.data());
75 const auto bytes = msg.dataLength();
76
77 if (bytes != sizeof(m_creds)) {
78 cosmos_throw (RuntimeError("SCM_CREDS message with mismatching length encountered"));
79 }
80
81 std::memcpy(&m_creds, data, bytes);
82}
83
84SendMessageHeader::ControlMessage UnixCredentialsMessage::serialize() const {
85 auto ret = this->createMsg(UnixMessage::CREDENTIALS, sizeof(m_creds));
86
87 auto data = this->data(ret);
88
89 std::memcpy(data, &m_creds, sizeof(m_creds));
90
91 return ret;
92}
93
94} // end ns
Thin Wrapper around OS file descriptors.
void close()
Explicitly close the contained FD.
Wrapper for struct cmsghdr used for iterating over received control messages.
const void * data() const
Returns the data portion of the control message.
size_t dataLength() const
The amount of bytes found at data().
Wrapper for struct cmsghdr used for creating new control messages for sending.
bool m_unclaimed_fds
Flag whether "live" FDs in m_fds have not yet been collected.
Definition unix_aux.hxx:171
SendMessageHeader::ControlMessage serialize() const
Serialize a ControlMessage for passing file descriptors.
Definition unix_aux.cxx:42
void deserialize(const ReceiveMessageHeader::ControlMessage &msg)
Parse received file descriptors from the given ControlMessage.
Definition unix_aux.cxx:20
Exception type for logical usage errors within the application.
FileNum
Primitive file descriptor.
Definition types.hxx:32
void setCurrentCreds()
Fill in the credentials from the current process context.
Definition unix_aux.cxx:14