2 * QEMU I/O channels external command driver
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io/channel-command.h"
23 #include "io/channel-watch.h"
24 #include "qapi/error.h"
25 #include "qemu/module.h"
26 #include "qemu/sockets.h"
30 * qio_channel_command_new_pid:
31 * @writefd: the FD connected to the command's stdin
32 * @readfd: the FD connected to the command's stdout
33 * @pid: the PID/HANDLE of the running child command
34 * @errp: pointer to a NULL-initialized error object
36 * Create a channel for performing I/O with the
37 * previously spawned command identified by @pid.
38 * The two file descriptors provide the connection
39 * to command's stdio streams, either one or which
40 * may be -1 to indicate that stream is not open.
42 * The channel will take ownership of the process
43 * @pid and will kill it when closing the channel.
44 * Similarly it will take responsibility for
45 * closing the file descriptors @writefd and @readfd.
47 * Returns: the command channel object, or NULL on error
49 static QIOChannelCommand
*
50 qio_channel_command_new_pid(int writefd
,
54 QIOChannelCommand
*ioc
;
56 ioc
= QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND
));
59 ioc
->writefd
= writefd
;
62 trace_qio_channel_command_new_pid(ioc
, writefd
, readfd
,
73 qio_channel_command_new_spawn(const char *const argv
[],
77 g_autoptr(GError
) err
= NULL
;
79 GSpawnFlags gflags
= G_SPAWN_CLOEXEC_PIPES
| G_SPAWN_DO_NOT_REAP_CHILD
;
80 int stdinfd
= -1, stdoutfd
= -1;
82 flags
= flags
& O_ACCMODE
;
83 gflags
|= flags
== O_WRONLY
? G_SPAWN_STDOUT_TO_DEV_NULL
: 0;
85 if (!g_spawn_async_with_pipes(NULL
, (char **)argv
, NULL
, gflags
, NULL
, NULL
,
87 flags
== O_RDONLY
? NULL
: &stdinfd
,
88 flags
== O_WRONLY
? NULL
: &stdoutfd
,
90 error_setg(errp
, "%s", err
->message
);
94 return qio_channel_command_new_pid(stdinfd
, stdoutfd
, pid
);
98 static int qio_channel_command_abort(QIOChannelCommand
*ioc
,
105 /* See if intermediate process has exited; if not, try a nice
106 * SIGTERM followed by a more severe SIGKILL.
109 trace_qio_channel_command_abort(ioc
, ioc
->pid
);
110 ret
= waitpid(ioc
->pid
, &status
, WNOHANG
);
111 trace_qio_channel_command_wait(ioc
, ioc
->pid
, ret
, status
);
112 if (ret
== (pid_t
)-1) {
113 if (errno
== EINTR
) {
116 error_setg_errno(errp
, errno
,
117 "Cannot wait on pid %llu",
118 (unsigned long long)ioc
->pid
);
121 } else if (ret
== 0) {
123 kill(ioc
->pid
, SIGTERM
);
124 } else if (step
== 1) {
125 kill(ioc
->pid
, SIGKILL
);
128 "Process %llu refused to die",
129 (unsigned long long)ioc
->pid
);
140 static int qio_channel_command_abort(QIOChannelCommand
*ioc
,
145 TerminateProcess(ioc
->pid
, 0);
146 ret
= WaitForSingleObject(ioc
->pid
, 1000);
147 if (ret
!= WAIT_OBJECT_0
) {
149 "Process %llu refused to die",
150 (unsigned long long)GetProcessId(ioc
->pid
));
159 static void qio_channel_command_init(Object
*obj
)
161 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
167 static void qio_channel_command_finalize(Object
*obj
)
169 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
170 if (ioc
->readfd
!= -1) {
173 if (ioc
->writefd
!= -1 &&
174 ioc
->writefd
!= ioc
->readfd
) {
177 ioc
->writefd
= ioc
->readfd
= -1;
179 qio_channel_command_abort(ioc
, NULL
);
180 g_spawn_close_pid(ioc
->pid
);
185 static bool win32_fd_poll(int fd
, gushort events
)
187 GPollFD pfd
= { .fd
= _get_osfhandle(fd
), .events
= events
};
191 res
= g_poll(&pfd
, 1, 0);
192 } while (res
< 0 && errno
== EINTR
);
201 static ssize_t
qio_channel_command_readv(QIOChannel
*ioc
,
202 const struct iovec
*iov
,
208 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
212 if (!cioc
->blocking
&& !win32_fd_poll(cioc
->readfd
, G_IO_IN
)) {
213 return QIO_CHANNEL_ERR_BLOCK
;
218 ret
= readv(cioc
->readfd
, iov
, niov
);
220 if (errno
== EAGAIN
) {
221 return QIO_CHANNEL_ERR_BLOCK
;
223 if (errno
== EINTR
) {
227 error_setg_errno(errp
, errno
,
228 "Unable to read from command");
235 static ssize_t
qio_channel_command_writev(QIOChannel
*ioc
,
236 const struct iovec
*iov
,
243 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
247 if (!cioc
->blocking
&& !win32_fd_poll(cioc
->writefd
, G_IO_OUT
)) {
248 return QIO_CHANNEL_ERR_BLOCK
;
253 ret
= writev(cioc
->writefd
, iov
, niov
);
255 if (errno
== EAGAIN
) {
256 return QIO_CHANNEL_ERR_BLOCK
;
258 if (errno
== EINTR
) {
261 error_setg_errno(errp
, errno
, "%s",
262 "Unable to write to command");
268 static int qio_channel_command_set_blocking(QIOChannel
*ioc
,
272 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
275 cioc
->blocking
= enabled
;
278 if ((cioc
->writefd
>= 0 && !g_unix_set_fd_nonblocking(cioc
->writefd
, !enabled
, NULL
)) ||
279 (cioc
->readfd
>= 0 && !g_unix_set_fd_nonblocking(cioc
->readfd
, !enabled
, NULL
))) {
280 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
288 static int qio_channel_command_close(QIOChannel
*ioc
,
291 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
297 /* We close FDs before killing, because that
298 * gives a better chance of clean shutdown
300 if (cioc
->readfd
!= -1 &&
301 close(cioc
->readfd
) < 0) {
304 if (cioc
->writefd
!= -1 &&
305 cioc
->writefd
!= cioc
->readfd
&&
306 close(cioc
->writefd
) < 0) {
309 cioc
->writefd
= cioc
->readfd
= -1;
313 wp
= waitpid(cioc
->pid
, NULL
, 0);
314 } while (wp
== (pid_t
)-1 && errno
== EINTR
);
315 if (wp
== (pid_t
)-1) {
316 error_setg_errno(errp
, errno
, "Failed to wait for pid %llu",
317 (unsigned long long)cioc
->pid
);
321 WaitForSingleObject(cioc
->pid
, INFINITE
);
325 error_setg_errno(errp
, errno
, "%s",
326 "Unable to close command");
332 static void qio_channel_command_set_aio_fd_handler(QIOChannel
*ioc
,
338 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
339 aio_set_fd_handler(ctx
, cioc
->readfd
, false,
340 io_read
, NULL
, NULL
, NULL
, opaque
);
341 aio_set_fd_handler(ctx
, cioc
->writefd
, false,
342 NULL
, io_write
, NULL
, NULL
, opaque
);
346 static GSource
*qio_channel_command_create_watch(QIOChannel
*ioc
,
347 GIOCondition condition
)
349 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
350 return qio_channel_create_fd_pair_watch(ioc
,
357 static void qio_channel_command_class_init(ObjectClass
*klass
,
358 void *class_data G_GNUC_UNUSED
)
360 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
362 ioc_klass
->io_writev
= qio_channel_command_writev
;
363 ioc_klass
->io_readv
= qio_channel_command_readv
;
364 ioc_klass
->io_set_blocking
= qio_channel_command_set_blocking
;
365 ioc_klass
->io_close
= qio_channel_command_close
;
366 ioc_klass
->io_create_watch
= qio_channel_command_create_watch
;
367 ioc_klass
->io_set_aio_fd_handler
= qio_channel_command_set_aio_fd_handler
;
370 static const TypeInfo qio_channel_command_info
= {
371 .parent
= TYPE_QIO_CHANNEL
,
372 .name
= TYPE_QIO_CHANNEL_COMMAND
,
373 .instance_size
= sizeof(QIOChannelCommand
),
374 .instance_init
= qio_channel_command_init
,
375 .instance_finalize
= qio_channel_command_finalize
,
376 .class_init
= qio_channel_command_class_init
,
379 static void qio_channel_command_register_types(void)
381 type_register_static(&qio_channel_command_info
);
384 type_init(qio_channel_command_register_types
);