PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/librpc-qcom/svc_clnt_common.c

https://github.com/semiecho/android_hardware_msm7k
C | 324 lines | 234 code | 52 blank | 38 comment | 16 complexity | a71a04ea158f4eba9f78526d3c552b2f MD5 | raw file
  1. #include <rpc/rpc.h>
  2. #include <arpa/inet.h>
  3. #include <errno.h>
  4. #include <debug.h>
  5. extern int r_open(const char *router);
  6. extern void r_close(int handle);
  7. extern int r_read(int handle, char *buf, uint32 size);
  8. extern int r_write(int handle, const char *buf, uint32 size);
  9. extern int r_control(int handle, const uint32 cmd, void *arg);
  10. static void xdr_std_destroy(xdr_s_type *xdr)
  11. {
  12. /* whatever */
  13. }
  14. static bool_t xdr_std_control(xdr_s_type *xdr, int request, void *info)
  15. {
  16. return r_control(xdr->fd, request, info);
  17. }
  18. static bool_t xdr_std_msg_done(xdr_s_type *xdr)
  19. {
  20. /* whatever */
  21. return TRUE;
  22. }
  23. /* Outgoing message control functions */
  24. static bool_t xdr_std_msg_start(xdr_s_type *xdr,
  25. rpc_msg_e_type rpc_msg_type)
  26. {
  27. /* xid is does not matter under our set of assumptions: that for a single
  28. * program/version channel, communication is synchronous. If several
  29. * processes attempt to call functions on a program, then the rpcrouter
  30. * driver will ensure that the calls are properly muxed, because the
  31. * processes will have separate PIDs, and the rpcrouter driver uses PIDs to
  32. * keep track of RPC transactions. For multiple threads in the same
  33. * process accessing the same program, we serialize access in clnt_call()
  34. * by locking a mutex around the RPC call. If threads in the same process
  35. * call into different programs, then there is no issue, again because of
  36. * the use of a mutex in clnt_call().
  37. *
  38. * NOTE: This comment assumes that the only way we talk to the RPC router
  39. * from a client is by using clnt_call(), which is the case for all
  40. * client code generated by rpcgen().
  41. *
  42. * NOTE: The RPC router driver will soon be able to open a separate device
  43. * file for each program/version channel. This will allow for
  44. * natural multiplexing among clients, as we won't have to rely on
  45. * the mutex for the case where different programs are being called
  46. * into by separate threads in the same process. When this happens,
  47. * we'll need to optimize the RPC library to add a separate mutex for
  48. * each program/version channel, which will require some sort of
  49. * registry.
  50. */
  51. if (rpc_msg_type == RPC_MSG_CALL) xdr->xid++;
  52. /* We start writing into the outgoing-message buffer at index 32, because
  53. we need to write header information before we send the message. The
  54. header information includes the destination address and the pacmark
  55. header.
  56. */
  57. xdr->out_next = (RPC_OFFSET+2)*sizeof(uint32);
  58. /* we write the pacmark header when we send the message. */
  59. ((uint32 *)xdr->out_msg)[RPC_OFFSET] = htonl(xdr->xid);
  60. /* rpc call or reply? */
  61. ((uint32 *)xdr->out_msg)[RPC_OFFSET+1] = htonl(rpc_msg_type);
  62. return TRUE;
  63. }
  64. static bool_t xdr_std_msg_abort(xdr_s_type *xdr)
  65. {
  66. /* dummy */
  67. return TRUE;
  68. }
  69. /* Can be used to send both calls and replies. */
  70. extern bool_t xdr_recv_reply_header(xdr_s_type *xdr, rpc_reply_header *reply);
  71. #include <stdio.h>
  72. static bool_t xdr_std_msg_send(xdr_s_type *xdr)
  73. {
  74. /* Send the RPC packet. */
  75. if (r_write(xdr->fd, (void *)xdr->out_msg, xdr->out_next) !=
  76. xdr->out_next)
  77. return FALSE;
  78. return TRUE;
  79. }
  80. static bool_t xdr_std_read(xdr_s_type *xdr)
  81. {
  82. xdr->in_len = r_read(xdr->fd, (void *)xdr->in_msg, RPCROUTER_MSGSIZE_MAX);
  83. if (xdr->in_len < 0) return FALSE;
  84. if (xdr->in_len < (RPC_OFFSET+2)*4) {
  85. xdr->in_len = -1;
  86. return FALSE;
  87. }
  88. xdr->in_next = (RPC_OFFSET+2)*4;
  89. return TRUE;
  90. }
  91. /* Message data functions */
  92. static bool_t xdr_std_send_uint32(xdr_s_type *xdr, const uint32 *value)
  93. {
  94. if (xdr->out_next >= RPCROUTER_MSGSIZE_MAX - 3) return FALSE;
  95. *(int32 *)(xdr->out_msg + xdr->out_next) = htonl(*value);
  96. xdr->out_next += 4;
  97. return TRUE;
  98. }
  99. static bool_t xdr_std_send_int8(xdr_s_type *xdr, const int8 *value)
  100. {
  101. uint32 val = *value;
  102. return xdr_std_send_uint32(xdr, &val);
  103. }
  104. static bool_t xdr_std_send_uint8(xdr_s_type *xdr, const uint8 *value)
  105. {
  106. uint32 val = *value;
  107. return xdr_std_send_uint32(xdr, &val);
  108. }
  109. static bool_t xdr_std_send_int16(xdr_s_type *xdr, const int16 *value)
  110. {
  111. uint32 val = *value;
  112. return xdr_std_send_uint32(xdr, &val);
  113. }
  114. static bool_t xdr_std_send_uint16(xdr_s_type *xdr, const uint16 *value)
  115. {
  116. uint32 val = *value;
  117. return xdr_std_send_uint32(xdr, &val);
  118. }
  119. static bool_t xdr_std_send_int32(xdr_s_type *xdr, const int32 *value)
  120. {
  121. return xdr_std_send_uint32(xdr, (uint32_t *)value);
  122. }
  123. static bool_t xdr_std_send_bytes(xdr_s_type *xdr, const uint8 *buf,
  124. uint32 len)
  125. {
  126. if (xdr->out_next + len > RPCROUTER_MSGSIZE_MAX) return FALSE;
  127. while(len--)
  128. xdr->out_msg[xdr->out_next++] = *buf++;
  129. while(xdr->out_next % 4)
  130. xdr->out_msg[xdr->out_next++] = 0;
  131. return TRUE;
  132. }
  133. #if 0
  134. #include <unwind.h>
  135. typedef struct
  136. {
  137. size_t count;
  138. intptr_t* addrs;
  139. } stack_crawl_state_t;
  140. static _Unwind_Reason_Code trace_function(_Unwind_Context *context, void *arg)
  141. {
  142. stack_crawl_state_t* state = (stack_crawl_state_t*)arg;
  143. if (state->count) {
  144. intptr_t ip = (intptr_t)_Unwind_GetIP(context);
  145. if (ip) {
  146. state->addrs[0] = ip;
  147. state->addrs++;
  148. state->count--;
  149. }
  150. }
  151. return _URC_NO_REASON;
  152. }
  153. static inline
  154. int get_backtrace(intptr_t* addrs, size_t max_entries)
  155. {
  156. stack_crawl_state_t state;
  157. state.count = max_entries;
  158. state.addrs = (intptr_t*)addrs;
  159. _Unwind_Backtrace(trace_function, (void*)&state);
  160. return max_entries - state.count;
  161. }
  162. #endif
  163. static bool_t xdr_std_recv_uint32(xdr_s_type *xdr, uint32 *value)
  164. {
  165. #if 0
  166. intptr_t *trace[20], *tr;
  167. int nc = get_backtrace(trace, 20);
  168. tr = trace;
  169. while(nc--)
  170. D("\t%02d: %p\n", nc, *tr++);
  171. #endif
  172. if (xdr->in_next + 4 > xdr->in_len) { return FALSE; }
  173. if (value) *value = ntohl(*(uint32 *)(xdr->in_msg + xdr->in_next));
  174. xdr->in_next += 4;
  175. return TRUE;
  176. }
  177. #define RECEIVE \
  178. uint32 val; \
  179. if (xdr_std_recv_uint32(xdr, &val)) { \
  180. *value = val; \
  181. return TRUE; \
  182. } \
  183. return FALSE
  184. static bool_t xdr_std_recv_int8(xdr_s_type *xdr, int8 *value)
  185. {
  186. RECEIVE;
  187. }
  188. static bool_t xdr_std_recv_uint8(xdr_s_type *xdr, uint8 *value)
  189. {
  190. RECEIVE;
  191. }
  192. static bool_t xdr_std_recv_int16(xdr_s_type *xdr, int16 *value)
  193. {
  194. RECEIVE;
  195. }
  196. static bool_t xdr_std_recv_uint16(xdr_s_type *xdr, uint16 *value)
  197. {
  198. RECEIVE;
  199. }
  200. #undef RECEIVE
  201. static bool_t xdr_std_recv_int32(xdr_s_type *xdr, int32 *value)
  202. {
  203. return xdr_std_recv_uint32(xdr, (uint32 * )value);
  204. }
  205. static bool_t xdr_std_recv_bytes(xdr_s_type *xdr, uint8 *buf, uint32 len)
  206. {
  207. if (xdr->in_next + (int)len > xdr->in_len) return FALSE;
  208. if (buf) memcpy(buf, &xdr->in_msg[xdr->in_next], len);
  209. xdr->in_next += len;
  210. xdr->in_next = (xdr->in_next + 3) & ~3;
  211. return TRUE;
  212. }
  213. const xdr_ops_s_type xdr_std_xops = {
  214. xdr_std_destroy,
  215. xdr_std_control,
  216. xdr_std_read,
  217. xdr_std_msg_done,
  218. xdr_std_msg_start,
  219. xdr_std_msg_abort,
  220. xdr_std_msg_send,
  221. xdr_std_send_int8,
  222. xdr_std_send_uint8,
  223. xdr_std_send_int16,
  224. xdr_std_send_uint16,
  225. xdr_std_send_int32,
  226. xdr_std_send_uint32,
  227. xdr_std_send_bytes,
  228. xdr_std_recv_int8,
  229. xdr_std_recv_uint8,
  230. xdr_std_recv_int16,
  231. xdr_std_recv_uint16,
  232. xdr_std_recv_int32,
  233. xdr_std_recv_uint32,
  234. xdr_std_recv_bytes,
  235. };
  236. xdr_s_type *xdr_init_common(const char *router, int is_client)
  237. {
  238. xdr_s_type *xdr = (xdr_s_type *)calloc(1, sizeof(xdr_s_type));
  239. xdr->xops = &xdr_std_xops;
  240. xdr->fd = r_open(router);
  241. if (xdr->fd < 0) {
  242. E("ERROR OPENING [%s]: %s\n", router, strerror(errno));
  243. free(xdr);
  244. return NULL;
  245. }
  246. xdr->is_client = is_client;
  247. D("OPENED [%s] fd %d\n", router, xdr->fd);
  248. return xdr;
  249. }
  250. xdr_s_type *xdr_clone(xdr_s_type *other)
  251. {
  252. xdr_s_type *xdr = (xdr_s_type *)calloc(1, sizeof(xdr_s_type));
  253. xdr->xops = &xdr_std_xops;
  254. xdr->fd = dup(other->fd);
  255. if (xdr->fd < 0) {
  256. E("ERROR DUPLICATING FD %d: %s\n", other->fd, strerror(errno));
  257. free(xdr);
  258. return NULL;
  259. }
  260. xdr->xid = xdr->xid;
  261. xdr->x_prog = other->x_prog;
  262. xdr->x_vers = other->x_vers;
  263. xdr->is_client = other->is_client;
  264. D("CLONED fd %d --> %d\n", other->fd, xdr->fd);
  265. return xdr;
  266. }
  267. void xdr_destroy_common(xdr_s_type *xdr)
  268. {
  269. D("CLOSING fd %d\n", xdr->fd);
  270. r_close(xdr->fd);
  271. free(xdr);
  272. }