Loading core/java/android/os/ParcelFileDescriptor.java +27 −25 Original line number Original line Diff line number Diff line Loading @@ -231,10 +231,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { final FileDescriptor fd = openInternal(file, mode); final FileDescriptor fd = openInternal(file, mode); if (fd == null) return null; if (fd == null) return null; final FileDescriptor[] comm = createCommSocketPair(true); final FileDescriptor[] comm = createCommSocketPair(); final ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd, comm[0]); final ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd, comm[0]); // Kick off thread to watch for status updates // Kick off thread to watch for status updates IoUtils.setBlocking(comm[1], true); final ListenerBridge bridge = new ListenerBridge(comm[1], handler.getLooper(), listener); final ListenerBridge bridge = new ListenerBridge(comm[1], handler.getLooper(), listener); bridge.start(); bridge.start(); Loading Loading @@ -378,7 +379,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { */ */ public static ParcelFileDescriptor[] createReliablePipe() throws IOException { public static ParcelFileDescriptor[] createReliablePipe() throws IOException { try { try { final FileDescriptor[] comm = createCommSocketPair(false); final FileDescriptor[] comm = createCommSocketPair(); final FileDescriptor[] fds = Libcore.os.pipe(); final FileDescriptor[] fds = Libcore.os.pipe(); return new ParcelFileDescriptor[] { return new ParcelFileDescriptor[] { new ParcelFileDescriptor(fds[0], comm[0]), new ParcelFileDescriptor(fds[0], comm[0]), Loading Loading @@ -416,7 +417,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { */ */ public static ParcelFileDescriptor[] createReliableSocketPair() throws IOException { public static ParcelFileDescriptor[] createReliableSocketPair() throws IOException { try { try { final FileDescriptor[] comm = createCommSocketPair(false); final FileDescriptor[] comm = createCommSocketPair(); final FileDescriptor fd0 = new FileDescriptor(); final FileDescriptor fd0 = new FileDescriptor(); final FileDescriptor fd1 = new FileDescriptor(); final FileDescriptor fd1 = new FileDescriptor(); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, fd0, fd1); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, fd0, fd1); Loading @@ -428,13 +429,13 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } } } } private static FileDescriptor[] createCommSocketPair(boolean blocking) throws IOException { private static FileDescriptor[] createCommSocketPair() throws IOException { try { try { final FileDescriptor comm1 = new FileDescriptor(); final FileDescriptor comm1 = new FileDescriptor(); final FileDescriptor comm2 = new FileDescriptor(); final FileDescriptor comm2 = new FileDescriptor(); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, comm1, comm2); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, comm1, comm2); IoUtils.setBlocking(comm1, blocking); IoUtils.setBlocking(comm1, false); IoUtils.setBlocking(comm2, blocking); IoUtils.setBlocking(comm2, false); return new FileDescriptor[] { comm1, comm2 }; return new FileDescriptor[] { comm1, comm2 }; } catch (ErrnoException e) { } catch (ErrnoException e) { throw e.rethrowAsIOException(); throw e.rethrowAsIOException(); Loading Loading @@ -670,8 +671,16 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } } try { try { if (status == Status.SILENCE) return; // Since we're about to close, read off any remote status. It's // okay to remember missing here. mStatus = readCommStatus(mCommFd, getOrCreateStatusBuffer()); // Skip writing status when other end has already gone away. if (mStatus != null) return; try { try { if (status != Status.SILENCE) { final byte[] buf = getOrCreateStatusBuffer(); final byte[] buf = getOrCreateStatusBuffer(); int writePtr = 0; int writePtr = 0; Loading @@ -686,18 +695,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } } Libcore.os.write(mCommFd, buf, 0, writePtr); Libcore.os.write(mCommFd, buf, 0, writePtr); } } catch (ErrnoException e) { } catch (ErrnoException e) { // Reporting status is best-effort // Reporting status is best-effort Log.w(TAG, "Failed to report status: " + e); Log.w(TAG, "Failed to report status: " + e); } } if (status != Status.SILENCE) { // Since we're about to close, read off any remote status. It's // okay to remember missing here. mStatus = readCommStatus(mCommFd, getOrCreateStatusBuffer()); } } finally { } finally { IoUtils.closeQuietly(mCommFd); IoUtils.closeQuietly(mCommFd); mCommFd = null; mCommFd = null; Loading Loading
core/java/android/os/ParcelFileDescriptor.java +27 −25 Original line number Original line Diff line number Diff line Loading @@ -231,10 +231,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { final FileDescriptor fd = openInternal(file, mode); final FileDescriptor fd = openInternal(file, mode); if (fd == null) return null; if (fd == null) return null; final FileDescriptor[] comm = createCommSocketPair(true); final FileDescriptor[] comm = createCommSocketPair(); final ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd, comm[0]); final ParcelFileDescriptor pfd = new ParcelFileDescriptor(fd, comm[0]); // Kick off thread to watch for status updates // Kick off thread to watch for status updates IoUtils.setBlocking(comm[1], true); final ListenerBridge bridge = new ListenerBridge(comm[1], handler.getLooper(), listener); final ListenerBridge bridge = new ListenerBridge(comm[1], handler.getLooper(), listener); bridge.start(); bridge.start(); Loading Loading @@ -378,7 +379,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { */ */ public static ParcelFileDescriptor[] createReliablePipe() throws IOException { public static ParcelFileDescriptor[] createReliablePipe() throws IOException { try { try { final FileDescriptor[] comm = createCommSocketPair(false); final FileDescriptor[] comm = createCommSocketPair(); final FileDescriptor[] fds = Libcore.os.pipe(); final FileDescriptor[] fds = Libcore.os.pipe(); return new ParcelFileDescriptor[] { return new ParcelFileDescriptor[] { new ParcelFileDescriptor(fds[0], comm[0]), new ParcelFileDescriptor(fds[0], comm[0]), Loading Loading @@ -416,7 +417,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { */ */ public static ParcelFileDescriptor[] createReliableSocketPair() throws IOException { public static ParcelFileDescriptor[] createReliableSocketPair() throws IOException { try { try { final FileDescriptor[] comm = createCommSocketPair(false); final FileDescriptor[] comm = createCommSocketPair(); final FileDescriptor fd0 = new FileDescriptor(); final FileDescriptor fd0 = new FileDescriptor(); final FileDescriptor fd1 = new FileDescriptor(); final FileDescriptor fd1 = new FileDescriptor(); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, fd0, fd1); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, fd0, fd1); Loading @@ -428,13 +429,13 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } } } } private static FileDescriptor[] createCommSocketPair(boolean blocking) throws IOException { private static FileDescriptor[] createCommSocketPair() throws IOException { try { try { final FileDescriptor comm1 = new FileDescriptor(); final FileDescriptor comm1 = new FileDescriptor(); final FileDescriptor comm2 = new FileDescriptor(); final FileDescriptor comm2 = new FileDescriptor(); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, comm1, comm2); Libcore.os.socketpair(AF_UNIX, SOCK_STREAM, 0, comm1, comm2); IoUtils.setBlocking(comm1, blocking); IoUtils.setBlocking(comm1, false); IoUtils.setBlocking(comm2, blocking); IoUtils.setBlocking(comm2, false); return new FileDescriptor[] { comm1, comm2 }; return new FileDescriptor[] { comm1, comm2 }; } catch (ErrnoException e) { } catch (ErrnoException e) { throw e.rethrowAsIOException(); throw e.rethrowAsIOException(); Loading Loading @@ -670,8 +671,16 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } } try { try { if (status == Status.SILENCE) return; // Since we're about to close, read off any remote status. It's // okay to remember missing here. mStatus = readCommStatus(mCommFd, getOrCreateStatusBuffer()); // Skip writing status when other end has already gone away. if (mStatus != null) return; try { try { if (status != Status.SILENCE) { final byte[] buf = getOrCreateStatusBuffer(); final byte[] buf = getOrCreateStatusBuffer(); int writePtr = 0; int writePtr = 0; Loading @@ -686,18 +695,11 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } } Libcore.os.write(mCommFd, buf, 0, writePtr); Libcore.os.write(mCommFd, buf, 0, writePtr); } } catch (ErrnoException e) { } catch (ErrnoException e) { // Reporting status is best-effort // Reporting status is best-effort Log.w(TAG, "Failed to report status: " + e); Log.w(TAG, "Failed to report status: " + e); } } if (status != Status.SILENCE) { // Since we're about to close, read off any remote status. It's // okay to remember missing here. mStatus = readCommStatus(mCommFd, getOrCreateStatusBuffer()); } } finally { } finally { IoUtils.closeQuietly(mCommFd); IoUtils.closeQuietly(mCommFd); mCommFd = null; mCommFd = null; Loading