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
27namespace android {
28
29void MediaLogService::registerWriter(const sp<IMemory>& shared, size_t size, const char *name)
30{
31    if (IPCThreadState::self()->getCallingUid() != AID_MEDIA || shared == 0 ||
32            size < kMinSize || size > kMaxSize || name == NULL ||
33            shared->size() < NBLog::Timeline::sharedSize(size)) {
34        return;
35    }
36    sp<NBLog::Reader> reader(new NBLog::Reader(size, shared));
37    NamedReader namedReader(reader, name);
38    Mutex::Autolock _l(mLock);
39    mNamedReaders.add(namedReader);
40}
41
42void MediaLogService::unregisterWriter(const sp<IMemory>& shared)
43{
44    if (IPCThreadState::self()->getCallingUid() != AID_MEDIA || shared == 0) {
45        return;
46    }
47    Mutex::Autolock _l(mLock);
48    for (size_t i = 0; i < mNamedReaders.size(); ) {
49        if (mNamedReaders[i].reader()->isIMemory(shared)) {
50            mNamedReaders.removeAt(i);
51        } else {
52            i++;
53        }
54    }
55}
56
57status_t MediaLogService::dump(int fd, const Vector<String16>& args)
58{
59    // FIXME merge with similar but not identical code at services/audioflinger/ServiceUtilities.cpp
60    static const String16 sDump("android.permission.DUMP");
61    if (!(IPCThreadState::self()->getCallingUid() == AID_MEDIA ||
62            PermissionCache::checkCallingPermission(sDump))) {
63        fdprintf(fd, "Permission Denial: can't dump media.log from pid=%d, uid=%d\n",
64                IPCThreadState::self()->getCallingPid(),
65                IPCThreadState::self()->getCallingUid());
66        return NO_ERROR;
67    }
68
69    Vector<NamedReader> namedReaders;
70    {
71        Mutex::Autolock _l(mLock);
72        namedReaders = mNamedReaders;
73    }
74    for (size_t i = 0; i < namedReaders.size(); i++) {
75        const NamedReader& namedReader = namedReaders[i];
76        if (fd >= 0) {
77            fdprintf(fd, "\n%s:\n", namedReader.name());
78        } else {
79            ALOGI("%s:", namedReader.name());
80        }
81        namedReader.reader()->dump(fd, 0 /*indent*/);
82    }
83    return NO_ERROR;
84}
85
86status_t MediaLogService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
87        uint32_t flags)
88{
89    return BnMediaLogService::onTransact(code, data, reply, flags);
90}
91
92}   // namespace android
93