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

Commit b4469f9e authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Add ADB command to wait for system server crash

"adb shell svc system-server wait-for-crash" will block until the process dies.

Bug: 124022170
Test: Manual
- Run "adb shell svc system-server wait-for-crash" on multiple terminals
- Make sure open FDs aren't increasing by checking /proc/`pid system_server`/fd
- adb shell killall system_server
- All the "adb shell svc system-server wait-for-crash" should finish.

Change-Id: I203d001cd296d506ebf9f5a70b97e05e5769961a
parent 32981ff4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -98,5 +98,6 @@ public class Svc {
            new UsbCommand(),
            new NfcCommand(),
            new BluetoothCommand(),
            new SystemServerCommand(),
    };
}
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.commands.svc;

import android.app.ActivityManager;
import android.os.ParcelFileDescriptor;

import java.io.FileInputStream;

public class SystemServerCommand extends Svc.Command {
    public SystemServerCommand() {
        super("system-server");
    }

    @Override
    public String shortHelp() {
        return "System server process related command";
    }

    @Override
    public String longHelp() {
        return shortHelp() + "\n"
                + "\n"
                + "usage: system-server wait-for-crash\n"
                + "         Wait until the system server process crashes.\n\n";
    }

    private void waitForCrash() throws Exception {
        ParcelFileDescriptor fd = ActivityManager.getService().getLifeMonitor();
        if (fd == null) {
            System.err.println("Unable to get life monitor.");
            return;
        }
        System.out.println("Waiting for the system server process to die...");
        new FileInputStream(fd.getFileDescriptor()).read();
    }

    @Override
    public void run(String[] args) {
        try {
            if (args.length > 1) {
                switch (args[1]) {
                    case "wait-for-crash":
                        waitForCrash();
                        return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.err.println(longHelp());
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -486,4 +486,7 @@ interface IActivityManager {
     * started from the shell.
     */
    void stopDelegateShellPermissionIdentity();

    /** Returns a file descriptor that'll be closed when the system server process dies. */
    ParcelFileDescriptor getLifeMonitor();
}
+22 −0
Original line number Diff line number Diff line
@@ -1484,6 +1484,8 @@ public class ActivityManagerService extends IActivityManager.Stub
    private static String sTheRealBuildSerial = Build.UNKNOWN;
    private ParcelFileDescriptor[] mLifeMonitorFds;
    final class UiHandler extends Handler {
        public UiHandler() {
            super(com.android.server.UiThread.get().getLooper(), null, true);
@@ -18462,4 +18464,24 @@ public class ActivityManagerService extends IActivityManager.Stub
    private boolean isOnOffloadQueue(int flags) {
        return (mEnableOffloadQueue && ((flags & Intent.FLAG_RECEIVER_OFFLOAD) != 0));
    }
    @Override
    public ParcelFileDescriptor getLifeMonitor() {
        if (!isCallerShell()) {
            throw new SecurityException("Only shell can call it");
        }
        synchronized (this) {
            try {
                if (mLifeMonitorFds == null) {
                    mLifeMonitorFds = ParcelFileDescriptor.createPipe();
                }
                // The returned FD will be closed, but we want to keep our reader open,
                // so return a dup instead.
                return mLifeMonitorFds[0].dup();
            } catch (IOException e) {
                Slog.w(TAG, "Unable to create pipe", e);
                return null;
            }
        }
    }
}