Java Code Examples for android.system.Os#close()

The following examples show how to use android.system.Os#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PinnerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Close FD, swallowing irrelevant errors.
 */
private static void safeClose(@Nullable FileDescriptor fd) {
    if (fd != null && fd.valid()) {
        try {
            Os.close(fd);
        } catch (ErrnoException ex) {
            // Swallow the exception: non-EBADF errors in close(2)
            // indicate deferred paging write errors, which we
            // don't care about here. The underlying file
            // descriptor is always closed.
            if (ex.errno == OsConstants.EBADF) {
                throw new AssertionError(ex);
            }
        }
    }
}
 
Example 2
Source File: IpSecService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * This function finds and forcibly binds to a random system port, ensuring that the port cannot
 * be unbound.
 *
 * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select
 * a random open port and then bind by number, this function creates a temp socket, binds to a
 * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP
 * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned
 * FileHandle.
 *
 * <p>The loop in this function handles the inherent race window between un-binding to a port
 * and re-binding, during which the system could *technically* hand that port out to someone
 * else.
 */
private int bindToRandomPort(FileDescriptor sockFd) throws IOException {
    for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) {
        try {
            FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
            Os.bind(probeSocket, INADDR_ANY, 0);
            int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort();
            Os.close(probeSocket);
            Log.v(TAG, "Binding to port " + port);
            Os.bind(sockFd, INADDR_ANY, port);
            return port;
        } catch (ErrnoException e) {
            // Someone miraculously claimed the port just after we closed probeSocket.
            if (e.errno == OsConstants.EADDRINUSE) {
                continue;
            }
            throw e.rethrowAsIOException();
        }
    }
    throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port");
}
 
Example 3
Source File: FileBridge.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    final byte[] temp = new byte[8192];
    try {
        while (IoBridge.read(mServer, temp, 0, MSG_LENGTH) == MSG_LENGTH) {
            final int cmd = Memory.peekInt(temp, 0, ByteOrder.BIG_ENDIAN);
            if (cmd == CMD_WRITE) {
                // Shuttle data into local file
                int len = Memory.peekInt(temp, 4, ByteOrder.BIG_ENDIAN);
                while (len > 0) {
                    int n = IoBridge.read(mServer, temp, 0, Math.min(temp.length, len));
                    if (n == -1) {
                        throw new IOException(
                                "Unexpected EOF; still expected " + len + " bytes");
                    }
                    IoBridge.write(mTarget, temp, 0, n);
                    len -= n;
                }

            } else if (cmd == CMD_FSYNC) {
                // Sync and echo back to confirm
                Os.fsync(mTarget);
                IoBridge.write(mServer, temp, 0, MSG_LENGTH);

            } else if (cmd == CMD_CLOSE) {
                // Close and echo back to confirm
                Os.fsync(mTarget);
                Os.close(mTarget);
                mClosed = true;
                IoBridge.write(mServer, temp, 0, MSG_LENGTH);
                break;
            }
        }

    } catch (ErrnoException | IOException e) {
        Log.wtf(TAG, "Failed during bridge", e);
    } finally {
        forceClose();
    }
}
 
Example 4
Source File: SharedMemory.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        Os.close(mFd);
    } catch (ErrnoException e) { /* swallow error */ }
    mMemoryReference.release();
    mMemoryReference = null;
}
 
Example 5
Source File: LocalSocketImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the socket.
 *
 * @throws IOException
 */
public void close() throws IOException {
    synchronized (LocalSocketImpl.this) {
        if ((fd == null) || (mFdCreatedInternally == false)) {
            fd = null;
            return;
        }
        try {
            Os.close(fd);
        } catch (ErrnoException e) {
            e.rethrowAsIOException();
        }
        fd = null;
    }
}
 
Example 6
Source File: FileBridge.java    From container with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
    final byte[] temp = new byte[8192];
    try {
        while (read(mServer, temp, 0, MSG_LENGTH) == MSG_LENGTH) {
            final int cmd = FileUtils.peekInt(temp, 0, ByteOrder.BIG_ENDIAN);
            if (cmd == CMD_WRITE) {
                // Shuttle data into local file
                int len = FileUtils.peekInt(temp, 4, ByteOrder.BIG_ENDIAN);
                while (len > 0) {
                    int n = read(mServer, temp, 0, Math.min(temp.length, len));
                    if (n == -1) {
                        throw new IOException(
                                "Unexpected EOF; still expected " + len + " bytes");
                    }
                    write(mTarget, temp, 0, n);
                    len -= n;
                }

            } else if (cmd == CMD_FSYNC) {
                // Sync and echo back to confirm
                Os.fsync(mTarget);
                write(mServer, temp, 0, MSG_LENGTH);

            } else if (cmd == CMD_CLOSE) {
                // Close and echo back to confirm
                Os.fsync(mTarget);
                Os.close(mTarget);
                mClosed = true;
                write(mServer, temp, 0, MSG_LENGTH);
                break;
            }
        }

    } catch (ErrnoException | IOException e) {
        Log.wtf(TAG, "Failed during bridge", e);
    } finally {
        forceClose();
    }
}
 
Example 7
Source File: FileBridge.java    From container with GNU General Public License v3.0 5 votes vote down vote up
public static void closeQuietly(FileDescriptor fd) {

        if (fd != null && fd.valid()) {
            try {
                Os.close(fd);
            } catch (ErrnoException e) {
                e.printStackTrace();
            }
        }
    }
 
Example 8
Source File: Provider.java    From Daedalus with GNU General Public License v3.0 5 votes vote down vote up
public void stop() {
    try {
        if (mInterruptFd != null) {
            Os.close(mInterruptFd);
        }
        if (mBlockFd != null) {
            Os.close(mBlockFd);
        }
        if (this.descriptor != null) {
            this.descriptor.close();
            this.descriptor = null;
        }
    } catch (Exception ignored) {
    }
}
 
Example 9
Source File: OpenVpnManagementThread.java    From bitmask_android with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void fdCloseLollipop(FileDescriptor fd) {
    try {
        Os.close(fd);
    } catch (Exception e) {
        VpnStatus.logException("Failed to close fd (" + fd + ")", e);
    }
}
 
Example 10
Source File: GifInfoHandle.java    From sketch with Apache License 2.0 5 votes vote down vote up
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private static int getNativeFileDescriptor(FileDescriptor fileDescriptor, boolean closeOriginalDescriptor) throws GifIOException, ErrnoException {
	try {
		final int nativeFileDescriptor = createTempNativeFileDescriptor();
		Os.dup2(fileDescriptor, nativeFileDescriptor);
		return nativeFileDescriptor;
	} finally {
		if (closeOriginalDescriptor) {
			Os.close(fileDescriptor);
		}
	}
}