diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-05-02 12:28:20 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-02 13:26:07 +0200 |
commit | de9b454f897aee3d7b1cd1a8235a40360da5425f (patch) | |
tree | 1af38f2869028d0c3bb2df50189ce089445b2cce /Userland/Libraries/LibIPC | |
parent | cc6db526a6e595918a3e43312df8f724275e4e6a (diff) | |
download | serenity-de9b454f897aee3d7b1cd1a8235a40360da5425f.zip |
LibIPC: Make sure FDs survive when passed into a MessageBuffer
Diffstat (limited to 'Userland/Libraries/LibIPC')
-rw-r--r-- | Userland/Libraries/LibIPC/Connection.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibIPC/Encoder.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibIPC/Message.h | 23 |
3 files changed, 34 insertions, 4 deletions
diff --git a/Userland/Libraries/LibIPC/Connection.h b/Userland/Libraries/LibIPC/Connection.h index 01464b2f16..260919ad7b 100644 --- a/Userland/Libraries/LibIPC/Connection.h +++ b/Userland/Libraries/LibIPC/Connection.h @@ -65,8 +65,8 @@ public: buffer.data.prepend(reinterpret_cast<const u8*>(&message_size), sizeof(message_size)); #ifdef __serenity__ - for (int fd : buffer.fds) { - auto rc = sendfd(m_socket->fd(), fd); + for (auto& fd : buffer.fds) { + auto rc = sendfd(m_socket->fd(), fd->value()); if (rc < 0) { perror("sendfd"); shutdown(); diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp index ee1132dde3..f067ae80d8 100644 --- a/Userland/Libraries/LibIPC/Encoder.cpp +++ b/Userland/Libraries/LibIPC/Encoder.cpp @@ -148,7 +148,16 @@ Encoder& Encoder::operator<<(const Dictionary& dictionary) Encoder& Encoder::operator<<(const File& file) { - m_buffer.fds.append(file.fd()); + int fd = file.fd(); + if (fd != -1) { + auto result = dup(fd); + if (result < 0) { + perror("dup"); + VERIFY_NOT_REACHED(); + } + fd = result; + } + m_buffer.fds.append(adopt_ref(*new AutoCloseFileDescriptor(fd))); return *this; } diff --git a/Userland/Libraries/LibIPC/Message.h b/Userland/Libraries/LibIPC/Message.h index c483909700..0cb74bdde1 100644 --- a/Userland/Libraries/LibIPC/Message.h +++ b/Userland/Libraries/LibIPC/Message.h @@ -7,13 +7,34 @@ #pragma once #include <AK/Function.h> +#include <AK/RefPtr.h> #include <AK/Vector.h> +#include <unistd.h> namespace IPC { +class AutoCloseFileDescriptor : public RefCounted<AutoCloseFileDescriptor> { +public: + AutoCloseFileDescriptor(int fd) + : m_fd(fd) + { + } + + ~AutoCloseFileDescriptor() + { + if (m_fd != -1) + close(m_fd); + } + + int value() const { return m_fd; } + +private: + int m_fd; +}; + struct MessageBuffer { Vector<u8, 1024> data; - Vector<int> fds; + Vector<RefPtr<AutoCloseFileDescriptor>> fds; }; class Message { |