瀏覽代碼

Kernel: Let inodes provide pre-open file descriptions

Some magical inodes, such as /proc/pid/fd/fileno, are going to want to open() to
a custom FileDescription, so add a hook for that.
Sergey Bugaev 5 年之前
父節點
當前提交
8642a7046c
共有 3 個文件被更改,包括 13 次插入2 次删除
  1. 2 0
      Kernel/FileSystem/Inode.h
  2. 3 0
      Kernel/FileSystem/VirtualFileSystem.cpp
  3. 8 2
      Kernel/Process.cpp

+ 2 - 0
Kernel/FileSystem/Inode.h

@@ -61,6 +61,8 @@ public:
     bool bind_socket(LocalSocket&);
     bool unbind_socket();
 
+    virtual FileDescription* preopen_fd() { return nullptr; };
+
     bool is_metadata_dirty() const { return m_metadata_dirty; }
 
     virtual int set_atime(time_t);

+ 3 - 0
Kernel/FileSystem/VirtualFileSystem.cpp

@@ -230,6 +230,9 @@ KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options
             return KResult(-EACCES);
     }
 
+    if (auto preopen_fd = inode.preopen_fd())
+        return *preopen_fd;
+
     if (metadata.is_device()) {
         if (custody.mount_flags() & MS_NODEV)
             return KResult(-EACCES);

+ 8 - 2
Kernel/Process.cpp

@@ -1908,8 +1908,14 @@ int Process::sys$open(const Syscall::SC_open_params* user_params)
     if (result.is_error())
         return result.error();
     auto description = result.value();
-    description->set_rw_mode(options);
-    description->set_file_flags(options);
+    if (description->file_flags()) {
+        // We already have file flags set on this description, so
+        // it must be a preopen description (probably, /proc/pid/fd).
+        // So don't reset its flags and r/w mode.
+    } else {
+        description->set_rw_mode(options);
+        description->set_file_flags(options);
+    }
     u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
     m_fds[fd].set(move(description), fd_flags);
     return fd;