Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc3df9f3 authored by Abhijeet Kaur's avatar Abhijeet Kaur
Browse files

Use dev/null instead of tmp file

Use dev/null fd (instead of using fd from temporary file) when
screenshot fd is passed as null, as file deletion does not need
to be handled for dev/null.

Test: Take interactive bugreport (which does not need screenshot)
manually using shell (which calls bugreport API)
Bug: 128981582
Merged-In: I1719899e6cf3bffe1a96849691c051ff4f3a05ec
Change-Id: I1719899e6cf3bffe1a96849691c051ff4f3a05ec
parent 7ba06189
Loading
Loading
Loading
Loading
+6 −29
Original line number Original line Diff line number Diff line
@@ -33,7 +33,7 @@ import com.android.internal.util.Preconditions;
import libcore.io.IoUtils;
import libcore.io.IoUtils;


import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;
@@ -148,7 +148,6 @@ public final class BugreportManager {
            @NonNull BugreportParams params,
            @NonNull BugreportParams params,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull BugreportCallback callback) {
            @NonNull BugreportCallback callback) {
        File tmpScreenshotFile = null;
        try {
        try {
            Preconditions.checkNotNull(bugreportFd);
            Preconditions.checkNotNull(bugreportFd);
            Preconditions.checkNotNull(params);
            Preconditions.checkNotNull(params);
@@ -157,13 +156,10 @@ public final class BugreportManager {


            if (screenshotFd == null) {
            if (screenshotFd == null) {
                // Binder needs a valid File Descriptor to be passed
                // Binder needs a valid File Descriptor to be passed
                tmpScreenshotFile = File.createTempFile("tmp", ".png");
                screenshotFd = ParcelFileDescriptor.open(new File("/dev/null"),
                screenshotFd = ParcelFileDescriptor.open(tmpScreenshotFile,
                        ParcelFileDescriptor.MODE_READ_ONLY);
                        ParcelFileDescriptor.MODE_READ_ONLY);
            }
            }
            DumpstateListener dsListener = new DumpstateListener(executor,
            DumpstateListener dsListener = new DumpstateListener(executor, callback);
                    callback, tmpScreenshotFile);

            // Note: mBinder can get callingUid from the binder transaction.
            // Note: mBinder can get callingUid from the binder transaction.
            mBinder.startBugreport(-1 /* callingUid */,
            mBinder.startBugreport(-1 /* callingUid */,
                    mContext.getOpPackageName(),
                    mContext.getOpPackageName(),
@@ -171,13 +167,9 @@ public final class BugreportManager {
                    screenshotFd.getFileDescriptor(),
                    screenshotFd.getFileDescriptor(),
                    params.getMode(), dsListener);
                    params.getMode(), dsListener);
        } catch (RemoteException e) {
        } catch (RemoteException e) {
            deleteFile(tmpScreenshotFile);
            throw e.rethrowFromSystemServer();
            throw e.rethrowFromSystemServer();
        } catch (IOException e) {
        } catch (FileNotFoundException e) {
            // Need to delete the file if it was created but failed while trying to get fd
            Log.wtf(TAG, "Not able to find /dev/null file: ", e);
            deleteFile(tmpScreenshotFile);
            Log.e(TAG, "Not able to create/open temporary screenshot file ", e);
            callback.onError(BugreportCallback.BUGREPORT_ERROR_RUNTIME);
        } finally {
        } finally {
            // We can close the file descriptors here because binder would have duped them.
            // We can close the file descriptors here because binder would have duped them.
            IoUtils.closeQuietly(bugreportFd);
            IoUtils.closeQuietly(bugreportFd);
@@ -199,26 +191,13 @@ public final class BugreportManager {
        }
        }
    }
    }


    private void deleteFile(@Nullable File tmpScreenshotFile) {
        try {
            if (tmpScreenshotFile != null && tmpScreenshotFile.exists()) {
                tmpScreenshotFile.delete();
            }
        } catch (SecurityException e) {
            Log.e(TAG, "Not able to delete temporary screenshot file ", e);
        }
    }

    private final class DumpstateListener extends IDumpstateListener.Stub {
    private final class DumpstateListener extends IDumpstateListener.Stub {
        private final Executor mExecutor;
        private final Executor mExecutor;
        private final BugreportCallback mCallback;
        private final BugreportCallback mCallback;
        private final File mTmpScreenshotFile;


        DumpstateListener(Executor executor, BugreportCallback callback,
        DumpstateListener(Executor executor, BugreportCallback callback) {
                @Nullable File tmpScreenshotFile) {
            mExecutor = executor;
            mExecutor = executor;
            mCallback = callback;
            mCallback = callback;
            mTmpScreenshotFile = tmpScreenshotFile;
        }
        }


        @Override
        @Override
@@ -242,7 +221,6 @@ public final class BugreportManager {
                });
                });
            } finally {
            } finally {
                Binder.restoreCallingIdentity(identity);
                Binder.restoreCallingIdentity(identity);
                deleteFile(mTmpScreenshotFile);
            }
            }
        }
        }


@@ -255,7 +233,6 @@ public final class BugreportManager {
                });
                });
            } finally {
            } finally {
                Binder.restoreCallingIdentity(identity);
                Binder.restoreCallingIdentity(identity);
                deleteFile(mTmpScreenshotFile);
            }
            }
        }
        }