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 #include "QemuPipeStream.h"
17 #include <qemu_pipe_bp.h>
18 
19 #include <cutils/log.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 
26 static const size_t kReadSize = 512 * 1024;
27 static const size_t kWriteOffset = kReadSize;
28 
QemuPipeStream(size_t bufSize)29 QemuPipeStream::QemuPipeStream(size_t bufSize) :
30     IOStream(bufSize),
31     m_sock((QEMU_PIPE_HANDLE)(-1)),
32     m_bufsize(bufSize),
33     m_buf(NULL),
34     m_read(0),
35     m_readLeft(0)
36 {
37 }
38 
QemuPipeStream(QEMU_PIPE_HANDLE sock,size_t bufSize)39 QemuPipeStream::QemuPipeStream(QEMU_PIPE_HANDLE sock, size_t bufSize) :
40     IOStream(bufSize),
41     m_sock(sock),
42     m_bufsize(bufSize),
43     m_buf(NULL),
44     m_read(0),
45     m_readLeft(0)
46 {
47 }
48 
~QemuPipeStream()49 QemuPipeStream::~QemuPipeStream()
50 {
51     if (valid()) {
52         flush();
53         qemu_pipe_close(m_sock);
54     }
55     if (m_buf != NULL) {
56         free(m_buf);
57     }
58 }
59 
60 
connect(void)61 int QemuPipeStream::connect(void)
62 {
63     m_sock = qemu_pipe_open("opengles");
64     if (!valid()) {
65         ALOGE("%s: failed to connect to opengles pipe", __FUNCTION__);
66         qemu_pipe_print_error(m_sock);
67         return -1;
68     }
69     return 0;
70 }
71 
allocBuffer(size_t minSize)72 void *QemuPipeStream::allocBuffer(size_t minSize)
73 {
74     // Add dedicated read buffer space at the front of the buffer.
75     minSize += kReadSize;
76 
77     size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize);
78     if (!m_buf) {
79         m_buf = (unsigned char *)malloc(allocSize);
80     }
81     else if (m_bufsize < allocSize) {
82         unsigned char *p = (unsigned char *)realloc(m_buf, allocSize);
83         if (p != NULL) {
84             m_buf = p;
85             m_bufsize = allocSize;
86         } else {
87             ERR("realloc (%zu) failed\n", allocSize);
88             free(m_buf);
89             m_buf = NULL;
90             m_bufsize = 0;
91         }
92     }
93 
94     return m_buf + kWriteOffset;
95 };
96 
commitBuffer(size_t size)97 int QemuPipeStream::commitBuffer(size_t size)
98 {
99     if (size == 0) return 0;
100     return writeFully(m_buf + kWriteOffset, size);
101 }
102 
writeFully(const void * buf,size_t len)103 int QemuPipeStream::writeFully(const void *buf, size_t len)
104 {
105     return qemu_pipe_write_fully(m_sock, buf, len);
106 }
107 
getSocket() const108 QEMU_PIPE_HANDLE QemuPipeStream::getSocket() const {
109     return m_sock;
110 }
111 
readFully(void * buf,size_t len)112 const unsigned char *QemuPipeStream::readFully(void *buf, size_t len)
113 {
114     return commitBufferAndReadFully(0, buf, len);
115 }
116 
commitBufferAndReadFully(size_t writeSize,void * userReadBufPtr,size_t totalReadSize)117 const unsigned char *QemuPipeStream::commitBufferAndReadFully(size_t writeSize, void *userReadBufPtr, size_t totalReadSize) {
118 
119     unsigned char* userReadBuf = static_cast<unsigned char*>(userReadBufPtr);
120 
121     if (!valid()) return NULL;
122 
123     if (!userReadBuf) {
124         if (totalReadSize > 0) {
125             ALOGE("QemuPipeStream::commitBufferAndReadFully failed, userReadBuf=NULL, totalReadSize %zu, lethal"
126                     " error, exiting.", totalReadSize);
127             abort();
128         }
129         if (!writeSize) {
130             return NULL;
131         }
132     }
133 
134     // Advance buffered read if not yet consumed.
135     size_t remaining = totalReadSize;
136     size_t bufferedReadSize = m_readLeft < remaining ? m_readLeft : remaining;
137     if (bufferedReadSize) {
138         memcpy(userReadBuf, m_buf + (m_read - m_readLeft), bufferedReadSize);
139         remaining -= bufferedReadSize;
140         m_readLeft -= bufferedReadSize;
141     }
142 
143     // Early out if nothing left to do.
144     if (!writeSize && !remaining) {
145         return userReadBuf;
146     }
147 
148     writeFully(m_buf + kWriteOffset, writeSize);
149 
150     // Now done writing. Early out if no reading left to do.
151     if (!remaining) {
152         return userReadBuf;
153     }
154 
155     // Read up to kReadSize bytes if all buffered read has been consumed.
156     size_t maxRead = m_readLeft ? 0 : kReadSize;
157 
158     ssize_t actual = 0;
159 
160     if (maxRead) {
161         actual = qemu_pipe_read(m_sock, m_buf, maxRead);
162         // Updated buffered read size.
163         if (actual > 0) {
164             m_read = m_readLeft = actual;
165         }
166 
167         if (actual == 0) {
168             ALOGD("%s: end of pipe", __FUNCTION__);
169             return NULL;
170         }
171     }
172 
173     // Consume buffered read and read more if necessary.
174     while (remaining) {
175         bufferedReadSize = m_readLeft < remaining ? m_readLeft : remaining;
176         if (bufferedReadSize) {
177             memcpy(userReadBuf + (totalReadSize - remaining),
178                    m_buf + (m_read - m_readLeft),
179                    bufferedReadSize);
180             remaining -= bufferedReadSize;
181             m_readLeft -= bufferedReadSize;
182             continue;
183         }
184 
185         actual = qemu_pipe_read(m_sock, m_buf, kReadSize);
186 
187         if (actual == 0) {
188             ALOGD("%s: Failed reading from pipe: %d", __FUNCTION__,  errno);
189             return NULL;
190         }
191 
192         if (actual > 0) {
193             m_read = m_readLeft = actual;
194             continue;
195         }
196 
197         if (!qemu_pipe_try_again(actual)) {
198             ALOGD("%s: Error reading from pipe: %d", __FUNCTION__, errno);
199             return NULL;
200         }
201     }
202 
203     return userReadBuf;
204 }
205 
read(void * buf,size_t * inout_len)206 const unsigned char *QemuPipeStream::read( void *buf, size_t *inout_len)
207 {
208     //DBG(">> QemuPipeStream::read %d\n", *inout_len);
209     if (!valid()) return NULL;
210     if (!buf) {
211       ERR("QemuPipeStream::read failed, buf=NULL");
212       return NULL;  // do not allow NULL buf in that implementation
213     }
214 
215     int n = recv(buf, *inout_len);
216 
217     if (n > 0) {
218         *inout_len = n;
219         return (const unsigned char *)buf;
220     }
221 
222     //DBG("<< QemuPipeStream::read %d\n", *inout_len);
223     return NULL;
224 }
225 
recv(void * buf,size_t len)226 int QemuPipeStream::recv(void *buf, size_t len)
227 {
228     if (!valid()) return int(ERR_INVALID_SOCKET);
229     char* p = (char *)buf;
230     int ret = 0;
231     while(len > 0) {
232         int res = qemu_pipe_read(m_sock, p, len);
233         if (res > 0) {
234             p += res;
235             ret += res;
236             len -= res;
237             continue;
238         }
239         if (res == 0) { /* EOF */
240              break;
241         }
242         if (qemu_pipe_try_again(res)) {
243             continue;
244         }
245 
246         /* A real error */
247         if (ret == 0)
248             ret = -1;
249         break;
250     }
251     return ret;
252 }
253