diff options
Diffstat (limited to 'systemtap/org.eclipse.linuxtools.systemtap.structures')
2 files changed, 90 insertions, 53 deletions
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/process/SystemtapProcessFactory.java b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/process/SystemtapProcessFactory.java index ddbbcab069..80536bb279 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/process/SystemtapProcessFactory.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/process/SystemtapProcessFactory.java @@ -27,26 +27,42 @@ import com.jcraft.jsch.Session; */ public class SystemtapProcessFactory { + private static final int DEFAULT_PORT = 22; + /** * Runs stap with the given arguments on the given host using the given * credentials. * - * @param user - * the user name to use on the remote machine. - * @param host - * the host where the systemtap process will be run. - * @param password - * password for authenticating with the given host. + * @param user the user name to use on the remote machine. + * @param host the host where the systemtap process will be run. + * @param password password for authenticating with the given host. * @return a {@link Channel} connected to the remotely running process. - * @throws JSchException - * thrown if there are problems connecting to the remote - * machine. + * @throws JSchException thrown if there are problems connecting to the remote machine. */ public static Channel execRemote(String[] args, OutputStream out, OutputStream err, String user, String host, String password) throws JSchException { + return execRemote(args, out, err, user, host, password, DEFAULT_PORT, null); + } + + /** + * Runs stap with the given arguments on the given host using the given + * credentials. + * + * @param user the user name to use on the remote machine. + * @param host the host where the systemtap process will be run. + * @param password password for authenticating with the given host. + * @param envp an array with extra enviroment variables to be used when running + * the command. Set to <code>null</code> if none are needed. + * @return a {@link Channel} connected to the remotely running process. + * @throws JSchException thrown if there are problems connecting to the remote machine. + * @since 3.0 + */ + public static Channel execRemote(String[] args, OutputStream out, + OutputStream err, String user, String host, String password, int port, String[] envp) + throws JSchException { JSch jsch = new JSch(); - Session session = jsch.getSession(user, host, 22); + Session session = jsch.getSession(user, host, port); session.setPassword(password); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); //$NON-NLS-1$//$NON-NLS-2$ @@ -54,16 +70,21 @@ public class SystemtapProcessFactory { session.connect(); StringBuilder command = new StringBuilder(); + if (envp != null) { + for (String var : envp) { + command.append(String.format("export %s; ", var)); //$NON-NLS-1$ + } + } for (int i = 0; i < args.length; i++) { - command.append(args[i]); + command.append(args[i] + ' '); } - Channel channel = session.openChannel("exec"); //$NON-NLS-1$ - ((ChannelExec) channel).setCommand(command.toString()); - + ChannelExec channel = (ChannelExec) session.openChannel("exec"); //$NON-NLS-1$ + channel.setCommand(command.toString()); channel.setInputStream(null, true); channel.setOutputStream(out, true); channel.setExtOutputStream(err, true); + channel.connect(); return channel; } @@ -72,23 +93,37 @@ public class SystemtapProcessFactory { * Runs stap with the given arguments on the given host using the given * credentials and waits for the process to finish executing. * - * @param user - * the user name to use on the remote machine. - * @param host - * the host where the systemtap process will be run. - * @param password - * password for authenticating with the given host. + * @param user the user name to use on the remote machine. + * @param host the host where the systemtap process will be run. + * @param password password for authenticating with the given host. * @return a {@link Channel} connected to the remotely running process. - * @throws JSchException - * thrown if there are problems connecting to the remote - * machine. + * @throws JSchException thrown if there are problems connecting to the remote machine. */ public static Channel execRemoteAndWait(String[] args, OutputStream out, OutputStream err, String user, String host, String password) throws JSchException { - Channel channel = execRemote(args, out, err, user, host, password); + return execRemote(args, out, err, user, host, password, DEFAULT_PORT, null); + } + + /** + * Runs stap with the given arguments on the given host using the given + * credentials and waits for the process to finish executing. + * + * @param user the user name to use on the remote machine. + * @param host the host where the systemtap process will be run. + * @param password password for authenticating with the given host. + * @param envp an array with extra enviroment variables to be used when running + * the command. Set to <code>null</code> if none are needed. + * @return a {@link Channel} connected to the remotely running process. + * @throws JSchException thrown if there are problems connecting to the remote machine. + * @since 3.0 + */ + public static Channel execRemoteAndWait(String[] args, OutputStream out, + OutputStream err, String user, String host, String password, int port, String[] envp) + throws JSchException { + Channel channel = execRemote(args, out, err, user, host, password, port, envp); - while (!channel.isClosed()){ + while (!channel.isClosed()) { try { Thread.sleep(250); } catch (InterruptedException e) { diff --git a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java index 84422f4a69..3798e584a3 100644 --- a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java +++ b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java @@ -60,13 +60,20 @@ public class Command implements Runnable { private List<IGobblerListener> errorListeners = new ArrayList<>(); //Only used to allow adding listeners before creating the StreamGobbler private int returnVal = Integer.MAX_VALUE; - private String[] cmd; - private String[] envVars; + /** + * @since 3.0 + */ + protected final String[] cmd; + /** + * @since 3.0 + */ + protected final String[] envVars; + protected Process process; /** * @since 2.1 */ - protected IProject project = null; + protected final IProject project; private final LoggingStreamDaemon logger; public static final int ERROR_STREAM = 0; @@ -91,18 +98,13 @@ public class Command implements Runnable { * This must be called by the implementing class in order to start the * StreamGobbler. * @param cmd The entire command to run - * @param envVars List of all environment variables to use - * @param project The project this script belongs to or null + * @param envVars List of all environment variables to use, or <code>null</code> if none + * @param project The project this script belongs to, or <code>null</code> if projectless * @since 2.1 */ public Command(String[] cmd, String[] envVars, IProject project) { - if (cmd != null) { - this.cmd = Arrays.copyOf(cmd, cmd.length); - } - - if (envVars != null) { - this.envVars = Arrays.copyOf(envVars, envVars.length); - } + this.cmd = cmd != null ? Arrays.copyOf(cmd, cmd.length) : null; + this.envVars = envVars != null ? Arrays.copyOf(envVars, envVars.length) : null; this.project = project; logger = new LoggingStreamDaemon(); addInputStreamListener(logger); @@ -115,7 +117,7 @@ public class Command implements Runnable { */ public void start() throws CoreException { IStatus status = init(); - if(status.isOK()) { + if (status.isOK()) { Thread t = new Thread(this, cmd[0]); t.start(); started = true; @@ -135,7 +137,7 @@ public class Command implements Runnable { try { process = RuntimeProcessFactory.getFactory().exec(cmd, envVars, project); - if (process == null){ + if (process == null) { return new Status(IStatus.ERROR, StructuresPlugin.PLUGIN_ID, Messages.Command_failedToRunSystemtap); } @@ -155,11 +157,11 @@ public class Command implements Runnable { * properly to the process itself. * @since 2.0 */ - protected void transferListeners(){ - for(IGobblerListener listener :inputListeners) { + protected void transferListeners() { + for (IGobblerListener listener : inputListeners) { inputGobbler.addDataListener(listener); } - for(IGobblerListener listener: errorListeners) { + for (IGobblerListener listener : errorListeners) { errorGobbler.addDataListener(listener); } } @@ -185,15 +187,15 @@ public class Command implements Runnable { * on this command. */ public synchronized void stop() { - if(!stopped) { - if(null != errorGobbler) { + if (!stopped) { + if (errorGobbler != null) { errorGobbler.stop(); } - if(null != inputGobbler) { + if (inputGobbler != null) { inputGobbler.stop(); } try { - if(process != null){ + if (process != null) { process.waitFor(); } } catch (InterruptedException e) { @@ -249,7 +251,7 @@ public class Command implements Runnable { * @param listener A listener to monitor the InputStream from the Process */ public void addInputStreamListener(IGobblerListener listener) { - if(null != inputGobbler) { + if (inputGobbler != null) { inputGobbler.addDataListener(listener); } else { inputListeners.add(listener); @@ -261,7 +263,7 @@ public class Command implements Runnable { * @param listener A listener to monitor the ErrorStream from the Process */ public void addErrorStreamListener(IGobblerListener listener) { - if(null != errorGobbler) { + if (errorGobbler != null) { errorGobbler.addDataListener(listener); } else { errorListeners.add(listener); @@ -273,7 +275,7 @@ public class Command implements Runnable { * @param listener An </code>IGobblerListener</code> that is monitoring the stream. */ public void removeInputStreamListener(IGobblerListener listener) { - if(null != inputGobbler) { + if (inputGobbler != null) { inputGobbler.removeDataListener(listener); } else { inputListeners.remove(listener); @@ -285,7 +287,7 @@ public class Command implements Runnable { * @param listener An </code>IGobblerListener</code> that is monitoring the stream. */ public void removeErrorStreamListener(IGobblerListener listener) { - if(null != errorGobbler) { + if (errorGobbler != null) { errorGobbler.removeDataListener(listener); } else { errorListeners.remove(listener); @@ -306,7 +308,7 @@ public class Command implements Runnable { * referenced after this is called. */ public void dispose() { - if(!disposed) { + if (!disposed) { stop(); disposed = true; @@ -316,12 +318,12 @@ public class Command implements Runnable { inputListeners = null; errorListeners = null; - if(null != inputGobbler) { + if (inputGobbler != null) { inputGobbler.dispose(); } inputGobbler = null; - if(null != errorGobbler) { + if (errorGobbler != null) { errorGobbler.dispose(); } errorGobbler = null; |