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"
31 * qio_channel_command_new_pid:
32 * @writefd: the FD connected to the command's stdin
33 * @readfd: the FD connected to the command's stdout
34 * @pid: the PID of the running child command
35 * @errp: pointer to a NULL-initialized error object
37 * Create a channel for performing I/O with the
38 * previously spawned command identified by @pid.
39 * The two file descriptors provide the connection
40 * to command's stdio streams, either one or which
41 * may be -1 to indicate that stream is not open.
43 * The channel will take ownership of the process
44 * @pid and will kill it when closing the channel.
45 * Similarly it will take responsibility for
46 * closing the file descriptors @writefd and @readfd.
48 * Returns: the command channel object, or NULL on error
50 static QIOChannelCommand
*
51 qio_channel_command_new_pid(int writefd
,
55 QIOChannelCommand
*ioc
;
57 ioc
= QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND
));
60 ioc
->writefd
= writefd
;
63 trace_qio_channel_command_new_pid(ioc
, writefd
, readfd
, pid
);
68 qio_channel_command_new_spawn(const char *const argv
[],
73 int stdinfd
[2] = { -1, -1 };
74 int stdoutfd
[2] = { -1, -1 };
76 bool stdinnull
= false, stdoutnull
= false;
77 QIOChannelCommand
*ioc
;
79 flags
= flags
& O_ACCMODE
;
81 if (flags
== O_RDONLY
) {
84 if (flags
== O_WRONLY
) {
88 if (stdinnull
|| stdoutnull
) {
89 devnull
= open("/dev/null", O_RDWR
);
91 error_setg_errno(errp
, errno
,
92 "Unable to open /dev/null");
97 if ((!stdinnull
&& !g_unix_open_pipe(stdinfd
, FD_CLOEXEC
, NULL
)) ||
98 (!stdoutnull
&& !g_unix_open_pipe(stdoutfd
, FD_CLOEXEC
, NULL
))) {
99 error_setg_errno(errp
, errno
,
100 "Unable to open pipe");
104 pid
= qemu_fork(errp
);
109 if (pid
== 0) { /* child */
110 dup2(stdinnull
? devnull
: stdinfd
[0], STDIN_FILENO
);
111 dup2(stdoutnull
? devnull
: stdoutfd
[1], STDOUT_FILENO
);
112 /* Leave stderr connected to qemu's stderr */
126 execv(argv
[0], (char * const *)argv
);
137 ioc
= qio_channel_command_new_pid(stdinnull
? devnull
: stdinfd
[1],
138 stdoutnull
? devnull
: stdoutfd
[0],
140 trace_qio_channel_command_new_spawn(ioc
, argv
[0], flags
);
147 if (stdinfd
[0] != -1) {
150 if (stdinfd
[1] != -1) {
153 if (stdoutfd
[0] != -1) {
156 if (stdoutfd
[1] != -1) {
164 qio_channel_command_new_spawn(const char *const argv
[],
168 error_setg_errno(errp
, ENOSYS
,
169 "Command spawn not supported on this platform");
175 static int qio_channel_command_abort(QIOChannelCommand
*ioc
,
182 /* See if intermediate process has exited; if not, try a nice
183 * SIGTERM followed by a more severe SIGKILL.
186 trace_qio_channel_command_abort(ioc
, ioc
->pid
);
187 ret
= waitpid(ioc
->pid
, &status
, WNOHANG
);
188 trace_qio_channel_command_wait(ioc
, ioc
->pid
, ret
, status
);
189 if (ret
== (pid_t
)-1) {
190 if (errno
== EINTR
) {
193 error_setg_errno(errp
, errno
,
194 "Cannot wait on pid %llu",
195 (unsigned long long)ioc
->pid
);
198 } else if (ret
== 0) {
200 kill(ioc
->pid
, SIGTERM
);
201 } else if (step
== 1) {
202 kill(ioc
->pid
, SIGKILL
);
205 "Process %llu refused to die",
206 (unsigned long long)ioc
->pid
);
219 static void qio_channel_command_init(Object
*obj
)
221 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
227 static void qio_channel_command_finalize(Object
*obj
)
229 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
230 if (ioc
->readfd
!= -1) {
233 if (ioc
->writefd
!= -1 &&
234 ioc
->writefd
!= ioc
->readfd
) {
237 ioc
->writefd
= ioc
->readfd
= -1;
240 qio_channel_command_abort(ioc
, NULL
);
246 static ssize_t
qio_channel_command_readv(QIOChannel
*ioc
,
247 const struct iovec
*iov
,
253 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
257 ret
= readv(cioc
->readfd
, iov
, niov
);
259 if (errno
== EAGAIN
) {
260 return QIO_CHANNEL_ERR_BLOCK
;
262 if (errno
== EINTR
) {
266 error_setg_errno(errp
, errno
,
267 "Unable to read from command");
274 static ssize_t
qio_channel_command_writev(QIOChannel
*ioc
,
275 const struct iovec
*iov
,
282 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
286 ret
= writev(cioc
->writefd
, iov
, niov
);
288 if (errno
== EAGAIN
) {
289 return QIO_CHANNEL_ERR_BLOCK
;
291 if (errno
== EINTR
) {
294 error_setg_errno(errp
, errno
, "%s",
295 "Unable to write to command");
301 static int qio_channel_command_set_blocking(QIOChannel
*ioc
,
306 /* command spawn is not supported on win32 */
307 g_assert_not_reached();
309 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
311 if (!g_unix_set_fd_nonblocking(cioc
->writefd
, !enabled
, NULL
) ||
312 !g_unix_set_fd_nonblocking(cioc
->readfd
, !enabled
, NULL
)) {
313 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
321 static int qio_channel_command_close(QIOChannel
*ioc
,
324 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
330 /* We close FDs before killing, because that
331 * gives a better chance of clean shutdown
333 if (cioc
->readfd
!= -1 &&
334 close(cioc
->readfd
) < 0) {
337 if (cioc
->writefd
!= -1 &&
338 cioc
->writefd
!= cioc
->readfd
&&
339 close(cioc
->writefd
) < 0) {
342 cioc
->writefd
= cioc
->readfd
= -1;
346 wp
= waitpid(cioc
->pid
, NULL
, 0);
347 } while (wp
== (pid_t
)-1 && errno
== EINTR
);
348 if (wp
== (pid_t
)-1) {
349 error_setg_errno(errp
, errno
, "Failed to wait for pid %llu",
350 (unsigned long long)cioc
->pid
);
356 error_setg_errno(errp
, errno
, "%s",
357 "Unable to close command");
363 static void qio_channel_command_set_aio_fd_handler(QIOChannel
*ioc
,
369 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
370 aio_set_fd_handler(ctx
, cioc
->readfd
, false,
371 io_read
, NULL
, NULL
, NULL
, opaque
);
372 aio_set_fd_handler(ctx
, cioc
->writefd
, false,
373 NULL
, io_write
, NULL
, NULL
, opaque
);
377 static GSource
*qio_channel_command_create_watch(QIOChannel
*ioc
,
378 GIOCondition condition
)
380 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
381 return qio_channel_create_fd_pair_watch(ioc
,
388 static void qio_channel_command_class_init(ObjectClass
*klass
,
389 void *class_data G_GNUC_UNUSED
)
391 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
393 ioc_klass
->io_writev
= qio_channel_command_writev
;
394 ioc_klass
->io_readv
= qio_channel_command_readv
;
395 ioc_klass
->io_set_blocking
= qio_channel_command_set_blocking
;
396 ioc_klass
->io_close
= qio_channel_command_close
;
397 ioc_klass
->io_create_watch
= qio_channel_command_create_watch
;
398 ioc_klass
->io_set_aio_fd_handler
= qio_channel_command_set_aio_fd_handler
;
401 static const TypeInfo qio_channel_command_info
= {
402 .parent
= TYPE_QIO_CHANNEL
,
403 .name
= TYPE_QIO_CHANNEL_COMMAND
,
404 .instance_size
= sizeof(QIOChannelCommand
),
405 .instance_init
= qio_channel_command_init
,
406 .instance_finalize
= qio_channel_command_finalize
,
407 .class_init
= qio_channel_command_class_init
,
410 static void qio_channel_command_register_types(void)
412 type_register_static(&qio_channel_command_info
);
415 type_init(qio_channel_command_register_types
);