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