blob: b441449e4f5b256c26db43dab8b24fddde3dd055 [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(
Chong Zhang8e062632020-03-31 10:56:37 -070083 const std::shared_ptr<ITranscodingClientListener>& in_listener,
84 const std::string& in_clientName,
85 const std::string& in_opPackageName,
86 int32_t in_clientUid, int32_t in_clientPid,
87 std::shared_ptr<ITranscodingClient>* _aidl_return) {
88 if (in_listener == nullptr) {
89 ALOGE("Client listener can not be null");
90 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -080091 return Status::fromServiceSpecificError(ERROR_ILLEGAL_ARGUMENT);
92 }
93
94 int32_t callingPid = AIBinder_getCallingPid();
95 int32_t callingUid = AIBinder_getCallingUid();
96
Chong Zhang8e062632020-03-31 10:56:37 -070097 // Check if we can trust clientUid. Only privilege caller could forward the
98 // uid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -080099 if (in_clientUid == USE_CALLING_UID) {
100 in_clientUid = callingUid;
101 } else if (!isTrustedCallingUid(callingUid)) {
102 ALOGE("MediaTranscodingService::registerClient failed (calling PID %d, calling UID %d) "
103 "rejected "
104 "(don't trust clientUid %d)",
105 in_clientPid, in_clientUid, in_clientUid);
106 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
107 "Untrusted caller (calling PID %d, UID %d) trying to "
108 "register client",
109 in_clientPid, in_clientUid);
110 }
111
Chong Zhang8e062632020-03-31 10:56:37 -0700112 // Check if we can trust clientPid. Only privilege caller could forward the
113 // pid on app client's behalf.
hkuang9c04b8d2020-01-22 10:03:21 -0800114 if (in_clientPid == USE_CALLING_PID) {
115 in_clientPid = callingPid;
116 } else if (!isTrustedCallingUid(callingUid)) {
117 ALOGE("MediaTranscodingService::registerClient client failed (calling PID %d, calling UID "
118 "%d) rejected "
119 "(don't trust clientPid %d)",
120 in_clientPid, in_clientUid, in_clientPid);
121 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
122 "Untrusted caller (calling PID %d, UID %d) trying to "
123 "register client",
124 in_clientPid, in_clientUid);
125 }
126
hkuang9c04b8d2020-01-22 10:03:21 -0800127 // Creates the client and uses its process id as client id.
Chong Zhang8e062632020-03-31 10:56:37 -0700128 std::shared_ptr<ITranscodingClient> newClient;
129
130 status_t err = mTranscodingClientManager.addClient(in_listener,
131 in_clientPid, in_clientUid, in_clientName, in_opPackageName, &newClient);
hkuang9c04b8d2020-01-22 10:03:21 -0800132 if (err != OK) {
Chong Zhang8e062632020-03-31 10:56:37 -0700133 *_aidl_return = nullptr;
hkuang9c04b8d2020-01-22 10:03:21 -0800134 return STATUS_ERROR_FMT(err, "Failed to add client to TranscodingClientManager");
135 }
136
Chong Zhang8e062632020-03-31 10:56:37 -0700137 *_aidl_return = newClient;
hkuang9c04b8d2020-01-22 10:03:21 -0800138 return Status::ok();
139}
140
141Status MediaTranscodingService::getNumOfClients(int32_t* _aidl_return) {
142 ALOGD("MediaTranscodingService::getNumOfClients");
hkuang5172cab2020-01-31 12:40:28 -0800143 *_aidl_return = mTranscodingClientManager.getNumOfClients();
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800144 return Status::ok();
145}
146
Hangyu Kuang71b9fb42019-11-27 10:33:32 -0800147} // namespace android