spapr_pci: Fix extended config space accesses
[qemu/ar7.git] / chardev / char-pipe.c
blob8a51872e5e986baf55d5bb29a1f5993eb59bd28b
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 "qapi/error.h"
27 #include "qemu/option.h"
28 #include "chardev/char.h"
30 #ifdef _WIN32
31 #include "chardev/char-win.h"
32 #else
33 #include "chardev/char-fd.h"
34 #endif
36 #ifdef _WIN32
37 #define MAXCONNECT 1
38 #define NTIMEOUT 5000
40 static int win_chr_pipe_init(Chardev *chr, const char *filename,
41 Error **errp)
43 WinChardev *s = WIN_CHARDEV(chr);
44 OVERLAPPED ov;
45 int ret;
46 DWORD size;
47 char *openname;
49 s->fpipe = TRUE;
51 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
52 if (!s->hsend) {
53 error_setg(errp, "Failed CreateEvent");
54 goto fail;
56 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
57 if (!s->hrecv) {
58 error_setg(errp, "Failed CreateEvent");
59 goto fail;
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 |
66 PIPE_WAIT,
67 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
68 g_free(openname);
69 if (s->file == INVALID_HANDLE_VALUE) {
70 error_setg(errp, "Failed CreateNamedPipe (%lu)", GetLastError());
71 s->file = NULL;
72 goto fail;
75 ZeroMemory(&ov, sizeof(ov));
76 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
77 ret = ConnectNamedPipe(s->file, &ov);
78 if (ret) {
79 error_setg(errp, "Failed ConnectNamedPipe");
80 goto fail;
83 ret = GetOverlappedResult(s->file, &ov, &size, TRUE);
84 if (!ret) {
85 error_setg(errp, "Failed GetOverlappedResult");
86 if (ov.hEvent) {
87 CloseHandle(ov.hEvent);
88 ov.hEvent = NULL;
90 goto fail;
93 if (ov.hEvent) {
94 CloseHandle(ov.hEvent);
95 ov.hEvent = NULL;
97 qemu_add_polling_cb(win_chr_pipe_poll, chr);
98 return 0;
100 fail:
101 return -1;
104 static void qemu_chr_open_pipe(Chardev *chr,
105 ChardevBackend *backend,
106 bool *be_opened,
107 Error **errp)
109 ChardevHostdev *opts = backend->u.pipe.data;
110 const char *filename = opts->device;
112 if (win_chr_pipe_init(chr, filename, errp) < 0) {
113 return;
117 #else
119 static void qemu_chr_open_pipe(Chardev *chr,
120 ChardevBackend *backend,
121 bool *be_opened,
122 Error **errp)
124 ChardevHostdev *opts = backend->u.pipe.data;
125 int fd_in, fd_out;
126 char *filename_in;
127 char *filename_out;
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));
134 g_free(filename_in);
135 g_free(filename_out);
136 if (fd_in < 0 || fd_out < 0) {
137 if (fd_in >= 0) {
138 close(fd_in);
140 if (fd_out >= 0) {
141 close(fd_out);
143 TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
144 if (fd_in < 0) {
145 error_setg_file_open(errp, errno, filename);
146 return;
149 qemu_chr_open_fd(chr, fd_in, fd_out);
152 #endif /* !_WIN32 */
154 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
155 Error **errp)
157 const char *device = qemu_opt_get(opts, "path");
158 ChardevHostdev *dev;
160 if (device == NULL) {
161 error_setg(errp, "chardev: pipe: no device path given");
162 return;
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,
180 #ifdef _WIN32
181 .parent = TYPE_CHARDEV_WIN,
182 #else
183 .parent = TYPE_CHARDEV_FD,
184 #endif
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);