From bf43cccf97069e4cd84aae694e1abd58f10342ea Mon Sep 17 00:00:00 2001 From: Jake Ehrlich Date: Wed, 24 May 2023 11:56:16 -0700 Subject: [PATCH 1/2] Make stdin a psuedoterminal when isatty is set --- src/runtime/job.cpp | 10 +++++++++- tools/shim-wake/main.c | 15 +++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/runtime/job.cpp b/src/runtime/job.cpp index 7c160cb3d..c36781be2 100644 --- a/src/runtime/job.cpp +++ b/src/runtime/job.cpp @@ -829,9 +829,17 @@ static void launch(JobTable *jobtable) { jobtable->imp->pipes[stdout_stream[0]] = entry; jobtable->imp->pipes[stderr_stream[0]] = entry; clock_gettime(CLOCK_REALTIME, &entry->job->start); + std::string stdin_file_str = "/dev/null"; + int stdin_stream[2]; + if (!task.stdin_file.empty()) { + stdin_file_str = task.stdin_file; + } else if (task.is_atty) { + create_psuedoterminal(stdin_stream); + stdin_file_str = "#" + std::to_string(stdin_stream[0]); + } std::stringstream prelude; prelude << find_execpath() << "/../lib/wake/shim-wake" << '\0' - << (task.stdin_file.empty() ? "/dev/null" : task.stdin_file.c_str()) << '\0' + << stdin_file_str << '\0' << std::to_string(stdout_stream[1]) << '\0' << std::to_string(stderr_stream[1]) << '\0' << task.dir << '\0'; std::string shim = prelude.str() + task.cmdline; diff --git a/tools/shim-wake/main.c b/tools/shim-wake/main.c index 29c7bafb6..a65b5bc46 100644 --- a/tools/shim-wake/main.c +++ b/tools/shim-wake/main.c @@ -147,10 +147,17 @@ int main(int argc, char **argv) { return 127; } - stdin_fd = open(argv[1], O_RDONLY); - if (stdin_fd == -1) { - fprintf(stderr, "open: %s: %s\n", argv[1], strerror(errno)); - return 127; + // Because we want to be able to read in both file descirptors and + // files for stdin, we use a '#' to denote a file descriptor. + const char* stdin_file = argv[1]; + if (stdin_file[0] == '#') { + stdin_fd = atoi(stdin_file + 1); + } else { + stdin_fd = open(stdin_file, O_RDONLY); + if (stdin_fd == -1) { + fprintf(stderr, "open: %s: %s\n", argv[1], strerror(errno)); + return 127; + } } stdout_fd = atoi(argv[2]); From 693d297f0a34d68ae37f5402c56767c63a03458d Mon Sep 17 00:00:00 2001 From: Jake Ehrlich Date: Wed, 24 May 2023 12:57:27 -0700 Subject: [PATCH 2/2] small fixes --- share/wake/lib/system/job.wake | 5 ++++- src/runtime/job.cpp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/share/wake/lib/system/job.wake b/share/wake/lib/system/job.wake index ad567260f..d291be092 100644 --- a/share/wake/lib/system/job.wake +++ b/share/wake/lib/system/job.wake @@ -999,7 +999,10 @@ export def makeJSONRunner (plan: JSONRunnerPlan): Runner = Fail f = Pair (Fail (makeError f)) "" Pass inFile = def outFile = "{build}/{prefix}.out.json" - def cmd = script, "-I", "-p", inFile, "-o", outFile, extraArgs + def always_args = "-I", "-p", inFile, "-o", outFile, extraArgs + def optional_arg = + if isatty then "-i", Nil else Nil + def cmd = script, optional_arg ++ always_args def proxy = RunnerInput label cmd Nil (extraEnv ++ environment) "." "" Nil prefix (estimate record) isatty diff --git a/src/runtime/job.cpp b/src/runtime/job.cpp index c36781be2..b2b2a5833 100644 --- a/src/runtime/job.cpp +++ b/src/runtime/job.cpp @@ -835,7 +835,7 @@ static void launch(JobTable *jobtable) { stdin_file_str = task.stdin_file; } else if (task.is_atty) { create_psuedoterminal(stdin_stream); - stdin_file_str = "#" + std::to_string(stdin_stream[0]); + stdin_file_str = "#" + std::to_string(stdin_stream[1]); } std::stringstream prelude; prelude << find_execpath() << "/../lib/wake/shim-wake" << '\0'