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
17 #include "qemu_pipe.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <android-base/file.h>
26
27 using android::base::ReadFully;
28 using android::base::WriteFully;
29
30 // Define QEMU_PIPE_DEBUG if you want to print error messages when an error
31 // occurs during pipe operations. The macro should simply take a printf-style
32 // formatting string followed by optional arguments.
33 #ifndef QEMU_PIPE_DEBUG
34 #define QEMU_PIPE_DEBUG(...) (void)0
35 #endif
36
qemu_pipe_open(const char * pipeName)37 int qemu_pipe_open(const char* pipeName) {
38 if (!pipeName) {
39 errno = EINVAL;
40 return -1;
41 }
42
43 int fd = TEMP_FAILURE_RETRY(open("/dev/qemu_pipe", O_RDWR));
44 if (fd < 0) {
45 QEMU_PIPE_DEBUG("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__, strerror(errno));
46 return -1;
47 }
48
49 // Write the pipe name, *including* the trailing zero which is necessary.
50 size_t pipeNameLen = strlen(pipeName);
51 if (WriteFully(fd, pipeName, pipeNameLen + 1U)) {
52 return fd;
53 }
54
55 // now, add 'pipe:' prefix and try again
56 // Note: host side will wait for the trailing '\0' to start
57 // service lookup.
58 const char pipe_prefix[] = "pipe:";
59 if (WriteFully(fd, pipe_prefix, strlen(pipe_prefix)) &&
60 WriteFully(fd, pipeName, pipeNameLen + 1U)) {
61 return fd;
62 }
63 QEMU_PIPE_DEBUG("%s: Could not write to %s pipe service: %s", __FUNCTION__, pipeName,
64 strerror(errno));
65 close(fd);
66 return -1;
67 }
68
qemu_pipe_frame_send(int fd,const void * buff,size_t len)69 int qemu_pipe_frame_send(int fd, const void* buff, size_t len) {
70 char header[5];
71 snprintf(header, sizeof(header), "%04zx", len);
72 if (!WriteFully(fd, header, 4)) {
73 QEMU_PIPE_DEBUG("Can't write qemud frame header: %s", strerror(errno));
74 return -1;
75 }
76 if (!WriteFully(fd, buff, len)) {
77 QEMU_PIPE_DEBUG("Can't write qemud frame payload: %s", strerror(errno));
78 return -1;
79 }
80 return 0;
81 }
82
qemu_pipe_frame_recv(int fd,void * buff,size_t len)83 int qemu_pipe_frame_recv(int fd, void* buff, size_t len) {
84 char header[5];
85 if (!ReadFully(fd, header, 4)) {
86 QEMU_PIPE_DEBUG("Can't read qemud frame header: %s", strerror(errno));
87 return -1;
88 }
89 header[4] = '\0';
90 size_t size;
91 if (sscanf(header, "%04zx", &size) != 1) {
92 QEMU_PIPE_DEBUG("Malformed qemud frame header: [%.*s]", 4, header);
93 return -1;
94 }
95 if (size > len) {
96 QEMU_PIPE_DEBUG("Oversized qemud frame (% bytes, expected <= %)", size, len);
97 return -1;
98 }
99 if (!ReadFully(fd, buff, size)) {
100 QEMU_PIPE_DEBUG("Could not read qemud frame payload: %s", strerror(errno));
101 return -1;
102 }
103 return size;
104 }
105