ionapp_export.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. heap_type = ION_HEAP_TYPE_SYSTEM;
  49. while ((opt = getopt(argc, argv, "hi:s:")) != -1) {
  50. switch (opt) {
  51. case 'h':
  52. print_usage(argc, argv);
  53. exit(0);
  54. break;
  55. case 'i':
  56. heapid = atoi(optarg);
  57. switch (heapid) {
  58. case 0:
  59. heap_type = ION_HEAP_TYPE_SYSTEM;
  60. break;
  61. case 1:
  62. heap_type = ION_HEAP_TYPE_SYSTEM_CONTIG;
  63. break;
  64. default:
  65. printf("ERROR: heap type not supported\n");
  66. exit(1);
  67. }
  68. break;
  69. case 's':
  70. heap_size = atoi(optarg);
  71. break;
  72. default:
  73. print_usage(argc, argv);
  74. exit(1);
  75. break;
  76. }
  77. }
  78. if (heap_size <= 0) {
  79. printf("heap_size cannot be 0\n");
  80. print_usage(argc, argv);
  81. exit(1);
  82. }
  83. printf("heap_type: %ld, heap_size: %ld\n", heap_type, heap_size);
  84. info.heap_type = heap_type;
  85. info.heap_size = heap_size;
  86. info.flag_type = flags;
  87. /* This is server: open the socket connection first */
  88. /* Here; 1 indicates server or exporter */
  89. status = opensocket(&sockfd, SOCKET_NAME, 1);
  90. if (status < 0) {
  91. fprintf(stderr, "<%s>: Failed opensocket.\n", __func__);
  92. goto err_socket;
  93. }
  94. skinfo.sockfd = sockfd;
  95. ret = ion_export_buffer_fd(&info);
  96. if (ret < 0) {
  97. fprintf(stderr, "FAILED: ion_get_buffer_fd\n");
  98. goto err_export;
  99. }
  100. client_fd = info.ionfd;
  101. shared_fd = info.buffd;
  102. map_buf = info.buffer;
  103. map_len = info.buflen;
  104. write_buffer(map_buf, map_len);
  105. /* share ion buf fd with other user process */
  106. printf("Sharing fd: %d, Client fd: %d\n", shared_fd, client_fd);
  107. skinfo.datafd = shared_fd;
  108. skinfo.buflen = map_len;
  109. ret = socket_send_fd(&skinfo);
  110. if (ret < 0) {
  111. fprintf(stderr, "FAILED: socket_send_fd\n");
  112. goto err_send;
  113. }
  114. err_send:
  115. err_export:
  116. ion_close_buffer_fd(&info);
  117. err_socket:
  118. closesocket(sockfd, SOCKET_NAME);
  119. return 0;
  120. }