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
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "sysemu/char.h"
38 static int win_chr_pipe_init(Chardev
*chr
, const char *filename
,
41 WinChardev
*s
= WIN_CHARDEV(chr
);
49 s
->hsend
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
51 error_setg(errp
, "Failed CreateEvent");
54 s
->hrecv
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
56 error_setg(errp
, "Failed CreateEvent");
60 openname
= g_strdup_printf("\\\\.\\pipe\\%s", filename
);
61 s
->hcom
= CreateNamedPipe(openname
,
62 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
63 PIPE_TYPE_BYTE
| PIPE_READMODE_BYTE
|
65 MAXCONNECT
, NSENDBUF
, NRECVBUF
, NTIMEOUT
, NULL
);
67 if (s
->hcom
== INVALID_HANDLE_VALUE
) {
68 error_setg(errp
, "Failed CreateNamedPipe (%lu)", GetLastError());
73 ZeroMemory(&ov
, sizeof(ov
));
74 ov
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
75 ret
= ConnectNamedPipe(s
->hcom
, &ov
);
77 error_setg(errp
, "Failed ConnectNamedPipe");
81 ret
= GetOverlappedResult(s
->hcom
, &ov
, &size
, TRUE
);
83 error_setg(errp
, "Failed GetOverlappedResult");
85 CloseHandle(ov
.hEvent
);
92 CloseHandle(ov
.hEvent
);
95 qemu_add_polling_cb(win_chr_pipe_poll
, chr
);
102 static void qemu_chr_open_pipe(Chardev
*chr
,
103 ChardevBackend
*backend
,
107 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
108 const char *filename
= opts
->device
;
110 if (win_chr_pipe_init(chr
, filename
, errp
) < 0) {
117 static void qemu_chr_open_pipe(Chardev
*chr
,
118 ChardevBackend
*backend
,
122 ChardevHostdev
*opts
= backend
->u
.pipe
.data
;
126 const char *filename
= opts
->device
;
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
));
133 g_free(filename_out
);
134 if (fd_in
< 0 || fd_out
< 0) {
141 TFR(fd_in
= fd_out
= qemu_open(filename
, O_RDWR
| O_BINARY
));
143 error_setg_file_open(errp
, errno
, filename
);
147 qemu_chr_open_fd(chr
, fd_in
, fd_out
);
152 static void qemu_chr_parse_pipe(QemuOpts
*opts
, ChardevBackend
*backend
,
155 const char *device
= qemu_opt_get(opts
, "path");
158 if (device
== NULL
) {
159 error_setg(errp
, "chardev: pipe: no device path given");
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
);
168 static void char_pipe_class_init(ObjectClass
*oc
, void *data
)
170 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
172 cc
->parse
= qemu_chr_parse_pipe
;
173 cc
->open
= qemu_chr_open_pipe
;
176 static const TypeInfo char_pipe_type_info
= {
177 .name
= TYPE_CHARDEV_PIPE
,
179 .parent
= TYPE_CHARDEV_WIN
,
181 .parent
= TYPE_CHARDEV_FD
,
183 .class_init
= char_pipe_class_init
,
186 static void register_types(void)
188 type_register_static(&char_pipe_type_info
);
191 type_init(register_types
);