2
0

channel-command.c 11 KB

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