Lingfeng Yang | 88c170c | 2016-11-30 00:52:35 +0000 | [diff] [blame] | 1 | /* |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 2 | * 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 Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 19 | #ifdef HOST_BUILD |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | typedef void* QEMU_PIPE_HANDLE; |
| 24 | |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame] | 25 | #define QEMU_PIPE_INVALID_HANDLE NULL |
| 26 | |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 27 | QEMU_PIPE_HANDLE qemu_pipe_open(const char* pipeName); |
| 28 | void qemu_pipe_close(QEMU_PIPE_HANDLE pipe); |
| 29 | |
| 30 | ssize_t qemu_pipe_read(QEMU_PIPE_HANDLE pipe, void* buffer, size_t len); |
| 31 | ssize_t qemu_pipe_write(QEMU_PIPE_HANDLE pipe, const void* buffer, size_t len); |
| 32 | |
| 33 | bool qemu_pipe_try_again(); |
| 34 | bool qemu_pipe_valid(QEMU_PIPE_HANDLE pipe); |
| 35 | |
| 36 | void qemu_pipe_print_error(QEMU_PIPE_HANDLE pipe); |
| 37 | |
| 38 | #else |
| 39 | |
| 40 | typedef int QEMU_PIPE_HANDLE; |
| 41 | |
Weilun Du | 8b38e8b | 2018-10-03 10:25:21 -0700 | [diff] [blame] | 42 | #define QEMU_PIPE_INVALID_HANDLE (-1) |
| 43 | |
Lingfeng Yang | d0b2a8a | 2019-08-07 00:59:55 +0000 | [diff] [blame] | 44 | #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 Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 54 | #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 Stefani | 1cb647a | 2019-03-07 21:58:17 +0100 | [diff] [blame] | 68 | #if PLATFORM_SDK_VERSION < 26 |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 69 | #include <cutils/log.h> |
Luca Stefani | 1cb647a | 2019-03-07 21:58:17 +0100 | [diff] [blame] | 70 | #else |
| 71 | #include <log/log.h> |
| 72 | #endif |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 73 | #ifdef __ANDROID__ |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 74 | #include <sys/cdefs.h> |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 75 | #endif |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 76 | #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 Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 89 | static bool WriteFully(QEMU_PIPE_HANDLE fd, const void* data, size_t byte_count) { |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 90 | const uint8_t* p = (const uint8_t*)(data); |
| 91 | size_t remaining = byte_count; |
| 92 | while (remaining > 0) { |
Lingfeng Yang | d0b2a8a | 2019-08-07 00:59:55 +0000 | [diff] [blame] | 93 | ssize_t n = QEMU_PIPE_RETRY(write(fd, p, remaining)); |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 94 | 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 Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 123 | static __inline__ QEMU_PIPE_HANDLE |
| 124 | qemu_pipe_open(const char* pipeName) { |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 125 | char buff[256]; |
| 126 | int buffLen; |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 127 | QEMU_PIPE_HANDLE fd; |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 128 | |
| 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 Yang | d0b2a8a | 2019-08-07 00:59:55 +0000 | [diff] [blame] | 136 | 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 | } |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 140 | if (fd < 0) { |
David Reveman | 74e99bb | 2019-02-15 18:47:25 -0500 | [diff] [blame] | 141 | D("%s: Could not open " QEMU_PIPE_PATH ": %s", __FUNCTION__, strerror(errno)); |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 142 | //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 Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 156 | static __inline__ void |
| 157 | qemu_pipe_close(QEMU_PIPE_HANDLE pipe) { |
| 158 | close(pipe); |
| 159 | } |
| 160 | |
| 161 | static __inline__ ssize_t |
| 162 | qemu_pipe_read(QEMU_PIPE_HANDLE pipe, void* buffer, size_t len) { |
| 163 | return read(pipe, buffer, len); |
| 164 | } |
| 165 | |
| 166 | static __inline__ ssize_t |
| 167 | qemu_pipe_write(QEMU_PIPE_HANDLE pipe, const void* buffer, size_t len) { |
| 168 | return write(pipe, buffer, len); |
| 169 | } |
| 170 | |
| 171 | static __inline__ bool |
| 172 | qemu_pipe_try_again() { |
Lingfeng Yang | d0b2a8a | 2019-08-07 00:59:55 +0000 | [diff] [blame] | 173 | return errno == EINTR || errno == EAGAIN; |
Lingfeng Yang | e38d15c | 2018-09-24 16:24:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static __inline__ bool |
| 177 | qemu_pipe_valid(QEMU_PIPE_HANDLE pipe) { |
| 178 | return pipe >= 0; |
| 179 | } |
| 180 | |
| 181 | static __inline__ void |
| 182 | qemu_pipe_print_error(QEMU_PIPE_HANDLE pipe) { |
| 183 | ALOGE("pipe error: fd %d errno %d", pipe, errno); |
| 184 | } |
| 185 | |
| 186 | #endif // !HOST_BUILD |
| 187 | |
bohu | 104a742 | 2017-02-24 22:42:03 -0800 | [diff] [blame] | 188 | #endif /* ANDROID_INCLUDE_HARDWARE_QEMU_PIPE_H */ |