blob: 82d4161a34256a6fd34247ff6984eb677e89c80f [file] [log] [blame]
Hangyu Kuang71b9fb42019-11-27 10:33:32 -08001/*
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"
hkuang9c04b8d2020-01-22 10:03:21 -080019#include <MediaTranscodingService.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080020#include <android/binder_manager.h>
21#include <android/binder_process.h>
hkuang9c04b8d2020-01-22 10:03:21 -080022#include <private/android_filesystem_config.h>
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080023#include <utils/Log.h>
24#include <utils/Vector.h>
25
26namespace android {
27
hkuang9c04b8d2020-01-22 10:03:21 -080028// Convenience methods for constructing binder::Status objects for error returns
29#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
30 Status::fromServiceSpecificErrorWithMessage( \
31 errorCode, \
32 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, ##__VA_ARGS__))
33
34// Can MediaTranscoding service trust the caller based on the calling UID?
35// TODO(hkuang): Add MediaProvider's UID.
36static bool isTrustedCallingUid(uid_t uid) {
37 switch (uid) {
38 case AID_ROOT: // root user
39 case AID_SYSTEM:
40 case AID_SHELL:
41 case AID_MEDIA: // mediaserver
42 return true;
43 default:
44 return false;
45 }
46}
47
hkuang5172cab2020-01-31 12:40:28 -080048MediaTranscodingService::MediaTranscodingService()
49 : mTranscodingClientManager(TranscodingClientManager::getInstance()) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080050 ALOGV("MediaTranscodingService is created");
51}
52
53MediaTranscodingService::~MediaTranscodingService() {
54 ALOGE("Should not be in ~MediaTranscodingService");
55}
56
hkuang9c04b8d2020-01-22 10:03:21 -080057binder_status_t MediaTranscodingService::dump(int fd, const char** /*args*/, uint32_t /*numArgs*/) {
58 String8 result;
59 const size_t SIZE = 256;
60 char buffer[SIZE];
61
62 snprintf(buffer, SIZE, "MediaTranscodingService: %p\n", this);
63 result.append(buffer);
64 write(fd, result.string(), result.size());
65
66 Vector<String16> args;
hkuang5172cab2020-01-31 12:40:28 -080067 mTranscodingClientManager.dumpAllClients(fd, args);
Hangyu Kuang71b9fb42019-11-27 10:33:32 -080068 return OK;
69}
70
71//static
72void MediaTranscodingService::instantiate() {
73 std::shared_ptr<MediaTranscodingService> service =
74 ::ndk::SharedRefBase::make<MediaTranscodingService>();
75 binder_status_t status =
76 AServiceManager_addService(service->asBinder().get(), getServiceName());
77 if (status != STATUS_OK) {
78 return;
79 }
80}
81
82Status MediaTranscodingService::registerClient(
hkuang9c04b8d2020-01-22 10:03:21 -080083 const std::shared_ptr<ITranscodingServiceClient>& in_client,
84 const std::string& in_opPackageName, int32_t in_clientUid, int32_t in_clientPid,
85 int32_t* _aidl_return) {
86 if (in_client == nullptr) {
87 ALOGE("Client can not be null");
88 *_aidl_return = kInvalidJobId;
89 return Status::fromServiceSpecificError(ERROR_ILLEGAL_ARGUMENT);
90 }
91
92 int32_t callingPid = AIBinder_getCallingPid();
93 int32_t callingUid = AIBinder_getCallingUid();
94
95 // Check if we can trust clientUid. Only privilege caller could forward the uid on app client's behalf.
96 if (in_clientUid == USE_CALLING_UID) {
97 in_clientUid = callingUid;
98 } else if (!isTrustedCallingUid(callingUid)) {
99 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
100 "rejected "
101 "(don't trust clientUid %d)",
102 in_clientPid, in_clientUid, in_clientUid);
103 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
104 "Untrusted caller (calling PID %d, UID %d) trying to "
105 "register client",
106 in_clientPid, in_clientUid);
107 }
108
109 // Check if we can trust clientPid. Only privilege caller could forward the pid on app client's behalf.
110 if (in_clientPid == USE_CALLING_PID) {
111 in_clientPid = callingPid;
112 } else if (!isTrustedCallingUid(callingUid)) {
113 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
114 "%d) rejected "
115 "(don't trust clientPid %d)",
116 in_clientPid, in_clientUid, in_clientPid);
117 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
118 "Untrusted caller (calling PID %d, UID %d) trying to "
119 "register client",
120 in_clientPid, in_clientUid);
121 }
122
123 // We know the clientId must be equal to its pid as we assigned client's pid as its clientId.
124 int32_t clientId = in_clientPid;
125
126 // Checks if the client already registers.
hkuang5172cab2020-01-31 12:40:28 -0800127 if (mTranscodingClientManager.isClientIdRegistered(clientId)) {
hkuang9c04b8d2020-01-22 10:03:21 -0800128 return Status::fromServiceSpecificError(ERROR_ALREADY_EXISTS);
129 }
130
131 // Creates the client and uses its process id as client id.
132 std::unique_ptr<TranscodingClientManager::ClientInfo> newClient =
133 std::make_unique<TranscodingClientManager::ClientInfo>(
134 in_client, clientId, in_clientPid, in_clientUid, in_opPackageName);
hkuang5172cab2020-01-31 12:40:28 -0800135 status_t err = mTranscodingClientManager.addClient(std::move(newClient));
hkuang9c04b8d2020-01-22 10:03:21 -0800136 if (err != OK) {
137 *_aidl_return = kInvalidClientId;
138 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
139 }
140
141 ALOGD("Assign client: %s pid: %d, uid: %d with id: %d", in_opPackageName.c_str(), in_clientPid,
142 in_clientUid, clientId);
143
144 *_aidl_return = clientId;
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800145 return Status::ok();
146}
147
hkuang9c04b8d2020-01-22 10:03:21 -0800148Status MediaTranscodingService::unregisterClient(int32_t clientId, bool* _aidl_return) {
149 ALOGD("unregisterClient id: %d", clientId);
150 int32_t callingUid = AIBinder_getCallingUid();
151 int32_t callingPid = AIBinder_getCallingPid();
152
153 // Only the client with clientId or the trusted caller could unregister the client.
154 if (callingPid != clientId) {
155 if (!isTrustedCallingUid(callingUid)) {
156 ALOGE("Untrusted caller (calling PID %d, UID %d) trying to "
157 "unregister client with id: %d",
158 callingUid, callingPid, clientId);
159 *_aidl_return = true;
160 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
161 "Untrusted caller (calling PID %d, UID %d) trying to "
162 "unregister client with id: %d",
163 callingUid, callingPid, clientId);
164 }
165 }
166
hkuang5172cab2020-01-31 12:40:28 -0800167 *_aidl_return = (mTranscodingClientManager.removeClient(clientId) == OK);
hkuang9c04b8d2020-01-22 10:03:21 -0800168 return Status::ok();
169}
170
171Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
172 ALOGD("MediaTranscodingService::getNumOfClients");
hkuang5172cab2020-01-31 12:40:28 -0800173 *_aidl_return = mTranscodingClientManager.getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800174 return Status::ok();
175}
176
177Status MediaTranscodingService::submitRequest(int32_t /*clientId*/,
hkuang48c365e2020-01-13 16:33:42 -0800178 const TranscodingRequestParcel& /*request*/,
179 TranscodingJobParcel* /*job*/,
180 int32_t* /*_aidl_return*/) {
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800181 // TODO(hkuang): Add implementation.
182 return Status::ok();
183}
184
185Status MediaTranscodingService::cancelJob(int32_t /*in_clientId*/, int32_t /*in_jobId*/,
186 bool* /*_aidl_return*/) {
187 // TODO(hkuang): Add implementation.
188 return Status::ok();
189}
190
hkuang48c365e2020-01-13 16:33:42 -0800191Status MediaTranscodingService::getJobWithId(int32_t /*in_jobId*/,
192 TranscodingJobParcel* /*out_job*/,
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800193 bool* /*_aidl_return*/) {
194 // TODO(hkuang): Add implementation.
195 return Status::ok();
196}
197
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800198} // namespace android