blob: faa4714a8697b294143e4d67558f796831fd38c0 [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;
20import android.content.Context;
21import android.os.BugreportParams;
22import android.os.IDumpstate;
23import android.os.IDumpstateListener;
24import android.os.IDumpstateToken;
25import android.os.RemoteException;
26import android.os.ServiceManager;
27import android.os.SystemClock;
28import android.os.SystemProperties;
29import android.util.Slog;
30
31import java.io.FileDescriptor;
32
33// TODO(b/111441001):
34// 1. Handle the case where another bugreport is in progress
35// 2. Make everything threadsafe
36// 3. Pass validation & other errors on listener
37
38/**
39 * Implementation of the service that provides a privileged API to capture and consume bugreports.
40 *
41 * <p>Delegates the actualy generation to a native implementation of {@code Dumpstate}.
42 */
43class BugreportManagerServiceImpl extends IDumpstate.Stub {
44 private static final String TAG = "BugreportManagerService";
45 private static final long DEFAULT_BUGREPORT_SERVICE_TIMEOUT_MILLIS = 30 * 1000;
46
47 private IDumpstate mDs = null;
48 private final Context mContext;
49
50 BugreportManagerServiceImpl(Context context) {
51 mContext = context;
52 }
53
54 @Override
55 @RequiresPermission(android.Manifest.permission.DUMP)
56 public IDumpstateToken setListener(String name, IDumpstateListener listener,
57 boolean getSectionDetails) throws RemoteException {
58 // TODO(b/111441001): Figure out if lazy setting of listener should be allowed
59 // and if so how to handle it.
60 throw new UnsupportedOperationException("setListener is not allowed on this service");
61 }
62
63
64 @Override
65 @RequiresPermission(android.Manifest.permission.DUMP)
66 public void startBugreport(FileDescriptor bugreportFd, FileDescriptor screenshotFd,
67 int bugreportMode, IDumpstateListener listener) throws RemoteException {
68
69 validate(bugreportMode);
70
71 mDs = getDumpstateService();
72 if (mDs == null) {
73 Slog.w(TAG, "Unable to get bugreport service");
74 // TODO(b/111441001): pass error on listener
75 return;
76 }
77 mDs.startBugreport(bugreportFd, screenshotFd, bugreportMode, listener);
78 }
79
80 private boolean validate(@BugreportParams.BugreportMode int mode) {
81 if (mode != BugreportParams.BUGREPORT_MODE_FULL
82 && mode != BugreportParams.BUGREPORT_MODE_INTERACTIVE
83 && mode != BugreportParams.BUGREPORT_MODE_REMOTE
84 && mode != BugreportParams.BUGREPORT_MODE_WEAR
85 && mode != BugreportParams.BUGREPORT_MODE_TELEPHONY
86 && mode != BugreportParams.BUGREPORT_MODE_WIFI) {
87 Slog.w(TAG, "Unknown bugreport mode: " + mode);
88 return false;
89 }
90 return true;
91 }
92
93 /*
94 * Start and get a handle to the native implementation of {@code IDumpstate} which does the
95 * actual bugreport generation.
96 *
97 * <p>Generating bugreports requires root privileges. To limit the footprint
98 * of the root access, the actual generation in Dumpstate binary is accessed as a
99 * oneshot service 'bugreport'.
100 */
101 private IDumpstate getDumpstateService() {
102 // Start bugreport service.
103 SystemProperties.set("ctl.start", "bugreport");
104
105 IDumpstate ds = null;
106 boolean timedOut = false;
107 int totalTimeWaitedMillis = 0;
108 int seedWaitTimeMillis = 500;
109 while (!timedOut) {
110 // Note that the binder service on the native side is "dumpstate".
111 ds = IDumpstate.Stub.asInterface(ServiceManager.getService("dumpstate"));
112 if (ds != null) {
113 Slog.i(TAG, "Got bugreport service handle.");
114 break;
115 }
116 SystemClock.sleep(seedWaitTimeMillis);
117 Slog.i(TAG,
118 "Waiting to get dumpstate service handle (" + totalTimeWaitedMillis + "ms)");
119 totalTimeWaitedMillis += seedWaitTimeMillis;
120 seedWaitTimeMillis *= 2;
121 timedOut = totalTimeWaitedMillis > DEFAULT_BUGREPORT_SERVICE_TIMEOUT_MILLIS;
122 }
123 if (timedOut) {
124 Slog.w(TAG,
125 "Timed out waiting to get dumpstate service handle ("
126 + totalTimeWaitedMillis + "ms)");
127 }
128 return ds;
129 }
130}