diff options
| -rw-r--r-- | services/core/java/com/android/server/BootReceiver.java | 8 | ||||
| -rw-r--r-- | services/core/java/com/android/server/os/NativeTombstoneManager.java | 41 |
2 files changed, 24 insertions, 25 deletions
diff --git a/services/core/java/com/android/server/BootReceiver.java b/services/core/java/com/android/server/BootReceiver.java index 572e9c2a5d82..7d016c82adc5 100644 --- a/services/core/java/com/android/server/BootReceiver.java +++ b/services/core/java/com/android/server/BootReceiver.java @@ -362,10 +362,8 @@ public class BootReceiver extends BroadcastReceiver { PosixFilePermissions.fromString("rw-rw----")); // Write the new proto container proto with headers. - ParcelFileDescriptor pfd; - try { - pfd = ParcelFileDescriptor.open(tombstoneProtoWithHeaders, MODE_READ_WRITE); - + try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open( + tombstoneProtoWithHeaders, MODE_READ_WRITE)) { ProtoOutputStream protoStream = new ProtoOutputStream( pfd.getFileDescriptor()); protoStream.write(TombstoneWithHeadersProto.TOMBSTONE, tombstoneBytes); @@ -379,6 +377,8 @@ public class BootReceiver extends BroadcastReceiver { } catch (FileNotFoundException ex) { Slog.e(TAG, "failed to open for write: " + tombstoneProtoWithHeaders, ex); throw ex; + } catch (IOException ex) { + Slog.e(TAG, "IO exception during write: " + tombstoneProtoWithHeaders, ex); } finally { // Remove the temporary file. if (tombstoneProtoWithHeaders != null) { diff --git a/services/core/java/com/android/server/os/NativeTombstoneManager.java b/services/core/java/com/android/server/os/NativeTombstoneManager.java index e5616d04554d..394c4aea8196 100644 --- a/services/core/java/com/android/server/os/NativeTombstoneManager.java +++ b/services/core/java/com/android/server/os/NativeTombstoneManager.java @@ -153,32 +153,31 @@ public final class NativeTombstoneManager { return Optional.empty(); } - ParcelFileDescriptor pfd; - try { - pfd = ParcelFileDescriptor.open(path, MODE_READ_WRITE); - } catch (FileNotFoundException ex) { - Slog.w(TAG, "failed to open " + path, ex); - return Optional.empty(); - } + try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(path, MODE_READ_WRITE)) { + final Optional<TombstoneFile> parsedTombstone = TombstoneFile.parse(pfd); + if (!parsedTombstone.isPresent()) { + return Optional.empty(); + } - final Optional<TombstoneFile> parsedTombstone = TombstoneFile.parse(pfd); - if (!parsedTombstone.isPresent()) { - IoUtils.closeQuietly(pfd); - return Optional.empty(); - } + if (addToList) { + synchronized (mLock) { + TombstoneFile previous = mTombstones.get(number); + if (previous != null) { + previous.dispose(); + } - if (addToList) { - synchronized (mLock) { - TombstoneFile previous = mTombstones.get(number); - if (previous != null) { - previous.dispose(); + mTombstones.put(number, parsedTombstone.get()); } - - mTombstones.put(number, parsedTombstone.get()); } - } - return parsedTombstone; + return parsedTombstone; + } catch (FileNotFoundException ex) { + Slog.w(TAG, "failed to open " + path, ex); + return Optional.empty(); + } catch (IOException ex) { + Slog.e(TAG, "IO exception during write to " + path, ex); + return Optional.empty(); + } } /** |