1 /*
2 * Copyright (C) 2022 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_TAG "DeathPipe"
18
19 #include "DeathPipe.h"
20
21 namespace android::frameworks::cameraservice::utils {
22
DeathPipe(IBinder * parent,const::ndk::SpAIBinder & binder)23 DeathPipe::DeathPipe(IBinder* parent, const ::ndk::SpAIBinder& binder):
24 mParent(parent), mAIBinder(binder) {
25 mDeathRecipient = ::ndk::ScopedAIBinder_DeathRecipient(
26 AIBinder_DeathRecipient_new(DeathPipe::onDeathCallback));
27 // Set an unlinked callback that allows Obituaries to be deallocated
28 AIBinder_DeathRecipient_setOnUnlinked(mDeathRecipient.get(),
29 DeathPipe::onUnlinkedCallback);
30 }
31
linkToDeath(const sp<IBinder::DeathRecipient> & recipient,void * cookie,uint32_t flags)32 status_t DeathPipe::linkToDeath(const sp<IBinder::DeathRecipient>& recipient,
33 void* cookie, uint32_t flags) {
34 LOG_ALWAYS_FATAL_IF(recipient == nullptr, "%s: recipient must be non-nullptr", __FUNCTION__);
35 std::lock_guard<std::mutex> _l(mLock);
36
37 // Create and immortalize an obituary before linking it to death.
38 // The created Obituary can now only be garbage collected if it is unlinked from death
39 std::shared_ptr<Obituary> obituary = std::make_shared<Obituary>(recipient, cookie,
40 flags, /* who= */ mParent);
41 obituary->immortalize();
42
43 // Ensure that "cookie" is a pointer to an immortal obituary.
44 // AIBinder_linkToDeath calls DeathPipe::onUnlinkedCallback if linking to death fails, marking
45 // it for garbage collection
46 binder_status_t ret = AIBinder_linkToDeath(mAIBinder.get(),
47 mDeathRecipient.get(),
48 /* cookie= */ obituary.get());
49 if (ret != STATUS_OK) {
50 return DEAD_OBJECT;
51 }
52 mObituaries.emplace_back(obituary);
53 return NO_ERROR;
54 }
55
unlinkToDeath(const wp<IBinder::DeathRecipient> & recipient,void * cookie,uint32_t flags,wp<IBinder::DeathRecipient> * outRecipient)56 status_t DeathPipe::unlinkToDeath(const wp<IBinder::DeathRecipient>& recipient,
57 void* cookie, uint32_t flags,
58 wp<IBinder::DeathRecipient>* outRecipient) {
59 std::lock_guard<std::mutex> _l(mLock);
60 // Temporary Obituary for checking equality
61 std::shared_ptr<Obituary> inObituary = std::make_shared<Obituary>(recipient, cookie,
62 flags, mParent);
63 for (auto it = mObituaries.begin(); it != mObituaries.end(); it++) {
64 if ((*inObituary) == (**it)) {
65 if (outRecipient != nullptr) {
66 *outRecipient = (*it)->recipient;
67 }
68 // Unlink the found Obituary from death. AIBinder_unlinkToDeath calls
69 // DeathPipe::onUnlinkedCallback with the given cookie when unlinking is done
70 binder_status_t ret = AIBinder_unlinkToDeath(mAIBinder.get(),
71 mDeathRecipient.get(),
72 /* cookie= */ (*it).get());
73 mObituaries.erase(it);
74 return ret == STATUS_OK ? NO_ERROR : DEAD_OBJECT;
75 }
76 }
77 return NAME_NOT_FOUND;
78 }
79
80 DeathPipe::~DeathPipe() = default;
81
82
onDeathCallback(void * cookie)83 void DeathPipe::onDeathCallback(void* cookie) {
84 // Cookie will always be a pointer to a valid immortal Obituary
85 Obituary* obituary = static_cast<Obituary*>(cookie);
86 obituary->onDeath();
87 // Don't call Obituary::clear() because VNDK Binder will call DeathPipe::onUnlinkedCallback()
88 // when it is ready
89 }
90
onUnlinkedCallback(void * cookie)91 void DeathPipe::onUnlinkedCallback(void* cookie) {
92 // Cookie will always be a pointer to a valid immortal Obituary.
93 Obituary* obituary = static_cast<Obituary*>(cookie);
94 // Mark obituary to be garbage collected if needed. onDeathCallback won't be called with
95 // this particular cookie after this.
96 obituary->clear();
97 }
98
99 } // namespace android::frameworks::cameraservice::utils