Refine search
/** * Change the permissions on a file / directory, recursively, if * needed. * @param filename name of the file whose permissions are to change * @param perm permission string * @param recursive true, if permissions should be changed recursively * @return the exit code from the command. * @throws IOException */ public static int chmod(String filename, String perm, boolean recursive) throws IOException { String [] cmd = Shell.getSetPermissionCommand(perm, recursive); String[] args = new String[cmd.length + 1]; System.arraycopy(cmd, 0, args, 0, cmd.length); args[cmd.length] = new File(filename).getPath(); ShellCommandExecutor shExec = new ShellCommandExecutor(args); try { shExec.execute(); }catch(IOException e) { if(LOG.isDebugEnabled()) { LOG.debug("Error while changing permission : " + filename +" Exception: " + StringUtils.stringifyException(e)); } } return shExec.getExitCode(); }
private static long getConf(String attr) { if(Shell.LINUX) { try { ShellCommandExecutor shellExecutorClk = new ShellCommandExecutor( new String[] {"getconf", attr }); shellExecutorClk.execute(); return Long.parseLong(shellExecutorClk.getOutput().replace("\n", "")); } catch (IOException|NumberFormatException e) { return -1; } } return -1; }
/** * Check if the executor had a timeout and logs the event. * @param executor to check * @param user user to log * @return true if timeout has occurred */ private boolean handleExecutorTimeout( ShellCommandExecutor executor, String user) { // If its a shell executor timeout, indicate so in the message // but treat the result as empty instead of throwing it up, // similar to how partial resolution failures are handled above if (executor.isTimedOut()) { LOG.warn( "Unable to return groups for user '{}' as shell group lookup " + "command '{}' ran longer than the configured timeout limit of " + "{} seconds.", user, Joiner.on(' ').join(executor.getExecString()), timeout ); return true; } return false; }
shExec = new ShellCommandExecutor(cmd, linkFile.getParentFile()); } else { shExec = new ShellCommandExecutor(cmd); shExec.execute(); } catch (Shell.ExitCodeException ec) { int returnVal = ec.getExitCode(); + "This behavior can be changed in the Local Security Policy management console"); } else if (returnVal != 0) { LOG.warn("Command '" + StringUtils.join(" ", cmd) + "' failed " + returnVal + " with: " + ec.getMessage()); if (LOG.isDebugEnabled()) { LOG.debug("Error while create symlink " + linkname + " to " + target + "." + " Exception: " + StringUtils.stringifyException(e)); return shExec.getExitCode();
@Override public void execute() throws IOException { super.execute(); } }
@Override public String[] getExecString() { String at = sshUserName.isEmpty() ? "" : "@"; String remoteCmd = StringUtils.join(super.getExecString(), " "); String cmd = String.format(tunnelCmd, sshOptions, sshUserName, at, hostname, remoteCmd, user); LOG.info("Executing full command [" + cmd + "]"); return new String[] { "/usr/bin/env", "bash", "-c", cmd }; }
/** * Create a ShellCommandExecutor object using the user's name. * * @param userName user's name * @return a ShellCommandExecutor object */ protected ShellCommandExecutor createGroupExecutor(String userName) { return new ShellCommandExecutor( getGroupsForUserCommand(userName), null, null, timeout); }
public NodeHealthMonitorExecutor(String[] args) { ArrayList<String> execScript = new ArrayList<String>(); execScript.add(nodeHealthScript); if (args != null) { execScript.addAll(Arrays.asList(args)); } shexec = new ShellCommandExecutor(execScript .toArray(new String[execScript.size()]), null, null, scriptTimeout); }
/** * Create a ShellCommandExecutor object for fetch a user's group id list. * * @param userName the user's name * @return a ShellCommandExecutor object */ protected ShellCommandExecutor createGroupIDExecutor(String userName) { return new ShellCommandExecutor( getGroupsIDForUserCommand(userName), null, null, timeout); }
/** * Initialize. * * @param configuration */ public void init(String location, long timeout) { this.healthCheckScript = location; this.scriptTimeout = timeout; ArrayList<String> execScript = new ArrayList<>(); execScript.add(healthCheckScript); this.shexec = new ShellCommandExecutor(execScript.toArray(new String[execScript.size()]), null, null, scriptTimeout); LOG.info("HealthChecker initialized with script at " + this.healthCheckScript + ", timeout=" + timeout); }
/** * Method used to terminate the node health monitoring service. * */ @Override protected void serviceStop() { if (nodeHealthScriptScheduler != null) { nodeHealthScriptScheduler.cancel(); } if (shexec != null) { Process p = shexec.getProcess(); if (p != null) { p.destroy(); } } }
/** * Static method to execute a shell command. * Covers most of the simple cases without requiring the user to implement * the <code>Shell</code> interface. * @param env the map of environment key=value * @param cmd shell command to execute. * @param timeout time in milliseconds after which script should be marked timeout * @return the output of the executed command. * @throws IOException on any problem. */ public static String execCommand(Map<String, String> env, String[] cmd, long timeout) throws IOException { ShellCommandExecutor exec = new ShellCommandExecutor(cmd, null, env, timeout); exec.execute(); return exec.getOutput(); }
private String getHealthReport(HealthCheckerExitStatus status){ String healthReport = null; switch (status) { case SUCCESS: healthReport = "Server is healthy."; break; case TIMED_OUT: healthReport = "Health script timed out"; break; case FAILED_WITH_EXCEPTION: healthReport = exceptionStackTrace; break; case FAILED_WITH_EXIT_CODE: healthReport = "Health script failed with exit code."; break; case FAILED: healthReport = shexec.getOutput(); break; } return healthReport; } }
/** * Returns the commands of this instance. * Arguments with spaces in are presented with quotes round; other * arguments are presented raw * * @return a string representation of the object. */ @Override public String toString() { StringBuilder builder = new StringBuilder(); String[] args = getExecString(); for (String s : args) { if (s.indexOf(' ') >= 0) { builder.append('"').append(s).append('"'); } else { builder.append(s); } builder.append(' '); } return builder.toString(); }
public static boolean checkIsBashSupported() throws InterruptedIOException { if (Shell.WINDOWS) { return false; } ShellCommandExecutor shexec; boolean supported = true; try { String[] args = {"bash", "-c", "echo 1000"}; shexec = new ShellCommandExecutor(args); shexec.execute(); } catch (InterruptedIOException iioe) { LOG.warn("Interrupted, unable to determine if bash is supported", iioe); throw iioe; } catch (IOException ioe) { LOG.warn("Bash is not supported by the OS", ioe); supported = false; } catch (SecurityException se) { LOG.info("Bash execution is not allowed by the JVM " + "security manager.Considering it not supported."); supported = false; } return supported; }
private static void unTarUsingTar(File inFile, File untarDir, boolean gzipped) throws IOException { StringBuffer untarCommand = new StringBuffer(); if (gzipped) { untarCommand.append(" gzip -dc '"); untarCommand.append(FileUtil.makeSecureShellPath(inFile)); untarCommand.append("' | ("); } untarCommand.append("cd '"); untarCommand.append(FileUtil.makeSecureShellPath(untarDir)); untarCommand.append("' && "); untarCommand.append("tar -xf "); if (gzipped) { untarCommand.append(" -)"); } else { untarCommand.append(FileUtil.makeSecureShellPath(inFile)); } String[] shellCmd = { "bash", "-c", untarCommand.toString() }; ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd); shexec.execute(); int exitcode = shexec.getExitCode(); if (exitcode != 0) { throw new IOException("Error untarring file " + inFile + ". Tar process exited with exit code " + exitcode); } }
try { String[] args = {"setsid", "bash", "-c", "echo $$"}; shexec = new ShellCommandExecutor(args); shexec.execute(); } catch (IOException ioe) { LOG.debug("setsid is not available on this machine. So not using it."); if (LOG.isDebugEnabled()) { LOG.debug("setsid exited with exit code " + (shexec != null ? shexec.getExitCode() : "(null executor)"));
break; case FAILED: setHealthStatus(false, shexec.getOutput()); break;
BufferedReader in = null; ShellCommandExecutor shexec = new ShellCommandExecutor(cmd); try { shexec.execute(); in = new BufferedReader(new StringReader(shexec.getOutput())); inpMsg = in.readLine(); exitValue = shexec.getExitCode(); if (inpMsg == null || exitValue != 0) { throw createIOException(fileName, inpMsg, errMsg, exitValue, null); inpMsg = shexec.getOutput(); errMsg = e.getMessage(); exitValue = e.getExitCode();
partialResolver.execute(); return parsePartialGroupNames( groupNames, partialResolver.getOutput()); } catch (ExitCodeException ece) { "Can't execute the shell command to " + "get the list of group id for user '" + userName + "'"; if (partialResolver.isTimedOut()) { message += " because of the command taking longer than " +