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 <unistd.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdio.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__,
46 strerror(errno));
47 return -1;
48 }
49
50 // Write the pipe name, *including* the trailing zero which is necessary.
51 size_t pipeNameLen = strlen(pipeName);
52 if (WriteFully(fd, pipeName, pipeNameLen + 1U)) {
53 return fd;
54 }
55
56 // now, add 'pipe:' prefix and try again
57 // Note: host side will wait for the trailing '\0' to start
58 // service lookup.
59 const char pipe_prefix[] = "pipe:";
60 if (WriteFully(fd, pipe_prefix, strlen(pipe_prefix)) &&
61 WriteFully(fd, pipeName, pipeNameLen + 1U)) {
62 return fd;
63 }
64 QEMU_PIPE_DEBUG("%s: Could not write to %s pipe service: %s",
65 __FUNCTION__, pipeName, strerror(errno));
66 close(fd);
67 return -1;
68 }
69
qemu_pipe_frame_send(int fd,const void * buff,size_t len)70 int qemu_pipe_frame_send(int fd, const void* buff, size_t len) {
71 char header[5];
72 snprintf(header, sizeof(header), "%04zx", len);
73 if (!WriteFully(fd, header, 4)) {
74 QEMU_PIPE_DEBUG("Can't write qemud frame header: %s", strerror(errno));
75 return -1;
76 }
77 if (!WriteFully(fd, buff, len)) {
78 QEMU_PIPE_DEBUG("Can't write qemud frame payload: %s", strerror(errno));
79 return -1;
80 }
81 return 0;
82 }
83
qemu_pipe_frame_recv(int fd,void * buff,size_t len)84 int qemu_pipe_frame_recv(int fd, void* buff, size_t len) {
85 char header[5];
86 if (!ReadFully(fd, header, 4)) {
87 QEMU_PIPE_DEBUG("Can't read qemud frame header: %s", strerror(errno));
88 return -1;
89 }
90 header[4] = '\0';
91 size_t size;
92 if (sscanf(header, "%04zx", &size) != 1) {
93 QEMU_PIPE_DEBUG("Malformed qemud frame header: [%.*s]", 4, header);
94 return -1;
95 }
96 if (size > len) {
97 QEMU_PIPE_DEBUG("Oversized qemud frame (% bytes, expected <= %)", size,
98 len);
99 return -1;
100 }
101 if (!ReadFully(fd, buff, size)) {
102 QEMU_PIPE_DEBUG("Could not read qemud frame payload: %s",
103 strerror(errno));
104 return -1;
105 }
106 return size;
107 }
108