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.
9 * Copyright (C) 2017 Pintu Kumar <pintu.ping@gmail.com>
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.
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.
29 #include "ipcsocket.h"
32 void print_usage(int argc, char *argv[])
34 printf("Usage: %s [-h <help>] [-i <heap id>] [-s <size in bytes>]\n",
38 int main(int argc, char *argv[])
40 int opt, ret, status, heapid;
41 int sockfd, client_fd, shared_fd;
42 unsigned char *map_buf;
43 unsigned long map_len, heap_type, heap_size, flags;
44 struct ion_buffer_info info;
45 struct socket_info skinfo;
48 print_usage(argc, argv);
54 heap_type = ION_HEAP_TYPE_SYSTEM;
56 while ((opt = getopt(argc, argv, "hi:s:")) != -1) {
59 print_usage(argc, argv);
63 heapid = atoi(optarg);
66 heap_type = ION_HEAP_TYPE_SYSTEM;
69 heap_type = ION_HEAP_TYPE_SYSTEM_CONTIG;
72 printf("ERROR: heap type not supported\n");
77 heap_size = atoi(optarg);
80 print_usage(argc, argv);
87 printf("heap_size cannot be 0\n");
88 print_usage(argc, argv);
92 printf("heap_type: %ld, heap_size: %ld\n", heap_type, heap_size);
93 info.heap_type = heap_type;
94 info.heap_size = heap_size;
95 info.flag_type = flags;
97 /* This is server: open the socket connection first */
98 /* Here; 1 indicates server or exporter */
99 status = opensocket(&sockfd, SOCKET_NAME, 1);
101 fprintf(stderr, "<%s>: Failed opensocket.\n", __func__);
104 skinfo.sockfd = sockfd;
106 ret = ion_export_buffer_fd(&info);
108 fprintf(stderr, "FAILED: ion_get_buffer_fd\n");
111 client_fd = info.ionfd;
112 shared_fd = info.buffd;
113 map_buf = info.buffer;
114 map_len = info.buflen;
115 write_buffer(map_buf, map_len);
117 /* share ion buf fd with other user process */
118 printf("Sharing fd: %d, Client fd: %d\n", shared_fd, client_fd);
119 skinfo.datafd = shared_fd;
120 skinfo.buflen = map_len;
122 ret = socket_send_fd(&skinfo);
124 fprintf(stderr, "FAILED: socket_send_fd\n");
130 ion_close_buffer_fd(&info);
133 closesocket(sockfd, SOCKET_NAME);