summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenedict Wong <benedictwong@google.com>2019-12-18 17:54:36 -0800
committerBenedict Wong <benedictwong@google.com>2019-12-19 12:24:35 -0800
commitc719c15db27e83d3293fca5b6a6df9592bc21fd0 (patch)
treea03b4d15c8314568eefa8a4ced2f47356a2d0a79
parent5a91d71a496d12b493f2bbbecf3dee953aed93b6 (diff)
Add fcntlInt to public API
In order to allow sockets to be set as non-blocking after the creation, fcntlInt must be exposed. Test: new tests added, passing Change-Id: If30d3750734a5a3910587a06df334c04fad92703
-rw-r--r--luni/src/main/java/android/system/Os.java7
-rw-r--r--luni/src/test/java/libcore/android/system/OsTest.java39
-rw-r--r--mmodules/core_platform_api/api/platform/current-api.txt1
3 files changed, 43 insertions, 4 deletions
diff --git a/luni/src/main/java/android/system/Os.java b/luni/src/main/java/android/system/Os.java
index e2b191eee7..70fdce5c16 100644
--- a/luni/src/main/java/android/system/Os.java
+++ b/luni/src/main/java/android/system/Os.java
@@ -151,9 +151,10 @@ public final class Os {
*/
public static void fchown(FileDescriptor fd, int uid, int gid) throws ErrnoException { Libcore.os.fchown(fd, uid, gid); }
- /** @hide */
- @libcore.api.CorePlatformApi
- public static int fcntlInt(FileDescriptor fd, int cmd, int arg) throws ErrnoException { return Libcore.os.fcntlInt(fd, cmd, arg); }
+ /**
+ * See <a href="http://man7.org/linux/man-pages/man2/fcntl.2.html">fcntl(2)</a>.
+ */
+ public static int fcntlInt(@NonNull FileDescriptor fd, int cmd, int arg) throws ErrnoException { return Libcore.os.fcntlInt(fd, cmd, arg); }
/** @hide */
public static int fcntlVoid(FileDescriptor fd, int cmd) throws ErrnoException { return Libcore.os.fcntlVoid(fd, cmd); }
diff --git a/luni/src/test/java/libcore/android/system/OsTest.java b/luni/src/test/java/libcore/android/system/OsTest.java
index 25d56bcbc3..0c650462cc 100644
--- a/luni/src/test/java/libcore/android/system/OsTest.java
+++ b/luni/src/test/java/libcore/android/system/OsTest.java
@@ -87,6 +87,45 @@ public class OsTest extends TestCase {
}
}
+ public void testFcntlInt_UdpSocket() throws Exception {
+ final FileDescriptor fd = Os.socket(AF_INET, SOCK_DGRAM, 0);
+ try {
+ assertEquals(0, (Os.fcntlVoid(fd, F_GETFL) & O_NONBLOCK));
+
+ // Verify that we can set file descriptor flags on sockets
+ Os.fcntlInt(fd, F_SETFL, SOCK_DGRAM | O_NONBLOCK);
+ assertTrue((Os.fcntlVoid(fd, F_GETFL) & O_NONBLOCK) != 0);
+
+ // Check that we can turn it off also.
+ Os.fcntlInt(fd, F_SETFL, SOCK_DGRAM);
+ assertEquals(0, (Os.fcntlVoid(fd, F_GETFL) & O_NONBLOCK));
+ } finally {
+ Os.close(fd);
+ }
+ }
+
+ public void testFcntlInt_InvalidCmd() throws Exception {
+ final FileDescriptor fd = Os.socket(AF_INET, SOCK_DGRAM, 0);
+ try {
+ final int unknownCmd = -1;
+ Os.fcntlInt(fd, unknownCmd, 0);
+ fail("Expected failure due to invalid cmd");
+ } catch (ErrnoException expected) {
+ assertEquals(EINVAL, expected.errno);
+ } finally {
+ Os.close(fd);
+ }
+ }
+
+ public void testFcntlInt_NullFd() throws Exception {
+ try {
+ Os.fcntlInt(null, F_SETFL, O_NONBLOCK);
+ fail("Expected failure due to null file descriptor");
+ } catch (ErrnoException expected) {
+ assertEquals(EBADF, expected.errno);
+ }
+ }
+
public void testUnixDomainSockets_in_file_system() throws Exception {
String path = System.getProperty("java.io.tmpdir") + "/test_unix_socket";
new File(path).delete();
diff --git a/mmodules/core_platform_api/api/platform/current-api.txt b/mmodules/core_platform_api/api/platform/current-api.txt
index 8abb9be236..28231569cb 100644
--- a/mmodules/core_platform_api/api/platform/current-api.txt
+++ b/mmodules/core_platform_api/api/platform/current-api.txt
@@ -44,7 +44,6 @@ package android.system {
public final class Os {
method public static android.system.StructCapUserData[] capget(android.system.StructCapUserHeader) throws android.system.ErrnoException;
method public static void capset(android.system.StructCapUserHeader, android.system.StructCapUserData[]) throws android.system.ErrnoException;
- method public static int fcntlInt(java.io.FileDescriptor, int, int) throws android.system.ErrnoException;
method public static int getpgid(int) throws android.system.ErrnoException;
method public static android.system.StructRlimit getrlimit(int) throws android.system.ErrnoException;
method public static int getsockoptInt(java.io.FileDescriptor, int, int) throws android.system.ErrnoException;