mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
Add close-on-exec flag for file descriptors.
I was surprised to find that dup()'ed fds don't share the close-on-exec flag. That means it has to be stored separately from the FileDescriptor object.
This commit is contained in:
parent
19b9401487
commit
97c799576a
Notes:
sideshowbarker
2024-07-19 16:10:49 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/97c799576a4
11
AK/Vector.h
11
AK/Vector.h
@ -142,6 +142,17 @@ public:
|
||||
m_impl->remove(index);
|
||||
}
|
||||
|
||||
Vector& operator=(const Vector<T>& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
clear();
|
||||
ensureCapacity(other.size());
|
||||
for (const auto& v : other)
|
||||
unchecked_append(v);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void append(Vector<T>&& other)
|
||||
{
|
||||
Vector<T> tmp = move(other);
|
||||
|
@ -375,6 +375,14 @@ int Process::do_exec(const String& path, Vector<String>&& arguments, Vector<Stri
|
||||
memset(m_signal_action_data, 0, sizeof(m_signal_action_data));
|
||||
m_signal_mask = 0xffffffff;
|
||||
|
||||
for (size_t i = 0; i < m_fds.size(); ++i) {
|
||||
auto& daf = m_fds[i];
|
||||
if (daf.descriptor && daf.flags & FD_CLOEXEC) {
|
||||
daf.descriptor->close();
|
||||
daf = { };
|
||||
}
|
||||
}
|
||||
|
||||
InterruptDisabler disabler;
|
||||
Scheduler::prepare_to_modify_tss(*this);
|
||||
|
||||
@ -597,21 +605,22 @@ Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring
|
||||
MM.populate_page_directory(*m_page_directory);
|
||||
|
||||
if (fork_parent) {
|
||||
m_file_descriptors.resize(fork_parent->m_file_descriptors.size());
|
||||
for (size_t i = 0; i < fork_parent->m_file_descriptors.size(); ++i) {
|
||||
if (!fork_parent->m_file_descriptors[i])
|
||||
m_fds.resize(fork_parent->m_fds.size());
|
||||
for (size_t i = 0; i < fork_parent->m_fds.size(); ++i) {
|
||||
if (!fork_parent->m_fds[i].descriptor)
|
||||
continue;
|
||||
#ifdef FORK_DEBUG
|
||||
dbgprintf("fork: cloning fd %u... (%p) istty? %u\n", i, fork_parent->m_file_descriptors[i].ptr(), fork_parent->m_file_descriptors[i]->isTTY());
|
||||
dbgprintf("fork: cloning fd %u... (%p) istty? %u\n", i, fork_parent->m_fds[i].ptr(), fork_parent->m_fds[i]->isTTY());
|
||||
#endif
|
||||
m_file_descriptors[i] = fork_parent->m_file_descriptors[i]->clone();
|
||||
m_fds[i].descriptor = fork_parent->m_fds[i].descriptor->clone();
|
||||
m_fds[i].flags = fork_parent->m_fds[i].flags;
|
||||
}
|
||||
} else {
|
||||
m_file_descriptors.resize(m_max_open_file_descriptors);
|
||||
m_fds.resize(m_max_open_file_descriptors);
|
||||
if (tty) {
|
||||
m_file_descriptors[0] = tty->open(O_RDONLY);
|
||||
m_file_descriptors[1] = tty->open(O_WRONLY);
|
||||
m_file_descriptors[2] = tty->open(O_WRONLY);
|
||||
m_fds[0].set(tty->open(O_RDONLY));
|
||||
m_fds[1].set(tty->open(O_WRONLY));
|
||||
m_fds[2].set(tty->open(O_WRONLY));
|
||||
}
|
||||
}
|
||||
|
||||
@ -929,8 +938,8 @@ FileDescriptor* Process::file_descriptor(int fd)
|
||||
{
|
||||
if (fd < 0)
|
||||
return nullptr;
|
||||
if ((size_t)fd < m_file_descriptors.size())
|
||||
return m_file_descriptors[fd].ptr();
|
||||
if ((size_t)fd < m_fds.size())
|
||||
return m_fds[fd].descriptor.ptr();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -938,8 +947,8 @@ const FileDescriptor* Process::file_descriptor(int fd) const
|
||||
{
|
||||
if (fd < 0)
|
||||
return nullptr;
|
||||
if ((size_t)fd < m_file_descriptors.size())
|
||||
return m_file_descriptors[fd].ptr();
|
||||
if ((size_t)fd < m_fds.size())
|
||||
return m_fds[fd].descriptor.ptr();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -1063,7 +1072,7 @@ int Process::sys$close(int fd)
|
||||
if (!descriptor)
|
||||
return -EBADF;
|
||||
int rc = descriptor->close();
|
||||
m_file_descriptors[fd] = nullptr;
|
||||
m_fds[fd] = { };
|
||||
return rc;
|
||||
}
|
||||
|
||||
@ -1082,15 +1091,19 @@ int Process::sys$fcntl(int fd, int cmd, dword arg)
|
||||
auto* descriptor = file_descriptor(fd);
|
||||
if (!descriptor)
|
||||
return -EBADF;
|
||||
// NOTE: The FD flags are not shared between FileDescriptor objects.
|
||||
// This means that dup() doesn't copy the FD_CLOEXEC flag!
|
||||
switch (cmd) {
|
||||
case F_GETFD:
|
||||
return descriptor->fd_flags();
|
||||
return m_fds[fd].flags;
|
||||
case F_SETFD:
|
||||
return descriptor->set_fd_flags(arg);
|
||||
m_fds[fd].flags = arg;
|
||||
break;
|
||||
case F_GETFL:
|
||||
return descriptor->file_flags();
|
||||
case F_SETFL:
|
||||
return descriptor->set_file_flags(arg);
|
||||
descriptor->set_file_flags(arg);
|
||||
break;
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
@ -1180,7 +1193,7 @@ int Process::sys$getcwd(char* buffer, size_t size)
|
||||
size_t Process::number_of_open_file_descriptors() const
|
||||
{
|
||||
size_t count = 0;
|
||||
for (auto& descriptor : m_file_descriptors) {
|
||||
for (auto& descriptor : m_fds) {
|
||||
if (descriptor)
|
||||
++count;
|
||||
}
|
||||
@ -1204,10 +1217,11 @@ int Process::sys$open(const char* path, int options)
|
||||
|
||||
int fd = 0;
|
||||
for (; fd < (int)m_max_open_file_descriptors; ++fd) {
|
||||
if (!m_file_descriptors[fd])
|
||||
if (!m_fds[fd])
|
||||
break;
|
||||
}
|
||||
m_file_descriptors[fd] = move(descriptor);
|
||||
dword flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
||||
m_fds[fd].set(move(descriptor), flags);
|
||||
return fd;
|
||||
}
|
||||
|
||||
@ -1215,7 +1229,7 @@ int Process::alloc_fd()
|
||||
{
|
||||
int fd = -1;
|
||||
for (int i = 0; i < (int)m_max_open_file_descriptors; ++i) {
|
||||
if (!m_file_descriptors[i]) {
|
||||
if (!m_fds[i]) {
|
||||
fd = i;
|
||||
break;
|
||||
}
|
||||
@ -1231,11 +1245,11 @@ int Process::sys$pipe(int* pipefd)
|
||||
auto fifo = FIFO::create();
|
||||
|
||||
int reader_fd = alloc_fd();
|
||||
m_file_descriptors[reader_fd] = FileDescriptor::create_pipe_reader(*fifo);
|
||||
m_fds[reader_fd].set(FileDescriptor::create_pipe_reader(*fifo));
|
||||
pipefd[0] = reader_fd;
|
||||
|
||||
int writer_fd = alloc_fd();
|
||||
m_file_descriptors[writer_fd] = FileDescriptor::create_pipe_writer(*fifo);
|
||||
m_fds[writer_fd].set(FileDescriptor::create_pipe_writer(*fifo));
|
||||
pipefd[1] = writer_fd;
|
||||
|
||||
return 0;
|
||||
@ -1616,10 +1630,10 @@ int Process::sys$dup(int old_fd)
|
||||
return -EMFILE;
|
||||
int new_fd = 0;
|
||||
for (; new_fd < (int)m_max_open_file_descriptors; ++new_fd) {
|
||||
if (!m_file_descriptors[new_fd])
|
||||
if (!m_fds[new_fd])
|
||||
break;
|
||||
}
|
||||
m_file_descriptors[new_fd] = descriptor;
|
||||
m_fds[new_fd].set(descriptor);
|
||||
return new_fd;
|
||||
}
|
||||
|
||||
@ -1630,7 +1644,7 @@ int Process::sys$dup2(int old_fd, int new_fd)
|
||||
return -EBADF;
|
||||
if (number_of_open_file_descriptors() == m_max_open_file_descriptors)
|
||||
return -EMFILE;
|
||||
m_file_descriptors[new_fd] = descriptor;
|
||||
m_fds[new_fd].set(descriptor);
|
||||
return new_fd;
|
||||
}
|
||||
|
||||
|
@ -252,7 +252,14 @@ private:
|
||||
DWORD m_wakeupTime { 0 };
|
||||
TSS32 m_tss;
|
||||
TSS32 m_tss_to_resume_kernel;
|
||||
Vector<RetainPtr<FileDescriptor>> m_file_descriptors;
|
||||
struct FileDescriptorAndFlags {
|
||||
operator bool() const { return !!descriptor; }
|
||||
void clear() { descriptor = nullptr; flags = 0; }
|
||||
void set(RetainPtr<FileDescriptor>&& d, dword f = 0) { descriptor = move(d), flags = f; }
|
||||
RetainPtr<FileDescriptor> descriptor;
|
||||
dword flags { 0 };
|
||||
};
|
||||
Vector<FileDescriptorAndFlags> m_fds;
|
||||
RingLevel m_ring { Ring0 };
|
||||
int m_error { 0 };
|
||||
void* m_kernelStack { nullptr };
|
||||
|
@ -52,14 +52,14 @@ bool Scheduler::pick_next()
|
||||
if (process.state() == Process::BlockedRead) {
|
||||
ASSERT(process.m_fdBlockedOnRead != -1);
|
||||
// FIXME: Block until the amount of data wanted is available.
|
||||
if (process.m_file_descriptors[process.m_fdBlockedOnRead]->hasDataAvailableForRead())
|
||||
if (process.m_fds[process.m_fdBlockedOnRead].descriptor->hasDataAvailableForRead())
|
||||
process.unblock();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (process.state() == Process::BlockedWrite) {
|
||||
ASSERT(process.m_blocked_fd != -1);
|
||||
if (process.m_file_descriptors[process.m_blocked_fd]->can_write())
|
||||
if (process.m_fds[process.m_blocked_fd].descriptor->can_write())
|
||||
process.unblock();
|
||||
return true;
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ __BEGIN_DECLS
|
||||
#define F_GETFL 3
|
||||
#define F_SETFL 4
|
||||
|
||||
#define FD_CLOEXEC 1
|
||||
|
||||
int fcntl(int fd, int cmd, ...);
|
||||
|
||||
__END_DECLS
|
||||
|
@ -99,6 +99,7 @@ int isatty(int fd);
|
||||
#define O_NONBLOCK 04000
|
||||
#define O_DIRECTORY 00200000
|
||||
#define O_NOFOLLOW 00400000
|
||||
#define O_CLOEXEC 02000000
|
||||
|
||||
__END_DECLS
|
||||
|
||||
|
@ -51,7 +51,6 @@ RetainPtr<FileDescriptor> FileDescriptor::clone()
|
||||
descriptor->m_currentOffset = m_currentOffset;
|
||||
#ifdef SERENITY
|
||||
descriptor->m_isBlocking = m_isBlocking;
|
||||
descriptor->m_fd_flags = m_fd_flags;
|
||||
descriptor->m_file_flags = m_file_flags;
|
||||
#endif
|
||||
return descriptor;
|
||||
|
@ -53,10 +53,7 @@ public:
|
||||
void setBlocking(bool b) { m_isBlocking = b; }
|
||||
|
||||
dword file_flags() const { return m_file_flags; }
|
||||
int set_file_flags(dword flags) { m_file_flags = flags; return 0; }
|
||||
|
||||
dword fd_flags() const { return m_fd_flags; }
|
||||
int set_fd_flags(dword flags) { m_fd_flags = flags; return 0; }
|
||||
void set_file_flags(dword flags) { m_file_flags = flags; }
|
||||
|
||||
bool is_fifo() const { return m_fifo; }
|
||||
FIFO::Direction fifo_direction() { return m_fifo_direction; }
|
||||
@ -77,7 +74,6 @@ private:
|
||||
|
||||
#ifdef SERENITY
|
||||
bool m_isBlocking { true };
|
||||
dword m_fd_flags { 0 };
|
||||
dword m_file_flags { 0 };
|
||||
|
||||
RetainPtr<FIFO> m_fifo;
|
||||
|
@ -25,6 +25,8 @@ namespace Unix {
|
||||
#define F_GETFL 3
|
||||
#define F_SETFL 4
|
||||
|
||||
#define FD_CLOEXEC 1
|
||||
|
||||
/* c_cc characters */
|
||||
#define VINTR 0
|
||||
#define VQUIT 1
|
||||
|
@ -14,8 +14,15 @@
|
||||
#define O_RDONLY 0
|
||||
#define O_WRONLY 1
|
||||
#define O_RDWR 2
|
||||
#define O_CREAT 0100
|
||||
#define O_EXCL 0200
|
||||
#define O_NOCTTY 0400
|
||||
#define O_TRUNC 01000
|
||||
#define O_APPEND 02000
|
||||
#define O_NONBLOCK 04000
|
||||
#define O_DIRECTORY 00200000
|
||||
#define O_NOFOLLOW 00400000
|
||||
#define O_CLOEXEC 02000000
|
||||
#define O_NOFOLLOW_NOERROR 0x4000000
|
||||
|
||||
class CharacterDevice;
|
||||
|
Loading…
Reference in New Issue
Block a user