blob: 1178cc18f828b46e17e573db7ff051a74ffd94b7 [file] [log] [blame]
Nandana Dutt3386fb72018-12-12 17:26:57 +00001/*
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
17package com.android.server.os;
18
19import android.annotation.RequiresPermission;
Nandana Dutt161a4462019-01-16 18:18:38 +000020import android.app.AppOpsManager;
Nandana Dutt3386fb72018-12-12 17:26:57 +000021import android.content.Context;
Nandana Dutt161a4462019-01-16 18:18:38 +000022import android.os.Binder;
Nandana Dutt3386fb72018-12-12 17:26:57 +000023import android.os.BugreportParams;
24import android.os.IDumpstate;
25import android.os.IDumpstateListener;
26import android.os.IDumpstateToken;
27import android.os.RemoteException;
28import android.os.ServiceManager;
29import android.os.SystemClock;
30import android.os.SystemProperties;
31import android.util.Slog;
32
33import 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 */
45class 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 Dutt161a4462019-01-16 18:18:38 +000051 private final AppOpsManager mAppOps;
Nandana Dutt3386fb72018-12-12 17:26:57 +000052
53 BugreportManagerServiceImpl(Context context) {
54 mContext = context;
Nandana Dutt161a4462019-01-16 18:18:38 +000055 mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Nandana Dutt3386fb72018-12-12 17:26:57 +000056 }
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 Dutt3386fb72018-12-12 17:26:57 +000067 @Override
68 @RequiresPermission(android.Manifest.permission.DUMP)
Nandana Dutt161a4462019-01-16 18:18:38 +000069 public void startBugreport(int callingUidUnused, String callingPackage,
70 FileDescriptor bugreportFd, FileDescriptor screenshotFd,
Nandana Dutt3386fb72018-12-12 17:26:57 +000071 int bugreportMode, IDumpstateListener listener) throws RemoteException {
Nandana Dutt161a4462019-01-16 18:18:38 +000072 int callingUid = Binder.getCallingUid();
73 // TODO(b/111441001): validate all arguments & ensure primary user
Nandana Dutt3386fb72018-12-12 17:26:57 +000074 validate(bugreportMode);
75
Nandana Dutt161a4462019-01-16 18:18:38 +000076 mAppOps.checkPackage(callingUid, callingPackage);
Nandana Dutt3386fb72018-12-12 17:26:57 +000077 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 Dutt161a4462019-01-16 18:18:38 +000083 mDs.startBugreport(callingUid, callingPackage,
84 bugreportFd, screenshotFd, bugreportMode, listener);
Nandana Dutt3386fb72018-12-12 17:26:57 +000085 }
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}