ionapp_export.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * ionapp_export.c
  3. *
  4. * It is a user space utility to create and export android
  5. * ion memory buffer fd to another process using unix domain socket as IPC.
  6. * This acts like a server for ionapp_import(client).
  7. * So, this server has to be started first before the client.
  8. *
  9. * Copyright (C) 2017 Pintu Kumar <pintu.ping@gmail.com>
  10. *
  11. * This software is licensed under the terms of the GNU General Public
  12. * License version 2, as published by the Free Software Foundation, and
  13. * may be copied, distributed, and modified under those terms.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <sys/time.h>
  27. #include "ionutils.h"
  28. #include "ipcsocket.h"
  29. void print_usage(int argc, char *argv[])
  30. {
  31. printf("Usage: %s [-h <help>] [-i <heap id>] [-s <size in bytes>]\n",
  32. argv[0]);
  33. }
  34. int main(int argc, char *argv[])
  35. {
  36. int opt, ret, status, heapid;
  37. int sockfd, client_fd, shared_fd;
  38. unsigned char *map_buf;
  39. unsigned long map_len, heap_type, heap_size, flags;
  40. struct ion_buffer_info info;
  41. struct socket_info skinfo;
  42. if (argc < 2) {
  43. print_usage(argc, argv);
  44. return -1;
  45. }
  46. heap_size = 0;
  47. flags = 0;
  48. while ((opt = getopt(argc, argv, "hi:s:")) != -1) {
  49. switch (opt) {
  50. case 'h':
  51. print_usage(argc, argv);
  52. exit(0);
  53. break;
  54. case 'i':
  55. heapid = atoi(optarg);
  56. switch (heapid) {
  57. case 0:
  58. heap_type = ION_HEAP_TYPE_SYSTEM;
  59. break;
  60. case 1:
  61. heap_type = ION_HEAP_TYPE_SYSTEM_CONTIG;
  62. break;
  63. default:
  64. printf("ERROR: heap type not supported\n");
  65. exit(1);
  66. }
  67. break;
  68. case 's':
  69. heap_size = atoi(optarg);
  70. break;
  71. default:
  72. print_usage(argc, argv);
  73. exit(1);
  74. break;
  75. }
  76. }
  77. if (heap_size <= 0) {
  78. printf("heap_size cannot be 0\n");
  79. print_usage(argc, argv);
  80. exit(1);
  81. }
  82. printf("heap_type: %ld, heap_size: %ld\n", heap_type, heap_size);
  83. info.heap_type = heap_type;
  84. info.heap_size = heap_size;
  85. info.flag_type = flags;
  86. /* This is server: open the socket connection first */
  87. /* Here; 1 indicates server or exporter */
  88. status = opensocket(&sockfd, SOCKET_NAME, 1);
  89. if (status < 0) {
  90. fprintf(stderr, "<%s>: Failed opensocket.\n", __func__);
  91. goto err_socket;
  92. }
  93. skinfo.sockfd = sockfd;
  94. ret = ion_export_buffer_fd(&info);
  95. if (ret < 0) {
  96. fprintf(stderr, "FAILED: ion_get_buffer_fd\n");
  97. goto err_export;
  98. }
  99. client_fd = info.ionfd;
  100. shared_fd = info.buffd;
  101. map_buf = info.buffer;
  102. map_len = info.buflen;
  103. write_buffer(map_buf, map_len);
  104. /* share ion buf fd with other user process */
  105. printf("Sharing fd: %d, Client fd: %d\n", shared_fd, client_fd);
  106. skinfo.datafd = shared_fd;
  107. skinfo.buflen = map_len;
  108. ret = socket_send_fd(&skinfo);
  109. if (ret < 0) {
  110. fprintf(stderr, "FAILED: socket_send_fd\n");
  111. goto err_send;
  112. }
  113. err_send:
  114. err_export:
  115. ion_close_buffer_fd(&info);
  116. err_socket:
  117. closesocket(sockfd, SOCKET_NAME);
  118. return 0;
  119. }