xref: /openbmc/qemu/chardev/char-pipe.c (revision f0984d40)
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 "qapi/error.h"
27 #include "qemu/main-loop.h"
28 #include "qemu/module.h"
29 #include "qemu/option.h"
30 #include "chardev/char.h"
31 
32 #ifdef _WIN32
33 #include "chardev/char-win.h"
34 #else
35 #include "chardev/char-fd.h"
36 #endif
37 
38 #ifdef _WIN32
39 #define MAXCONNECT 1
40 #define NTIMEOUT 5000
41 
42 static int win_chr_pipe_init(Chardev *chr, const char *filename,
43                              Error **errp)
44 {
45     WinChardev *s = WIN_CHARDEV(chr);
46     OVERLAPPED ov;
47     int ret;
48     DWORD size;
49     char *openname;
50 
51     s->fpipe = TRUE;
52 
53     s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
54     if (!s->hsend) {
55         error_setg(errp, "Failed CreateEvent");
56         goto fail;
57     }
58     s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
59     if (!s->hrecv) {
60         error_setg(errp, "Failed CreateEvent");
61         goto fail;
62     }
63 
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_win32(errp, GetLastError(), "Failed CreateNamedPipe");
73         s->file = NULL;
74         goto fail;
75     }
76 
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;
83     }
84 
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;
91         }
92         goto fail;
93     }
94 
95     if (ov.hEvent) {
96         CloseHandle(ov.hEvent);
97         ov.hEvent = NULL;
98     }
99     qemu_add_polling_cb(win_chr_pipe_poll, chr);
100     return 0;
101 
102  fail:
103     return -1;
104 }
105 
106 static void qemu_chr_open_pipe(Chardev *chr,
107                                ChardevBackend *backend,
108                                bool *be_opened,
109                                Error **errp)
110 {
111     ChardevHostdev *opts = backend->u.pipe.data;
112     const char *filename = opts->device;
113 
114     if (win_chr_pipe_init(chr, filename, errp) < 0) {
115         return;
116     }
117 }
118 
119 #else
120 
121 static void qemu_chr_open_pipe(Chardev *chr,
122                                ChardevBackend *backend,
123                                bool *be_opened,
124                                Error **errp)
125 {
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;
131 
132     filename_in = g_strdup_printf("%s.in", filename);
133     filename_out = g_strdup_printf("%s.out", filename);
134     fd_in = RETRY_ON_EINTR(qemu_open_old(filename_in, O_RDWR | O_BINARY));
135     fd_out = RETRY_ON_EINTR(qemu_open_old(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);
141         }
142         if (fd_out >= 0) {
143             close(fd_out);
144         }
145         fd_in = fd_out = RETRY_ON_EINTR(
146             qemu_open_old(filename, O_RDWR | O_BINARY)
147         );
148         if (fd_in < 0) {
149             error_setg_file_open(errp, errno, filename);
150             return;
151         }
152     }
153     qemu_chr_open_fd(chr, fd_in, fd_out);
154 }
155 
156 #endif /* !_WIN32 */
157 
158 static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
159                                 Error **errp)
160 {
161     const char *device = qemu_opt_get(opts, "path");
162     ChardevHostdev *dev;
163 
164     if (device == NULL) {
165         error_setg(errp, "chardev: pipe: no device path given");
166         return;
167     }
168     backend->type = CHARDEV_BACKEND_KIND_PIPE;
169     dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1);
170     qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev));
171     dev->device = g_strdup(device);
172 }
173 
174 static void char_pipe_class_init(ObjectClass *oc, void *data)
175 {
176     ChardevClass *cc = CHARDEV_CLASS(oc);
177 
178     cc->parse = qemu_chr_parse_pipe;
179     cc->open = qemu_chr_open_pipe;
180 }
181 
182 static const TypeInfo char_pipe_type_info = {
183     .name = TYPE_CHARDEV_PIPE,
184 #ifdef _WIN32
185     .parent = TYPE_CHARDEV_WIN,
186 #else
187     .parent = TYPE_CHARDEV_FD,
188 #endif
189     .class_init = char_pipe_class_init,
190 };
191 
192 static void register_types(void)
193 {
194     type_register_static(&char_pipe_type_info);
195 }
196 
197 type_init(register_types);
198