channel-command.c 11 KB

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