21
#include "qemu/osdep.h"
22
#include "io/channel-command.h"
23
#include "io/channel-util.h"
24
#include "io/channel-watch.h"
25
#include "qapi/error.h"
26
#include "qemu/module.h"
27
#include "qemu/sockets.h"
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,
74
qio_channel_command_new_spawn(const char *const argv[],
78
g_autoptr(GError) err = NULL;
80
GSpawnFlags gflags = G_SPAWN_CLOEXEC_PIPES | G_SPAWN_DO_NOT_REAP_CHILD;
81
int stdinfd = -1, stdoutfd = -1;
83
flags = flags & O_ACCMODE;
84
gflags |= flags == O_WRONLY ? G_SPAWN_STDOUT_TO_DEV_NULL : 0;
86
if (!g_spawn_async_with_pipes(NULL, (char **)argv, NULL, gflags, NULL, NULL,
88
flags == O_RDONLY ? NULL : &stdinfd,
89
flags == O_WRONLY ? NULL : &stdoutfd,
91
error_setg(errp, "%s", err->message);
95
return qio_channel_command_new_pid(stdinfd, stdoutfd, pid);
99
static int qio_channel_command_abort(QIOChannelCommand *ioc,
110
trace_qio_channel_command_abort(ioc, ioc->pid);
111
ret = waitpid(ioc->pid, &status, WNOHANG);
112
trace_qio_channel_command_wait(ioc, ioc->pid, ret, status);
113
if (ret == (pid_t)-1) {
114
if (errno == EINTR) {
117
error_setg_errno(errp, errno,
118
"Cannot wait on pid %llu",
119
(unsigned long long)ioc->pid);
122
} else if (ret == 0) {
124
kill(ioc->pid, SIGTERM);
125
} else if (step == 1) {
126
kill(ioc->pid, SIGKILL);
129
"Process %llu refused to die",
130
(unsigned long long)ioc->pid);
141
static int qio_channel_command_abort(QIOChannelCommand *ioc,
146
TerminateProcess(ioc->pid, 0);
147
ret = WaitForSingleObject(ioc->pid, 1000);
148
if (ret != WAIT_OBJECT_0) {
150
"Process %llu refused to die",
151
(unsigned long long)GetProcessId(ioc->pid));
160
static void qio_channel_command_init(Object *obj)
162
QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
168
static void qio_channel_command_finalize(Object *obj)
170
QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
171
if (ioc->readfd != -1) {
174
if (ioc->writefd != -1 &&
175
ioc->writefd != ioc->readfd) {
178
ioc->writefd = ioc->readfd = -1;
180
qio_channel_command_abort(ioc, NULL);
181
g_spawn_close_pid(ioc->pid);
186
static bool win32_fd_poll(int fd, gushort events)
188
GPollFD pfd = { .fd = _get_osfhandle(fd), .events = events };
192
res = g_poll(&pfd, 1, 0);
193
} while (res < 0 && errno == EINTR);
202
static ssize_t qio_channel_command_readv(QIOChannel *ioc,
203
const struct iovec *iov,
210
QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
214
if (!cioc->blocking && !win32_fd_poll(cioc->readfd, G_IO_IN)) {
215
return QIO_CHANNEL_ERR_BLOCK;
220
ret = readv(cioc->readfd, iov, niov);
222
if (errno == EAGAIN) {
223
return QIO_CHANNEL_ERR_BLOCK;
225
if (errno == EINTR) {
229
error_setg_errno(errp, errno,
230
"Unable to read from command");
237
static ssize_t qio_channel_command_writev(QIOChannel *ioc,
238
const struct iovec *iov,
245
QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
249
if (!cioc->blocking && !win32_fd_poll(cioc->writefd, G_IO_OUT)) {
250
return QIO_CHANNEL_ERR_BLOCK;
255
ret = writev(cioc->writefd, iov, niov);
257
if (errno == EAGAIN) {
258
return QIO_CHANNEL_ERR_BLOCK;
260
if (errno == EINTR) {
263
error_setg_errno(errp, errno, "%s",
264
"Unable to write to command");
270
static int qio_channel_command_set_blocking(QIOChannel *ioc,
274
QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
277
cioc->blocking = enabled;
280
if ((cioc->writefd >= 0 && !g_unix_set_fd_nonblocking(cioc->writefd, !enabled, NULL)) ||
281
(cioc->readfd >= 0 && !g_unix_set_fd_nonblocking(cioc->readfd, !enabled, NULL))) {
282
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
290
static int qio_channel_command_close(QIOChannel *ioc,
293
QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
302
if (cioc->readfd != -1 &&
303
close(cioc->readfd) < 0) {
306
if (cioc->writefd != -1 &&
307
cioc->writefd != cioc->readfd &&
308
close(cioc->writefd) < 0) {
311
cioc->writefd = cioc->readfd = -1;
315
wp = waitpid(cioc->pid, NULL, 0);
316
} while (wp == (pid_t)-1 && errno == EINTR);
317
if (wp == (pid_t)-1) {
318
error_setg_errno(errp, errno, "Failed to wait for pid %llu",
319
(unsigned long long)cioc->pid);
323
WaitForSingleObject(cioc->pid, INFINITE);
327
error_setg_errno(errp, errno, "%s",
328
"Unable to close command");
334
static void qio_channel_command_set_aio_fd_handler(QIOChannel *ioc,
335
AioContext *read_ctx,
337
AioContext *write_ctx,
341
QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
343
qio_channel_util_set_aio_fd_handler(cioc->readfd, read_ctx, io_read,
344
cioc->writefd, write_ctx, io_write,
349
static GSource *qio_channel_command_create_watch(QIOChannel *ioc,
350
GIOCondition condition)
352
QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
353
return qio_channel_create_fd_pair_watch(ioc,
360
static void qio_channel_command_class_init(ObjectClass *klass,
361
void *class_data G_GNUC_UNUSED)
363
QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
365
ioc_klass->io_writev = qio_channel_command_writev;
366
ioc_klass->io_readv = qio_channel_command_readv;
367
ioc_klass->io_set_blocking = qio_channel_command_set_blocking;
368
ioc_klass->io_close = qio_channel_command_close;
369
ioc_klass->io_create_watch = qio_channel_command_create_watch;
370
ioc_klass->io_set_aio_fd_handler = qio_channel_command_set_aio_fd_handler;
373
static const TypeInfo qio_channel_command_info = {
374
.parent = TYPE_QIO_CHANNEL,
375
.name = TYPE_QIO_CHANNEL_COMMAND,
376
.instance_size = sizeof(QIOChannelCommand),
377
.instance_init = qio_channel_command_init,
378
.instance_finalize = qio_channel_command_finalize,
379
.class_init = qio_channel_command_class_init,
382
static void qio_channel_command_register_types(void)
384
type_register_static(&qio_channel_command_info);
387
type_init(qio_channel_command_register_types);