Skip to content

Commit ddcaa0c

Browse files
Mine02C4jdupak
authored andcommitted
CLI: Add support for mapping stdin to a host file in syscall emulation
1 parent 33ef57c commit ddcaa0c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/cli/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ void create_parser(QCommandLineParser &p) {
9696
p.addOption(
9797
{ { "serial-out", "serout" }, "File connected to the serial port output.", "FNAME" });
9898
p.addOption({ { "os-emulation", "osemu" }, "Operating system emulation." });
99+
p.addOption(
100+
{ { "std-in", "stdin" }, "File connected to the syscall standard input.", "FNAME" });
99101
p.addOption(
100102
{ { "std-out", "stdout" }, "File connected to the syscall standard output.", "FNAME" });
101103
p.addOption(
@@ -419,8 +421,13 @@ void configure_serial_port(QCommandLineParser &p, SerialPort *ser_port) {
419421

420422
void configure_osemu(QCommandLineParser &p, MachineConfig &config, Machine *machine) {
421423
CharIOHandler *std_out = nullptr;
424+
QScopedPointer<QString> std_in_path;
422425
int siz;
423426

427+
siz = p.values("std-in").size();
428+
if (siz >= 1) {
429+
std_in_path.reset(new QString(p.values("std-in").at(siz - 1)));
430+
}
424431
siz = p.values("std-out").size();
425432
if (siz >= 1) {
426433
auto *qf = new QFile(p.values("std-out").at(siz - 1));
@@ -438,6 +445,12 @@ void configure_osemu(QCommandLineParser &p, MachineConfig &config, Machine *mach
438445
auto *osemu_handler = new osemu::OsSyscallExceptionHandler(
439446
config.osemu_known_syscall_stop(), config.osemu_unknown_syscall_stop(),
440447
config.osemu_fs_root());
448+
if (!std_in_path.isNull() && !std_in_path->isEmpty()) {
449+
if (!osemu_handler->map_stdin_to_hostfile(*std_in_path)) {
450+
fprintf(stderr, "Failed to map stdin to host file '%s'\n", std_in_path->toLatin1().data());
451+
exit(EXIT_FAILURE);
452+
}
453+
}
441454
if (std_out) {
442455
machine::Machine::connect(
443456
osemu_handler, &osemu::OsSyscallExceptionHandler::char_written, std_out,

src/os_emulation/ossyscall.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,25 @@ bool OsSyscallExceptionHandler::handle_exception(
623623
return true;
624624
}
625625

626+
bool OsSyscallExceptionHandler::map_stdin_to_hostfile(const QString &hostpath) {
627+
if (hostpath.isEmpty()) return false;
628+
int hostfd = open(hostpath.toLatin1().data(), O_RDONLY);
629+
if (hostfd < 0) {
630+
fprintf(stderr, "map_stdin_to_hostfile: failed to open host file '%s' errno=%d", hostpath.toLatin1().data(), errno);
631+
return false;
632+
}
633+
if (fd_mapping.size() <= 0) {
634+
fd_mapping.push_back(FD_UNUSED);
635+
}
636+
// If there is an existing host fd mapped at target fd 0, close it (but don't close FD_TERMINAL/FD_UNUSED/FD_INVALID)
637+
int prev = fd_mapping[0];
638+
if (prev >= 0 && prev != FD_TERMINAL && prev != FD_UNUSED && prev != FD_INVALID) {
639+
close(prev);
640+
}
641+
fd_mapping[0] = hostfd;
642+
return true;
643+
}
644+
626645
int32_t OsSyscallExceptionHandler::write_mem(
627646
machine::FrontendMemory *mem,
628647
Address addr,

src/os_emulation/ossyscall.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ class OsSyscallExceptionHandler : public machine::ExceptionHandler {
3535
machine::Address next_addr,
3636
machine::Address jump_branch_pc,
3737
machine::Address mem_ref_addr) override;
38+
/**
39+
* Map target fd 0 (stdin) to a host file opened from @hostpath.
40+
* Returns true on success (host file opened and mapped), false on error.
41+
*
42+
* Notes:
43+
* - Existing mapping at target fd 0 is closed if it refers to a host fd.
44+
* - Uses filepath_to_host() to resolve @hostpath into host file path respecting fs_root.
45+
*/
46+
bool map_stdin_to_hostfile(const QString &hostpath);
3847
OSSYCALL_HANDLER_DECLARE(syscall_default_handler);
3948
OSSYCALL_HANDLER_DECLARE(do_sys_exit);
4049
OSSYCALL_HANDLER_DECLARE(do_sys_set_thread_area);

0 commit comments

Comments
 (0)