blob: 4c81436e69c852cc83e2ace9ebcf66272e469120 [file] [log] [blame]
Glenn Kasten6f1c1912013-01-18 15:31:41 -08001/*
2 * Copyright (C) 2013 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#define LOG_TAG "MediaLog"
18//#define LOG_NDEBUG 0
19
20#include <sys/mman.h>
21#include <utils/Log.h>
Glenn Kastenbc512072013-03-26 15:09:04 -070022#include <binder/PermissionCache.h>
Glenn Kasten6f1c1912013-01-18 15:31:41 -080023#include <media/nbaio/NBLog.h>
24#include <private/android_filesystem_config.h>
25#include "MediaLogService.h"
26
27namespace android {
28
Nicolas Roulet40a44982017-02-03 13:39:57 -080029// static const char kDeadlockedString[] = "MediaLogService may be deadlocked\n";
Eric Laurent8349a912016-03-14 18:45:12 -070030
Glenn Kasten6f1c1912013-01-18 15:31:41 -080031void MediaLogService::registerWriter(const sp<IMemory>& shared, size_t size, const char *name)
32{
Glenn Kastenae0cff12016-02-24 13:57:49 -080033 if (IPCThreadState::self()->getCallingUid() != AID_AUDIOSERVER || shared == 0 ||
Glenn Kasten6f1c1912013-01-18 15:31:41 -080034 size < kMinSize || size > kMaxSize || name == NULL ||
35 shared->size() < NBLog::Timeline::sharedSize(size)) {
36 return;
37 }
Glenn Kasten535e1612016-12-05 12:19:36 -080038 sp<NBLog::Reader> reader(new NBLog::Reader(shared, size));
Nicolas Roulet40a44982017-02-03 13:39:57 -080039 NBLog::NamedReader namedReader(reader, name);
Glenn Kasten6f1c1912013-01-18 15:31:41 -080040 Mutex::Autolock _l(mLock);
41 mNamedReaders.add(namedReader);
Nicolas Roulet40a44982017-02-03 13:39:57 -080042 mMerger.addReader(namedReader);
Glenn Kasten6f1c1912013-01-18 15:31:41 -080043}
44
45void MediaLogService::unregisterWriter(const sp<IMemory>& shared)
46{
Glenn Kastenae0cff12016-02-24 13:57:49 -080047 if (IPCThreadState::self()->getCallingUid() != AID_AUDIOSERVER || shared == 0) {
Glenn Kasten6f1c1912013-01-18 15:31:41 -080048 return;
49 }
50 Mutex::Autolock _l(mLock);
51 for (size_t i = 0; i < mNamedReaders.size(); ) {
52 if (mNamedReaders[i].reader()->isIMemory(shared)) {
53 mNamedReaders.removeAt(i);
54 } else {
55 i++;
56 }
57 }
58}
59
Eric Laurent8349a912016-03-14 18:45:12 -070060bool MediaLogService::dumpTryLock(Mutex& mutex)
61{
62 bool locked = false;
63 for (int i = 0; i < kDumpLockRetries; ++i) {
64 if (mutex.tryLock() == NO_ERROR) {
65 locked = true;
66 break;
67 }
68 usleep(kDumpLockSleepUs);
69 }
70 return locked;
71}
72
Glenn Kasten0f11b512014-01-31 16:18:54 -080073status_t MediaLogService::dump(int fd, const Vector<String16>& args __unused)
Glenn Kasten6f1c1912013-01-18 15:31:41 -080074{
Glenn Kastenbc512072013-03-26 15:09:04 -070075 // FIXME merge with similar but not identical code at services/audioflinger/ServiceUtilities.cpp
76 static const String16 sDump("android.permission.DUMP");
Glenn Kastenae0cff12016-02-24 13:57:49 -080077 if (!(IPCThreadState::self()->getCallingUid() == AID_AUDIOSERVER ||
Glenn Kastenbc512072013-03-26 15:09:04 -070078 PermissionCache::checkCallingPermission(sDump))) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -070079 dprintf(fd, "Permission Denial: can't dump media.log from pid=%d, uid=%d\n",
Glenn Kasten5ea77ae2013-04-18 15:26:47 -070080 IPCThreadState::self()->getCallingPid(),
81 IPCThreadState::self()->getCallingUid());
Glenn Kastenbc512072013-03-26 15:09:04 -070082 return NO_ERROR;
83 }
84
Nicolas Roulet40a44982017-02-03 13:39:57 -080085#if 0
86 Vector<NBLog::NamedReader> namedReaders;
Glenn Kasten6f1c1912013-01-18 15:31:41 -080087 {
Eric Laurent8349a912016-03-14 18:45:12 -070088 bool locked = dumpTryLock(mLock);
89
90 // failed to lock - MediaLogService is probably deadlocked
91 if (!locked) {
92 String8 result(kDeadlockedString);
93 if (fd >= 0) {
94 write(fd, result.string(), result.size());
95 } else {
96 ALOGW("%s:", result.string());
97 }
98 return NO_ERROR;
99 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800100 // namedReaders = mNamedReaders;
101 // for (size_t i = 0; i < namedReaders.size(); i++) {
102 // const NBLog::NamedReader& namedReader = namedReaders[i];
103 // if (fd >= 0) {
104 // dprintf(fd, "\n%s:\n", namedReader.name());
105 // } else {
106 // ALOGI("%s:", namedReader.name());
107 // }
108 // namedReader.reader()->dump(fd, 0 /*indent*/);
109 // }
110
Eric Laurent8349a912016-03-14 18:45:12 -0700111 mLock.unlock();
Glenn Kasten6f1c1912013-01-18 15:31:41 -0800112 }
Nicolas Roulet40a44982017-02-03 13:39:57 -0800113#endif
Eric Laurent8349a912016-03-14 18:45:12 -0700114
Nicolas Roulet40a44982017-02-03 13:39:57 -0800115 mMerger.merge();
116 mMergeReader.dump(fd);
Glenn Kasten6f1c1912013-01-18 15:31:41 -0800117 return NO_ERROR;
118}
119
120status_t MediaLogService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
121 uint32_t flags)
122{
123 return BnMediaLogService::onTransact(code, data, reply, flags);
124}
125
126} // namespace android