channel-posix.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "qemu/osdep.h"
  2. #include "qemu/cutils.h"
  3. #include <termios.h>
  4. #include "qapi/error.h"
  5. #include "qemu/sockets.h"
  6. #include "channel.h"
  7. #include "cutils.h"
  8. #ifdef CONFIG_SOLARIS
  9. #include <stropts.h>
  10. #endif
  11. #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
  12. struct GAChannel {
  13. GIOChannel *listen_channel;
  14. GIOChannel *client_channel;
  15. GAChannelMethod method;
  16. GAChannelCallback event_cb;
  17. gpointer user_data;
  18. };
  19. static int ga_channel_client_add(GAChannel *c, int fd);
  20. static gboolean ga_channel_listen_accept(GIOChannel *channel,
  21. GIOCondition condition, gpointer data)
  22. {
  23. GAChannel *c = data;
  24. int ret, client_fd;
  25. bool accepted = false;
  26. g_assert(channel != NULL);
  27. client_fd = qemu_accept(g_io_channel_unix_get_fd(channel), NULL, NULL);
  28. if (client_fd == -1) {
  29. g_warning("error converting fd to gsocket: %s", strerror(errno));
  30. goto out;
  31. }
  32. qemu_socket_set_nonblock(client_fd);
  33. ret = ga_channel_client_add(c, client_fd);
  34. if (ret) {
  35. g_warning("error setting up connection");
  36. close(client_fd);
  37. goto out;
  38. }
  39. accepted = true;
  40. out:
  41. /* only accept 1 connection at a time */
  42. return !accepted;
  43. }
  44. /* start polling for readable events on listen fd, new==true
  45. * indicates we should use the existing s->listen_channel
  46. */
  47. static void ga_channel_listen_add(GAChannel *c, int listen_fd, bool create)
  48. {
  49. if (create) {
  50. c->listen_channel = g_io_channel_unix_new(listen_fd);
  51. }
  52. g_io_add_watch(c->listen_channel, G_IO_IN, ga_channel_listen_accept, c);
  53. }
  54. static void ga_channel_listen_close(GAChannel *c)
  55. {
  56. g_assert(c->listen_channel);
  57. g_io_channel_shutdown(c->listen_channel, true, NULL);
  58. g_io_channel_unref(c->listen_channel);
  59. c->listen_channel = NULL;
  60. }
  61. /* cleanup state for closed connection/session, start accepting new
  62. * connections if we're in listening mode
  63. */
  64. static void ga_channel_client_close(GAChannel *c)
  65. {
  66. g_assert(c->client_channel);
  67. g_io_channel_shutdown(c->client_channel, true, NULL);
  68. g_io_channel_unref(c->client_channel);
  69. c->client_channel = NULL;
  70. if (c->listen_channel) {
  71. ga_channel_listen_add(c, 0, false);
  72. }
  73. }
  74. static gboolean ga_channel_client_event(GIOChannel *channel,
  75. GIOCondition condition, gpointer data)
  76. {
  77. GAChannel *c = data;
  78. gboolean client_cont;
  79. g_assert(c);
  80. if (c->event_cb) {
  81. client_cont = c->event_cb(condition, c->user_data);
  82. if (!client_cont) {
  83. ga_channel_client_close(c);
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. static int ga_channel_client_add(GAChannel *c, int fd)
  90. {
  91. GIOChannel *client_channel;
  92. GError *err = NULL;
  93. g_assert(c && !c->client_channel);
  94. client_channel = g_io_channel_unix_new(fd);
  95. g_assert(client_channel);
  96. g_io_channel_set_encoding(client_channel, NULL, &err);
  97. if (err != NULL) {
  98. g_warning("error setting channel encoding to binary");
  99. g_error_free(err);
  100. return -1;
  101. }
  102. g_io_add_watch(client_channel, G_IO_IN | G_IO_HUP,
  103. ga_channel_client_event, c);
  104. c->client_channel = client_channel;
  105. return 0;
  106. }
  107. static gboolean ga_channel_open(GAChannel *c, const gchar *path,
  108. GAChannelMethod method, int fd, Error **errp)
  109. {
  110. int ret;
  111. c->method = method;
  112. switch (c->method) {
  113. case GA_CHANNEL_VIRTIO_SERIAL: {
  114. assert(fd < 0);
  115. fd = qga_open_cloexec(
  116. path,
  117. #ifndef CONFIG_SOLARIS
  118. O_ASYNC |
  119. #endif
  120. O_RDWR | O_NONBLOCK,
  121. 0
  122. );
  123. if (fd == -1) {
  124. error_setg_errno(errp, errno, "error opening channel '%s'", path);
  125. return false;
  126. }
  127. #ifdef CONFIG_SOLARIS
  128. ret = ioctl(fd, I_SETSIG, S_OUTPUT | S_INPUT | S_HIPRI);
  129. if (ret == -1) {
  130. error_setg_errno(errp, errno, "error setting event mask for channel");
  131. close(fd);
  132. return false;
  133. }
  134. #endif
  135. #ifdef __FreeBSD__
  136. /*
  137. * In the default state channel sends echo of every command to a
  138. * client. The client program doesn't expect this and raises an
  139. * error. Suppress echo by resetting ECHO terminal flag.
  140. */
  141. struct termios tio;
  142. if (tcgetattr(fd, &tio) < 0) {
  143. error_setg_errno(errp, errno, "error getting channel termios attrs");
  144. close(fd);
  145. return false;
  146. }
  147. tio.c_lflag &= ~ECHO;
  148. if (tcsetattr(fd, TCSAFLUSH, &tio) < 0) {
  149. error_setg_errno(errp, errno, "error setting channel termios attrs");
  150. close(fd);
  151. return false;
  152. }
  153. #endif /* __FreeBSD__ */
  154. ret = ga_channel_client_add(c, fd);
  155. if (ret) {
  156. error_setg(errp, "error adding channel to main loop");
  157. close(fd);
  158. return false;
  159. }
  160. break;
  161. }
  162. case GA_CHANNEL_ISA_SERIAL: {
  163. struct termios tio;
  164. assert(fd < 0);
  165. fd = qga_open_cloexec(path, O_RDWR | O_NOCTTY | O_NONBLOCK, 0);
  166. if (fd == -1) {
  167. error_setg_errno(errp, errno, "error opening channel '%s'", path);
  168. return false;
  169. }
  170. tcgetattr(fd, &tio);
  171. /* set up serial port for non-canonical, dumb byte streaming */
  172. tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
  173. INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
  174. IMAXBEL);
  175. tio.c_oflag = 0;
  176. tio.c_lflag = 0;
  177. tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
  178. /* 1 available byte min or reads will block (we'll set non-blocking
  179. * elsewhere, else we have to deal with read()=0 instead)
  180. */
  181. tio.c_cc[VMIN] = 1;
  182. tio.c_cc[VTIME] = 0;
  183. /* flush everything waiting for read/xmit, it's garbage at this point */
  184. tcflush(fd, TCIFLUSH);
  185. tcsetattr(fd, TCSANOW, &tio);
  186. ret = ga_channel_client_add(c, fd);
  187. if (ret) {
  188. error_setg(errp, "error adding channel to main loop");
  189. close(fd);
  190. return false;
  191. }
  192. break;
  193. }
  194. case GA_CHANNEL_UNIX_LISTEN: {
  195. if (fd < 0) {
  196. fd = unix_listen(path, errp);
  197. if (fd < 0) {
  198. return false;
  199. }
  200. }
  201. ga_channel_listen_add(c, fd, true);
  202. break;
  203. }
  204. case GA_CHANNEL_VSOCK_LISTEN: {
  205. if (fd < 0) {
  206. SocketAddress *addr;
  207. char *addr_str;
  208. addr_str = g_strdup_printf("vsock:%s", path);
  209. addr = socket_parse(addr_str, errp);
  210. g_free(addr_str);
  211. if (!addr) {
  212. return false;
  213. }
  214. fd = socket_listen(addr, 1, errp);
  215. qapi_free_SocketAddress(addr);
  216. if (fd < 0) {
  217. return false;
  218. }
  219. }
  220. ga_channel_listen_add(c, fd, true);
  221. break;
  222. }
  223. default:
  224. error_setg(errp, "error binding/listening to specified socket");
  225. return false;
  226. }
  227. return true;
  228. }
  229. GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size)
  230. {
  231. GError *err = NULL;
  232. gsize written = 0;
  233. GIOStatus status = G_IO_STATUS_NORMAL;
  234. while (size) {
  235. g_debug("sending data, count: %d", (int)size);
  236. status = g_io_channel_write_chars(c->client_channel, buf, size,
  237. &written, &err);
  238. if (status == G_IO_STATUS_NORMAL) {
  239. size -= written;
  240. buf += written;
  241. } else if (status != G_IO_STATUS_AGAIN) {
  242. g_warning("error writing to channel: %s", err->message);
  243. return status;
  244. }
  245. }
  246. do {
  247. status = g_io_channel_flush(c->client_channel, &err);
  248. } while (status == G_IO_STATUS_AGAIN);
  249. if (status != G_IO_STATUS_NORMAL) {
  250. g_warning("error flushing channel: %s", err->message);
  251. }
  252. return status;
  253. }
  254. GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
  255. {
  256. return g_io_channel_read_chars(c->client_channel, buf, size, count, NULL);
  257. }
  258. GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
  259. int listen_fd, GAChannelCallback cb, gpointer opaque)
  260. {
  261. Error *err = NULL;
  262. GAChannel *c = g_new0(GAChannel, 1);
  263. c->event_cb = cb;
  264. c->user_data = opaque;
  265. if (!ga_channel_open(c, path, method, listen_fd, &err)) {
  266. g_critical("%s", error_get_pretty(err));
  267. error_free(err);
  268. ga_channel_free(c);
  269. return NULL;
  270. }
  271. return c;
  272. }
  273. void ga_channel_free(GAChannel *c)
  274. {
  275. if (c->listen_channel) {
  276. ga_channel_listen_close(c);
  277. }
  278. if (c->client_channel) {
  279. ga_channel_client_close(c);
  280. }
  281. g_free(c);
  282. }