1 /*
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>
22 #include <binder/PermissionCache.h>
23 #include <media/nbaio/NBLog.h>
24 #include <private/android_filesystem_config.h>
25 #include "MediaLogService.h"
26 
27 namespace android {
28 
29 // static const char kDeadlockedString[] = "MediaLogService may be deadlocked\n";
MediaLogService()30 MediaLogService::MediaLogService() :
31     BnMediaLogService(),
32     mMergerShared((NBLog::Shared*) malloc(NBLog::Timeline::sharedSize(kMergeBufferSize))),
33     mMerger(mMergerShared, kMergeBufferSize),
34     mMergeReader(mMergerShared, kMergeBufferSize, mMerger),
35     mMergeThread(new NBLog::MergeThread(mMerger))
36 {
37     mMergeThread->run("MergeThread");
38 }
39 
~MediaLogService()40 MediaLogService::~MediaLogService()
41 {
42     mMergeThread->requestExit();
43     mMergeThread->setTimeoutUs(0);
44     mMergeThread->join();
45     free(mMergerShared);
46 }
47 
registerWriter(const sp<IMemory> & shared,size_t size,const char * name)48 void MediaLogService::registerWriter(const sp<IMemory>& shared, size_t size, const char *name)
49 {
50     if (IPCThreadState::self()->getCallingUid() != AID_AUDIOSERVER || shared == 0 ||
51             size < kMinSize || size > kMaxSize || name == NULL ||
52             shared->size() < NBLog::Timeline::sharedSize(size)) {
53         return;
54     }
55     sp<NBLog::Reader> reader(new NBLog::Reader(shared, size));
56     NBLog::NamedReader namedReader(reader, name);
57     Mutex::Autolock _l(mLock);
58     mNamedReaders.add(namedReader);
59     mMerger.addReader(namedReader);
60 }
61 
unregisterWriter(const sp<IMemory> & shared)62 void MediaLogService::unregisterWriter(const sp<IMemory>& shared)
63 {
64     if (IPCThreadState::self()->getCallingUid() != AID_AUDIOSERVER || shared == 0) {
65         return;
66     }
67     Mutex::Autolock _l(mLock);
68     for (size_t i = 0; i < mNamedReaders.size(); ) {
69         if (mNamedReaders[i].reader()->isIMemory(shared)) {
70             mNamedReaders.removeAt(i);
71         } else {
72             i++;
73         }
74     }
75 }
76 
dumpTryLock(Mutex & mutex)77 bool MediaLogService::dumpTryLock(Mutex& mutex)
78 {
79     bool locked = false;
80     for (int i = 0; i < kDumpLockRetries; ++i) {
81         if (mutex.tryLock() == NO_ERROR) {
82             locked = true;
83             break;
84         }
85         usleep(kDumpLockSleepUs);
86     }
87     return locked;
88 }
89 
dump(int fd,const Vector<String16> & args __unused)90 status_t MediaLogService::dump(int fd, const Vector<String16>& args __unused)
91 {
92     // FIXME merge with similar but not identical code at services/audioflinger/ServiceUtilities.cpp
93     static const String16 sDump("android.permission.DUMP");
94     if (!(IPCThreadState::self()->getCallingUid() == AID_AUDIOSERVER ||
95             PermissionCache::checkCallingPermission(sDump))) {
96         dprintf(fd, "Permission Denial: can't dump media.log from pid=%d, uid=%d\n",
97                 IPCThreadState::self()->getCallingPid(),
98                 IPCThreadState::self()->getCallingUid());
99         return NO_ERROR;
100     }
101 
102 #if 0
103     Vector<NBLog::NamedReader> namedReaders;
104     {
105         bool locked = dumpTryLock(mLock);
106 
107         // failed to lock - MediaLogService is probably deadlocked
108         if (!locked) {
109             String8 result(kDeadlockedString);
110             if (fd >= 0) {
111                 write(fd, result.string(), result.size());
112             } else {
113                 ALOGW("%s:", result.string());
114             }
115             return NO_ERROR;
116         }
117             // namedReaders = mNamedReaders;
118             // for (size_t i = 0; i < namedReaders.size(); i++) {
119             //     const NBLog::NamedReader& namedReader = namedReaders[i];
120             //     if (fd >= 0) {
121             //         dprintf(fd, "\n%s:\n", namedReader.name());
122             //     } else {
123             //         ALOGI("%s:", namedReader.name());
124             //     }
125             //     namedReader.reader()->dump(fd, 0 /*indent*/);
126             // }
127 
128         mLock.unlock();
129     }
130 #endif
131     // FIXME request merge to make sure log is up to date
132     mMergeReader.dump(fd);
133     return NO_ERROR;
134 }
135 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)136 status_t MediaLogService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
137         uint32_t flags)
138 {
139     return BnMediaLogService::onTransact(code, data, reply, flags);
140 }
141 
requestMergeWakeup()142 void MediaLogService::requestMergeWakeup() {
143     mMergeThread->wakeup();
144 }
145 
146 }   // namespace android
147