Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.os; |
| 18 | |
| 19 | import android.annotation.RequiresPermission; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 20 | import android.app.AppOpsManager; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 21 | import android.content.Context; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 22 | import android.os.Binder; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 23 | import android.os.BugreportParams; |
| 24 | import android.os.IDumpstate; |
| 25 | import android.os.IDumpstateListener; |
| 26 | import android.os.IDumpstateToken; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
| 29 | import android.os.SystemClock; |
| 30 | import android.os.SystemProperties; |
| 31 | import android.util.Slog; |
| 32 | |
| 33 | import java.io.FileDescriptor; |
| 34 | |
| 35 | // TODO(b/111441001): |
| 36 | // 1. Handle the case where another bugreport is in progress |
| 37 | // 2. Make everything threadsafe |
| 38 | // 3. Pass validation & other errors on listener |
| 39 | |
| 40 | /** |
| 41 | * Implementation of the service that provides a privileged API to capture and consume bugreports. |
| 42 | * |
| 43 | * <p>Delegates the actualy generation to a native implementation of {@code Dumpstate}. |
| 44 | */ |
| 45 | class BugreportManagerServiceImpl extends IDumpstate.Stub { |
| 46 | private static final String TAG = "BugreportManagerService"; |
| 47 | private static final long DEFAULT_BUGREPORT_SERVICE_TIMEOUT_MILLIS = 30 * 1000; |
| 48 | |
| 49 | private IDumpstate mDs = null; |
| 50 | private final Context mContext; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 51 | private final AppOpsManager mAppOps; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 52 | |
| 53 | BugreportManagerServiceImpl(Context context) { |
| 54 | mContext = context; |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 55 | mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | @RequiresPermission(android.Manifest.permission.DUMP) |
| 60 | public IDumpstateToken setListener(String name, IDumpstateListener listener, |
| 61 | boolean getSectionDetails) throws RemoteException { |
| 62 | // TODO(b/111441001): Figure out if lazy setting of listener should be allowed |
| 63 | // and if so how to handle it. |
| 64 | throw new UnsupportedOperationException("setListener is not allowed on this service"); |
| 65 | } |
| 66 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 67 | @Override |
| 68 | @RequiresPermission(android.Manifest.permission.DUMP) |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 69 | public void startBugreport(int callingUidUnused, String callingPackage, |
| 70 | FileDescriptor bugreportFd, FileDescriptor screenshotFd, |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 71 | int bugreportMode, IDumpstateListener listener) throws RemoteException { |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 72 | int callingUid = Binder.getCallingUid(); |
| 73 | // TODO(b/111441001): validate all arguments & ensure primary user |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 74 | validate(bugreportMode); |
| 75 | |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 76 | mAppOps.checkPackage(callingUid, callingPackage); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 77 | mDs = getDumpstateService(); |
| 78 | if (mDs == null) { |
| 79 | Slog.w(TAG, "Unable to get bugreport service"); |
| 80 | // TODO(b/111441001): pass error on listener |
| 81 | return; |
| 82 | } |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame^] | 83 | mDs.startBugreport(callingUid, callingPackage, |
| 84 | bugreportFd, screenshotFd, bugreportMode, listener); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private boolean validate(@BugreportParams.BugreportMode int mode) { |
| 88 | if (mode != BugreportParams.BUGREPORT_MODE_FULL |
| 89 | && mode != BugreportParams.BUGREPORT_MODE_INTERACTIVE |
| 90 | && mode != BugreportParams.BUGREPORT_MODE_REMOTE |
| 91 | && mode != BugreportParams.BUGREPORT_MODE_WEAR |
| 92 | && mode != BugreportParams.BUGREPORT_MODE_TELEPHONY |
| 93 | && mode != BugreportParams.BUGREPORT_MODE_WIFI) { |
| 94 | Slog.w(TAG, "Unknown bugreport mode: " + mode); |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Start and get a handle to the native implementation of {@code IDumpstate} which does the |
| 102 | * actual bugreport generation. |
| 103 | * |
| 104 | * <p>Generating bugreports requires root privileges. To limit the footprint |
| 105 | * of the root access, the actual generation in Dumpstate binary is accessed as a |
| 106 | * oneshot service 'bugreport'. |
| 107 | */ |
| 108 | private IDumpstate getDumpstateService() { |
| 109 | // Start bugreport service. |
| 110 | SystemProperties.set("ctl.start", "bugreport"); |
| 111 | |
| 112 | IDumpstate ds = null; |
| 113 | boolean timedOut = false; |
| 114 | int totalTimeWaitedMillis = 0; |
| 115 | int seedWaitTimeMillis = 500; |
| 116 | while (!timedOut) { |
| 117 | // Note that the binder service on the native side is "dumpstate". |
| 118 | ds = IDumpstate.Stub.asInterface(ServiceManager.getService("dumpstate")); |
| 119 | if (ds != null) { |
| 120 | Slog.i(TAG, "Got bugreport service handle."); |
| 121 | break; |
| 122 | } |
| 123 | SystemClock.sleep(seedWaitTimeMillis); |
| 124 | Slog.i(TAG, |
| 125 | "Waiting to get dumpstate service handle (" + totalTimeWaitedMillis + "ms)"); |
| 126 | totalTimeWaitedMillis += seedWaitTimeMillis; |
| 127 | seedWaitTimeMillis *= 2; |
| 128 | timedOut = totalTimeWaitedMillis > DEFAULT_BUGREPORT_SERVICE_TIMEOUT_MILLIS; |
| 129 | } |
| 130 | if (timedOut) { |
| 131 | Slog.w(TAG, |
| 132 | "Timed out waiting to get dumpstate service handle (" |
| 133 | + totalTimeWaitedMillis + "ms)"); |
| 134 | } |
| 135 | return ds; |
| 136 | } |
| 137 | } |