GNU Linux-libre 4.19.286-gnu1
[releases.git] / tools / testing / selftests / android / ion / ionapp_export.c
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
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/time.h>
28 #include "ionutils.h"
29 #include "ipcsocket.h"
30
31
32 void print_usage(int argc, char *argv[])
33 {
34         printf("Usage: %s [-h <help>] [-i <heap id>] [-s <size in bytes>]\n",
35                 argv[0]);
36 }
37
38 int main(int argc, char *argv[])
39 {
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;
46
47         if (argc < 2) {
48                 print_usage(argc, argv);
49                 return -1;
50         }
51
52         heap_size = 0;
53         flags = 0;
54         heap_type = ION_HEAP_TYPE_SYSTEM;
55
56         while ((opt = getopt(argc, argv, "hi:s:")) != -1) {
57                 switch (opt) {
58                 case 'h':
59                         print_usage(argc, argv);
60                         exit(0);
61                         break;
62                 case 'i':
63                         heapid = atoi(optarg);
64                         switch (heapid) {
65                         case 0:
66                                 heap_type = ION_HEAP_TYPE_SYSTEM;
67                                 break;
68                         case 1:
69                                 heap_type = ION_HEAP_TYPE_SYSTEM_CONTIG;
70                                 break;
71                         default:
72                                 printf("ERROR: heap type not supported\n");
73                                 exit(1);
74                         }
75                         break;
76                 case 's':
77                         heap_size = atoi(optarg);
78                         break;
79                 default:
80                         print_usage(argc, argv);
81                         exit(1);
82                         break;
83                 }
84         }
85
86         if (heap_size <= 0) {
87                 printf("heap_size cannot be 0\n");
88                 print_usage(argc, argv);
89                 exit(1);
90         }
91
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;
96
97         /* This is server: open the socket connection first */
98         /* Here; 1 indicates server or exporter */
99         status = opensocket(&sockfd, SOCKET_NAME, 1);
100         if (status < 0) {
101                 fprintf(stderr, "<%s>: Failed opensocket.\n", __func__);
102                 goto err_socket;
103         }
104         skinfo.sockfd = sockfd;
105
106         ret = ion_export_buffer_fd(&info);
107         if (ret < 0) {
108                 fprintf(stderr, "FAILED: ion_get_buffer_fd\n");
109                 goto err_export;
110         }
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);
116
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;
121
122         ret = socket_send_fd(&skinfo);
123         if (ret < 0) {
124                 fprintf(stderr, "FAILED: socket_send_fd\n");
125                 goto err_send;
126         }
127
128 err_send:
129 err_export:
130         ion_close_buffer_fd(&info);
131
132 err_socket:
133         closesocket(sockfd, SOCKET_NAME);
134
135         return 0;
136 }