2020-11-21 18:59:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
2020-11-21 18:59:12 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-21 18:59:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-01-14 08:31:21 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2024-04-16 19:46:30 +00:00
|
|
|
#include <LibCore/System.h>
|
2021-01-14 08:31:21 +00:00
|
|
|
|
2020-11-21 18:59:12 +00:00
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
class File {
|
2021-01-14 08:31:21 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(File);
|
|
|
|
|
2020-11-21 18:59:12 +00:00
|
|
|
public:
|
2021-09-16 07:01:30 +00:00
|
|
|
File() = default;
|
2020-11-21 18:59:12 +00:00
|
|
|
|
2024-04-16 19:46:30 +00:00
|
|
|
static File adopt_file(NonnullOwnPtr<Core::File> file)
|
2020-11-21 18:59:12 +00:00
|
|
|
{
|
2024-04-16 19:46:30 +00:00
|
|
|
return File(file->leak_fd(Badge<File> {}));
|
2020-11-21 18:59:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 19:46:30 +00:00
|
|
|
static File adopt_fd(int fd)
|
2021-01-14 08:31:21 +00:00
|
|
|
{
|
2024-04-16 19:46:30 +00:00
|
|
|
return File(fd);
|
2021-01-14 08:31:21 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 19:46:30 +00:00
|
|
|
static ErrorOr<File> clone_fd(int fd)
|
2022-12-17 16:43:36 +00:00
|
|
|
{
|
2024-04-16 19:46:30 +00:00
|
|
|
int new_fd = TRY(Core::System::dup(fd));
|
|
|
|
return File(new_fd);
|
2022-12-17 16:43:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 08:31:21 +00:00
|
|
|
File(File&& other)
|
|
|
|
: m_fd(exchange(other.m_fd, -1))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
File& operator=(File&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
m_fd = exchange(other.m_fd, -1);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~File()
|
|
|
|
{
|
2024-04-16 19:46:30 +00:00
|
|
|
if (m_fd != -1)
|
|
|
|
(void)Core::System::close(m_fd);
|
2021-01-14 08:31:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:59:12 +00:00
|
|
|
int fd() const { return m_fd; }
|
|
|
|
|
2021-01-14 08:31:21 +00:00
|
|
|
// NOTE: This is 'const' since generated IPC messages expose all parameters by const reference.
|
|
|
|
[[nodiscard]] int take_fd() const
|
|
|
|
{
|
|
|
|
return exchange(m_fd, -1);
|
|
|
|
}
|
|
|
|
|
2024-06-25 20:57:16 +00:00
|
|
|
// FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding.
|
|
|
|
// Perhaps we should add an option to IPC::File to allow the receiver to decide whether to
|
|
|
|
// make it O_CLOEXEC or not. Or an attribute in the .ipc file?
|
|
|
|
ErrorOr<void> clear_close_on_exec()
|
|
|
|
{
|
|
|
|
auto fd_flags = TRY(Core::System::fcntl(m_fd, F_GETFD));
|
|
|
|
fd_flags &= ~FD_CLOEXEC;
|
|
|
|
TRY(Core::System::fcntl(m_fd, F_SETFD, fd_flags));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:59:12 +00:00
|
|
|
private:
|
2024-04-16 19:46:30 +00:00
|
|
|
explicit File(int fd)
|
|
|
|
: m_fd(fd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-14 08:31:21 +00:00
|
|
|
mutable int m_fd { -1 };
|
2020-11-21 18:59:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|