• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SYSTEM_QEMU_PIPE_H
17 #define ANDROID_INCLUDE_SYSTEM_QEMU_PIPE_H
18 
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <errno.h>
23 
24 // Define QEMU_PIPE_DEBUG if you want to print error messages when an error
25 // occurs during pipe operations. The macro should simply take a printf-style
26 // formatting string followed by optional arguments.
27 #ifndef QEMU_PIPE_DEBUG
28 #  define  QEMU_PIPE_DEBUG(...)   (void)0
29 #endif
30 
31 // Try to open a new Qemu fast-pipe. This function returns a file descriptor
32 // that can be used to communicate with a named service managed by the
33 // emulator.
34 //
35 // This file descriptor can be used as a standard pipe/socket descriptor.
36 //
37 // 'pipeName' is the name of the emulator service you want to connect to,
38 // and must begin with 'pipe:' (e.g. 'pipe:camera' or 'pipe:opengles').
39 //
40 // On success, return a valid file descriptor, or -1/errno on failure. E.g.:
41 //
42 // EINVAL  -> unknown/unsupported pipeName
43 // ENOSYS  -> fast pipes not available in this system.
44 //
45 // ENOSYS should never happen, except if you're trying to run within a
46 // misconfigured emulator.
47 //
48 // You should be able to open several pipes to the same pipe service,
49 // except for a few special cases (e.g. GSM modem), where EBUSY will be
50 // returned if more than one client tries to connect to it.
qemu_pipe_open(const char * pipeName)51 static __inline__ int qemu_pipe_open(const char* pipeName) {
52     // Sanity check.
53     if (!pipeName || memcmp(pipeName, "pipe:", 5) != 0) {
54         errno = EINVAL;
55         return -1;
56     }
57 
58     int fd = TEMP_FAILURE_RETRY(open("/dev/qemu_pipe", O_RDWR));
59     if (fd < 0) {
60         QEMU_PIPE_DEBUG("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__,
61                         strerror(errno));
62         return -1;
63     }
64 
65     // Write the pipe name, *including* the trailing zero which is necessary.
66     size_t pipeNameLen = strlen(pipeName);
67     ssize_t ret = TEMP_FAILURE_RETRY(write(fd, pipeName, pipeNameLen + 1U));
68     if (ret != (ssize_t)pipeNameLen + 1) {
69         QEMU_PIPE_DEBUG("%s: Could not connect to %s pipe service: %s",
70                         __FUNCTION__, pipeName, strerror(errno));
71         if (ret == 0) {
72             errno = ECONNRESET;
73         } else if (ret > 0) {
74             errno = EINVAL;
75         }
76         return -1;
77     }
78     return fd;
79 }
80 
81 // Send a framed message |buff| of |len| bytes through the |fd| descriptor.
82 // This really adds a 4-hexchar prefix describing the payload size.
83 // Returns 0 on success, and -1 on error.
qemu_pipe_frame_send(int fd,const void * buff,size_t len)84 static int __inline__ qemu_pipe_frame_send(int fd,
85                                            const void* buff,
86                                            size_t len) {
87     char header[5];
88     snprintf(header, sizeof(header), "%04zx", len);
89     ssize_t ret = TEMP_FAILURE_RETRY(write(fd, header, 4));
90     if (ret != 4) {
91         QEMU_PIPE_DEBUG("Can't write qemud frame header: %s", strerror(errno));
92         return -1;
93     }
94     ret = TEMP_FAILURE_RETRY(write(fd, buff, len));
95     if (ret != (ssize_t)len) {
96         QEMU_PIPE_DEBUG("Can't write qemud frame payload: %s", strerror(errno));
97         return -1;
98     }
99     return 0;
100 }
101 
102 // Read a frame message from |fd|, and store it into |buff| of |len| bytes.
103 // If the framed message is larger than |len|, then this returns -1 and the
104 // content is lost. Otherwise, this returns the size of the message. NOTE:
105 // empty messages are possible in a framed wire protocol and do not mean
106 // end-of-stream.
qemu_pipe_frame_recv(int fd,void * buff,size_t len)107 static int __inline__ qemu_pipe_frame_recv(int fd, void* buff, size_t len) {
108     char header[5];
109     ssize_t ret = TEMP_FAILURE_RETRY(read(fd, header, 4));
110     if (ret != 4) {
111         QEMU_PIPE_DEBUG("Can't read qemud frame header: %s", strerror(errno));
112         return -1;
113     }
114     header[4] = '\0';
115     size_t size;
116     if (sscanf(header, "%04zx", &size) != 1) {
117         QEMU_PIPE_DEBUG("Malformed qemud frame header: [%.*s]", 4, header);
118         return -1;
119     }
120     if (size > len) {
121         QEMU_PIPE_DEBUG("Oversized qemud frame (% bytes, expected <= %)", size,
122                         len);
123         return -1;
124     }
125     ret = TEMP_FAILURE_RETRY(read(fd, buff, size));
126     if (ret != (ssize_t)size) {
127         QEMU_PIPE_DEBUG("Could not read qemud frame payload: %s",
128                         strerror(errno));
129         return -1;
130     }
131     return size;
132 }
133 
134 #endif /* ANDROID_INCLUDE_HARDWARE_QEMUD_PIPE_H */
135