summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AK/Vector.h11
-rw-r--r--Kernel/Process.cpp66
-rw-r--r--Kernel/Process.h9
-rw-r--r--Kernel/Scheduler.cpp4
-rw-r--r--LibC/fcntl.h2
-rw-r--r--LibC/unistd.h1
-rw-r--r--VirtualFileSystem/FileDescriptor.cpp1
-rw-r--r--VirtualFileSystem/FileDescriptor.h6
-rw-r--r--VirtualFileSystem/UnixTypes.h2
-rw-r--r--VirtualFileSystem/VirtualFileSystem.h7
10 files changed, 74 insertions, 35 deletions
diff --git a/AK/Vector.h b/AK/Vector.h
index 0c0a7c3e30..8824e10a46 100644
--- a/AK/Vector.h
+++ b/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);
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp
index fb84a485df..85f51032e8 100644
--- a/Kernel/Process.cpp
+++ b/Kernel/Process.cpp
@@ -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;
}
diff --git a/Kernel/Process.h b/Kernel/Process.h
index c2a76a0571..f7915883cb 100644
--- a/Kernel/Process.h
+++ b/Kernel/Process.h
@@ -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 };
diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp
index a2f746abdb..15a2118c88 100644
--- a/Kernel/Scheduler.cpp
+++ b/Kernel/Scheduler.cpp
@@ -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;
}
diff --git a/LibC/fcntl.h b/LibC/fcntl.h
index e0ee379b1d..c717acdc1a 100644
--- a/LibC/fcntl.h
+++ b/LibC/fcntl.h
@@ -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
diff --git a/LibC/unistd.h b/LibC/unistd.h
index a809a91151..431a5f17bc 100644
--- a/LibC/unistd.h
+++ b/LibC/unistd.h
@@ -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
diff --git a/VirtualFileSystem/FileDescriptor.cpp b/VirtualFileSystem/FileDescriptor.cpp
index 6a3898aeac..df2ffcfd6c 100644
--- a/VirtualFileSystem/FileDescriptor.cpp
+++ b/VirtualFileSystem/FileDescriptor.cpp
@@ -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;
diff --git a/VirtualFileSystem/FileDescriptor.h b/VirtualFileSystem/FileDescriptor.h
index ec3753de9d..6e41729d8f 100644
--- a/VirtualFileSystem/FileDescriptor.h
+++ b/VirtualFileSystem/FileDescriptor.h
@@ -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;
diff --git a/VirtualFileSystem/UnixTypes.h b/VirtualFileSystem/UnixTypes.h
index d47bc68253..954854a763 100644
--- a/VirtualFileSystem/UnixTypes.h
+++ b/VirtualFileSystem/UnixTypes.h
@@ -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
diff --git a/VirtualFileSystem/VirtualFileSystem.h b/VirtualFileSystem/VirtualFileSystem.h
index 3ef1597899..782c24b271 100644
--- a/VirtualFileSystem/VirtualFileSystem.h
+++ b/VirtualFileSystem/VirtualFileSystem.h
@@ -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;