]> git.proxmox.com Git - mirror_qemu.git/blame - io/channel-command.c
ppc440: Stop using system io region for PCIe buses
[mirror_qemu.git] / io / channel-command.c
CommitLineData
195e14d0
DB
1/*
2 * QEMU I/O channels external command driver
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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
c8198bd5 9 * version 2.1 of the License, or (at your option) any later version.
195e14d0
DB
10 *
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.
15 *
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/>.
18 *
19 */
20
cae9fc56 21#include "qemu/osdep.h"
195e14d0
DB
22#include "io/channel-command.h"
23#include "io/channel-watch.h"
da34e65c 24#include "qapi/error.h"
0b8fa32f 25#include "qemu/module.h"
195e14d0
DB
26#include "qemu/sockets.h"
27#include "trace.h"
28
05e50e8f
MAL
29/**
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
a95570e3 33 * @pid: the PID/HANDLE of the running child command
05e50e8f
MAL
34 * @errp: pointer to a NULL-initialized error object
35 *
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.
41 *
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.
46 *
47 * Returns: the command channel object, or NULL on error
48 */
49static QIOChannelCommand *
195e14d0
DB
50qio_channel_command_new_pid(int writefd,
51 int readfd,
a95570e3 52 GPid pid)
195e14d0
DB
53{
54 QIOChannelCommand *ioc;
55
56 ioc = QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND));
57
58 ioc->readfd = readfd;
59 ioc->writefd = writefd;
60 ioc->pid = pid;
61
ec5b6c9c
MAL
62 trace_qio_channel_command_new_pid(ioc, writefd, readfd,
63#ifdef WIN32
64 GetProcessId(pid)
65#else
66 pid
67#endif
68 );
195e14d0
DB
69 return ioc;
70}
71
195e14d0
DB
72QIOChannelCommand *
73qio_channel_command_new_spawn(const char *const argv[],
74 int flags,
75 Error **errp)
76{
a95570e3
MAL
77 g_autoptr(GError) err = NULL;
78 GPid pid = 0;
79 GSpawnFlags gflags = G_SPAWN_CLOEXEC_PIPES | G_SPAWN_DO_NOT_REAP_CHILD;
80 int stdinfd = -1, stdoutfd = -1;
195e14d0
DB
81
82 flags = flags & O_ACCMODE;
a95570e3 83 gflags |= flags == O_WRONLY ? G_SPAWN_STDOUT_TO_DEV_NULL : 0;
195e14d0 84
a95570e3
MAL
85 if (!g_spawn_async_with_pipes(NULL, (char **)argv, NULL, gflags, NULL, NULL,
86 &pid,
87 flags == O_RDONLY ? NULL : &stdinfd,
88 flags == O_WRONLY ? NULL : &stdoutfd,
89 NULL, &err)) {
90 error_setg(errp, "%s", err->message);
91 return NULL;
195e14d0
DB
92 }
93
a95570e3 94 return qio_channel_command_new_pid(stdinfd, stdoutfd, pid);
195e14d0
DB
95}
96
195e14d0
DB
97#ifndef WIN32
98static int qio_channel_command_abort(QIOChannelCommand *ioc,
99 Error **errp)
100{
101 pid_t ret;
102 int status;
103 int step = 0;
104
105 /* See if intermediate process has exited; if not, try a nice
106 * SIGTERM followed by a more severe SIGKILL.
107 */
108 rewait:
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) {
114 goto rewait;
115 } else {
116 error_setg_errno(errp, errno,
117 "Cannot wait on pid %llu",
118 (unsigned long long)ioc->pid);
119 return -1;
120 }
121 } else if (ret == 0) {
122 if (step == 0) {
123 kill(ioc->pid, SIGTERM);
124 } else if (step == 1) {
125 kill(ioc->pid, SIGKILL);
126 } else {
127 error_setg(errp,
128 "Process %llu refused to die",
129 (unsigned long long)ioc->pid);
130 return -1;
131 }
0c0a55b2 132 step++;
195e14d0
DB
133 usleep(10 * 1000);
134 goto rewait;
135 }
136
137 return 0;
138}
ec5b6c9c
MAL
139#else
140static int qio_channel_command_abort(QIOChannelCommand *ioc,
141 Error **errp)
142{
143 DWORD ret;
144
145 TerminateProcess(ioc->pid, 0);
146 ret = WaitForSingleObject(ioc->pid, 1000);
147 if (ret != WAIT_OBJECT_0) {
148 error_setg(errp,
149 "Process %llu refused to die",
150 (unsigned long long)GetProcessId(ioc->pid));
151 return -1;
152 }
153
154 return 0;
155}
195e14d0
DB
156#endif /* ! WIN32 */
157
158
159static void qio_channel_command_init(Object *obj)
160{
161 QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
162 ioc->readfd = -1;
163 ioc->writefd = -1;
a95570e3 164 ioc->pid = 0;
195e14d0
DB
165}
166
167static void qio_channel_command_finalize(Object *obj)
168{
169 QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
170 if (ioc->readfd != -1) {
171 close(ioc->readfd);
195e14d0 172 }
e155494c
DB
173 if (ioc->writefd != -1 &&
174 ioc->writefd != ioc->readfd) {
195e14d0 175 close(ioc->writefd);
195e14d0 176 }
e155494c 177 ioc->writefd = ioc->readfd = -1;
195e14d0 178 if (ioc->pid > 0) {
195e14d0 179 qio_channel_command_abort(ioc, NULL);
a95570e3 180 g_spawn_close_pid(ioc->pid);
195e14d0
DB
181 }
182}
183
ec5b6c9c
MAL
184#ifdef WIN32
185static bool win32_fd_poll(int fd, gushort events)
186{
187 GPollFD pfd = { .fd = _get_osfhandle(fd), .events = events };
188 int res;
189
190 do {
191 res = g_poll(&pfd, 1, 0);
192 } while (res < 0 && errno == EINTR);
193 if (res == 0) {
194 return false;
195 }
196
197 return true;
198}
199#endif
195e14d0
DB
200
201static ssize_t qio_channel_command_readv(QIOChannel *ioc,
202 const struct iovec *iov,
203 size_t niov,
204 int **fds,
205 size_t *nfds,
84615a19 206 int flags,
195e14d0
DB
207 Error **errp)
208{
209 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
210 ssize_t ret;
211
ec5b6c9c
MAL
212#ifdef WIN32
213 if (!cioc->blocking && !win32_fd_poll(cioc->readfd, G_IO_IN)) {
214 return QIO_CHANNEL_ERR_BLOCK;
215 }
216#endif
217
195e14d0
DB
218 retry:
219 ret = readv(cioc->readfd, iov, niov);
220 if (ret < 0) {
30fd3e27 221 if (errno == EAGAIN) {
195e14d0
DB
222 return QIO_CHANNEL_ERR_BLOCK;
223 }
224 if (errno == EINTR) {
225 goto retry;
226 }
227
228 error_setg_errno(errp, errno,
229 "Unable to read from command");
230 return -1;
231 }
232
233 return ret;
234}
235
236static ssize_t qio_channel_command_writev(QIOChannel *ioc,
237 const struct iovec *iov,
238 size_t niov,
239 int *fds,
240 size_t nfds,
b88651cb 241 int flags,
195e14d0
DB
242 Error **errp)
243{
244 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
245 ssize_t ret;
246
ec5b6c9c
MAL
247#ifdef WIN32
248 if (!cioc->blocking && !win32_fd_poll(cioc->writefd, G_IO_OUT)) {
249 return QIO_CHANNEL_ERR_BLOCK;
250 }
251#endif
252
195e14d0
DB
253 retry:
254 ret = writev(cioc->writefd, iov, niov);
255 if (ret <= 0) {
30fd3e27 256 if (errno == EAGAIN) {
195e14d0
DB
257 return QIO_CHANNEL_ERR_BLOCK;
258 }
259 if (errno == EINTR) {
260 goto retry;
261 }
262 error_setg_errno(errp, errno, "%s",
263 "Unable to write to command");
264 return -1;
265 }
266 return ret;
267}
268
269static int qio_channel_command_set_blocking(QIOChannel *ioc,
270 bool enabled,
271 Error **errp)
272{
ec5b6c9c
MAL
273 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
274
17fc1245 275#ifdef WIN32
ec5b6c9c 276 cioc->blocking = enabled;
17fc1245 277#else
195e14d0 278
ec5b6c9c
MAL
279 if ((cioc->writefd >= 0 && !g_unix_set_fd_nonblocking(cioc->writefd, !enabled, NULL)) ||
280 (cioc->readfd >= 0 && !g_unix_set_fd_nonblocking(cioc->readfd, !enabled, NULL))) {
17fc1245
MAL
281 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
282 return -1;
195e14d0 283 }
17fc1245 284#endif
195e14d0
DB
285 return 0;
286}
287
288
289static int qio_channel_command_close(QIOChannel *ioc,
290 Error **errp)
291{
292 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
293 int rv = 0;
fe823b6f
TH
294#ifndef WIN32
295 pid_t wp;
296#endif
195e14d0
DB
297
298 /* We close FDs before killing, because that
299 * gives a better chance of clean shutdown
300 */
e155494c
DB
301 if (cioc->readfd != -1 &&
302 close(cioc->readfd) < 0) {
195e14d0
DB
303 rv = -1;
304 }
e155494c
DB
305 if (cioc->writefd != -1 &&
306 cioc->writefd != cioc->readfd &&
307 close(cioc->writefd) < 0) {
195e14d0
DB
308 rv = -1;
309 }
e155494c 310 cioc->writefd = cioc->readfd = -1;
fe823b6f 311
195e14d0 312#ifndef WIN32
fe823b6f
TH
313 do {
314 wp = waitpid(cioc->pid, NULL, 0);
315 } while (wp == (pid_t)-1 && errno == EINTR);
316 if (wp == (pid_t)-1) {
317 error_setg_errno(errp, errno, "Failed to wait for pid %llu",
318 (unsigned long long)cioc->pid);
195e14d0
DB
319 return -1;
320 }
ec5b6c9c
MAL
321#else
322 WaitForSingleObject(cioc->pid, INFINITE);
195e14d0 323#endif
fe823b6f 324
195e14d0
DB
325 if (rv < 0) {
326 error_setg_errno(errp, errno, "%s",
327 "Unable to close command");
328 }
329 return rv;
330}
331
332
bf88c124
PB
333static void qio_channel_command_set_aio_fd_handler(QIOChannel *ioc,
334 AioContext *ctx,
335 IOHandler *io_read,
336 IOHandler *io_write,
337 void *opaque)
338{
339 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
60f782b6
SH
340 aio_set_fd_handler(ctx, cioc->readfd, io_read, NULL, NULL, NULL, opaque);
341 aio_set_fd_handler(ctx, cioc->writefd, NULL, io_write, NULL, NULL, opaque);
bf88c124
PB
342}
343
344
195e14d0
DB
345static GSource *qio_channel_command_create_watch(QIOChannel *ioc,
346 GIOCondition condition)
347{
348 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
349 return qio_channel_create_fd_pair_watch(ioc,
350 cioc->readfd,
351 cioc->writefd,
352 condition);
353}
354
355
356static void qio_channel_command_class_init(ObjectClass *klass,
357 void *class_data G_GNUC_UNUSED)
358{
359 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
360
361 ioc_klass->io_writev = qio_channel_command_writev;
362 ioc_klass->io_readv = qio_channel_command_readv;
363 ioc_klass->io_set_blocking = qio_channel_command_set_blocking;
364 ioc_klass->io_close = qio_channel_command_close;
365 ioc_klass->io_create_watch = qio_channel_command_create_watch;
bf88c124 366 ioc_klass->io_set_aio_fd_handler = qio_channel_command_set_aio_fd_handler;
195e14d0
DB
367}
368
369static const TypeInfo qio_channel_command_info = {
370 .parent = TYPE_QIO_CHANNEL,
371 .name = TYPE_QIO_CHANNEL_COMMAND,
372 .instance_size = sizeof(QIOChannelCommand),
373 .instance_init = qio_channel_command_init,
374 .instance_finalize = qio_channel_command_finalize,
375 .class_init = qio_channel_command_class_init,
376};
377
378static void qio_channel_command_register_types(void)
379{
380 type_register_static(&qio_channel_command_info);
381}
382
383type_init(qio_channel_command_register_types);