qemu-char.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  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-common.h"
  25. #include "monitor/monitor.h"
  26. #include "ui/console.h"
  27. #include "sysemu/sysemu.h"
  28. #include "qemu/timer.h"
  29. #include "char/char.h"
  30. #include "hw/usb.h"
  31. #include "hw/baum.h"
  32. #include "hw/msmouse.h"
  33. #include "qmp-commands.h"
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <time.h>
  37. #include <errno.h>
  38. #include <sys/time.h>
  39. #include <zlib.h>
  40. #ifndef _WIN32
  41. #include <sys/times.h>
  42. #include <sys/wait.h>
  43. #include <termios.h>
  44. #include <sys/mman.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/resource.h>
  47. #include <sys/socket.h>
  48. #include <netinet/in.h>
  49. #include <net/if.h>
  50. #include <arpa/inet.h>
  51. #include <dirent.h>
  52. #include <netdb.h>
  53. #include <sys/select.h>
  54. #ifdef CONFIG_BSD
  55. #include <sys/stat.h>
  56. #if defined(__GLIBC__)
  57. #include <pty.h>
  58. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  59. #include <libutil.h>
  60. #else
  61. #include <util.h>
  62. #endif
  63. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  64. #include <dev/ppbus/ppi.h>
  65. #include <dev/ppbus/ppbconf.h>
  66. #elif defined(__DragonFly__)
  67. #include <dev/misc/ppi/ppi.h>
  68. #include <bus/ppbus/ppbconf.h>
  69. #endif
  70. #else
  71. #ifdef __linux__
  72. #include <pty.h>
  73. #include <linux/ppdev.h>
  74. #include <linux/parport.h>
  75. #endif
  76. #ifdef __sun__
  77. #include <sys/stat.h>
  78. #include <sys/ethernet.h>
  79. #include <sys/sockio.h>
  80. #include <netinet/arp.h>
  81. #include <netinet/in.h>
  82. #include <netinet/in_systm.h>
  83. #include <netinet/ip.h>
  84. #include <netinet/ip_icmp.h> // must come after ip.h
  85. #include <netinet/udp.h>
  86. #include <netinet/tcp.h>
  87. #include <net/if.h>
  88. #include <syslog.h>
  89. #include <stropts.h>
  90. #endif
  91. #endif
  92. #endif
  93. #include "qemu/sockets.h"
  94. #include "ui/qemu-spice.h"
  95. #define READ_BUF_LEN 4096
  96. /***********************************************************/
  97. /* character device */
  98. static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
  99. QTAILQ_HEAD_INITIALIZER(chardevs);
  100. void qemu_chr_be_event(CharDriverState *s, int event)
  101. {
  102. /* Keep track if the char device is open */
  103. switch (event) {
  104. case CHR_EVENT_OPENED:
  105. s->opened = 1;
  106. break;
  107. case CHR_EVENT_CLOSED:
  108. s->opened = 0;
  109. break;
  110. }
  111. if (!s->chr_event)
  112. return;
  113. s->chr_event(s->handler_opaque, event);
  114. }
  115. static void qemu_chr_fire_open_event(void *opaque)
  116. {
  117. CharDriverState *s = opaque;
  118. qemu_chr_be_event(s, CHR_EVENT_OPENED);
  119. qemu_free_timer(s->open_timer);
  120. s->open_timer = NULL;
  121. }
  122. void qemu_chr_generic_open(CharDriverState *s)
  123. {
  124. if (s->open_timer == NULL) {
  125. s->open_timer = qemu_new_timer_ms(rt_clock,
  126. qemu_chr_fire_open_event, s);
  127. qemu_mod_timer(s->open_timer, qemu_get_clock_ms(rt_clock) - 1);
  128. }
  129. }
  130. int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
  131. {
  132. return s->chr_write(s, buf, len);
  133. }
  134. int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
  135. {
  136. if (!s->chr_ioctl)
  137. return -ENOTSUP;
  138. return s->chr_ioctl(s, cmd, arg);
  139. }
  140. int qemu_chr_be_can_write(CharDriverState *s)
  141. {
  142. if (!s->chr_can_read)
  143. return 0;
  144. return s->chr_can_read(s->handler_opaque);
  145. }
  146. void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
  147. {
  148. if (s->chr_read) {
  149. s->chr_read(s->handler_opaque, buf, len);
  150. }
  151. }
  152. int qemu_chr_fe_get_msgfd(CharDriverState *s)
  153. {
  154. return s->get_msgfd ? s->get_msgfd(s) : -1;
  155. }
  156. int qemu_chr_add_client(CharDriverState *s, int fd)
  157. {
  158. return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
  159. }
  160. void qemu_chr_accept_input(CharDriverState *s)
  161. {
  162. if (s->chr_accept_input)
  163. s->chr_accept_input(s);
  164. qemu_notify_event();
  165. }
  166. void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
  167. {
  168. char buf[READ_BUF_LEN];
  169. va_list ap;
  170. va_start(ap, fmt);
  171. vsnprintf(buf, sizeof(buf), fmt, ap);
  172. qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
  173. va_end(ap);
  174. }
  175. void qemu_chr_add_handlers(CharDriverState *s,
  176. IOCanReadHandler *fd_can_read,
  177. IOReadHandler *fd_read,
  178. IOEventHandler *fd_event,
  179. void *opaque)
  180. {
  181. if (!opaque && !fd_can_read && !fd_read && !fd_event) {
  182. /* chr driver being released. */
  183. ++s->avail_connections;
  184. }
  185. s->chr_can_read = fd_can_read;
  186. s->chr_read = fd_read;
  187. s->chr_event = fd_event;
  188. s->handler_opaque = opaque;
  189. if (s->chr_update_read_handler)
  190. s->chr_update_read_handler(s);
  191. /* We're connecting to an already opened device, so let's make sure we
  192. also get the open event */
  193. if (s->opened) {
  194. qemu_chr_generic_open(s);
  195. }
  196. }
  197. static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  198. {
  199. return len;
  200. }
  201. static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
  202. {
  203. CharDriverState *chr;
  204. chr = g_malloc0(sizeof(CharDriverState));
  205. chr->chr_write = null_chr_write;
  206. return chr;
  207. }
  208. /* MUX driver for serial I/O splitting */
  209. #define MAX_MUX 4
  210. #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
  211. #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
  212. typedef struct {
  213. IOCanReadHandler *chr_can_read[MAX_MUX];
  214. IOReadHandler *chr_read[MAX_MUX];
  215. IOEventHandler *chr_event[MAX_MUX];
  216. void *ext_opaque[MAX_MUX];
  217. CharDriverState *drv;
  218. int focus;
  219. int mux_cnt;
  220. int term_got_escape;
  221. int max_size;
  222. /* Intermediate input buffer allows to catch escape sequences even if the
  223. currently active device is not accepting any input - but only until it
  224. is full as well. */
  225. unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
  226. int prod[MAX_MUX];
  227. int cons[MAX_MUX];
  228. int timestamps;
  229. int linestart;
  230. int64_t timestamps_start;
  231. } MuxDriver;
  232. static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  233. {
  234. MuxDriver *d = chr->opaque;
  235. int ret;
  236. if (!d->timestamps) {
  237. ret = d->drv->chr_write(d->drv, buf, len);
  238. } else {
  239. int i;
  240. ret = 0;
  241. for (i = 0; i < len; i++) {
  242. if (d->linestart) {
  243. char buf1[64];
  244. int64_t ti;
  245. int secs;
  246. ti = qemu_get_clock_ms(rt_clock);
  247. if (d->timestamps_start == -1)
  248. d->timestamps_start = ti;
  249. ti -= d->timestamps_start;
  250. secs = ti / 1000;
  251. snprintf(buf1, sizeof(buf1),
  252. "[%02d:%02d:%02d.%03d] ",
  253. secs / 3600,
  254. (secs / 60) % 60,
  255. secs % 60,
  256. (int)(ti % 1000));
  257. d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
  258. d->linestart = 0;
  259. }
  260. ret += d->drv->chr_write(d->drv, buf+i, 1);
  261. if (buf[i] == '\n') {
  262. d->linestart = 1;
  263. }
  264. }
  265. }
  266. return ret;
  267. }
  268. static const char * const mux_help[] = {
  269. "% h print this help\n\r",
  270. "% x exit emulator\n\r",
  271. "% s save disk data back to file (if -snapshot)\n\r",
  272. "% t toggle console timestamps\n\r"
  273. "% b send break (magic sysrq)\n\r",
  274. "% c switch between console and monitor\n\r",
  275. "% % sends %\n\r",
  276. NULL
  277. };
  278. int term_escape_char = 0x01; /* ctrl-a is used for escape */
  279. static void mux_print_help(CharDriverState *chr)
  280. {
  281. int i, j;
  282. char ebuf[15] = "Escape-Char";
  283. char cbuf[50] = "\n\r";
  284. if (term_escape_char > 0 && term_escape_char < 26) {
  285. snprintf(cbuf, sizeof(cbuf), "\n\r");
  286. snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
  287. } else {
  288. snprintf(cbuf, sizeof(cbuf),
  289. "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
  290. term_escape_char);
  291. }
  292. chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
  293. for (i = 0; mux_help[i] != NULL; i++) {
  294. for (j=0; mux_help[i][j] != '\0'; j++) {
  295. if (mux_help[i][j] == '%')
  296. chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
  297. else
  298. chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
  299. }
  300. }
  301. }
  302. static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
  303. {
  304. if (d->chr_event[mux_nr])
  305. d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
  306. }
  307. static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
  308. {
  309. if (d->term_got_escape) {
  310. d->term_got_escape = 0;
  311. if (ch == term_escape_char)
  312. goto send_char;
  313. switch(ch) {
  314. case '?':
  315. case 'h':
  316. mux_print_help(chr);
  317. break;
  318. case 'x':
  319. {
  320. const char *term = "QEMU: Terminated\n\r";
  321. chr->chr_write(chr,(uint8_t *)term,strlen(term));
  322. exit(0);
  323. break;
  324. }
  325. case 's':
  326. bdrv_commit_all();
  327. break;
  328. case 'b':
  329. qemu_chr_be_event(chr, CHR_EVENT_BREAK);
  330. break;
  331. case 'c':
  332. /* Switch to the next registered device */
  333. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
  334. d->focus++;
  335. if (d->focus >= d->mux_cnt)
  336. d->focus = 0;
  337. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
  338. break;
  339. case 't':
  340. d->timestamps = !d->timestamps;
  341. d->timestamps_start = -1;
  342. d->linestart = 0;
  343. break;
  344. }
  345. } else if (ch == term_escape_char) {
  346. d->term_got_escape = 1;
  347. } else {
  348. send_char:
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. static void mux_chr_accept_input(CharDriverState *chr)
  354. {
  355. MuxDriver *d = chr->opaque;
  356. int m = d->focus;
  357. while (d->prod[m] != d->cons[m] &&
  358. d->chr_can_read[m] &&
  359. d->chr_can_read[m](d->ext_opaque[m])) {
  360. d->chr_read[m](d->ext_opaque[m],
  361. &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
  362. }
  363. }
  364. static int mux_chr_can_read(void *opaque)
  365. {
  366. CharDriverState *chr = opaque;
  367. MuxDriver *d = chr->opaque;
  368. int m = d->focus;
  369. if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
  370. return 1;
  371. if (d->chr_can_read[m])
  372. return d->chr_can_read[m](d->ext_opaque[m]);
  373. return 0;
  374. }
  375. static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
  376. {
  377. CharDriverState *chr = opaque;
  378. MuxDriver *d = chr->opaque;
  379. int m = d->focus;
  380. int i;
  381. mux_chr_accept_input (opaque);
  382. for(i = 0; i < size; i++)
  383. if (mux_proc_byte(chr, d, buf[i])) {
  384. if (d->prod[m] == d->cons[m] &&
  385. d->chr_can_read[m] &&
  386. d->chr_can_read[m](d->ext_opaque[m]))
  387. d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
  388. else
  389. d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
  390. }
  391. }
  392. static void mux_chr_event(void *opaque, int event)
  393. {
  394. CharDriverState *chr = opaque;
  395. MuxDriver *d = chr->opaque;
  396. int i;
  397. /* Send the event to all registered listeners */
  398. for (i = 0; i < d->mux_cnt; i++)
  399. mux_chr_send_event(d, i, event);
  400. }
  401. static void mux_chr_update_read_handler(CharDriverState *chr)
  402. {
  403. MuxDriver *d = chr->opaque;
  404. if (d->mux_cnt >= MAX_MUX) {
  405. fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
  406. return;
  407. }
  408. d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
  409. d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
  410. d->chr_read[d->mux_cnt] = chr->chr_read;
  411. d->chr_event[d->mux_cnt] = chr->chr_event;
  412. /* Fix up the real driver with mux routines */
  413. if (d->mux_cnt == 0) {
  414. qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
  415. mux_chr_event, chr);
  416. }
  417. if (d->focus != -1) {
  418. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
  419. }
  420. d->focus = d->mux_cnt;
  421. d->mux_cnt++;
  422. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
  423. }
  424. static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
  425. {
  426. CharDriverState *chr;
  427. MuxDriver *d;
  428. chr = g_malloc0(sizeof(CharDriverState));
  429. d = g_malloc0(sizeof(MuxDriver));
  430. chr->opaque = d;
  431. d->drv = drv;
  432. d->focus = -1;
  433. chr->chr_write = mux_chr_write;
  434. chr->chr_update_read_handler = mux_chr_update_read_handler;
  435. chr->chr_accept_input = mux_chr_accept_input;
  436. /* Frontend guest-open / -close notification is not support with muxes */
  437. chr->chr_guest_open = NULL;
  438. chr->chr_guest_close = NULL;
  439. /* Muxes are always open on creation */
  440. qemu_chr_generic_open(chr);
  441. return chr;
  442. }
  443. #ifdef _WIN32
  444. int send_all(int fd, const void *buf, int len1)
  445. {
  446. int ret, len;
  447. len = len1;
  448. while (len > 0) {
  449. ret = send(fd, buf, len, 0);
  450. if (ret < 0) {
  451. errno = WSAGetLastError();
  452. if (errno != WSAEWOULDBLOCK) {
  453. return -1;
  454. }
  455. } else if (ret == 0) {
  456. break;
  457. } else {
  458. buf += ret;
  459. len -= ret;
  460. }
  461. }
  462. return len1 - len;
  463. }
  464. #else
  465. int send_all(int fd, const void *_buf, int len1)
  466. {
  467. int ret, len;
  468. const uint8_t *buf = _buf;
  469. len = len1;
  470. while (len > 0) {
  471. ret = write(fd, buf, len);
  472. if (ret < 0) {
  473. if (errno != EINTR && errno != EAGAIN)
  474. return -1;
  475. } else if (ret == 0) {
  476. break;
  477. } else {
  478. buf += ret;
  479. len -= ret;
  480. }
  481. }
  482. return len1 - len;
  483. }
  484. #endif /* !_WIN32 */
  485. #define STDIO_MAX_CLIENTS 1
  486. static int stdio_nb_clients;
  487. #ifndef _WIN32
  488. typedef struct {
  489. int fd_in, fd_out;
  490. int max_size;
  491. } FDCharDriver;
  492. static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  493. {
  494. FDCharDriver *s = chr->opaque;
  495. return send_all(s->fd_out, buf, len);
  496. }
  497. static int fd_chr_read_poll(void *opaque)
  498. {
  499. CharDriverState *chr = opaque;
  500. FDCharDriver *s = chr->opaque;
  501. s->max_size = qemu_chr_be_can_write(chr);
  502. return s->max_size;
  503. }
  504. static void fd_chr_read(void *opaque)
  505. {
  506. CharDriverState *chr = opaque;
  507. FDCharDriver *s = chr->opaque;
  508. int size, len;
  509. uint8_t buf[READ_BUF_LEN];
  510. len = sizeof(buf);
  511. if (len > s->max_size)
  512. len = s->max_size;
  513. if (len == 0)
  514. return;
  515. size = read(s->fd_in, buf, len);
  516. if (size == 0) {
  517. /* FD has been closed. Remove it from the active list. */
  518. qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
  519. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  520. return;
  521. }
  522. if (size > 0) {
  523. qemu_chr_be_write(chr, buf, size);
  524. }
  525. }
  526. static void fd_chr_update_read_handler(CharDriverState *chr)
  527. {
  528. FDCharDriver *s = chr->opaque;
  529. if (s->fd_in >= 0) {
  530. if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
  531. } else {
  532. qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
  533. fd_chr_read, NULL, chr);
  534. }
  535. }
  536. }
  537. static void fd_chr_close(struct CharDriverState *chr)
  538. {
  539. FDCharDriver *s = chr->opaque;
  540. if (s->fd_in >= 0) {
  541. if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
  542. } else {
  543. qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
  544. }
  545. }
  546. g_free(s);
  547. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  548. }
  549. /* open a character device to a unix fd */
  550. static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
  551. {
  552. CharDriverState *chr;
  553. FDCharDriver *s;
  554. chr = g_malloc0(sizeof(CharDriverState));
  555. s = g_malloc0(sizeof(FDCharDriver));
  556. s->fd_in = fd_in;
  557. s->fd_out = fd_out;
  558. chr->opaque = s;
  559. chr->chr_write = fd_chr_write;
  560. chr->chr_update_read_handler = fd_chr_update_read_handler;
  561. chr->chr_close = fd_chr_close;
  562. qemu_chr_generic_open(chr);
  563. return chr;
  564. }
  565. static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
  566. {
  567. int fd_out;
  568. TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
  569. O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
  570. if (fd_out < 0) {
  571. return NULL;
  572. }
  573. return qemu_chr_open_fd(-1, fd_out);
  574. }
  575. static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
  576. {
  577. int fd_in, fd_out;
  578. char filename_in[256], filename_out[256];
  579. const char *filename = qemu_opt_get(opts, "path");
  580. if (filename == NULL) {
  581. fprintf(stderr, "chardev: pipe: no filename given\n");
  582. return NULL;
  583. }
  584. snprintf(filename_in, 256, "%s.in", filename);
  585. snprintf(filename_out, 256, "%s.out", filename);
  586. TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
  587. TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
  588. if (fd_in < 0 || fd_out < 0) {
  589. if (fd_in >= 0)
  590. close(fd_in);
  591. if (fd_out >= 0)
  592. close(fd_out);
  593. TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
  594. if (fd_in < 0) {
  595. return NULL;
  596. }
  597. }
  598. return qemu_chr_open_fd(fd_in, fd_out);
  599. }
  600. /* for STDIO, we handle the case where several clients use it
  601. (nographic mode) */
  602. #define TERM_FIFO_MAX_SIZE 1
  603. static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
  604. static int term_fifo_size;
  605. static int stdio_read_poll(void *opaque)
  606. {
  607. CharDriverState *chr = opaque;
  608. /* try to flush the queue if needed */
  609. if (term_fifo_size != 0 && qemu_chr_be_can_write(chr) > 0) {
  610. qemu_chr_be_write(chr, term_fifo, 1);
  611. term_fifo_size = 0;
  612. }
  613. /* see if we can absorb more chars */
  614. if (term_fifo_size == 0)
  615. return 1;
  616. else
  617. return 0;
  618. }
  619. static void stdio_read(void *opaque)
  620. {
  621. int size;
  622. uint8_t buf[1];
  623. CharDriverState *chr = opaque;
  624. size = read(0, buf, 1);
  625. if (size == 0) {
  626. /* stdin has been closed. Remove it from the active list. */
  627. qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
  628. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  629. return;
  630. }
  631. if (size > 0) {
  632. if (qemu_chr_be_can_write(chr) > 0) {
  633. qemu_chr_be_write(chr, buf, 1);
  634. } else if (term_fifo_size == 0) {
  635. term_fifo[term_fifo_size++] = buf[0];
  636. }
  637. }
  638. }
  639. /* init terminal so that we can grab keys */
  640. static struct termios oldtty;
  641. static int old_fd0_flags;
  642. static bool stdio_allow_signal;
  643. static void term_exit(void)
  644. {
  645. tcsetattr (0, TCSANOW, &oldtty);
  646. fcntl(0, F_SETFL, old_fd0_flags);
  647. }
  648. static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
  649. {
  650. struct termios tty;
  651. tty = oldtty;
  652. if (!echo) {
  653. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  654. |INLCR|IGNCR|ICRNL|IXON);
  655. tty.c_oflag |= OPOST;
  656. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  657. tty.c_cflag &= ~(CSIZE|PARENB);
  658. tty.c_cflag |= CS8;
  659. tty.c_cc[VMIN] = 1;
  660. tty.c_cc[VTIME] = 0;
  661. }
  662. /* if graphical mode, we allow Ctrl-C handling */
  663. if (!stdio_allow_signal)
  664. tty.c_lflag &= ~ISIG;
  665. tcsetattr (0, TCSANOW, &tty);
  666. }
  667. static void qemu_chr_close_stdio(struct CharDriverState *chr)
  668. {
  669. term_exit();
  670. stdio_nb_clients--;
  671. qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
  672. fd_chr_close(chr);
  673. }
  674. static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
  675. {
  676. CharDriverState *chr;
  677. if (stdio_nb_clients >= STDIO_MAX_CLIENTS) {
  678. return NULL;
  679. }
  680. if (is_daemonized()) {
  681. error_report("cannot use stdio with -daemonize");
  682. return NULL;
  683. }
  684. if (stdio_nb_clients == 0) {
  685. old_fd0_flags = fcntl(0, F_GETFL);
  686. tcgetattr (0, &oldtty);
  687. fcntl(0, F_SETFL, O_NONBLOCK);
  688. atexit(term_exit);
  689. }
  690. chr = qemu_chr_open_fd(0, 1);
  691. chr->chr_close = qemu_chr_close_stdio;
  692. chr->chr_set_echo = qemu_chr_set_echo_stdio;
  693. qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
  694. stdio_nb_clients++;
  695. stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
  696. display_type != DT_NOGRAPHIC);
  697. qemu_chr_fe_set_echo(chr, false);
  698. return chr;
  699. }
  700. #ifdef __sun__
  701. /* Once Solaris has openpty(), this is going to be removed. */
  702. static int openpty(int *amaster, int *aslave, char *name,
  703. struct termios *termp, struct winsize *winp)
  704. {
  705. const char *slave;
  706. int mfd = -1, sfd = -1;
  707. *amaster = *aslave = -1;
  708. mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  709. if (mfd < 0)
  710. goto err;
  711. if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
  712. goto err;
  713. if ((slave = ptsname(mfd)) == NULL)
  714. goto err;
  715. if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
  716. goto err;
  717. if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
  718. (termp != NULL && tcgetattr(sfd, termp) < 0))
  719. goto err;
  720. if (amaster)
  721. *amaster = mfd;
  722. if (aslave)
  723. *aslave = sfd;
  724. if (winp)
  725. ioctl(sfd, TIOCSWINSZ, winp);
  726. return 0;
  727. err:
  728. if (sfd != -1)
  729. close(sfd);
  730. close(mfd);
  731. return -1;
  732. }
  733. static void cfmakeraw (struct termios *termios_p)
  734. {
  735. termios_p->c_iflag &=
  736. ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  737. termios_p->c_oflag &= ~OPOST;
  738. termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  739. termios_p->c_cflag &= ~(CSIZE|PARENB);
  740. termios_p->c_cflag |= CS8;
  741. termios_p->c_cc[VMIN] = 0;
  742. termios_p->c_cc[VTIME] = 0;
  743. }
  744. #endif
  745. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  746. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  747. || defined(__GLIBC__)
  748. typedef struct {
  749. int fd;
  750. int connected;
  751. int polling;
  752. int read_bytes;
  753. QEMUTimer *timer;
  754. } PtyCharDriver;
  755. static void pty_chr_update_read_handler(CharDriverState *chr);
  756. static void pty_chr_state(CharDriverState *chr, int connected);
  757. static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  758. {
  759. PtyCharDriver *s = chr->opaque;
  760. if (!s->connected) {
  761. /* guest sends data, check for (re-)connect */
  762. pty_chr_update_read_handler(chr);
  763. return 0;
  764. }
  765. return send_all(s->fd, buf, len);
  766. }
  767. static int pty_chr_read_poll(void *opaque)
  768. {
  769. CharDriverState *chr = opaque;
  770. PtyCharDriver *s = chr->opaque;
  771. s->read_bytes = qemu_chr_be_can_write(chr);
  772. return s->read_bytes;
  773. }
  774. static void pty_chr_read(void *opaque)
  775. {
  776. CharDriverState *chr = opaque;
  777. PtyCharDriver *s = chr->opaque;
  778. int size, len;
  779. uint8_t buf[READ_BUF_LEN];
  780. len = sizeof(buf);
  781. if (len > s->read_bytes)
  782. len = s->read_bytes;
  783. if (len == 0)
  784. return;
  785. size = read(s->fd, buf, len);
  786. if ((size == -1 && errno == EIO) ||
  787. (size == 0)) {
  788. pty_chr_state(chr, 0);
  789. return;
  790. }
  791. if (size > 0) {
  792. pty_chr_state(chr, 1);
  793. qemu_chr_be_write(chr, buf, size);
  794. }
  795. }
  796. static void pty_chr_update_read_handler(CharDriverState *chr)
  797. {
  798. PtyCharDriver *s = chr->opaque;
  799. qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
  800. pty_chr_read, NULL, chr);
  801. s->polling = 1;
  802. /*
  803. * Short timeout here: just need wait long enougth that qemu makes
  804. * it through the poll loop once. When reconnected we want a
  805. * short timeout so we notice it almost instantly. Otherwise
  806. * read() gives us -EIO instantly, making pty_chr_state() reset the
  807. * timeout to the normal (much longer) poll interval before the
  808. * timer triggers.
  809. */
  810. qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
  811. }
  812. static void pty_chr_state(CharDriverState *chr, int connected)
  813. {
  814. PtyCharDriver *s = chr->opaque;
  815. if (!connected) {
  816. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  817. s->connected = 0;
  818. s->polling = 0;
  819. /* (re-)connect poll interval for idle guests: once per second.
  820. * We check more frequently in case the guests sends data to
  821. * the virtual device linked to our pty. */
  822. qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
  823. } else {
  824. if (!s->connected)
  825. qemu_chr_generic_open(chr);
  826. s->connected = 1;
  827. }
  828. }
  829. static void pty_chr_timer(void *opaque)
  830. {
  831. struct CharDriverState *chr = opaque;
  832. PtyCharDriver *s = chr->opaque;
  833. if (s->connected)
  834. return;
  835. if (s->polling) {
  836. /* If we arrive here without polling being cleared due
  837. * read returning -EIO, then we are (re-)connected */
  838. pty_chr_state(chr, 1);
  839. return;
  840. }
  841. /* Next poll ... */
  842. pty_chr_update_read_handler(chr);
  843. }
  844. static void pty_chr_close(struct CharDriverState *chr)
  845. {
  846. PtyCharDriver *s = chr->opaque;
  847. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  848. close(s->fd);
  849. qemu_del_timer(s->timer);
  850. qemu_free_timer(s->timer);
  851. g_free(s);
  852. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  853. }
  854. static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
  855. {
  856. CharDriverState *chr;
  857. PtyCharDriver *s;
  858. struct termios tty;
  859. const char *label;
  860. int master_fd, slave_fd, len;
  861. #if defined(__OpenBSD__) || defined(__DragonFly__)
  862. char pty_name[PATH_MAX];
  863. #define q_ptsname(x) pty_name
  864. #else
  865. char *pty_name = NULL;
  866. #define q_ptsname(x) ptsname(x)
  867. #endif
  868. if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
  869. return NULL;
  870. }
  871. /* Set raw attributes on the pty. */
  872. tcgetattr(slave_fd, &tty);
  873. cfmakeraw(&tty);
  874. tcsetattr(slave_fd, TCSAFLUSH, &tty);
  875. close(slave_fd);
  876. chr = g_malloc0(sizeof(CharDriverState));
  877. len = strlen(q_ptsname(master_fd)) + 5;
  878. chr->filename = g_malloc(len);
  879. snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
  880. qemu_opt_set(opts, "path", q_ptsname(master_fd));
  881. label = qemu_opts_id(opts);
  882. fprintf(stderr, "char device redirected to %s%s%s%s\n",
  883. q_ptsname(master_fd),
  884. label ? " (label " : "",
  885. label ? label : "",
  886. label ? ")" : "");
  887. s = g_malloc0(sizeof(PtyCharDriver));
  888. chr->opaque = s;
  889. chr->chr_write = pty_chr_write;
  890. chr->chr_update_read_handler = pty_chr_update_read_handler;
  891. chr->chr_close = pty_chr_close;
  892. s->fd = master_fd;
  893. s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
  894. return chr;
  895. }
  896. static void tty_serial_init(int fd, int speed,
  897. int parity, int data_bits, int stop_bits)
  898. {
  899. struct termios tty;
  900. speed_t spd;
  901. #if 0
  902. printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
  903. speed, parity, data_bits, stop_bits);
  904. #endif
  905. tcgetattr (fd, &tty);
  906. #define check_speed(val) if (speed <= val) { spd = B##val; break; }
  907. speed = speed * 10 / 11;
  908. do {
  909. check_speed(50);
  910. check_speed(75);
  911. check_speed(110);
  912. check_speed(134);
  913. check_speed(150);
  914. check_speed(200);
  915. check_speed(300);
  916. check_speed(600);
  917. check_speed(1200);
  918. check_speed(1800);
  919. check_speed(2400);
  920. check_speed(4800);
  921. check_speed(9600);
  922. check_speed(19200);
  923. check_speed(38400);
  924. /* Non-Posix values follow. They may be unsupported on some systems. */
  925. check_speed(57600);
  926. check_speed(115200);
  927. #ifdef B230400
  928. check_speed(230400);
  929. #endif
  930. #ifdef B460800
  931. check_speed(460800);
  932. #endif
  933. #ifdef B500000
  934. check_speed(500000);
  935. #endif
  936. #ifdef B576000
  937. check_speed(576000);
  938. #endif
  939. #ifdef B921600
  940. check_speed(921600);
  941. #endif
  942. #ifdef B1000000
  943. check_speed(1000000);
  944. #endif
  945. #ifdef B1152000
  946. check_speed(1152000);
  947. #endif
  948. #ifdef B1500000
  949. check_speed(1500000);
  950. #endif
  951. #ifdef B2000000
  952. check_speed(2000000);
  953. #endif
  954. #ifdef B2500000
  955. check_speed(2500000);
  956. #endif
  957. #ifdef B3000000
  958. check_speed(3000000);
  959. #endif
  960. #ifdef B3500000
  961. check_speed(3500000);
  962. #endif
  963. #ifdef B4000000
  964. check_speed(4000000);
  965. #endif
  966. spd = B115200;
  967. } while (0);
  968. cfsetispeed(&tty, spd);
  969. cfsetospeed(&tty, spd);
  970. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  971. |INLCR|IGNCR|ICRNL|IXON);
  972. tty.c_oflag |= OPOST;
  973. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
  974. tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
  975. switch(data_bits) {
  976. default:
  977. case 8:
  978. tty.c_cflag |= CS8;
  979. break;
  980. case 7:
  981. tty.c_cflag |= CS7;
  982. break;
  983. case 6:
  984. tty.c_cflag |= CS6;
  985. break;
  986. case 5:
  987. tty.c_cflag |= CS5;
  988. break;
  989. }
  990. switch(parity) {
  991. default:
  992. case 'N':
  993. break;
  994. case 'E':
  995. tty.c_cflag |= PARENB;
  996. break;
  997. case 'O':
  998. tty.c_cflag |= PARENB | PARODD;
  999. break;
  1000. }
  1001. if (stop_bits == 2)
  1002. tty.c_cflag |= CSTOPB;
  1003. tcsetattr (fd, TCSANOW, &tty);
  1004. }
  1005. static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
  1006. {
  1007. FDCharDriver *s = chr->opaque;
  1008. switch(cmd) {
  1009. case CHR_IOCTL_SERIAL_SET_PARAMS:
  1010. {
  1011. QEMUSerialSetParams *ssp = arg;
  1012. tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
  1013. ssp->data_bits, ssp->stop_bits);
  1014. }
  1015. break;
  1016. case CHR_IOCTL_SERIAL_SET_BREAK:
  1017. {
  1018. int enable = *(int *)arg;
  1019. if (enable)
  1020. tcsendbreak(s->fd_in, 1);
  1021. }
  1022. break;
  1023. case CHR_IOCTL_SERIAL_GET_TIOCM:
  1024. {
  1025. int sarg = 0;
  1026. int *targ = (int *)arg;
  1027. ioctl(s->fd_in, TIOCMGET, &sarg);
  1028. *targ = 0;
  1029. if (sarg & TIOCM_CTS)
  1030. *targ |= CHR_TIOCM_CTS;
  1031. if (sarg & TIOCM_CAR)
  1032. *targ |= CHR_TIOCM_CAR;
  1033. if (sarg & TIOCM_DSR)
  1034. *targ |= CHR_TIOCM_DSR;
  1035. if (sarg & TIOCM_RI)
  1036. *targ |= CHR_TIOCM_RI;
  1037. if (sarg & TIOCM_DTR)
  1038. *targ |= CHR_TIOCM_DTR;
  1039. if (sarg & TIOCM_RTS)
  1040. *targ |= CHR_TIOCM_RTS;
  1041. }
  1042. break;
  1043. case CHR_IOCTL_SERIAL_SET_TIOCM:
  1044. {
  1045. int sarg = *(int *)arg;
  1046. int targ = 0;
  1047. ioctl(s->fd_in, TIOCMGET, &targ);
  1048. targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
  1049. | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
  1050. if (sarg & CHR_TIOCM_CTS)
  1051. targ |= TIOCM_CTS;
  1052. if (sarg & CHR_TIOCM_CAR)
  1053. targ |= TIOCM_CAR;
  1054. if (sarg & CHR_TIOCM_DSR)
  1055. targ |= TIOCM_DSR;
  1056. if (sarg & CHR_TIOCM_RI)
  1057. targ |= TIOCM_RI;
  1058. if (sarg & CHR_TIOCM_DTR)
  1059. targ |= TIOCM_DTR;
  1060. if (sarg & CHR_TIOCM_RTS)
  1061. targ |= TIOCM_RTS;
  1062. ioctl(s->fd_in, TIOCMSET, &targ);
  1063. }
  1064. break;
  1065. default:
  1066. return -ENOTSUP;
  1067. }
  1068. return 0;
  1069. }
  1070. static void qemu_chr_close_tty(CharDriverState *chr)
  1071. {
  1072. FDCharDriver *s = chr->opaque;
  1073. int fd = -1;
  1074. if (s) {
  1075. fd = s->fd_in;
  1076. }
  1077. fd_chr_close(chr);
  1078. if (fd >= 0) {
  1079. close(fd);
  1080. }
  1081. }
  1082. static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
  1083. {
  1084. const char *filename = qemu_opt_get(opts, "path");
  1085. CharDriverState *chr;
  1086. int fd;
  1087. TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
  1088. if (fd < 0) {
  1089. return NULL;
  1090. }
  1091. tty_serial_init(fd, 115200, 'N', 8, 1);
  1092. chr = qemu_chr_open_fd(fd, fd);
  1093. chr->chr_ioctl = tty_serial_ioctl;
  1094. chr->chr_close = qemu_chr_close_tty;
  1095. return chr;
  1096. }
  1097. #else /* ! __linux__ && ! __sun__ */
  1098. static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
  1099. {
  1100. return NULL;
  1101. }
  1102. #endif /* __linux__ || __sun__ */
  1103. #if defined(__linux__)
  1104. typedef struct {
  1105. int fd;
  1106. int mode;
  1107. } ParallelCharDriver;
  1108. static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
  1109. {
  1110. if (s->mode != mode) {
  1111. int m = mode;
  1112. if (ioctl(s->fd, PPSETMODE, &m) < 0)
  1113. return 0;
  1114. s->mode = mode;
  1115. }
  1116. return 1;
  1117. }
  1118. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1119. {
  1120. ParallelCharDriver *drv = chr->opaque;
  1121. int fd = drv->fd;
  1122. uint8_t b;
  1123. switch(cmd) {
  1124. case CHR_IOCTL_PP_READ_DATA:
  1125. if (ioctl(fd, PPRDATA, &b) < 0)
  1126. return -ENOTSUP;
  1127. *(uint8_t *)arg = b;
  1128. break;
  1129. case CHR_IOCTL_PP_WRITE_DATA:
  1130. b = *(uint8_t *)arg;
  1131. if (ioctl(fd, PPWDATA, &b) < 0)
  1132. return -ENOTSUP;
  1133. break;
  1134. case CHR_IOCTL_PP_READ_CONTROL:
  1135. if (ioctl(fd, PPRCONTROL, &b) < 0)
  1136. return -ENOTSUP;
  1137. /* Linux gives only the lowest bits, and no way to know data
  1138. direction! For better compatibility set the fixed upper
  1139. bits. */
  1140. *(uint8_t *)arg = b | 0xc0;
  1141. break;
  1142. case CHR_IOCTL_PP_WRITE_CONTROL:
  1143. b = *(uint8_t *)arg;
  1144. if (ioctl(fd, PPWCONTROL, &b) < 0)
  1145. return -ENOTSUP;
  1146. break;
  1147. case CHR_IOCTL_PP_READ_STATUS:
  1148. if (ioctl(fd, PPRSTATUS, &b) < 0)
  1149. return -ENOTSUP;
  1150. *(uint8_t *)arg = b;
  1151. break;
  1152. case CHR_IOCTL_PP_DATA_DIR:
  1153. if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
  1154. return -ENOTSUP;
  1155. break;
  1156. case CHR_IOCTL_PP_EPP_READ_ADDR:
  1157. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1158. struct ParallelIOArg *parg = arg;
  1159. int n = read(fd, parg->buffer, parg->count);
  1160. if (n != parg->count) {
  1161. return -EIO;
  1162. }
  1163. }
  1164. break;
  1165. case CHR_IOCTL_PP_EPP_READ:
  1166. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1167. struct ParallelIOArg *parg = arg;
  1168. int n = read(fd, parg->buffer, parg->count);
  1169. if (n != parg->count) {
  1170. return -EIO;
  1171. }
  1172. }
  1173. break;
  1174. case CHR_IOCTL_PP_EPP_WRITE_ADDR:
  1175. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1176. struct ParallelIOArg *parg = arg;
  1177. int n = write(fd, parg->buffer, parg->count);
  1178. if (n != parg->count) {
  1179. return -EIO;
  1180. }
  1181. }
  1182. break;
  1183. case CHR_IOCTL_PP_EPP_WRITE:
  1184. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1185. struct ParallelIOArg *parg = arg;
  1186. int n = write(fd, parg->buffer, parg->count);
  1187. if (n != parg->count) {
  1188. return -EIO;
  1189. }
  1190. }
  1191. break;
  1192. default:
  1193. return -ENOTSUP;
  1194. }
  1195. return 0;
  1196. }
  1197. static void pp_close(CharDriverState *chr)
  1198. {
  1199. ParallelCharDriver *drv = chr->opaque;
  1200. int fd = drv->fd;
  1201. pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
  1202. ioctl(fd, PPRELEASE);
  1203. close(fd);
  1204. g_free(drv);
  1205. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1206. }
  1207. static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
  1208. {
  1209. const char *filename = qemu_opt_get(opts, "path");
  1210. CharDriverState *chr;
  1211. ParallelCharDriver *drv;
  1212. int fd;
  1213. TFR(fd = qemu_open(filename, O_RDWR));
  1214. if (fd < 0) {
  1215. return NULL;
  1216. }
  1217. if (ioctl(fd, PPCLAIM) < 0) {
  1218. close(fd);
  1219. return NULL;
  1220. }
  1221. drv = g_malloc0(sizeof(ParallelCharDriver));
  1222. drv->fd = fd;
  1223. drv->mode = IEEE1284_MODE_COMPAT;
  1224. chr = g_malloc0(sizeof(CharDriverState));
  1225. chr->chr_write = null_chr_write;
  1226. chr->chr_ioctl = pp_ioctl;
  1227. chr->chr_close = pp_close;
  1228. chr->opaque = drv;
  1229. qemu_chr_generic_open(chr);
  1230. return chr;
  1231. }
  1232. #endif /* __linux__ */
  1233. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  1234. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1235. {
  1236. int fd = (int)(intptr_t)chr->opaque;
  1237. uint8_t b;
  1238. switch(cmd) {
  1239. case CHR_IOCTL_PP_READ_DATA:
  1240. if (ioctl(fd, PPIGDATA, &b) < 0)
  1241. return -ENOTSUP;
  1242. *(uint8_t *)arg = b;
  1243. break;
  1244. case CHR_IOCTL_PP_WRITE_DATA:
  1245. b = *(uint8_t *)arg;
  1246. if (ioctl(fd, PPISDATA, &b) < 0)
  1247. return -ENOTSUP;
  1248. break;
  1249. case CHR_IOCTL_PP_READ_CONTROL:
  1250. if (ioctl(fd, PPIGCTRL, &b) < 0)
  1251. return -ENOTSUP;
  1252. *(uint8_t *)arg = b;
  1253. break;
  1254. case CHR_IOCTL_PP_WRITE_CONTROL:
  1255. b = *(uint8_t *)arg;
  1256. if (ioctl(fd, PPISCTRL, &b) < 0)
  1257. return -ENOTSUP;
  1258. break;
  1259. case CHR_IOCTL_PP_READ_STATUS:
  1260. if (ioctl(fd, PPIGSTATUS, &b) < 0)
  1261. return -ENOTSUP;
  1262. *(uint8_t *)arg = b;
  1263. break;
  1264. default:
  1265. return -ENOTSUP;
  1266. }
  1267. return 0;
  1268. }
  1269. static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
  1270. {
  1271. const char *filename = qemu_opt_get(opts, "path");
  1272. CharDriverState *chr;
  1273. int fd;
  1274. fd = qemu_open(filename, O_RDWR);
  1275. if (fd < 0) {
  1276. return NULL;
  1277. }
  1278. chr = g_malloc0(sizeof(CharDriverState));
  1279. chr->opaque = (void *)(intptr_t)fd;
  1280. chr->chr_write = null_chr_write;
  1281. chr->chr_ioctl = pp_ioctl;
  1282. return chr;
  1283. }
  1284. #endif
  1285. #else /* _WIN32 */
  1286. static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
  1287. typedef struct {
  1288. int max_size;
  1289. HANDLE hcom, hrecv, hsend;
  1290. OVERLAPPED orecv, osend;
  1291. BOOL fpipe;
  1292. DWORD len;
  1293. } WinCharState;
  1294. typedef struct {
  1295. HANDLE hStdIn;
  1296. HANDLE hInputReadyEvent;
  1297. HANDLE hInputDoneEvent;
  1298. HANDLE hInputThread;
  1299. uint8_t win_stdio_buf;
  1300. } WinStdioCharState;
  1301. #define NSENDBUF 2048
  1302. #define NRECVBUF 2048
  1303. #define MAXCONNECT 1
  1304. #define NTIMEOUT 5000
  1305. static int win_chr_poll(void *opaque);
  1306. static int win_chr_pipe_poll(void *opaque);
  1307. static void win_chr_close(CharDriverState *chr)
  1308. {
  1309. WinCharState *s = chr->opaque;
  1310. if (s->hsend) {
  1311. CloseHandle(s->hsend);
  1312. s->hsend = NULL;
  1313. }
  1314. if (s->hrecv) {
  1315. CloseHandle(s->hrecv);
  1316. s->hrecv = NULL;
  1317. }
  1318. if (s->hcom) {
  1319. CloseHandle(s->hcom);
  1320. s->hcom = NULL;
  1321. }
  1322. if (s->fpipe)
  1323. qemu_del_polling_cb(win_chr_pipe_poll, chr);
  1324. else
  1325. qemu_del_polling_cb(win_chr_poll, chr);
  1326. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1327. }
  1328. static int win_chr_init(CharDriverState *chr, const char *filename)
  1329. {
  1330. WinCharState *s = chr->opaque;
  1331. COMMCONFIG comcfg;
  1332. COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
  1333. COMSTAT comstat;
  1334. DWORD size;
  1335. DWORD err;
  1336. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1337. if (!s->hsend) {
  1338. fprintf(stderr, "Failed CreateEvent\n");
  1339. goto fail;
  1340. }
  1341. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1342. if (!s->hrecv) {
  1343. fprintf(stderr, "Failed CreateEvent\n");
  1344. goto fail;
  1345. }
  1346. s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
  1347. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  1348. if (s->hcom == INVALID_HANDLE_VALUE) {
  1349. fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
  1350. s->hcom = NULL;
  1351. goto fail;
  1352. }
  1353. if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
  1354. fprintf(stderr, "Failed SetupComm\n");
  1355. goto fail;
  1356. }
  1357. ZeroMemory(&comcfg, sizeof(COMMCONFIG));
  1358. size = sizeof(COMMCONFIG);
  1359. GetDefaultCommConfig(filename, &comcfg, &size);
  1360. comcfg.dcb.DCBlength = sizeof(DCB);
  1361. CommConfigDialog(filename, NULL, &comcfg);
  1362. if (!SetCommState(s->hcom, &comcfg.dcb)) {
  1363. fprintf(stderr, "Failed SetCommState\n");
  1364. goto fail;
  1365. }
  1366. if (!SetCommMask(s->hcom, EV_ERR)) {
  1367. fprintf(stderr, "Failed SetCommMask\n");
  1368. goto fail;
  1369. }
  1370. cto.ReadIntervalTimeout = MAXDWORD;
  1371. if (!SetCommTimeouts(s->hcom, &cto)) {
  1372. fprintf(stderr, "Failed SetCommTimeouts\n");
  1373. goto fail;
  1374. }
  1375. if (!ClearCommError(s->hcom, &err, &comstat)) {
  1376. fprintf(stderr, "Failed ClearCommError\n");
  1377. goto fail;
  1378. }
  1379. qemu_add_polling_cb(win_chr_poll, chr);
  1380. return 0;
  1381. fail:
  1382. win_chr_close(chr);
  1383. return -1;
  1384. }
  1385. static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
  1386. {
  1387. WinCharState *s = chr->opaque;
  1388. DWORD len, ret, size, err;
  1389. len = len1;
  1390. ZeroMemory(&s->osend, sizeof(s->osend));
  1391. s->osend.hEvent = s->hsend;
  1392. while (len > 0) {
  1393. if (s->hsend)
  1394. ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
  1395. else
  1396. ret = WriteFile(s->hcom, buf, len, &size, NULL);
  1397. if (!ret) {
  1398. err = GetLastError();
  1399. if (err == ERROR_IO_PENDING) {
  1400. ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
  1401. if (ret) {
  1402. buf += size;
  1403. len -= size;
  1404. } else {
  1405. break;
  1406. }
  1407. } else {
  1408. break;
  1409. }
  1410. } else {
  1411. buf += size;
  1412. len -= size;
  1413. }
  1414. }
  1415. return len1 - len;
  1416. }
  1417. static int win_chr_read_poll(CharDriverState *chr)
  1418. {
  1419. WinCharState *s = chr->opaque;
  1420. s->max_size = qemu_chr_be_can_write(chr);
  1421. return s->max_size;
  1422. }
  1423. static void win_chr_readfile(CharDriverState *chr)
  1424. {
  1425. WinCharState *s = chr->opaque;
  1426. int ret, err;
  1427. uint8_t buf[READ_BUF_LEN];
  1428. DWORD size;
  1429. ZeroMemory(&s->orecv, sizeof(s->orecv));
  1430. s->orecv.hEvent = s->hrecv;
  1431. ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
  1432. if (!ret) {
  1433. err = GetLastError();
  1434. if (err == ERROR_IO_PENDING) {
  1435. ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
  1436. }
  1437. }
  1438. if (size > 0) {
  1439. qemu_chr_be_write(chr, buf, size);
  1440. }
  1441. }
  1442. static void win_chr_read(CharDriverState *chr)
  1443. {
  1444. WinCharState *s = chr->opaque;
  1445. if (s->len > s->max_size)
  1446. s->len = s->max_size;
  1447. if (s->len == 0)
  1448. return;
  1449. win_chr_readfile(chr);
  1450. }
  1451. static int win_chr_poll(void *opaque)
  1452. {
  1453. CharDriverState *chr = opaque;
  1454. WinCharState *s = chr->opaque;
  1455. COMSTAT status;
  1456. DWORD comerr;
  1457. ClearCommError(s->hcom, &comerr, &status);
  1458. if (status.cbInQue > 0) {
  1459. s->len = status.cbInQue;
  1460. win_chr_read_poll(chr);
  1461. win_chr_read(chr);
  1462. return 1;
  1463. }
  1464. return 0;
  1465. }
  1466. static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
  1467. {
  1468. const char *filename = qemu_opt_get(opts, "path");
  1469. CharDriverState *chr;
  1470. WinCharState *s;
  1471. chr = g_malloc0(sizeof(CharDriverState));
  1472. s = g_malloc0(sizeof(WinCharState));
  1473. chr->opaque = s;
  1474. chr->chr_write = win_chr_write;
  1475. chr->chr_close = win_chr_close;
  1476. if (win_chr_init(chr, filename) < 0) {
  1477. g_free(s);
  1478. g_free(chr);
  1479. return NULL;
  1480. }
  1481. qemu_chr_generic_open(chr);
  1482. return chr;
  1483. }
  1484. static int win_chr_pipe_poll(void *opaque)
  1485. {
  1486. CharDriverState *chr = opaque;
  1487. WinCharState *s = chr->opaque;
  1488. DWORD size;
  1489. PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
  1490. if (size > 0) {
  1491. s->len = size;
  1492. win_chr_read_poll(chr);
  1493. win_chr_read(chr);
  1494. return 1;
  1495. }
  1496. return 0;
  1497. }
  1498. static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
  1499. {
  1500. WinCharState *s = chr->opaque;
  1501. OVERLAPPED ov;
  1502. int ret;
  1503. DWORD size;
  1504. char openname[256];
  1505. s->fpipe = TRUE;
  1506. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1507. if (!s->hsend) {
  1508. fprintf(stderr, "Failed CreateEvent\n");
  1509. goto fail;
  1510. }
  1511. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1512. if (!s->hrecv) {
  1513. fprintf(stderr, "Failed CreateEvent\n");
  1514. goto fail;
  1515. }
  1516. snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
  1517. s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  1518. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
  1519. PIPE_WAIT,
  1520. MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
  1521. if (s->hcom == INVALID_HANDLE_VALUE) {
  1522. fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
  1523. s->hcom = NULL;
  1524. goto fail;
  1525. }
  1526. ZeroMemory(&ov, sizeof(ov));
  1527. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  1528. ret = ConnectNamedPipe(s->hcom, &ov);
  1529. if (ret) {
  1530. fprintf(stderr, "Failed ConnectNamedPipe\n");
  1531. goto fail;
  1532. }
  1533. ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
  1534. if (!ret) {
  1535. fprintf(stderr, "Failed GetOverlappedResult\n");
  1536. if (ov.hEvent) {
  1537. CloseHandle(ov.hEvent);
  1538. ov.hEvent = NULL;
  1539. }
  1540. goto fail;
  1541. }
  1542. if (ov.hEvent) {
  1543. CloseHandle(ov.hEvent);
  1544. ov.hEvent = NULL;
  1545. }
  1546. qemu_add_polling_cb(win_chr_pipe_poll, chr);
  1547. return 0;
  1548. fail:
  1549. win_chr_close(chr);
  1550. return -1;
  1551. }
  1552. static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
  1553. {
  1554. const char *filename = qemu_opt_get(opts, "path");
  1555. CharDriverState *chr;
  1556. WinCharState *s;
  1557. chr = g_malloc0(sizeof(CharDriverState));
  1558. s = g_malloc0(sizeof(WinCharState));
  1559. chr->opaque = s;
  1560. chr->chr_write = win_chr_write;
  1561. chr->chr_close = win_chr_close;
  1562. if (win_chr_pipe_init(chr, filename) < 0) {
  1563. g_free(s);
  1564. g_free(chr);
  1565. return NULL;
  1566. }
  1567. qemu_chr_generic_open(chr);
  1568. return chr;
  1569. }
  1570. static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
  1571. {
  1572. CharDriverState *chr;
  1573. WinCharState *s;
  1574. chr = g_malloc0(sizeof(CharDriverState));
  1575. s = g_malloc0(sizeof(WinCharState));
  1576. s->hcom = fd_out;
  1577. chr->opaque = s;
  1578. chr->chr_write = win_chr_write;
  1579. qemu_chr_generic_open(chr);
  1580. return chr;
  1581. }
  1582. static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
  1583. {
  1584. return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
  1585. }
  1586. static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
  1587. {
  1588. const char *file_out = qemu_opt_get(opts, "path");
  1589. HANDLE fd_out;
  1590. fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  1591. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1592. if (fd_out == INVALID_HANDLE_VALUE) {
  1593. return NULL;
  1594. }
  1595. return qemu_chr_open_win_file(fd_out);
  1596. }
  1597. static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
  1598. {
  1599. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  1600. DWORD dwSize;
  1601. int len1;
  1602. len1 = len;
  1603. while (len1 > 0) {
  1604. if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
  1605. break;
  1606. }
  1607. buf += dwSize;
  1608. len1 -= dwSize;
  1609. }
  1610. return len - len1;
  1611. }
  1612. static void win_stdio_wait_func(void *opaque)
  1613. {
  1614. CharDriverState *chr = opaque;
  1615. WinStdioCharState *stdio = chr->opaque;
  1616. INPUT_RECORD buf[4];
  1617. int ret;
  1618. DWORD dwSize;
  1619. int i;
  1620. ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
  1621. &dwSize);
  1622. if (!ret) {
  1623. /* Avoid error storm */
  1624. qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
  1625. return;
  1626. }
  1627. for (i = 0; i < dwSize; i++) {
  1628. KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
  1629. if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
  1630. int j;
  1631. if (kev->uChar.AsciiChar != 0) {
  1632. for (j = 0; j < kev->wRepeatCount; j++) {
  1633. if (qemu_chr_be_can_write(chr)) {
  1634. uint8_t c = kev->uChar.AsciiChar;
  1635. qemu_chr_be_write(chr, &c, 1);
  1636. }
  1637. }
  1638. }
  1639. }
  1640. }
  1641. }
  1642. static DWORD WINAPI win_stdio_thread(LPVOID param)
  1643. {
  1644. CharDriverState *chr = param;
  1645. WinStdioCharState *stdio = chr->opaque;
  1646. int ret;
  1647. DWORD dwSize;
  1648. while (1) {
  1649. /* Wait for one byte */
  1650. ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
  1651. /* Exit in case of error, continue if nothing read */
  1652. if (!ret) {
  1653. break;
  1654. }
  1655. if (!dwSize) {
  1656. continue;
  1657. }
  1658. /* Some terminal emulator returns \r\n for Enter, just pass \n */
  1659. if (stdio->win_stdio_buf == '\r') {
  1660. continue;
  1661. }
  1662. /* Signal the main thread and wait until the byte was eaten */
  1663. if (!SetEvent(stdio->hInputReadyEvent)) {
  1664. break;
  1665. }
  1666. if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
  1667. != WAIT_OBJECT_0) {
  1668. break;
  1669. }
  1670. }
  1671. qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
  1672. return 0;
  1673. }
  1674. static void win_stdio_thread_wait_func(void *opaque)
  1675. {
  1676. CharDriverState *chr = opaque;
  1677. WinStdioCharState *stdio = chr->opaque;
  1678. if (qemu_chr_be_can_write(chr)) {
  1679. qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
  1680. }
  1681. SetEvent(stdio->hInputDoneEvent);
  1682. }
  1683. static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
  1684. {
  1685. WinStdioCharState *stdio = chr->opaque;
  1686. DWORD dwMode = 0;
  1687. GetConsoleMode(stdio->hStdIn, &dwMode);
  1688. if (echo) {
  1689. SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
  1690. } else {
  1691. SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
  1692. }
  1693. }
  1694. static void win_stdio_close(CharDriverState *chr)
  1695. {
  1696. WinStdioCharState *stdio = chr->opaque;
  1697. if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
  1698. CloseHandle(stdio->hInputReadyEvent);
  1699. }
  1700. if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
  1701. CloseHandle(stdio->hInputDoneEvent);
  1702. }
  1703. if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
  1704. TerminateThread(stdio->hInputThread, 0);
  1705. }
  1706. g_free(chr->opaque);
  1707. g_free(chr);
  1708. stdio_nb_clients--;
  1709. }
  1710. static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
  1711. {
  1712. CharDriverState *chr;
  1713. WinStdioCharState *stdio;
  1714. DWORD dwMode;
  1715. int is_console = 0;
  1716. if (stdio_nb_clients >= STDIO_MAX_CLIENTS
  1717. || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
  1718. return NULL;
  1719. }
  1720. chr = g_malloc0(sizeof(CharDriverState));
  1721. stdio = g_malloc0(sizeof(WinStdioCharState));
  1722. stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  1723. if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
  1724. fprintf(stderr, "cannot open stdio: invalid handle\n");
  1725. exit(1);
  1726. }
  1727. is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
  1728. chr->opaque = stdio;
  1729. chr->chr_write = win_stdio_write;
  1730. chr->chr_close = win_stdio_close;
  1731. if (stdio_nb_clients == 0) {
  1732. if (is_console) {
  1733. if (qemu_add_wait_object(stdio->hStdIn,
  1734. win_stdio_wait_func, chr)) {
  1735. fprintf(stderr, "qemu_add_wait_object: failed\n");
  1736. }
  1737. } else {
  1738. DWORD dwId;
  1739. stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1740. stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1741. stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
  1742. chr, 0, &dwId);
  1743. if (stdio->hInputThread == INVALID_HANDLE_VALUE
  1744. || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
  1745. || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
  1746. fprintf(stderr, "cannot create stdio thread or event\n");
  1747. exit(1);
  1748. }
  1749. if (qemu_add_wait_object(stdio->hInputReadyEvent,
  1750. win_stdio_thread_wait_func, chr)) {
  1751. fprintf(stderr, "qemu_add_wait_object: failed\n");
  1752. }
  1753. }
  1754. }
  1755. dwMode |= ENABLE_LINE_INPUT;
  1756. stdio_clients[stdio_nb_clients++] = chr;
  1757. if (stdio_nb_clients == 1 && is_console) {
  1758. /* set the terminal in raw mode */
  1759. /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
  1760. dwMode |= ENABLE_PROCESSED_INPUT;
  1761. }
  1762. SetConsoleMode(stdio->hStdIn, dwMode);
  1763. chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
  1764. qemu_chr_fe_set_echo(chr, false);
  1765. return chr;
  1766. }
  1767. #endif /* !_WIN32 */
  1768. /***********************************************************/
  1769. /* UDP Net console */
  1770. typedef struct {
  1771. int fd;
  1772. uint8_t buf[READ_BUF_LEN];
  1773. int bufcnt;
  1774. int bufptr;
  1775. int max_size;
  1776. } NetCharDriver;
  1777. static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1778. {
  1779. NetCharDriver *s = chr->opaque;
  1780. return send(s->fd, (const void *)buf, len, 0);
  1781. }
  1782. static int udp_chr_read_poll(void *opaque)
  1783. {
  1784. CharDriverState *chr = opaque;
  1785. NetCharDriver *s = chr->opaque;
  1786. s->max_size = qemu_chr_be_can_write(chr);
  1787. /* If there were any stray characters in the queue process them
  1788. * first
  1789. */
  1790. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1791. qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
  1792. s->bufptr++;
  1793. s->max_size = qemu_chr_be_can_write(chr);
  1794. }
  1795. return s->max_size;
  1796. }
  1797. static void udp_chr_read(void *opaque)
  1798. {
  1799. CharDriverState *chr = opaque;
  1800. NetCharDriver *s = chr->opaque;
  1801. if (s->max_size == 0)
  1802. return;
  1803. s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
  1804. s->bufptr = s->bufcnt;
  1805. if (s->bufcnt <= 0)
  1806. return;
  1807. s->bufptr = 0;
  1808. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1809. qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
  1810. s->bufptr++;
  1811. s->max_size = qemu_chr_be_can_write(chr);
  1812. }
  1813. }
  1814. static void udp_chr_update_read_handler(CharDriverState *chr)
  1815. {
  1816. NetCharDriver *s = chr->opaque;
  1817. if (s->fd >= 0) {
  1818. qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
  1819. udp_chr_read, NULL, chr);
  1820. }
  1821. }
  1822. static void udp_chr_close(CharDriverState *chr)
  1823. {
  1824. NetCharDriver *s = chr->opaque;
  1825. if (s->fd >= 0) {
  1826. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  1827. closesocket(s->fd);
  1828. }
  1829. g_free(s);
  1830. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1831. }
  1832. static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
  1833. {
  1834. CharDriverState *chr = NULL;
  1835. NetCharDriver *s = NULL;
  1836. Error *local_err = NULL;
  1837. int fd = -1;
  1838. chr = g_malloc0(sizeof(CharDriverState));
  1839. s = g_malloc0(sizeof(NetCharDriver));
  1840. fd = inet_dgram_opts(opts, &local_err);
  1841. if (fd < 0) {
  1842. goto return_err;
  1843. }
  1844. s->fd = fd;
  1845. s->bufcnt = 0;
  1846. s->bufptr = 0;
  1847. chr->opaque = s;
  1848. chr->chr_write = udp_chr_write;
  1849. chr->chr_update_read_handler = udp_chr_update_read_handler;
  1850. chr->chr_close = udp_chr_close;
  1851. return chr;
  1852. return_err:
  1853. if (local_err) {
  1854. qerror_report_err(local_err);
  1855. error_free(local_err);
  1856. }
  1857. g_free(chr);
  1858. g_free(s);
  1859. if (fd >= 0) {
  1860. closesocket(fd);
  1861. }
  1862. return NULL;
  1863. }
  1864. /***********************************************************/
  1865. /* TCP Net console */
  1866. typedef struct {
  1867. int fd, listen_fd;
  1868. int connected;
  1869. int max_size;
  1870. int do_telnetopt;
  1871. int do_nodelay;
  1872. int is_unix;
  1873. int msgfd;
  1874. } TCPCharDriver;
  1875. static void tcp_chr_accept(void *opaque);
  1876. static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1877. {
  1878. TCPCharDriver *s = chr->opaque;
  1879. if (s->connected) {
  1880. return send_all(s->fd, buf, len);
  1881. } else {
  1882. /* XXX: indicate an error ? */
  1883. return len;
  1884. }
  1885. }
  1886. static int tcp_chr_read_poll(void *opaque)
  1887. {
  1888. CharDriverState *chr = opaque;
  1889. TCPCharDriver *s = chr->opaque;
  1890. if (!s->connected)
  1891. return 0;
  1892. s->max_size = qemu_chr_be_can_write(chr);
  1893. return s->max_size;
  1894. }
  1895. #define IAC 255
  1896. #define IAC_BREAK 243
  1897. static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
  1898. TCPCharDriver *s,
  1899. uint8_t *buf, int *size)
  1900. {
  1901. /* Handle any telnet client's basic IAC options to satisfy char by
  1902. * char mode with no echo. All IAC options will be removed from
  1903. * the buf and the do_telnetopt variable will be used to track the
  1904. * state of the width of the IAC information.
  1905. *
  1906. * IAC commands come in sets of 3 bytes with the exception of the
  1907. * "IAC BREAK" command and the double IAC.
  1908. */
  1909. int i;
  1910. int j = 0;
  1911. for (i = 0; i < *size; i++) {
  1912. if (s->do_telnetopt > 1) {
  1913. if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
  1914. /* Double IAC means send an IAC */
  1915. if (j != i)
  1916. buf[j] = buf[i];
  1917. j++;
  1918. s->do_telnetopt = 1;
  1919. } else {
  1920. if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
  1921. /* Handle IAC break commands by sending a serial break */
  1922. qemu_chr_be_event(chr, CHR_EVENT_BREAK);
  1923. s->do_telnetopt++;
  1924. }
  1925. s->do_telnetopt++;
  1926. }
  1927. if (s->do_telnetopt >= 4) {
  1928. s->do_telnetopt = 1;
  1929. }
  1930. } else {
  1931. if ((unsigned char)buf[i] == IAC) {
  1932. s->do_telnetopt = 2;
  1933. } else {
  1934. if (j != i)
  1935. buf[j] = buf[i];
  1936. j++;
  1937. }
  1938. }
  1939. }
  1940. *size = j;
  1941. }
  1942. static int tcp_get_msgfd(CharDriverState *chr)
  1943. {
  1944. TCPCharDriver *s = chr->opaque;
  1945. int fd = s->msgfd;
  1946. s->msgfd = -1;
  1947. return fd;
  1948. }
  1949. #ifndef _WIN32
  1950. static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
  1951. {
  1952. TCPCharDriver *s = chr->opaque;
  1953. struct cmsghdr *cmsg;
  1954. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  1955. int fd;
  1956. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
  1957. cmsg->cmsg_level != SOL_SOCKET ||
  1958. cmsg->cmsg_type != SCM_RIGHTS)
  1959. continue;
  1960. fd = *((int *)CMSG_DATA(cmsg));
  1961. if (fd < 0)
  1962. continue;
  1963. #ifndef MSG_CMSG_CLOEXEC
  1964. qemu_set_cloexec(fd);
  1965. #endif
  1966. if (s->msgfd != -1)
  1967. close(s->msgfd);
  1968. s->msgfd = fd;
  1969. }
  1970. }
  1971. static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
  1972. {
  1973. TCPCharDriver *s = chr->opaque;
  1974. struct msghdr msg = { NULL, };
  1975. struct iovec iov[1];
  1976. union {
  1977. struct cmsghdr cmsg;
  1978. char control[CMSG_SPACE(sizeof(int))];
  1979. } msg_control;
  1980. int flags = 0;
  1981. ssize_t ret;
  1982. iov[0].iov_base = buf;
  1983. iov[0].iov_len = len;
  1984. msg.msg_iov = iov;
  1985. msg.msg_iovlen = 1;
  1986. msg.msg_control = &msg_control;
  1987. msg.msg_controllen = sizeof(msg_control);
  1988. #ifdef MSG_CMSG_CLOEXEC
  1989. flags |= MSG_CMSG_CLOEXEC;
  1990. #endif
  1991. ret = recvmsg(s->fd, &msg, flags);
  1992. if (ret > 0 && s->is_unix) {
  1993. unix_process_msgfd(chr, &msg);
  1994. }
  1995. return ret;
  1996. }
  1997. #else
  1998. static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
  1999. {
  2000. TCPCharDriver *s = chr->opaque;
  2001. return qemu_recv(s->fd, buf, len, 0);
  2002. }
  2003. #endif
  2004. static void tcp_chr_read(void *opaque)
  2005. {
  2006. CharDriverState *chr = opaque;
  2007. TCPCharDriver *s = chr->opaque;
  2008. uint8_t buf[READ_BUF_LEN];
  2009. int len, size;
  2010. if (!s->connected || s->max_size <= 0)
  2011. return;
  2012. len = sizeof(buf);
  2013. if (len > s->max_size)
  2014. len = s->max_size;
  2015. size = tcp_chr_recv(chr, (void *)buf, len);
  2016. if (size == 0) {
  2017. /* connection closed */
  2018. s->connected = 0;
  2019. if (s->listen_fd >= 0) {
  2020. qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
  2021. }
  2022. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  2023. closesocket(s->fd);
  2024. s->fd = -1;
  2025. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  2026. } else if (size > 0) {
  2027. if (s->do_telnetopt)
  2028. tcp_chr_process_IAC_bytes(chr, s, buf, &size);
  2029. if (size > 0)
  2030. qemu_chr_be_write(chr, buf, size);
  2031. }
  2032. }
  2033. #ifndef _WIN32
  2034. CharDriverState *qemu_chr_open_eventfd(int eventfd)
  2035. {
  2036. return qemu_chr_open_fd(eventfd, eventfd);
  2037. }
  2038. #endif
  2039. static void tcp_chr_connect(void *opaque)
  2040. {
  2041. CharDriverState *chr = opaque;
  2042. TCPCharDriver *s = chr->opaque;
  2043. s->connected = 1;
  2044. if (s->fd >= 0) {
  2045. qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
  2046. tcp_chr_read, NULL, chr);
  2047. }
  2048. qemu_chr_generic_open(chr);
  2049. }
  2050. #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
  2051. static void tcp_chr_telnet_init(int fd)
  2052. {
  2053. char buf[3];
  2054. /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
  2055. IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
  2056. send(fd, (char *)buf, 3, 0);
  2057. IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
  2058. send(fd, (char *)buf, 3, 0);
  2059. IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
  2060. send(fd, (char *)buf, 3, 0);
  2061. IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
  2062. send(fd, (char *)buf, 3, 0);
  2063. }
  2064. static void socket_set_nodelay(int fd)
  2065. {
  2066. int val = 1;
  2067. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
  2068. }
  2069. static int tcp_chr_add_client(CharDriverState *chr, int fd)
  2070. {
  2071. TCPCharDriver *s = chr->opaque;
  2072. if (s->fd != -1)
  2073. return -1;
  2074. socket_set_nonblock(fd);
  2075. if (s->do_nodelay)
  2076. socket_set_nodelay(fd);
  2077. s->fd = fd;
  2078. qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
  2079. tcp_chr_connect(chr);
  2080. return 0;
  2081. }
  2082. static void tcp_chr_accept(void *opaque)
  2083. {
  2084. CharDriverState *chr = opaque;
  2085. TCPCharDriver *s = chr->opaque;
  2086. struct sockaddr_in saddr;
  2087. #ifndef _WIN32
  2088. struct sockaddr_un uaddr;
  2089. #endif
  2090. struct sockaddr *addr;
  2091. socklen_t len;
  2092. int fd;
  2093. for(;;) {
  2094. #ifndef _WIN32
  2095. if (s->is_unix) {
  2096. len = sizeof(uaddr);
  2097. addr = (struct sockaddr *)&uaddr;
  2098. } else
  2099. #endif
  2100. {
  2101. len = sizeof(saddr);
  2102. addr = (struct sockaddr *)&saddr;
  2103. }
  2104. fd = qemu_accept(s->listen_fd, addr, &len);
  2105. if (fd < 0 && errno != EINTR) {
  2106. return;
  2107. } else if (fd >= 0) {
  2108. if (s->do_telnetopt)
  2109. tcp_chr_telnet_init(fd);
  2110. break;
  2111. }
  2112. }
  2113. if (tcp_chr_add_client(chr, fd) < 0)
  2114. close(fd);
  2115. }
  2116. static void tcp_chr_close(CharDriverState *chr)
  2117. {
  2118. TCPCharDriver *s = chr->opaque;
  2119. if (s->fd >= 0) {
  2120. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  2121. closesocket(s->fd);
  2122. }
  2123. if (s->listen_fd >= 0) {
  2124. qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
  2125. closesocket(s->listen_fd);
  2126. }
  2127. g_free(s);
  2128. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  2129. }
  2130. static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
  2131. {
  2132. CharDriverState *chr = NULL;
  2133. TCPCharDriver *s = NULL;
  2134. Error *local_err = NULL;
  2135. int fd = -1;
  2136. int is_listen;
  2137. int is_waitconnect;
  2138. int do_nodelay;
  2139. int is_unix;
  2140. int is_telnet;
  2141. is_listen = qemu_opt_get_bool(opts, "server", 0);
  2142. is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
  2143. is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
  2144. do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
  2145. is_unix = qemu_opt_get(opts, "path") != NULL;
  2146. if (!is_listen)
  2147. is_waitconnect = 0;
  2148. chr = g_malloc0(sizeof(CharDriverState));
  2149. s = g_malloc0(sizeof(TCPCharDriver));
  2150. if (is_unix) {
  2151. if (is_listen) {
  2152. fd = unix_listen_opts(opts, &local_err);
  2153. } else {
  2154. fd = unix_connect_opts(opts, &local_err, NULL, NULL);
  2155. }
  2156. } else {
  2157. if (is_listen) {
  2158. fd = inet_listen_opts(opts, 0, &local_err);
  2159. } else {
  2160. fd = inet_connect_opts(opts, &local_err, NULL, NULL);
  2161. }
  2162. }
  2163. if (fd < 0) {
  2164. goto fail;
  2165. }
  2166. if (!is_waitconnect)
  2167. socket_set_nonblock(fd);
  2168. s->connected = 0;
  2169. s->fd = -1;
  2170. s->listen_fd = -1;
  2171. s->msgfd = -1;
  2172. s->is_unix = is_unix;
  2173. s->do_nodelay = do_nodelay && !is_unix;
  2174. chr->opaque = s;
  2175. chr->chr_write = tcp_chr_write;
  2176. chr->chr_close = tcp_chr_close;
  2177. chr->get_msgfd = tcp_get_msgfd;
  2178. chr->chr_add_client = tcp_chr_add_client;
  2179. if (is_listen) {
  2180. s->listen_fd = fd;
  2181. qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
  2182. if (is_telnet)
  2183. s->do_telnetopt = 1;
  2184. } else {
  2185. s->connected = 1;
  2186. s->fd = fd;
  2187. socket_set_nodelay(fd);
  2188. tcp_chr_connect(chr);
  2189. }
  2190. /* for "info chardev" monitor command */
  2191. chr->filename = g_malloc(256);
  2192. if (is_unix) {
  2193. snprintf(chr->filename, 256, "unix:%s%s",
  2194. qemu_opt_get(opts, "path"),
  2195. qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
  2196. } else if (is_telnet) {
  2197. snprintf(chr->filename, 256, "telnet:%s:%s%s",
  2198. qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
  2199. qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
  2200. } else {
  2201. snprintf(chr->filename, 256, "tcp:%s:%s%s",
  2202. qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
  2203. qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
  2204. }
  2205. if (is_listen && is_waitconnect) {
  2206. printf("QEMU waiting for connection on: %s\n",
  2207. chr->filename);
  2208. tcp_chr_accept(chr);
  2209. socket_set_nonblock(s->listen_fd);
  2210. }
  2211. return chr;
  2212. fail:
  2213. if (local_err) {
  2214. qerror_report_err(local_err);
  2215. error_free(local_err);
  2216. }
  2217. if (fd >= 0) {
  2218. closesocket(fd);
  2219. }
  2220. g_free(s);
  2221. g_free(chr);
  2222. return NULL;
  2223. }
  2224. /***********************************************************/
  2225. /* Memory chardev */
  2226. typedef struct {
  2227. size_t outbuf_size;
  2228. size_t outbuf_capacity;
  2229. uint8_t *outbuf;
  2230. } MemoryDriver;
  2231. static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  2232. {
  2233. MemoryDriver *d = chr->opaque;
  2234. /* TODO: the QString implementation has the same code, we should
  2235. * introduce a generic way to do this in cutils.c */
  2236. if (d->outbuf_capacity < d->outbuf_size + len) {
  2237. /* grow outbuf */
  2238. d->outbuf_capacity += len;
  2239. d->outbuf_capacity *= 2;
  2240. d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
  2241. }
  2242. memcpy(d->outbuf + d->outbuf_size, buf, len);
  2243. d->outbuf_size += len;
  2244. return len;
  2245. }
  2246. void qemu_chr_init_mem(CharDriverState *chr)
  2247. {
  2248. MemoryDriver *d;
  2249. d = g_malloc(sizeof(*d));
  2250. d->outbuf_size = 0;
  2251. d->outbuf_capacity = 4096;
  2252. d->outbuf = g_malloc0(d->outbuf_capacity);
  2253. memset(chr, 0, sizeof(*chr));
  2254. chr->opaque = d;
  2255. chr->chr_write = mem_chr_write;
  2256. }
  2257. QString *qemu_chr_mem_to_qs(CharDriverState *chr)
  2258. {
  2259. MemoryDriver *d = chr->opaque;
  2260. return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
  2261. }
  2262. /* NOTE: this driver can not be closed with qemu_chr_delete()! */
  2263. void qemu_chr_close_mem(CharDriverState *chr)
  2264. {
  2265. MemoryDriver *d = chr->opaque;
  2266. g_free(d->outbuf);
  2267. g_free(chr->opaque);
  2268. chr->opaque = NULL;
  2269. chr->chr_write = NULL;
  2270. }
  2271. size_t qemu_chr_mem_osize(const CharDriverState *chr)
  2272. {
  2273. const MemoryDriver *d = chr->opaque;
  2274. return d->outbuf_size;
  2275. }
  2276. QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
  2277. {
  2278. char host[65], port[33], width[8], height[8];
  2279. int pos;
  2280. const char *p;
  2281. QemuOpts *opts;
  2282. Error *local_err = NULL;
  2283. opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
  2284. if (error_is_set(&local_err)) {
  2285. qerror_report_err(local_err);
  2286. error_free(local_err);
  2287. return NULL;
  2288. }
  2289. if (strstart(filename, "mon:", &p)) {
  2290. filename = p;
  2291. qemu_opt_set(opts, "mux", "on");
  2292. }
  2293. if (strcmp(filename, "null") == 0 ||
  2294. strcmp(filename, "pty") == 0 ||
  2295. strcmp(filename, "msmouse") == 0 ||
  2296. strcmp(filename, "braille") == 0 ||
  2297. strcmp(filename, "stdio") == 0) {
  2298. qemu_opt_set(opts, "backend", filename);
  2299. return opts;
  2300. }
  2301. if (strstart(filename, "vc", &p)) {
  2302. qemu_opt_set(opts, "backend", "vc");
  2303. if (*p == ':') {
  2304. if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
  2305. /* pixels */
  2306. qemu_opt_set(opts, "width", width);
  2307. qemu_opt_set(opts, "height", height);
  2308. } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
  2309. /* chars */
  2310. qemu_opt_set(opts, "cols", width);
  2311. qemu_opt_set(opts, "rows", height);
  2312. } else {
  2313. goto fail;
  2314. }
  2315. }
  2316. return opts;
  2317. }
  2318. if (strcmp(filename, "con:") == 0) {
  2319. qemu_opt_set(opts, "backend", "console");
  2320. return opts;
  2321. }
  2322. if (strstart(filename, "COM", NULL)) {
  2323. qemu_opt_set(opts, "backend", "serial");
  2324. qemu_opt_set(opts, "path", filename);
  2325. return opts;
  2326. }
  2327. if (strstart(filename, "file:", &p)) {
  2328. qemu_opt_set(opts, "backend", "file");
  2329. qemu_opt_set(opts, "path", p);
  2330. return opts;
  2331. }
  2332. if (strstart(filename, "pipe:", &p)) {
  2333. qemu_opt_set(opts, "backend", "pipe");
  2334. qemu_opt_set(opts, "path", p);
  2335. return opts;
  2336. }
  2337. if (strstart(filename, "tcp:", &p) ||
  2338. strstart(filename, "telnet:", &p)) {
  2339. if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
  2340. host[0] = 0;
  2341. if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
  2342. goto fail;
  2343. }
  2344. qemu_opt_set(opts, "backend", "socket");
  2345. qemu_opt_set(opts, "host", host);
  2346. qemu_opt_set(opts, "port", port);
  2347. if (p[pos] == ',') {
  2348. if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
  2349. goto fail;
  2350. }
  2351. if (strstart(filename, "telnet:", &p))
  2352. qemu_opt_set(opts, "telnet", "on");
  2353. return opts;
  2354. }
  2355. if (strstart(filename, "udp:", &p)) {
  2356. qemu_opt_set(opts, "backend", "udp");
  2357. if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
  2358. host[0] = 0;
  2359. if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
  2360. goto fail;
  2361. }
  2362. }
  2363. qemu_opt_set(opts, "host", host);
  2364. qemu_opt_set(opts, "port", port);
  2365. if (p[pos] == '@') {
  2366. p += pos + 1;
  2367. if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
  2368. host[0] = 0;
  2369. if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
  2370. goto fail;
  2371. }
  2372. }
  2373. qemu_opt_set(opts, "localaddr", host);
  2374. qemu_opt_set(opts, "localport", port);
  2375. }
  2376. return opts;
  2377. }
  2378. if (strstart(filename, "unix:", &p)) {
  2379. qemu_opt_set(opts, "backend", "socket");
  2380. if (qemu_opts_do_parse(opts, p, "path") != 0)
  2381. goto fail;
  2382. return opts;
  2383. }
  2384. if (strstart(filename, "/dev/parport", NULL) ||
  2385. strstart(filename, "/dev/ppi", NULL)) {
  2386. qemu_opt_set(opts, "backend", "parport");
  2387. qemu_opt_set(opts, "path", filename);
  2388. return opts;
  2389. }
  2390. if (strstart(filename, "/dev/", NULL)) {
  2391. qemu_opt_set(opts, "backend", "tty");
  2392. qemu_opt_set(opts, "path", filename);
  2393. return opts;
  2394. }
  2395. fail:
  2396. qemu_opts_del(opts);
  2397. return NULL;
  2398. }
  2399. static const struct {
  2400. const char *name;
  2401. CharDriverState *(*open)(QemuOpts *opts);
  2402. } backend_table[] = {
  2403. { .name = "null", .open = qemu_chr_open_null },
  2404. { .name = "socket", .open = qemu_chr_open_socket },
  2405. { .name = "udp", .open = qemu_chr_open_udp },
  2406. { .name = "msmouse", .open = qemu_chr_open_msmouse },
  2407. { .name = "vc", .open = text_console_init },
  2408. #ifdef _WIN32
  2409. { .name = "file", .open = qemu_chr_open_win_file_out },
  2410. { .name = "pipe", .open = qemu_chr_open_win_pipe },
  2411. { .name = "console", .open = qemu_chr_open_win_con },
  2412. { .name = "serial", .open = qemu_chr_open_win },
  2413. { .name = "stdio", .open = qemu_chr_open_win_stdio },
  2414. #else
  2415. { .name = "file", .open = qemu_chr_open_file_out },
  2416. { .name = "pipe", .open = qemu_chr_open_pipe },
  2417. { .name = "pty", .open = qemu_chr_open_pty },
  2418. { .name = "stdio", .open = qemu_chr_open_stdio },
  2419. #endif
  2420. #ifdef CONFIG_BRLAPI
  2421. { .name = "braille", .open = chr_baum_init },
  2422. #endif
  2423. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  2424. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  2425. || defined(__FreeBSD_kernel__)
  2426. { .name = "tty", .open = qemu_chr_open_tty },
  2427. #endif
  2428. #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
  2429. || defined(__FreeBSD_kernel__)
  2430. { .name = "parport", .open = qemu_chr_open_pp },
  2431. #endif
  2432. #ifdef CONFIG_SPICE
  2433. { .name = "spicevmc", .open = qemu_chr_open_spice },
  2434. #if SPICE_SERVER_VERSION >= 0x000c02
  2435. { .name = "spiceport", .open = qemu_chr_open_spice_port },
  2436. #endif
  2437. #endif
  2438. };
  2439. CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
  2440. void (*init)(struct CharDriverState *s))
  2441. {
  2442. CharDriverState *chr;
  2443. int i;
  2444. if (qemu_opts_id(opts) == NULL) {
  2445. fprintf(stderr, "chardev: no id specified\n");
  2446. return NULL;
  2447. }
  2448. if (qemu_opt_get(opts, "backend") == NULL) {
  2449. fprintf(stderr, "chardev: \"%s\" missing backend\n",
  2450. qemu_opts_id(opts));
  2451. return NULL;
  2452. }
  2453. for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
  2454. if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
  2455. break;
  2456. }
  2457. if (i == ARRAY_SIZE(backend_table)) {
  2458. fprintf(stderr, "chardev: backend \"%s\" not found\n",
  2459. qemu_opt_get(opts, "backend"));
  2460. return NULL;
  2461. }
  2462. chr = backend_table[i].open(opts);
  2463. if (!chr) {
  2464. fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
  2465. qemu_opt_get(opts, "backend"));
  2466. return NULL;
  2467. }
  2468. if (!chr->filename)
  2469. chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
  2470. chr->init = init;
  2471. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  2472. if (qemu_opt_get_bool(opts, "mux", 0)) {
  2473. CharDriverState *base = chr;
  2474. int len = strlen(qemu_opts_id(opts)) + 6;
  2475. base->label = g_malloc(len);
  2476. snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
  2477. chr = qemu_chr_open_mux(base);
  2478. chr->filename = base->filename;
  2479. chr->avail_connections = MAX_MUX;
  2480. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  2481. } else {
  2482. chr->avail_connections = 1;
  2483. }
  2484. chr->label = g_strdup(qemu_opts_id(opts));
  2485. return chr;
  2486. }
  2487. CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
  2488. {
  2489. const char *p;
  2490. CharDriverState *chr;
  2491. QemuOpts *opts;
  2492. if (strstart(filename, "chardev:", &p)) {
  2493. return qemu_chr_find(p);
  2494. }
  2495. opts = qemu_chr_parse_compat(label, filename);
  2496. if (!opts)
  2497. return NULL;
  2498. chr = qemu_chr_new_from_opts(opts, init);
  2499. if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
  2500. monitor_init(chr, MONITOR_USE_READLINE);
  2501. }
  2502. qemu_opts_del(opts);
  2503. return chr;
  2504. }
  2505. void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
  2506. {
  2507. if (chr->chr_set_echo) {
  2508. chr->chr_set_echo(chr, echo);
  2509. }
  2510. }
  2511. void qemu_chr_fe_open(struct CharDriverState *chr)
  2512. {
  2513. if (chr->chr_guest_open) {
  2514. chr->chr_guest_open(chr);
  2515. }
  2516. }
  2517. void qemu_chr_fe_close(struct CharDriverState *chr)
  2518. {
  2519. if (chr->chr_guest_close) {
  2520. chr->chr_guest_close(chr);
  2521. }
  2522. }
  2523. void qemu_chr_delete(CharDriverState *chr)
  2524. {
  2525. QTAILQ_REMOVE(&chardevs, chr, next);
  2526. if (chr->chr_close)
  2527. chr->chr_close(chr);
  2528. g_free(chr->filename);
  2529. g_free(chr->label);
  2530. g_free(chr);
  2531. }
  2532. ChardevInfoList *qmp_query_chardev(Error **errp)
  2533. {
  2534. ChardevInfoList *chr_list = NULL;
  2535. CharDriverState *chr;
  2536. QTAILQ_FOREACH(chr, &chardevs, next) {
  2537. ChardevInfoList *info = g_malloc0(sizeof(*info));
  2538. info->value = g_malloc0(sizeof(*info->value));
  2539. info->value->label = g_strdup(chr->label);
  2540. info->value->filename = g_strdup(chr->filename);
  2541. info->next = chr_list;
  2542. chr_list = info;
  2543. }
  2544. return chr_list;
  2545. }
  2546. CharDriverState *qemu_chr_find(const char *name)
  2547. {
  2548. CharDriverState *chr;
  2549. QTAILQ_FOREACH(chr, &chardevs, next) {
  2550. if (strcmp(chr->label, name) != 0)
  2551. continue;
  2552. return chr;
  2553. }
  2554. return NULL;
  2555. }
  2556. /* Get a character (serial) device interface. */
  2557. CharDriverState *qemu_char_get_next_serial(void)
  2558. {
  2559. static int next_serial;
  2560. /* FIXME: This function needs to go away: use chardev properties! */
  2561. return serial_hds[next_serial++];
  2562. }
  2563. QemuOptsList qemu_chardev_opts = {
  2564. .name = "chardev",
  2565. .implied_opt_name = "backend",
  2566. .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
  2567. .desc = {
  2568. {
  2569. .name = "backend",
  2570. .type = QEMU_OPT_STRING,
  2571. },{
  2572. .name = "path",
  2573. .type = QEMU_OPT_STRING,
  2574. },{
  2575. .name = "host",
  2576. .type = QEMU_OPT_STRING,
  2577. },{
  2578. .name = "port",
  2579. .type = QEMU_OPT_STRING,
  2580. },{
  2581. .name = "localaddr",
  2582. .type = QEMU_OPT_STRING,
  2583. },{
  2584. .name = "localport",
  2585. .type = QEMU_OPT_STRING,
  2586. },{
  2587. .name = "to",
  2588. .type = QEMU_OPT_NUMBER,
  2589. },{
  2590. .name = "ipv4",
  2591. .type = QEMU_OPT_BOOL,
  2592. },{
  2593. .name = "ipv6",
  2594. .type = QEMU_OPT_BOOL,
  2595. },{
  2596. .name = "wait",
  2597. .type = QEMU_OPT_BOOL,
  2598. },{
  2599. .name = "server",
  2600. .type = QEMU_OPT_BOOL,
  2601. },{
  2602. .name = "delay",
  2603. .type = QEMU_OPT_BOOL,
  2604. },{
  2605. .name = "telnet",
  2606. .type = QEMU_OPT_BOOL,
  2607. },{
  2608. .name = "width",
  2609. .type = QEMU_OPT_NUMBER,
  2610. },{
  2611. .name = "height",
  2612. .type = QEMU_OPT_NUMBER,
  2613. },{
  2614. .name = "cols",
  2615. .type = QEMU_OPT_NUMBER,
  2616. },{
  2617. .name = "rows",
  2618. .type = QEMU_OPT_NUMBER,
  2619. },{
  2620. .name = "mux",
  2621. .type = QEMU_OPT_BOOL,
  2622. },{
  2623. .name = "signal",
  2624. .type = QEMU_OPT_BOOL,
  2625. },{
  2626. .name = "name",
  2627. .type = QEMU_OPT_STRING,
  2628. },{
  2629. .name = "debug",
  2630. .type = QEMU_OPT_NUMBER,
  2631. },
  2632. { /* end of list */ }
  2633. },
  2634. };