1 /*
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 <string.h>
27 #include <errno.h>
28 
29 #ifndef D
30 #  define  D(...)   do{}while(0)
31 #endif
32 
ReadFully(int fd,void * data,size_t byte_count)33 static bool ReadFully(int fd, void* data, size_t byte_count) {
34   uint8_t* p = (uint8_t*)(data);
35   size_t remaining = byte_count;
36   while (remaining > 0) {
37     ssize_t n = TEMP_FAILURE_RETRY(read(fd, p, remaining));
38     if (n <= 0) return false;
39     p += n;
40     remaining -= n;
41   }
42   return true;
43 }
44 
WriteFully(int fd,const void * data,size_t byte_count)45 static bool WriteFully(int fd, const void* data, size_t byte_count) {
46   const uint8_t* p = (const uint8_t*)(data);
47   size_t remaining = byte_count;
48   while (remaining > 0) {
49     ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining));
50     if (n == -1) return false;
51     p += n;
52     remaining -= n;
53   }
54   return true;
55 }
56 
57 /* Try to open a new Qemu fast-pipe. This function returns a file descriptor
58  * that can be used to communicate with a named service managed by the
59  * emulator.
60  *
61  * This file descriptor can be used as a standard pipe/socket descriptor.
62  *
63  * 'pipeName' is the name of the emulator service you want to connect to.
64  * E.g. 'opengles' or 'camera'.
65  *
66  * On success, return a valid file descriptor
67  * Returns -1 on error, and errno gives the error code, e.g.:
68  *
69  *    EINVAL  -> unknown/unsupported pipeName
70  *    ENOSYS  -> fast pipes not available in this system.
71  *
72  * ENOSYS should never happen, except if you're trying to run within a
73  * misconfigured emulator.
74  *
75  * You should be able to open several pipes to the same pipe service,
76  * except for a few special cases (e.g. GSM modem), where EBUSY will be
77  * returned if more than one client tries to connect to it.
78  */
79 static __inline__ int
qemu_pipe_open(const char * pipeName)80 qemu_pipe_open(const char*  pipeName)
81 {
82     char  buff[256];
83     int   buffLen;
84     int   fd, ret;
85 
86     if (pipeName == NULL || pipeName[0] == '\0') {
87         errno = EINVAL;
88         return -1;
89     }
90 
91     snprintf(buff, sizeof buff, "pipe:%s", pipeName);
92 
93     fd = TEMP_FAILURE_RETRY(open("/dev/qemu_pipe", O_RDWR));
94     if (fd < 0 && errno == ENOENT)
95         fd = TEMP_FAILURE_RETRY(open("/dev/goldfish_pipe", O_RDWR));
96     if (fd < 0) {
97         D("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__, strerror(errno));
98         //errno = ENOSYS;
99         return -1;
100     }
101 
102     buffLen = strlen(buff);
103 
104     if (!WriteFully(fd, buff, buffLen + 1)) {
105         D("%s: Could not connect to %s pipe service: %s", __FUNCTION__, pipeName, strerror(errno));
106         return -1;
107     }
108 
109     return fd;
110 }
111 
112 #endif /* ANDROID_INCLUDE_HARDWARE_QEMUD_PIPE_H */
113