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
);
55 while ((opt
= getopt(argc
, argv
, "hi:s:")) != -1) {
58 print_usage(argc
, argv
);
62 heapid
= atoi(optarg
);
65 heap_type
= ION_HEAP_TYPE_SYSTEM
;
68 heap_type
= ION_HEAP_TYPE_SYSTEM_CONTIG
;
71 printf("ERROR: heap type not supported\n");
76 heap_size
= atoi(optarg
);
79 print_usage(argc
, argv
);
86 printf("heap_size cannot be 0\n");
87 print_usage(argc
, argv
);
91 printf("heap_type: %ld, heap_size: %ld\n", heap_type
, heap_size
);
92 info
.heap_type
= heap_type
;
93 info
.heap_size
= heap_size
;
94 info
.flag_type
= flags
;
96 /* This is server: open the socket connection first */
97 /* Here; 1 indicates server or exporter */
98 status
= opensocket(&sockfd
, SOCKET_NAME
, 1);
100 fprintf(stderr
, "<%s>: Failed opensocket.\n", __func__
);
103 skinfo
.sockfd
= sockfd
;
105 ret
= ion_export_buffer_fd(&info
);
107 fprintf(stderr
, "FAILED: ion_get_buffer_fd\n");
110 client_fd
= info
.ionfd
;
111 shared_fd
= info
.buffd
;
112 map_buf
= info
.buffer
;
113 map_len
= info
.buflen
;
114 write_buffer(map_buf
, map_len
);
116 /* share ion buf fd with other user process */
117 printf("Sharing fd: %d, Client fd: %d\n", shared_fd
, client_fd
);
118 skinfo
.datafd
= shared_fd
;
119 skinfo
.buflen
= map_len
;
121 ret
= socket_send_fd(&skinfo
);
123 fprintf(stderr
, "FAILED: socket_send_fd\n");
129 ion_close_buffer_fd(&info
);
132 closesocket(sockfd
, SOCKET_NAME
);