Skip to content

Commit

Permalink
Posix only: shell, popen kill parent if child killed by signal.
Browse files Browse the repository at this point in the history
Don't know how to do this on Windows and can't test it.
Main reason for change: to allow killing batch test runs.
  • Loading branch information
skaller committed Jun 20, 2022
1 parent c631dbb commit 1d9ebae
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/packages/program.fdoc
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,8 @@ class Shell_class[OS, process_status_t]
//------------------------------------------------------------
// system() function

//$ System command is ISO C and C++ standard.
gen raw_system: string -> int = "::std::system($1.c_str())"
requires Cxx_headers::cstdlib
;
virtual gen raw_system: string -> int;

//$ basic command with line quoting.
gen basic_system (cmd: string) :int =>
cmd.quote_line_for_system.raw_system
Expand Down Expand Up @@ -927,17 +925,35 @@ class Bash {
fun quote_line_for_system (s:string) => s;
fun quote_line_for_popen (s:string) => s + " ";

//$ System command is ISO C and C++ standard.
gen c_system: string -> int = "::std::system($1.c_str())"
requires Cxx_headers::cstdlib
;
gen raw_system (cmd:string) : int = {
var result = c_system cmd;
var posix_result = C_hack::cast[PosixProcess::process_status_t] result;
if PosixProcess::WIFSIGNALED posix_result do
eprintln "[system] Child process terminated by signal, aborting parent";
System::exit 1;
done
return result;
}

gen raw_get_stdout(x:string) = {
var fout = PosixProcess::popen_in(x+" ");
if valid fout do
var output = load fout;
var fout = PosixProcess::popen_in(x+" ");
if valid fout do
var output = load fout;

var result = PosixProcess::pclose fout;
return PosixProcess::WEXITSTATUS result, output;
else
println$ "Unable to run command '" + x "'";
return -1,"";
var result = PosixProcess::pclose fout;
if PosixProcess::WIFSIGNALED result do
eprintln "[get_stdout] Child process terminated by signal, aborting parent";
System::exit 1;
done
return PosixProcess::WEXITSTATUS result, output;
else
println$ "Unable to run command '" + x "'";
return -1,"";
done
}

//$ Parse a bash command line into words.
Expand Down Expand Up @@ -1061,6 +1077,12 @@ class CmdExe
fun quote_line_for_system(s:string) => '"' + s + '"';
fun quote_line_for_popen(s:string) => '"' + s + '"';

//$ System command is ISO C and C++ standard.
gen c_system: string -> int = "::std::system($1.c_str())"
requires Cxx_headers::cstdlib
;
gen raw_system (cmd:string) => c_system cmd;

gen raw_get_stdout(x:string) = {
//eprintln("CMD.EXE: raw_get_stout of " + x);
var fout = Win32Process::popen_in(x);
Expand Down

0 comments on commit 1d9ebae

Please sign in to comment.