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(int writefd
,
35 QIOChannelCommand
*ioc
;
37 ioc
= QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND
));
40 ioc
->writefd
= writefd
;
43 trace_qio_channel_command_new_pid(ioc
, writefd
, readfd
, pid
);
50 qio_channel_command_new_spawn(const char *const argv
[],
55 int stdinfd
[2] = { -1, -1 };
56 int stdoutfd
[2] = { -1, -1 };
58 bool stdinnull
= false, stdoutnull
= false;
59 QIOChannelCommand
*ioc
;
61 flags
= flags
& O_ACCMODE
;
63 if (flags
== O_RDONLY
) {
66 if (flags
== O_WRONLY
) {
70 if (stdinnull
|| stdoutnull
) {
71 devnull
= open("/dev/null", O_RDWR
);
73 error_setg_errno(errp
, errno
,
74 "Unable to open /dev/null");
79 if ((!stdinnull
&& pipe(stdinfd
) < 0) ||
80 (!stdoutnull
&& pipe(stdoutfd
) < 0)) {
81 error_setg_errno(errp
, errno
,
82 "Unable to open pipe");
86 pid
= qemu_fork(errp
);
91 if (pid
== 0) { /* child */
92 dup2(stdinnull
? devnull
: stdinfd
[0], STDIN_FILENO
);
93 dup2(stdoutnull
? devnull
: stdoutfd
[1], STDOUT_FILENO
);
94 /* Leave stderr connected to qemu's stderr */
108 execv(argv
[0], (char * const *)argv
);
119 ioc
= qio_channel_command_new_pid(stdinnull
? devnull
: stdinfd
[1],
120 stdoutnull
? devnull
: stdoutfd
[0],
122 trace_qio_channel_command_new_spawn(ioc
, argv
[0], flags
);
129 if (stdinfd
[0] != -1) {
132 if (stdinfd
[1] != -1) {
135 if (stdoutfd
[0] != -1) {
138 if (stdoutfd
[1] != -1) {
146 qio_channel_command_new_spawn(const char *const argv
[],
150 error_setg_errno(errp
, ENOSYS
,
151 "Command spawn not supported on this platform");
157 static int qio_channel_command_abort(QIOChannelCommand
*ioc
,
164 /* See if intermediate process has exited; if not, try a nice
165 * SIGTERM followed by a more severe SIGKILL.
168 trace_qio_channel_command_abort(ioc
, ioc
->pid
);
169 ret
= waitpid(ioc
->pid
, &status
, WNOHANG
);
170 trace_qio_channel_command_wait(ioc
, ioc
->pid
, ret
, status
);
171 if (ret
== (pid_t
)-1) {
172 if (errno
== EINTR
) {
175 error_setg_errno(errp
, errno
,
176 "Cannot wait on pid %llu",
177 (unsigned long long)ioc
->pid
);
180 } else if (ret
== 0) {
182 kill(ioc
->pid
, SIGTERM
);
183 } else if (step
== 1) {
184 kill(ioc
->pid
, SIGKILL
);
187 "Process %llu refused to die",
188 (unsigned long long)ioc
->pid
);
201 static void qio_channel_command_init(Object
*obj
)
203 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
209 static void qio_channel_command_finalize(Object
*obj
)
211 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
212 if (ioc
->readfd
!= -1) {
215 if (ioc
->writefd
!= -1 &&
216 ioc
->writefd
!= ioc
->readfd
) {
219 ioc
->writefd
= ioc
->readfd
= -1;
222 qio_channel_command_abort(ioc
, NULL
);
228 static ssize_t
qio_channel_command_readv(QIOChannel
*ioc
,
229 const struct iovec
*iov
,
235 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
239 ret
= readv(cioc
->readfd
, iov
, niov
);
241 if (errno
== EAGAIN
) {
242 return QIO_CHANNEL_ERR_BLOCK
;
244 if (errno
== EINTR
) {
248 error_setg_errno(errp
, errno
,
249 "Unable to read from command");
256 static ssize_t
qio_channel_command_writev(QIOChannel
*ioc
,
257 const struct iovec
*iov
,
263 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
267 ret
= writev(cioc
->writefd
, iov
, niov
);
269 if (errno
== EAGAIN
) {
270 return QIO_CHANNEL_ERR_BLOCK
;
272 if (errno
== EINTR
) {
275 error_setg_errno(errp
, errno
, "%s",
276 "Unable to write to command");
282 static int qio_channel_command_set_blocking(QIOChannel
*ioc
,
286 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
289 qemu_set_block(cioc
->writefd
);
290 qemu_set_block(cioc
->readfd
);
292 qemu_set_nonblock(cioc
->writefd
);
293 qemu_set_nonblock(cioc
->readfd
);
300 static int qio_channel_command_close(QIOChannel
*ioc
,
303 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
309 /* We close FDs before killing, because that
310 * gives a better chance of clean shutdown
312 if (cioc
->readfd
!= -1 &&
313 close(cioc
->readfd
) < 0) {
316 if (cioc
->writefd
!= -1 &&
317 cioc
->writefd
!= cioc
->readfd
&&
318 close(cioc
->writefd
) < 0) {
321 cioc
->writefd
= cioc
->readfd
= -1;
325 wp
= waitpid(cioc
->pid
, NULL
, 0);
326 } while (wp
== (pid_t
)-1 && errno
== EINTR
);
327 if (wp
== (pid_t
)-1) {
328 error_setg_errno(errp
, errno
, "Failed to wait for pid %llu",
329 (unsigned long long)cioc
->pid
);
335 error_setg_errno(errp
, errno
, "%s",
336 "Unable to close command");
342 static void qio_channel_command_set_aio_fd_handler(QIOChannel
*ioc
,
348 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
349 aio_set_fd_handler(ctx
, cioc
->readfd
, false,
350 io_read
, NULL
, NULL
, NULL
, opaque
);
351 aio_set_fd_handler(ctx
, cioc
->writefd
, false,
352 NULL
, io_write
, NULL
, NULL
, opaque
);
356 static GSource
*qio_channel_command_create_watch(QIOChannel
*ioc
,
357 GIOCondition condition
)
359 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
360 return qio_channel_create_fd_pair_watch(ioc
,
367 static void qio_channel_command_class_init(ObjectClass
*klass
,
368 void *class_data G_GNUC_UNUSED
)
370 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
372 ioc_klass
->io_writev
= qio_channel_command_writev
;
373 ioc_klass
->io_readv
= qio_channel_command_readv
;
374 ioc_klass
->io_set_blocking
= qio_channel_command_set_blocking
;
375 ioc_klass
->io_close
= qio_channel_command_close
;
376 ioc_klass
->io_create_watch
= qio_channel_command_create_watch
;
377 ioc_klass
->io_set_aio_fd_handler
= qio_channel_command_set_aio_fd_handler
;
380 static const TypeInfo qio_channel_command_info
= {
381 .parent
= TYPE_QIO_CHANNEL
,
382 .name
= TYPE_QIO_CHANNEL_COMMAND
,
383 .instance_size
= sizeof(QIOChannelCommand
),
384 .instance_init
= qio_channel_command_init
,
385 .instance_finalize
= qio_channel_command_finalize
,
386 .class_init
= qio_channel_command_class_init
,
389 static void qio_channel_command_register_types(void)
391 type_register_static(&qio_channel_command_info
);
394 type_init(qio_channel_command_register_types
);