blob: c830c6e1d7c21deac862afb8f7071cd43889bf07 [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
2 * Copyright (C) 2010 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
Takeshi Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshi27ed8ad2010-07-29 10:12:27 +090018#define LOG_TAG "DrmManagerService(Native)"
19#include <utils/Log.h>
20
Takeshi Aimi34738462010-11-16 13:56:11 +090021#include <private/android_filesystem_config.h>
Andy Hunge9eb03e2020-04-04 14:08:23 -070022#include <mediautils/MemoryLeakTrackUtil.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090023
aimitakeshi27ed8ad2010-07-29 10:12:27 +090024#include <errno.h>
25#include <utils/threads.h>
26#include <binder/IServiceManager.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090027#include <binder/IPCThreadState.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090028#include <sys/stat.h>
29#include "DrmManagerService.h"
30#include "DrmManager.h"
31
Riley Spahnf785f492014-07-01 15:32:31 -070032#include <selinux/android.h>
33
aimitakeshi27ed8ad2010-07-29 10:12:27 +090034using namespace android;
35
Riley Spahnf785f492014-07-01 15:32:31 -070036static int selinux_enabled;
37static char *drmserver_context;
Takeshi Aimi34738462010-11-16 13:56:11 +090038static Vector<uid_t> trustedUids;
39
Riley Spahnf785f492014-07-01 15:32:31 -070040const char *const DrmManagerService::drm_perm_labels[] = {
41 "consumeRights",
42 "setPlaybackStatus",
43 "openDecryptSession",
44 "closeDecryptSession",
45 "initializeDecryptUnit",
46 "decrypt",
47 "finalizeDecryptUnit",
48 "pread"
49};
50
51const char *DrmManagerService::get_perm_label(drm_perm_t perm) {
52 unsigned int index = perm;
53
Aurimas Liutikasc9036842016-02-18 15:58:04 -080054 if (index >= (sizeof(drm_perm_labels) / sizeof(drm_perm_labels[0]))) {
Riley Spahnf785f492014-07-01 15:32:31 -070055 ALOGE("SELinux: Failed to retrieve permission label(perm=%d).\n", perm);
56 abort();
57 }
58 return drm_perm_labels[index];
59}
60
Steven Moreland1a394d52019-01-11 15:49:24 -080061bool DrmManagerService::selinuxIsProtectedCallAllowed(pid_t spid, const char* ssid, drm_perm_t perm) {
Riley Spahnf785f492014-07-01 15:32:31 -070062 if (selinux_enabled <= 0) {
63 return true;
64 }
65
Steven Moreland1a394d52019-01-11 15:49:24 -080066 char *sctx = NULL;
Riley Spahnf785f492014-07-01 15:32:31 -070067 const char *selinux_class = "drmservice";
68 const char *str_perm = get_perm_label(perm);
69
Steven Moreland1a394d52019-01-11 15:49:24 -080070 if (ssid == NULL) {
71 android_errorWriteLog(0x534e4554, "121035042");
72
73 if (getpidcon(spid, &sctx) != 0) {
74 ALOGE("SELinux: getpidcon(pid=%d) failed.\n", spid);
75 return false;
76 }
Riley Spahnf785f492014-07-01 15:32:31 -070077 }
78
Steven Moreland1a394d52019-01-11 15:49:24 -080079 bool allowed = (selinux_check_access(ssid ? ssid : sctx, drmserver_context,
80 selinux_class, str_perm, NULL) == 0);
Riley Spahnf785f492014-07-01 15:32:31 -070081 freecon(sctx);
82
83 return allowed;
84}
85
86bool DrmManagerService::isProtectedCallAllowed(drm_perm_t perm) {
Jeff Tinker6868e982014-06-17 16:45:46 -070087 // TODO
88 // Following implementation is just for reference.
89 // Each OEM manufacturer should implement/replace with their own solutions.
90 IPCThreadState* ipcState = IPCThreadState::self();
91 uid_t uid = ipcState->getCallingUid();
Riley Spahnf785f492014-07-01 15:32:31 -070092 pid_t spid = ipcState->getCallingPid();
Steven Moreland1a394d52019-01-11 15:49:24 -080093 const char* ssid = ipcState->getCallingSid();
Jeff Tinker6868e982014-06-17 16:45:46 -070094
95 for (unsigned int i = 0; i < trustedUids.size(); ++i) {
96 if (trustedUids[i] == uid) {
Steven Moreland1a394d52019-01-11 15:49:24 -080097 return selinuxIsProtectedCallAllowed(spid, ssid, perm);
Jeff Tinker6868e982014-06-17 16:45:46 -070098 }
99 }
100 return false;
Takeshi Aimi34738462010-11-16 13:56:11 +0900101}
102
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900103void DrmManagerService::instantiate() {
Steve Block3856b092011-10-20 11:56:00 +0100104 ALOGV("instantiate");
Steven Moreland1a394d52019-01-11 15:49:24 -0800105 sp<DrmManagerService> service = new DrmManagerService();
106 service->setRequestingSid(true);
107 defaultServiceManager()->addService(String16("drm.drmManager"), service);
Takeshi Aimi34738462010-11-16 13:56:11 +0900108
109 if (0 >= trustedUids.size()) {
110 // TODO
111 // Following implementation is just for reference.
112 // Each OEM manufacturer should implement/replace with their own solutions.
113
114 // Add trusted uids here
115 trustedUids.push(AID_MEDIA);
116 }
Riley Spahnf785f492014-07-01 15:32:31 -0700117
118 selinux_enabled = is_selinux_enabled();
119 if (selinux_enabled > 0 && getcon(&drmserver_context) != 0) {
120 ALOGE("SELinux: DrmManagerService failed to get context for DrmManagerService. Aborting.\n");
121 abort();
122 }
123
124 union selinux_callback cb;
125 cb.func_log = selinux_log_callback;
126 selinux_set_callback(SELINUX_CB_LOG, cb);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900127}
128
Takeshi Aimie943f842010-10-08 23:05:49 +0900129DrmManagerService::DrmManagerService() :
130 mDrmManager(NULL) {
Steve Block3856b092011-10-20 11:56:00 +0100131 ALOGV("created");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900132 mDrmManager = new DrmManager();
Robert Shih7bcf7922020-02-07 15:01:57 -0800133 mDrmManager->initMetricsLooper();
Takeshi Aimie943f842010-10-08 23:05:49 +0900134 mDrmManager->loadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900135}
136
137DrmManagerService::~DrmManagerService() {
Steve Block3856b092011-10-20 11:56:00 +0100138 ALOGV("Destroyed");
Takeshi Aimie943f842010-10-08 23:05:49 +0900139 mDrmManager->unloadPlugIns();
Robert Shih7bcf7922020-02-07 15:01:57 -0800140 mDrmManager = NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900141}
142
Gloria Wang8f001512011-07-21 15:10:22 -0700143int DrmManagerService::addUniqueId(bool isNative) {
144 return mDrmManager->addUniqueId(isNative);
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900145}
146
147void DrmManagerService::removeUniqueId(int uniqueId) {
148 mDrmManager->removeUniqueId(uniqueId);
149}
150
Takeshi Aimie943f842010-10-08 23:05:49 +0900151void DrmManagerService::addClient(int uniqueId) {
152 mDrmManager->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900153}
154
Takeshi Aimie943f842010-10-08 23:05:49 +0900155void DrmManagerService::removeClient(int uniqueId) {
156 mDrmManager->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900157}
158
159status_t DrmManagerService::setDrmServiceListener(
160 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("Entering setDrmServiceListener");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900162 mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
163 return DRM_NO_ERROR;
164}
165
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900166DrmConstraints* DrmManagerService::getConstraints(
167 int uniqueId, const String8* path, const int action) {
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV("Entering getConstraints from content");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900169 return mDrmManager->getConstraints(uniqueId, path, action);
170}
171
Takeshi Aimi34738462010-11-16 13:56:11 +0900172DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
Steve Block3856b092011-10-20 11:56:00 +0100173 ALOGV("Entering getMetadata from content");
Takeshi Aimi34738462010-11-16 13:56:11 +0900174 return mDrmManager->getMetadata(uniqueId, path);
175}
176
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900177bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100178 ALOGV("Entering canHandle");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900179 return mDrmManager->canHandle(uniqueId, path, mimeType);
180}
181
182DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100183 ALOGV("Entering processDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900184 return mDrmManager->processDrmInfo(uniqueId, drmInfo);
185}
186
187DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Steve Block3856b092011-10-20 11:56:00 +0100188 ALOGV("Entering acquireDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900189 return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
190}
191
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900192status_t DrmManagerService::saveRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900193 int uniqueId, const DrmRights& drmRights,
194 const String8& rightsPath, const String8& contentPath) {
Steve Block3856b092011-10-20 11:56:00 +0100195 ALOGV("Entering saveRights");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900196 return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
197}
198
James Dongbf5b3b22012-07-30 17:57:39 -0700199String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Steve Block3856b092011-10-20 11:56:00 +0100200 ALOGV("Entering getOriginalMimeType");
James Dongbf5b3b22012-07-30 17:57:39 -0700201 return mDrmManager->getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900202}
203
204int DrmManagerService::getDrmObjectType(
205 int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("Entering getDrmObjectType");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900207 return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
208}
209
210int DrmManagerService::checkRightsStatus(
211 int uniqueId, const String8& path, int action) {
Steve Block3856b092011-10-20 11:56:00 +0100212 ALOGV("Entering checkRightsStatus");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900213 return mDrmManager->checkRightsStatus(uniqueId, path, action);
214}
215
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900216status_t DrmManagerService::consumeRights(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700217 int uniqueId, sp<DecryptHandle>& decryptHandle, int action, bool reserve) {
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("Entering consumeRights");
Riley Spahnf785f492014-07-01 15:32:31 -0700219 if (!isProtectedCallAllowed(CONSUME_RIGHTS)) {
James Dong328745b2012-02-28 13:55:55 -0800220 return DRM_ERROR_NO_PERMISSION;
221 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900222 return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900223}
224
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900225status_t DrmManagerService::setPlaybackStatus(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700226 int uniqueId, sp<DecryptHandle>& decryptHandle, int playbackStatus, int64_t position) {
Steve Block3856b092011-10-20 11:56:00 +0100227 ALOGV("Entering setPlaybackStatus");
Riley Spahnf785f492014-07-01 15:32:31 -0700228 if (!isProtectedCallAllowed(SET_PLAYBACK_STATUS)) {
James Dong328745b2012-02-28 13:55:55 -0800229 return DRM_ERROR_NO_PERMISSION;
230 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900231 return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900232}
233
234bool DrmManagerService::validateAction(
235 int uniqueId, const String8& path,
236 int action, const ActionDescription& description) {
Steve Block3856b092011-10-20 11:56:00 +0100237 ALOGV("Entering validateAction");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900238 return mDrmManager->validateAction(uniqueId, path, action, description);
239}
240
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900241status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
Steve Block3856b092011-10-20 11:56:00 +0100242 ALOGV("Entering removeRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900243 return mDrmManager->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900244}
245
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900246status_t DrmManagerService::removeAllRights(int uniqueId) {
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("Entering removeAllRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900248 return mDrmManager->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900249}
250
251int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("Entering openConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900253 return mDrmManager->openConvertSession(uniqueId, mimeType);
254}
255
256DrmConvertedStatus* DrmManagerService::convertData(
257 int uniqueId, int convertId, const DrmBuffer* inputData) {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("Entering convertData");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900259 return mDrmManager->convertData(uniqueId, convertId, inputData);
260}
261
262DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("Entering closeConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900264 return mDrmManager->closeConvertSession(uniqueId, convertId);
265}
266
267status_t DrmManagerService::getAllSupportInfo(
268 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("Entering getAllSupportInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900270 return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
271}
272
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700273sp<DecryptHandle> DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800274 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("Entering DrmManagerService::openDecryptSession");
Riley Spahnf785f492014-07-01 15:32:31 -0700276 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
James Dong9d2f3862012-01-10 08:24:37 -0800277 return mDrmManager->openDecryptSession(uniqueId, fd, offset, length, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900278 }
279
280 return NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900281}
282
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700283sp<DecryptHandle> DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800284 int uniqueId, const char* uri, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100285 ALOGV("Entering DrmManagerService::openDecryptSession with uri");
Riley Spahnf785f492014-07-01 15:32:31 -0700286 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
James Dong9d2f3862012-01-10 08:24:37 -0800287 return mDrmManager->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900288 }
289
290 return NULL;
Takeshi Aimie943f842010-10-08 23:05:49 +0900291}
292
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700293sp<DecryptHandle> DrmManagerService::openDecryptSession(
Kei Takahashicba7b322012-01-18 17:10:19 +0900294 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
295 ALOGV("Entering DrmManagerService::openDecryptSession for streaming");
Riley Spahnf785f492014-07-01 15:32:31 -0700296 if (isProtectedCallAllowed(OPEN_DECRYPT_SESSION)) {
Kei Takahashicba7b322012-01-18 17:10:19 +0900297 return mDrmManager->openDecryptSession(uniqueId, buf, mimeType);
298 }
299
300 return NULL;
301}
302
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700303status_t DrmManagerService::closeDecryptSession(int uniqueId, sp<DecryptHandle>& decryptHandle) {
Steve Block3856b092011-10-20 11:56:00 +0100304 ALOGV("Entering closeDecryptSession");
Riley Spahnf785f492014-07-01 15:32:31 -0700305 if (!isProtectedCallAllowed(CLOSE_DECRYPT_SESSION)) {
James Dong328745b2012-02-28 13:55:55 -0800306 return DRM_ERROR_NO_PERMISSION;
307 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900308 return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900309}
310
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700311status_t DrmManagerService::initializeDecryptUnit(int uniqueId, sp<DecryptHandle>& decryptHandle,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900312 int decryptUnitId, const DrmBuffer* headerInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("Entering initializeDecryptUnit");
Riley Spahnf785f492014-07-01 15:32:31 -0700314 if (!isProtectedCallAllowed(INITIALIZE_DECRYPT_UNIT)) {
James Dong328745b2012-02-28 13:55:55 -0800315 return DRM_ERROR_NO_PERMISSION;
316 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900317 return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900318}
319
320status_t DrmManagerService::decrypt(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700321 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId,
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900322 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
Steve Block3856b092011-10-20 11:56:00 +0100323 ALOGV("Entering decrypt");
Riley Spahnf785f492014-07-01 15:32:31 -0700324 if (!isProtectedCallAllowed(DECRYPT)) {
James Dong328745b2012-02-28 13:55:55 -0800325 return DRM_ERROR_NO_PERMISSION;
326 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900327 return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900328}
329
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900330status_t DrmManagerService::finalizeDecryptUnit(
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700331 int uniqueId, sp<DecryptHandle>& decryptHandle, int decryptUnitId) {
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("Entering finalizeDecryptUnit");
Riley Spahnf785f492014-07-01 15:32:31 -0700333 if (!isProtectedCallAllowed(FINALIZE_DECRYPT_UNIT)) {
James Dong328745b2012-02-28 13:55:55 -0800334 return DRM_ERROR_NO_PERMISSION;
335 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900336 return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900337}
338
Jeff Tinker5d49bef2018-10-03 23:01:09 -0700339ssize_t DrmManagerService::pread(int uniqueId, sp<DecryptHandle>& decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800340 void* buffer, ssize_t numBytes, off64_t offset) {
Steve Block3856b092011-10-20 11:56:00 +0100341 ALOGV("Entering pread");
Riley Spahnf785f492014-07-01 15:32:31 -0700342 if (!isProtectedCallAllowed(PREAD)) {
James Dong328745b2012-02-28 13:55:55 -0800343 return DRM_ERROR_NO_PERMISSION;
344 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900345 return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
346}
347
Andy Hung07b745e2016-05-23 16:21:07 -0700348status_t DrmManagerService::dump(int fd, const Vector<String16>& args)
James Dong8635b7b2011-03-14 17:01:38 -0700349{
350 const size_t SIZE = 256;
351 char buffer[SIZE];
352 String8 result;
353 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
354 snprintf(buffer, SIZE, "Permission Denial: "
355 "can't dump DrmManagerService from pid=%d, uid=%d\n",
356 IPCThreadState::self()->getCallingPid(),
357 IPCThreadState::self()->getCallingUid());
358 result.append(buffer);
359 } else {
360#if DRM_MEMORY_LEAK_TRACK
361 bool dumpMem = false;
362 for (size_t i = 0; i < args.size(); i++) {
363 if (args[i] == String16("-m")) {
364 dumpMem = true;
365 }
366 }
367 if (dumpMem) {
Andy Hung07b745e2016-05-23 16:21:07 -0700368 result.append("\nDumping memory:\n");
369 std::string s = dumpMemoryAddresses(100 /* limit */);
370 result.append(s.c_str(), s.size());
James Dong8635b7b2011-03-14 17:01:38 -0700371 }
Andy Hung07b745e2016-05-23 16:21:07 -0700372#else
373 (void)args;
James Dong8635b7b2011-03-14 17:01:38 -0700374#endif
375 }
376 write(fd, result.string(), result.size());
377 return NO_ERROR;
378}
379