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