IPCThreadState.cpp revision 9c634fd2d13722106d475b000b05babcce1f65be
1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2005 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define LOG_TAG "IPCThreadState"
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
19cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <binder/IPCThreadState.h>
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <binder/Binder.h>
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <binder/BpBinder.h>
23fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri#include <binder/TextOutput.h>
24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <cutils/sched_policy.h>
26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <utils/Log.h>
27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <utils/threads.h>
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
29e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav#include <private/binder/binder_module.h>
30a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye#include <private/binder/Static.h>
31a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye
322290993eddf5262a8df7fc9478daed52401e325aDianne Hackborn#include <sys/ioctl.h>
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <signal.h>
34bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes#include <errno.h>
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <stdio.h>
36be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn#include <unistd.h>
37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#ifdef HAVE_PTHREADS
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <pthread.h>
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <sched.h>
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <sys/resource.h>
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#endif
43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#ifdef HAVE_WIN32_THREADS
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#include <windows.h>
45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#endif
46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#if LOG_NDEBUG
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define IF_LOG_TRANSACTIONS() if (false)
51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define IF_LOG_COMMANDS() if (false)
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define LOG_REMOTEREFS(...)
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define IF_LOG_REMOTEREFS() if (false)
54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define LOG_THREADPOOL(...)
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define LOG_ONEWAY(...)
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#else
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact")
60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc")
61e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn#define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__)
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs")
63340a183c3293d98842e4a2671b500e8e80258818Todd Kennedy#define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__)
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__)
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#endif
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
68c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell// ---------------------------------------------------------------------------
69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornnamespace android {
71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic const char* getReturnString(size_t idx);
73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic const void* printReturnCommand(TextOutput& out, const void* _cmd);
74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic const void* printCommand(TextOutput& out, const void* _cmd);
75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn// Static const and functions will be optimized out if not used,
77e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn// when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out.
78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic const char *kReturnStrings[] = {
79340a183c3293d98842e4a2671b500e8e80258818Todd Kennedy    "BR_ERROR",
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_OK",
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_TRANSACTION",
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_REPLY",
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_ACQUIRE_RESULT",
84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_DEAD_REPLY",
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_TRANSACTION_COMPLETE",
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_INCREFS",
87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_ACQUIRE",
88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_RELEASE",
89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_DECREFS",
90e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    "BR_ATTEMPT_ACQUIRE",
91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_NOOP",
92340a183c3293d98842e4a2671b500e8e80258818Todd Kennedy    "BR_SPAWN_LOOPER",
93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_FINISHED",
94cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    "BR_DEAD_BINDER",
95d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    "BR_CLEAR_DEATH_NOTIFICATION_DONE",
96c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BR_FAILED_REPLY"
97c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell};
98c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell
99c077b4cd990a9f220a27ad5eca04828e17136064Adam Powellstatic const char *kCommandStrings[] = {
100c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_TRANSACTION",
101c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_REPLY",
102c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_ACQUIRE_RESULT",
103d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    "BC_FREE_BUFFER",
104c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_INCREFS",
105d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    "BC_ACQUIRE",
106c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_RELEASE",
107c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_DECREFS",
108c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_INCREFS_DONE",
109c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_ACQUIRE_DONE",
110c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_ATTEMPT_ACQUIRE",
111c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_REGISTER_LOOPER",
112c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_ENTER_LOOPER",
113c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_EXIT_LOOPER",
114c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_REQUEST_DEATH_NOTIFICATION",
115c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_CLEAR_DEATH_NOTIFICATION",
116c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    "BC_DEAD_BINDER_DONE"
117c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell};
118c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell
119c077b4cd990a9f220a27ad5eca04828e17136064Adam Powellstatic const char* getReturnString(size_t idx)
120c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell{
121c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0]))
122c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        return kReturnStrings[idx];
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    else
124c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        return "unknown";
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
127d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedystatic const void* printBinderTransactionData(TextOutput& out, const void* data)
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    const binder_transaction_data* btd =
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        (const binder_transaction_data*)data;
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (btd->target.handle < 1024) {
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /* want to print descriptors in decimal; guess based on value */
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        out << "target.desc=" << btd->target.handle;
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    } else {
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        out << "target.ptr=" << btd->target.ptr;
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    out << " (cookie " << btd->cookie << ")" << endl
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl
139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size
140e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        << " bytes)" << endl
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size
142340a183c3293d98842e4a2671b500e8e80258818Todd Kennedy        << " bytes)";
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return btd+1;
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic const void* printReturnCommand(TextOutput& out, const void* _cmd)
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]);
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    const int32_t* cmd = (const int32_t*)_cmd;
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    uint32_t code = (uint32_t)*cmd++;
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    size_t cmdIndex = code & 0xff;
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (code == BR_ERROR) {
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl;
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return cmd;
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    } else if (cmdIndex >= N) {
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        out << "Unknown reply: " << code << endl;
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return cmd;
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    out << kReturnStrings[cmdIndex];
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    switch (code) {
1629dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main        case BR_TRANSACTION:
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BR_REPLY: {
1649dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main            out << ": " << indent;
1659dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main            cmd = (const int32_t *)printBinderTransactionData(out, cmd);
1669dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main            out << dedent;
1679dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main        } break;
1689dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main
1699dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main        case BR_ACQUIRE_RESULT: {
1709dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main            const int32_t res = *cmd++;
1719dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main            out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1742290993eddf5262a8df7fc9478daed52401e325aDianne Hackborn        case BR_INCREFS:
1752290993eddf5262a8df7fc9478daed52401e325aDianne Hackborn        case BR_ACQUIRE:
1769f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount        case BR_RELEASE:
1779f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount        case BR_DECREFS: {
1789f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            const int32_t b = *cmd++;
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t c = *cmd++;
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
182e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn
183e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        case BR_ATTEMPT_ACQUIRE: {
184e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            const int32_t p = *cmd++;
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t b = *cmd++;
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t c = *cmd++;
187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c
188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                << "), pri=" << p;
189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BR_DEAD_BINDER:
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BR_CLEAR_DEATH_NOTIFICATION_DONE: {
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t c = *cmd++;
194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": death cookie " << (void*)(long)c;
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        default:
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // no details to show for: BR_OK, BR_DEAD_REPLY,
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // BR_TRANSACTION_COMPLETE, BR_FINISHED
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            break;
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    out << endl;
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return cmd;
205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic const void* printCommand(TextOutput& out, const void* _cmd)
208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]);
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    const int32_t* cmd = (const int32_t*)_cmd;
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    uint32_t code = (uint32_t)*cmd++;
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    size_t cmdIndex = code & 0xff;
2132c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn
2142c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn    if (cmdIndex >= N) {
2152c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn        out << "Unknown command: " << code << endl;
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return cmd;
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    out << kCommandStrings[cmdIndex];
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    switch (code) {
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_TRANSACTION:
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_REPLY: {
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": " << indent;
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            cmd = (const int32_t *)printBinderTransactionData(out, cmd);
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << dedent;
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_ACQUIRE_RESULT: {
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t res = *cmd++;
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)");
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
233d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        case BC_FREE_BUFFER: {
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t buf = *cmd++;
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": buffer=" << (void*)(long)buf;
236d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        } break;
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_INCREFS:
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_ACQUIRE:
240abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        case BC_RELEASE:
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_DECREFS: {
242d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy            const int32_t d = *cmd++;
2438491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy            out << ": desc=" << d;
2440adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        } break;
2450adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
2460adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        case BC_INCREFS_DONE:
2470adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        case BC_ACQUIRE_DONE: {
248c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            const int32_t b = *cmd++;
249c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            const int32_t c = *cmd++;
250c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")";
251c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        } break;
2520adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
2530adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        case BC_ATTEMPT_ACQUIRE: {
2540adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            const int32_t p = *cmd++;
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t d = *cmd++;
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": desc=" << d << ", pri=" << p;
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_REQUEST_DEATH_NOTIFICATION:
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_CLEAR_DEATH_NOTIFICATION: {
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t h = *cmd++;
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t c = *cmd++;
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")";
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        case BC_DEAD_BINDER_DONE: {
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const int32_t c = *cmd++;
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            out << ": death cookie " << (void*)(long)c;
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        default:
272e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER,
273e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            // BC_EXIT_LOOPER
274e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            break;
275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    out << endl;
2782bdd09a243b422960168170e2344274cc9cfd788Todd Kennedy    return cmd;
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER;
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic bool gHaveTLS = false;
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatic pthread_key_t gTLS = 0;
2842a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackbornstatic bool gShutdown = false;
2852a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackbornstatic bool gDisableBackgroundScheduling = false;
2862a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
2872a4d8518f36346ea25a22a736453ff28f2954165Dianne HackbornIPCThreadState* IPCThreadState::self()
288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (gHaveTLS) {
290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornrestart:
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        const pthread_key_t k = gTLS;
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (st) return st;
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return new IPCThreadState;
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (gShutdown) return NULL;
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    pthread_mutex_lock(&gTLSMutex);
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (!gHaveTLS) {
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (pthread_key_create(&gTLS, threadDestructor) != 0) {
302abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            pthread_mutex_unlock(&gTLSMutex);
303abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            return NULL;
304abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        }
305abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        gHaveTLS = true;
30679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell    }
30779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell    pthread_mutex_unlock(&gTLSMutex);
30879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell    goto restart;
309abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell}
310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne HackbornIPCThreadState* IPCThreadState::selfOrNull()
312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
3139f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount    if (gHaveTLS) {
3149f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount        const pthread_key_t k = gTLS;
3159f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount        IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);
3169f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount        return st;
3179f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount    }
3189f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount    return NULL;
3199f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount}
3209f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount
3219f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mountvoid IPCThreadState::shutdown()
3229f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount{
3239306f07b1d66f4974a81f781d29739b30124fff0George Mount    gShutdown = true;
3249306f07b1d66f4974a81f781d29739b30124fff0George Mount
3259f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount    if (gHaveTLS) {
326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // XXX Need to wait for all thread pool threads to exit!
3275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS);
3285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (st) {
3295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            delete st;
3305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            pthread_setspecific(gTLS, NULL);
3315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
3325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        gHaveTLS = false;
3335c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
3345c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
3355c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
3365c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornvoid IPCThreadState::disableBackgroundScheduling(bool disable)
3375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn{
3385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    gDisableBackgroundScheduling = disable;
3395c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
3405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
3415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornsp<ProcessState> IPCThreadState::process()
3425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn{
3435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    return mProcess;
3445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
3455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
3465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornstatus_t IPCThreadState::clearLastError()
3475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn{
3485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    const status_t err = mLastError;
3495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    mLastError = NO_ERROR;
3505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    return err;
3515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
3525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
3535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpid_t IPCThreadState::getCallingPid() const
3545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn{
3555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    return mCallingPid;
3565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
357d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
3585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornuid_t IPCThreadState::getCallingUid() const
3595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn{
3605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    return mCallingUid;
3615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
362d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
3635c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornint64_t IPCThreadState::clearCallingIdentity()
3645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn{
3655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid;
3665c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    clearCaller();
3675c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    return token;
3685c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
3695c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::setStrictModePolicy(int32_t policy)
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mStrictModePolicy = policy;
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornint32_t IPCThreadState::getStrictModePolicy() const
376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return mStrictModePolicy;
378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::setLastTransactionBinderFlags(int32_t flags)
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mLastTransactionBinderFlags = flags;
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornint32_t IPCThreadState::getLastTransactionBinderFlags() const
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return mLastTransactionBinderFlags;
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::restoreCallingIdentity(int64_t token)
391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mCallingUid = (int)(token>>32);
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mCallingPid = (int)token;
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::clearCaller()
397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mCallingPid = getpid();
399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mCallingUid = getuid();
400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::flushCommands()
403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (mProcess->mDriverFD <= 0)
405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return;
406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    talkWithDriver(false);
407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatus_t IPCThreadState::getAndExecuteCommand()
410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    status_t result;
412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int32_t cmd;
413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    result = talkWithDriver();
415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (result >= NO_ERROR) {
416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        size_t IN = mIn.dataAvail();
417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (IN < sizeof(int32_t)) return result;
418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        cmd = mIn.readInt32();
419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        IF_LOG_COMMANDS() {
420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            alog << "Processing top-level Command: "
421c861fd8b28b0b878ccc194ee6099b3b1bc4c3ae8Scott Kennedy                 << getReturnString(cmd) << endl;
422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        result = executeCommand(cmd);
425cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // After executing the command, ensure that the thread is returned to the
427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // foreground cgroup before rejoining the pool.  The driver takes care of
428cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // restoring the priority, but doesn't do anything with cgroups so we
429cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // need to take care of that here in userspace.  Note that we do make
430cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // sure to go in the foreground after executing a transaction, but
431cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // there are other callbacks into user code that could have changed
432cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // our group so we want to make absolutely sure it is put back.
433cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        set_sched_policy(mMyThreadId, SP_FOREGROUND);
434cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
435cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return result;
437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
438cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
439cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn// When we've cleared the incoming command queue, process any pending derefs
440cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::processPendingDerefs()
441cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
442cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (mIn.dataPosition() >= mIn.dataSize()) {
443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        size_t numPending = mPendingWeakDerefs.size();
444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (numPending > 0) {
445cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (size_t i = 0; i < numPending; i++) {
446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                RefBase::weakref_type* refs = mPendingWeakDerefs[i];
447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                refs->decWeak(mProcess.get());
448cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
4493fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell            mPendingWeakDerefs.clear();
4503fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell        }
4513fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell
4523fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell        numPending = mPendingStrongDerefs.size();
4533fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell        if (numPending > 0) {
4543fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell            for (size_t i = 0; i < numPending; i++) {
4553fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell                BBinder* obj = mPendingStrongDerefs[i];
4563fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell                obj->decStrong(mProcess.get());
4573fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell            }
4583fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell            mPendingStrongDerefs.clear();
4593fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell        }
4603fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell    }
4613fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell}
4623fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell
4633fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powellvoid IPCThreadState::joinThreadPool(bool isMain)
4643fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell{
4653fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell    LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
4663fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell
4673fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell    mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER);
4683fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell
4693fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell    // This thread may have been spawned by a thread that was in the background
4703fafb0817f980d1819d0708b3c3da00454ba4c1fAdam Powell    // scheduling group, so first we will make sure it is in the foreground
471cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // one to avoid performing an initial transaction in the background.
4720adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    set_sched_policy(mMyThreadId, SP_FOREGROUND);
473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
474cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    status_t result;
475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    do {
476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        processPendingDerefs();
4770adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        // now get the next command to be processed, waiting if necessary
4780adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        result = getAndExecuteCommand();
4790adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
4800adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
4810adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
4820adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                  mProcess->mDriverFD, result);
483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            abort();
4840adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        }
4850adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Let this thread exit the thread pool if it is no longer
4870adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        // needed and it is not the main process thread.
4880adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if(result == TIMED_OUT && !isMain) {
4890adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            break;
4900adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        }
4910adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    } while (result != -ECONNREFUSED && result != -EBADF);
492e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn
4930adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%p\n",
494e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        (void*)pthread_self(), getpid(), (void*)result);
495e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn
496e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    mOut.writeInt32(BC_EXIT_LOOPER);
497e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    talkWithDriver(false);
498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
499cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
500cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornint IPCThreadState::setupPolling(int* fd)
501cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (mProcess->mDriverFD <= 0) {
503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return -EBADF;
504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(BC_ENTER_LOOPER);
507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    *fd = mProcess->mDriverFD;
508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return 0;
509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatus_t IPCThreadState::handlePolledCommands()
512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    status_t result;
514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    do {
516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        result = getAndExecuteCommand();
517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    } while (mIn.dataPosition() < mIn.dataSize());
518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    processPendingDerefs();
520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    flushCommands();
521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return result;
522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
523cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
524cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::stopProcess(bool /*immediate*/)
525cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
526cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    //ALOGI("**** STOPPING PROCESS");
527cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    flushCommands();
528cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int fd = mProcess->mDriverFD;
529cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mProcess->mDriverFD = -1;
530cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    close(fd);
531cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    //kill(getpid(), SIGKILL);
532cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatus_t IPCThreadState::transact(int32_t handle,
535cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                  uint32_t code, const Parcel& data,
536cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                  Parcel* reply, uint32_t flags)
537cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
538cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    status_t err = data.errorCheck();
539cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
540cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    flags |= TF_ACCEPT_FDS;
541cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
542cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    IF_LOG_TRANSACTIONS() {
543cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        TextOutput::Bundle _b(alog);
544cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand "
545cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            << handle << " / code " << TypeCode(code) << ": "
546cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            << indent << data << dedent << endl;
547cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
548cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
549cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (err == NO_ERROR) {
550cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(),
551cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY");
552cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);
553cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
554cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
555cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (err != NO_ERROR) {
556cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (reply) reply->setError(err);
557cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return (mLastError = err);
558cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
559cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
560cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if ((flags & TF_ONE_WAY) == 0) {
561cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        #if 0
562cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (code == 4) { // relayout
563cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            ALOGI(">>>>>> CALLING transaction 4");
564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            ALOGI(">>>>>> CALLING transaction %d", code);
566cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
567cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        #endif
568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (reply) {
569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            err = waitForResponse(reply);
570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
5715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            Parcel fakeReply;
5725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            err = waitForResponse(&fakeReply);
5735c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
5745c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        #if 0
5755c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (code == 4) { // relayout
5765c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            ALOGI("<<<<<< RETURNING transaction 4");
5775c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        } else {
5785c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            ALOGI("<<<<<< RETURNING transaction %d", code);
5795c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
5805c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        #endif
5815c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
5825c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        IF_LOG_TRANSACTIONS() {
5835c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            TextOutput::Bundle _b(alog);
5845c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand "
5855c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                << handle << ": ";
5865c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            if (reply) alog << indent << *reply << dedent << endl;
587cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            else alog << "(none requested)" << endl;
588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
589cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    } else {
590cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        err = waitForResponse(NULL, NULL);
591cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
592cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
593cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return err;
594cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
595cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
596cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::incStrongHandle(int32_t handle)
597cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
598cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle);
599cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(BC_ACQUIRE);
600cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(handle);
601cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
602cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
603cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::decStrongHandle(int32_t handle)
604cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
605cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle);
606cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(BC_RELEASE);
607cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(handle);
608cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
609cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
610cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::incWeakHandle(int32_t handle)
611cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
612cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle);
613cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(BC_INCREFS);
614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(handle);
615cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
616cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
617d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedyvoid IPCThreadState::decWeakHandle(int32_t handle)
618d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy{
619edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy    LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle);
620d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    mOut.writeInt32(BC_DECREFS);
621d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    mOut.writeInt32(handle);
622d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy}
623d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
624d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedystatus_t IPCThreadState::attemptIncStrongHandle(int32_t handle)
625d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy{
626d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy#if HAS_BC_ATTEMPT_ACQUIRE
627cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle);
628cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(BC_ATTEMPT_ACQUIRE);
629d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    mOut.writeInt32(0); // xxx was thread priority
630cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(handle);
631d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    status_t result = UNKNOWN_ERROR;
632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
633edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy    waitForResponse(NULL, &result);
634edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy
635edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy#if LOG_REFCOUNTS
636edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy    printf("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n",
6378491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy        handle, result == NO_ERROR ? "SUCCESS" : "FAILURE");
638edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy#endif
639edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy
640edaa101bfa72b5e5a5b14b9e6c66579eaed5e8ccTodd Kennedy    return result;
641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#else
642cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    (void)handle;
643cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ALOGE("%s(%d): Not supported\n", __func__, handle);
644d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    return INVALID_OPERATION;
645cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#endif
646cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
647d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
648cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid IPCThreadState::expungeHandle(int32_t handle, IBinder* binder)
649d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy{
650cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#if LOG_REFCOUNTS
651cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    printf("IPCThreadState::expungeHandle(%ld)\n", handle);
652cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#endif
653cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    self()->mProcess->expungeHandle(handle, binder);
654cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
655cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
656a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyestatus_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy)
657cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
658cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION);
659cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32((int32_t)handle);
660cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writePointer((uintptr_t)proxy);
661cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return NO_ERROR;
662cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
663cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
664cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatus_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy)
665cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
666a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION);
667cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writeInt32((int32_t)handle);
668cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.writePointer((uintptr_t)proxy);
669cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return NO_ERROR;
670cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
671cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
672cba2e2c881e8e16ea5025b564c94320174d65f01Dianne HackbornIPCThreadState::IPCThreadState()
673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    : mProcess(ProcessState::self()),
674cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn      mMyThreadId(gettid()),
675cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn      mStrictModePolicy(0),
676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn      mLastTransactionBinderFlags(0)
677cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
678cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    pthread_setspecific(gTLS, this);
679a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    clearCaller();
680cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mIn.setDataCapacity(256);
681cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    mOut.setDataCapacity(256);
682cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
683cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
684cba2e2c881e8e16ea5025b564c94320174d65f01Dianne HackbornIPCThreadState::~IPCThreadState()
685cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
686cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
687cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
688cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatus_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags)
6890adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn{
6900adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    status_t err;
6910adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    status_t statusBuffer;
692cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer);
693cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (err < NO_ERROR) return err;
694cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
695cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return waitForResponse(NULL, NULL);
696cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
697cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
6980adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackbornstatus_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)
6990adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn{
7000adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    uint32_t cmd;
7010adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    int32_t err;
7020adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
7030adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    while (1) {
7040adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if ((err=talkWithDriver()) < NO_ERROR) break;
7050adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        err = mIn.errorCheck();
7060adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if (err < NO_ERROR) break;
7070adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if (mIn.dataAvail() == 0) continue;
7080adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
7090adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        cmd = (uint32_t)mIn.readInt32();
7100adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
7110adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        IF_LOG_COMMANDS() {
7120adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            alog << "Processing waitForResponse Command: "
7130adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                << getReturnString(cmd) << endl;
7140adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        }
7150adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
7160adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        switch (cmd) {
7170adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        case BR_TRANSACTION_COMPLETE:
7180adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            if (!reply && !acquireResult) goto finish;
7190adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            break;
7200adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
7210adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        case BR_DEAD_REPLY:
7220adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            err = DEAD_OBJECT;
7230adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            goto finish;
7240adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
7250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        case BR_FAILED_REPLY:
726cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            err = FAILED_TRANSACTION;
727cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            goto finish;
728cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
729d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        case BR_ACQUIRE_RESULT:
730cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            {
731cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT");
732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                const int32_t result = mIn.readInt32();
7339c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                if (!acquireResult) continue;
7349c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                *acquireResult = result ? NO_ERROR : INVALID_OPERATION;
7359c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            }
7369c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            goto finish;
7379c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn
7389c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        case BR_REPLY:
7399c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn            {
7409c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                binder_transaction_data tr;
7419c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                err = mIn.read(&tr, sizeof(tr));
742cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY");
743cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (err != NO_ERROR) goto finish;
744cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
745cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (reply) {
746cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if ((tr.flags & TF_STATUS_CODE) == 0) {
747cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        reply->ipcSetDataReference(
748cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
749cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            tr.data_size,
750cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
751cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            tr.offsets_size/sizeof(binder_size_t),
752cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            freeBuffer, this);
753cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    } else {
754cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer);
755cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        freeBuffer(NULL,
756cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
757cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            tr.data_size,
758cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
759cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            tr.offsets_size/sizeof(binder_size_t), this);
760cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
761cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                } else {
762cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    freeBuffer(NULL,
763cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
764cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        tr.data_size,
765cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
76620735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell                        tr.offsets_size/sizeof(binder_size_t), this);
767cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    continue;
768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            goto finish;
771cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        default:
773cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            err = executeCommand(cmd);
774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (err != NO_ERROR) goto finish;
775cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            break;
776cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
777cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
778cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
779cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornfinish:
780cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (err != NO_ERROR) {
781cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (acquireResult) *acquireResult = err;
782cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (reply) reply->setError(err);
783cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mLastError = err;
784cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
785cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
786cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    return err;
787cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
788cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
7893a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brownstatus_t IPCThreadState::talkWithDriver(bool doReceive)
7903a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown{
7913a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    if (mProcess->mDriverFD <= 0) {
7923a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown        return -EBADF;
7933a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    }
7943a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown
7953a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    binder_write_read bwr;
7963a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown
7973a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    // Is the read buffer empty?
7983a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    const bool needRead = mIn.dataPosition() >= mIn.dataSize();
7993a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown
800cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // We don't want to write anything if we are still reading
801cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // from data left in the input buffer and the caller
802cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // has requested to read the next data.
803cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0;
804cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
805cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    bwr.write_size = outAvail;
806cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    bwr.write_buffer = (uintptr_t)mOut.data();
807cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
808cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // This is what we'll read.
809cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (doReceive && needRead) {
810cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        bwr.read_size = mIn.dataCapacity();
811cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        bwr.read_buffer = (uintptr_t)mIn.data();
812cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    } else {
813cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        bwr.read_size = 0;
814cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        bwr.read_buffer = 0;
815cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
816cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
817cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    IF_LOG_COMMANDS() {
818cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        TextOutput::Bundle _b(alog);
819cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (outAvail != 0) {
820cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            alog << "Sending commands to driver: " << indent;
821cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const void* cmds = (const void*)bwr.write_buffer;
822cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            const void* end = ((const uint8_t*)cmds)+bwr.write_size;
823cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            alog << HexDump(cmds, bwr.write_size) << endl;
824cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            while (cmds < end) cmds = printCommand(alog, cmds);
825cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            alog << dedent;
826cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
827cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        alog << "Size of receive buffer: " << bwr.read_size
828cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            << ", needRead: " << needRead << ", doReceive: " << doReceive << endl;
829cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
830cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
831cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // Return immediately if there is nothing to do.
832cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR;
833cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
834cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    bwr.write_consumed = 0;
835cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    bwr.read_consumed = 0;
836d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    status_t err;
837cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    do {
838cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        IF_LOG_COMMANDS() {
839cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            alog << "About to read/write, write size = " << mOut.dataSize() << endl;
840cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
841cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#if defined(HAVE_ANDROID_OS)
842cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)
8438491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy            err = NO_ERROR;
844cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        else
845cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            err = -errno;
846cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn#else
8472a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        err = INVALID_OPERATION;
8482a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn#endif
8492a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (mProcess->mDriverFD <= 0) {
8502a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            err = -EBADF;
8512a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
8522a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        IF_LOG_COMMANDS() {
8532a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            alog << "Finished read/write, write size = " << mOut.dataSize() << endl;
8542a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
8552a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    } while (err == -EINTR);
8562a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
8572a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    IF_LOG_COMMANDS() {
8582a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: "
8592a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            << bwr.write_consumed << " (of " << mOut.dataSize()
8602a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                        << "), read consumed: " << bwr.read_consumed << endl;
8618491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy    }
8622a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
8632a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    if (err >= NO_ERROR) {
8642a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (bwr.write_consumed > 0) {
8652a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (bwr.write_consumed < mOut.dataSize())
866cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mOut.remove(0, bwr.write_consumed);
86779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            else
86879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                mOut.setDataSize(0);
86979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        }
870abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        if (bwr.read_consumed > 0) {
87179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            mIn.setDataSize(bwr.read_consumed);
87279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            mIn.setDataPosition(0);
87379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        }
87479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        IF_LOG_COMMANDS() {
875abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            TextOutput::Bundle _b(alog);
87679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            alog << "Remaining data size: " << mOut.dataSize() << endl;
87779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            alog << "Received commands from driver: " << indent;
878abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            const void* cmds = mIn.data();
87979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            const void* end = mIn.data() + mIn.dataSize();
8803ab5a96f51cbd0498a62f1e91f1ef99e95cb1b1cAdam Powell            alog << HexDump(cmds, mIn.dataSize()) << endl;
8813ab5a96f51cbd0498a62f1e91f1ef99e95cb1b1cAdam Powell            while (cmds < end) cmds = printReturnCommand(alog, cmds);
882abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            alog << dedent;
883abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        }
88479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        return NO_ERROR;
885c83dccecb0ec4bf9f4befdb18caccb8f373ed7bcAdam Powell    }
886abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell
887abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    return err;
888abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell}
88979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell
89079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powellstatus_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,
891abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)
89279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell{
89379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell    binder_transaction_data tr;
894abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell
895abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */
896abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    tr.target.handle = handle;
897cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    tr.code = code;
898cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    tr.flags = binderFlags;
899cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    tr.cookie = 0;
900cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    tr.sender_pid = 0;
901cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    tr.sender_euid = 0;
902cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
903d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    const status_t err = data.errorCheck();
904cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    if (err == NO_ERROR) {
905cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tr.data_size = data.ipcDataSize();
906cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tr.data.ptr.buffer = data.ipcData();
907d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t);
908cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tr.data.ptr.offsets = data.ipcObjects();
909cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    } else if (statusBuffer) {
910d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        tr.flags |= TF_STATUS_CODE;
911cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        *statusBuffer = err;
9128491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy        tr.data_size = sizeof(status_t);
913cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer);
914cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tr.offsets_size = 0;
915cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        tr.data.ptr.offsets = 0;
9160e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    } else {
9170e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick        return (mLastError = err);
9180e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    }
9190e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick
9200e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    mOut.writeInt32(cmd);
9210e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    mOut.write(&tr, sizeof(tr));
9220e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick
9230e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    return NO_ERROR;
924d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy}
925cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
926cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornsp<BBinder> the_context_object;
9270e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick
928cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornvoid setTheContextObject(sp<BBinder> obj)
929d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy{
930cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    the_context_object = obj;
9318491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy}
932cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
933cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornstatus_t IPCThreadState::executeCommand(int32_t cmd)
934cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
9350e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    BBinder* obj;
9360e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    RefBase::weakref_type* refs;
9370e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    status_t result = NO_ERROR;
9380e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick
9390e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    switch ((uint32_t)cmd) {
9400e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick    case BR_ERROR:
9410e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick        result = mIn.readInt32();
9420e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick        break;
943d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
944cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    case BR_OK:
945cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        break;
9460e35b9f59703416786e7ac460726c2c06a9194fcDaniel Resnick
947fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri    case BR_ACQUIRE:
948fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        refs = (RefBase::weakref_type*)mIn.readPointer();
949fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        obj = (BBinder*)mIn.readPointer();
950fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        ALOG_ASSERT(refs->refBase() == obj,
951fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri                   "BR_ACQUIRE: object %p does not match cookie %p (expected %p)",
952fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri                   refs, obj, refs->refBase());
953fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        obj->incStrong(mProcess.get());
954fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        IF_LOG_REMOTEREFS() {
955fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri            LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj);
956fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri            obj->printRefs();
957fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        }
958fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        mOut.writeInt32(BC_ACQUIRE_DONE);
959fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        mOut.writePointer((uintptr_t)refs);
960fed04169c31e7e3d8c7a328876358dd3564062bbClara Bayarri        mOut.writePointer((uintptr_t)obj);
961cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        break;
962d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
963cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    case BR_RELEASE:
964cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        refs = (RefBase::weakref_type*)mIn.readPointer();
965cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        obj = (BBinder*)mIn.readPointer();
966cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ALOG_ASSERT(refs->refBase() == obj,
967cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                   "BR_RELEASE: object %p does not match cookie %p (expected %p)",
968d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                   refs, obj, refs->refBase());
969cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        IF_LOG_REMOTEREFS() {
970cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj);
971cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            obj->printRefs();
972cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
973cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mPendingStrongDerefs.push(obj);
974cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        break;
975cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
976cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    case BR_INCREFS:
977cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        refs = (RefBase::weakref_type*)mIn.readPointer();
978cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        obj = (BBinder*)mIn.readPointer();
979d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        refs->incWeak(mProcess.get());
980cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mOut.writeInt32(BC_INCREFS_DONE);
981e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        mOut.writePointer((uintptr_t)refs);
982e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        mOut.writePointer((uintptr_t)obj);
983e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        break;
984e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
985e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav    case BR_DECREFS:
986e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        refs = (RefBase::weakref_type*)mIn.readPointer();
987e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        obj = (BBinder*)mIn.readPointer();
988e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        // NOTE: This assertion is not valid, because the object may no
989e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        // longer exist (thus the (BBinder*)cast above resulting in a different
990e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        // memory address).
991e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        //ALOG_ASSERT(refs->refBase() == obj,
992e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        //           "BR_DECREFS: object %p does not match cookie %p (expected %p)",
993e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        //           refs, obj, refs->refBase());
994e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        mPendingWeakDerefs.push(refs);
995e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        break;
996e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
997e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav    case BR_ATTEMPT_ACQUIRE:
998e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        refs = (RefBase::weakref_type*)mIn.readPointer();
999e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        obj = (BBinder*)mIn.readPointer();
1000e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1001e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        {
1002e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            const bool success = refs->attemptIncStrong(mProcess.get());
1003e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            ALOG_ASSERT(success && refs->refBase() == obj,
1004e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                       "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)",
1005e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                       refs, obj, refs->refBase());
1006e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1007e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mOut.writeInt32(BC_ACQUIRE_RESULT);
1008e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mOut.writeInt32((int32_t)success);
1009e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        }
1010e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        break;
1011e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1012e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav    case BR_TRANSACTION:
1013e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        {
1014e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            binder_transaction_data tr;
1015e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            result = mIn.read(&tr, sizeof(tr));
1016e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            ALOG_ASSERT(result == NO_ERROR,
10172750390fbdc2e7037a7e10141d23f2068ae8c2d7Svet Ganov                "Not enough command data for brTRANSACTION");
10182750390fbdc2e7037a7e10141d23f2068ae8c2d7Svet Ganov            if (result != NO_ERROR) break;
10192750390fbdc2e7037a7e10141d23f2068ae8c2d7Svet Ganov
10202750390fbdc2e7037a7e10141d23f2068ae8c2d7Svet Ganov            Parcel buffer;
10212750390fbdc2e7037a7e10141d23f2068ae8c2d7Svet Ganov            buffer.ipcSetDataReference(
10222750390fbdc2e7037a7e10141d23f2068ae8c2d7Svet Ganov                reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer),
1023e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                tr.data_size,
1024e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets),
1025e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                tr.offsets_size/sizeof(binder_size_t), freeBuffer, this);
1026e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1027e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            const pid_t origPid = mCallingPid;
1028e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            const uid_t origUid = mCallingUid;
1029e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            const int32_t origStrictModePolicy = mStrictModePolicy;
1030e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags;
1031e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1032e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mCallingPid = tr.sender_pid;
1033e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mCallingUid = tr.sender_euid;
1034e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mLastTransactionBinderFlags = tr.flags;
1035e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1036e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            int curPrio = getpriority(PRIO_PROCESS, mMyThreadId);
1037e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            if (gDisableBackgroundScheduling) {
1038e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                if (curPrio > ANDROID_PRIORITY_NORMAL) {
1039e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // We have inherited a reduced priority from the caller, but do not
1040e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // want to run in that state in this process.  The driver set our
1041e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // priority already (though not our scheduling class), so bounce
1042e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // it back to the default before invoking the transaction.
1043e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    setpriority(PRIO_PROCESS, mMyThreadId, ANDROID_PRIORITY_NORMAL);
1044e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                }
1045e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            } else {
1046e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                if (curPrio >= ANDROID_PRIORITY_BACKGROUND) {
1047e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // We want to use the inherited priority from the caller.
1048e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // Ensure this thread is in the background scheduling class,
1049e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // since the driver won't modify scheduling classes for us.
1050e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // The scheduling group is reset to default by the caller
1051e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    // once this method returns after the transaction is complete.
1052e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    set_sched_policy(mMyThreadId, SP_BACKGROUND);
1053e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                }
1054e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            }
1055e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1056e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            //ALOGI(">>>> TRANSACT from pid %d uid %d\n", mCallingPid, mCallingUid);
10575fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian
1058e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            Parcel reply;
1059e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            status_t error;
1060e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            IF_LOG_TRANSACTIONS() {
1061e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                TextOutput::Bundle _b(alog);
1062e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                alog << "BR_TRANSACTION thr " << (void*)pthread_self()
1063dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov                    << " / obj " << tr.target.ptr << " / code "
1064dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov                    << TypeCode(tr.code) << ": " << indent << buffer
1065dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov                    << dedent << endl
1066dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov                    << "Data addr = "
1067dddb7e2bbb343ecb435026c13028ba5cd1ae42a5Svet Ganov                    << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer)
1068e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    << ", offsets addr="
1069e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl;
1070e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            }
1071e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            if (tr.target.ptr) {
1072e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                sp<BBinder> b((BBinder*)tr.cookie);
1073e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                error = b->transact(tr.code, buffer, &reply, tr.flags);
1074e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1075e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            } else {
1076e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                error = the_context_object->transact(tr.code, buffer, &reply, tr.flags);
1077e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            }
1078e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1079e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            //ALOGI("<<<< TRANSACT from pid %d restore pid %d uid %d\n",
1080e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            //     mCallingPid, origPid, origUid);
1081e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1082e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            if ((tr.flags & TF_ONE_WAY) == 0) {
1083e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                LOG_ONEWAY("Sending reply to %d!", mCallingPid);
1084e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                if (error < NO_ERROR) reply.setError(error);
1085e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                sendReply(reply, 0);
1086e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            } else {
1087e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                LOG_ONEWAY("NOT sending reply to %d!", mCallingPid);
1088e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            }
1089e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1090e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mCallingPid = origPid;
1091e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mCallingUid = origUid;
1092e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mStrictModePolicy = origStrictModePolicy;
1093e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mLastTransactionBinderFlags = origTransactionBinderFlags;
1094e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1095e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            IF_LOG_TRANSACTIONS() {
1096e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                TextOutput::Bundle _b(alog);
1097e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj "
1098e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav                    << tr.target.ptr << ": " << indent << reply << dedent << endl;
1099e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            }
1100e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1101e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        }
1102e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        break;
1103e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav
1104e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav    case BR_DEAD_BINDER:
1105e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav        {
1106e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            BpBinder *proxy = (BpBinder*)mIn.readPointer();
1107e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            proxy->sendObituary();
1108e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mOut.writeInt32(BC_DEAD_BINDER_DONE);
1109e1e957f536c20ebe2a7e6c417ccb2fd8f7fa845bSvetoslav            mOut.writePointer((uintptr_t)proxy);
1110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } break;
1111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    case BR_CLEAR_DEATH_NOTIFICATION_DONE:
1113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        {
1114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            BpBinder *proxy = (BpBinder*)mIn.readPointer();
11158491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy            proxy->getWeakRefs()->decWeak(proxy);
11160f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        } break;
1117bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes
11180f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell    case BR_FINISHED:
1119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        result = TIMED_OUT;
1120d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        break;
1121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    case BR_NOOP:
1123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        break;
11249277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
1125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    case BR_SPAWN_LOOPER:
1126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mProcess->spawnPooledThread(false);
11279277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        break;
1128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    default:
11309277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        printf("*** BAD COMMAND %d received from Binder driver\n", cmd);
11319277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        result = UNKNOWN_ERROR;
11329277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        break;
11339277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    }
11349277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
11359277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    if (result != NO_ERROR) {
11369277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        mLastError = result;
11379277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    }
11389277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
11399277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    return result;
11409277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn}
11419277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
11429277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornvoid IPCThreadState::threadDestructor(void *st)
11439277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn{
1144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        IPCThreadState* const self = static_cast<IPCThreadState*>(st);
11459277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        if (self) {
11469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                self->flushCommands();
11479277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn#if defined(HAVE_ANDROID_OS)
11489277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        if (self->mProcess->mDriverFD > 0) {
11499277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
11509277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        }
11519277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn#endif
11529277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                delete self;
11539277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        }
11549277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn}
11559277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
11569277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
1157d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedyvoid IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data,
1158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                size_t /*dataSize*/,
1159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                const binder_size_t* /*objects*/,
1160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                size_t /*objectsSize*/, void* /*cookie*/)
1161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn{
1162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    //ALOGI("Freeing parcel %p", &parcel);
1163d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    IF_LOG_COMMANDS() {
1164d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        alog << "Writing BC_FREE_BUFFER for " << data << endl;
1165d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    }
1166d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    ALOG_ASSERT(data != NULL, "Called with NULL data");
1167d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    if (parcel != NULL) parcel->closeFileDescriptors();
1168d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    IPCThreadState* state = self();
1169d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    state->mOut.writeInt32(BC_FREE_BUFFER);
1170d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    state->mOut.writePointer((uintptr_t)data);
1171d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy}
1172d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
1173d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy}; // namespace android
1174d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy