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 
46 // Make sure that `e` is not zero. Assumes `def` is not zero.
checkErr(const int e,const int def)47 int checkErr(const int e, const int def) {
48     return e ? e : def;
49 }
50 
open_verbose_path(const char * name,const int flags)51 int open_verbose_path(const char* name, const int flags) {
52     const int fd = QEMU_PIPE_RETRY(open(name, flags));
53     if (fd < 0) {
54         const int e = errno;
55         ALOGE("%s:%d: Could not open '%s': %s",
56               __func__, __LINE__, name, strerror(e));
57         return -checkErr(e, EINVAL);
58     }
59     return fd;
60 }
61 
open_verbose_vsock(const VsockPort port,const int flags)62 int open_verbose_vsock(const VsockPort port, const int flags) {
63     const int fd = QEMU_PIPE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0));
64     if (fd < 0) {
65         // it is ok if socket(AF_VSOCK, ...) fails - vsock might be unsupported yet
66         return -checkErr(errno, EINVAL);
67     }
68 
69     struct sockaddr_vm sa;
70     memset(&sa, 0, sizeof(sa));
71     sa.svm_family = AF_VSOCK;
72     sa.svm_port = static_cast<int>(port);
73     sa.svm_cid = VMADDR_CID_HOST;
74 
75     int r;
76 
77     r = QEMU_PIPE_RETRY(connect(fd,
78                                 reinterpret_cast<const struct sockaddr*>(&sa),
79                                 sizeof(sa)));
80     if (r < 0) {
81         // it is ok if connect(fd, &sa, ...) fails - vsock might be unsupported yet
82         close(fd);
83         return -checkErr(errno, EINVAL);
84     }
85 
86     if (flags) {
87         const int oldFlags = QEMU_PIPE_RETRY(fcntl(fd, F_GETFL, 0));
88         if (oldFlags < 0) {
89             ALOGE("%s:%d fcntl(fd=%d, F_GETFL) failed with '%s' (%d)",
90                   __func__, __LINE__, fd, strerror(errno), errno);
91             close(fd);
92             return -checkErr(errno, EINVAL);
93         }
94 
95         const int newFlags = oldFlags | flags;
96 
97         r = QEMU_PIPE_RETRY(fcntl(fd, F_SETFL, newFlags));
98         if (r < 0) {
99             ALOGE("%s:%d fcntl(fd=%d, F_SETFL, flags=0x%X) failed with '%s' (%d)",
100                   __func__, __LINE__, fd, newFlags, strerror(errno), errno);
101             close(fd);
102             return -checkErr(errno, EINVAL);
103         }
104     }
105 
106     return fd;
107 }
108 
open_verbose(const char * pipeName,const int flags)109 int open_verbose(const char *pipeName, const int flags) {
110     int fd;
111 
112     // We can't use vsock for grapshics for security reasons,
113     // virtio-gpu should be used instead.
114     if (!is_graphics_pipe(pipeName)) {
115         fd = open_verbose_vsock(VsockPort::Data, flags);
116         if (fd >= 0) {
117             gVsockAvailable = true;
118         }
119         return fd;
120     }
121 
122     fd = open_verbose_path("/dev/goldfish_pipe_dprctd", flags);
123     if (fd >= 0) {
124         return fd;
125     }
126 
127     ALOGE("%s:%d: both vsock and goldfish_pipe paths failed",
128           __func__, __LINE__);
129     return fd;
130 }
131 
vsock_ping()132 void vsock_ping() {
133     const int fd = open_verbose_vsock(VsockPort::Ping, 0);
134     if (fd >= 0) {
135         ALOGE("%s:%d open_verbose_vsock(kVsockPingPort) is expected to fail, "
136               "but it succeeded, fd=%d", __func__, __LINE__, fd);
137         close(fd);
138     }
139 }
140 }  // namespace
141 
142 extern "C" {
143 
qemu_pipe_open_ns(const char * ns,const char * pipeName,int flags)144 int qemu_pipe_open_ns(const char* ns, const char* pipeName, int flags) {
145     if (pipeName == NULL || pipeName[0] == '\0') {
146         errno = EINVAL;
147         return -EINVAL;
148     }
149 
150     const int fd = open_verbose(pipeName, flags);
151     if (fd < 0) {
152         return fd;
153     }
154 
155     char buf[256];
156     int bufLen;
157     if (ns) {
158         bufLen = snprintf(buf, sizeof(buf), "pipe:%s:%s", ns, pipeName);
159     } else {
160         bufLen = snprintf(buf, sizeof(buf), "pipe:%s", pipeName);
161     }
162 
163     const int e = qemu_pipe_write_fully(fd, buf, bufLen + 1);
164     if (e < 0) {
165         ALOGE("%s:%d: Could not connect to the '%s' service: %s",
166               __func__, __LINE__, buf, strerror(-e));
167         close(fd);
168         return e;
169     }
170 
171     return fd;
172 }
173 
qemu_pipe_open(const char * pipeName)174 int qemu_pipe_open(const char* pipeName) {
175     return qemu_pipe_open_ns(NULL, pipeName, O_RDWR | O_NONBLOCK);
176 }
177 
qemu_pipe_close(int pipe)178 void qemu_pipe_close(int pipe) {
179     close(pipe);
180 }
181 
qemu_pipe_read(int pipe,void * buffer,int size)182 int qemu_pipe_read(int pipe, void* buffer, int size) {
183     const ssize_t r = read(pipe, buffer, size);
184     return (r >= 0) ? r : -checkErr(errno, EIO);
185 }
186 
qemu_pipe_write(int pipe,const void * buffer,int size)187 int qemu_pipe_write(int pipe, const void* buffer, int size) {
188     const ssize_t r = write(pipe, buffer, size);
189     return (r >= 0) ? r : -checkErr(errno, EIO);
190 }
191 
qemu_pipe_try_again(int ret)192 int qemu_pipe_try_again(int ret) {
193     if (ret >= 0) {
194         return 0;
195     }
196 
197     switch (errno) {
198     case EAGAIN:
199         if (gVsockAvailable) {
200             vsock_ping();
201             errno = EAGAIN;
202         }
203         return 1;
204 
205     case EINTR:
206         return 1;
207 
208     default:
209         return 0;
210     }
211 }
212 
qemu_pipe_print_error(int pipe)213 void qemu_pipe_print_error(int pipe) {
214     ALOGE("pipe error: fd %d errno %d", pipe, errno);
215 }
216 
217 }  // extern "C"
218