blob: ca3b795782450bef942c09720b58bd15dfef3579 [file] [log] [blame]
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +02001/*
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 */
bohuf66a7ee2017-03-29 12:19:40 -070016
17#include "qemu_pipe.h"
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020018
19#include <unistd.h>
20#include <fcntl.h>
21#include <string.h>
22#include <errno.h>
bohuf66a7ee2017-03-29 12:19:40 -070023#include <stdio.h>
24
bohuee13ff12017-03-29 12:19:49 -070025#include <android-base/file.h>
26
27using android::base::ReadFully;
28using android::base::WriteFully;
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020029
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
bohu79b30052017-03-28 14:51:14 -070037// Try to open a new Qemu fast-pipe. This function returns a file descriptor
38// that can be used to communicate with a named service managed by the
39// emulator.
40//
41// This file descriptor can be used as a standard pipe/socket descriptor.
42//
43// 'pipeName' is the name of the emulator service you want to connect to,
44// and must begin with 'pipe:' (e.g. 'pipe:camera' or 'pipe:opengles').
45//
46// On success, return a valid file descriptor, or -1/errno on failure. E.g.:
47//
48// EINVAL -> unknown/unsupported pipeName
49// ENOSYS -> fast pipes not available in this system.
50//
51// ENOSYS should never happen, except if you're trying to run within a
52// misconfigured emulator.
53//
54// You should be able to open several pipes to the same pipe service,
55// except for a few special cases (e.g. GSM modem), where EBUSY will be
56// returned if more than one client tries to connect to it.
bohuf66a7ee2017-03-29 12:19:40 -070057int qemu_pipe_open(const char* pipeName) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020058 // Sanity check.
bohu79b30052017-03-28 14:51:14 -070059 if (!pipeName || memcmp(pipeName, "pipe:", 5) != 0) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020060 errno = EINVAL;
61 return -1;
62 }
63
64 int fd = TEMP_FAILURE_RETRY(open("/dev/qemu_pipe", O_RDWR));
65 if (fd < 0) {
66 QEMU_PIPE_DEBUG("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__,
67 strerror(errno));
68 return -1;
69 }
70
71 // Write the pipe name, *including* the trailing zero which is necessary.
72 size_t pipeNameLen = strlen(pipeName);
bohuee13ff12017-03-29 12:19:49 -070073 if (!WriteFully(fd, pipeName, pipeNameLen + 1U)) {
bohu79b30052017-03-28 14:51:14 -070074 QEMU_PIPE_DEBUG("%s: Could not connect to %s pipe service: %s",
75 __FUNCTION__, pipeName, strerror(errno));
bohuee13ff12017-03-29 12:19:49 -070076 close(fd);
bohu79b30052017-03-28 14:51:14 -070077 return -1;
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020078 }
bohu79b30052017-03-28 14:51:14 -070079 return fd;
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020080}
81
bohu79b30052017-03-28 14:51:14 -070082// Send a framed message |buff| of |len| bytes through the |fd| descriptor.
83// This really adds a 4-hexchar prefix describing the payload size.
84// Returns 0 on success, and -1 on error.
bohuf66a7ee2017-03-29 12:19:40 -070085int qemu_pipe_frame_send(int fd, const void* buff, size_t len) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020086 char header[5];
Miodrag Dinic192be1d2016-09-30 13:48:27 +020087 snprintf(header, sizeof(header), "%04zx", len);
bohuee13ff12017-03-29 12:19:49 -070088 if (!WriteFully(fd, header, 4)) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020089 QEMU_PIPE_DEBUG("Can't write qemud frame header: %s", strerror(errno));
90 return -1;
91 }
bohuee13ff12017-03-29 12:19:49 -070092 if (!WriteFully(fd, buff, len)) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020093 QEMU_PIPE_DEBUG("Can't write qemud frame payload: %s", strerror(errno));
94 return -1;
95 }
96 return 0;
97}
98
bohu79b30052017-03-28 14:51:14 -070099// Read a frame message from |fd|, and store it into |buff| of |len| bytes.
100// If the framed message is larger than |len|, then this returns -1 and the
101// content is lost. Otherwise, this returns the size of the message. NOTE:
102// empty messages are possible in a framed wire protocol and do not mean
103// end-of-stream.
bohuf66a7ee2017-03-29 12:19:40 -0700104int qemu_pipe_frame_recv(int fd, void* buff, size_t len) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200105 char header[5];
bohuee13ff12017-03-29 12:19:49 -0700106 if (!ReadFully(fd, header, 4)) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200107 QEMU_PIPE_DEBUG("Can't read qemud frame header: %s", strerror(errno));
108 return -1;
109 }
110 header[4] = '\0';
111 size_t size;
112 if (sscanf(header, "%04zx", &size) != 1) {
113 QEMU_PIPE_DEBUG("Malformed qemud frame header: [%.*s]", 4, header);
114 return -1;
115 }
116 if (size > len) {
117 QEMU_PIPE_DEBUG("Oversized qemud frame (% bytes, expected <= %)", size,
118 len);
119 return -1;
120 }
bohuee13ff12017-03-29 12:19:49 -0700121 if (!ReadFully(fd, buff, size)) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200122 QEMU_PIPE_DEBUG("Could not read qemud frame payload: %s",
123 strerror(errno));
124 return -1;
125 }
126 return size;
127}