GNU Linux-libre 4.19.286-gnu1
[releases.git] / tools / testing / selftests / android / ion / ionapp_import.c
1 /*
2  * ionapp_import.c
3  *
4  * It is a user space utility to receive android ion memory buffer fd
5  * over unix domain socket IPC that can be exported by ionapp_export.
6  * This acts like a client for ionapp_export.
7  *
8  * Copyright (C) 2017 Pintu Kumar <pintu.ping@gmail.com>
9  *
10  * This software is licensed under the terms of the GNU General Public
11  * License version 2, as published by the Free Software Foundation, and
12  * may be copied, distributed, and modified under those terms.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include "ionutils.h"
26 #include "ipcsocket.h"
27
28
29 int main(void)
30 {
31         int ret, status;
32         int sockfd, shared_fd;
33         unsigned char *map_buf;
34         unsigned long map_len;
35         struct ion_buffer_info info;
36         struct socket_info skinfo;
37
38         /* This is the client part. Here 0 means client or importer */
39         status = opensocket(&sockfd, SOCKET_NAME, 0);
40         if (status < 0) {
41                 fprintf(stderr, "No exporter exists...\n");
42                 ret = status;
43                 goto err_socket;
44         }
45
46         skinfo.sockfd = sockfd;
47
48         ret = socket_receive_fd(&skinfo);
49         if (ret < 0) {
50                 fprintf(stderr, "Failed: socket_receive_fd\n");
51                 goto err_recv;
52         }
53
54         shared_fd = skinfo.datafd;
55         printf("Received buffer fd: %d\n", shared_fd);
56         if (shared_fd <= 0) {
57                 fprintf(stderr, "ERROR: improper buf fd\n");
58                 ret = -1;
59                 goto err_fd;
60         }
61
62         memset(&info, 0, sizeof(info));
63         info.buffd = shared_fd;
64         info.buflen = ION_BUFFER_LEN;
65
66         ret = ion_import_buffer_fd(&info);
67         if (ret < 0) {
68                 fprintf(stderr, "Failed: ion_use_buffer_fd\n");
69                 goto err_import;
70         }
71
72         map_buf = info.buffer;
73         map_len = info.buflen;
74         read_buffer(map_buf, map_len);
75
76         /* Write probably new data to the same buffer again */
77         map_len = ION_BUFFER_LEN;
78         write_buffer(map_buf, map_len);
79
80 err_import:
81         ion_close_buffer_fd(&info);
82 err_fd:
83 err_recv:
84 err_socket:
85         closesocket(sockfd, SOCKET_NAME);
86
87         return ret;
88 }