Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu/ar7.git] / chardev / char-pipe.c
blobb200f9365c04dd256ef806ab8362a2483de06fc6
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
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"
32 #ifdef _WIN32
33 #include "chardev/char-win.h"
34 #else
35 #include "chardev/char-fd.h"
36 #endif
38 #ifdef _WIN32
39 #define MAXCONNECT 1
40 #define NTIMEOUT 5000
42 static int win_chr_pipe_init(Chardev *chr, const char *filename,
43 Error **errp)
45 WinChardev *s = WIN_CHARDEV(chr);
46 OVERLAPPED ov;
47 int ret;
48 DWORD size;
49 char *openname;
51 s->fpipe = TRUE;
53 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
54 if (!s->hsend) {
55 error_setg(errp, "Failed CreateEvent");
56 goto fail;
58 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
59 if (!s->hrecv) {
60 error_setg(errp, "Failed CreateEvent");
61 goto fail;
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 |
68 PIPE_WAIT,
69 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
70 g_free(openname);
71 if (s->file == INVALID_HANDLE_VALUE) {
72 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
73 s->file = NULL;
74 goto fail;
77 ZeroMemory(&ov, sizeof(ov));
78 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
79 ret = ConnectNamedPipe(s->file, &ov);
80 if (ret) {
81 error_setg(errp, "Failed ConnectNamedPipe");
82 goto fail;
85 ret = GetOverlappedResult(s->file, &ov, &size, TRUE);
86 if (!ret) {
87 error_setg(errp, "Failed GetOverlappedResult");
88 if (ov.hEvent) {
89 CloseHandle(ov.hEvent);
90 ov.hEvent = NULL;
92 goto fail;
95 if (ov.hEvent) {
96 CloseHandle(ov.hEvent);
97 ov.hEvent = NULL;
99 qemu_add_polling_cb(win_chr_pipe_poll, chr);
100 return 0;
102 fail:
103 return -1;
106 static void qemu_chr_open_pipe(Chardev *chr,
107 ChardevBackend *backend,
108 bool *be_opened,
109 Error **errp)
111 ChardevHostdev *opts = backend->u.pipe.data;
112 const char *filename = opts->device;
114 if (win_chr_pipe_init(chr, filename, errp) < 0) {
115 return;
119 #else
121 static void qemu_chr_open_pipe(Chardev *chr,
122 ChardevBackend *backend,
123 bool *be_opened,
124 Error **errp)
126 ChardevHostdev *opts = backend->u.pipe.data;
127 int fd_in, fd_out;
128 char *filename_in;
129 char *filename_out;
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));
136 g_free(filename_in);
137 g_free(filename_out);
138 if (fd_in < 0 || fd_out < 0) {
139 if (fd_in >= 0) {
140 close(fd_in);
142 if (fd_out >= 0) {
143 close(fd_out);
145 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
146 if (fd_in < 0) {
147 error_setg_file_open(errp, errno, filename);
148 return;
151 qemu_chr_open_fd(chr, fd_in, fd_out);
154 #endif /* !_WIN32 */
156 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
157 Error **errp)
159 const char *device = qemu_opt_get(opts, "path");
160 ChardevHostdev *dev;
162 if (device == NULL) {
163 error_setg(errp, "chardev: pipe: no device path given");
164 return;
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,
182 #ifdef _WIN32
183 .parent = TYPE_CHARDEV_WIN,
184 #else
185 .parent = TYPE_CHARDEV_FD,
186 #endif
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);