ipc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stddef.h>
  14. #include <errno.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <stdbool.h>
  19. #include <sys/socket.h>
  20. #include <sys/un.h>
  21. #include <unistd.h>
  22. #include <glib.h>
  23. #include "ipc-common.h"
  24. #include "ipc.h"
  25. #include "src/log.h"
  26. struct service_handler {
  27. const struct ipc_handler *handler;
  28. uint8_t size;
  29. };
  30. struct ipc {
  31. struct service_handler *services;
  32. int service_max;
  33. const char *path;
  34. size_t size;
  35. GIOChannel *cmd_io;
  36. guint cmd_watch;
  37. bool notifications;
  38. GIOChannel *notif_io;
  39. guint notif_watch;
  40. ipc_disconnect_cb disconnect_cb;
  41. void *disconnect_cb_data;
  42. };
  43. static void ipc_disconnect(struct ipc *ipc, bool in_cleanup)
  44. {
  45. if (ipc->cmd_watch) {
  46. g_source_remove(ipc->cmd_watch);
  47. ipc->cmd_watch = 0;
  48. }
  49. if (ipc->cmd_io) {
  50. g_io_channel_shutdown(ipc->cmd_io, TRUE, NULL);
  51. g_io_channel_unref(ipc->cmd_io);
  52. ipc->cmd_io = NULL;
  53. }
  54. if (ipc->notif_watch) {
  55. g_source_remove(ipc->notif_watch);
  56. ipc->notif_watch = 0;
  57. }
  58. if (ipc->notif_io) {
  59. g_io_channel_shutdown(ipc->notif_io, TRUE, NULL);
  60. g_io_channel_unref(ipc->notif_io);
  61. ipc->notif_io = NULL;
  62. }
  63. if (in_cleanup)
  64. return;
  65. if (ipc->disconnect_cb)
  66. ipc->disconnect_cb(ipc->disconnect_cb_data);
  67. }
  68. static int ipc_handle_msg(struct service_handler *handlers, size_t max_index,
  69. const void *buf, ssize_t len)
  70. {
  71. const struct ipc_hdr *msg = buf;
  72. const struct ipc_handler *handler;
  73. if (len < (ssize_t) sizeof(*msg)) {
  74. DBG("message too small (%zd bytes)", len);
  75. return -EBADMSG;
  76. }
  77. if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
  78. DBG("message malformed (%zd bytes)", len);
  79. return -EBADMSG;
  80. }
  81. /* if service is valid */
  82. if (msg->service_id > max_index) {
  83. DBG("unknown service (0x%x)", msg->service_id);
  84. return -EOPNOTSUPP;
  85. }
  86. /* if service is registered */
  87. if (!handlers[msg->service_id].handler) {
  88. DBG("service not registered (0x%x)", msg->service_id);
  89. return -EOPNOTSUPP;
  90. }
  91. /* if opcode is valid */
  92. if (msg->opcode == IPC_OP_STATUS ||
  93. msg->opcode > handlers[msg->service_id].size) {
  94. DBG("invalid opcode 0x%x for service 0x%x", msg->opcode,
  95. msg->service_id);
  96. return -EOPNOTSUPP;
  97. }
  98. /* opcode is table offset + 1 */
  99. handler = &handlers[msg->service_id].handler[msg->opcode - 1];
  100. /* if payload size is valid */
  101. if ((handler->var_len && handler->data_len > msg->len) ||
  102. (!handler->var_len && handler->data_len != msg->len)) {
  103. DBG("invalid size for opcode 0x%x service 0x%x",
  104. msg->opcode, msg->service_id);
  105. return -EMSGSIZE;
  106. }
  107. handler->handler(msg->payload, msg->len);
  108. return 0;
  109. }
  110. static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
  111. gpointer user_data)
  112. {
  113. struct ipc *ipc = user_data;
  114. char buf[IPC_MTU];
  115. ssize_t ret;
  116. int fd, err;
  117. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
  118. info("IPC: command socket closed");
  119. ipc->cmd_watch = 0;
  120. goto fail;
  121. }
  122. fd = g_io_channel_unix_get_fd(io);
  123. ret = read(fd, buf, sizeof(buf));
  124. if (ret < 0) {
  125. error("IPC: command read failed (%s)", strerror(errno));
  126. goto fail;
  127. }
  128. err = ipc_handle_msg(ipc->services, ipc->service_max, buf, ret);
  129. if (err < 0) {
  130. error("IPC: failed to handle message (%s)", strerror(-err));
  131. goto fail;
  132. }
  133. return TRUE;
  134. fail:
  135. ipc_disconnect(ipc, false);
  136. return FALSE;
  137. }
  138. static gboolean notif_watch_cb(GIOChannel *io, GIOCondition cond,
  139. gpointer user_data)
  140. {
  141. struct ipc *ipc = user_data;
  142. info("IPC: notification socket closed");
  143. ipc->notif_watch = 0;
  144. ipc_disconnect(ipc, false);
  145. return FALSE;
  146. }
  147. static GIOChannel *ipc_connect(const char *path, size_t size,
  148. GIOFunc connect_cb, void *user_data)
  149. {
  150. struct sockaddr_un addr;
  151. GIOCondition cond;
  152. GIOChannel *io;
  153. int sk;
  154. sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
  155. if (sk < 0) {
  156. error("IPC: failed to create socket: %d (%s)", errno,
  157. strerror(errno));
  158. return NULL;
  159. }
  160. io = g_io_channel_unix_new(sk);
  161. g_io_channel_set_close_on_unref(io, TRUE);
  162. g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
  163. memset(&addr, 0, sizeof(addr));
  164. addr.sun_family = AF_UNIX;
  165. memcpy(addr.sun_path, path, size);
  166. connect(sk, (struct sockaddr *) &addr, sizeof(addr));
  167. cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  168. g_io_add_watch(io, cond, connect_cb, user_data);
  169. return io;
  170. }
  171. static gboolean notif_connect_cb(GIOChannel *io, GIOCondition cond,
  172. gpointer user_data)
  173. {
  174. struct ipc *ipc = user_data;
  175. DBG("");
  176. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
  177. error("IPC: notification socket connect failed");
  178. ipc_disconnect(ipc, false);
  179. return FALSE;
  180. }
  181. cond = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  182. ipc->notif_watch = g_io_add_watch(io, cond, notif_watch_cb, ipc);
  183. cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  184. ipc->cmd_watch = g_io_add_watch(ipc->cmd_io, cond, cmd_watch_cb, ipc);
  185. info("IPC: successfully connected (with notifications)");
  186. return FALSE;
  187. }
  188. static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond,
  189. gpointer user_data)
  190. {
  191. struct ipc *ipc = user_data;
  192. DBG("");
  193. if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
  194. error("IPC: command socket connect failed");
  195. ipc_disconnect(ipc, false);
  196. return FALSE;
  197. }
  198. if (ipc->notifications) {
  199. ipc->notif_io = ipc_connect(ipc->path, ipc->size,
  200. notif_connect_cb, ipc);
  201. if (!ipc->notif_io)
  202. ipc_disconnect(ipc, false);
  203. return FALSE;
  204. }
  205. cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
  206. ipc->cmd_watch = g_io_add_watch(ipc->cmd_io, cond, cmd_watch_cb, ipc);
  207. info("IPC: successfully connected (without notifications)");
  208. return FALSE;
  209. }
  210. struct ipc *ipc_init(const char *path, size_t size, int max_service_id,
  211. bool notifications,
  212. ipc_disconnect_cb cb, void *cb_data)
  213. {
  214. struct ipc *ipc;
  215. ipc = g_new0(struct ipc, 1);
  216. ipc->services = g_new0(struct service_handler, max_service_id + 1);
  217. ipc->service_max = max_service_id;
  218. ipc->path = path;
  219. ipc->size = size;
  220. ipc->notifications = notifications;
  221. ipc->cmd_io = ipc_connect(path, size, cmd_connect_cb, ipc);
  222. if (!ipc->cmd_io) {
  223. g_free(ipc->services);
  224. g_free(ipc);
  225. return NULL;
  226. }
  227. ipc->disconnect_cb = cb;
  228. ipc->disconnect_cb_data = cb_data;
  229. return ipc;
  230. }
  231. void ipc_cleanup(struct ipc *ipc)
  232. {
  233. ipc_disconnect(ipc, true);
  234. g_free(ipc->services);
  235. g_free(ipc);
  236. }
  237. static void ipc_send(int sk, uint8_t service_id, uint8_t opcode, uint16_t len,
  238. void *param, int fd)
  239. {
  240. struct msghdr msg;
  241. struct iovec iv[2];
  242. struct ipc_hdr m;
  243. char cmsgbuf[CMSG_SPACE(sizeof(int))];
  244. struct cmsghdr *cmsg;
  245. memset(&msg, 0, sizeof(msg));
  246. memset(&m, 0, sizeof(m));
  247. memset(cmsgbuf, 0, sizeof(cmsgbuf));
  248. m.service_id = service_id;
  249. m.opcode = opcode;
  250. m.len = len;
  251. iv[0].iov_base = &m;
  252. iv[0].iov_len = sizeof(m);
  253. iv[1].iov_base = param;
  254. iv[1].iov_len = len;
  255. msg.msg_iov = iv;
  256. msg.msg_iovlen = 2;
  257. if (fd >= 0) {
  258. msg.msg_control = cmsgbuf;
  259. msg.msg_controllen = sizeof(cmsgbuf);
  260. cmsg = CMSG_FIRSTHDR(&msg);
  261. cmsg->cmsg_level = SOL_SOCKET;
  262. cmsg->cmsg_type = SCM_RIGHTS;
  263. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  264. /* Initialize the payload */
  265. memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
  266. }
  267. if (sendmsg(sk, &msg, 0) < 0) {
  268. error("IPC send failed :%s", strerror(errno));
  269. /* TODO disconnect IPC here when this function becomes static */
  270. raise(SIGTERM);
  271. }
  272. }
  273. void ipc_send_rsp(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
  274. uint8_t status)
  275. {
  276. struct ipc_status s;
  277. int sk;
  278. sk = g_io_channel_unix_get_fd(ipc->cmd_io);
  279. if (status == IPC_STATUS_SUCCESS) {
  280. ipc_send(sk, service_id, opcode, 0, NULL, -1);
  281. return;
  282. }
  283. s.code = status;
  284. ipc_send(sk, service_id, IPC_OP_STATUS, sizeof(s), &s, -1);
  285. }
  286. void ipc_send_rsp_full(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
  287. uint16_t len, void *param, int fd)
  288. {
  289. ipc_send(g_io_channel_unix_get_fd(ipc->cmd_io), service_id, opcode, len,
  290. param, fd);
  291. }
  292. void ipc_send_notif(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
  293. uint16_t len, void *param)
  294. {
  295. return ipc_send_notif_with_fd(ipc, service_id, opcode, len, param, -1);
  296. }
  297. void ipc_send_notif_with_fd(struct ipc *ipc, uint8_t service_id, uint8_t opcode,
  298. uint16_t len, void *param, int fd)
  299. {
  300. if (!ipc || !ipc->notif_io)
  301. return;
  302. ipc_send(g_io_channel_unix_get_fd(ipc->notif_io), service_id, opcode,
  303. len, param, fd);
  304. }
  305. void ipc_register(struct ipc *ipc, uint8_t service,
  306. const struct ipc_handler *handlers, uint8_t size)
  307. {
  308. if (service > ipc->service_max)
  309. return;
  310. ipc->services[service].handler = handlers;
  311. ipc->services[service].size = size;
  312. }
  313. void ipc_unregister(struct ipc *ipc, uint8_t service)
  314. {
  315. if (service > ipc->service_max)
  316. return;
  317. ipc->services[service].handler = NULL;
  318. ipc->services[service].size = 0;
  319. }