Search in sources :

Example 11 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class NetworkScoreServiceTest method testDump_noDumpPermission.

@Test
public void testDump_noDumpPermission() {
    doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission(eq(permission.DUMP), anyString());
    try {
        mNetworkScoreService.dump(new FileDescriptor(), new PrintWriter(new StringWriter()), new String[0]);
        fail("SecurityException expected");
    } catch (SecurityException e) {
    // expected
    }
}
Also used : StringWriter(java.io.StringWriter) FileDescriptor(java.io.FileDescriptor) PrintWriter(java.io.PrintWriter) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 12 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class NativeDaemonConnector method listenToSocket.

private void listenToSocket() throws IOException {
    LocalSocket socket = null;
    try {
        socket = new LocalSocket();
        LocalSocketAddress address = determineSocketAddress();
        socket.connect(address);
        InputStream inputStream = socket.getInputStream();
        synchronized (mDaemonLock) {
            mOutputStream = socket.getOutputStream();
        }
        mCallbacks.onDaemonConnected();
        FileDescriptor[] fdList = null;
        byte[] buffer = new byte[BUFFER_SIZE];
        int start = 0;
        while (true) {
            int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
            if (count < 0) {
                loge("got " + count + " reading with start = " + start);
                break;
            }
            fdList = socket.getAncillaryFileDescriptors();
            // Add our starting point to the count and reset the start.
            count += start;
            start = 0;
            for (int i = 0; i < count; i++) {
                if (buffer[i] == 0) {
                    // Note - do not log this raw message since it may contain
                    // sensitive data
                    final String rawEvent = new String(buffer, start, i - start, StandardCharsets.UTF_8);
                    boolean releaseWl = false;
                    try {
                        final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(rawEvent, fdList);
                        log("RCV <- {" + event + "}");
                        if (event.isClassUnsolicited()) {
                            // TODO: migrate to sending NativeDaemonEvent instances
                            if (mCallbacks.onCheckHoldWakeLock(event.getCode()) && mWakeLock != null) {
                                mWakeLock.acquire();
                                releaseWl = true;
                            }
                            Message msg = mCallbackHandler.obtainMessage(event.getCode(), uptimeMillisInt(), 0, event.getRawEvent());
                            if (mCallbackHandler.sendMessage(msg)) {
                                releaseWl = false;
                            }
                        } else {
                            mResponseQueue.add(event.getCmdNumber(), event);
                        }
                    } catch (IllegalArgumentException e) {
                        log("Problem parsing message " + e);
                    } finally {
                        if (releaseWl) {
                            mWakeLock.release();
                        }
                    }
                    start = i + 1;
                }
            }
            if (start == 0) {
                log("RCV incomplete");
            }
            // buffer and read again.
            if (start != count) {
                final int remaining = BUFFER_SIZE - start;
                System.arraycopy(buffer, start, buffer, 0, remaining);
                start = remaining;
            } else {
                start = 0;
            }
        }
    } catch (IOException ex) {
        loge("Communications error: " + ex);
        throw ex;
    } finally {
        synchronized (mDaemonLock) {
            if (mOutputStream != null) {
                try {
                    loge("closing stream for " + mSocket);
                    mOutputStream.close();
                } catch (IOException e) {
                    loge("Failed closing output stream: " + e);
                }
                mOutputStream = null;
            }
        }
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ex) {
            loge("Failed closing socket: " + ex);
        }
    }
}
Also used : Message(android.os.Message) InputStream(java.io.InputStream) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 13 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class PinnerService method pinFile.

/** mlock length bytes of fileToPin in memory, starting at offset
     *  length == 0 means pin from offset to end of file
     *  maxSize == 0 means infinite
     */
private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
    FileDescriptor fd = new FileDescriptor();
    try {
        fd = Os.open(fileToPin, OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, OsConstants.O_RDONLY);
        StructStat sb = Os.fstat(fd);
        if (offset + length > sb.st_size) {
            Os.close(fd);
            Slog.e(TAG, "Failed to pin file " + fileToPin + ", request extends beyond end of file.  offset + length =  " + (offset + length) + ", file length = " + sb.st_size);
            return null;
        }
        if (length == 0) {
            length = sb.st_size - offset;
        }
        if (maxSize > 0 && length > maxSize) {
            Slog.e(TAG, "Could not pin file " + fileToPin + ", size = " + length + ", maxSize = " + maxSize);
            Os.close(fd);
            return null;
        }
        long address = Os.mmap(0, length, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, fd, offset);
        Os.close(fd);
        Os.mlock(address, length);
        return new PinnedFile(address, length, fileToPin);
    } catch (ErrnoException e) {
        Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
        if (fd.valid()) {
            try {
                Os.close(fd);
            } catch (ErrnoException eClose) {
                Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
            }
        }
        return null;
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 14 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class FileBackupHelperBase method performBackup_checked.

/**
     * Check the parameters so the native code doesn't have to throw all the exceptions
     * since it's easier to do that from Java.
     */
static void performBackup_checked(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState, String[] files, String[] keys) {
    if (files.length == 0) {
        return;
    }
    // files must be all absolute paths
    for (String f : files) {
        if (f.charAt(0) != '/') {
            throw new RuntimeException("files must have all absolute paths: " + f);
        }
    }
    // the length of files and keys must be the same
    if (files.length != keys.length) {
        throw new RuntimeException("files.length=" + files.length + " keys.length=" + keys.length);
    }
    // oldStateFd can be null
    FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null;
    FileDescriptor newStateFd = newState.getFileDescriptor();
    if (newStateFd == null) {
        throw new NullPointerException();
    }
    int err = performBackup_native(oldStateFd, data.mBackupWriter, newStateFd, files, keys);
    if (err != 0) {
        // TODO: more here
        throw new RuntimeException("Backup failed 0x" + Integer.toHexString(err));
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 15 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class BackupHelperDispatcher method doOneBackup.

private void doOneBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState, Header header, BackupHelper helper) throws IOException {
    int err;
    FileDescriptor newStateFD = newState.getFileDescriptor();
    // allocate space for the header in the file
    int pos = allocateHeader_native(header, newStateFD);
    if (pos < 0) {
        throw new IOException("allocateHeader_native failed (error " + pos + ")");
    }
    data.setKeyPrefix(header.keyPrefix);
    // do the backup
    helper.performBackup(oldState, data, newState);
    // fill in the header (seeking back to pos).  The file pointer will be returned to
    // where it was at the end of performBackup.  Header.chunkSize will not be filled in.
    err = writeHeader_native(header, newStateFD, pos);
    if (err != 0) {
        throw new IOException("writeHeader_native failed (error " + err + ")");
    }
}
Also used : IOException(java.io.IOException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Aggregations

FileDescriptor (java.io.FileDescriptor)363 IOException (java.io.IOException)162 ParcelFileDescriptor (android.os.ParcelFileDescriptor)95 ErrnoException (android.system.ErrnoException)95 FileInputStream (java.io.FileInputStream)83 File (java.io.File)51 AssetFileDescriptor (android.content.res.AssetFileDescriptor)37 FileOutputStream (java.io.FileOutputStream)35 Bitmap (android.graphics.Bitmap)30 LocalSocket (android.net.LocalSocket)26 FileNotFoundException (java.io.FileNotFoundException)26 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 InputStream (java.io.InputStream)16 Socket (java.net.Socket)14 ErrnoException (libcore.io.ErrnoException)14 Test (org.junit.Test)14 ServerSocket (java.net.ServerSocket)13 SSLHandshakeCallbacks (org.conscrypt.NativeCrypto.SSLHandshakeCallbacks)13 RemoteException (android.os.RemoteException)12 BitmapFactory (android.graphics.BitmapFactory)11