blob: 82c4a9444def9aba79547a2344d35f40437451bf [file] [log] [blame]
Lingfeng Yang88c170c2016-11-30 00:52:35 +00001/*
bohu104a7422017-02-24 22:42:03 -08002 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H
17#define ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H
18
Lingfeng Yange38d15c2018-09-24 16:24:01 -070019#ifdef HOST_BUILD
20
21#include <sys/types.h>
22
23typedef void* QEMU_PIPE_HANDLE;
24
Weilun Du8b38e8b2018-10-03 10:25:21 -070025#define QEMU_PIPE_INVALID_HANDLE NULL
26
Lingfeng Yange38d15c2018-09-24 16:24:01 -070027QEMU_PIPE_HANDLE qemu_pipe_open(const char* pipeName);
28void qemu_pipe_close(QEMU_PIPE_HANDLE pipe);
29
30ssize_t qemu_pipe_read(QEMU_PIPE_HANDLE pipe, void* buffer, size_t len);
31ssize_t qemu_pipe_write(QEMU_PIPE_HANDLE pipe, const void* buffer, size_t len);
32
33bool qemu_pipe_try_again();
34bool qemu_pipe_valid(QEMU_PIPE_HANDLE pipe);
35
36void qemu_pipe_print_error(QEMU_PIPE_HANDLE pipe);
37
38#else
39
40typedef int QEMU_PIPE_HANDLE;
41
Weilun Du8b38e8b2018-10-03 10:25:21 -070042#define QEMU_PIPE_INVALID_HANDLE (-1)
43
Lingfeng Yangd0b2a8a2019-08-07 00:59:55 +000044#ifndef QEMU_PIPE_RETRY
45#define QEMU_PIPE_RETRY(exp) ({ \
46 __typeof__(exp) _rc; \
47 do { \
48 _rc = (exp); \
49 } while (_rc == -1 && (errno == EINTR || errno == EAGAIN)); \
50 _rc; }) \
51
52#endif
53
David Reveman74e99bb2019-02-15 18:47:25 -050054#ifndef QEMU_PIPE_PATH
55#define QEMU_PIPE_PATH "/dev/qemu_pipe"
56#endif
57
58#ifndef TEMP_FAILURE_RETRY
59#define TEMP_FAILURE_RETRY(exp) ({ \
60 __typeof__(exp) _rc; \
61 do { \
62 _rc = (exp); \
63 } while (_rc == -1 && errno == EINTR); \
64 _rc; })
65#include <stdint.h>
66#endif
67
Luca Stefani1cb647a2019-03-07 21:58:17 +010068#if PLATFORM_SDK_VERSION < 26
Lingfeng Yange38d15c2018-09-24 16:24:01 -070069#include <cutils/log.h>
Luca Stefani1cb647a2019-03-07 21:58:17 +010070#else
71#include <log/log.h>
72#endif
David Reveman74e99bb2019-02-15 18:47:25 -050073#ifdef __ANDROID__
bohu104a7422017-02-24 22:42:03 -080074#include <sys/cdefs.h>
David Reveman74e99bb2019-02-15 18:47:25 -050075#endif
bohu104a7422017-02-24 22:42:03 -080076#include <unistd.h>
77#include <fcntl.h>
78#include <sys/mman.h>
79#include <pthread.h> /* for pthread_once() */
80#include <stdlib.h>
81#include <stdio.h>
82#include <string.h>
83#include <errno.h>
84
85#ifndef D
86# define D(...) do{}while(0)
87#endif
88
Lingfeng Yange38d15c2018-09-24 16:24:01 -070089static bool WriteFully(QEMU_PIPE_HANDLE fd, const void* data, size_t byte_count) {
bohu104a7422017-02-24 22:42:03 -080090 const uint8_t* p = (const uint8_t*)(data);
91 size_t remaining = byte_count;
92 while (remaining > 0) {
Lingfeng Yangd0b2a8a2019-08-07 00:59:55 +000093 ssize_t n = QEMU_PIPE_RETRY(write(fd, p, remaining));
bohu104a7422017-02-24 22:42:03 -080094 if (n == -1) return false;
95 p += n;
96 remaining -= n;
97 }
98 return true;
99}
100
101/* Try to open a new Qemu fast-pipe. This function returns a file descriptor
102 * that can be used to communicate with a named service managed by the
103 * emulator.
104 *
105 * This file descriptor can be used as a standard pipe/socket descriptor.
106 *
107 * 'pipeName' is the name of the emulator service you want to connect to.
108 * E.g. 'opengles' or 'camera'.
109 *
110 * On success, return a valid file descriptor
111 * Returns -1 on error, and errno gives the error code, e.g.:
112 *
113 * EINVAL -> unknown/unsupported pipeName
114 * ENOSYS -> fast pipes not available in this system.
115 *
116 * ENOSYS should never happen, except if you're trying to run within a
117 * misconfigured emulator.
118 *
119 * You should be able to open several pipes to the same pipe service,
120 * except for a few special cases (e.g. GSM modem), where EBUSY will be
121 * returned if more than one client tries to connect to it.
122 */
Lingfeng Yange38d15c2018-09-24 16:24:01 -0700123static __inline__ QEMU_PIPE_HANDLE
124qemu_pipe_open(const char* pipeName) {
bohu104a7422017-02-24 22:42:03 -0800125 char buff[256];
126 int buffLen;
Lingfeng Yange38d15c2018-09-24 16:24:01 -0700127 QEMU_PIPE_HANDLE fd;
bohu104a7422017-02-24 22:42:03 -0800128
129 if (pipeName == NULL || pipeName[0] == '\0') {
130 errno = EINVAL;
131 return -1;
132 }
133
134 snprintf(buff, sizeof buff, "pipe:%s", pipeName);
135
Lingfeng Yangd0b2a8a2019-08-07 00:59:55 +0000136 fd = QEMU_PIPE_RETRY(open(QEMU_PIPE_PATH, O_RDWR | O_NONBLOCK));
137 if (fd < 0 && errno == ENOENT) {
138 fd = QEMU_PIPE_RETRY(open("/dev/goldfish_pipe", O_RDWR | O_NONBLOCK));
139 }
bohu104a7422017-02-24 22:42:03 -0800140 if (fd < 0) {
David Reveman74e99bb2019-02-15 18:47:25 -0500141 D("%s: Could not open " QEMU_PIPE_PATH ": %s", __FUNCTION__, strerror(errno));
bohu104a7422017-02-24 22:42:03 -0800142 //errno = ENOSYS;
143 return -1;
144 }
145
146 buffLen = strlen(buff);
147
148 if (!WriteFully(fd, buff, buffLen + 1)) {
149 D("%s: Could not connect to %s pipe service: %s", __FUNCTION__, pipeName, strerror(errno));
150 return -1;
151 }
152
153 return fd;
154}
155
Lingfeng Yange38d15c2018-09-24 16:24:01 -0700156static __inline__ void
157qemu_pipe_close(QEMU_PIPE_HANDLE pipe) {
158 close(pipe);
159}
160
161static __inline__ ssize_t
162qemu_pipe_read(QEMU_PIPE_HANDLE pipe, void* buffer, size_t len) {
163 return read(pipe, buffer, len);
164}
165
166static __inline__ ssize_t
167qemu_pipe_write(QEMU_PIPE_HANDLE pipe, const void* buffer, size_t len) {
168 return write(pipe, buffer, len);
169}
170
171static __inline__ bool
172qemu_pipe_try_again() {
Lingfeng Yangd0b2a8a2019-08-07 00:59:55 +0000173 return errno == EINTR || errno == EAGAIN;
Lingfeng Yange38d15c2018-09-24 16:24:01 -0700174}
175
176static __inline__ bool
177qemu_pipe_valid(QEMU_PIPE_HANDLE pipe) {
178 return pipe >= 0;
179}
180
181static __inline__ void
182qemu_pipe_print_error(QEMU_PIPE_HANDLE pipe) {
183 ALOGE("pipe error: fd %d errno %d", pipe, errno);
184}
185
186#endif // !HOST_BUILD
187
bohu104a7422017-02-24 22:42:03 -0800188#endif /* ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H */