ionapp_import.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * ionapp_import.c
  3. *
  4. * It is a user space utility to receive android ion memory buffer fd
  5. * over unix domain socket IPC that can be exported by ionapp_export.
  6. * This acts like a client for ionapp_export.
  7. *
  8. * Copyright (C) 2017 Pintu Kumar <pintu.ping@gmail.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include "ionutils.h"
  25. #include "ipcsocket.h"
  26. int main(void)
  27. {
  28. int ret, status;
  29. int sockfd, shared_fd;
  30. unsigned char *map_buf;
  31. unsigned long map_len;
  32. struct ion_buffer_info info;
  33. struct socket_info skinfo;
  34. /* This is the client part. Here 0 means client or importer */
  35. status = opensocket(&sockfd, SOCKET_NAME, 0);
  36. if (status < 0) {
  37. fprintf(stderr, "No exporter exists...\n");
  38. ret = status;
  39. goto err_socket;
  40. }
  41. skinfo.sockfd = sockfd;
  42. ret = socket_receive_fd(&skinfo);
  43. if (ret < 0) {
  44. fprintf(stderr, "Failed: socket_receive_fd\n");
  45. goto err_recv;
  46. }
  47. shared_fd = skinfo.datafd;
  48. printf("Received buffer fd: %d\n", shared_fd);
  49. if (shared_fd <= 0) {
  50. fprintf(stderr, "ERROR: improper buf fd\n");
  51. ret = -1;
  52. goto err_fd;
  53. }
  54. memset(&info, 0, sizeof(info));
  55. info.buffd = shared_fd;
  56. info.buflen = ION_BUFFER_LEN;
  57. ret = ion_import_buffer_fd(&info);
  58. if (ret < 0) {
  59. fprintf(stderr, "Failed: ion_use_buffer_fd\n");
  60. goto err_import;
  61. }
  62. map_buf = info.buffer;
  63. map_len = info.buflen;
  64. read_buffer(map_buf, map_len);
  65. /* Write probably new data to the same buffer again */
  66. map_len = ION_BUFFER_LEN;
  67. write_buffer(map_buf, map_len);
  68. err_import:
  69. ion_close_buffer_fd(&info);
  70. err_fd:
  71. err_recv:
  72. err_socket:
  73. closesocket(sockfd, SOCKET_NAME);
  74. return ret;
  75. }