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 #include "qemu/osdep.h" 25 #include "qapi/error.h" 26 #include "chardev/char.h" 27 28 #ifdef _WIN32 29 #include "chardev/char-win.h" 30 #else 31 #include "chardev/char-fd.h" 32 #endif 33 34 #ifdef _WIN32 35 #define MAXCONNECT 1 36 #define NTIMEOUT 5000 37 38 static int win_chr_pipe_init(Chardev *chr, const char *filename, 39 Error **errp) 40 { 41 WinChardev *s = WIN_CHARDEV(chr); 42 OVERLAPPED ov; 43 int ret; 44 DWORD size; 45 char *openname; 46 47 s->fpipe = TRUE; 48 49 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); 50 if (!s->hsend) { 51 error_setg(errp, "Failed CreateEvent"); 52 goto fail; 53 } 54 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); 55 if (!s->hrecv) { 56 error_setg(errp, "Failed CreateEvent"); 57 goto fail; 58 } 59 60 openname = g_strdup_printf("\\\\.\\pipe\\%s", filename); 61 s->file = CreateNamedPipe(openname, 62 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 63 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | 64 PIPE_WAIT, 65 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); 66 g_free(openname); 67 if (s->file == INVALID_HANDLE_VALUE) { 68 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError()); 69 s->file = NULL; 70 goto fail; 71 } 72 73 ZeroMemory(&ov, sizeof(ov)); 74 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 75 ret = ConnectNamedPipe(s->file, &ov); 76 if (ret) { 77 error_setg(errp, "Failed ConnectNamedPipe"); 78 goto fail; 79 } 80 81 ret = GetOverlappedResult(s->file, &ov, &size, TRUE); 82 if (!ret) { 83 error_setg(errp, "Failed GetOverlappedResult"); 84 if (ov.hEvent) { 85 CloseHandle(ov.hEvent); 86 ov.hEvent = NULL; 87 } 88 goto fail; 89 } 90 91 if (ov.hEvent) { 92 CloseHandle(ov.hEvent); 93 ov.hEvent = NULL; 94 } 95 qemu_add_polling_cb(win_chr_pipe_poll, chr); 96 return 0; 97 98 fail: 99 return -1; 100 } 101 102 static void qemu_chr_open_pipe(Chardev *chr, 103 ChardevBackend *backend, 104 bool *be_opened, 105 Error **errp) 106 { 107 ChardevHostdev *opts = backend->u.pipe.data; 108 const char *filename = opts->device; 109 110 if (win_chr_pipe_init(chr, filename, errp) < 0) { 111 return; 112 } 113 } 114 115 #else 116 117 static void qemu_chr_open_pipe(Chardev *chr, 118 ChardevBackend *backend, 119 bool *be_opened, 120 Error **errp) 121 { 122 ChardevHostdev *opts = backend->u.pipe.data; 123 int fd_in, fd_out; 124 char *filename_in; 125 char *filename_out; 126 const char *filename = opts->device; 127 128 filename_in = g_strdup_printf("%s.in", filename); 129 filename_out = g_strdup_printf("%s.out", filename); 130 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); 131 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); 132 g_free(filename_in); 133 g_free(filename_out); 134 if (fd_in < 0 || fd_out < 0) { 135 if (fd_in >= 0) { 136 close(fd_in); 137 } 138 if (fd_out >= 0) { 139 close(fd_out); 140 } 141 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); 142 if (fd_in < 0) { 143 error_setg_file_open(errp, errno, filename); 144 return; 145 } 146 } 147 qemu_chr_open_fd(chr, fd_in, fd_out); 148 } 149 150 #endif /* !_WIN32 */ 151 152 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, 153 Error **errp) 154 { 155 const char *device = qemu_opt_get(opts, "path"); 156 ChardevHostdev *dev; 157 158 if (device == NULL) { 159 error_setg(errp, "chardev: pipe: no device path given"); 160 return; 161 } 162 backend->type = CHARDEV_BACKEND_KIND_PIPE; 163 dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1); 164 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev)); 165 dev->device = g_strdup(device); 166 } 167 168 static void char_pipe_class_init(ObjectClass *oc, void *data) 169 { 170 ChardevClass *cc = CHARDEV_CLASS(oc); 171 172 cc->parse = qemu_chr_parse_pipe; 173 cc->open = qemu_chr_open_pipe; 174 } 175 176 static const TypeInfo char_pipe_type_info = { 177 .name = TYPE_CHARDEV_PIPE, 178 #ifdef _WIN32 179 .parent = TYPE_CHARDEV_WIN, 180 #else 181 .parent = TYPE_CHARDEV_FD, 182 #endif 183 .class_init = char_pipe_class_init, 184 }; 185 186 static void register_types(void) 187 { 188 type_register_static(&char_pipe_type_info); 189 } 190 191 type_init(register_types); 192