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

[JENKINS-65911] Correct JNA method signature for fcntl(2) #9026

Merged
merged 1 commit into from Mar 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/jna/GNUCLibrary.java
Expand Up @@ -77,7 +77,7 @@ public interface GNUCLibrary extends Library {

int fcntl(int fd, int command);

int fcntl(int fd, int command, int flags);
int fcntl(int fd, int command, Object... flags);

// obtained from Linux. Needs to be checked if these values are portable.
int F_GETFD = 1;
Expand Down
103 changes: 103 additions & 0 deletions core/src/test/java/hudson/util/jna/GNUCLibraryTest.java
@@ -0,0 +1,103 @@
package hudson.util.jna;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import com.sun.jna.Native;
import hudson.Functions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;

public class GNUCLibraryTest {

private static final int EBADF = 9;
private static final int O_CREAT = "Linux".equals(System.getProperty("os.name")) ? 64 : 512;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static final int O_RDWR = 2;

@Test
public void openTest() throws IOException {
assumeTrue(Functions.isGlibcSupported());

int fd = GNUCLibrary.LIBC.open("/dev/null", 0);
assertNotEquals(-1, fd);

int result = GNUCLibrary.LIBC.close(fd);
assertEquals(0, result);

result = GNUCLibrary.LIBC.close(fd);
assertEquals(-1, result);
assertEquals(EBADF, Native.getLastError());

Path tmpFile = Files.createTempFile("openTest", null);
Files.delete(tmpFile);
assertFalse(Files.exists(tmpFile));
try {
fd = GNUCLibrary.LIBC.open(tmpFile.toString(), O_CREAT | O_RDWR);
assertTrue(Files.exists(tmpFile));

result = GNUCLibrary.LIBC.close(fd);
assertEquals(0, result);
} finally {
Files.deleteIfExists(tmpFile);
}
}

@Test
public void closeTest() {
assumeTrue(Functions.isGlibcSupported());

int fd = GNUCLibrary.LIBC.open("/dev/null", 0);
assertNotEquals(-1, fd);

int result = GNUCLibrary.LIBC.close(fd);
assertEquals(0, result);

result = GNUCLibrary.LIBC.close(fd);
assertEquals(-1, result);
assertEquals(EBADF, Native.getLastError());
}

@Test
public void fcntlTest() {
assumeTrue(Functions.isGlibcSupported());

int fd = GNUCLibrary.LIBC.open("/dev/null", 0);
assertNotEquals(-1, fd);
try {
int flags = GNUCLibrary.LIBC.fcntl(fd, GNUCLibrary.F_GETFD);
assertEquals(0, flags);
int result =
GNUCLibrary.LIBC.fcntl(fd, GNUCLibrary.F_SETFD, flags | GNUCLibrary.FD_CLOEXEC);
assertEquals(0, result);
result = GNUCLibrary.LIBC.fcntl(fd, GNUCLibrary.F_GETFD);
assertEquals(GNUCLibrary.FD_CLOEXEC, result);
} finally {
GNUCLibrary.LIBC.close(fd);
}
}

@Test
public void renameTest() throws IOException {
assumeTrue(Functions.isGlibcSupported());

Path oldFile = Files.createTempFile("renameTest", null);
Path newFile = Files.createTempFile("renameTest", null);
Files.delete(newFile);
assertTrue(Files.exists(oldFile));
assertFalse(Files.exists(newFile));
try {
GNUCLibrary.LIBC.rename(oldFile.toString(), newFile.toString());

assertFalse(Files.exists(oldFile));
assertTrue(Files.exists(newFile));
} finally {
Files.deleteIfExists(oldFile);
Files.deleteIfExists(newFile);
}
}
}