@@ -32,11 +32,16 @@ namespace tinykvm
3232 : m_machine(machine),
3333 m_current_working_directory_fd (AT_FDCWD)
3434 {
35- // XXX: TODO: Create proper redirects for stdout/stderr by
36- // for example providing a pipe to stdout/stderr.
37- m_fds[0 ] = Entry{ .real_fd = 0 , .is_writable = false }; // stdin
38- m_fds[1 ] = Entry{ .real_fd = 1 , .is_writable = true }; // stdout
39- m_fds[2 ] = Entry{ .real_fd = 2 , .is_writable = true }; // stderr
35+ // Create proper redirects for stdin/stdout/stderr
36+ const int stdin_fd = dup (0 );
37+ const int stdout_fd = dup (1 );
38+ const int stderr_fd = dup (2 );
39+ if (stdin_fd < 0 || stdout_fd < 0 || stderr_fd < 0 ) {
40+ throw std::runtime_error (" TinyKVM: Failed to duplicate stdin/stdout/stderr" );
41+ }
42+ m_fds[0 ] = Entry{ .real_fd = stdin_fd, .is_writable = false }; // stdin
43+ m_fds[1 ] = Entry{ .real_fd = stdout_fd, .is_writable = true }; // stdout
44+ m_fds[2 ] = Entry{ .real_fd = stderr_fd, .is_writable = true }; // stderr
4045 }
4146
4247 FileDescriptors::~FileDescriptors ()
@@ -166,7 +171,8 @@ namespace tinykvm
166171
167172 int FileDescriptors::manage (int fd, bool is_socket, bool is_writable)
168173 {
169- if (fd < 0 ) {
174+ (void )is_socket; // Unused
175+ if (fd <= 2 ) {
170176 throw std::runtime_error (" TinyKVM: Invalid file descriptor in FileDescriptors::add()" );
171177 }
172178 if (this ->m_max_total_fds_opened != 0 && this ->m_total_fds_opened >= this ->m_max_total_fds_opened ) {
@@ -213,7 +219,7 @@ namespace tinykvm
213219 FileDescriptors::Entry& FileDescriptors::manage_as (int vfd, int fd, bool is_socket, bool is_writable)
214220 {
215221 (void )is_socket; // Unused
216- if (fd < 0 ) {
222+ if (fd <= 2 ) {
217223 throw std::runtime_error (" TinyKVM: Invalid fd in FileDescriptors::manage_as()" );
218224 }
219225 if (this ->m_max_total_fds_opened != 0 &&
0 commit comments