List of usage examples for org.apache.commons.exec CommandLine CommandLine
public CommandLine(final CommandLine other)
From source file:org.ng200.openolympus.cerberus.compilers.JavaCompiler.java
@Override public void compile(final List<Path> inputFiles, final Path outputFile, final Map<String, Object> additionalParameters) throws CompilationException, IOException { FileAccess.createDirectories(outputFile); final CommandLine commandLine = new CommandLine("javac"); commandLine.setSubstitutionMap(additionalParameters); this.arguments.forEach((arg) -> commandLine.addArgument(arg)); commandLine.addArgument("-d"); commandLine.addArgument(outputFile.toAbsolutePath().toString()); commandLine.addArgument("-nowarn"); // Prohibit warnings because they // screw/*from w ww.j a v a 2s. c o m*/ // up error detection inputFiles.forEach((file) -> commandLine .addArguments(MessageFormat.format("\"{0}\"", file.toAbsolutePath().toString()))); // Add // input // files JavaCompiler.logger.info("Running javac with arguments: {}", Arrays.asList(commandLine.getArguments())); final DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(new int[] { 0, 1 }); final ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(null, errorStream, null)); executor.setWatchdog(new ExecuteWatchdog(20000));// 20 seconds to // compile int result; try { result = executor.execute(commandLine); } catch (final IOException e) { JavaCompiler.logger.error("Could not execute javac: {}", e); throw new CompilationException("Could not execute javac", e); } switch (result) { case 0: return; case 1: try { String errorString = errorStream.toString("UTF-8"); final Pattern pattern = Pattern.compile( "^(" + inputFiles.stream().map(file -> Pattern.quote(file.toAbsolutePath().toString())) .collect(Collectors.joining("|")) + "):", Pattern.MULTILINE); errorString = pattern.matcher(errorString).replaceAll(""); JavaCompiler.logger.debug("Compilation error: {}", errorString); throw new CompilerError("javac.wrote.stderr", errorString); } catch (final UnsupportedEncodingException e) { throw new CompilationException("Unsupported encoding! The compiler should output UTF-8!", e); } } }
From source file:org.ng200.openolympus.cerberus.executors.JavaExecutor.java
@Override public ExecutionResult execute(final Path program) throws IOException { final Path chrootRoot = this.storage.getPath().resolve("chroot"); final Path chrootedProgram = chrootRoot.resolve(program.getFileName().toString()); FileAccess.createDirectories(chrootedProgram); FileAccess.copyDirectory(program, chrootedProgram, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); final Path outOfMemoryFile = chrootRoot.resolve("outOfMemory"); final Path policyFile = this.storage.getPath().resolve("olymp.policy"); try (Stream<Path> paths = FileAccess.walkPaths(storage.getPath())) { paths.forEach(path -> {//w w w . ja v a 2 s . co m try { Files.setPosixFilePermissions(path, new HashSet<PosixFilePermission>( Lists.from(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_EXECUTE, PosixFilePermission.OTHERS_READ))); } catch (Exception e) { throw new RuntimeException(e); } }); } this.buildPolicy(chrootRoot, policyFile); final CommandLine commandLine = new CommandLine("sudo"); commandLine.addArgument("olympus_watchdog"); this.setUpOlrunnerLimits(commandLine); commandLine.addArgument("--security=0"); commandLine.addArgument("--jail=/"); commandLine.addArgument("--"); commandLine.addArgument("/usr/bin/java"); commandLine.addArgument("-classpath"); commandLine.addArgument(chrootedProgram.toAbsolutePath().toString()); commandLine.addArgument("-Djava.security.manager"); commandLine.addArgument("-Djava.security.policy=" + policyFile.toAbsolutePath().toString()); commandLine.addArgument("-Xmx" + this.getMemoryLimit()); commandLine.addArgument("-Xms" + this.getMemoryLimit()); commandLine.addArgument(MessageFormat.format("-XX:OnOutOfMemoryError=touch {0}; echo \"\" > {0}", outOfMemoryFile.toAbsolutePath().toString()), false); commandLine.addArgument("Main"); final DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(20000)); // 20 seconds for the // sandbox to // complete executor.setWorkingDirectory(chrootRoot.toFile()); executor.setStreamHandler(new PumpStreamHandler(this.outputStream, this.errorStream, this.inputStream)); try { executor.execute(commandLine); } catch (final IOException e) { if (!e.getMessage().toLowerCase().equals("stream closed")) { throw e; } } final ExecutionResult readOlrunnerVerdict = this.readOlrunnerVerdict(chrootRoot.resolve("verdict.txt")); if (FileAccess.exists(outOfMemoryFile)) { readOlrunnerVerdict.setResultType(ExecutionResultType.MEMORY_LIMIT); } readOlrunnerVerdict.setMemoryPeak(this.getMemoryLimit()); return readOlrunnerVerdict; }
From source file:org.ng200.openolympus.cerberus.executors.OpenOlympusWatchdogExecutor.java
private static String callNativeId(boolean group) throws IOException { OpenOlympusWatchdogExecutor.ensureUserAndGroupExists(); final CommandLine commandLine = new CommandLine("id"); commandLine.addArgument(group ? "-g" : "-u"); commandLine.addArgument("olympuswatchdogchild"); final DefaultExecutor executor = new DefaultExecutor(); final ByteArrayOutputStream out = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(out)); executor.setWatchdog(new ExecuteWatchdog(1000)); try {/*from ww w. ja v a 2 s .c o m*/ executor.execute(commandLine); return out.toString(StandardCharsets.UTF_8.name()); } catch (final ExecuteException e) { throw new ExecuteException( "Couldn't find user/group id of the olympuswatchdogchild user/group: does it even exist?", e.getExitValue(), e); } }
From source file:org.ng200.openolympus.cerberus.executors.OpenOlympusWatchdogExecutor.java
private static void ensureUserAndGroupExists() throws IOException { if (alreadyEnsuredUserExists) return;/* w w w . ja v a 2 s . com*/ final CommandLine commandLine = new CommandLine("sudo"); commandLine.addArgument("useradd"); commandLine.addArgument("-U"); commandLine.addArgument("-M"); // Don't create home directory commandLine.addArgument("-s"); commandLine.addArgument("/bin/false"); commandLine.addArgument("olympuswatchdogchild"); final DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(new int[] { 0, /* Added user */ 9 /* User already exists */ }); executor.setWatchdog(new ExecuteWatchdog(1000)); try { executor.execute(commandLine); alreadyEnsuredUserExists = true; } catch (final ExecuteException e) { throw new ExecuteException( "Couldn't find user/group id of the olympuswatchdogchild user/group: does it even exist?", e.getExitValue(), e); } }
From source file:org.ng200.openolympus.cerberus.executors.SandboxedExecutor.java
@Override public ExecutionResult execute(final Path program) throws IOException { SandboxedExecutor.logger.debug("Copying program into jail"); final Path chrootedProgram = this.storage.getPath().resolve("chroot") .resolve(program.getFileName().toString()); chrootedProgram.getParent().toFile().mkdirs(); FileAccess.copy(program, chrootedProgram, StandardCopyOption.COPY_ATTRIBUTES); final CommandLine commandLine = new CommandLine("sudo"); commandLine.addArgument("olympus_watchdog"); this.setUpOlrunnerLimits(commandLine); commandLine.addArgument(MessageFormat.format("--jail={0}", this.storage.getPath().resolve("chroot").toAbsolutePath().toString())); commandLine.addArgument("--"); commandLine/*from www.j ava 2s . c o m*/ .addArgument("/" + this.storage.getPath().resolve("chroot").relativize(chrootedProgram).toString()); final DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(new ExecuteWatchdog(60000)); // 60 seconds for the // sandbox to // complete executor.setWorkingDirectory(this.storage.getPath().toFile()); executor.setStreamHandler(new PumpStreamHandler(this.outputStream, this.errorStream, this.inputStream)); SandboxedExecutor.logger.debug("Executing in sandbox: {}", commandLine.toString()); try { executor.execute(commandLine); } catch (final ExecuteException e) { SandboxedExecutor.logger.info("Execution failed: {}", e); throw e; } catch (final IOException e) { if (!e.getMessage().toLowerCase().equals("stream closed")) { throw e; } } return this.readOlrunnerVerdict(this.storage.getPath().resolve("verdict.txt")); }
From source file:org.ng200.openolympus.controller.admin.DiagnosticsController.java
private File which(final String applicationName) { try {// ww w.ja va2 s. c o m final DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(20000)); final CommandLine commandLine = new CommandLine("which"); commandLine.addArgument(applicationName); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(byteArrayOutputStream)); executor.execute(commandLine); final String fileName = byteArrayOutputStream.toString().trim(); if (fileName.isEmpty()) { return null; } return new File(fileName); } catch (final IOException e) { return null; } }
From source file:org.ng200.openolympus.FileAccess.java
public static void rsync(final Path from, final Path to) throws IOException { final CommandLine commandLine = new CommandLine("/usr/bin/rsync"); commandLine.addArgument("-r"); commandLine.addArgument("--ignore-errors"); commandLine.addArgument(from.toAbsolutePath().toString()); commandLine.addArgument(to.toAbsolutePath().toString()); final DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(20000)); // 20 seconds for the // sandbox to // complete/*from w w w . ja v a2 s .c om*/ final ByteArrayOutputStream outAndErr = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outAndErr)); executor.setExitValues(new int[] { 0 }); try { executor.execute(commandLine); } catch (final ExecuteException e) { throw new IOException("Rsync failed:\n" + outAndErr.toString(), e); } }
From source file:org.obm.push.mail.greenmail.ExternalProcess.java
public ExternalProcess(String executablePath, long processTimeout, long processStartTimeNeeded) { Preconditions.checkNotNull(Strings.emptyToNull(executablePath)); Preconditions.checkArgument(processTimeout > 0); this.cli = new CommandLine(executablePath); this.environment = Maps.newHashMap(); this.processTimeout = processTimeout; this.processStartTimeNeeded = processStartTimeNeeded; this.processOutputStream = new ByteArrayOutputStream(); this.streamHandler = new PumpStreamHandler(processOutputStream, System.err); }
From source file:org.onehippo.forge.gallerymagick.core.command.GraphicsMagickCommand.java
/** * {@inheritDoc}/*from w w w. j ava 2 s .c o m*/ */ @Override protected CommandLine createCommandLine() { CommandLine cmdLine = new CommandLine(getExecutable()); cmdLine.addArgument(getSubCommand()); for (String argument : getArguments()) { cmdLine.addArgument(argument); } return cmdLine; }
From source file:org.onehippo.forge.gallerymagick.core.command.ImageMagickCommand.java
/** * {@inheritDoc}/* www .j a v a2s. c om*/ */ @Override protected CommandLine createCommandLine() { String executable = getExecutable(); if (StringUtils.isBlank(executable)) { executable = getSubCommand(); } CommandLine cmdLine = new CommandLine(executable); for (String argument : getArguments()) { cmdLine.addArgument(argument); } return cmdLine; }