xref: /openbmc/qemu/io/channel-command.c (revision 0b63052a46adcd70ac654624b6cad196c9269379)
1195e14d0SDaniel P. Berrange /*
2195e14d0SDaniel P. Berrange  * QEMU I/O channels external command driver
3195e14d0SDaniel P. Berrange  *
4195e14d0SDaniel P. Berrange  * Copyright (c) 2015 Red Hat, Inc.
5195e14d0SDaniel P. Berrange  *
6195e14d0SDaniel P. Berrange  * This library is free software; you can redistribute it and/or
7195e14d0SDaniel P. Berrange  * modify it under the terms of the GNU Lesser General Public
8195e14d0SDaniel P. Berrange  * License as published by the Free Software Foundation; either
9c8198bd5SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10195e14d0SDaniel P. Berrange  *
11195e14d0SDaniel P. Berrange  * This library is distributed in the hope that it will be useful,
12195e14d0SDaniel P. Berrange  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13195e14d0SDaniel P. Berrange  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14195e14d0SDaniel P. Berrange  * Lesser General Public License for more details.
15195e14d0SDaniel P. Berrange  *
16195e14d0SDaniel P. Berrange  * You should have received a copy of the GNU Lesser General Public
17195e14d0SDaniel P. Berrange  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18195e14d0SDaniel P. Berrange  *
19195e14d0SDaniel P. Berrange  */
20195e14d0SDaniel P. Berrange 
21cae9fc56SPeter Maydell #include "qemu/osdep.h"
22195e14d0SDaniel P. Berrange #include "io/channel-command.h"
23*06e0f098SStefan Hajnoczi #include "io/channel-util.h"
24195e14d0SDaniel P. Berrange #include "io/channel-watch.h"
25da34e65cSMarkus Armbruster #include "qapi/error.h"
260b8fa32fSMarkus Armbruster #include "qemu/module.h"
27195e14d0SDaniel P. Berrange #include "qemu/sockets.h"
28195e14d0SDaniel P. Berrange #include "trace.h"
29195e14d0SDaniel P. Berrange 
3005e50e8fSMarc-André Lureau /**
3105e50e8fSMarc-André Lureau  * qio_channel_command_new_pid:
3205e50e8fSMarc-André Lureau  * @writefd: the FD connected to the command's stdin
3305e50e8fSMarc-André Lureau  * @readfd: the FD connected to the command's stdout
34a95570e3SMarc-André Lureau  * @pid: the PID/HANDLE of the running child command
3505e50e8fSMarc-André Lureau  * @errp: pointer to a NULL-initialized error object
3605e50e8fSMarc-André Lureau  *
3705e50e8fSMarc-André Lureau  * Create a channel for performing I/O with the
3805e50e8fSMarc-André Lureau  * previously spawned command identified by @pid.
3905e50e8fSMarc-André Lureau  * The two file descriptors provide the connection
4005e50e8fSMarc-André Lureau  * to command's stdio streams, either one or which
4105e50e8fSMarc-André Lureau  * may be -1 to indicate that stream is not open.
4205e50e8fSMarc-André Lureau  *
4305e50e8fSMarc-André Lureau  * The channel will take ownership of the process
4405e50e8fSMarc-André Lureau  * @pid and will kill it when closing the channel.
4505e50e8fSMarc-André Lureau  * Similarly it will take responsibility for
4605e50e8fSMarc-André Lureau  * closing the file descriptors @writefd and @readfd.
4705e50e8fSMarc-André Lureau  *
4805e50e8fSMarc-André Lureau  * Returns: the command channel object, or NULL on error
4905e50e8fSMarc-André Lureau  */
5005e50e8fSMarc-André Lureau static QIOChannelCommand *
qio_channel_command_new_pid(int writefd,int readfd,GPid pid)51195e14d0SDaniel P. Berrange qio_channel_command_new_pid(int writefd,
52195e14d0SDaniel P. Berrange                             int readfd,
53a95570e3SMarc-André Lureau                             GPid pid)
54195e14d0SDaniel P. Berrange {
55195e14d0SDaniel P. Berrange     QIOChannelCommand *ioc;
56195e14d0SDaniel P. Berrange 
57195e14d0SDaniel P. Berrange     ioc = QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND));
58195e14d0SDaniel P. Berrange 
59195e14d0SDaniel P. Berrange     ioc->readfd = readfd;
60195e14d0SDaniel P. Berrange     ioc->writefd = writefd;
61195e14d0SDaniel P. Berrange     ioc->pid = pid;
62195e14d0SDaniel P. Berrange 
63ec5b6c9cSMarc-André Lureau     trace_qio_channel_command_new_pid(ioc, writefd, readfd,
64ec5b6c9cSMarc-André Lureau #ifdef WIN32
65ec5b6c9cSMarc-André Lureau                                       GetProcessId(pid)
66ec5b6c9cSMarc-André Lureau #else
67ec5b6c9cSMarc-André Lureau                                       pid
68ec5b6c9cSMarc-André Lureau #endif
69ec5b6c9cSMarc-André Lureau         );
70195e14d0SDaniel P. Berrange     return ioc;
71195e14d0SDaniel P. Berrange }
72195e14d0SDaniel P. Berrange 
73195e14d0SDaniel P. Berrange QIOChannelCommand *
qio_channel_command_new_spawn(const char * const argv[],int flags,Error ** errp)74195e14d0SDaniel P. Berrange qio_channel_command_new_spawn(const char *const argv[],
75195e14d0SDaniel P. Berrange                               int flags,
76195e14d0SDaniel P. Berrange                               Error **errp)
77195e14d0SDaniel P. Berrange {
78a95570e3SMarc-André Lureau     g_autoptr(GError) err = NULL;
79a95570e3SMarc-André Lureau     GPid pid = 0;
80a95570e3SMarc-André Lureau     GSpawnFlags gflags = G_SPAWN_CLOEXEC_PIPES | G_SPAWN_DO_NOT_REAP_CHILD;
81a95570e3SMarc-André Lureau     int stdinfd = -1, stdoutfd = -1;
82195e14d0SDaniel P. Berrange 
83195e14d0SDaniel P. Berrange     flags = flags & O_ACCMODE;
84a95570e3SMarc-André Lureau     gflags |= flags == O_WRONLY ? G_SPAWN_STDOUT_TO_DEV_NULL : 0;
85195e14d0SDaniel P. Berrange 
86a95570e3SMarc-André Lureau     if (!g_spawn_async_with_pipes(NULL, (char **)argv, NULL, gflags, NULL, NULL,
87a95570e3SMarc-André Lureau                                   &pid,
88a95570e3SMarc-André Lureau                                   flags == O_RDONLY ? NULL : &stdinfd,
89a95570e3SMarc-André Lureau                                   flags == O_WRONLY ? NULL : &stdoutfd,
90a95570e3SMarc-André Lureau                                   NULL, &err)) {
91a95570e3SMarc-André Lureau         error_setg(errp, "%s", err->message);
92195e14d0SDaniel P. Berrange         return NULL;
93195e14d0SDaniel P. Berrange     }
94195e14d0SDaniel P. Berrange 
95a95570e3SMarc-André Lureau     return qio_channel_command_new_pid(stdinfd, stdoutfd, pid);
96a95570e3SMarc-André Lureau }
97a95570e3SMarc-André Lureau 
98195e14d0SDaniel P. Berrange #ifndef WIN32
qio_channel_command_abort(QIOChannelCommand * ioc,Error ** errp)99195e14d0SDaniel P. Berrange static int qio_channel_command_abort(QIOChannelCommand *ioc,
100195e14d0SDaniel P. Berrange                                      Error **errp)
101195e14d0SDaniel P. Berrange {
102195e14d0SDaniel P. Berrange     pid_t ret;
103195e14d0SDaniel P. Berrange     int status;
104195e14d0SDaniel P. Berrange     int step = 0;
105195e14d0SDaniel P. Berrange 
106195e14d0SDaniel P. Berrange     /* See if intermediate process has exited; if not, try a nice
107195e14d0SDaniel P. Berrange      * SIGTERM followed by a more severe SIGKILL.
108195e14d0SDaniel P. Berrange      */
109195e14d0SDaniel P. Berrange  rewait:
110195e14d0SDaniel P. Berrange     trace_qio_channel_command_abort(ioc, ioc->pid);
111195e14d0SDaniel P. Berrange     ret = waitpid(ioc->pid, &status, WNOHANG);
112195e14d0SDaniel P. Berrange     trace_qio_channel_command_wait(ioc, ioc->pid, ret, status);
113195e14d0SDaniel P. Berrange     if (ret == (pid_t)-1) {
114195e14d0SDaniel P. Berrange         if (errno == EINTR) {
115195e14d0SDaniel P. Berrange             goto rewait;
116195e14d0SDaniel P. Berrange         } else {
117195e14d0SDaniel P. Berrange             error_setg_errno(errp, errno,
118195e14d0SDaniel P. Berrange                              "Cannot wait on pid %llu",
119195e14d0SDaniel P. Berrange                              (unsigned long long)ioc->pid);
120195e14d0SDaniel P. Berrange             return -1;
121195e14d0SDaniel P. Berrange         }
122195e14d0SDaniel P. Berrange     } else if (ret == 0) {
123195e14d0SDaniel P. Berrange         if (step == 0) {
124195e14d0SDaniel P. Berrange             kill(ioc->pid, SIGTERM);
125195e14d0SDaniel P. Berrange         } else if (step == 1) {
126195e14d0SDaniel P. Berrange             kill(ioc->pid, SIGKILL);
127195e14d0SDaniel P. Berrange         } else {
128195e14d0SDaniel P. Berrange             error_setg(errp,
129195e14d0SDaniel P. Berrange                        "Process %llu refused to die",
130195e14d0SDaniel P. Berrange                        (unsigned long long)ioc->pid);
131195e14d0SDaniel P. Berrange             return -1;
132195e14d0SDaniel P. Berrange         }
1330c0a55b2SDaniel P. Berrange         step++;
134195e14d0SDaniel P. Berrange         usleep(10 * 1000);
135195e14d0SDaniel P. Berrange         goto rewait;
136195e14d0SDaniel P. Berrange     }
137195e14d0SDaniel P. Berrange 
138195e14d0SDaniel P. Berrange     return 0;
139195e14d0SDaniel P. Berrange }
140ec5b6c9cSMarc-André Lureau #else
qio_channel_command_abort(QIOChannelCommand * ioc,Error ** errp)141ec5b6c9cSMarc-André Lureau static int qio_channel_command_abort(QIOChannelCommand *ioc,
142ec5b6c9cSMarc-André Lureau                                      Error **errp)
143ec5b6c9cSMarc-André Lureau {
144ec5b6c9cSMarc-André Lureau     DWORD ret;
145ec5b6c9cSMarc-André Lureau 
146ec5b6c9cSMarc-André Lureau     TerminateProcess(ioc->pid, 0);
147ec5b6c9cSMarc-André Lureau     ret = WaitForSingleObject(ioc->pid, 1000);
148ec5b6c9cSMarc-André Lureau     if (ret != WAIT_OBJECT_0) {
149ec5b6c9cSMarc-André Lureau         error_setg(errp,
150ec5b6c9cSMarc-André Lureau                    "Process %llu refused to die",
151ec5b6c9cSMarc-André Lureau                    (unsigned long long)GetProcessId(ioc->pid));
152ec5b6c9cSMarc-André Lureau         return -1;
153ec5b6c9cSMarc-André Lureau     }
154ec5b6c9cSMarc-André Lureau 
155ec5b6c9cSMarc-André Lureau     return 0;
156ec5b6c9cSMarc-André Lureau }
157195e14d0SDaniel P. Berrange #endif /* ! WIN32 */
158195e14d0SDaniel P. Berrange 
159195e14d0SDaniel P. Berrange 
qio_channel_command_init(Object * obj)160195e14d0SDaniel P. Berrange static void qio_channel_command_init(Object *obj)
161195e14d0SDaniel P. Berrange {
162195e14d0SDaniel P. Berrange     QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
163195e14d0SDaniel P. Berrange     ioc->readfd = -1;
164195e14d0SDaniel P. Berrange     ioc->writefd = -1;
165a95570e3SMarc-André Lureau     ioc->pid = 0;
166195e14d0SDaniel P. Berrange }
167195e14d0SDaniel P. Berrange 
qio_channel_command_finalize(Object * obj)168195e14d0SDaniel P. Berrange static void qio_channel_command_finalize(Object *obj)
169195e14d0SDaniel P. Berrange {
170195e14d0SDaniel P. Berrange     QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
171195e14d0SDaniel P. Berrange     if (ioc->readfd != -1) {
172195e14d0SDaniel P. Berrange         close(ioc->readfd);
173195e14d0SDaniel P. Berrange     }
174e155494cSDaniel P. Berrange     if (ioc->writefd != -1 &&
175e155494cSDaniel P. Berrange         ioc->writefd != ioc->readfd) {
176195e14d0SDaniel P. Berrange         close(ioc->writefd);
177195e14d0SDaniel P. Berrange     }
178e155494cSDaniel P. Berrange     ioc->writefd = ioc->readfd = -1;
179195e14d0SDaniel P. Berrange     if (ioc->pid > 0) {
180195e14d0SDaniel P. Berrange         qio_channel_command_abort(ioc, NULL);
181a95570e3SMarc-André Lureau         g_spawn_close_pid(ioc->pid);
182195e14d0SDaniel P. Berrange     }
183195e14d0SDaniel P. Berrange }
184195e14d0SDaniel P. Berrange 
185ec5b6c9cSMarc-André Lureau #ifdef WIN32
win32_fd_poll(int fd,gushort events)186ec5b6c9cSMarc-André Lureau static bool win32_fd_poll(int fd, gushort events)
187ec5b6c9cSMarc-André Lureau {
188ec5b6c9cSMarc-André Lureau     GPollFD pfd = { .fd = _get_osfhandle(fd), .events = events };
189ec5b6c9cSMarc-André Lureau     int res;
190ec5b6c9cSMarc-André Lureau 
191ec5b6c9cSMarc-André Lureau     do {
192ec5b6c9cSMarc-André Lureau         res = g_poll(&pfd, 1, 0);
193ec5b6c9cSMarc-André Lureau     } while (res < 0 && errno == EINTR);
194ec5b6c9cSMarc-André Lureau     if (res == 0) {
195ec5b6c9cSMarc-André Lureau         return false;
196ec5b6c9cSMarc-André Lureau     }
197ec5b6c9cSMarc-André Lureau 
198ec5b6c9cSMarc-André Lureau     return true;
199ec5b6c9cSMarc-André Lureau }
200ec5b6c9cSMarc-André Lureau #endif
201195e14d0SDaniel P. Berrange 
qio_channel_command_readv(QIOChannel * ioc,const struct iovec * iov,size_t niov,int ** fds,size_t * nfds,int flags,Error ** errp)202195e14d0SDaniel P. Berrange static ssize_t qio_channel_command_readv(QIOChannel *ioc,
203195e14d0SDaniel P. Berrange                                          const struct iovec *iov,
204195e14d0SDaniel P. Berrange                                          size_t niov,
205195e14d0SDaniel P. Berrange                                          int **fds,
206195e14d0SDaniel P. Berrange                                          size_t *nfds,
20784615a19Smanish.mishra                                          int flags,
208195e14d0SDaniel P. Berrange                                          Error **errp)
209195e14d0SDaniel P. Berrange {
210195e14d0SDaniel P. Berrange     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
211195e14d0SDaniel P. Berrange     ssize_t ret;
212195e14d0SDaniel P. Berrange 
213ec5b6c9cSMarc-André Lureau #ifdef WIN32
214ec5b6c9cSMarc-André Lureau     if (!cioc->blocking && !win32_fd_poll(cioc->readfd, G_IO_IN)) {
215ec5b6c9cSMarc-André Lureau         return QIO_CHANNEL_ERR_BLOCK;
216ec5b6c9cSMarc-André Lureau     }
217ec5b6c9cSMarc-André Lureau #endif
218ec5b6c9cSMarc-André Lureau 
219195e14d0SDaniel P. Berrange  retry:
220195e14d0SDaniel P. Berrange     ret = readv(cioc->readfd, iov, niov);
221195e14d0SDaniel P. Berrange     if (ret < 0) {
22230fd3e27SDaniel P. Berrange         if (errno == EAGAIN) {
223195e14d0SDaniel P. Berrange             return QIO_CHANNEL_ERR_BLOCK;
224195e14d0SDaniel P. Berrange         }
225195e14d0SDaniel P. Berrange         if (errno == EINTR) {
226195e14d0SDaniel P. Berrange             goto retry;
227195e14d0SDaniel P. Berrange         }
228195e14d0SDaniel P. Berrange 
229195e14d0SDaniel P. Berrange         error_setg_errno(errp, errno,
230195e14d0SDaniel P. Berrange                          "Unable to read from command");
231195e14d0SDaniel P. Berrange         return -1;
232195e14d0SDaniel P. Berrange     }
233195e14d0SDaniel P. Berrange 
234195e14d0SDaniel P. Berrange     return ret;
235195e14d0SDaniel P. Berrange }
236195e14d0SDaniel P. Berrange 
qio_channel_command_writev(QIOChannel * ioc,const struct iovec * iov,size_t niov,int * fds,size_t nfds,int flags,Error ** errp)237195e14d0SDaniel P. Berrange static ssize_t qio_channel_command_writev(QIOChannel *ioc,
238195e14d0SDaniel P. Berrange                                           const struct iovec *iov,
239195e14d0SDaniel P. Berrange                                           size_t niov,
240195e14d0SDaniel P. Berrange                                           int *fds,
241195e14d0SDaniel P. Berrange                                           size_t nfds,
242b88651cbSLeonardo Bras                                           int flags,
243195e14d0SDaniel P. Berrange                                           Error **errp)
244195e14d0SDaniel P. Berrange {
245195e14d0SDaniel P. Berrange     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
246195e14d0SDaniel P. Berrange     ssize_t ret;
247195e14d0SDaniel P. Berrange 
248ec5b6c9cSMarc-André Lureau #ifdef WIN32
249ec5b6c9cSMarc-André Lureau     if (!cioc->blocking && !win32_fd_poll(cioc->writefd, G_IO_OUT)) {
250ec5b6c9cSMarc-André Lureau         return QIO_CHANNEL_ERR_BLOCK;
251ec5b6c9cSMarc-André Lureau     }
252ec5b6c9cSMarc-André Lureau #endif
253ec5b6c9cSMarc-André Lureau 
254195e14d0SDaniel P. Berrange  retry:
255195e14d0SDaniel P. Berrange     ret = writev(cioc->writefd, iov, niov);
256195e14d0SDaniel P. Berrange     if (ret <= 0) {
25730fd3e27SDaniel P. Berrange         if (errno == EAGAIN) {
258195e14d0SDaniel P. Berrange             return QIO_CHANNEL_ERR_BLOCK;
259195e14d0SDaniel P. Berrange         }
260195e14d0SDaniel P. Berrange         if (errno == EINTR) {
261195e14d0SDaniel P. Berrange             goto retry;
262195e14d0SDaniel P. Berrange         }
263195e14d0SDaniel P. Berrange         error_setg_errno(errp, errno, "%s",
264195e14d0SDaniel P. Berrange                          "Unable to write to command");
265195e14d0SDaniel P. Berrange         return -1;
266195e14d0SDaniel P. Berrange     }
267195e14d0SDaniel P. Berrange     return ret;
268195e14d0SDaniel P. Berrange }
269195e14d0SDaniel P. Berrange 
qio_channel_command_set_blocking(QIOChannel * ioc,bool enabled,Error ** errp)270195e14d0SDaniel P. Berrange static int qio_channel_command_set_blocking(QIOChannel *ioc,
271195e14d0SDaniel P. Berrange                                             bool enabled,
272195e14d0SDaniel P. Berrange                                             Error **errp)
273195e14d0SDaniel P. Berrange {
274195e14d0SDaniel P. Berrange     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
275195e14d0SDaniel P. Berrange 
276ec5b6c9cSMarc-André Lureau #ifdef WIN32
277ec5b6c9cSMarc-André Lureau     cioc->blocking = enabled;
278ec5b6c9cSMarc-André Lureau #else
279ec5b6c9cSMarc-André Lureau 
280ec5b6c9cSMarc-André Lureau     if ((cioc->writefd >= 0 && !g_unix_set_fd_nonblocking(cioc->writefd, !enabled, NULL)) ||
281ec5b6c9cSMarc-André Lureau         (cioc->readfd >= 0 && !g_unix_set_fd_nonblocking(cioc->readfd, !enabled, NULL))) {
28217fc1245SMarc-André Lureau         error_setg_errno(errp, errno, "Failed to set FD nonblocking");
28317fc1245SMarc-André Lureau         return -1;
284195e14d0SDaniel P. Berrange     }
28517fc1245SMarc-André Lureau #endif
286195e14d0SDaniel P. Berrange     return 0;
287195e14d0SDaniel P. Berrange }
288195e14d0SDaniel P. Berrange 
289195e14d0SDaniel P. Berrange 
qio_channel_command_close(QIOChannel * ioc,Error ** errp)290195e14d0SDaniel P. Berrange static int qio_channel_command_close(QIOChannel *ioc,
291195e14d0SDaniel P. Berrange                                      Error **errp)
292195e14d0SDaniel P. Berrange {
293195e14d0SDaniel P. Berrange     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
294195e14d0SDaniel P. Berrange     int rv = 0;
295fe823b6fSThomas Huth #ifndef WIN32
296fe823b6fSThomas Huth     pid_t wp;
297fe823b6fSThomas Huth #endif
298195e14d0SDaniel P. Berrange 
299195e14d0SDaniel P. Berrange     /* We close FDs before killing, because that
300195e14d0SDaniel P. Berrange      * gives a better chance of clean shutdown
301195e14d0SDaniel P. Berrange      */
302e155494cSDaniel P. Berrange     if (cioc->readfd != -1 &&
303e155494cSDaniel P. Berrange         close(cioc->readfd) < 0) {
304195e14d0SDaniel P. Berrange         rv = -1;
305195e14d0SDaniel P. Berrange     }
306e155494cSDaniel P. Berrange     if (cioc->writefd != -1 &&
307e155494cSDaniel P. Berrange         cioc->writefd != cioc->readfd &&
308e155494cSDaniel P. Berrange         close(cioc->writefd) < 0) {
309195e14d0SDaniel P. Berrange         rv = -1;
310195e14d0SDaniel P. Berrange     }
311e155494cSDaniel P. Berrange     cioc->writefd = cioc->readfd = -1;
312fe823b6fSThomas Huth 
313195e14d0SDaniel P. Berrange #ifndef WIN32
314fe823b6fSThomas Huth     do {
315fe823b6fSThomas Huth         wp = waitpid(cioc->pid, NULL, 0);
316fe823b6fSThomas Huth     } while (wp == (pid_t)-1 && errno == EINTR);
317fe823b6fSThomas Huth     if (wp == (pid_t)-1) {
318fe823b6fSThomas Huth         error_setg_errno(errp, errno, "Failed to wait for pid %llu",
319fe823b6fSThomas Huth                          (unsigned long long)cioc->pid);
320195e14d0SDaniel P. Berrange         return -1;
321195e14d0SDaniel P. Berrange     }
322ec5b6c9cSMarc-André Lureau #else
323ec5b6c9cSMarc-André Lureau     WaitForSingleObject(cioc->pid, INFINITE);
324195e14d0SDaniel P. Berrange #endif
325fe823b6fSThomas Huth 
326195e14d0SDaniel P. Berrange     if (rv < 0) {
327195e14d0SDaniel P. Berrange         error_setg_errno(errp, errno, "%s",
328195e14d0SDaniel P. Berrange                          "Unable to close command");
329195e14d0SDaniel P. Berrange     }
330195e14d0SDaniel P. Berrange     return rv;
331195e14d0SDaniel P. Berrange }
332195e14d0SDaniel P. Berrange 
333195e14d0SDaniel P. Berrange 
qio_channel_command_set_aio_fd_handler(QIOChannel * ioc,AioContext * read_ctx,IOHandler * io_read,AioContext * write_ctx,IOHandler * io_write,void * opaque)334bf88c124SPaolo Bonzini static void qio_channel_command_set_aio_fd_handler(QIOChannel *ioc,
335*06e0f098SStefan Hajnoczi                                                    AioContext *read_ctx,
336bf88c124SPaolo Bonzini                                                    IOHandler *io_read,
337*06e0f098SStefan Hajnoczi                                                    AioContext *write_ctx,
338bf88c124SPaolo Bonzini                                                    IOHandler *io_write,
339bf88c124SPaolo Bonzini                                                    void *opaque)
340bf88c124SPaolo Bonzini {
341bf88c124SPaolo Bonzini     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
342*06e0f098SStefan Hajnoczi 
343*06e0f098SStefan Hajnoczi     qio_channel_util_set_aio_fd_handler(cioc->readfd, read_ctx, io_read,
344*06e0f098SStefan Hajnoczi                                         cioc->writefd, write_ctx, io_write,
345*06e0f098SStefan Hajnoczi                                         opaque);
346bf88c124SPaolo Bonzini }
347bf88c124SPaolo Bonzini 
348bf88c124SPaolo Bonzini 
qio_channel_command_create_watch(QIOChannel * ioc,GIOCondition condition)349195e14d0SDaniel P. Berrange static GSource *qio_channel_command_create_watch(QIOChannel *ioc,
350195e14d0SDaniel P. Berrange                                                  GIOCondition condition)
351195e14d0SDaniel P. Berrange {
352195e14d0SDaniel P. Berrange     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
353195e14d0SDaniel P. Berrange     return qio_channel_create_fd_pair_watch(ioc,
354195e14d0SDaniel P. Berrange                                             cioc->readfd,
355195e14d0SDaniel P. Berrange                                             cioc->writefd,
356195e14d0SDaniel P. Berrange                                             condition);
357195e14d0SDaniel P. Berrange }
358195e14d0SDaniel P. Berrange 
359195e14d0SDaniel P. Berrange 
qio_channel_command_class_init(ObjectClass * klass,void * class_data G_GNUC_UNUSED)360195e14d0SDaniel P. Berrange static void qio_channel_command_class_init(ObjectClass *klass,
361195e14d0SDaniel P. Berrange                                            void *class_data G_GNUC_UNUSED)
362195e14d0SDaniel P. Berrange {
363195e14d0SDaniel P. Berrange     QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
364195e14d0SDaniel P. Berrange 
365195e14d0SDaniel P. Berrange     ioc_klass->io_writev = qio_channel_command_writev;
366195e14d0SDaniel P. Berrange     ioc_klass->io_readv = qio_channel_command_readv;
367195e14d0SDaniel P. Berrange     ioc_klass->io_set_blocking = qio_channel_command_set_blocking;
368195e14d0SDaniel P. Berrange     ioc_klass->io_close = qio_channel_command_close;
369195e14d0SDaniel P. Berrange     ioc_klass->io_create_watch = qio_channel_command_create_watch;
370bf88c124SPaolo Bonzini     ioc_klass->io_set_aio_fd_handler = qio_channel_command_set_aio_fd_handler;
371195e14d0SDaniel P. Berrange }
372195e14d0SDaniel P. Berrange 
373195e14d0SDaniel P. Berrange static const TypeInfo qio_channel_command_info = {
374195e14d0SDaniel P. Berrange     .parent = TYPE_QIO_CHANNEL,
375195e14d0SDaniel P. Berrange     .name = TYPE_QIO_CHANNEL_COMMAND,
376195e14d0SDaniel P. Berrange     .instance_size = sizeof(QIOChannelCommand),
377195e14d0SDaniel P. Berrange     .instance_init = qio_channel_command_init,
378195e14d0SDaniel P. Berrange     .instance_finalize = qio_channel_command_finalize,
379195e14d0SDaniel P. Berrange     .class_init = qio_channel_command_class_init,
380195e14d0SDaniel P. Berrange };
381195e14d0SDaniel P. Berrange 
qio_channel_command_register_types(void)382195e14d0SDaniel P. Berrange static void qio_channel_command_register_types(void)
383195e14d0SDaniel P. Berrange {
384195e14d0SDaniel P. Berrange     type_register_static(&qio_channel_command_info);
385195e14d0SDaniel P. Berrange }
386195e14d0SDaniel P. Berrange 
387195e14d0SDaniel P. Berrange type_init(qio_channel_command_register_types);
388