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.1 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 "qapi/error.h" 25 #include "qemu/module.h" 26 #include "qemu/sockets.h" 27 #include "trace.h" 28 29 #ifndef WIN32 30 /** 31 * qio_channel_command_new_pid: 32 * @writefd: the FD connected to the command's stdin 33 * @readfd: the FD connected to the command's stdout 34 * @pid: the PID/HANDLE of the running child command 35 * @errp: pointer to a NULL-initialized error object 36 * 37 * Create a channel for performing I/O with the 38 * previously spawned command identified by @pid. 39 * The two file descriptors provide the connection 40 * to command's stdio streams, either one or which 41 * may be -1 to indicate that stream is not open. 42 * 43 * The channel will take ownership of the process 44 * @pid and will kill it when closing the channel. 45 * Similarly it will take responsibility for 46 * closing the file descriptors @writefd and @readfd. 47 * 48 * Returns: the command channel object, or NULL on error 49 */ 50 static QIOChannelCommand * 51 qio_channel_command_new_pid(int writefd, 52 int readfd, 53 GPid pid) 54 { 55 QIOChannelCommand *ioc; 56 57 ioc = QIO_CHANNEL_COMMAND(object_new(TYPE_QIO_CHANNEL_COMMAND)); 58 59 ioc->readfd = readfd; 60 ioc->writefd = writefd; 61 ioc->pid = pid; 62 63 trace_qio_channel_command_new_pid(ioc, writefd, readfd, pid); 64 return ioc; 65 } 66 67 QIOChannelCommand * 68 qio_channel_command_new_spawn(const char *const argv[], 69 int flags, 70 Error **errp) 71 { 72 g_autoptr(GError) err = NULL; 73 GPid pid = 0; 74 GSpawnFlags gflags = G_SPAWN_CLOEXEC_PIPES | G_SPAWN_DO_NOT_REAP_CHILD; 75 int stdinfd = -1, stdoutfd = -1; 76 77 flags = flags & O_ACCMODE; 78 gflags |= flags == O_WRONLY ? G_SPAWN_STDOUT_TO_DEV_NULL : 0; 79 80 if (!g_spawn_async_with_pipes(NULL, (char **)argv, NULL, gflags, NULL, NULL, 81 &pid, 82 flags == O_RDONLY ? NULL : &stdinfd, 83 flags == O_WRONLY ? NULL : &stdoutfd, 84 NULL, &err)) { 85 error_setg(errp, "%s", err->message); 86 return NULL; 87 } 88 89 return qio_channel_command_new_pid(stdinfd, stdoutfd, pid); 90 } 91 92 #else /* WIN32 */ 93 QIOChannelCommand * 94 qio_channel_command_new_spawn(const char *const argv[], 95 int flags, 96 Error **errp) 97 { 98 error_setg_errno(errp, ENOSYS, 99 "Command spawn not supported on this platform"); 100 return NULL; 101 } 102 #endif /* WIN32 */ 103 104 #ifndef WIN32 105 static int qio_channel_command_abort(QIOChannelCommand *ioc, 106 Error **errp) 107 { 108 pid_t ret; 109 int status; 110 int step = 0; 111 112 /* See if intermediate process has exited; if not, try a nice 113 * SIGTERM followed by a more severe SIGKILL. 114 */ 115 rewait: 116 trace_qio_channel_command_abort(ioc, ioc->pid); 117 ret = waitpid(ioc->pid, &status, WNOHANG); 118 trace_qio_channel_command_wait(ioc, ioc->pid, ret, status); 119 if (ret == (pid_t)-1) { 120 if (errno == EINTR) { 121 goto rewait; 122 } else { 123 error_setg_errno(errp, errno, 124 "Cannot wait on pid %llu", 125 (unsigned long long)ioc->pid); 126 return -1; 127 } 128 } else if (ret == 0) { 129 if (step == 0) { 130 kill(ioc->pid, SIGTERM); 131 } else if (step == 1) { 132 kill(ioc->pid, SIGKILL); 133 } else { 134 error_setg(errp, 135 "Process %llu refused to die", 136 (unsigned long long)ioc->pid); 137 return -1; 138 } 139 step++; 140 usleep(10 * 1000); 141 goto rewait; 142 } 143 144 return 0; 145 } 146 #endif /* ! WIN32 */ 147 148 149 static void qio_channel_command_init(Object *obj) 150 { 151 QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj); 152 ioc->readfd = -1; 153 ioc->writefd = -1; 154 ioc->pid = 0; 155 } 156 157 static void qio_channel_command_finalize(Object *obj) 158 { 159 QIOChannelCommand *ioc = QIO_CHANNEL_COMMAND(obj); 160 if (ioc->readfd != -1) { 161 close(ioc->readfd); 162 } 163 if (ioc->writefd != -1 && 164 ioc->writefd != ioc->readfd) { 165 close(ioc->writefd); 166 } 167 ioc->writefd = ioc->readfd = -1; 168 if (ioc->pid > 0) { 169 #ifndef WIN32 170 qio_channel_command_abort(ioc, NULL); 171 #endif 172 g_spawn_close_pid(ioc->pid); 173 } 174 } 175 176 177 static ssize_t qio_channel_command_readv(QIOChannel *ioc, 178 const struct iovec *iov, 179 size_t niov, 180 int **fds, 181 size_t *nfds, 182 Error **errp) 183 { 184 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); 185 ssize_t ret; 186 187 retry: 188 ret = readv(cioc->readfd, iov, niov); 189 if (ret < 0) { 190 if (errno == EAGAIN) { 191 return QIO_CHANNEL_ERR_BLOCK; 192 } 193 if (errno == EINTR) { 194 goto retry; 195 } 196 197 error_setg_errno(errp, errno, 198 "Unable to read from command"); 199 return -1; 200 } 201 202 return ret; 203 } 204 205 static ssize_t qio_channel_command_writev(QIOChannel *ioc, 206 const struct iovec *iov, 207 size_t niov, 208 int *fds, 209 size_t nfds, 210 int flags, 211 Error **errp) 212 { 213 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); 214 ssize_t ret; 215 216 retry: 217 ret = writev(cioc->writefd, iov, niov); 218 if (ret <= 0) { 219 if (errno == EAGAIN) { 220 return QIO_CHANNEL_ERR_BLOCK; 221 } 222 if (errno == EINTR) { 223 goto retry; 224 } 225 error_setg_errno(errp, errno, "%s", 226 "Unable to write to command"); 227 return -1; 228 } 229 return ret; 230 } 231 232 static int qio_channel_command_set_blocking(QIOChannel *ioc, 233 bool enabled, 234 Error **errp) 235 { 236 #ifdef WIN32 237 /* command spawn is not supported on win32 */ 238 g_assert_not_reached(); 239 #else 240 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); 241 242 if (!g_unix_set_fd_nonblocking(cioc->writefd, !enabled, NULL) || 243 !g_unix_set_fd_nonblocking(cioc->readfd, !enabled, NULL)) { 244 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 245 return -1; 246 } 247 #endif 248 return 0; 249 } 250 251 252 static int qio_channel_command_close(QIOChannel *ioc, 253 Error **errp) 254 { 255 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); 256 int rv = 0; 257 #ifndef WIN32 258 pid_t wp; 259 #endif 260 261 /* We close FDs before killing, because that 262 * gives a better chance of clean shutdown 263 */ 264 if (cioc->readfd != -1 && 265 close(cioc->readfd) < 0) { 266 rv = -1; 267 } 268 if (cioc->writefd != -1 && 269 cioc->writefd != cioc->readfd && 270 close(cioc->writefd) < 0) { 271 rv = -1; 272 } 273 cioc->writefd = cioc->readfd = -1; 274 275 #ifndef WIN32 276 do { 277 wp = waitpid(cioc->pid, NULL, 0); 278 } while (wp == (pid_t)-1 && errno == EINTR); 279 if (wp == (pid_t)-1) { 280 error_setg_errno(errp, errno, "Failed to wait for pid %llu", 281 (unsigned long long)cioc->pid); 282 return -1; 283 } 284 #endif 285 286 if (rv < 0) { 287 error_setg_errno(errp, errno, "%s", 288 "Unable to close command"); 289 } 290 return rv; 291 } 292 293 294 static void qio_channel_command_set_aio_fd_handler(QIOChannel *ioc, 295 AioContext *ctx, 296 IOHandler *io_read, 297 IOHandler *io_write, 298 void *opaque) 299 { 300 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); 301 aio_set_fd_handler(ctx, cioc->readfd, false, 302 io_read, NULL, NULL, NULL, opaque); 303 aio_set_fd_handler(ctx, cioc->writefd, false, 304 NULL, io_write, NULL, NULL, opaque); 305 } 306 307 308 static GSource *qio_channel_command_create_watch(QIOChannel *ioc, 309 GIOCondition condition) 310 { 311 QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc); 312 return qio_channel_create_fd_pair_watch(ioc, 313 cioc->readfd, 314 cioc->writefd, 315 condition); 316 } 317 318 319 static void qio_channel_command_class_init(ObjectClass *klass, 320 void *class_data G_GNUC_UNUSED) 321 { 322 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); 323 324 ioc_klass->io_writev = qio_channel_command_writev; 325 ioc_klass->io_readv = qio_channel_command_readv; 326 ioc_klass->io_set_blocking = qio_channel_command_set_blocking; 327 ioc_klass->io_close = qio_channel_command_close; 328 ioc_klass->io_create_watch = qio_channel_command_create_watch; 329 ioc_klass->io_set_aio_fd_handler = qio_channel_command_set_aio_fd_handler; 330 } 331 332 static const TypeInfo qio_channel_command_info = { 333 .parent = TYPE_QIO_CHANNEL, 334 .name = TYPE_QIO_CHANNEL_COMMAND, 335 .instance_size = sizeof(QIOChannelCommand), 336 .instance_init = qio_channel_command_init, 337 .instance_finalize = qio_channel_command_finalize, 338 .class_init = qio_channel_command_class_init, 339 }; 340 341 static void qio_channel_command_register_types(void) 342 { 343 type_register_static(&qio_channel_command_info); 344 } 345 346 type_init(qio_channel_command_register_types); 347