diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java index 4858bd11cc6..9c7a6345f9f 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTY.java @@ -15,12 +15,27 @@ import java.io.OutputStream; public class PTY { String slave; - public int master; InputStream in; OutputStream out; + int master; private static boolean hasPTY; - + + /** + * The master fd is use on two streams. We need to wrap the fd + * so when stream.close() is call the other stream is disable. + */ + public class MasterFD { + + public int getFD() { + return master; + } + + public void setFD(int fd) { + master = fd; + } + } + public PTY() throws IOException { if (hasPTY) { slave= forkpty(); @@ -29,8 +44,9 @@ public class PTY { if (slave == null) { throw new IOException("Can not create pty"); } - in = new PTYInputStream(master); - out = new PTYOutputStream(master); + + in = new PTYInputStream(new MasterFD()); + out = new PTYOutputStream(new MasterFD()); } public String getSlaveName() { diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java index fa6a29a4918..b4198feb385 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYInputStream.java @@ -8,15 +8,18 @@ package org.eclipse.cdt.utils.pty; import java.io.InputStream; import java.io.IOException; +import org.eclipse.cdt.utils.pty.PTY.MasterFD; + class PTYInputStream extends InputStream { - private int fd; + + MasterFD master; /** * Fome a Unix valid file descriptor set a Reader. * @param desc file descriptor. */ - public PTYInputStream(int fd) { - this.fd = fd; + public PTYInputStream(MasterFD fd) { + master = fd; } /** @@ -46,7 +49,7 @@ class PTYInputStream extends InputStream { } byte[] tmpBuf = new byte[len]; - len = read0(fd, tmpBuf, len); + len = read0(master.getFD(), tmpBuf, len); if (len <= 0) return -1; @@ -59,12 +62,12 @@ class PTYInputStream extends InputStream { * @exception IOException on error. */ public void close() throws IOException { - if (fd == -1) + if (master.getFD() == -1) return; - int status = close0(fd); + int status = close0(master.getFD()); if (status == -1) throw new IOException("close error"); - fd = -1; + master.setFD(-1); } private native int read0(int fd, byte[] buf, int len) throws IOException; diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java index 31ba3235246..4537478e1ce 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/pty/PTYOutputStream.java @@ -8,15 +8,18 @@ package org.eclipse.cdt.utils.pty; import java.io.OutputStream; import java.io.IOException; +import org.eclipse.cdt.utils.pty.PTY.MasterFD; + public class PTYOutputStream extends OutputStream { - private int fd; + + MasterFD master; /** * Fome a Unix valid file descriptor set a Reader. * @param desc file descriptor. */ - public PTYOutputStream(int fd) { - this.fd = fd; + public PTYOutputStream(MasterFD fd) { + master = fd; } /** @@ -37,7 +40,7 @@ public class PTYOutputStream extends OutputStream { } byte[] tmpBuf = new byte[len]; System.arraycopy(b, off, tmpBuf, off, len); - write0(fd, tmpBuf, len); + write0(master.getFD(), tmpBuf, len); } /** * Implementation of read for the InputStream. @@ -55,12 +58,12 @@ public class PTYOutputStream extends OutputStream { * @exception IOException on error. */ public void close() throws IOException { - if (fd == -1) + if (master.getFD() == -1) return; - int status = close0(fd); + int status = close0(master.getFD()); if (status == -1) throw new IOException("close error"); //$NON-NLS-1$ - fd = -1; + master.setFD(-1); } private native int write0(int fd, byte[] b, int len) throws IOException;