提交 e7d75dc5 编写于 作者: K Kohsuke Kawaguchi

Make stdout swapping more reliable.

This matches fb65a2c3c8584a9ac4daa49ecbce7a540dc7f5a5 in remoting.
上级 22d433de
......@@ -38,28 +38,39 @@ public class StandardOutputSwapper extends ComputerListener {
private static final class ChannelSwapper extends MasterToSlaveCallable<Boolean,Exception> {
public Boolean call() throws Exception {
if (File.pathSeparatorChar==';') return false; // Windows
OutputStream o = Channel.current().getUnderlyingOutput();
if (o instanceof StandardOutputStream) {
StandardOutputStream stdout = (StandardOutputStream)o;
// duplicate the OS file descriptor and create FileOutputStream around it
int out = GNUCLibrary.LIBC.dup(1);
if (out<0) throw new IOException("Failed to dup(1)");
Constructor<FileDescriptor> c = FileDescriptor.class.getDeclaredConstructor(int.class);
c.setAccessible(true);
FileOutputStream fos = new FileOutputStream(c.newInstance(out));
Channel c = Channel.current();
// swap it into channel so that it'll use the new file descriptor
stdout.swap(fos);
StandardOutputStream sos = (StandardOutputStream) c.getProperty(StandardOutputStream.class);
if (sos!=null) {
swap(sos);
return true;
}
// close fd=1 (stdout) and duplicate fd=2 (stderr) into fd=1 (stdout)
GNUCLibrary.LIBC.close(1);
GNUCLibrary.LIBC.dup2(2,1);
OutputStream o = c.getUnderlyingOutput();
if (o instanceof StandardOutputStream) {
swap((StandardOutputStream) o);
return true;
}
return false;
}
private void swap(StandardOutputStream stdout) throws IOException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
// duplicate the OS file descriptor and create FileOutputStream around it
int out = GNUCLibrary.LIBC.dup(1);
if (out<0) throw new IOException("Failed to dup(1)");
Constructor<FileDescriptor> c = FileDescriptor.class.getDeclaredConstructor(int.class);
c.setAccessible(true);
FileOutputStream fos = new FileOutputStream(c.newInstance(out));
// swap it into channel so that it'll use the new file descriptor
stdout.swap(fos);
// close fd=1 (stdout) and duplicate fd=2 (stderr) into fd=1 (stdout)
GNUCLibrary.LIBC.close(1);
GNUCLibrary.LIBC.dup2(2,1);
}
}
private static final Logger LOGGER = Logger.getLogger(StandardOutputSwapper.class.getName());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册