Skip to content
Open
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
16 changes: 3 additions & 13 deletions core/src/main/java/org/jruby/RubyKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1830,8 +1830,7 @@ private static int systemCommon(ThreadContext context, IRubyObject recv, IRubyOb
long[] tuple;

try {
args = dropLastArgIfOptions(runtime, args);
if (! Platform.IS_WINDOWS && args[args.length -1].asJavaString().matches(".*[^&]&\\s*")) {
if (! Platform.IS_WINDOWS && args[args.length -1].toString().matches(".*[^&]&\\s*")) {
// looks like we need to send process to the background
ShellLauncher.runWithoutWait(runtime, args);
return 0;
Expand All @@ -1846,17 +1845,6 @@ private static int systemCommon(ThreadContext context, IRubyObject recv, IRubyOb
return (int) tuple[0];
}

private static IRubyObject[] dropLastArgIfOptions(final Ruby runtime, final IRubyObject[] args) {
IRubyObject lastArg = args[args.length - 1];
if (lastArg instanceof RubyHash) {
if (!((RubyHash) lastArg).isEmpty()) {
runtime.getWarnings().warn(ID.UNSUPPORTED_SUBPROCESS_OPTION, "system does not support options in JRuby yet: " + lastArg);
}
return Arrays.copyOf(args, args.length - 1);
}
return args;
}

public static IRubyObject exec(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
return execCommon(context, null, args[0], null, args);
}
Expand Down Expand Up @@ -1914,6 +1902,8 @@ private static IRubyObject execCommon(ThreadContext context, IRubyObject env, IR
try {
ShellLauncher.LaunchConfig cfg = new ShellLauncher.LaunchConfig(runtime, args, true);

cfg.processArguments();

// Duplicated in part from ShellLauncher.runExternalAndWait
if (cfg.shouldRunInShell()) {
// execute command with sh -c
Expand Down
237 changes: 208 additions & 29 deletions core/src/main/java/org/jruby/util/ShellLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,27 @@

package org.jruby.util;

import static com.headius.backport9.buffer.Buffers.clearBuffer;
import static com.headius.backport9.buffer.Buffers.flipBuffer;
import static java.lang.System.out;
import jnr.posix.util.Platform;
import org.jruby.Main;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFixnum;
import org.jruby.RubyHash;
import org.jruby.RubyIO;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.exceptions.RaiseException;
import org.jruby.ext.rbconfig.RbConfigLibrary;
import org.jruby.javasupport.Java;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.io.ChannelHelper;
import org.jruby.util.io.IOOptions;
import org.jruby.util.io.ModeFlags;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -55,24 +73,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jruby.Main;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.RubyIO;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import jnr.posix.util.Platform;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.javasupport.Java;
import org.jruby.runtime.Helpers;
import org.jruby.ext.rbconfig.RbConfigLibrary;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.io.ChannelHelper;
import org.jruby.util.io.IOOptions;
import org.jruby.util.io.ModeFlags;
import static com.headius.backport9.buffer.Buffers.clearBuffer;
import static com.headius.backport9.buffer.Buffers.flipBuffer;
import static java.lang.System.out;

/**
* This mess of a class is what happens when all Java gives you is
Expand Down Expand Up @@ -105,6 +108,7 @@ public class ShellLauncher {
Pattern.compile("[*?{}\\[\\]<>()~&|$;'`\\\\\"\\n]");

private static final Pattern WIN_ENVVAR_PATTERN = Pattern.compile("%\\w+%");
public static final ProcessBuilder.Redirect REDIRECT_ERROR_TO_OUT = ProcessBuilder.Redirect.to(new File("REDIRECT ERROR TO OUT"));

private static class ScriptThreadProcess extends Process implements Runnable {
private final String[] argArray;
Expand Down Expand Up @@ -469,6 +473,8 @@ public static int runExternalAndWait(Ruby runtime, IRubyObject[] rawArgs, Map me
File pwd = new File(runtime.getCurrentDirectory());
LaunchConfig cfg = new LaunchConfig(runtime, rawArgs, true);

cfg.processArguments();

try {
Process process;
try {
Expand Down Expand Up @@ -517,6 +523,8 @@ public static long runExternal(Ruby runtime, IRubyObject env, IRubyObject prog,
File pwd = new File(runtime.getCurrentDirectory());
LaunchConfig cfg = new LaunchConfig(runtime, rawArgs, true);

cfg.processArguments();

try {
Process process;
try {
Expand Down Expand Up @@ -579,6 +587,36 @@ public static Process buildProcess(Ruby runtime, String[] args, String[] env, Fi
.start();
}

public static Process buildProcess(Ruby runtime, LaunchConfig cfg, String[] args, String[] env, File pwd) throws IOException {
ProcessBuilder builder = new ProcessBuilder(args);

// duplicate this logic or improve jnr-posix ProcessMaker to support redirects
Map<String, String> environment = builder.environment();
environment.clear();
Map<String, String> newEnvironment = runtime.getPosix().newProcessMaker().environment(env).environment();
newEnvironment.forEach((k, v) -> environment.put(k, v));

builder.directory(pwd);

if (cfg.redirectIn != null) {
builder.redirectInput(cfg.redirectIn);
}

if (cfg.redirectOut != null) {
builder.redirectOutput(cfg.redirectOut);
}

if (cfg.redirectErr != null) {
if (cfg.redirectErr == REDIRECT_ERROR_TO_OUT) {
builder.redirectErrorStream(true);
} else {
builder.redirectError(cfg.redirectErr);
}
}

return builder.start();
}

public static long runExternalWithoutWait(Ruby runtime, IRubyObject[] rawArgs) {
return runWithoutWait(runtime, rawArgs, runtime.getOutputStream());
}
Expand All @@ -591,6 +629,8 @@ public static int execAndWait(Ruby runtime, IRubyObject[] rawArgs, Map mergeEnv)
File pwd = new File(runtime.getCurrentDirectory());
LaunchConfig cfg = new LaunchConfig(runtime, rawArgs, true);

cfg.processArguments();

if (cfg.shouldRunInProcess()) {
log(runtime, "ExecAndWait in-process");
try {
Expand Down Expand Up @@ -832,6 +872,9 @@ private static Process popenShared(Ruby runtime, IRubyObject[] strings, Map env,

String[] args = parseCommandLine(runtime.getCurrentContext(), runtime, strings);
LaunchConfig cfg = new LaunchConfig(runtime, strings, true);

cfg.processArguments();

boolean useShell = Platform.IS_WINDOWS ? cfg.shouldRunInShell() : false;
if (addShell) for (String arg : args) useShell |= shouldUseShell(arg);

Expand Down Expand Up @@ -1055,11 +1098,132 @@ private void pumpInerr(Process child, Ruby runtime) {

public static class LaunchConfig {
public LaunchConfig(Ruby runtime, IRubyObject[] rawArgs, boolean doExecutableSearch) {
this(runtime, rawArgs, ProcessBuilder.Redirect.PIPE, null, doExecutableSearch);
}

public LaunchConfig(Ruby runtime, IRubyObject[] rawArgs, ProcessBuilder.Redirect defaultRedirect, File defaultChdir, boolean doExecutableSearch) {
this.runtime = runtime;
this.rawArgs = rawArgs;
this.doExecutableSearch = doExecutableSearch;
this.redirectIn = defaultRedirect;
this.redirectOut = defaultRedirect;
this.redirectErr = defaultRedirect;
this.chdir = defaultChdir;
}

public void processArguments() {
shell = getShell(runtime);
args = parseCommandLine(runtime.getCurrentContext(), runtime, rawArgs);
args = parseCommandLine(runtime.getCurrentContext(), runtime, processOptions(rawArgs));
}

private IRubyObject[] processOptions(IRubyObject[] args) {
if (args.length > 1) {
IRubyObject tmp = TypeConverter.checkHashType(runtime, args[args.length - 1], false);

if (tmp instanceof RubyHash) {
options = (RubyHash) tmp;
args = Arrays.copyOf(args, args.length - 1);

for (Map.Entry<IRubyObject, IRubyObject> entry : (Set<Map.Entry<IRubyObject, IRubyObject>>) options.directEntrySet()) {
IRubyObject key = entry.getKey();
IRubyObject value = entry.getValue();

if (key instanceof RubyFixnum || key instanceof RubyIO) {
throw redirectNotSupported(runtime, key, value);
}

if (!(key instanceof RubySymbol) && !(key instanceof RubyString)) {
throw runtime.newArgumentError("bad keyword argument: " + key.inspect());
}

switch (key.toString()) {
case "unsetenv_others":
case "pgroup":
case "new_pgroup":
case "rlimit_resourcename":
case "umask":
case "close_others":
throw runtime.newNotImplementedError("native support unavailable, option not supported: " + key.inspect());

case "chdir":
if (value instanceof RubyString) {
chdir = new File(value.toString());
break;
}

throw redirectNotSupported(runtime, key, value);

case "in":
// "in" redirect only supports string filename bare or as only element of an array
value = unwrapInOutRedirectArray(runtime, key, value);

if (value instanceof RubyString) {
redirectIn = ProcessBuilder.Redirect.from(new File(value.toString()));
break;
}

throw redirectNotSupported(runtime, key, value);

case "out":
// "out" redirect only supports string filename
if (value instanceof RubyString) {
redirectOut = ProcessBuilder.Redirect.from(new File(value.toString()));
break;
}

throw redirectNotSupported(runtime, key, value);

case "err":
// "err" redirect only supports string filename, or [:child, :out]
if (value instanceof RubyString) {
redirectErr = ProcessBuilder.Redirect.from(new File(value.toString()));
break;
}

if (value instanceof RubyArray) {
RubyArray toArray = (RubyArray) value;

if (toArray.size() == 2) {
IRubyObject elt0 = toArray.eltOk(0);
IRubyObject elt1 = toArray.eltOk(1);

if (elt0 instanceof RubySymbol &&
elt0.toString().equals("child") &&
elt1 instanceof RubySymbol &&
elt1.toString().equals("out")) {
redirectErr = REDIRECT_ERROR_TO_OUT;
break;
}
}
}

throw redirectNotSupported(runtime, key, value);
}
}
}
}

return args;
}

private IRubyObject unwrapInOutRedirectArray(Ruby runtime, IRubyObject key, IRubyObject value) {
if (value instanceof RubyArray) {
RubyArray fromArray = (RubyArray) value;

int size = fromArray.size();
if (size != 1) {
throw redirectNotSupported(runtime, key, value);
}

value = fromArray.eltOk(0);
}
return value;
}

private RaiseException redirectNotSupported(Ruby runtime, IRubyObject key, IRubyObject value) {
return runtime.newNotImplementedError(
"native support unavailable, redirect not supported: "
+ key.inspect() + " => " + value.inspect());
}

/**
Expand Down Expand Up @@ -1301,13 +1465,19 @@ private static boolean shouldVerifyPathExecutable(String cmdline) {
private final Ruby runtime;
private final boolean doExecutableSearch;
private final IRubyObject[] rawArgs;
private final String shell;
private final String[] args;
private String shell;
private String[] args;
private String[] execArgs;
private boolean cmdBuiltin = false;

private String executable;
private File executableFile;

ProcessBuilder.Redirect redirectIn;
ProcessBuilder.Redirect redirectOut;
ProcessBuilder.Redirect redirectErr;
File chdir;
RubyHash options;
}

public static Process run(Ruby runtime, IRubyObject[] rawArgs, boolean doExecutableSearch) throws IOException {
Expand Down Expand Up @@ -1365,14 +1535,23 @@ 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 {
Process aProcess;
File pwd = new File(dir);
LaunchConfig cfg = new LaunchConfig(runtime, rawArgs, doExecutableSearch);
String virtualCWD = runtime.getCurrentDirectory();
File chdir = null;

// default chdir is virtual cwd
if (virtualCWD != System.getProperty("user.dir")) {
chdir = new File(virtualCWD);
}

LaunchConfig cfg = new LaunchConfig(runtime, rawArgs, ProcessBuilder.Redirect.INHERIT, chdir, doExecutableSearch);

cfg.processArguments();

try {
if (!forceExternalProcess && cfg.shouldRunInProcess()) {
log(runtime, "Launching in-process");
ScriptThreadProcess ipScript = new ScriptThreadProcess(runtime,
expandGlobs(runtime, cfg.getExecArgs()), getCurrentEnv(runtime, env), pwd);
expandGlobs(runtime, cfg.getExecArgs()), getCurrentEnv(runtime, env), cfg.chdir);
ipScript.start();
return ipScript;
} else {
Expand All @@ -1388,7 +1567,7 @@ public static Process run(Ruby runtime, Map env, String dir, IRubyObject[] rawAr
String[] args = cfg.getExecArgs();
if (dir.startsWith("uri:classloader:")) {
// system commands can't run with a URI for the current dir, so the best we can use is user.dir
pwd = new File(System.getProperty("user.dir"));
chdir = new File(System.getProperty("user.dir"));

// only if we inside a jar and spawning org.jruby.Main we
// change to the current directory inside the jar
Expand All @@ -1397,7 +1576,7 @@ public static Process run(Ruby runtime, Map env, String dir, IRubyObject[] rawAr
"org.jruby.Main -C " + dir);
}
}
aProcess = buildProcess(runtime, args, getCurrentEnv(runtime, env), pwd);
aProcess = buildProcess(runtime, args, getCurrentEnv(runtime, env), cfg.chdir);
}
} catch (SecurityException se) {
throw runtime.newSecurityError(se.getLocalizedMessage());
Expand Down