@@ -19,9 +19,11 @@ namespace tinykvm
1919 {
2020 int real_fd = -1 ;
2121 bool is_writable = false ;
22+ bool is_forked = false ;
2223 };
2324 using open_readable_t = std::function<bool (std::string&)>;
2425 using open_writable_t = std::function<bool (std::string&)>;
26+ using find_readonly_master_vm_fd_t = std::function<std::optional<const Entry*>(int )>;
2527
2628 FileDescriptors (Machine& machine);
2729 ~FileDescriptors ();
@@ -37,13 +39,22 @@ namespace tinykvm
3739 // / @param vfd The virtual file descriptor to remove.
3840 void free (int vfd);
3941
40- std::optional<Entry*> entry_for_vfd (int vfd);
42+ std::optional<const Entry*> entry_for_vfd (int vfd) const ;
4143
4244 // / @brief Translate a virtual file descriptor to a real file descriptor,
4345 // / or throw an exception, failing execution.
4446 // / @param vfd The virtual file descriptor to translate.
4547 // / @return The real file descriptor.
46- int translate (int vfd) const ;
48+ int translate (int vfd);
49+
50+ // / @brief Check if a file descriptor is a socket or a file. If this fd was
51+ // / created by duplicating an fd from the main VM, this function instead
52+ // / returns -1, preventing a disallowed operation on the fd. Eg. it's allowed
53+ // / to close a duplicated fd, but not use epoll_ctl() on it.
54+ // / @param vfd The virtual file descriptor to check.
55+ // / @return The real file descriptor, or -1 if the fd was created by
56+ // / duplicating an fd from the main VM.
57+ int translate_unless_forked (int vfd);
4758
4859 bool is_socket_vfd (int vfd) const noexcept {
4960 return (vfd & SOCKET_BIT) != 0 ;
@@ -97,6 +108,14 @@ namespace tinykvm
97108 m_verbose = verbose;
98109 }
99110
111+ // / @brief Set the callback for finding the read-only master VM file descriptor.
112+ // / This is used to find the real file descriptor for a virtual file
113+ // / descriptor that is a read-only master VM file descriptor.
114+ // / @param callback The callback to set.
115+ void set_find_readonly_master_vm_fd_callback (find_readonly_master_vm_fd_t callback) noexcept {
116+ m_find_ro_master_vm_fd = callback;
117+ }
118+
100119 private:
101120 Machine& m_machine;
102121 std::map<int , Entry> m_fds;
@@ -108,5 +127,6 @@ namespace tinykvm
108127 bool m_verbose = false ;
109128 open_readable_t m_open_readable;
110129 open_writable_t m_open_writable;
130+ find_readonly_master_vm_fd_t m_find_ro_master_vm_fd;
111131 };
112132}
0 commit comments