diff options
Diffstat (limited to 'systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/process/SystemtapProcessFactory.java')
-rw-r--r-- | systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/process/SystemtapProcessFactory.java | 85 |
1 files changed, 60 insertions, 25 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) { |