2
0

qemu-char.c 74 KB

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