Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MediaTranscodingService" |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 19 | #include "MediaTranscodingService.h" |
| 20 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 23 | #include <media/TranscodingClientManager.h> |
| 24 | #include <media/TranscodingJobScheduler.h> |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 25 | #include <private/android_filesystem_config.h> |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <utils/Vector.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 31 | // Convenience methods for constructing binder::Status objects for error returns |
| 32 | #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ |
| 33 | Status::fromServiceSpecificErrorWithMessage( \ |
| 34 | errorCode, \ |
| 35 | String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__)) |
| 36 | |
| 37 | // Can MediaTranscoding service trust the caller based on the calling UID? |
| 38 | // TODO(hkuang): Add MediaProvider's UID. |
| 39 | static bool isTrustedCallingUid(uid_t uid) { |
| 40 | switch (uid) { |
| 41 | case AID_ROOT: // root user |
| 42 | case AID_SYSTEM: |
| 43 | case AID_SHELL: |
| 44 | case AID_MEDIA: // mediaserver |
| 45 | return true; |
| 46 | default: |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 51 | // DummyTranscoder and DummyProcessInfo are currently used to instantiate |
| 52 | // MediaTranscodingService on service side for testing, so that we could |
| 53 | // actually test the IPC calls of MediaTranscodingService to expose some |
| 54 | // issues that's observable only over IPC. |
| 55 | class DummyTranscoder : public TranscoderInterface { |
| 56 | void start(int64_t clientId, int32_t jobId) override { |
| 57 | (void)clientId; |
| 58 | (void)jobId; |
| 59 | } |
| 60 | void pause(int64_t clientId, int32_t jobId) override { |
| 61 | (void)clientId; |
| 62 | (void)jobId; |
| 63 | } |
| 64 | void resume(int64_t clientId, int32_t jobId) override { |
| 65 | (void)clientId; |
| 66 | (void)jobId; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | class DummyProcessInfo : public ProcessInfoInterface { |
| 71 | bool isProcessOnTop(int32_t pid) override { |
| 72 | (void)pid; |
| 73 | return true; |
| 74 | } |
| 75 | }; |
| 76 | |
hkuang | 5172cab | 2020-01-31 12:40:28 -0800 | [diff] [blame] | 77 | MediaTranscodingService::MediaTranscodingService() |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 78 | : MediaTranscodingService(std::make_shared<DummyTranscoder>(), |
| 79 | std::make_shared<DummyProcessInfo>()) {} |
| 80 | |
| 81 | MediaTranscodingService::MediaTranscodingService( |
| 82 | const std::shared_ptr<TranscoderInterface>& transcoder, |
| 83 | const std::shared_ptr<ProcessInfoInterface>& procInfo) |
| 84 | : mJobScheduler(new TranscodingJobScheduler(transcoder, procInfo)), |
| 85 | mClientManager(new TranscodingClientManager(mJobScheduler)) { |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 86 | ALOGV("MediaTranscodingService is created"); |
| 87 | } |
| 88 | |
| 89 | MediaTranscodingService::~MediaTranscodingService() { |
| 90 | ALOGE("Should not be in ~MediaTranscodingService"); |
| 91 | } |
| 92 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 93 | binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) { |
| 94 | String8 result; |
| 95 | const size_t SIZE = 256; |
| 96 | char buffer[SIZE]; |
| 97 | |
| 98 | snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this); |
| 99 | result.append(buffer); |
| 100 | write(fd, result.string(), result.size()); |
| 101 | |
| 102 | Vector<String16> args; |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 103 | mClientManager->dumpAllClients(fd, args); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 104 | return OK; |
| 105 | } |
| 106 | |
| 107 | //static |
| 108 | void MediaTranscodingService::instantiate() { |
| 109 | std::shared_ptr<MediaTranscodingService> service = |
| 110 | ::ndk::SharedRefBase::make<MediaTranscodingService>(); |
| 111 | binder_status_t status = |
| 112 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
| 113 | if (status != STATUS_OK) { |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | Status MediaTranscodingService::registerClient( |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 119 | const std::shared_ptr<ITranscodingClientCallback>& in_callback, |
| 120 | const std::string& in_clientName, const std::string& in_opPackageName, int32_t in_clientUid, |
| 121 | int32_t in_clientPid, std::shared_ptr<ITranscodingClient>* _aidl_return) { |
| 122 | if (in_callback == nullptr) { |
| 123 | ALOGE("Client callback can not be null"); |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 124 | *_aidl_return = nullptr; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 125 | return Status::fromServiceSpecificError(ERROR_ILLEGAL_ARGUMENT); |
| 126 | } |
| 127 | |
| 128 | int32_t callingPid = AIBinder_getCallingPid(); |
| 129 | int32_t callingUid = AIBinder_getCallingUid(); |
| 130 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 131 | // Check if we can trust clientUid. Only privilege caller could forward the |
| 132 | // uid on app client's behalf. |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 133 | if (in_clientUid == USE_CALLING_UID) { |
| 134 | in_clientUid = callingUid; |
| 135 | } else if (!isTrustedCallingUid(callingUid)) { |
| 136 | ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) " |
| 137 | "rejected " |
| 138 | "(don't trust clientUid %d)", |
| 139 | in_clientPid, in_clientUid, in_clientUid); |
| 140 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 141 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 142 | "register client", |
| 143 | in_clientPid, in_clientUid); |
| 144 | } |
| 145 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 146 | // Check if we can trust clientPid. Only privilege caller could forward the |
| 147 | // pid on app client's behalf. |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 148 | if (in_clientPid == USE_CALLING_PID) { |
| 149 | in_clientPid = callingPid; |
| 150 | } else if (!isTrustedCallingUid(callingUid)) { |
| 151 | ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID " |
| 152 | "%d) rejected " |
| 153 | "(don't trust clientPid %d)", |
| 154 | in_clientPid, in_clientUid, in_clientPid); |
| 155 | return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED, |
| 156 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 157 | "register client", |
| 158 | in_clientPid, in_clientUid); |
| 159 | } |
| 160 | |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 161 | // Creates the client and uses its process id as client id. |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 162 | std::shared_ptr<ITranscodingClient> newClient; |
| 163 | |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 164 | status_t err = mClientManager->addClient(in_callback, in_clientPid, in_clientUid, in_clientName, |
| 165 | in_opPackageName, &newClient); |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 166 | if (err != OK) { |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 167 | *_aidl_return = nullptr; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 168 | return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager"); |
| 169 | } |
| 170 | |
Chong Zhang | 8e06263 | 2020-03-31 10:56:37 -0700 | [diff] [blame] | 171 | *_aidl_return = newClient; |
hkuang | 9c04b8d | 2020-01-22 10:03:21 -0800 | [diff] [blame] | 172 | return Status::ok(); |
| 173 | } |
| 174 | |
| 175 | Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) { |
| 176 | ALOGD("MediaTranscodingService::getNumOfClients"); |
Chong Zhang | 182b06a | 2020-04-09 14:38:05 -0700 | [diff] [blame] | 177 | *_aidl_return = mClientManager->getNumOfClients(); |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 178 | return Status::ok(); |
| 179 | } |
| 180 | |
Hangyu Kuang | 71b9fb4 | 2019-11-27 10:33:32 -0800 | [diff] [blame] | 181 | } // namespace android |