char-fe.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "qemu/error-report.h"
  26. #include "qapi/error.h"
  27. #include "qapi/qmp/qerror.h"
  28. #include "sysemu/replay.h"
  29. #include "chardev/char-fe.h"
  30. #include "chardev/char-io.h"
  31. #include "chardev-internal.h"
  32. int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
  33. {
  34. Chardev *s = be->chr;
  35. if (!s) {
  36. return 0;
  37. }
  38. return qemu_chr_write(s, buf, len, false);
  39. }
  40. int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
  41. {
  42. Chardev *s = be->chr;
  43. if (!s) {
  44. return 0;
  45. }
  46. return qemu_chr_write(s, buf, len, true);
  47. }
  48. int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
  49. {
  50. Chardev *s = be->chr;
  51. int offset = 0;
  52. int res;
  53. if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) {
  54. return 0;
  55. }
  56. if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
  57. return replay_char_read_all_load(buf);
  58. }
  59. while (offset < len) {
  60. retry:
  61. res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset,
  62. len - offset);
  63. if (res == -1 && errno == EAGAIN) {
  64. g_usleep(100);
  65. goto retry;
  66. }
  67. if (res == 0) {
  68. break;
  69. }
  70. if (res < 0) {
  71. if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
  72. replay_char_read_all_save_error(res);
  73. }
  74. return res;
  75. }
  76. offset += res;
  77. }
  78. if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
  79. replay_char_read_all_save_buf(buf, offset);
  80. }
  81. return offset;
  82. }
  83. int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
  84. {
  85. Chardev *s = be->chr;
  86. int res;
  87. if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) {
  88. res = -ENOTSUP;
  89. } else {
  90. res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg);
  91. }
  92. return res;
  93. }
  94. int qemu_chr_fe_get_msgfd(CharBackend *be)
  95. {
  96. Chardev *s = be->chr;
  97. int fd;
  98. int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
  99. if (s && qemu_chr_replay(s)) {
  100. error_report("Replay: get msgfd is not supported "
  101. "for serial devices yet");
  102. exit(1);
  103. }
  104. return res;
  105. }
  106. int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
  107. {
  108. Chardev *s = be->chr;
  109. if (!s) {
  110. return -1;
  111. }
  112. return CHARDEV_GET_CLASS(s)->get_msgfds ?
  113. CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
  114. }
  115. int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
  116. {
  117. Chardev *s = be->chr;
  118. if (!s) {
  119. return -1;
  120. }
  121. return CHARDEV_GET_CLASS(s)->set_msgfds ?
  122. CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
  123. }
  124. void qemu_chr_fe_accept_input(CharBackend *be)
  125. {
  126. Chardev *s = be->chr;
  127. if (!s) {
  128. return;
  129. }
  130. if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
  131. CHARDEV_GET_CLASS(s)->chr_accept_input(s);
  132. }
  133. qemu_notify_event();
  134. }
  135. void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
  136. {
  137. char buf[CHR_READ_BUF_LEN];
  138. va_list ap;
  139. va_start(ap, fmt);
  140. vsnprintf(buf, sizeof(buf), fmt, ap);
  141. /* XXX this blocks entire thread. Rewrite to use
  142. * qemu_chr_fe_write and background I/O callbacks */
  143. qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
  144. va_end(ap);
  145. }
  146. Chardev *qemu_chr_fe_get_driver(CharBackend *be)
  147. {
  148. /* this is unsafe for the users that support chardev hotswap */
  149. assert(be->chr_be_change == NULL);
  150. return be->chr;
  151. }
  152. bool qemu_chr_fe_backend_connected(CharBackend *be)
  153. {
  154. return !!be->chr;
  155. }
  156. bool qemu_chr_fe_backend_open(CharBackend *be)
  157. {
  158. return be->chr && be->chr->be_open;
  159. }
  160. bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
  161. {
  162. int tag = 0;
  163. if (s) {
  164. if (CHARDEV_IS_MUX(s)) {
  165. MuxChardev *d = MUX_CHARDEV(s);
  166. if (d->mux_cnt >= MAX_MUX) {
  167. goto unavailable;
  168. }
  169. d->backends[d->mux_cnt] = b;
  170. tag = d->mux_cnt++;
  171. } else if (s->be) {
  172. goto unavailable;
  173. } else {
  174. s->be = b;
  175. }
  176. }
  177. b->fe_open = false;
  178. b->tag = tag;
  179. b->chr = s;
  180. return true;
  181. unavailable:
  182. error_setg(errp, QERR_DEVICE_IN_USE, s->label);
  183. return false;
  184. }
  185. void qemu_chr_fe_deinit(CharBackend *b, bool del)
  186. {
  187. assert(b);
  188. if (b->chr) {
  189. qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, NULL, true);
  190. if (b->chr->be == b) {
  191. b->chr->be = NULL;
  192. }
  193. if (CHARDEV_IS_MUX(b->chr)) {
  194. MuxChardev *d = MUX_CHARDEV(b->chr);
  195. d->backends[b->tag] = NULL;
  196. }
  197. if (del) {
  198. Object *obj = OBJECT(b->chr);
  199. if (obj->parent) {
  200. object_unparent(obj);
  201. } else {
  202. object_unref(obj);
  203. }
  204. }
  205. b->chr = NULL;
  206. }
  207. }
  208. void qemu_chr_fe_set_handlers_full(CharBackend *b,
  209. IOCanReadHandler *fd_can_read,
  210. IOReadHandler *fd_read,
  211. IOEventHandler *fd_event,
  212. BackendChangeHandler *be_change,
  213. void *opaque,
  214. GMainContext *context,
  215. bool set_open,
  216. bool sync_state)
  217. {
  218. Chardev *s;
  219. int fe_open;
  220. s = b->chr;
  221. if (!s) {
  222. return;
  223. }
  224. if (!opaque && !fd_can_read && !fd_read && !fd_event) {
  225. fe_open = 0;
  226. remove_fd_in_watch(s);
  227. } else {
  228. fe_open = 1;
  229. }
  230. b->chr_can_read = fd_can_read;
  231. b->chr_read = fd_read;
  232. b->chr_event = fd_event;
  233. b->chr_be_change = be_change;
  234. b->opaque = opaque;
  235. qemu_chr_be_update_read_handlers(s, context);
  236. if (set_open) {
  237. qemu_chr_fe_set_open(b, fe_open);
  238. }
  239. if (fe_open) {
  240. qemu_chr_fe_take_focus(b);
  241. /* We're connecting to an already opened device, so let's make sure we
  242. also get the open event */
  243. if (sync_state && s->be_open) {
  244. qemu_chr_be_event(s, CHR_EVENT_OPENED);
  245. }
  246. }
  247. }
  248. void qemu_chr_fe_set_handlers(CharBackend *b,
  249. IOCanReadHandler *fd_can_read,
  250. IOReadHandler *fd_read,
  251. IOEventHandler *fd_event,
  252. BackendChangeHandler *be_change,
  253. void *opaque,
  254. GMainContext *context,
  255. bool set_open)
  256. {
  257. qemu_chr_fe_set_handlers_full(b, fd_can_read, fd_read, fd_event, be_change,
  258. opaque, context, set_open,
  259. true);
  260. }
  261. void qemu_chr_fe_take_focus(CharBackend *b)
  262. {
  263. if (!b->chr) {
  264. return;
  265. }
  266. if (CHARDEV_IS_MUX(b->chr)) {
  267. mux_set_focus(b->chr, b->tag);
  268. }
  269. }
  270. int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
  271. {
  272. if (!be->chr) {
  273. error_setg(errp, "missing associated backend");
  274. return -1;
  275. }
  276. return qemu_chr_wait_connected(be->chr, errp);
  277. }
  278. void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
  279. {
  280. Chardev *chr = be->chr;
  281. if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
  282. CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
  283. }
  284. }
  285. void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
  286. {
  287. Chardev *chr = be->chr;
  288. if (!chr) {
  289. return;
  290. }
  291. if (be->fe_open == fe_open) {
  292. return;
  293. }
  294. be->fe_open = fe_open;
  295. if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) {
  296. CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open);
  297. }
  298. }
  299. guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
  300. FEWatchFunc func, void *user_data)
  301. {
  302. Chardev *s = be->chr;
  303. GSource *src;
  304. guint tag;
  305. if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
  306. return 0;
  307. }
  308. src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
  309. if (!src) {
  310. return 0;
  311. }
  312. g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
  313. tag = g_source_attach(src, s->gcontext);
  314. g_source_unref(src);
  315. return tag;
  316. }
  317. void qemu_chr_fe_disconnect(CharBackend *be)
  318. {
  319. Chardev *chr = be->chr;
  320. if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
  321. CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);
  322. }
  323. }