blob: a4529deb8e6d512370e38261f706784d3cc9c74d [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
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020025
26// Define QEMU_PIPE_DEBUG if you want to print error messages when an error
27// occurs during pipe operations. The macro should simply take a printf-style
28// formatting string followed by optional arguments.
29#ifndef QEMU_PIPE_DEBUG
30# define QEMU_PIPE_DEBUG(...) (void)0
31#endif
32
bohu79b30052017-03-28 14:51:14 -070033// Try to open a new Qemu fast-pipe. This function returns a file descriptor
34// that can be used to communicate with a named service managed by the
35// emulator.
36//
37// This file descriptor can be used as a standard pipe/socket descriptor.
38//
39// 'pipeName' is the name of the emulator service you want to connect to,
40// and must begin with 'pipe:' (e.g. 'pipe:camera' or 'pipe:opengles').
41//
42// On success, return a valid file descriptor, or -1/errno on failure. E.g.:
43//
44// EINVAL -> unknown/unsupported pipeName
45// ENOSYS -> fast pipes not available in this system.
46//
47// ENOSYS should never happen, except if you're trying to run within a
48// misconfigured emulator.
49//
50// You should be able to open several pipes to the same pipe service,
51// except for a few special cases (e.g. GSM modem), where EBUSY will be
52// returned if more than one client tries to connect to it.
bohuf66a7ee2017-03-29 12:19:40 -070053int qemu_pipe_open(const char* pipeName) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020054 // Sanity check.
bohu79b30052017-03-28 14:51:14 -070055 if (!pipeName || memcmp(pipeName, "pipe:", 5) != 0) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020056 errno = EINVAL;
57 return -1;
58 }
59
60 int fd = TEMP_FAILURE_RETRY(open("/dev/qemu_pipe", O_RDWR));
61 if (fd < 0) {
62 QEMU_PIPE_DEBUG("%s: Could not open /dev/qemu_pipe: %s", __FUNCTION__,
63 strerror(errno));
64 return -1;
65 }
66
67 // Write the pipe name, *including* the trailing zero which is necessary.
68 size_t pipeNameLen = strlen(pipeName);
bohuec768702017-03-28 14:51:23 -070069 ssize_t ret = TEMP_FAILURE_RETRY(write(fd, pipeName, pipeNameLen + 1U));
70 if (ret != (ssize_t)pipeNameLen + 1) {
bohu79b30052017-03-28 14:51:14 -070071 QEMU_PIPE_DEBUG("%s: Could not connect to %s pipe service: %s",
72 __FUNCTION__, pipeName, strerror(errno));
bohuec768702017-03-28 14:51:23 -070073 if (ret == 0) {
74 errno = ECONNRESET;
75 } else if (ret > 0) {
76 errno = EINVAL;
77 }
bohu79b30052017-03-28 14:51:14 -070078 return -1;
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020079 }
bohu79b30052017-03-28 14:51:14 -070080 return fd;
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020081}
82
bohu79b30052017-03-28 14:51:14 -070083// Send a framed message |buff| of |len| bytes through the |fd| descriptor.
84// This really adds a 4-hexchar prefix describing the payload size.
85// Returns 0 on success, and -1 on error.
bohuf66a7ee2017-03-29 12:19:40 -070086int qemu_pipe_frame_send(int fd, const void* buff, size_t len) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020087 char header[5];
Miodrag Dinic192be1d2016-09-30 13:48:27 +020088 snprintf(header, sizeof(header), "%04zx", len);
bohuec768702017-03-28 14:51:23 -070089 ssize_t ret = TEMP_FAILURE_RETRY(write(fd, header, 4));
90 if (ret != 4) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020091 QEMU_PIPE_DEBUG("Can't write qemud frame header: %s", strerror(errno));
92 return -1;
93 }
bohuec768702017-03-28 14:51:23 -070094 ret = TEMP_FAILURE_RETRY(write(fd, buff, len));
95 if (ret != (ssize_t)len) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +020096 QEMU_PIPE_DEBUG("Can't write qemud frame payload: %s", strerror(errno));
97 return -1;
98 }
99 return 0;
100}
101
bohu79b30052017-03-28 14:51:14 -0700102// Read a frame message from |fd|, and store it into |buff| of |len| bytes.
103// If the framed message is larger than |len|, then this returns -1 and the
104// content is lost. Otherwise, this returns the size of the message. NOTE:
105// empty messages are possible in a framed wire protocol and do not mean
106// end-of-stream.
bohuf66a7ee2017-03-29 12:19:40 -0700107int qemu_pipe_frame_recv(int fd, void* buff, size_t len) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200108 char header[5];
bohuec768702017-03-28 14:51:23 -0700109 ssize_t ret = TEMP_FAILURE_RETRY(read(fd, header, 4));
110 if (ret != 4) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200111 QEMU_PIPE_DEBUG("Can't read qemud frame header: %s", strerror(errno));
112 return -1;
113 }
114 header[4] = '\0';
115 size_t size;
116 if (sscanf(header, "%04zx", &size) != 1) {
117 QEMU_PIPE_DEBUG("Malformed qemud frame header: [%.*s]", 4, header);
118 return -1;
119 }
120 if (size > len) {
121 QEMU_PIPE_DEBUG("Oversized qemud frame (% bytes, expected <= %)", size,
122 len);
123 return -1;
124 }
bohuec768702017-03-28 14:51:23 -0700125 ret = TEMP_FAILURE_RETRY(read(fd, buff, size));
126 if (ret != (ssize_t)size) {
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200127 QEMU_PIPE_DEBUG("Could not read qemud frame payload: %s",
128 strerror(errno));
129 return -1;
130 }
131 return size;
132}