xref: /openbmc/qemu/chardev/char-pipe.c (revision f7160f32)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qapi/error.h"
28 #include "qemu/main-loop.h"
29 #include "qemu/module.h"
30 #include "qemu/option.h"
31 #include "chardev/char.h"
32 
33 #ifdef _WIN32
34 #include "chardev/char-win.h"
35 #else
36 #include "chardev/char-fd.h"
37 #endif
38 
39 #ifdef _WIN32
40 #define MAXCONNECT 1
41 #define NTIMEOUT 5000
42 
43 static int win_chr_pipe_init(Chardev *chr, const char *filename,
44                              Error **errp)
45 {
46     WinChardev *s = WIN_CHARDEV(chr);
47     OVERLAPPED ov;
48     int ret;
49     DWORD size;
50     char *openname;
51 
52     s->fpipe = TRUE;
53 
54     s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
55     if (!s->hsend) {
56         error_setg(errp, "Failed CreateEvent");
57         goto fail;
58     }
59     s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
60     if (!s->hrecv) {
61         error_setg(errp, "Failed CreateEvent");
62         goto fail;
63     }
64 
65     openname = g_strdup_printf("\\\\.\\pipe\\%s", filename);
66     s->file = CreateNamedPipe(openname,
67                               PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
68                               PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
69                               PIPE_WAIT,
70                               MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
71     g_free(openname);
72     if (s->file == INVALID_HANDLE_VALUE) {
73         error_setg_win32(errp, GetLastError(), "Failed CreateNamedPipe");
74         s->file = NULL;
75         goto fail;
76     }
77 
78     ZeroMemory(&ov, sizeof(ov));
79     ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
80     ret = ConnectNamedPipe(s->file, &ov);
81     if (ret) {
82         error_setg(errp, "Failed ConnectNamedPipe");
83         goto fail;
84     }
85 
86     ret = GetOverlappedResult(s->file, &ov, &size, TRUE);
87     if (!ret) {
88         error_setg(errp, "Failed GetOverlappedResult");
89         if (ov.hEvent) {
90             CloseHandle(ov.hEvent);
91             ov.hEvent = NULL;
92         }
93         goto fail;
94     }
95 
96     if (ov.hEvent) {
97         CloseHandle(ov.hEvent);
98         ov.hEvent = NULL;
99     }
100     qemu_add_polling_cb(win_chr_pipe_poll, chr);
101     return 0;
102 
103  fail:
104     return -1;
105 }
106 
107 static void qemu_chr_open_pipe(Chardev *chr,
108                                ChardevBackend *backend,
109                                bool *be_opened,
110                                Error **errp)
111 {
112     ChardevHostdev *opts = backend->u.pipe.data;
113     const char *filename = opts->device;
114 
115     if (win_chr_pipe_init(chr, filename, errp) < 0) {
116         return;
117     }
118 }
119 
120 #else
121 
122 static void qemu_chr_open_pipe(Chardev *chr,
123                                ChardevBackend *backend,
124                                bool *be_opened,
125                                Error **errp)
126 {
127     ChardevHostdev *opts = backend->u.pipe.data;
128     int fd_in, fd_out;
129     char *filename_in;
130     char *filename_out;
131     const char *filename = opts->device;
132 
133     filename_in = g_strdup_printf("%s.in", filename);
134     filename_out = g_strdup_printf("%s.out", filename);
135     TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
136     TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
137     g_free(filename_in);
138     g_free(filename_out);
139     if (fd_in < 0 || fd_out < 0) {
140         if (fd_in >= 0) {
141             close(fd_in);
142         }
143         if (fd_out >= 0) {
144             close(fd_out);
145         }
146         TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
147         if (fd_in < 0) {
148             error_setg_file_open(errp, errno, filename);
149             return;
150         }
151     }
152     qemu_chr_open_fd(chr, fd_in, fd_out);
153 }
154 
155 #endif /* !_WIN32 */
156 
157 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
158                                 Error **errp)
159 {
160     const char *device = qemu_opt_get(opts, "path");
161     ChardevHostdev *dev;
162 
163     if (device == NULL) {
164         error_setg(errp, "chardev: pipe: no device path given");
165         return;
166     }
167     backend->type = CHARDEV_BACKEND_KIND_PIPE;
168     dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1);
169     qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev));
170     dev->device = g_strdup(device);
171 }
172 
173 static void char_pipe_class_init(ObjectClass *oc, void *data)
174 {
175     ChardevClass *cc = CHARDEV_CLASS(oc);
176 
177     cc->parse = qemu_chr_parse_pipe;
178     cc->open = qemu_chr_open_pipe;
179 }
180 
181 static const TypeInfo char_pipe_type_info = {
182     .name = TYPE_CHARDEV_PIPE,
183 #ifdef _WIN32
184     .parent = TYPE_CHARDEV_WIN,
185 #else
186     .parent = TYPE_CHARDEV_FD,
187 #endif
188     .class_init = char_pipe_class_init,
189 };
190 
191 static void register_types(void)
192 {
193     type_register_static(&char_pipe_type_info);
194 }
195 
196 type_init(register_types);
197