• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 #include <atomic>
18 #include <errno.h>
19 #include <log/log.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <linux/vm_sockets.h>
28 #include <qemu_pipe_bp.h>
29 
30 namespace {
31 enum class VsockPort {
32     Data = 5000,
33     Ping = 5001,
34 };
35 
36 std::atomic<bool> gVsockAvailable = false;
37 
is_graphics_pipe(const char * name)38 bool is_graphics_pipe(const char* name) {
39     if (!strcmp(name, "opengles")) { return true; }
40     if (!strcmp(name, "GLProcessPipe")) { return true; }
41     if (!strcmp(name, "refcount")) { return true; }
42 
43     return false;
44 }
45 
open_verbose_path(const char * name,const int flags)46 int open_verbose_path(const char* name, const int flags) {
47     const int fd = QEMU_PIPE_RETRY(open(name, flags));
48     if (fd < 0) {
49         ALOGE("%s:%d: Could not open '%s': %s",
50               __func__, __LINE__, name, strerror(errno));
51     }
52     return fd;
53 }
54 
open_verbose_vsock(const VsockPort port,const int flags)55 int open_verbose_vsock(const VsockPort port, const int flags) {
56     const int fd = QEMU_PIPE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0));
57     if (fd < 0) {
58         // it is ok if socket(AF_VSOCK, ...) fails - vsock might be unsupported yet
59         return -1;
60     }
61 
62     struct sockaddr_vm sa;
63     memset(&sa, 0, sizeof(sa));
64     sa.svm_family = AF_VSOCK;
65     sa.svm_port = static_cast<int>(port);
66     sa.svm_cid = VMADDR_CID_HOST;
67 
68     int r;
69 
70     r = QEMU_PIPE_RETRY(connect(fd,
71                                 reinterpret_cast<const struct sockaddr*>(&sa),
72                                 sizeof(sa)));
73     if (r < 0) {
74         // it is ok if connect(fd, &sa, ...) fails - vsock might be unsupported yet
75         close(fd);
76         return -1;
77     }
78 
79     if (flags) {
80         const int oldFlags = QEMU_PIPE_RETRY(fcntl(fd, F_GETFL, 0));
81         if (oldFlags < 0) {
82             ALOGE("%s:%d fcntl(fd=%d, F_GETFL) failed with '%s' (%d)",
83                   __func__, __LINE__, fd, strerror(errno), errno);
84             close(fd);
85             return -1;
86         }
87 
88         const int newFlags = oldFlags | flags;
89 
90         r = QEMU_PIPE_RETRY(fcntl(fd, F_SETFL, newFlags));
91         if (r < 0) {
92             ALOGE("%s:%d fcntl(fd=%d, F_SETFL, flags=0x%X) failed with '%s' (%d)",
93                   __func__, __LINE__, fd, newFlags, strerror(errno), errno);
94             close(fd);
95             return -1;
96         }
97     }
98 
99     return fd;
100 }
101 
open_verbose(const char * pipeName,const int flags)102 int open_verbose(const char *pipeName, const int flags) {
103     int fd;
104 
105     // We can't use vsock for grapshics for security reasons,
106     // virtio-gpu should be used instead.
107     if (!is_graphics_pipe(pipeName)) {
108         fd = open_verbose_vsock(VsockPort::Data, flags);
109         if (fd >= 0) {
110             gVsockAvailable = true;
111             return fd;
112         }
113     }
114 
115     fd = open_verbose_path("/dev/goldfish_pipe", flags);
116     if (fd >= 0) {
117         return fd;
118     }
119 
120     ALOGE("%s:%d: both vsock and goldfish_pipe paths failed",
121           __func__, __LINE__);
122     return -1;
123 }
124 
vsock_ping()125 void vsock_ping() {
126     const int fd = open_verbose_vsock(VsockPort::Ping, 0);
127     if (fd >= 0) {
128         ALOGE("%s:%d open_verbose_vsock(kVsockPingPort) is expected to fail, "
129               "but it succeeded, fd=%d", __func__, __LINE__, fd);
130         close(fd);
131     }
132 }
133 
134 }  // namespace
135 
136 extern "C" {
137 
qemu_pipe_open_ns(const char * ns,const char * pipeName,int flags)138 int qemu_pipe_open_ns(const char* ns, const char* pipeName, int flags) {
139     if (pipeName == NULL || pipeName[0] == '\0') {
140         errno = EINVAL;
141         return -1;
142     }
143 
144     const int fd = open_verbose(pipeName, flags);
145     if (fd < 0) {
146         return fd;
147     }
148 
149     char buf[256];
150     int bufLen;
151     if (ns) {
152         bufLen = snprintf(buf, sizeof(buf), "pipe:%s:%s", ns, pipeName);
153     } else {
154         bufLen = snprintf(buf, sizeof(buf), "pipe:%s", pipeName);
155     }
156 
157     if (qemu_pipe_write_fully(fd, buf, bufLen + 1)) {
158         ALOGE("%s:%d: Could not connect to the '%s' service: %s",
159               __func__, __LINE__, buf, strerror(errno));
160         close(fd);
161         return -1;
162     }
163 
164     return fd;
165 }
166 
qemu_pipe_open(const char * pipeName)167 int qemu_pipe_open(const char* pipeName) {
168     return qemu_pipe_open_ns(NULL, pipeName, O_RDWR | O_NONBLOCK);
169 }
170 
qemu_pipe_close(int pipe)171 void qemu_pipe_close(int pipe) {
172     close(pipe);
173 }
174 
qemu_pipe_read(int pipe,void * buffer,int size)175 int qemu_pipe_read(int pipe, void* buffer, int size) {
176     return read(pipe, buffer, size);
177 }
178 
qemu_pipe_write(int pipe,const void * buffer,int size)179 int qemu_pipe_write(int pipe, const void* buffer, int size) {
180     return write(pipe, buffer, size);
181 }
182 
qemu_pipe_try_again(int ret)183 int qemu_pipe_try_again(int ret) {
184     if (ret >= 0) {
185         return 0;
186     }
187 
188     switch (errno) {
189     case EAGAIN:
190         if (gVsockAvailable) {
191             vsock_ping();
192             errno = EAGAIN;
193         }
194         return 1;
195 
196     case EINTR:
197         return 1;
198 
199     default:
200         return 0;
201     }
202 }
203 
qemu_pipe_print_error(int pipe)204 void qemu_pipe_print_error(int pipe) {
205     ALOGE("pipe error: fd %d errno %d", pipe, errno);
206 }
207 
208 }  // extern "C"
209