Skip to content

Commit c9640c7

Browse files
committed
Add current working directory to FDs
1 parent 6b4879a commit c9640c7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/tinykvm/linux/fds.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ namespace tinykvm
214214
/// @return True if the socket address is allowed, false otherwise.
215215
bool validate_socket_address(const int socket_fd, struct sockaddr_storage& socket_address) const noexcept;
216216

217+
/// @brief Set the current working directory. This is used in a few system
218+
/// calls, such as getcwd() and chdir().
219+
/// @param path The path to set as the current working directory.
220+
void set_current_working_directory(const std::string& path) noexcept {
221+
m_current_working_directory = path;
222+
}
223+
224+
/// @brief Get the current working directory. This is used in a few system
225+
/// calls, such as getcwd() and chdir().
226+
/// @return The current working directory.
227+
const std::string& current_working_directory() const noexcept {
228+
return m_current_working_directory;
229+
}
230+
217231
/// @brief Set verbose mode. This will print out information about
218232
/// file descriptor management.
219233
/// @param verbose True to enable verbose mode, false to disable it.
@@ -238,6 +252,7 @@ namespace tinykvm
238252
int m_next_file_fd = 0x1000;
239253
int m_next_socket_fd = 0x1000 | SOCKET_BIT;
240254
std::array<int, 3> m_stdout_redirects { 0, 1, 2 };
255+
std::string m_current_working_directory;
241256
bool m_verbose = false;
242257
open_readable_t m_open_readable;
243258
open_writable_t m_open_writable;

lib/tinykvm/linux/system_calls.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,14 +1334,20 @@ void Machine::setup_linux_system_calls()
13341334
Machine::install_syscall_handler(
13351335
SYS_getcwd, [](vCPU& cpu) { // GETCWD
13361336
auto& regs = cpu.registers();
1337+
const uint64_t g_buf = regs.rdi;
1338+
const size_t buflen = regs.rsi;
13371339

1338-
const char fakepath[] = "/";
1339-
if (sizeof(fakepath) <= regs.rsi) {
1340-
cpu.machine().copy_to_guest(regs.rdi, fakepath, sizeof(fakepath));
1341-
regs.rax = regs.rdi;
1340+
const std::string& cwd = cpu.machine().fds().current_working_directory();
1341+
if (cwd.size()+1 <= buflen) {
1342+
// Copy the current working directory to the guest, including
1343+
// the null terminator.
1344+
cpu.machine().copy_to_guest(g_buf, cwd.c_str(), cwd.size()+1);
1345+
regs.rax = g_buf;
13421346
} else {
13431347
regs.rax = 0;
13441348
}
1349+
SYSPRINT("getcwd(buf=0x%lX (%s), buflen=%zu) = %lld\n",
1350+
g_buf, cwd.c_str(), buflen, regs.rax);
13451351
cpu.set_registers(regs);
13461352
});
13471353
Machine::install_syscall_handler(

0 commit comments

Comments
 (0)