blob: 68febf87d09cb1e3587ce526df14a5f3e082362c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
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
19//#include <sys/cdefs.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <sys/mman.h>
23#include <pthread.h> /* for pthread_once() */
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27
28#ifndef D
29# define D(...) do{}while(0)
30#endif
31
32/*
33 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
34 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
35 * not already defined, then define it here.
36 */
37#ifndef TEMP_FAILURE_RETRY
38/* Used to retry syscalls that can return EINTR. */
39#define TEMP_FAILURE_RETRY(exp) ({ \
40 typeof (exp) _rc; \
41 do { \
42 _rc = (exp); \
43 } while (_rc == -1 && errno == EINTR); \
44 _rc; })
45#endif
46
47/* Try to open a new Qemu fast-pipe. This function returns a file descriptor
48 * that can be used to communicate with a named service managed by the
49 * emulator.
50 *
51 * This file descriptor can be used as a standard pipe/socket descriptor.
52 *
53 * 'pipeName' is the name of the emulator service you want to connect to.
54 * E.g. 'opengles' or 'camera'.
55 *
56 * On success, return a valid file descriptor
57 * Returns -1 on error, and errno gives the error code, e.g.:
58 *
59 * EINVAL -> unknown/unsupported pipeName
60 * ENOSYS -> fast pipes not available in this system.
61 *
62 * ENOSYS should never happen, except if you're trying to run within a
63 * misconfigured emulator.
64 *
65 * You should be able to open several pipes to the same pipe service,
66 * except for a few special cases (e.g. GSM modem), where EBUSY will be
67 * returned if more than one client tries to connect to it.
68 */
69static __inline__ int
70qemu_pipe_open(const char* pipeName)
71{
72 char buff[256];
73 int buffLen;
74 int fd, ret;
75
76 if (pipeName == NULL || pipeName[0] == '\0') {
77 errno = EINVAL;
78 return -1;
79 }
80
81 snprintf(buff, sizeof buff, "pipe:%s", pipeName);
82
83 fd = open("/dev/qemu_pipe", O_RDWR);
84 if (fd < 0) {
85 D("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__, strerror(errno));
86 //errno = ENOSYS;
87 return -1;
88 }
89
90 buffLen = strlen(buff);
91
92 ret = TEMP_FAILURE_RETRY(write(fd, buff, buffLen+1));
93 if (ret != buffLen+1) {
94 D("%s: Could not connect to %s pipe service: %s", __FUNCTION__, pipeName, strerror(errno));
95 if (ret == 0) {
96 errno = ECONNRESET;
97 } else if (ret > 0) {
98 errno = EINVAL;
99 }
100 return -1;
101 }
102
103 return fd;
104}
105
106#endif /* ANDROID_INCLUDE_HARDWARE_QEMUD_PIPE_H */