blob: a220fe886b184be66fe70c58209febe16c2346c7 [file] [log] [blame]
Daniel P. Berrange195e14d2015-08-27 16:25:30 +01001/*
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
9 * version 2 of the License, or (at your option) any later version.
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
21#include "io/channel-command.h"
22#include "io/channel-watch.h"
23#include "qemu/sockets.h"
24#include "trace.h"
25
26
27QIOChannelCommand *
28qio_channel_command_new_pid(int writefd,
29 int readfd,
30 pid_t pid)
31{
32 QIOChannelCommand *ioc;
33
34 ioc = QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND));
35
36 ioc->readfd = readfd;
37 ioc->writefd = writefd;
38 ioc->pid = pid;
39
40 trace_qio_channel_command_new_pid(ioc, writefd, readfd, pid);
41 return ioc;
42}
43
44
45#ifndef WIN32
46QIOChannelCommand *
47qio_channel_command_new_spawn(const char *const argv[],
48 int flags,
49 Error **errp)
50{
51 pid_t pid = -1;
52 int stdinfd[2] = { -1, -1 };
53 int stdoutfd[2] = { -1, -1 };
54 int devnull = -1;
55 bool stdinnull = false, stdoutnull = false;
56 QIOChannelCommand *ioc;
57
58 flags = flags & O_ACCMODE;
59
60 if (flags == O_RDONLY) {
61 stdinnull = true;
62 }
63 if (flags == O_WRONLY) {
64 stdoutnull = true;
65 }
66
67 if (stdinnull || stdoutnull) {
68 devnull = open("/dev/null", O_RDWR);
69 if (!devnull) {
70 error_setg_errno(errp, errno,
71 "Unable to open /dev/null");
72 goto error;
73 }
74 }
75
76 if ((!stdinnull && pipe(stdinfd) < 0) ||
77 (!stdoutnull && pipe(stdoutfd) < 0)) {
78 error_setg_errno(errp, errno,
79 "Unable to open pipe");
80 goto error;
81 }
82
83 pid = qemu_fork(errp);
84 if (pid < 0) {
85 goto error;
86 }
87
88 if (pid == 0) { /* child */
89 dup2(stdinnull ? devnull : stdinfd[0], STDIN_FILENO);
90 dup2(stdoutnull ? devnull : stdoutfd[1], STDOUT_FILENO);
91 /* Leave stderr connected to qemu's stderr */
92
93 if (!stdinnull) {
94 close(stdinfd[0]);
95 close(stdinfd[1]);
96 }
97 if (!stdoutnull) {
98 close(stdoutfd[0]);
99 close(stdoutfd[1]);
100 }
101
102 execv(argv[0], (char * const *)argv);
103 _exit(1);
104 }
105
106 if (!stdinnull) {
107 close(stdinfd[0]);
108 }
109 if (!stdoutnull) {
110 close(stdoutfd[1]);
111 }
112
113 ioc = qio_channel_command_new_pid(stdinnull ? devnull : stdinfd[1],
114 stdoutnull ? devnull : stdoutfd[0],
115 pid);
116 trace_qio_channel_command_new_spawn(ioc, argv[0], flags);
117 return ioc;
118
119 error:
120 if (stdinfd[0] != -1) {
121 close(stdinfd[0]);
122 }
123 if (stdinfd[1] != -1) {
124 close(stdinfd[1]);
125 }
126 if (stdoutfd[0] != -1) {
127 close(stdoutfd[0]);
128 }
129 if (stdoutfd[1] != -1) {
130 close(stdoutfd[1]);
131 }
132 return NULL;
133}
134
135#else /* WIN32 */
136QIOChannelCommand *
137qio_channel_command_new_spawn(const char *const argv[],
138 int flags,
139 Error **errp)
140{
141 error_setg_errno(errp, ENOSYS,
142 "Command spawn not supported on this platform");
143 return NULL;
144}
145#endif /* WIN32 */
146
147#ifndef WIN32
148static int qio_channel_command_abort(QIOChannelCommand *ioc,
149 Error **errp)
150{
151 pid_t ret;
152 int status;
153 int step = 0;
154
155 /* See if intermediate process has exited; if not, try a nice
156 * SIGTERM followed by a more severe SIGKILL.
157 */
158 rewait:
159 trace_qio_channel_command_abort(ioc, ioc->pid);
160 ret = waitpid(ioc->pid, &status, WNOHANG);
161 trace_qio_channel_command_wait(ioc, ioc->pid, ret, status);
162 if (ret == (pid_t)-1) {
163 if (errno == EINTR) {
164 goto rewait;
165 } else {
166 error_setg_errno(errp, errno,
167 "Cannot wait on pid %llu",
168 (unsigned long long)ioc->pid);
169 return -1;
170 }
171 } else if (ret == 0) {
172 if (step == 0) {
173 kill(ioc->pid, SIGTERM);
174 } else if (step == 1) {
175 kill(ioc->pid, SIGKILL);
176 } else {
177 error_setg(errp,
178 "Process %llu refused to die",
179 (unsigned long long)ioc->pid);
180 return -1;
181 }
Daniel P. Berrange0c0a55b2016-01-11 13:00:36 +0000182 step++;
Daniel P. Berrange195e14d2015-08-27 16:25:30 +0100183 usleep(10 * 1000);
184 goto rewait;
185 }
186
187 return 0;
188}
189#endif /* ! WIN32 */
190
191
192static void qio_channel_command_init(Object *obj)
193{
194 QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
195 ioc->readfd = -1;
196 ioc->writefd = -1;
197 ioc->pid = -1;
198}
199
200static void qio_channel_command_finalize(Object *obj)
201{
202 QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj);
203 if (ioc->readfd != -1) {
204 close(ioc->readfd);
205 ioc->readfd = -1;
206 }
207 if (ioc->writefd != -1) {
208 close(ioc->writefd);
209 ioc->writefd = -1;
210 }
211 if (ioc->pid > 0) {
212#ifndef WIN32
213 qio_channel_command_abort(ioc, NULL);
214#endif
215 }
216}
217
218
219static ssize_t qio_channel_command_readv(QIOChannel *ioc,
220 const struct iovec *iov,
221 size_t niov,
222 int **fds,
223 size_t *nfds,
224 Error **errp)
225{
226 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
227 ssize_t ret;
228
229 retry:
230 ret = readv(cioc->readfd, iov, niov);
231 if (ret < 0) {
232 if (errno == EAGAIN ||
233 errno == EWOULDBLOCK) {
234 return QIO_CHANNEL_ERR_BLOCK;
235 }
236 if (errno == EINTR) {
237 goto retry;
238 }
239
240 error_setg_errno(errp, errno,
241 "Unable to read from command");
242 return -1;
243 }
244
245 return ret;
246}
247
248static ssize_t qio_channel_command_writev(QIOChannel *ioc,
249 const struct iovec *iov,
250 size_t niov,
251 int *fds,
252 size_t nfds,
253 Error **errp)
254{
255 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
256 ssize_t ret;
257
258 retry:
259 ret = writev(cioc->writefd, iov, niov);
260 if (ret <= 0) {
261 if (errno == EAGAIN ||
262 errno == EWOULDBLOCK) {
263 return QIO_CHANNEL_ERR_BLOCK;
264 }
265 if (errno == EINTR) {
266 goto retry;
267 }
268 error_setg_errno(errp, errno, "%s",
269 "Unable to write to command");
270 return -1;
271 }
272 return ret;
273}
274
275static int qio_channel_command_set_blocking(QIOChannel *ioc,
276 bool enabled,
277 Error **errp)
278{
279 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
280
281 if (enabled) {
282 qemu_set_block(cioc->writefd);
283 qemu_set_block(cioc->readfd);
284 } else {
285 qemu_set_nonblock(cioc->writefd);
286 qemu_set_nonblock(cioc->readfd);
287 }
288
289 return 0;
290}
291
292
293static int qio_channel_command_close(QIOChannel *ioc,
294 Error **errp)
295{
296 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
297 int rv = 0;
298
299 /* We close FDs before killing, because that
300 * gives a better chance of clean shutdown
301 */
302 if (close(cioc->writefd) < 0) {
303 rv = -1;
304 }
305 if (close(cioc->readfd) < 0) {
306 rv = -1;
307 }
308#ifndef WIN32
309 if (qio_channel_command_abort(cioc, errp) < 0) {
310 return -1;
311 }
312#endif
313 if (rv < 0) {
314 error_setg_errno(errp, errno, "%s",
315 "Unable to close command");
316 }
317 return rv;
318}
319
320
321static GSource *qio_channel_command_create_watch(QIOChannel *ioc,
322 GIOCondition condition)
323{
324 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
325 return qio_channel_create_fd_pair_watch(ioc,
326 cioc->readfd,
327 cioc->writefd,
328 condition);
329}
330
331
332static void qio_channel_command_class_init(ObjectClass *klass,
333 void *class_data G_GNUC_UNUSED)
334{
335 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
336
337 ioc_klass->io_writev = qio_channel_command_writev;
338 ioc_klass->io_readv = qio_channel_command_readv;
339 ioc_klass->io_set_blocking = qio_channel_command_set_blocking;
340 ioc_klass->io_close = qio_channel_command_close;
341 ioc_klass->io_create_watch = qio_channel_command_create_watch;
342}
343
344static const TypeInfo qio_channel_command_info = {
345 .parent = TYPE_QIO_CHANNEL,
346 .name = TYPE_QIO_CHANNEL_COMMAND,
347 .instance_size = sizeof(QIOChannelCommand),
348 .instance_init = qio_channel_command_init,
349 .instance_finalize = qio_channel_command_finalize,
350 .class_init = qio_channel_command_class_init,
351};
352
353static void qio_channel_command_register_types(void)
354{
355 type_register_static(&qio_channel_command_info);
356}
357
358type_init(qio_channel_command_register_types);