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/module.h"
29 #include "qemu/option.h"
30 #include "chardev/char.h"
33 #include "chardev/char-win.h"
35 #include "chardev/char-fd.h"
42 static int win_chr_pipe_init(Chardev
*chr
, const char *filename
,
45 WinChardev
*s
= WIN_CHARDEV(chr
);
53 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
55 error_setg(errp
, "Failed CreateEvent");
58 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
60 error_setg(errp
, "Failed CreateEvent");
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
|
69 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
71 if (s
->file
== INVALID_HANDLE_VALUE
) {
72 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
77 ZeroMemory(&ov
, sizeof(ov
));
78 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
79 ret
= ConnectNamedPipe(s
->file
, &ov
);
81 error_setg(errp
, "Failed ConnectNamedPipe");
85 ret
= GetOverlappedResult(s
->file
, &ov
, &size
, TRUE
);
87 error_setg(errp
, "Failed GetOverlappedResult");
89 CloseHandle(ov
.hEvent
);
96 CloseHandle(ov
.hEvent
);
99 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
106 static void qemu_chr_open_pipe(Chardev
*chr
,
107 ChardevBackend
*backend
,
111 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
112 const char *filename
= opts
->device
;
114 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
121 static void qemu_chr_open_pipe(Chardev
*chr
,
122 ChardevBackend
*backend
,
126 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
130 const char *filename
= opts
->device
;
132 filename_in
= g_strdup_printf("%s.in", filename
);
133 filename_out
= g_strdup_printf("%s.out", filename
);
134 TFR(fd_in
= qemu_open(filename_in
, O_RDWR
| O_BINARY
));
135 TFR(fd_out
= qemu_open(filename_out
, O_RDWR
| O_BINARY
));
137 g_free(filename_out
);
138 if (fd_in
< 0 || fd_out
< 0) {
145 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
147 error_setg_file_open(errp
, errno
, filename
);
151 qemu_chr_open_fd(chr
, fd_in
, fd_out
);
156 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
159 const char *device
= qemu_opt_get(opts
, "path");
162 if (device
== NULL
) {
163 error_setg(errp
, "chardev: pipe: no device path given");
166 backend
->type
= CHARDEV_BACKEND_KIND_PIPE
;
167 dev
= backend
->u
.pipe
.data
= g_new0(ChardevHostdev
, 1);
168 qemu_chr_parse_common(opts
, qapi_ChardevHostdev_base(dev
));
169 dev
->device
= g_strdup(device
);
172 static void char_pipe_class_init(ObjectClass
*oc
, void *data
)
174 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
176 cc
->parse
= qemu_chr_parse_pipe
;
177 cc
->open
= qemu_chr_open_pipe
;
180 static const TypeInfo char_pipe_type_info
= {
181 .name
= TYPE_CHARDEV_PIPE
,
183 .parent
= TYPE_CHARDEV_WIN
,
185 .parent
= TYPE_CHARDEV_FD
,
187 .class_init
= char_pipe_class_init
,
190 static void register_types(void)
192 type_register_static(&char_pipe_type_info
);
195 type_init(register_types
);