aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Waterman <andrew@sifive.com>2023-04-24 15:37:01 -0700
committerGitHub <noreply@github.com>2023-04-24 15:37:01 -0700
commit19a7f343af6fa197f3234e0630d10bd1e8db9849 (patch)
tree7d5a7acea6260310accb8e419181f499d9fb0803
parent7e82fd7d19264d95675e884f314adc20cdee0b62 (diff)
parent5afa62e541b67a5ebd03a8783858484afc7920e1 (diff)
downloadspike-19a7f343af6fa197f3234e0630d10bd1e8db9849.zip
spike-19a7f343af6fa197f3234e0630d10bd1e8db9849.tar.gz
spike-19a7f343af6fa197f3234e0630d10bd1e8db9849.tar.bz2
Merge pull request #1333 from poemonsense/fix-syscall-close
Close file descriptors in destructor of syscall_t
-rw-r--r--fesvr/syscall.cc13
-rw-r--r--fesvr/syscall.h2
2 files changed, 12 insertions, 3 deletions
diff --git a/fesvr/syscall.cc b/fesvr/syscall.cc
index 875ffb7..e277be1 100644
--- a/fesvr/syscall.cc
+++ b/fesvr/syscall.cc
@@ -174,9 +174,16 @@ syscall_t::syscall_t(htif_t* htif)
if (stdin_fd < 0 || stdout_fd0 < 0 || stdout_fd1 < 0)
throw std::runtime_error("could not dup stdin/stdout");
- fds.alloc(stdin_fd); // stdin -> stdin
- fds.alloc(stdout_fd0); // stdout -> stdout
- fds.alloc(stdout_fd1); // stderr -> stdout
+ fds_index.push_back(fds.alloc(stdin_fd)); // stdin -> stdin
+ fds_index.push_back(fds.alloc(stdout_fd0)); // stdout -> stdout
+ fds_index.push_back(fds.alloc(stdout_fd1)); // stderr -> stdout
+}
+
+syscall_t::~syscall_t() {
+ for (auto i: fds_index) {
+ close(fds.lookup(i));
+ fds.dealloc(i);
+ }
}
std::string syscall_t::do_chroot(const char* fn)
diff --git a/fesvr/syscall.h b/fesvr/syscall.h
index 4915efd..c002e6c 100644
--- a/fesvr/syscall.h
+++ b/fesvr/syscall.h
@@ -28,6 +28,7 @@ class syscall_t : public device_t
{
public:
syscall_t(htif_t*);
+ ~syscall_t();
void set_chroot(const char* where);
@@ -38,6 +39,7 @@ class syscall_t : public device_t
memif_t* memif;
std::vector<syscall_func_t> table;
fds_t fds;
+ std::vector<reg_t> fds_index;
void handle_syscall(command_t cmd);
void dispatch(addr_t mm);