mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:27:43 +00:00
Add a /proc/PID/fds text files that lists all the fds open in a process.
This commit is contained in:
parent
065f0aee35
commit
fd03776443
5 changed files with 44 additions and 5 deletions
|
@ -27,6 +27,22 @@ ProcFileSystem::~ProcFileSystem()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ByteBuffer procfs$pid_fds(const Process& process)
|
||||||
|
{
|
||||||
|
char* buffer;
|
||||||
|
auto stringImpl = StringImpl::createUninitialized(process.number_of_open_file_descriptors() * 80, buffer);
|
||||||
|
memset(buffer, 0, stringImpl->length());
|
||||||
|
char* ptr = buffer;
|
||||||
|
for (size_t i = 0; i < process.max_open_file_descriptors(); ++i) {
|
||||||
|
auto* handle = process.file_descriptor(i);
|
||||||
|
if (!handle)
|
||||||
|
continue;
|
||||||
|
ptr += ksprintf(ptr, "% 3u %s\n", i, handle->absolute_path().characters());
|
||||||
|
}
|
||||||
|
*ptr = '\0';
|
||||||
|
return ByteBuffer::copy((byte*)buffer, ptr - buffer);
|
||||||
|
}
|
||||||
|
|
||||||
ByteBuffer procfs$pid_vm(const Process& process)
|
ByteBuffer procfs$pid_vm(const Process& process)
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
|
@ -109,6 +125,7 @@ void ProcFileSystem::addProcess(Process& process)
|
||||||
m_pid2inode.set(process.pid(), dir.index());
|
m_pid2inode.set(process.pid(), dir.index());
|
||||||
addFile(createGeneratedFile("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
|
addFile(createGeneratedFile("vm", [&process] { return procfs$pid_vm(process); }), dir.index());
|
||||||
addFile(createGeneratedFile("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
|
addFile(createGeneratedFile("stack", [&process] { return procfs$pid_stack(process); }), dir.index());
|
||||||
|
addFile(createGeneratedFile("fds", [&process] { return procfs$pid_fds(process); }), dir.index());
|
||||||
if (process.executableInode().isValid())
|
if (process.executableInode().isValid())
|
||||||
addFile(createGeneratedFile("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
|
addFile(createGeneratedFile("exe", [&process] { return procfs$pid_exe(process); }, 00120777), dir.index());
|
||||||
}
|
}
|
||||||
|
|
|
@ -867,8 +867,9 @@ int Process::sys$close(int fd)
|
||||||
auto* handle = fileHandleIfExists(fd);
|
auto* handle = fileHandleIfExists(fd);
|
||||||
if (!handle)
|
if (!handle)
|
||||||
return -EBADF;
|
return -EBADF;
|
||||||
// FIXME: Implement.
|
int rc = handle->close();
|
||||||
return 0;
|
m_file_descriptors[fd] = nullptr;
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Process::sys$lstat(const char* path, Unix::stat* statbuf)
|
int Process::sys$lstat(const char* path, Unix::stat* statbuf)
|
||||||
|
@ -966,9 +967,13 @@ int Process::sys$open(const char* path, int options)
|
||||||
if (options & O_DIRECTORY && !handle->isDirectory())
|
if (options & O_DIRECTORY && !handle->isDirectory())
|
||||||
return -ENOTDIR; // FIXME: This should be handled by VFS::open.
|
return -ENOTDIR; // FIXME: This should be handled by VFS::open.
|
||||||
|
|
||||||
int fd = m_file_descriptors.size();
|
int fd = 0;
|
||||||
|
for (; fd < m_max_open_file_descriptors; ++fd) {
|
||||||
|
if (!m_file_descriptors[fd])
|
||||||
|
break;
|
||||||
|
}
|
||||||
handle->setFD(fd);
|
handle->setFD(fd);
|
||||||
m_file_descriptors.append(move(handle));
|
m_file_descriptors[fd] = move(handle);
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -136,6 +136,9 @@ public:
|
||||||
InodeIdentifier executableInode() const { return m_executable ? m_executable->inode : InodeIdentifier(); }
|
InodeIdentifier executableInode() const { return m_executable ? m_executable->inode : InodeIdentifier(); }
|
||||||
|
|
||||||
size_t number_of_open_file_descriptors() const;
|
size_t number_of_open_file_descriptors() const;
|
||||||
|
size_t max_open_file_descriptors() const { return m_max_open_file_descriptors; }
|
||||||
|
FileHandle* file_descriptor(size_t i) { return m_file_descriptors[i].ptr(); }
|
||||||
|
const FileHandle* file_descriptor(size_t i) const { return m_file_descriptors[i].ptr(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class MemoryManager;
|
friend class MemoryManager;
|
||||||
|
|
|
@ -175,3 +175,15 @@ const TTY* FileHandle::tty() const
|
||||||
return static_cast<const TTY*>(device);
|
return static_cast<const TTY*>(device);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int FileHandle::close()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
String FileHandle::absolute_path() const
|
||||||
|
{
|
||||||
|
if (isTTY())
|
||||||
|
return tty()->ttyName();
|
||||||
|
return VirtualFileSystem::the().absolutePath(m_vnode->inode);
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ public:
|
||||||
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&);
|
explicit FileHandle(RetainPtr<VirtualFileSystem::Node>&&);
|
||||||
~FileHandle();
|
~FileHandle();
|
||||||
|
|
||||||
|
int close();
|
||||||
|
|
||||||
Unix::off_t seek(Unix::off_t, int whence);
|
Unix::off_t seek(Unix::off_t, int whence);
|
||||||
Unix::ssize_t read(byte*, Unix::size_t);
|
Unix::ssize_t read(byte*, Unix::size_t);
|
||||||
Unix::ssize_t write(const byte* data, Unix::size_t);
|
Unix::ssize_t write(const byte* data, Unix::size_t);
|
||||||
|
@ -22,7 +24,7 @@ public:
|
||||||
|
|
||||||
ByteBuffer readEntireFile();
|
ByteBuffer readEntireFile();
|
||||||
|
|
||||||
String absolutePath() const;
|
String absolute_path() const;
|
||||||
|
|
||||||
bool isDirectory() const;
|
bool isDirectory() const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue