| 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qapi/error.h" |
| 27 | #include "qemu/main-loop.h" |
| 28 | #include "qemu/module.h" |
| 29 | #include "qemu/option.h" |
| 30 | #include "chardev/char.h" |
| 31 | |
| 32 | #ifdef _WIN32 |
| 33 | #include "chardev/char-win.h" |
| 34 | #else |
| 35 | #include "chardev/char-fd.h" |
| 36 | #endif |
| 37 | |
| 38 | #ifdef _WIN32 |
| 39 | #define MAXCONNECT 1 |
| 40 | #define NTIMEOUT 5000 |
| 41 | |
| 42 | static int win_chr_pipe_init(Chardev *chr, const char *filename, |
| 43 | Error **errp) |
| 44 | { |
| 45 | WinChardev *s = WIN_CHARDEV(chr); |
| 46 | OVERLAPPED ov; |
| 47 | int ret; |
| 48 | DWORD size; |
| 49 | char *openname; |
| 50 | |
| 51 | s->fpipe = TRUE; |
| 52 | |
| 53 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 54 | if (!s->hsend) { |
| 55 | error_setg(errp, "Failed CreateEvent"); |
| 56 | goto fail; |
| 57 | } |
| 58 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 59 | if (!s->hrecv) { |
| 60 | error_setg(errp, "Failed CreateEvent"); |
| 61 | goto fail; |
| 62 | } |
| 63 | |
| 64 | openname = g_strdup_printf("\\\\.\\pipe\\%s", filename); |
| 65 | s->file = CreateNamedPipe(openname, |
| 66 | PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 67 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 68 | PIPE_WAIT, |
| 69 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 70 | g_free(openname); |
| 71 | if (s->file == INVALID_HANDLE_VALUE) { |
| 72 | error_setg_win32(errp, GetLastError(), "Failed CreateNamedPipe"); |
| 73 | s->file = NULL; |
| 74 | goto fail; |
| 75 | } |
| 76 | |
| 77 | ZeroMemory(&ov, sizeof(ov)); |
| 78 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 79 | ret = ConnectNamedPipe(s->file, &ov); |
| 80 | if (ret) { |
| 81 | error_setg(errp, "Failed ConnectNamedPipe"); |
| 82 | goto fail; |
| 83 | } |
| 84 | |
| 85 | ret = GetOverlappedResult(s->file, &ov, &size, TRUE); |
| 86 | if (!ret) { |
| 87 | error_setg(errp, "Failed GetOverlappedResult"); |
| 88 | if (ov.hEvent) { |
| 89 | CloseHandle(ov.hEvent); |
| 90 | ov.hEvent = NULL; |
| 91 | } |
| 92 | goto fail; |
| 93 | } |
| 94 | |
| 95 | if (ov.hEvent) { |
| 96 | CloseHandle(ov.hEvent); |
| 97 | ov.hEvent = NULL; |
| 98 | } |
| 99 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
| 100 | return 0; |
| 101 | |
| 102 | fail: |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | static bool pipe_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) |
| 107 | { |
| 108 | ChardevHostdev *opts = backend->u.pipe.data; |
| 109 | const char *filename = opts->device; |
| 110 | |
| 111 | if (win_chr_pipe_init(chr, filename, errp) < 0) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | #else |
| 120 | |
| 121 | static bool pipe_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) |
| 122 | { |
| 123 | ChardevHostdev *opts = backend->u.pipe.data; |
| 124 | int fd_in, fd_out; |
| 125 | char *filename_in; |
| 126 | char *filename_out; |
| 127 | const char *filename = opts->device; |
| 128 | |
| 129 | filename_in = g_strdup_printf("%s.in", filename); |
| 130 | filename_out = g_strdup_printf("%s.out", filename); |
| 131 | fd_in = RETRY_ON_EINTR(qemu_open_old(filename_in, O_RDWR | O_BINARY)); |
| 132 | fd_out = RETRY_ON_EINTR(qemu_open_old(filename_out, O_RDWR | O_BINARY)); |
| 133 | g_free(filename_in); |
| 134 | g_free(filename_out); |
| 135 | if (fd_in < 0 || fd_out < 0) { |
| 136 | if (fd_in >= 0) { |
| 137 | close(fd_in); |
| 138 | } |
| 139 | if (fd_out >= 0) { |
| 140 | close(fd_out); |
| 141 | } |
| 142 | fd_in = fd_out = RETRY_ON_EINTR( |
| 143 | qemu_open_old(filename, O_RDWR | O_BINARY) |
| 144 | ); |
| 145 | if (fd_in < 0) { |
| 146 | error_setg_file_open(errp, errno, filename); |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (!qemu_chr_open_fd(chr, fd_in, fd_out, errp)) { |
| 152 | close(fd_in); |
| 153 | if (fd_out != fd_in) { |
| 154 | close(fd_out); |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | qemu_chr_be_event(chr, CHR_EVENT_OPENED); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | #endif /* !_WIN32 */ |
| 164 | |
| 165 | static void pipe_chr_parse(QemuOpts *opts, ChardevBackend *backend, |
| 166 | Error **errp) |
| 167 | { |
| 168 | const char *device = qemu_opt_get(opts, "path"); |
| 169 | ChardevHostdev *dev; |
| 170 | |
| 171 | if (device == NULL) { |
| 172 | error_setg(errp, "chardev: pipe: no device path given"); |
| 173 | return; |
| 174 | } |
| 175 | backend->type = CHARDEV_BACKEND_KIND_PIPE; |
| 176 | dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1); |
| 177 | qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev)); |
| 178 | dev->device = g_strdup(device); |
| 179 | } |
| 180 | |
| 181 | static void char_pipe_class_init(ObjectClass *oc, const void *data) |
| 182 | { |
| 183 | ChardevClass *cc = CHARDEV_CLASS(oc); |
| 184 | |
| 185 | cc->chr_parse = pipe_chr_parse; |
| 186 | cc->chr_open = pipe_chr_open; |
| 187 | } |
| 188 | |
| 189 | static const TypeInfo char_pipe_type_info = { |
| 190 | .name = TYPE_CHARDEV_PIPE, |
| 191 | #ifdef _WIN32 |
| 192 | .parent = TYPE_CHARDEV_WIN, |
| 193 | #else |
| 194 | .parent = TYPE_CHARDEV_FD, |
| 195 | #endif |
| 196 | .class_init = char_pipe_class_init, |
| 197 | }; |
| 198 | |
| 199 | static void register_types(void) |
| 200 | { |
| 201 | type_register_static(&char_pipe_type_info); |
| 202 | } |
| 203 | |
| 204 | type_init(register_types); |