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 "qapi/error.h"
27 #include "qemu/option.h"
28 #include "chardev/char.h"
31 #include "chardev/char-win.h"
33 #include "chardev/char-fd.h"
40 static int win_chr_pipe_init(Chardev
*chr
, const char *filename
,
43 WinChardev
*s
= WIN_CHARDEV(chr
);
51 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
53 error_setg(errp
, "Failed CreateEvent");
56 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
58 error_setg(errp
, "Failed CreateEvent");
62 openname
= g_strdup_printf("\\\\.\\pipe\\%s", filename
);
63 s
->file
= CreateNamedPipe(openname
,
64 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
65 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
67 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
69 if (s
->file
== INVALID_HANDLE_VALUE
) {
70 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
75 ZeroMemory(&ov
, sizeof(ov
));
76 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
77 ret
= ConnectNamedPipe(s
->file
, &ov
);
79 error_setg(errp
, "Failed ConnectNamedPipe");
83 ret
= GetOverlappedResult(s
->file
, &ov
, &size
, TRUE
);
85 error_setg(errp
, "Failed GetOverlappedResult");
87 CloseHandle(ov
.hEvent
);
94 CloseHandle(ov
.hEvent
);
97 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
104 static void qemu_chr_open_pipe(Chardev
*chr
,
105 ChardevBackend
*backend
,
109 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
110 const char *filename
= opts
->device
;
112 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
119 static void qemu_chr_open_pipe(Chardev
*chr
,
120 ChardevBackend
*backend
,
124 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
128 const char *filename
= opts
->device
;
130 filename_in
= g_strdup_printf("%s.in", filename
);
131 filename_out
= g_strdup_printf("%s.out", filename
);
132 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
133 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
135 g_free(filename_out
);
136 if (fd_in
< 0 || fd_out
< 0) {
143 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
145 error_setg_file_open(errp
, errno
, filename
);
149 qemu_chr_open_fd(chr
, fd_in
, fd_out
);
154 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
157 const char *device
= qemu_opt_get(opts
, "path");
160 if (device
== NULL
) {
161 error_setg(errp
, "chardev: pipe: no device path given");
164 backend
->type
= CHARDEV_BACKEND_KIND_PIPE
;
165 dev
= backend
->u
.pipe
.data
= g_new0(ChardevHostdev
, 1);
166 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(dev
));
167 dev
->device
= g_strdup(device
);
170 static void char_pipe_class_init(ObjectClass
*oc
, void *data
)
172 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
174 cc
->parse
= qemu_chr_parse_pipe
;
175 cc
->open
= qemu_chr_open_pipe
;
178 static const TypeInfo char_pipe_type_info
= {
179 .name
= TYPE_CHARDEV_PIPE
,
181 .parent
= TYPE_CHARDEV_WIN
,
183 .parent
= TYPE_CHARDEV_FD
,
185 .class_init
= char_pipe_class_init
,
188 static void register_types(void)
190 type_register_static(&char_pipe_type_info
);
193 type_init(register_types
);