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