Skip to content

Commit

Permalink
Merge pull request #45 from headius/fix_blocking
Browse files Browse the repository at this point in the history
Fix fcntl binding
  • Loading branch information
headius authored Jul 17, 2024
2 parents aaa74df + 2bfd9f8 commit f5269b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/jnr/enxio/channels/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import jnr.ffi.annotations.In;
import jnr.ffi.annotations.Out;
import jnr.ffi.annotations.Transient;
import jnr.ffi.annotations.Variadic;
import jnr.ffi.types.size_t;
import jnr.ffi.types.ssize_t;
import jnr.ffi.Platform;
import jnr.ffi.types.u_int64_t;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -46,7 +48,8 @@ public static interface LibC {
public @ssize_t int read(int fd, @Out byte[] data, @size_t long size);
public @ssize_t int write(int fd, @In ByteBuffer data, @size_t long size);
public @ssize_t int write(int fd, @In byte[] data, @size_t long size);
public int fcntl(int fd, int cmd, int data);
@Variadic(fixedCount = 2)
public int fcntl(int fd, int cmd, @u_int64_t int data);
public int poll(@In @Out ByteBuffer pfds, int nfds, int timeout);
public int poll(@In @Out Pointer pfds, int nfds, int timeout);
public int kqueue();
Expand Down Expand Up @@ -155,6 +158,12 @@ public static void setBlocking(int fd, boolean block) {

libc().fcntl(fd, LibC.F_SETFL, flags);
}

public static boolean getBlocking(int fd) {
int flags = libc().fcntl(fd, LibC.F_GETFL, 0);

return !((flags & LibC.O_NONBLOCK) == LibC.O_NONBLOCK);
}

public static int shutdown(int fd, int how) {
return libc().shutdown(fd, how);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/jnr/enxio/channels/NativeTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package jnr.enxio.channels;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.nio.channels.Pipe;

public class NativeTest {
@Rule
Expand All @@ -23,4 +25,20 @@ public void closeThrowsOnNativeError() throws Exception {
expectedEx.expect(NativeException.class);
Native.close(fd);
}

@Test
public void setBlocking() throws Exception {
Pipe pipe = Pipe.open();
Pipe.SinkChannel sink = pipe.sink();
// sink.getClass().getModule().addOpens("sun.nio.ch", NativeTest.class.getModule());
Field fd1 = sink.getClass().getDeclaredField("fd");
fd1.setAccessible(true);
FileDescriptor descriptor = (FileDescriptor) fd1.get(sink);
Field fdField = descriptor.getClass().getDeclaredField("fd");
fdField.setAccessible(true);
int fd = (int)(Integer)fdField.get(descriptor);
Assert.assertEquals(true, Native.getBlocking(fd));
Native.setBlocking(fd, false);
Assert.assertEquals(false, Native.getBlocking(fd));
}
}

0 comments on commit f5269b2

Please sign in to comment.