David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 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 | */ |
bohu | f66a7ee | 2017-03-29 12:19:40 -0700 | [diff] [blame^] | 16 | |
| 17 | #include "qemu_pipe.h" |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
bohu | f66a7ee | 2017-03-29 12:19:40 -0700 | [diff] [blame^] | 23 | #include <stdio.h> |
| 24 | |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 25 | |
| 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 | |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 33 | // 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. |
bohu | f66a7ee | 2017-03-29 12:19:40 -0700 | [diff] [blame^] | 53 | int qemu_pipe_open(const char* pipeName) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 54 | // Sanity check. |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 55 | if (!pipeName || memcmp(pipeName, "pipe:", 5) != 0) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 56 | 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); |
bohu | ec76870 | 2017-03-28 14:51:23 -0700 | [diff] [blame] | 69 | ssize_t ret = TEMP_FAILURE_RETRY(write(fd, pipeName, pipeNameLen + 1U)); |
| 70 | if (ret != (ssize_t)pipeNameLen + 1) { |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 71 | QEMU_PIPE_DEBUG("%s: Could not connect to %s pipe service: %s", |
| 72 | __FUNCTION__, pipeName, strerror(errno)); |
bohu | ec76870 | 2017-03-28 14:51:23 -0700 | [diff] [blame] | 73 | if (ret == 0) { |
| 74 | errno = ECONNRESET; |
| 75 | } else if (ret > 0) { |
| 76 | errno = EINVAL; |
| 77 | } |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 78 | return -1; |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 79 | } |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 80 | return fd; |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 81 | } |
| 82 | |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 83 | // 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. |
bohu | f66a7ee | 2017-03-29 12:19:40 -0700 | [diff] [blame^] | 86 | int qemu_pipe_frame_send(int fd, const void* buff, size_t len) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 87 | char header[5]; |
Miodrag Dinic | 192be1d | 2016-09-30 13:48:27 +0200 | [diff] [blame] | 88 | snprintf(header, sizeof(header), "%04zx", len); |
bohu | ec76870 | 2017-03-28 14:51:23 -0700 | [diff] [blame] | 89 | ssize_t ret = TEMP_FAILURE_RETRY(write(fd, header, 4)); |
| 90 | if (ret != 4) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 91 | QEMU_PIPE_DEBUG("Can't write qemud frame header: %s", strerror(errno)); |
| 92 | return -1; |
| 93 | } |
bohu | ec76870 | 2017-03-28 14:51:23 -0700 | [diff] [blame] | 94 | ret = TEMP_FAILURE_RETRY(write(fd, buff, len)); |
| 95 | if (ret != (ssize_t)len) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 96 | QEMU_PIPE_DEBUG("Can't write qemud frame payload: %s", strerror(errno)); |
| 97 | return -1; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
bohu | 79b3005 | 2017-03-28 14:51:14 -0700 | [diff] [blame] | 102 | // 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. |
bohu | f66a7ee | 2017-03-29 12:19:40 -0700 | [diff] [blame^] | 107 | int qemu_pipe_frame_recv(int fd, void* buff, size_t len) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 108 | char header[5]; |
bohu | ec76870 | 2017-03-28 14:51:23 -0700 | [diff] [blame] | 109 | ssize_t ret = TEMP_FAILURE_RETRY(read(fd, header, 4)); |
| 110 | if (ret != 4) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 111 | 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 | } |
bohu | ec76870 | 2017-03-28 14:51:23 -0700 | [diff] [blame] | 125 | ret = TEMP_FAILURE_RETRY(read(fd, buff, size)); |
| 126 | if (ret != (ssize_t)size) { |
David 'Digit' Turner | c7b098c | 2016-06-16 17:05:52 +0200 | [diff] [blame] | 127 | QEMU_PIPE_DEBUG("Could not read qemud frame payload: %s", |
| 128 | strerror(errno)); |
| 129 | return -1; |
| 130 | } |
| 131 | return size; |
| 132 | } |