mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:27:45 +00:00
LibIPC: Make sure FDs survive when passed into a MessageBuffer
This commit is contained in:
parent
cc6db526a6
commit
de9b454f89
3 changed files with 34 additions and 4 deletions
|
@ -65,8 +65,8 @@ public:
|
||||||
buffer.data.prepend(reinterpret_cast<const u8*>(&message_size), sizeof(message_size));
|
buffer.data.prepend(reinterpret_cast<const u8*>(&message_size), sizeof(message_size));
|
||||||
|
|
||||||
#ifdef __serenity__
|
#ifdef __serenity__
|
||||||
for (int fd : buffer.fds) {
|
for (auto& fd : buffer.fds) {
|
||||||
auto rc = sendfd(m_socket->fd(), fd);
|
auto rc = sendfd(m_socket->fd(), fd->value());
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("sendfd");
|
perror("sendfd");
|
||||||
shutdown();
|
shutdown();
|
||||||
|
|
|
@ -148,7 +148,16 @@ Encoder& Encoder::operator<<(const Dictionary& dictionary)
|
||||||
|
|
||||||
Encoder& Encoder::operator<<(const File& file)
|
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;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,13 +7,34 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
|
#include <AK/RefPtr.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
namespace IPC {
|
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 {
|
struct MessageBuffer {
|
||||||
Vector<u8, 1024> data;
|
Vector<u8, 1024> data;
|
||||||
Vector<int> fds;
|
Vector<RefPtr<AutoCloseFileDescriptor>> fds;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Message {
|
class Message {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue