Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fcntl binding #45

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading