1 // SPDX-License-Identifier: GPL-2.0-only
5 * It is a user space utility to create and export android
6 * ion memory buffer fd to another process using unix domain socket as IPC.
7 * This acts like a server for ionapp_import(client).
8 * So, this server has to be started first before the client.
10 * Copyright (C) 2017 Pintu Kumar <pintu.ping@gmail.com>
20 #include "ipcsocket.h"
23 void print_usage(int argc
, char *argv
[])
25 printf("Usage: %s [-h <help>] [-i <heap id>] [-s <size in bytes>]\n",
29 int main(int argc
, char *argv
[])
31 int opt
, ret
, status
, heapid
;
32 int sockfd
, client_fd
, shared_fd
;
33 unsigned char *map_buf
;
34 unsigned long map_len
, heap_type
, heap_size
, flags
;
35 struct ion_buffer_info info
;
36 struct socket_info skinfo
;
39 print_usage(argc
, argv
);
45 heap_type
= ION_HEAP_TYPE_SYSTEM
;
47 while ((opt
= getopt(argc
, argv
, "hi:s:")) != -1) {
50 print_usage(argc
, argv
);
54 heapid
= atoi(optarg
);
57 heap_type
= ION_HEAP_TYPE_SYSTEM
;
60 heap_type
= ION_HEAP_TYPE_SYSTEM_CONTIG
;
63 printf("ERROR: heap type not supported\n");
68 heap_size
= atoi(optarg
);
71 print_usage(argc
, argv
);
78 printf("heap_size cannot be 0\n");
79 print_usage(argc
, argv
);
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
;
88 /* This is server: open the socket connection first */
89 /* Here; 1 indicates server or exporter */
90 status
= opensocket(&sockfd
, SOCKET_NAME
, 1);
92 fprintf(stderr
, "<%s>: Failed opensocket.\n", __func__
);
95 skinfo
.sockfd
= sockfd
;
97 ret
= ion_export_buffer_fd(&info
);
99 fprintf(stderr
, "FAILED: ion_get_buffer_fd\n");
102 client_fd
= info
.ionfd
;
103 shared_fd
= info
.buffd
;
104 map_buf
= info
.buffer
;
105 map_len
= info
.buflen
;
106 write_buffer(map_buf
, map_len
);
108 /* share ion buf fd with other user process */
109 printf("Sharing fd: %d, Client fd: %d\n", shared_fd
, client_fd
);
110 skinfo
.datafd
= shared_fd
;
111 skinfo
.buflen
= map_len
;
113 ret
= socket_send_fd(&skinfo
);
115 fprintf(stderr
, "FAILED: socket_send_fd\n");
121 ion_close_buffer_fd(&info
);
124 closesocket(sockfd
, SOCKET_NAME
);