4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qapi/error.h"
28 #include "qemu/main-loop.h"
29 #include "qemu/module.h"
30 #include "qemu/option.h"
31 #include "chardev/char.h"
34 #include "chardev/char-win.h"
36 #include "chardev/char-fd.h"
43 static int win_chr_pipe_init(Chardev *chr, const char *filename,
46 WinChardev *s = WIN_CHARDEV(chr);
54 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
56 error_setg(errp, "Failed CreateEvent");
59 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
61 error_setg(errp, "Failed CreateEvent");
65 openname = g_strdup_printf("\\\\.\\pipe\\%s", filename);
66 s->file = CreateNamedPipe(openname,
67 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
68 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
70 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
72 if (s->file == INVALID_HANDLE_VALUE) {
73 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
78 ZeroMemory(&ov, sizeof(ov));
79 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
80 ret = ConnectNamedPipe(s->file, &ov);
82 error_setg(errp, "Failed ConnectNamedPipe");
86 ret = GetOverlappedResult(s->file, &ov, &size, TRUE);
88 error_setg(errp, "Failed GetOverlappedResult");
90 CloseHandle(ov.hEvent);
97 CloseHandle(ov.hEvent);
100 qemu_add_polling_cb(win_chr_pipe_poll, chr);
107 static void qemu_chr_open_pipe(Chardev *chr,
108 ChardevBackend *backend,
112 ChardevHostdev *opts = backend->u.pipe.data;
113 const char *filename = opts->device;
115 if (win_chr_pipe_init(chr, filename, errp) < 0) {
122 static void qemu_chr_open_pipe(Chardev *chr,
123 ChardevBackend *backend,
127 ChardevHostdev *opts = backend->u.pipe.data;
131 const char *filename = opts->device;
133 filename_in = g_strdup_printf("%s.in", filename);
134 filename_out = g_strdup_printf("%s.out", filename);
135 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
136 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
138 g_free(filename_out);
139 if (fd_in < 0 || fd_out < 0) {
146 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
148 error_setg_file_open(errp, errno, filename);
152 qemu_chr_open_fd(chr, fd_in, fd_out);
157 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
160 const char *device = qemu_opt_get(opts, "path");
163 if (device == NULL) {
164 error_setg(errp, "chardev: pipe: no device path given");
167 backend->type = CHARDEV_BACKEND_KIND_PIPE;
168 dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1);
169 qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev));
170 dev->device = g_strdup(device);
173 static void char_pipe_class_init(ObjectClass *oc, void *data)
175 ChardevClass *cc = CHARDEV_CLASS(oc);
177 cc->parse = qemu_chr_parse_pipe;
178 cc->open = qemu_chr_open_pipe;
181 static const TypeInfo char_pipe_type_info = {
182 .name = TYPE_CHARDEV_PIPE,
184 .parent = TYPE_CHARDEV_WIN,
186 .parent = TYPE_CHARDEV_FD,
188 .class_init = char_pipe_class_init,
191 static void register_types(void)
193 type_register_static(&char_pipe_type_info);
196 type_init(register_types);