binder.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* Copyright 2008 The Android Open Source Project
  2. */
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <sys/mman.h>
  11. #include "binder.h"
  12. #define MAX_BIO_SIZE (1 << 30)
  13. #define TRACE 0
  14. #define LOG_TAG "Binder"
  15. #include <cutils/log.h>
  16. void bio_init_from_txn(struct binder_io *io, struct binder_transaction_data *txn);
  17. #if TRACE
  18. void hexdump(void *_data, size_t len)
  19. {
  20. unsigned char *data = _data;
  21. size_t count;
  22. for (count = 0; count < len; count++) {
  23. if ((count & 15) == 0)
  24. fprintf(stderr,"%04zu:", count);
  25. fprintf(stderr," %02x %c", *data,
  26. (*data < 32) || (*data > 126) ? '.' : *data);
  27. data++;
  28. if ((count & 15) == 15)
  29. fprintf(stderr,"\n");
  30. }
  31. if ((count & 15) != 0)
  32. fprintf(stderr,"\n");
  33. }
  34. void binder_dump_txn(struct binder_transaction_data *txn)
  35. {
  36. struct flat_binder_object *obj;
  37. binder_size_t *offs = (binder_size_t *)(uintptr_t)txn->data.ptr.offsets;
  38. size_t count = txn->offsets_size / sizeof(binder_size_t);
  39. fprintf(stderr," target %016"PRIx64" cookie %016"PRIx64" code %08x flags %08x\n",
  40. (uint64_t)txn->target.ptr, (uint64_t)txn->cookie, txn->code, txn->flags);
  41. fprintf(stderr," pid %8d uid %8d data %"PRIu64" offs %"PRIu64"\n",
  42. txn->sender_pid, txn->sender_euid, (uint64_t)txn->data_size, (uint64_t)txn->offsets_size);
  43. hexdump((void *)(uintptr_t)txn->data.ptr.buffer, txn->data_size);
  44. while (count--) {
  45. obj = (struct flat_binder_object *) (((char*)(uintptr_t)txn->data.ptr.buffer) + *offs++);
  46. fprintf(stderr," - type %08x flags %08x ptr %016"PRIx64" cookie %016"PRIx64"\n",
  47. obj->type, obj->flags, (uint64_t)obj->binder, (uint64_t)obj->cookie);
  48. }
  49. }
  50. #define NAME(n) case n: return #n
  51. const char *cmd_name(uint32_t cmd)
  52. {
  53. switch(cmd) {
  54. NAME(BR_NOOP);
  55. NAME(BR_TRANSACTION_COMPLETE);
  56. NAME(BR_INCREFS);
  57. NAME(BR_ACQUIRE);
  58. NAME(BR_RELEASE);
  59. NAME(BR_DECREFS);
  60. NAME(BR_TRANSACTION);
  61. NAME(BR_REPLY);
  62. NAME(BR_FAILED_REPLY);
  63. NAME(BR_DEAD_REPLY);
  64. NAME(BR_DEAD_BINDER);
  65. default: return "???";
  66. }
  67. }
  68. #else
  69. #define hexdump(a,b) do{} while (0)
  70. #define binder_dump_txn(txn) do{} while (0)
  71. #endif
  72. #define BIO_F_SHARED 0x01 /* needs to be buffer freed */
  73. #define BIO_F_OVERFLOW 0x02 /* ran out of space */
  74. #define BIO_F_IOERROR 0x04
  75. #define BIO_F_MALLOCED 0x08 /* needs to be free()'d */
  76. struct binder_state
  77. {
  78. int fd;
  79. void *mapped;
  80. size_t mapsize;
  81. };
  82. struct binder_state *binder_open(size_t mapsize)
  83. {
  84. struct binder_state *bs;
  85. struct binder_version vers;
  86. bs = malloc(sizeof(*bs));
  87. if (!bs) {
  88. errno = ENOMEM;
  89. return NULL;
  90. }
  91. bs->fd = open("/dev/binder", O_RDWR);
  92. if (bs->fd < 0) {
  93. fprintf(stderr,"binder: cannot open device (%s)\n",
  94. strerror(errno));
  95. goto fail_open;
  96. }
  97. if ((ioctl(bs->fd, BINDER_VERSION, &vers) == -1) ||
  98. (vers.protocol_version != BINDER_CURRENT_PROTOCOL_VERSION)) {
  99. fprintf(stderr,
  100. "binder: kernel driver version (%d) differs from user space version (%d)\n",
  101. vers.protocol_version, BINDER_CURRENT_PROTOCOL_VERSION);
  102. goto fail_open;
  103. }
  104. bs->mapsize = mapsize;
  105. bs->mapped = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);
  106. if (bs->mapped == MAP_FAILED) {
  107. fprintf(stderr,"binder: cannot map device (%s)\n",
  108. strerror(errno));
  109. goto fail_map;
  110. }
  111. return bs;
  112. fail_map:
  113. close(bs->fd);
  114. fail_open:
  115. free(bs);
  116. return NULL;
  117. }
  118. void binder_close(struct binder_state *bs)
  119. {
  120. munmap(bs->mapped, bs->mapsize);
  121. close(bs->fd);
  122. free(bs);
  123. }
  124. int binder_become_context_manager(struct binder_state *bs)
  125. {
  126. return ioctl(bs->fd, BINDER_SET_CONTEXT_MGR, 0);
  127. }
  128. int binder_write(struct binder_state *bs, void *data, size_t len)
  129. {
  130. struct binder_write_read bwr;
  131. int res;
  132. bwr.write_size = len;
  133. bwr.write_consumed = 0;
  134. bwr.write_buffer = (uintptr_t) data;
  135. bwr.read_size = 0;
  136. bwr.read_consumed = 0;
  137. bwr.read_buffer = 0;
  138. res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
  139. if (res < 0) {
  140. fprintf(stderr,"binder_write: ioctl failed (%s)\n",
  141. strerror(errno));
  142. }
  143. return res;
  144. }
  145. void binder_send_reply(struct binder_state *bs,
  146. struct binder_io *reply,
  147. binder_uintptr_t buffer_to_free,
  148. int status)
  149. {
  150. struct {
  151. uint32_t cmd_free;
  152. binder_uintptr_t buffer;
  153. uint32_t cmd_reply;
  154. struct binder_transaction_data txn;
  155. } __attribute__((packed)) data;
  156. data.cmd_free = BC_FREE_BUFFER;
  157. data.buffer = buffer_to_free;
  158. data.cmd_reply = BC_REPLY;
  159. data.txn.target.ptr = 0;
  160. data.txn.cookie = 0;
  161. data.txn.code = 0;
  162. if (status) {
  163. data.txn.flags = TF_STATUS_CODE;
  164. data.txn.data_size = sizeof(int);
  165. data.txn.offsets_size = 0;
  166. data.txn.data.ptr.buffer = (uintptr_t)&status;
  167. data.txn.data.ptr.offsets = 0;
  168. } else {
  169. data.txn.flags = 0;
  170. data.txn.data_size = reply->data - reply->data0;
  171. data.txn.offsets_size = ((char*) reply->offs) - ((char*) reply->offs0);
  172. data.txn.data.ptr.buffer = (uintptr_t)reply->data0;
  173. data.txn.data.ptr.offsets = (uintptr_t)reply->offs0;
  174. }
  175. binder_write(bs, &data, sizeof(data));
  176. }
  177. int binder_parse(struct binder_state *bs, struct binder_io *bio,
  178. uintptr_t ptr, size_t size, binder_handler func)
  179. {
  180. int r = 1;
  181. uintptr_t end = ptr + (uintptr_t) size;
  182. while (ptr < end) {
  183. uint32_t cmd = *(uint32_t *) ptr;
  184. ptr += sizeof(uint32_t);
  185. #if TRACE
  186. fprintf(stderr,"%s:\n", cmd_name(cmd));
  187. #endif
  188. switch(cmd) {
  189. case BR_NOOP:
  190. break;
  191. case BR_TRANSACTION_COMPLETE:
  192. break;
  193. case BR_INCREFS:
  194. case BR_ACQUIRE:
  195. case BR_RELEASE:
  196. case BR_DECREFS:
  197. #if TRACE
  198. fprintf(stderr," %p, %p\n", (void *)ptr, (void *)(ptr + sizeof(void *)));
  199. #endif
  200. ptr += sizeof(struct binder_ptr_cookie);
  201. break;
  202. case BR_TRANSACTION: {
  203. struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
  204. if ((end - ptr) < sizeof(*txn)) {
  205. ALOGE("parse: txn too small!\n");
  206. return -1;
  207. }
  208. binder_dump_txn(txn);
  209. if (func) {
  210. unsigned rdata[256/4];
  211. struct binder_io msg;
  212. struct binder_io reply;
  213. int res;
  214. bio_init(&reply, rdata, sizeof(rdata), 4);
  215. bio_init_from_txn(&msg, txn);
  216. res = func(bs, txn, &msg, &reply);
  217. binder_send_reply(bs, &reply, txn->data.ptr.buffer, res);
  218. }
  219. ptr += sizeof(*txn);
  220. break;
  221. }
  222. case BR_REPLY: {
  223. struct binder_transaction_data *txn = (struct binder_transaction_data *) ptr;
  224. if ((end - ptr) < sizeof(*txn)) {
  225. ALOGE("parse: reply too small!\n");
  226. return -1;
  227. }
  228. binder_dump_txn(txn);
  229. if (bio) {
  230. bio_init_from_txn(bio, txn);
  231. bio = 0;
  232. } else {
  233. /* todo FREE BUFFER */
  234. }
  235. ptr += sizeof(*txn);
  236. r = 0;
  237. break;
  238. }
  239. case BR_DEAD_BINDER: {
  240. struct binder_death *death = (struct binder_death *)(uintptr_t) *(binder_uintptr_t *)ptr;
  241. ptr += sizeof(binder_uintptr_t);
  242. death->func(bs, death->ptr);
  243. break;
  244. }
  245. case BR_FAILED_REPLY:
  246. r = -1;
  247. break;
  248. case BR_DEAD_REPLY:
  249. r = -1;
  250. break;
  251. default:
  252. ALOGE("parse: OOPS %d\n", cmd);
  253. return -1;
  254. }
  255. }
  256. return r;
  257. }
  258. void binder_acquire(struct binder_state *bs, uint32_t target)
  259. {
  260. uint32_t cmd[2];
  261. cmd[0] = BC_ACQUIRE;
  262. cmd[1] = target;
  263. binder_write(bs, cmd, sizeof(cmd));
  264. }
  265. void binder_release(struct binder_state *bs, uint32_t target)
  266. {
  267. uint32_t cmd[2];
  268. cmd[0] = BC_RELEASE;
  269. cmd[1] = target;
  270. binder_write(bs, cmd, sizeof(cmd));
  271. }
  272. void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death)
  273. {
  274. struct {
  275. uint32_t cmd;
  276. struct binder_handle_cookie payload;
  277. } __attribute__((packed)) data;
  278. data.cmd = BC_REQUEST_DEATH_NOTIFICATION;
  279. data.payload.handle = target;
  280. data.payload.cookie = (uintptr_t) death;
  281. binder_write(bs, &data, sizeof(data));
  282. }
  283. int binder_call(struct binder_state *bs,
  284. struct binder_io *msg, struct binder_io *reply,
  285. uint32_t target, uint32_t code)
  286. {
  287. int res;
  288. struct binder_write_read bwr;
  289. struct {
  290. uint32_t cmd;
  291. struct binder_transaction_data txn;
  292. } __attribute__((packed)) writebuf;
  293. unsigned readbuf[32];
  294. if (msg->flags & BIO_F_OVERFLOW) {
  295. fprintf(stderr,"binder: txn buffer overflow\n");
  296. goto fail;
  297. }
  298. writebuf.cmd = BC_TRANSACTION;
  299. writebuf.txn.target.handle = target;
  300. writebuf.txn.code = code;
  301. writebuf.txn.flags = 0;
  302. writebuf.txn.data_size = msg->data - msg->data0;
  303. writebuf.txn.offsets_size = ((char*) msg->offs) - ((char*) msg->offs0);
  304. writebuf.txn.data.ptr.buffer = (uintptr_t)msg->data0;
  305. writebuf.txn.data.ptr.offsets = (uintptr_t)msg->offs0;
  306. bwr.write_size = sizeof(writebuf);
  307. bwr.write_consumed = 0;
  308. bwr.write_buffer = (uintptr_t) &writebuf;
  309. hexdump(msg->data0, msg->data - msg->data0);
  310. for (;;) {
  311. bwr.read_size = sizeof(readbuf);
  312. bwr.read_consumed = 0;
  313. bwr.read_buffer = (uintptr_t) readbuf;
  314. res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
  315. if (res < 0) {
  316. fprintf(stderr,"binder: ioctl failed (%s)\n", strerror(errno));
  317. goto fail;
  318. }
  319. res = binder_parse(bs, reply, (uintptr_t) readbuf, bwr.read_consumed, 0);
  320. if (res == 0) return 0;
  321. if (res < 0) goto fail;
  322. }
  323. fail:
  324. memset(reply, 0, sizeof(*reply));
  325. reply->flags |= BIO_F_IOERROR;
  326. return -1;
  327. }
  328. void binder_loop(struct binder_state *bs, binder_handler func)
  329. {
  330. int res;
  331. struct binder_write_read bwr;
  332. uint32_t readbuf[32];
  333. bwr.write_size = 0;
  334. bwr.write_consumed = 0;
  335. bwr.write_buffer = 0;
  336. readbuf[0] = BC_ENTER_LOOPER;
  337. binder_write(bs, readbuf, sizeof(uint32_t));
  338. for (;;) {
  339. bwr.read_size = sizeof(readbuf);
  340. bwr.read_consumed = 0;
  341. bwr.read_buffer = (uintptr_t) readbuf;
  342. res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
  343. if (res < 0) {
  344. ALOGE("binder_loop: ioctl failed (%s)\n", strerror(errno));
  345. break;
  346. }
  347. res = binder_parse(bs, 0, (uintptr_t) readbuf, bwr.read_consumed, func);
  348. if (res == 0) {
  349. ALOGE("binder_loop: unexpected reply?!\n");
  350. break;
  351. }
  352. if (res < 0) {
  353. ALOGE("binder_loop: io error %d %s\n", res, strerror(errno));
  354. break;
  355. }
  356. }
  357. }
  358. void bio_init_from_txn(struct binder_io *bio, struct binder_transaction_data *txn)
  359. {
  360. bio->data = bio->data0 = (char *)(intptr_t)txn->data.ptr.buffer;
  361. bio->offs = bio->offs0 = (binder_size_t *)(intptr_t)txn->data.ptr.offsets;
  362. bio->data_avail = txn->data_size;
  363. bio->offs_avail = txn->offsets_size / sizeof(size_t);
  364. bio->flags = BIO_F_SHARED;
  365. }
  366. void bio_init(struct binder_io *bio, void *data,
  367. size_t maxdata, size_t maxoffs)
  368. {
  369. size_t n = maxoffs * sizeof(size_t);
  370. if (n > maxdata) {
  371. bio->flags = BIO_F_OVERFLOW;
  372. bio->data_avail = 0;
  373. bio->offs_avail = 0;
  374. return;
  375. }
  376. bio->data = bio->data0 = (char *) data + n;
  377. bio->offs = bio->offs0 = data;
  378. bio->data_avail = maxdata - n;
  379. bio->offs_avail = maxoffs;
  380. bio->flags = 0;
  381. }
  382. static void *bio_alloc(struct binder_io *bio, size_t size)
  383. {
  384. size = (size + 3) & (~3);
  385. if (size > bio->data_avail) {
  386. bio->flags |= BIO_F_OVERFLOW;
  387. return NULL;
  388. } else {
  389. void *ptr = bio->data;
  390. bio->data += size;
  391. bio->data_avail -= size;
  392. return ptr;
  393. }
  394. }
  395. void binder_done(struct binder_state *bs,
  396. struct binder_io *msg,
  397. struct binder_io *reply)
  398. {
  399. struct {
  400. uint32_t cmd;
  401. uintptr_t buffer;
  402. } __attribute__((packed)) data;
  403. if (reply->flags & BIO_F_SHARED) {
  404. data.cmd = BC_FREE_BUFFER;
  405. data.buffer = (uintptr_t) reply->data0;
  406. binder_write(bs, &data, sizeof(data));
  407. reply->flags = 0;
  408. }
  409. }
  410. static struct flat_binder_object *bio_alloc_obj(struct binder_io *bio)
  411. {
  412. struct flat_binder_object *obj;
  413. obj = bio_alloc(bio, sizeof(*obj));
  414. if (obj && bio->offs_avail) {
  415. bio->offs_avail--;
  416. *bio->offs++ = ((char*) obj) - ((char*) bio->data0);
  417. return obj;
  418. }
  419. bio->flags |= BIO_F_OVERFLOW;
  420. return NULL;
  421. }
  422. void bio_put_uint32(struct binder_io *bio, uint32_t n)
  423. {
  424. uint32_t *ptr = bio_alloc(bio, sizeof(n));
  425. if (ptr)
  426. *ptr = n;
  427. }
  428. void bio_put_obj(struct binder_io *bio, void *ptr)
  429. {
  430. struct flat_binder_object *obj;
  431. obj = bio_alloc_obj(bio);
  432. if (!obj)
  433. return;
  434. obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  435. obj->type = BINDER_TYPE_BINDER;
  436. obj->binder = (uintptr_t)ptr;
  437. obj->cookie = 0;
  438. }
  439. void bio_put_ref(struct binder_io *bio, uint32_t handle)
  440. {
  441. struct flat_binder_object *obj;
  442. if (handle)
  443. obj = bio_alloc_obj(bio);
  444. else
  445. obj = bio_alloc(bio, sizeof(*obj));
  446. if (!obj)
  447. return;
  448. obj->flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  449. obj->type = BINDER_TYPE_HANDLE;
  450. obj->handle = handle;
  451. obj->cookie = 0;
  452. }
  453. void bio_put_string16(struct binder_io *bio, const uint16_t *str)
  454. {
  455. size_t len;
  456. uint16_t *ptr;
  457. if (!str) {
  458. bio_put_uint32(bio, 0xffffffff);
  459. return;
  460. }
  461. len = 0;
  462. while (str[len]) len++;
  463. if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
  464. bio_put_uint32(bio, 0xffffffff);
  465. return;
  466. }
  467. /* Note: The payload will carry 32bit size instead of size_t */
  468. bio_put_uint32(bio, (uint32_t) len);
  469. len = (len + 1) * sizeof(uint16_t);
  470. ptr = bio_alloc(bio, len);
  471. if (ptr)
  472. memcpy(ptr, str, len);
  473. }
  474. void bio_put_string16_x(struct binder_io *bio, const char *_str)
  475. {
  476. unsigned char *str = (unsigned char*) _str;
  477. size_t len;
  478. uint16_t *ptr;
  479. if (!str) {
  480. bio_put_uint32(bio, 0xffffffff);
  481. return;
  482. }
  483. len = strlen(_str);
  484. if (len >= (MAX_BIO_SIZE / sizeof(uint16_t))) {
  485. bio_put_uint32(bio, 0xffffffff);
  486. return;
  487. }
  488. /* Note: The payload will carry 32bit size instead of size_t */
  489. bio_put_uint32(bio, len);
  490. ptr = bio_alloc(bio, (len + 1) * sizeof(uint16_t));
  491. if (!ptr)
  492. return;
  493. while (*str)
  494. *ptr++ = *str++;
  495. *ptr++ = 0;
  496. }
  497. static void *bio_get(struct binder_io *bio, size_t size)
  498. {
  499. size = (size + 3) & (~3);
  500. if (bio->data_avail < size){
  501. bio->data_avail = 0;
  502. bio->flags |= BIO_F_OVERFLOW;
  503. return NULL;
  504. } else {
  505. void *ptr = bio->data;
  506. bio->data += size;
  507. bio->data_avail -= size;
  508. return ptr;
  509. }
  510. }
  511. uint32_t bio_get_uint32(struct binder_io *bio)
  512. {
  513. uint32_t *ptr = bio_get(bio, sizeof(*ptr));
  514. return ptr ? *ptr : 0;
  515. }
  516. uint16_t *bio_get_string16(struct binder_io *bio, size_t *sz)
  517. {
  518. size_t len;
  519. /* Note: The payload will carry 32bit size instead of size_t */
  520. len = (size_t) bio_get_uint32(bio);
  521. if (sz)
  522. *sz = len;
  523. return bio_get(bio, (len + 1) * sizeof(uint16_t));
  524. }
  525. static struct flat_binder_object *_bio_get_obj(struct binder_io *bio)
  526. {
  527. size_t n;
  528. size_t off = bio->data - bio->data0;
  529. /* TODO: be smarter about this? */
  530. for (n = 0; n < bio->offs_avail; n++) {
  531. if (bio->offs[n] == off)
  532. return bio_get(bio, sizeof(struct flat_binder_object));
  533. }
  534. bio->data_avail = 0;
  535. bio->flags |= BIO_F_OVERFLOW;
  536. return NULL;
  537. }
  538. uint32_t bio_get_ref(struct binder_io *bio)
  539. {
  540. struct flat_binder_object *obj;
  541. obj = _bio_get_obj(bio);
  542. if (!obj)
  543. return 0;
  544. if (obj->type == BINDER_TYPE_HANDLE)
  545. return obj->handle;
  546. return 0;
  547. }