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