char-pipe.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/main-loop.h"
  27. #include "qemu/module.h"
  28. #include "qemu/option.h"
  29. #include "chardev/char.h"
  30. #ifdef _WIN32
  31. #include "chardev/char-win.h"
  32. #else
  33. #include "chardev/char-fd.h"
  34. #endif
  35. #ifdef _WIN32
  36. #define MAXCONNECT 1
  37. #define NTIMEOUT 5000
  38. static int win_chr_pipe_init(Chardev *chr, const char *filename,
  39. Error **errp)
  40. {
  41. WinChardev *s = WIN_CHARDEV(chr);
  42. OVERLAPPED ov;
  43. int ret;
  44. DWORD size;
  45. char *openname;
  46. s->fpipe = TRUE;
  47. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  48. if (!s->hsend) {
  49. error_setg(errp, "Failed CreateEvent");
  50. goto fail;
  51. }
  52. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  53. if (!s->hrecv) {
  54. error_setg(errp, "Failed CreateEvent");
  55. goto fail;
  56. }
  57. openname = g_strdup_printf("\\\\.\\pipe\\%s", filename);
  58. s->file = CreateNamedPipe(openname,
  59. PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  60. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
  61. PIPE_WAIT,
  62. MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
  63. g_free(openname);
  64. if (s->file == INVALID_HANDLE_VALUE) {
  65. error_setg_win32(errp, GetLastError(), "Failed CreateNamedPipe");
  66. s->file = NULL;
  67. goto fail;
  68. }
  69. ZeroMemory(&ov, sizeof(ov));
  70. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  71. ret = ConnectNamedPipe(s->file, &ov);
  72. if (ret) {
  73. error_setg(errp, "Failed ConnectNamedPipe");
  74. goto fail;
  75. }
  76. ret = GetOverlappedResult(s->file, &ov, &size, TRUE);
  77. if (!ret) {
  78. error_setg(errp, "Failed GetOverlappedResult");
  79. if (ov.hEvent) {
  80. CloseHandle(ov.hEvent);
  81. ov.hEvent = NULL;
  82. }
  83. goto fail;
  84. }
  85. if (ov.hEvent) {
  86. CloseHandle(ov.hEvent);
  87. ov.hEvent = NULL;
  88. }
  89. qemu_add_polling_cb(win_chr_pipe_poll, chr);
  90. return 0;
  91. fail:
  92. return -1;
  93. }
  94. static void qemu_chr_open_pipe(Chardev *chr,
  95. ChardevBackend *backend,
  96. bool *be_opened,
  97. Error **errp)
  98. {
  99. ChardevHostdev *opts = backend->u.pipe.data;
  100. const char *filename = opts->device;
  101. if (win_chr_pipe_init(chr, filename, errp) < 0) {
  102. return;
  103. }
  104. }
  105. #else
  106. static void qemu_chr_open_pipe(Chardev *chr,
  107. ChardevBackend *backend,
  108. bool *be_opened,
  109. Error **errp)
  110. {
  111. ChardevHostdev *opts = backend->u.pipe.data;
  112. int fd_in, fd_out;
  113. char *filename_in;
  114. char *filename_out;
  115. const char *filename = opts->device;
  116. filename_in = g_strdup_printf("%s.in", filename);
  117. filename_out = g_strdup_printf("%s.out", filename);
  118. TFR(fd_in = qemu_open_old(filename_in, O_RDWR | O_BINARY));
  119. TFR(fd_out = qemu_open_old(filename_out, O_RDWR | O_BINARY));
  120. g_free(filename_in);
  121. g_free(filename_out);
  122. if (fd_in < 0 || fd_out < 0) {
  123. if (fd_in >= 0) {
  124. close(fd_in);
  125. }
  126. if (fd_out >= 0) {
  127. close(fd_out);
  128. }
  129. TFR(fd_in = fd_out = qemu_open_old(filename, O_RDWR | O_BINARY));
  130. if (fd_in < 0) {
  131. error_setg_file_open(errp, errno, filename);
  132. return;
  133. }
  134. }
  135. qemu_chr_open_fd(chr, fd_in, fd_out);
  136. }
  137. #endif /* !_WIN32 */
  138. static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
  139. Error **errp)
  140. {
  141. const char *device = qemu_opt_get(opts, "path");
  142. ChardevHostdev *dev;
  143. if (device == NULL) {
  144. error_setg(errp, "chardev: pipe: no device path given");
  145. return;
  146. }
  147. backend->type = CHARDEV_BACKEND_KIND_PIPE;
  148. dev = backend->u.pipe.data = g_new0(ChardevHostdev, 1);
  149. qemu_chr_parse_common(opts, qapi_ChardevHostdev_base(dev));
  150. dev->device = g_strdup(device);
  151. }
  152. static void char_pipe_class_init(ObjectClass *oc, void *data)
  153. {
  154. ChardevClass *cc = CHARDEV_CLASS(oc);
  155. cc->parse = qemu_chr_parse_pipe;
  156. cc->open = qemu_chr_open_pipe;
  157. }
  158. static const TypeInfo char_pipe_type_info = {
  159. .name = TYPE_CHARDEV_PIPE,
  160. #ifdef _WIN32
  161. .parent = TYPE_CHARDEV_WIN,
  162. #else
  163. .parent = TYPE_CHARDEV_FD,
  164. #endif
  165. .class_init = char_pipe_class_init,
  166. };
  167. static void register_types(void)
  168. {
  169. type_register_static(&char_pipe_type_info);
  170. }
  171. type_init(register_types);