@Override public FileDescriptor open(String path, int flags, int mode) throws ErrnoException { BlockGuard.getThreadPolicy().onReadFromDisk(); if ((mode & O_ACCMODE) != O_RDONLY) { BlockGuard.getThreadPolicy().onWriteToDisk(); } return os.open(path, flags, mode); }
@Override public int sendto( FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { BlockGuard.getThreadPolicy().onNetwork(); return os.sendto(fd, buffer, flags, inetAddress, port); }
@Override public int poll(StructPollfd[] fds, int timeoutMs) throws ErrnoException { // Greater than 0 is a timeout in milliseconds and -1 means "block forever", // but 0 means "poll and return immediately", which shouldn't be subject to BlockGuard. if (timeoutMs != 0) { BlockGuard.getThreadPolicy().onNetwork(); } return os.poll(fds, timeoutMs); }
@Override public int recvfrom( FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { BlockGuard.getThreadPolicy().onNetwork(); return os.recvfrom(fd, bytes, byteOffset, byteCount, flags, srcAddress); }
@Override public int sendto( FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws ErrnoException, SocketException { // We permit datagrams without hostname lookups. if (inetAddress != null) { BlockGuard.getThreadPolicy().onNetwork(); } return os.sendto(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); }
@Override public void close(FileDescriptor fd) throws ErrnoException { try { if (S_ISSOCK(Libcore.os.fstat(fd).st_mode)) { if (isLingerSocket(fd)) { // If the fd is a socket with SO_LINGER set, we might block indefinitely. // We allow non-linger sockets so that apps can close their network // connections in methods like onDestroy which will run on the UI thread. BlockGuard.getThreadPolicy().onNetwork(); } untagSocket(fd); } } catch (ErrnoException ignored) { // We're called via Socket.close (which doesn't ask for us to be called), so we // must not throw here, because Socket.close must not throw if asked to close an // already-closed socket. Also, the passed-in FileDescriptor isn't necessarily // a socket at all. } os.close(fd); }
@Override public void fsync(FileDescriptor fd) throws ErrnoException { BlockGuard.getThreadPolicy().onWriteToDisk(); os.fsync(fd); }
@Override public void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException, SocketException { BlockGuard.getThreadPolicy().onNetwork(); os.connect(fd, address, port); }
@Override public FileDescriptor accept(FileDescriptor fd, InetSocketAddress peerAddress) throws ErrnoException, SocketException { BlockGuard.getThreadPolicy().onNetwork(); return tagSocket(os.accept(fd, peerAddress)); }
@Override public int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException { BlockGuard.getThreadPolicy().onWriteToDisk(); return os.writev(fd, buffers, offsets, byteCounts); }
@Override public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { BlockGuard.getThreadPolicy().onWriteToDisk(); return os.write(fd, bytes, byteOffset, byteCount); }
@Override public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { BlockGuard.getThreadPolicy().onWriteToDisk(); return os.write(fd, buffer); }
@Override public int recvfrom(FileDescriptor fd, ByteBuffer buffer, int flags, InetSocketAddress srcAddress) throws ErrnoException, SocketException { BlockGuard.getThreadPolicy().onNetwork(); return os.recvfrom(fd, buffer, flags, srcAddress); }
@Override public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException { BlockGuard.getThreadPolicy().onReadFromDisk(); return os.read(fd, bytes, byteOffset, byteCount); }
@Override public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException { BlockGuard.getThreadPolicy().onReadFromDisk(); return os.read(fd, buffer); }
@Override public void ftruncate(FileDescriptor fd, long length) throws ErrnoException { BlockGuard.getThreadPolicy().onWriteToDisk(); os.ftruncate(fd, length); }