diff options
author | Torbjörn Svensson | 2020-08-07 10:21:16 +0000 |
---|---|---|
committer | Jonah Graham | 2020-08-12 15:48:35 +0000 |
commit | 49dc726dd1ba5b22161146b6709ad6c71e97a9d2 (patch) | |
tree | 44283c3049bf1c6a5ec101cec6e30294348567bc /core/org.eclipse.cdt.core.native/src/org | |
parent | 0d50385e635af212006d8205c3af091803a26c6f (diff) | |
download | org.eclipse.cdt-49dc726dd1ba5b22161146b6709ad6c71e97a9d2.tar.gz org.eclipse.cdt-49dc726dd1ba5b22161146b6709ad6c71e97a9d2.tar.xz org.eclipse.cdt-49dc726dd1ba5b22161146b6709ad6c71e97a9d2.zip |
Bug 521515: Use channel object for native stream access
On Linux and mac, a simple int will do for accessing streams (file
descriptor).
On Windows, a HANDLE is used. This is in reality a pointer and on 64
bit JVM, the pointer will not fit in an int. To not force a change to
the API everytime a different platform has a different requirement
for accessing streams, use a common interface and specific classes
that is known by the native part. The java part of the function
block should just pass the object back to the native code when
needing to reference an open stream.
Change-Id: Ibc3ff5c85735dac6a0ce8e9a1f25b839d7e9aab9
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
Diffstat (limited to 'core/org.eclipse.cdt.core.native/src/org')
4 files changed, 65 insertions, 31 deletions
diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/pty/PTY.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/pty/PTY.java index a92f5a06ace..4080fa25b8c 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/pty/PTY.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/pty/PTY.java @@ -20,6 +20,7 @@ import java.io.IOException; import org.eclipse.cdt.internal.core.natives.CNativePlugin; import org.eclipse.cdt.internal.core.natives.Messages; import org.eclipse.cdt.utils.spawner.Spawner; +import org.eclipse.cdt.utils.spawner.Spawner.IChannel; import org.eclipse.core.runtime.Platform; /** @@ -223,7 +224,8 @@ public class PTY { * @noreference This method is not intended to be referenced by clients. * @since 5.6 */ - public int exec_pty(Spawner spawner, String[] cmdarray, String[] envp, String dir, int[] chan) throws IOException { + public int exec_pty(Spawner spawner, String[] cmdarray, String[] envp, String dir, IChannel[] chan) + throws IOException { if (isWinPTY) { return exec2(cmdarray, envp, dir, chan, slave, master, console); } else { @@ -250,7 +252,7 @@ public class PTY { /** * Native method when executing with a terminal emulation (winpty only). */ - native int exec2(String[] cmdarray, String[] envp, String dir, int[] chan, String slaveName, int masterFD, + native int exec2(String[] cmdarray, String[] envp, String dir, IChannel[] chan, String slaveName, int masterFD, boolean console) throws IOException; /** diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java index c5249b99cb0..ee16552777f 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/Spawner.java @@ -65,7 +65,7 @@ public class Spawner extends Process { int pid = 0; int status; - final int[] fChannels = { -1, -1, -1 }; + final IChannel[] fChannels = { null, null, null }; boolean isDone; OutputStream out; InputStream in; @@ -363,7 +363,7 @@ public class Spawner extends Process { Reaper reaper = new Reaper(cmdarray, envp, dirpath) { @Override - int execute(String[] cmd, String[] env, String dir, int[] channels) throws IOException { + int execute(String[] cmd, String[] env, String dir, IChannel[] channels) throws IOException { return pty.exec_pty(Spawner.this, cmd, env, dir, channels); } @@ -429,7 +429,7 @@ public class Spawner extends Process { /** * Native method use in normal exec() calls. */ - native int exec0(String[] cmdarray, String[] envp, String dir, int[] chan) throws IOException; + native int exec0(String[] cmdarray, String[] envp, String dir, IChannel[] chan) throws IOException; /** * Native method use in no redirect meaning to streams will created. @@ -440,8 +440,8 @@ public class Spawner extends Process { * Native method when executing with a terminal emulation. * @noreference This method is not intended to be referenced by clients. */ - public native int exec2(String[] cmdarray, String[] envp, String dir, int[] chan, String slaveName, int masterFD, - boolean console) throws IOException; + public native int exec2(String[] cmdarray, String[] envp, String dir, IChannel[] chan, String slaveName, + int masterFD, boolean console) throws IOException; /** * Native method to drop a signal on the process with pid. @@ -464,6 +464,34 @@ public class Spawner extends Process { } } + /** + * @since 6.0 + */ + public static interface IChannel { + } + + /** + * @since 6.0 + */ + public static class WinChannel implements IChannel { + final long handle; + + public WinChannel(long handle) { + this.handle = handle; + } + } + + /** + * @since 6.0 + */ + public static class UnixChannel implements IChannel { + final int fd; + + public UnixChannel(int fd) { + this.fd = fd; + } + } + // Spawn a thread to handle the forking and waiting // We do it this way because on linux the SIGCHLD is // send to the one thread. So do the forking and @@ -482,7 +510,7 @@ public class Spawner extends Process { fException = null; } - int execute(String[] cmdarray, String[] envp, String dir, int[] channels) throws IOException { + int execute(String[] cmdarray, String[] envp, String dir, IChannel[] channels) throws IOException { return exec0(cmdarray, envp, dir, channels); } diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerInputStream.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerInputStream.java index 5e8e2926cc1..27533cc5fda 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerInputStream.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerInputStream.java @@ -18,16 +18,17 @@ import java.io.IOException; import java.io.InputStream; import org.eclipse.cdt.internal.core.natives.Messages; +import org.eclipse.cdt.utils.spawner.Spawner.IChannel; class SpawnerInputStream extends InputStream { - private int fd; + private IChannel channel; /** * From a Unix valid file descriptor set a Reader. * @param fd file descriptor. */ - public SpawnerInputStream(int fd) { - this.fd = fd; + public SpawnerInputStream(IChannel channel) { + this.channel = channel; } /** @@ -48,7 +49,7 @@ class SpawnerInputStream extends InputStream { */ @Override public int read(byte[] buf, int off, int len) throws IOException { - if (fd == -1) { + if (channel == null) { return -1; } if (buf == null) { @@ -60,7 +61,7 @@ class SpawnerInputStream extends InputStream { } byte[] tmpBuf = off > 0 ? new byte[len] : buf; - len = read0(fd, tmpBuf, len); + len = read0(channel, tmpBuf, len); if (len <= 0) return -1; @@ -76,21 +77,21 @@ class SpawnerInputStream extends InputStream { */ @Override public void close() throws IOException { - if (fd == -1) + if (channel == null) return; - int status = close0(fd); + int status = close0(channel); if (status == -1) throw new IOException(Messages.Util_exception_closeError); - fd = -1; + channel = null; } @Override public int available() throws IOException { - if (fd == -1) { + if (channel == null) { return 0; } try { - return available0(fd); + return available0(channel); } catch (UnsatisfiedLinkError e) { // for those platforms that do not implement available0 return super.available(); @@ -102,11 +103,11 @@ class SpawnerInputStream extends InputStream { close(); } - private native int read0(int fileDesc, byte[] buf, int len) throws IOException; + private native int read0(IChannel channel, byte[] buf, int len) throws IOException; - private native int close0(int fileDesc) throws IOException; + private native int close0(IChannel channel) throws IOException; - private native int available0(int fileDesc) throws IOException; + private native int available0(IChannel channel) throws IOException; static { System.loadLibrary("spawner"); //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java index 01863b491f0..a6e51e824da 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/spawner/SpawnerOutputStream.java @@ -16,19 +16,22 @@ package org.eclipse.cdt.utils.spawner; import java.io.IOException; import java.io.OutputStream; +import org.eclipse.cdt.utils.spawner.Spawner.IChannel; + /** * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ public class SpawnerOutputStream extends OutputStream { - private int fd; + private IChannel channel; /** * From a Unix valid file descriptor set a Reader. - * @param fd file descriptor. + * @param channel file descriptor. + * @since 6.0 */ - public SpawnerOutputStream(int fd) { - this.fd = fd; + public SpawnerOutputStream(IChannel channel) { + this.channel = channel; } /** @@ -45,7 +48,7 @@ public class SpawnerOutputStream extends OutputStream { } byte[] tmpBuf = new byte[len]; System.arraycopy(b, off, tmpBuf, off, len); - write0(fd, tmpBuf, len); + write0(channel, tmpBuf, len); } /** @@ -66,12 +69,12 @@ public class SpawnerOutputStream extends OutputStream { */ @Override public void close() throws IOException { - if (fd == -1) + if (channel == null) return; - int status = close0(fd); + int status = close0(channel); if (status == -1) throw new IOException("close error"); //$NON-NLS-1$ - fd = -1; + channel = null; } @Override @@ -79,9 +82,9 @@ public class SpawnerOutputStream extends OutputStream { close(); } - private native int write0(int fd, byte[] b, int len) throws IOException; + private native int write0(IChannel channel, byte[] b, int len) throws IOException; - private native int close0(int fd); + private native int close0(IChannel channel); static { System.loadLibrary("spawner"); //$NON-NLS-1$ |