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 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 "qemu/sockets.h"
29 qio_channel_command_new_pid(int writefd
,
33 QIOChannelCommand
*ioc
;
35 ioc
= QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND
));
38 ioc
->writefd
= writefd
;
41 trace_qio_channel_command_new_pid(ioc
, writefd
, readfd
, pid
);
48 qio_channel_command_new_spawn(const char *const argv
[],
53 int stdinfd
[2] = { -1, -1 };
54 int stdoutfd
[2] = { -1, -1 };
56 bool stdinnull
= false, stdoutnull
= false;
57 QIOChannelCommand
*ioc
;
59 flags
= flags
& O_ACCMODE
;
61 if (flags
== O_RDONLY
) {
64 if (flags
== O_WRONLY
) {
68 if (stdinnull
|| stdoutnull
) {
69 devnull
= open("/dev/null", O_RDWR
);
71 error_setg_errno(errp
, errno
,
72 "Unable to open /dev/null");
77 if ((!stdinnull
&& pipe(stdinfd
) < 0) ||
78 (!stdoutnull
&& pipe(stdoutfd
) < 0)) {
79 error_setg_errno(errp
, errno
,
80 "Unable to open pipe");
84 pid
= qemu_fork(errp
);
89 if (pid
== 0) { /* child */
90 dup2(stdinnull
? devnull
: stdinfd
[0], STDIN_FILENO
);
91 dup2(stdoutnull
? devnull
: stdoutfd
[1], STDOUT_FILENO
);
92 /* Leave stderr connected to qemu's stderr */
106 execv(argv
[0], (char * const *)argv
);
117 ioc
= qio_channel_command_new_pid(stdinnull
? devnull
: stdinfd
[1],
118 stdoutnull
? devnull
: stdoutfd
[0],
120 trace_qio_channel_command_new_spawn(ioc
, argv
[0], flags
);
127 if (stdinfd
[0] != -1) {
130 if (stdinfd
[1] != -1) {
133 if (stdoutfd
[0] != -1) {
136 if (stdoutfd
[1] != -1) {
144 qio_channel_command_new_spawn(const char *const argv
[],
148 error_setg_errno(errp
, ENOSYS
,
149 "Command spawn not supported on this platform");
155 static int qio_channel_command_abort(QIOChannelCommand
*ioc
,
162 /* See if intermediate process has exited; if not, try a nice
163 * SIGTERM followed by a more severe SIGKILL.
166 trace_qio_channel_command_abort(ioc
, ioc
->pid
);
167 ret
= waitpid(ioc
->pid
, &status
, WNOHANG
);
168 trace_qio_channel_command_wait(ioc
, ioc
->pid
, ret
, status
);
169 if (ret
== (pid_t
)-1) {
170 if (errno
== EINTR
) {
173 error_setg_errno(errp
, errno
,
174 "Cannot wait on pid %llu",
175 (unsigned long long)ioc
->pid
);
178 } else if (ret
== 0) {
180 kill(ioc
->pid
, SIGTERM
);
181 } else if (step
== 1) {
182 kill(ioc
->pid
, SIGKILL
);
185 "Process %llu refused to die",
186 (unsigned long long)ioc
->pid
);
199 static void qio_channel_command_init(Object
*obj
)
201 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
207 static void qio_channel_command_finalize(Object
*obj
)
209 QIOChannelCommand
*ioc
= QIO_CHANNEL_COMMAND(obj
);
210 if (ioc
->readfd
!= -1) {
213 if (ioc
->writefd
!= -1 &&
214 ioc
->writefd
!= ioc
->readfd
) {
217 ioc
->writefd
= ioc
->readfd
= -1;
220 qio_channel_command_abort(ioc
, NULL
);
226 static ssize_t
qio_channel_command_readv(QIOChannel
*ioc
,
227 const struct iovec
*iov
,
233 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
237 ret
= readv(cioc
->readfd
, iov
, niov
);
239 if (errno
== EAGAIN
) {
240 return QIO_CHANNEL_ERR_BLOCK
;
242 if (errno
== EINTR
) {
246 error_setg_errno(errp
, errno
,
247 "Unable to read from command");
254 static ssize_t
qio_channel_command_writev(QIOChannel
*ioc
,
255 const struct iovec
*iov
,
261 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
265 ret
= writev(cioc
->writefd
, iov
, niov
);
267 if (errno
== EAGAIN
) {
268 return QIO_CHANNEL_ERR_BLOCK
;
270 if (errno
== EINTR
) {
273 error_setg_errno(errp
, errno
, "%s",
274 "Unable to write to command");
280 static int qio_channel_command_set_blocking(QIOChannel
*ioc
,
284 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
287 qemu_set_block(cioc
->writefd
);
288 qemu_set_block(cioc
->readfd
);
290 qemu_set_nonblock(cioc
->writefd
);
291 qemu_set_nonblock(cioc
->readfd
);
298 static int qio_channel_command_close(QIOChannel
*ioc
,
301 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
304 /* We close FDs before killing, because that
305 * gives a better chance of clean shutdown
307 if (cioc
->readfd
!= -1 &&
308 close(cioc
->readfd
) < 0) {
311 if (cioc
->writefd
!= -1 &&
312 cioc
->writefd
!= cioc
->readfd
&&
313 close(cioc
->writefd
) < 0) {
316 cioc
->writefd
= cioc
->readfd
= -1;
318 if (qio_channel_command_abort(cioc
, errp
) < 0) {
323 error_setg_errno(errp
, errno
, "%s",
324 "Unable to close command");
330 static GSource
*qio_channel_command_create_watch(QIOChannel
*ioc
,
331 GIOCondition condition
)
333 QIOChannelCommand
*cioc
= QIO_CHANNEL_COMMAND(ioc
);
334 return qio_channel_create_fd_pair_watch(ioc
,
341 static void qio_channel_command_class_init(ObjectClass
*klass
,
342 void *class_data G_GNUC_UNUSED
)
344 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
346 ioc_klass
->io_writev
= qio_channel_command_writev
;
347 ioc_klass
->io_readv
= qio_channel_command_readv
;
348 ioc_klass
->io_set_blocking
= qio_channel_command_set_blocking
;
349 ioc_klass
->io_close
= qio_channel_command_close
;
350 ioc_klass
->io_create_watch
= qio_channel_command_create_watch
;
353 static const TypeInfo qio_channel_command_info
= {
354 .parent
= TYPE_QIO_CHANNEL
,
355 .name
= TYPE_QIO_CHANNEL_COMMAND
,
356 .instance_size
= sizeof(QIOChannelCommand
),
357 .instance_init
= qio_channel_command_init
,
358 .instance_finalize
= qio_channel_command_finalize
,
359 .class_init
= qio_channel_command_class_init
,
362 static void qio_channel_command_register_types(void)
364 type_register_static(&qio_channel_command_info
);
367 type_init(qio_channel_command_register_types
);