/** * Create a ShellCommandExecutor object which returns exit code 1, * emulating the case that the user does not exist. * * @param userName not used * @return a mock ShellCommandExecutor object */ @Override protected ShellCommandExecutor createGroupExecutor(String userName) { ShellCommandExecutor executor = mock(ShellCommandExecutor.class); try { doThrow(new ExitCodeException(1, "id: foobarusernotexist: No such user")). when(executor).execute(); when(executor.getOutput()).thenReturn(""); } catch (IOException e) { LOG.warn(e.getMessage()); } return executor; }
/** * Create a ShellCommandExecutor object which returns partially resolved * group names for a user. * * @param userName not used * @return a mock ShellCommandExecutor object */ @Override protected ShellCommandExecutor createGroupExecutor(String userName) { ShellCommandExecutor executor = mock(ShellCommandExecutor.class); try { // There is both a group name 9999 and a group ID 9999. // This is treated as unresolvable group. doThrow(new ExitCodeException(1, "cannot find name for group ID 9999")). when(executor).execute(); when(executor.getOutput()).thenReturn("9999\n9999 abc def"); } catch (IOException e) { LOG.warn(e.getMessage()); } return executor; }
public void run() { try { Vector<String> args = new Vector<String>(); if (isSetsidAvailable()) { args.add("setsid"); } args.add("bash"); args.add("-c"); args.add(" echo $$ > " + pidFile + "; sh " + shellScript + " " + N + ";"); shexec = new ShellCommandExecutor(args.toArray(new String[0])); shexec.execute(); } catch (ExitCodeException ee) { LOG.info("Shell Command exit with a non-zero exit code. This is" + " expected as we are killing the subprocesses of the" + " task intentionally. " + ee); } catch (IOException ioe) { LOG.info("Error executing shell command " + ioe); } finally { LOG.info("Exit code: " + shexec.getExitCode()); } }
@Override public void init() throws IOException { // Send command to executor which will just start up, // verify configuration/permissions and exit List<String> command = new ArrayList<String>( Arrays.asList(containerExecutorExe, "--checksetup")); String[] commandArray = command.toArray(new String[command.size()]); ShellCommandExecutor shExec = new ShellCommandExecutor(commandArray); if (LOG.isDebugEnabled()) { LOG.debug("checkLinuxExecutorSetup: " + Arrays.toString(commandArray)); } try { shExec.execute(); } catch (ExitCodeException e) { int exitCode = shExec.getExitCode(); LOG.warn("Exit code from container executor initialization is : " + exitCode, e); logOutput(shExec.getOutput()); throw new IOException("Linux container executor not configured properly" + " (error=" + exitCode + ")", e); } resourcesHandler.init(this); }
/** * Creates a hardlink * @param file - existing source file * @param linkName - desired target link file */ public static void createHardLink(File file, File linkName) throws IOException { if (file == null) { throw new IOException( "invalid arguments to createHardLink: source file is null"); } if (linkName == null) { throw new IOException( "invalid arguments to createHardLink: link name is null"); } // construct and execute shell command String[] hardLinkCommand = getHardLinkCommand.linkOne(file, linkName); ShellCommandExecutor shexec = new ShellCommandExecutor(hardLinkCommand); try { shexec.execute(); } catch (ExitCodeException e) { throw new IOException("Failed to execute command " + Arrays.toString(hardLinkCommand) + "; command output: \"" + shexec.getOutput() + "\"" + "; WrappedException: \"" + e.getMessage() + "\""); } }
@Override public void setup(LocalDirAllocator allocator, LocalStorage localStorage) throws IOException { // Check the permissions of the task-controller binary by running // it plainly. If permissions are correct, it returns an error // code 1, else it returns 24 or something else if some other bugs // are also present. String[] taskControllerCmd = new String[] { taskControllerExe }; ShellCommandExecutor shExec = new ShellCommandExecutor(taskControllerCmd); try { shExec.execute(); } catch (ExitCodeException e) { int exitCode = shExec.getExitCode(); if (exitCode != 1) { LOG.warn("Exit code from checking binary permissions is : " + exitCode); logOutput(shExec.getOutput()); throw new IOException("Task controller setup failed because of invalid" + "permissions/ownership with exit code " + exitCode, e); } } this.allocator = allocator; this.localStorage = localStorage; }
@Override public void signalTask(String user, int taskPid, Signal signal) throws IOException { String[] command = new String[]{taskControllerExe, user, localStorage.getDirsString(), Integer.toString(Commands.SIGNAL_TASK.getValue()), Integer.toString(taskPid), Integer.toString(signal.getValue())}; ShellCommandExecutor shExec = new ShellCommandExecutor(command); if (LOG.isDebugEnabled()) { LOG.debug("signalTask: " + Arrays.toString(command)); } try { shExec.execute(); } catch (ExitCodeException e) { int ret_code = shExec.getExitCode(); if (ret_code != ResultCode.INVALID_TASK_PID.getValue()) { logOutput(shExec.getOutput()); throw new IOException("Problem signalling task " + taskPid + " with " + signal + "; exit = " + ret_code); } } }
public void run() { try { Vector<String> args = new Vector<String>(); if(ProcessTree.isSetsidAvailable) { args.add("setsid"); } args.add("bash"); args.add("-c"); args.add(" echo $$ > " + pidFile + "; sh " + shellScript + " " + N + ";") ; shexec = new ShellCommandExecutor(args.toArray(new String[0])); shexec.execute(); } catch (ExitCodeException ee) { LOG.info("Shell Command exit with a non-zero exit code. This is" + " expected as we are killing the subprocesses of the" + " task intentionally. " + ee); } catch (IOException ioe) { LOG.info("Error executing shell command " + ioe); } finally { LOG.info("Exit code: " + shexec.getExitCode()); } }
public void run() { try { Vector<String> args = new Vector<String>(); if(isSetsidAvailable()) { args.add("setsid"); } args.add("bash"); args.add("-c"); args.add(" echo $$ > " + pidFile + "; sh " + shellScript + " " + N + ";") ; shexec = new ShellCommandExecutor(args.toArray(new String[0])); shexec.execute(); } catch (ExitCodeException ee) { LOG.info("Shell Command exit with a non-zero exit code. This is" + " expected as we are killing the subprocesses of the" + " task intentionally. " + ee); } catch (IOException ioe) { LOG.info("Error executing shell command " + ioe); } finally { LOG.info("Exit code: " + shexec.getExitCode()); } }
/** * Get the current user's group list from Unix by running the command 'groups' * NOTE. For non-existing user it will return EMPTY list * @param user user name * @return the groups list that the <code>user</code> belongs to * @throws IOException if encounter any error when running the command */ private static List<String> getUnixGroups(final String user) throws IOException { String result = ""; try { result = Shell.execCommand(Shell.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list; LOG.warn("got exception trying to get groups for user " + user, e); } StringTokenizer tokenizer = new StringTokenizer(result, Shell.TOKEN_SEPARATOR_REGEX); List<String> groups = new LinkedList<String>(); while (tokenizer.hasMoreTokens()) { groups.add(tokenizer.nextToken()); } return groups; }
/** * Is the process with PID pid still alive? * This method assumes that isAlive is called on a pid that was alive not * too long ago, and hence assumes no chance of pid-wrapping-around. * * @param pid pid of the process to check. * @return true if process is alive. */ public static boolean isAlive(String pid) { ShellCommandExecutor shexec = null; try { String[] args = { "kill", "-0", pid }; shexec = new ShellCommandExecutor(args); shexec.execute(); } catch (ExitCodeException ee) { return false; } catch (IOException ioe) { LOG.warn("Error executing shell command " + Arrays.toString(shexec.getExecString()) + ioe); return false; } return (shexec.getExitCode() == 0 ? true : false); }
/** * Is the process group with still alive? * * This method assumes that isAlive is called on a pid that was alive not * too long ago, and hence assumes no chance of pid-wrapping-around. * * @param pgrpId process group id * @return true if any of process in group is alive. */ public static boolean isProcessGroupAlive(String pgrpId) { ShellCommandExecutor shexec = null; try { String[] args = { "kill", "-0", "-"+pgrpId }; shexec = new ShellCommandExecutor(args); shexec.execute(); } catch (ExitCodeException ee) { return false; } catch (IOException ioe) { LOG.warn("Error executing shell command " + Arrays.toString(shexec.getExecString()) + ioe); return false; } return (shexec.getExitCode() == 0 ? true : false); }
@Override public void setup(LocalDirAllocator allocator, LocalStorage localStorage) throws IOException { // Check the permissions of the task-controller binary by running it plainly. // If permissions are correct, it returns an error code 1, else it returns // 24 or something else if some other bugs are also present. String[] taskControllerCmd = new String[] { taskControllerExe }; ShellCommandExecutor shExec = new ShellCommandExecutor(taskControllerCmd); try { shExec.execute(); } catch (ExitCodeException e) { int exitCode = shExec.getExitCode(); if (exitCode != 1) { LOG.warn("Exit code from checking binary permissions is : " + exitCode); logOutput(shExec.getOutput()); throw new IOException("Task controller setup failed because of invalid" + "permissions/ownership with exit code " + exitCode, e); } } this.allocator = allocator; this.localStorage = localStorage; }
/** * Get the current user's group list from Unix by running the command 'groups' * NOTE. For non-existing user it will return EMPTY list * @param user user name * @return the groups list that the <code>user</code> belongs to * @throws IOException if encounter any error when running the command */ private static List<String> getUnixGroups(final String user) throws IOException { String result = ""; try { result = Shell.execCommand(Shell.getGroupsForUserCommand(user)); } catch (ExitCodeException e) { // if we didn't get the group - just return empty list; LOG.warn("got exception trying to get groups for user " + user, e); } StringTokenizer tokenizer = new StringTokenizer(result); List<String> groups = new LinkedList<String>(); while (tokenizer.hasMoreTokens()) { groups.add(tokenizer.nextToken()); } return groups; }