Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4752,16 +4752,20 @@ public static IRubyObject popen(ThreadContext context, IRubyObject recv, IRubyOb

if (isDash(pOpen.cmd)) throw runtime.newNotImplementedError("popen(\"-\") is unimplemented");

ShellLauncher.Redirects redirects = ShellLauncher.Redirects.NONE;
if (options != null) {
redirects = ShellLauncher.Redirects.from(context, options, false);
String[] unsupported = redirects.handlesErr() ?
UNSUPPORTED_POPEN_OPTIONS_WITH_ERR : UNSUPPORTED_SPAWN_OPTIONS;
checkUnsupportedOptions(context, options, unsupported, "unsupported popen option");
}

try {
ShellLauncher.POpenProcess process;
if (pOpen.cmdPlusArgs == null) {
process = ShellLauncher.popen(runtime, pOpen.cmd, modes);
process = ShellLauncher.popen(runtime, pOpen.cmd, modes, redirects);
} else {
process = ShellLauncher.popen(runtime, pOpen.cmdPlusArgs, pOpen.env, modes);
}

if (options != null) {
checkUnsupportedOptions(context, options, UNSUPPORTED_SPAWN_OPTIONS, "unsupported popen option");
process = ShellLauncher.popen(runtime, pOpen.cmdPlusArgs, pOpen.env, modes, redirects);
}

io.setupPopen(context, modes, process);
Expand Down Expand Up @@ -5426,6 +5430,7 @@ protected IOOptions updateIOOptionsFromOptions(ThreadContext context, RubyHash o

static final Set<String> ALL_SPAWN_OPTIONS;
static final String[] UNSUPPORTED_SPAWN_OPTIONS;
private static final String[] UNSUPPORTED_POPEN_OPTIONS_WITH_ERR;

static {
String[] SPAWN_OPTIONS = new String[] {
Expand Down Expand Up @@ -5454,6 +5459,9 @@ protected IOOptions updateIOOptionsFromOptions(ThreadContext context, RubyHash o
};

ALL_SPAWN_OPTIONS = new HashSet<>(Arrays.asList(SPAWN_OPTIONS));
UNSUPPORTED_POPEN_OPTIONS_WITH_ERR = Arrays.stream(UNSUPPORTED_SPAWN_OPTIONS)
.filter(option -> !option.equals("err"))
.toArray(String[]::new);
}

@Deprecated(since = "9.2.1.0")
Expand Down
25 changes: 16 additions & 9 deletions core/src/main/java/org/jruby/RubyKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1925,14 +1925,20 @@ private static int systemCommon(ThreadContext context, IRubyObject recv, IRubyOb
Ruby runtime = context.runtime;
long[] tuple;

ShellLauncher.Redirects redirects = ShellLauncher.Redirects.NONE;
if (args[args.length - 1] instanceof RubyHash opts) {
redirects = ShellLauncher.Redirects.from(context, opts, true);
warnUnsupportedSystemOptions(context, opts, redirects);
args = Arrays.copyOf(args, args.length - 1);
}

try {
args = dropLastArgIfOptions(context, args);
if (! Platform.IS_WINDOWS && args[args.length -1].asJavaString().matches(".*[^&]&\\s*")) {
// looks like we need to send process to the background
ShellLauncher.runWithoutWait(runtime, args);
ShellLauncher.runWithoutWait(runtime, args, redirects);
return 0;
}
tuple = ShellLauncher.runAndWaitPid(runtime, args);
tuple = ShellLauncher.runAndWaitPid(runtime, args, redirects);
} catch (Exception e) {
tuple = new long[] {127, -1};
}
Expand All @@ -1942,14 +1948,15 @@ private static int systemCommon(ThreadContext context, IRubyObject recv, IRubyOb
return (int) tuple[0];
}

private static IRubyObject[] dropLastArgIfOptions(ThreadContext context, final IRubyObject[] args) {
IRubyObject lastArg = args[args.length - 1];
if (lastArg instanceof RubyHash hash) {
if (!hash.isEmpty()) Warn.warn(context, "system does not support options in JRuby yet: " + lastArg);
private static void warnUnsupportedSystemOptions(ThreadContext context, RubyHash opts,
ShellLauncher.Redirects redirects) {
RubyHash unsupported = opts.dupFast(context);
if (redirects.handlesOut()) unsupported.fastDelete(asSymbol(context, "out"));
if (redirects.handlesErr()) unsupported.fastDelete(asSymbol(context, "err"));

return Arrays.copyOf(args, args.length - 1);
if (!unsupported.isEmpty()) {
Warn.warn(context, "system does not support options in JRuby yet: " + unsupported);
}
return args;
}

public static IRubyObject exec(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Expand Down
132 changes: 122 additions & 10 deletions core/src/main/java/org/jruby/util/ShellLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static com.headius.backport9.buffer.Buffers.flipBuffer;
import static java.lang.System.out;
import static org.jruby.api.Access.objectClass;
import static org.jruby.api.Convert.asSymbol;
import static org.jruby.api.Create.newHash;
import static org.jruby.api.Create.newString;
import static org.jruby.api.Error.argumentError;
Expand Down Expand Up @@ -68,6 +69,7 @@
import org.jruby.main.Main;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFile;
import org.jruby.RubyHash;
import org.jruby.RubyIO;
import org.jruby.RubyInstanceConfig;
Expand Down Expand Up @@ -467,11 +469,19 @@ public static int runAndWait(Ruby runtime, IRubyObject[] rawArgs) {
}

public static long[] runAndWaitPid(Ruby runtime, IRubyObject[] rawArgs) {
return runAndWaitPid(runtime, rawArgs, runtime.getOutputStream(), true);
return runAndWaitPid(runtime, rawArgs, Redirects.NONE);
}

public static long[] runAndWaitPid(Ruby runtime, IRubyObject[] rawArgs, Redirects redirects) {
return runAndWaitPid(runtime, rawArgs, runtime.getOutputStream(), true, redirects);
}

public static long runWithoutWait(Ruby runtime, IRubyObject[] rawArgs) {
return runWithoutWait(runtime, rawArgs, runtime.getOutputStream());
return runWithoutWait(runtime, rawArgs, Redirects.NONE);
}

public static long runWithoutWait(Ruby runtime, IRubyObject[] rawArgs, Redirects redirects) {
return runWithoutWait(runtime, rawArgs, runtime.getOutputStream(), redirects);
}

public static int runExternalAndWait(Ruby runtime, IRubyObject[] rawArgs, Map mergeEnv) {
Expand Down Expand Up @@ -591,6 +601,20 @@ public static Process buildProcess(Ruby runtime, String[] args, String[] env, Fi
.start();
}

public static Process buildProcess(Ruby runtime, String[] args, String[] env, File pwd,
Redirects redirects) throws IOException {
if (redirects.isEmpty()) return buildProcess(runtime, args, env, pwd);

// jnr-posix's ProcessMaker does not implement redirects, so use ProcessBuilder directly
ProcessBuilder builder = new ProcessBuilder(args).directory(pwd);
Map<String, String> environment = builder.environment();
environment.clear();
environment.putAll(runtime.getPosix().newProcessMaker().environment(env).environment());
redirects.applyTo(builder);

return builder.start();
}

public static long runExternalWithoutWait(Ruby runtime, IRubyObject[] rawArgs) {
return runWithoutWait(runtime, rawArgs, runtime.getOutputStream());
}
Expand Down Expand Up @@ -633,10 +657,15 @@ public static int runAndWait(Ruby runtime, IRubyObject[] rawArgs, OutputStream o
}

public static long[] runAndWaitPid(Ruby runtime, IRubyObject[] rawArgs, OutputStream output, boolean doExecutableSearch) {
return runAndWaitPid(runtime, rawArgs, output, doExecutableSearch, Redirects.NONE);
}

private static long[] runAndWaitPid(Ruby runtime, IRubyObject[] rawArgs, OutputStream output, boolean doExecutableSearch,
Redirects redirects) {
OutputStream error = runtime.getErrorStream();
InputStream input = runtime.getInputStream();
try {
Process aProcess = run(runtime, rawArgs, doExecutableSearch);
Process aProcess = run(runtime, rawArgs, doExecutableSearch, redirects);
handleStreams(runtime, aProcess, input, output, error);
return new long[] {aProcess.waitFor(), getPidFromProcess(aProcess)};
} catch (IOException e) {
Expand All @@ -647,9 +676,13 @@ public static long[] runAndWaitPid(Ruby runtime, IRubyObject[] rawArgs, OutputSt
}

private static long runWithoutWait(Ruby runtime, IRubyObject[] rawArgs, OutputStream output) {
return runWithoutWait(runtime, rawArgs, output, Redirects.NONE);
}

private static long runWithoutWait(Ruby runtime, IRubyObject[] rawArgs, OutputStream output, Redirects redirects) {
OutputStream error = runtime.getErrorStream();
try {
Process aProcess = run(runtime, rawArgs, true);
Process aProcess = run(runtime, rawArgs, true, redirects);
handleStreamsNonblocking(runtime, aProcess, output, error);
return getPidFromProcess(aProcess);
} catch (IOException e) {
Expand Down Expand Up @@ -831,11 +864,21 @@ public static Process run(Ruby runtime, IRubyObject string) throws IOException {
}

public static POpenProcess popen(Ruby runtime, IRubyObject string, ModeFlags modes) throws IOException {
return new POpenProcess(popenShared(runtime, new IRubyObject[] {string}, null, true), runtime, modes);
return popen(runtime, string, modes, Redirects.NONE);
}

public static POpenProcess popen(Ruby runtime, IRubyObject string, ModeFlags modes,
Redirects redirects) throws IOException {
return new POpenProcess(popenShared(runtime, new IRubyObject[] {string}, null, true, redirects), runtime, modes);
}

public static POpenProcess popen(Ruby runtime, IRubyObject[] strings, Map env, ModeFlags modes) throws IOException {
return new POpenProcess(popenShared(runtime, strings, env), runtime, modes);
return popen(runtime, strings, env, modes, Redirects.NONE);
}

public static POpenProcess popen(Ruby runtime, IRubyObject[] strings, Map env, ModeFlags modes,
Redirects redirects) throws IOException {
return new POpenProcess(popenShared(runtime, strings, env, false, redirects), runtime, modes);
}

public static POpenProcess popen(Ruby runtime, IRubyObject string, Map env, ModeFlags modes) throws IOException {
Expand Down Expand Up @@ -871,6 +914,11 @@ private static Process popenShared(Ruby runtime, IRubyObject[] strings, Map env)
}

private static Process popenShared(Ruby runtime, IRubyObject[] strings, Map env, boolean addShell) throws IOException {
return popenShared(runtime, strings, env, addShell, Redirects.NONE);
}

private static Process popenShared(Ruby runtime, IRubyObject[] strings, Map env, boolean addShell,
Redirects redirects) throws IOException {
var context = runtime.getCurrentContext();
String shell = getShell(runtime);
File pwd = new File(runtime.getCurrentDirectory());
Expand Down Expand Up @@ -900,7 +948,7 @@ private static Process popenShared(Ruby runtime, IRubyObject[] strings, Map env,
cfg.verifyExecutableForDirect();
}

return buildProcess(runtime, cfg.execArgs, getCurrentEnv(runtime, env), pwd);
return buildProcess(runtime, cfg.execArgs, getCurrentEnv(runtime, env), pwd, redirects);
} catch (SecurityException se) {
throw runtime.newSecurityError(se.getLocalizedMessage());
}
Expand Down Expand Up @@ -1114,6 +1162,59 @@ private void pumpInerr(Process child, Ruby runtime) {
}
}

/**
* The out: and err: spawn options that ProcessBuilder can apply: a file path (including File::NULL)
* for either, and [:child, :out] for err:.
*/
public record Redirects(ProcessBuilder.Redirect out, ProcessBuilder.Redirect err, boolean errToOut) {
public static final Redirects NONE = new Redirects(null, null, false);

public static Redirects from(ThreadContext context, RubyHash opts, boolean withOut) {
IRubyObject outValue = withOut ? opts.fastARef(asSymbol(context, "out")) : null;
ProcessBuilder.Redirect out = fileRedirect(context, outValue);
IRubyObject errValue = opts.fastARef(asSymbol(context, "err"));
boolean errToOut = isChildOut(context, errValue);
ProcessBuilder.Redirect err = errToOut ? null : fileRedirect(context, errValue);

return new Redirects(out, err, errToOut);
}

public boolean handlesOut() {
return out != null;
}

public boolean handlesErr() {
return err != null || errToOut;
}

public boolean isEmpty() {
return !handlesOut() && !handlesErr();
}

void applyTo(ProcessBuilder builder) {
if (out != null) builder.redirectOutput(out);
if (err != null) builder.redirectError(err);
if (errToOut) builder.redirectErrorStream(true);
}

private static boolean isChildOut(ThreadContext context, IRubyObject value) {
return value instanceof RubyArray<?> ary && ary.size() == 2 &&
ary.eltOk(0) == asSymbol(context, "child") && ary.eltOk(1) == asSymbol(context, "out");
}

// IO objects respond to to_path, but redirect their descriptor, which we can't do
private static ProcessBuilder.Redirect fileRedirect(ThreadContext context, IRubyObject value) {
if (value == null || value instanceof RubyIO) return null;
if (!(value instanceof RubyString) && !value.respondsTo("to_path")) return null;

String path = RubyFile.get_path(context, value).toString();
ProcessBuilder.Redirect discard = ProcessBuilder.Redirect.DISCARD;
if (path.equals(discard.file().getPath())) return discard;

return ProcessBuilder.Redirect.to(JRubyFile.create(context.runtime.getCurrentDirectory(), path));
}
}

public static class LaunchConfig {
public LaunchConfig(Ruby runtime, IRubyObject[] rawArgs, boolean doExecutableSearch) {
this.runtime = runtime;
Expand Down Expand Up @@ -1372,6 +1473,11 @@ private static boolean shouldVerifyPathExecutable(String cmdline) {
}

public static Process run(Ruby runtime, IRubyObject[] rawArgs, boolean doExecutableSearch) throws IOException {
return run(runtime, rawArgs, doExecutableSearch, Redirects.NONE);
}

private static Process run(Ruby runtime, IRubyObject[] rawArgs, boolean doExecutableSearch,
Redirects redirects) throws IOException {
RubyHash env = null;
RubyHash opts = null;
String dir = runtime.getCurrentDirectory();
Expand All @@ -1391,7 +1497,7 @@ public static Process run(Ruby runtime, IRubyObject[] rawArgs, boolean doExecuta
if (!chdir.isNil()) dir = chdir.asJavaString();
}

return run(runtime, env, dir, rawArgs, doExecutableSearch, false);
return run(runtime, env, dir, rawArgs, doExecutableSearch, false, redirects);
}

private static boolean hasGlobCharacter(String word) {
Expand Down Expand Up @@ -1425,12 +1531,18 @@ public static Process run(Ruby runtime, IRubyObject[] rawArgs, boolean doExecuta
}

public static Process run(Ruby runtime, Map env, String dir, IRubyObject[] rawArgs, boolean doExecutableSearch, boolean forceExternalProcess) throws IOException {
return run(runtime, env, dir, rawArgs, doExecutableSearch, forceExternalProcess, Redirects.NONE);
}

private static Process run(Ruby runtime, Map env, String dir, IRubyObject[] rawArgs, boolean doExecutableSearch,
boolean forceExternalProcess, Redirects redirects) throws IOException {
Process aProcess;
File pwd = new File(dir);
LaunchConfig cfg = new LaunchConfig(runtime, rawArgs, doExecutableSearch);

try {
if (!forceExternalProcess && cfg.shouldRunInProcess()) {
// an in-process script shares our streams, so it can't be redirected
if (!forceExternalProcess && redirects.isEmpty() && cfg.shouldRunInProcess()) {
log(runtime, "Launching in-process");
ScriptThreadProcess ipScript = new ScriptThreadProcess(runtime,
expandGlobs(runtime, cfg.getExecArgs()), getCurrentEnv(runtime, env), pwd);
Expand Down Expand Up @@ -1458,7 +1570,7 @@ public static Process run(Ruby runtime, Map env, String dir, IRubyObject[] rawAr
"org.jruby.main.Main -C " + dir);
}
}
aProcess = buildProcess(runtime, args, getCurrentEnv(runtime, env), pwd);
aProcess = buildProcess(runtime, args, getCurrentEnv(runtime, env), pwd, redirects);
}
} catch (SecurityException se) {
throw runtime.newSecurityError(se.getLocalizedMessage());
Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/io/popen_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
@io.read.should == "foo\n"
end

it "redirects the child's stderr to the file given with err:" do
code = "puts 'out'; STDERR.puts 'err'"
IO.popen([*ruby_exe, "-e", code], err: @fname, &:read).should == "out\n"
File.read(@fname).should == "err\n"
end

platform_is_not :windows do
it "redirects the child's STDIN from the parent's STDOUT" do
skip "requires STDOUT to be a terminal device" unless STDOUT.tty?
Expand Down
27 changes: 27 additions & 0 deletions spec/ruby/core/kernel/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@
-> { @object.system("sad") }.should output_to_fd("", STDERR)
end

describe "with redirect options" do
before :each do
@redirect = tmp("kernel_system_redirect")
end

after :each do
rm_r @redirect
end

it "redirects stdout to the file given with out:" do
@object.system(*ruby_exe, "-e", "puts 'out'", out: @redirect)
File.read(@redirect).should == "out\n"
end

it "discards stderr when err: is File::NULL" do
call = @object.equal?(Kernel) ? "Kernel.system" : "system"
code = "require 'rbconfig'; #{call}(RbConfig.ruby, '-e', 'STDERR.puts :err', err: File::NULL)"
ruby_exe(code, args: "2>&1").should == ""
end

it "redirects stderr to the child's stdout when err: is [:child, :out]" do
code = "STDOUT.sync = true; puts 'out'; STDERR.puts 'err'"
@object.system(*ruby_exe, "-e", code, out: @redirect, err: [:child, :out])
File.read(@redirect).should == "out\nerr\n"
end
end

platform_is_not :windows do
before :each do
@shell = ENV['SHELL']
Expand Down
Loading