編輯:關於 Android 程式設計
本文參考《Android 系統源碼情景分析》(作者羅升陽),聚焦於 Service Manager 完成 Service 註冊後,如何將註冊結果透過 BC_REPLY 命令協議回傳給 Binder 驅動,再轉交給發起請求的進程。
Service Manager 程式碼:
frameworks/base/cmd/servicemanager 下的 binder.c、service_manager.c、binder.h
驅動層程式碼:
kernel/goldfish/drivers/staging/android 下的 binder.c、binder.h
當 Service Manager 成功將一個 Service 元件註冊到內部的 svclist 清單之後,接著會呼叫 binder_send_reply,將註冊結果回傳給 Binder 驅動程式,再由驅動將結果回傳給請求註冊的進程。
void binder_send_reply(struct binder_state *bs, struct binder_io *reply,
void *buffer_to_free, int status) {
struct {
uint32_t cmd_free;
void *buffer;
uint32_t cmd_reply;
struct binder_txn txn;
} __attribute__((packed)) data;
data.cmd_free = BC_FREE_BUFFER; // 後接一個內核緩衝區的用戶空間地址
data.buffer = buffer_to_free;
data.cmd_reply = BC_REPLY; // 後接一個 binder_transaction_data 結構
data.txn.target = 0;
data.txn.cookie = 0;
data.txn.code = 0;
if (status) { // status 為 0 表示成功
data.txn.flags = TF_STATUS_CODE;
data.txn.data_size = sizeof(int);
data.txn.offs_size = 0;
data.txn.data = &status;
data.txn.offs = 0;
} else {
data.txn.flags = 0;
data.txn.data_size = reply->data - reply->data0;
data.txn.offs_size = ((char*) reply->offs) - ((char*) reply->offs0);
data.txn.data = reply->data0;
data.txn.offs = reply->offs0;
}
binder_write(bs, &data, sizeof(data));
}
這裡定義了一個匿名結構體 data,用來描述一個 BC_FREE_BUFFER 與一個 BC_REPLY 命令協議,分別以成員 cmd_free 與 cmd_reply 表示。BC_FREE_BUFFER 後接的是一個內核緩衝區的用戶空間地址,保存在 buffer 中;BC_REPLY 後接的是一個 binder_transaction_data 結構,保存在 txn 中。
接著呼叫 binder_write,將這兩個命令協議發送給 Binder 驅動:
int binder_write(struct binder_state *bs, void *data, unsigned len) {
struct binder_write_read bwr;
int res;
bwr.write_size = len;
bwr.write_consumed = 0;
bwr.write_buffer = (unsigned) data;
bwr.read_size = 0;
bwr.read_consumed = 0;
bwr.read_buffer = 0;
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
if (res < 0) {
fprintf(stderr, "binder_write: ioctl failed (%s)\n", strerror(errno));
}
return res;
}
binder_write 透過 IO 控制命令 BINDER_WRITE_READ 將命令協議送出,映射到驅動層的 binder_thread_write。在該函式中,BC_FREE_BUFFER 與 BC_REPLY 都會進入同一個 case 分支:
case BC_TRANSACTION:
case BC_REPLY: {
struct binder_transaction_data tr;
if (copy_from_user(&tr, ptr, sizeof(tr))) // 複製上面賦值的 data.txn
return -EFAULT;
ptr += sizeof(tr);
binder_transaction(proc, thread, &tr, cmd == BC_REPLY);
break;
}
tr 即上面已賦值的 data.txn,接著呼叫 binder_transaction 進行處理:
static void binder_transaction(struct binder_proc *proc, struct binder_thread *thread,
struct binder_transaction_data *tr, int reply) {
...
if (reply) {
// 從線程的事務堆棧取出對應的 binder_transaction
in_reply_to = thread->transaction_stack;
if (in_reply_to == NULL) {
return_error = BR_FAILED_REPLY;
goto err_empty_call_stack;
}
thread->transaction_stack = in_reply_to->to_parent;
target_thread = in_reply_to->from; // 找到目標線程
target_proc = target_thread->proc; // 找到目標進程
}
...
target_list = &target_thread->todo;
target_wait = &target_thread->wait;
...
t = kzalloc(sizeof(*t), GFP_KERNEL); // 配置 binder_transaction
tcomplete = kzalloc(sizeof(*tcomplete), GFP_KERNEL); // 配置 binder_work
...
t->to_proc = target_proc;
t->to_thread = target_thread;
t->code = tr->code;
t->buffer = binder_alloc_buf(target_proc, tr->data_size, tr->offsets_size, ...);
...
// 將資料緩衝區與偏移數組從用戶空間複製進來
if (copy_from_user(t->buffer->data, tr->data.ptr.buffer, tr->data_size)) { ... }
if (copy_from_user(offp, tr->data.ptr.offsets, tr->offsets_size)) { ... }
...
t->work.type = BINDER_WORK_TRANSACTION;
list_add_tail(&t->work.entry, target_list); // 加入目標線程的 todo
tcomplete->type = BINDER_WORK_TRANSACTION_COMPLETE;
list_add_tail(&tcomplete->entry, &thread->todo); // 加入本線程的 todo
if (target_wait) wake_up_interruptible(target_wait); // 喚醒目標線程
}
當 Binder 驅動為某個線程分發一個進程間通信請求時,會將一個 binder_transaction 壓入其事務堆棧。因此這裡首先從 thread 的事務堆棧取出該結構,保存在 in_reply_to 中。in_reply_to 的 from 成員指向先前發起通信的線程,於是接著取得目標線程 target_thread,並將其 todo 與 wait 作為本次的目標佇列。
隨後初始化 binder_transaction 結構 t 並加入目標線程的 todo,同時初始化 binder_work 加入本線程(service_manager 主線程)的 todo,最後喚醒目標線程。
service_manager 主線程執行完 binder_transaction 後逐層返回,最終回到 binder_loop 的 for 迴圈。由於此時 read_size 大於 0,ioctl 會映射到 binder_thread_read:
static int binder_thread_read(struct binder_proc *proc, struct binder_thread *thread,
void __user *buffer, int size,
signed long *consumed, int non_block) {
void __user *ptr = buffer + *consumed;
void __user *end = buffer + size;
...
wait_for_proc_work = thread->transaction_stack == NULL && list_empty(&thread->todo);
...
while (1) {
// 若 todo 上為 BINDER_WORK_TRANSACTION_COMPLETE
case BINDER_WORK_TRANSACTION_COMPLETE: {
cmd = BR_TRANSACTION_COMPLETE;
if (put_user(cmd, (uint32_t __user *)ptr))
return -EFAULT;
ptr += sizeof(uint32_t);
list_del(&w->entry); // 刪除 todo 上的工作項
kfree(w); // 釋放結構體
break;
}
}
...
}
執行完 binder_thread_read 後返回 binder_loop,開始執行 binder_parse:
int binder_parse(struct binder_state *bs, struct binder_io *bio,
uint32_t *ptr, uint32_t size, binder_handler func) {
int r = 1;
uint32_t *end = ptr + (size / 4);
while (ptr < end) {
uint32_t cmd = *ptr++;
switch (cmd) {
...
case BR_TRANSACTION_COMPLETE:
break; // 完成通知,無需額外處理
...
}
}
return r;
}
執行完 binder_parse 後,binder_loop 再次進入 for 迴圈,並透過 wait_event_interruptible_exclusive 睡眠等待,直到其所屬進程有新的未處理項為止。整個 BC_REPLY 回報流程至此完成。
圖文並茂:Android簡單創建Activity全流程
本文以圖文並茂的方式,帶大家走一遍在Android中簡單創建Activity的完整流程,適合剛入門的開發者參考。 1)建立一個Android專案 先填寫專案相關資訊:
Android 多線程斷點續傳下載的完整思路與實踐要點
這個月接到一個項目,要做一個類似應用市場的軟件管理 App。其中一塊功能是文件下載,而且要支持多線程與斷點續傳。此前沒寫過這類功能,於是把各站資料搜了一遍整理成文,既分享
Line B612 靜音自拍 APP 怎麼用
Line 推出的靜音自拍 App「B612」是一款專為自拍打造的應用,最大的特色是不會發出快門聲。它刻意把操作簡化到極致,拍照前不需要像傳統相機那樣調整一堆參數;同時把最
Android 事件分發詳解:Activity、ViewGroup 與 View 的傳遞流程
事件分發是 Android 使用者體驗的核心,也是自定義 View 與複雜手勢處理的基礎。本文透過日誌與流程圖,整理事件如何從 Activity 往下傳遞,又如何被 Vi