qemu-char.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912
  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.h"
  26. #include "ui/console.h"
  27. #include "sysemu.h"
  28. #include "qemu-timer.h"
  29. #include "qemu-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_socket.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 (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. return chr;
  695. }
  696. #ifdef __sun__
  697. /* Once Solaris has openpty(), this is going to be removed. */
  698. static int openpty(int *amaster, int *aslave, char *name,
  699. struct termios *termp, struct winsize *winp)
  700. {
  701. const char *slave;
  702. int mfd = -1, sfd = -1;
  703. *amaster = *aslave = -1;
  704. mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  705. if (mfd < 0)
  706. goto err;
  707. if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
  708. goto err;
  709. if ((slave = ptsname(mfd)) == NULL)
  710. goto err;
  711. if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
  712. goto err;
  713. if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
  714. (termp != NULL && tcgetattr(sfd, termp) < 0))
  715. goto err;
  716. if (amaster)
  717. *amaster = mfd;
  718. if (aslave)
  719. *aslave = sfd;
  720. if (winp)
  721. ioctl(sfd, TIOCSWINSZ, winp);
  722. return 0;
  723. err:
  724. if (sfd != -1)
  725. close(sfd);
  726. close(mfd);
  727. return -1;
  728. }
  729. static void cfmakeraw (struct termios *termios_p)
  730. {
  731. termios_p->c_iflag &=
  732. ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  733. termios_p->c_oflag &= ~OPOST;
  734. termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  735. termios_p->c_cflag &= ~(CSIZE|PARENB);
  736. termios_p->c_cflag |= CS8;
  737. termios_p->c_cc[VMIN] = 0;
  738. termios_p->c_cc[VTIME] = 0;
  739. }
  740. #endif
  741. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  742. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  743. || defined(__GLIBC__)
  744. typedef struct {
  745. int fd;
  746. int connected;
  747. int polling;
  748. int read_bytes;
  749. QEMUTimer *timer;
  750. } PtyCharDriver;
  751. static void pty_chr_update_read_handler(CharDriverState *chr);
  752. static void pty_chr_state(CharDriverState *chr, int connected);
  753. static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  754. {
  755. PtyCharDriver *s = chr->opaque;
  756. if (!s->connected) {
  757. /* guest sends data, check for (re-)connect */
  758. pty_chr_update_read_handler(chr);
  759. return 0;
  760. }
  761. return send_all(s->fd, buf, len);
  762. }
  763. static int pty_chr_read_poll(void *opaque)
  764. {
  765. CharDriverState *chr = opaque;
  766. PtyCharDriver *s = chr->opaque;
  767. s->read_bytes = qemu_chr_be_can_write(chr);
  768. return s->read_bytes;
  769. }
  770. static void pty_chr_read(void *opaque)
  771. {
  772. CharDriverState *chr = opaque;
  773. PtyCharDriver *s = chr->opaque;
  774. int size, len;
  775. uint8_t buf[READ_BUF_LEN];
  776. len = sizeof(buf);
  777. if (len > s->read_bytes)
  778. len = s->read_bytes;
  779. if (len == 0)
  780. return;
  781. size = read(s->fd, buf, len);
  782. if ((size == -1 && errno == EIO) ||
  783. (size == 0)) {
  784. pty_chr_state(chr, 0);
  785. return;
  786. }
  787. if (size > 0) {
  788. pty_chr_state(chr, 1);
  789. qemu_chr_be_write(chr, buf, size);
  790. }
  791. }
  792. static void pty_chr_update_read_handler(CharDriverState *chr)
  793. {
  794. PtyCharDriver *s = chr->opaque;
  795. qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
  796. pty_chr_read, NULL, chr);
  797. s->polling = 1;
  798. /*
  799. * Short timeout here: just need wait long enougth that qemu makes
  800. * it through the poll loop once. When reconnected we want a
  801. * short timeout so we notice it almost instantly. Otherwise
  802. * read() gives us -EIO instantly, making pty_chr_state() reset the
  803. * timeout to the normal (much longer) poll interval before the
  804. * timer triggers.
  805. */
  806. qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 10);
  807. }
  808. static void pty_chr_state(CharDriverState *chr, int connected)
  809. {
  810. PtyCharDriver *s = chr->opaque;
  811. if (!connected) {
  812. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  813. s->connected = 0;
  814. s->polling = 0;
  815. /* (re-)connect poll interval for idle guests: once per second.
  816. * We check more frequently in case the guests sends data to
  817. * the virtual device linked to our pty. */
  818. qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 1000);
  819. } else {
  820. if (!s->connected)
  821. qemu_chr_generic_open(chr);
  822. s->connected = 1;
  823. }
  824. }
  825. static void pty_chr_timer(void *opaque)
  826. {
  827. struct CharDriverState *chr = opaque;
  828. PtyCharDriver *s = chr->opaque;
  829. if (s->connected)
  830. return;
  831. if (s->polling) {
  832. /* If we arrive here without polling being cleared due
  833. * read returning -EIO, then we are (re-)connected */
  834. pty_chr_state(chr, 1);
  835. return;
  836. }
  837. /* Next poll ... */
  838. pty_chr_update_read_handler(chr);
  839. }
  840. static void pty_chr_close(struct CharDriverState *chr)
  841. {
  842. PtyCharDriver *s = chr->opaque;
  843. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  844. close(s->fd);
  845. qemu_del_timer(s->timer);
  846. qemu_free_timer(s->timer);
  847. g_free(s);
  848. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  849. }
  850. static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
  851. {
  852. CharDriverState *chr;
  853. PtyCharDriver *s;
  854. struct termios tty;
  855. int master_fd, slave_fd, len;
  856. #if defined(__OpenBSD__) || defined(__DragonFly__)
  857. char pty_name[PATH_MAX];
  858. #define q_ptsname(x) pty_name
  859. #else
  860. char *pty_name = NULL;
  861. #define q_ptsname(x) ptsname(x)
  862. #endif
  863. if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
  864. return NULL;
  865. }
  866. /* Set raw attributes on the pty. */
  867. tcgetattr(slave_fd, &tty);
  868. cfmakeraw(&tty);
  869. tcsetattr(slave_fd, TCSAFLUSH, &tty);
  870. close(slave_fd);
  871. chr = g_malloc0(sizeof(CharDriverState));
  872. len = strlen(q_ptsname(master_fd)) + 5;
  873. chr->filename = g_malloc(len);
  874. snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
  875. qemu_opt_set(opts, "path", q_ptsname(master_fd));
  876. fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
  877. s = g_malloc0(sizeof(PtyCharDriver));
  878. chr->opaque = s;
  879. chr->chr_write = pty_chr_write;
  880. chr->chr_update_read_handler = pty_chr_update_read_handler;
  881. chr->chr_close = pty_chr_close;
  882. s->fd = master_fd;
  883. s->timer = qemu_new_timer_ms(rt_clock, pty_chr_timer, chr);
  884. return chr;
  885. }
  886. static void tty_serial_init(int fd, int speed,
  887. int parity, int data_bits, int stop_bits)
  888. {
  889. struct termios tty;
  890. speed_t spd;
  891. #if 0
  892. printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
  893. speed, parity, data_bits, stop_bits);
  894. #endif
  895. tcgetattr (fd, &tty);
  896. #define check_speed(val) if (speed <= val) { spd = B##val; break; }
  897. speed = speed * 10 / 11;
  898. do {
  899. check_speed(50);
  900. check_speed(75);
  901. check_speed(110);
  902. check_speed(134);
  903. check_speed(150);
  904. check_speed(200);
  905. check_speed(300);
  906. check_speed(600);
  907. check_speed(1200);
  908. check_speed(1800);
  909. check_speed(2400);
  910. check_speed(4800);
  911. check_speed(9600);
  912. check_speed(19200);
  913. check_speed(38400);
  914. /* Non-Posix values follow. They may be unsupported on some systems. */
  915. check_speed(57600);
  916. check_speed(115200);
  917. #ifdef B230400
  918. check_speed(230400);
  919. #endif
  920. #ifdef B460800
  921. check_speed(460800);
  922. #endif
  923. #ifdef B500000
  924. check_speed(500000);
  925. #endif
  926. #ifdef B576000
  927. check_speed(576000);
  928. #endif
  929. #ifdef B921600
  930. check_speed(921600);
  931. #endif
  932. #ifdef B1000000
  933. check_speed(1000000);
  934. #endif
  935. #ifdef B1152000
  936. check_speed(1152000);
  937. #endif
  938. #ifdef B1500000
  939. check_speed(1500000);
  940. #endif
  941. #ifdef B2000000
  942. check_speed(2000000);
  943. #endif
  944. #ifdef B2500000
  945. check_speed(2500000);
  946. #endif
  947. #ifdef B3000000
  948. check_speed(3000000);
  949. #endif
  950. #ifdef B3500000
  951. check_speed(3500000);
  952. #endif
  953. #ifdef B4000000
  954. check_speed(4000000);
  955. #endif
  956. spd = B115200;
  957. } while (0);
  958. cfsetispeed(&tty, spd);
  959. cfsetospeed(&tty, spd);
  960. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  961. |INLCR|IGNCR|ICRNL|IXON);
  962. tty.c_oflag |= OPOST;
  963. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
  964. tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
  965. switch(data_bits) {
  966. default:
  967. case 8:
  968. tty.c_cflag |= CS8;
  969. break;
  970. case 7:
  971. tty.c_cflag |= CS7;
  972. break;
  973. case 6:
  974. tty.c_cflag |= CS6;
  975. break;
  976. case 5:
  977. tty.c_cflag |= CS5;
  978. break;
  979. }
  980. switch(parity) {
  981. default:
  982. case 'N':
  983. break;
  984. case 'E':
  985. tty.c_cflag |= PARENB;
  986. break;
  987. case 'O':
  988. tty.c_cflag |= PARENB | PARODD;
  989. break;
  990. }
  991. if (stop_bits == 2)
  992. tty.c_cflag |= CSTOPB;
  993. tcsetattr (fd, TCSANOW, &tty);
  994. }
  995. static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
  996. {
  997. FDCharDriver *s = chr->opaque;
  998. switch(cmd) {
  999. case CHR_IOCTL_SERIAL_SET_PARAMS:
  1000. {
  1001. QEMUSerialSetParams *ssp = arg;
  1002. tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
  1003. ssp->data_bits, ssp->stop_bits);
  1004. }
  1005. break;
  1006. case CHR_IOCTL_SERIAL_SET_BREAK:
  1007. {
  1008. int enable = *(int *)arg;
  1009. if (enable)
  1010. tcsendbreak(s->fd_in, 1);
  1011. }
  1012. break;
  1013. case CHR_IOCTL_SERIAL_GET_TIOCM:
  1014. {
  1015. int sarg = 0;
  1016. int *targ = (int *)arg;
  1017. ioctl(s->fd_in, TIOCMGET, &sarg);
  1018. *targ = 0;
  1019. if (sarg & TIOCM_CTS)
  1020. *targ |= CHR_TIOCM_CTS;
  1021. if (sarg & TIOCM_CAR)
  1022. *targ |= CHR_TIOCM_CAR;
  1023. if (sarg & TIOCM_DSR)
  1024. *targ |= CHR_TIOCM_DSR;
  1025. if (sarg & TIOCM_RI)
  1026. *targ |= CHR_TIOCM_RI;
  1027. if (sarg & TIOCM_DTR)
  1028. *targ |= CHR_TIOCM_DTR;
  1029. if (sarg & TIOCM_RTS)
  1030. *targ |= CHR_TIOCM_RTS;
  1031. }
  1032. break;
  1033. case CHR_IOCTL_SERIAL_SET_TIOCM:
  1034. {
  1035. int sarg = *(int *)arg;
  1036. int targ = 0;
  1037. ioctl(s->fd_in, TIOCMGET, &targ);
  1038. targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
  1039. | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
  1040. if (sarg & CHR_TIOCM_CTS)
  1041. targ |= TIOCM_CTS;
  1042. if (sarg & CHR_TIOCM_CAR)
  1043. targ |= TIOCM_CAR;
  1044. if (sarg & CHR_TIOCM_DSR)
  1045. targ |= TIOCM_DSR;
  1046. if (sarg & CHR_TIOCM_RI)
  1047. targ |= TIOCM_RI;
  1048. if (sarg & CHR_TIOCM_DTR)
  1049. targ |= TIOCM_DTR;
  1050. if (sarg & CHR_TIOCM_RTS)
  1051. targ |= TIOCM_RTS;
  1052. ioctl(s->fd_in, TIOCMSET, &targ);
  1053. }
  1054. break;
  1055. default:
  1056. return -ENOTSUP;
  1057. }
  1058. return 0;
  1059. }
  1060. static void qemu_chr_close_tty(CharDriverState *chr)
  1061. {
  1062. FDCharDriver *s = chr->opaque;
  1063. int fd = -1;
  1064. if (s) {
  1065. fd = s->fd_in;
  1066. }
  1067. fd_chr_close(chr);
  1068. if (fd >= 0) {
  1069. close(fd);
  1070. }
  1071. }
  1072. static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
  1073. {
  1074. const char *filename = qemu_opt_get(opts, "path");
  1075. CharDriverState *chr;
  1076. int fd;
  1077. TFR(fd = qemu_open(filename, O_RDWR | O_NONBLOCK));
  1078. if (fd < 0) {
  1079. return NULL;
  1080. }
  1081. tty_serial_init(fd, 115200, 'N', 8, 1);
  1082. chr = qemu_chr_open_fd(fd, fd);
  1083. chr->chr_ioctl = tty_serial_ioctl;
  1084. chr->chr_close = qemu_chr_close_tty;
  1085. return chr;
  1086. }
  1087. #else /* ! __linux__ && ! __sun__ */
  1088. static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
  1089. {
  1090. return NULL;
  1091. }
  1092. #endif /* __linux__ || __sun__ */
  1093. #if defined(__linux__)
  1094. typedef struct {
  1095. int fd;
  1096. int mode;
  1097. } ParallelCharDriver;
  1098. static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
  1099. {
  1100. if (s->mode != mode) {
  1101. int m = mode;
  1102. if (ioctl(s->fd, PPSETMODE, &m) < 0)
  1103. return 0;
  1104. s->mode = mode;
  1105. }
  1106. return 1;
  1107. }
  1108. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1109. {
  1110. ParallelCharDriver *drv = chr->opaque;
  1111. int fd = drv->fd;
  1112. uint8_t b;
  1113. switch(cmd) {
  1114. case CHR_IOCTL_PP_READ_DATA:
  1115. if (ioctl(fd, PPRDATA, &b) < 0)
  1116. return -ENOTSUP;
  1117. *(uint8_t *)arg = b;
  1118. break;
  1119. case CHR_IOCTL_PP_WRITE_DATA:
  1120. b = *(uint8_t *)arg;
  1121. if (ioctl(fd, PPWDATA, &b) < 0)
  1122. return -ENOTSUP;
  1123. break;
  1124. case CHR_IOCTL_PP_READ_CONTROL:
  1125. if (ioctl(fd, PPRCONTROL, &b) < 0)
  1126. return -ENOTSUP;
  1127. /* Linux gives only the lowest bits, and no way to know data
  1128. direction! For better compatibility set the fixed upper
  1129. bits. */
  1130. *(uint8_t *)arg = b | 0xc0;
  1131. break;
  1132. case CHR_IOCTL_PP_WRITE_CONTROL:
  1133. b = *(uint8_t *)arg;
  1134. if (ioctl(fd, PPWCONTROL, &b) < 0)
  1135. return -ENOTSUP;
  1136. break;
  1137. case CHR_IOCTL_PP_READ_STATUS:
  1138. if (ioctl(fd, PPRSTATUS, &b) < 0)
  1139. return -ENOTSUP;
  1140. *(uint8_t *)arg = b;
  1141. break;
  1142. case CHR_IOCTL_PP_DATA_DIR:
  1143. if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
  1144. return -ENOTSUP;
  1145. break;
  1146. case CHR_IOCTL_PP_EPP_READ_ADDR:
  1147. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1148. struct ParallelIOArg *parg = arg;
  1149. int n = read(fd, parg->buffer, parg->count);
  1150. if (n != parg->count) {
  1151. return -EIO;
  1152. }
  1153. }
  1154. break;
  1155. case CHR_IOCTL_PP_EPP_READ:
  1156. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1157. struct ParallelIOArg *parg = arg;
  1158. int n = read(fd, parg->buffer, parg->count);
  1159. if (n != parg->count) {
  1160. return -EIO;
  1161. }
  1162. }
  1163. break;
  1164. case CHR_IOCTL_PP_EPP_WRITE_ADDR:
  1165. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1166. struct ParallelIOArg *parg = arg;
  1167. int n = write(fd, parg->buffer, parg->count);
  1168. if (n != parg->count) {
  1169. return -EIO;
  1170. }
  1171. }
  1172. break;
  1173. case CHR_IOCTL_PP_EPP_WRITE:
  1174. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1175. struct ParallelIOArg *parg = arg;
  1176. int n = write(fd, parg->buffer, parg->count);
  1177. if (n != parg->count) {
  1178. return -EIO;
  1179. }
  1180. }
  1181. break;
  1182. default:
  1183. return -ENOTSUP;
  1184. }
  1185. return 0;
  1186. }
  1187. static void pp_close(CharDriverState *chr)
  1188. {
  1189. ParallelCharDriver *drv = chr->opaque;
  1190. int fd = drv->fd;
  1191. pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
  1192. ioctl(fd, PPRELEASE);
  1193. close(fd);
  1194. g_free(drv);
  1195. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1196. }
  1197. static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
  1198. {
  1199. const char *filename = qemu_opt_get(opts, "path");
  1200. CharDriverState *chr;
  1201. ParallelCharDriver *drv;
  1202. int fd;
  1203. TFR(fd = qemu_open(filename, O_RDWR));
  1204. if (fd < 0) {
  1205. return NULL;
  1206. }
  1207. if (ioctl(fd, PPCLAIM) < 0) {
  1208. close(fd);
  1209. return NULL;
  1210. }
  1211. drv = g_malloc0(sizeof(ParallelCharDriver));
  1212. drv->fd = fd;
  1213. drv->mode = IEEE1284_MODE_COMPAT;
  1214. chr = g_malloc0(sizeof(CharDriverState));
  1215. chr->chr_write = null_chr_write;
  1216. chr->chr_ioctl = pp_ioctl;
  1217. chr->chr_close = pp_close;
  1218. chr->opaque = drv;
  1219. qemu_chr_generic_open(chr);
  1220. return chr;
  1221. }
  1222. #endif /* __linux__ */
  1223. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  1224. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1225. {
  1226. int fd = (int)(intptr_t)chr->opaque;
  1227. uint8_t b;
  1228. switch(cmd) {
  1229. case CHR_IOCTL_PP_READ_DATA:
  1230. if (ioctl(fd, PPIGDATA, &b) < 0)
  1231. return -ENOTSUP;
  1232. *(uint8_t *)arg = b;
  1233. break;
  1234. case CHR_IOCTL_PP_WRITE_DATA:
  1235. b = *(uint8_t *)arg;
  1236. if (ioctl(fd, PPISDATA, &b) < 0)
  1237. return -ENOTSUP;
  1238. break;
  1239. case CHR_IOCTL_PP_READ_CONTROL:
  1240. if (ioctl(fd, PPIGCTRL, &b) < 0)
  1241. return -ENOTSUP;
  1242. *(uint8_t *)arg = b;
  1243. break;
  1244. case CHR_IOCTL_PP_WRITE_CONTROL:
  1245. b = *(uint8_t *)arg;
  1246. if (ioctl(fd, PPISCTRL, &b) < 0)
  1247. return -ENOTSUP;
  1248. break;
  1249. case CHR_IOCTL_PP_READ_STATUS:
  1250. if (ioctl(fd, PPIGSTATUS, &b) < 0)
  1251. return -ENOTSUP;
  1252. *(uint8_t *)arg = b;
  1253. break;
  1254. default:
  1255. return -ENOTSUP;
  1256. }
  1257. return 0;
  1258. }
  1259. static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
  1260. {
  1261. const char *filename = qemu_opt_get(opts, "path");
  1262. CharDriverState *chr;
  1263. int fd;
  1264. fd = qemu_open(filename, O_RDWR);
  1265. if (fd < 0) {
  1266. return NULL;
  1267. }
  1268. chr = g_malloc0(sizeof(CharDriverState));
  1269. chr->opaque = (void *)(intptr_t)fd;
  1270. chr->chr_write = null_chr_write;
  1271. chr->chr_ioctl = pp_ioctl;
  1272. return chr;
  1273. }
  1274. #endif
  1275. #else /* _WIN32 */
  1276. static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
  1277. typedef struct {
  1278. int max_size;
  1279. HANDLE hcom, hrecv, hsend;
  1280. OVERLAPPED orecv, osend;
  1281. BOOL fpipe;
  1282. DWORD len;
  1283. } WinCharState;
  1284. typedef struct {
  1285. HANDLE hStdIn;
  1286. HANDLE hInputReadyEvent;
  1287. HANDLE hInputDoneEvent;
  1288. HANDLE hInputThread;
  1289. uint8_t win_stdio_buf;
  1290. } WinStdioCharState;
  1291. #define NSENDBUF 2048
  1292. #define NRECVBUF 2048
  1293. #define MAXCONNECT 1
  1294. #define NTIMEOUT 5000
  1295. static int win_chr_poll(void *opaque);
  1296. static int win_chr_pipe_poll(void *opaque);
  1297. static void win_chr_close(CharDriverState *chr)
  1298. {
  1299. WinCharState *s = chr->opaque;
  1300. if (s->hsend) {
  1301. CloseHandle(s->hsend);
  1302. s->hsend = NULL;
  1303. }
  1304. if (s->hrecv) {
  1305. CloseHandle(s->hrecv);
  1306. s->hrecv = NULL;
  1307. }
  1308. if (s->hcom) {
  1309. CloseHandle(s->hcom);
  1310. s->hcom = NULL;
  1311. }
  1312. if (s->fpipe)
  1313. qemu_del_polling_cb(win_chr_pipe_poll, chr);
  1314. else
  1315. qemu_del_polling_cb(win_chr_poll, chr);
  1316. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1317. }
  1318. static int win_chr_init(CharDriverState *chr, const char *filename)
  1319. {
  1320. WinCharState *s = chr->opaque;
  1321. COMMCONFIG comcfg;
  1322. COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
  1323. COMSTAT comstat;
  1324. DWORD size;
  1325. DWORD err;
  1326. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1327. if (!s->hsend) {
  1328. fprintf(stderr, "Failed CreateEvent\n");
  1329. goto fail;
  1330. }
  1331. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1332. if (!s->hrecv) {
  1333. fprintf(stderr, "Failed CreateEvent\n");
  1334. goto fail;
  1335. }
  1336. s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
  1337. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  1338. if (s->hcom == INVALID_HANDLE_VALUE) {
  1339. fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
  1340. s->hcom = NULL;
  1341. goto fail;
  1342. }
  1343. if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
  1344. fprintf(stderr, "Failed SetupComm\n");
  1345. goto fail;
  1346. }
  1347. ZeroMemory(&comcfg, sizeof(COMMCONFIG));
  1348. size = sizeof(COMMCONFIG);
  1349. GetDefaultCommConfig(filename, &comcfg, &size);
  1350. comcfg.dcb.DCBlength = sizeof(DCB);
  1351. CommConfigDialog(filename, NULL, &comcfg);
  1352. if (!SetCommState(s->hcom, &comcfg.dcb)) {
  1353. fprintf(stderr, "Failed SetCommState\n");
  1354. goto fail;
  1355. }
  1356. if (!SetCommMask(s->hcom, EV_ERR)) {
  1357. fprintf(stderr, "Failed SetCommMask\n");
  1358. goto fail;
  1359. }
  1360. cto.ReadIntervalTimeout = MAXDWORD;
  1361. if (!SetCommTimeouts(s->hcom, &cto)) {
  1362. fprintf(stderr, "Failed SetCommTimeouts\n");
  1363. goto fail;
  1364. }
  1365. if (!ClearCommError(s->hcom, &err, &comstat)) {
  1366. fprintf(stderr, "Failed ClearCommError\n");
  1367. goto fail;
  1368. }
  1369. qemu_add_polling_cb(win_chr_poll, chr);
  1370. return 0;
  1371. fail:
  1372. win_chr_close(chr);
  1373. return -1;
  1374. }
  1375. static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
  1376. {
  1377. WinCharState *s = chr->opaque;
  1378. DWORD len, ret, size, err;
  1379. len = len1;
  1380. ZeroMemory(&s->osend, sizeof(s->osend));
  1381. s->osend.hEvent = s->hsend;
  1382. while (len > 0) {
  1383. if (s->hsend)
  1384. ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
  1385. else
  1386. ret = WriteFile(s->hcom, buf, len, &size, NULL);
  1387. if (!ret) {
  1388. err = GetLastError();
  1389. if (err == ERROR_IO_PENDING) {
  1390. ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
  1391. if (ret) {
  1392. buf += size;
  1393. len -= size;
  1394. } else {
  1395. break;
  1396. }
  1397. } else {
  1398. break;
  1399. }
  1400. } else {
  1401. buf += size;
  1402. len -= size;
  1403. }
  1404. }
  1405. return len1 - len;
  1406. }
  1407. static int win_chr_read_poll(CharDriverState *chr)
  1408. {
  1409. WinCharState *s = chr->opaque;
  1410. s->max_size = qemu_chr_be_can_write(chr);
  1411. return s->max_size;
  1412. }
  1413. static void win_chr_readfile(CharDriverState *chr)
  1414. {
  1415. WinCharState *s = chr->opaque;
  1416. int ret, err;
  1417. uint8_t buf[READ_BUF_LEN];
  1418. DWORD size;
  1419. ZeroMemory(&s->orecv, sizeof(s->orecv));
  1420. s->orecv.hEvent = s->hrecv;
  1421. ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
  1422. if (!ret) {
  1423. err = GetLastError();
  1424. if (err == ERROR_IO_PENDING) {
  1425. ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
  1426. }
  1427. }
  1428. if (size > 0) {
  1429. qemu_chr_be_write(chr, buf, size);
  1430. }
  1431. }
  1432. static void win_chr_read(CharDriverState *chr)
  1433. {
  1434. WinCharState *s = chr->opaque;
  1435. if (s->len > s->max_size)
  1436. s->len = s->max_size;
  1437. if (s->len == 0)
  1438. return;
  1439. win_chr_readfile(chr);
  1440. }
  1441. static int win_chr_poll(void *opaque)
  1442. {
  1443. CharDriverState *chr = opaque;
  1444. WinCharState *s = chr->opaque;
  1445. COMSTAT status;
  1446. DWORD comerr;
  1447. ClearCommError(s->hcom, &comerr, &status);
  1448. if (status.cbInQue > 0) {
  1449. s->len = status.cbInQue;
  1450. win_chr_read_poll(chr);
  1451. win_chr_read(chr);
  1452. return 1;
  1453. }
  1454. return 0;
  1455. }
  1456. static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
  1457. {
  1458. const char *filename = qemu_opt_get(opts, "path");
  1459. CharDriverState *chr;
  1460. WinCharState *s;
  1461. chr = g_malloc0(sizeof(CharDriverState));
  1462. s = g_malloc0(sizeof(WinCharState));
  1463. chr->opaque = s;
  1464. chr->chr_write = win_chr_write;
  1465. chr->chr_close = win_chr_close;
  1466. if (win_chr_init(chr, filename) < 0) {
  1467. g_free(s);
  1468. g_free(chr);
  1469. return NULL;
  1470. }
  1471. qemu_chr_generic_open(chr);
  1472. return chr;
  1473. }
  1474. static int win_chr_pipe_poll(void *opaque)
  1475. {
  1476. CharDriverState *chr = opaque;
  1477. WinCharState *s = chr->opaque;
  1478. DWORD size;
  1479. PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
  1480. if (size > 0) {
  1481. s->len = size;
  1482. win_chr_read_poll(chr);
  1483. win_chr_read(chr);
  1484. return 1;
  1485. }
  1486. return 0;
  1487. }
  1488. static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
  1489. {
  1490. WinCharState *s = chr->opaque;
  1491. OVERLAPPED ov;
  1492. int ret;
  1493. DWORD size;
  1494. char openname[256];
  1495. s->fpipe = TRUE;
  1496. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1497. if (!s->hsend) {
  1498. fprintf(stderr, "Failed CreateEvent\n");
  1499. goto fail;
  1500. }
  1501. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1502. if (!s->hrecv) {
  1503. fprintf(stderr, "Failed CreateEvent\n");
  1504. goto fail;
  1505. }
  1506. snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
  1507. s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  1508. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
  1509. PIPE_WAIT,
  1510. MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
  1511. if (s->hcom == INVALID_HANDLE_VALUE) {
  1512. fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
  1513. s->hcom = NULL;
  1514. goto fail;
  1515. }
  1516. ZeroMemory(&ov, sizeof(ov));
  1517. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  1518. ret = ConnectNamedPipe(s->hcom, &ov);
  1519. if (ret) {
  1520. fprintf(stderr, "Failed ConnectNamedPipe\n");
  1521. goto fail;
  1522. }
  1523. ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
  1524. if (!ret) {
  1525. fprintf(stderr, "Failed GetOverlappedResult\n");
  1526. if (ov.hEvent) {
  1527. CloseHandle(ov.hEvent);
  1528. ov.hEvent = NULL;
  1529. }
  1530. goto fail;
  1531. }
  1532. if (ov.hEvent) {
  1533. CloseHandle(ov.hEvent);
  1534. ov.hEvent = NULL;
  1535. }
  1536. qemu_add_polling_cb(win_chr_pipe_poll, chr);
  1537. return 0;
  1538. fail:
  1539. win_chr_close(chr);
  1540. return -1;
  1541. }
  1542. static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
  1543. {
  1544. const char *filename = qemu_opt_get(opts, "path");
  1545. CharDriverState *chr;
  1546. WinCharState *s;
  1547. chr = g_malloc0(sizeof(CharDriverState));
  1548. s = g_malloc0(sizeof(WinCharState));
  1549. chr->opaque = s;
  1550. chr->chr_write = win_chr_write;
  1551. chr->chr_close = win_chr_close;
  1552. if (win_chr_pipe_init(chr, filename) < 0) {
  1553. g_free(s);
  1554. g_free(chr);
  1555. return NULL;
  1556. }
  1557. qemu_chr_generic_open(chr);
  1558. return chr;
  1559. }
  1560. static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
  1561. {
  1562. CharDriverState *chr;
  1563. WinCharState *s;
  1564. chr = g_malloc0(sizeof(CharDriverState));
  1565. s = g_malloc0(sizeof(WinCharState));
  1566. s->hcom = fd_out;
  1567. chr->opaque = s;
  1568. chr->chr_write = win_chr_write;
  1569. qemu_chr_generic_open(chr);
  1570. return chr;
  1571. }
  1572. static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
  1573. {
  1574. return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
  1575. }
  1576. static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
  1577. {
  1578. const char *file_out = qemu_opt_get(opts, "path");
  1579. HANDLE fd_out;
  1580. fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  1581. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1582. if (fd_out == INVALID_HANDLE_VALUE) {
  1583. return NULL;
  1584. }
  1585. return qemu_chr_open_win_file(fd_out);
  1586. }
  1587. static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
  1588. {
  1589. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  1590. DWORD dwSize;
  1591. int len1;
  1592. len1 = len;
  1593. while (len1 > 0) {
  1594. if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
  1595. break;
  1596. }
  1597. buf += dwSize;
  1598. len1 -= dwSize;
  1599. }
  1600. return len - len1;
  1601. }
  1602. static void win_stdio_wait_func(void *opaque)
  1603. {
  1604. CharDriverState *chr = opaque;
  1605. WinStdioCharState *stdio = chr->opaque;
  1606. INPUT_RECORD buf[4];
  1607. int ret;
  1608. DWORD dwSize;
  1609. int i;
  1610. ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf),
  1611. &dwSize);
  1612. if (!ret) {
  1613. /* Avoid error storm */
  1614. qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
  1615. return;
  1616. }
  1617. for (i = 0; i < dwSize; i++) {
  1618. KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
  1619. if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
  1620. int j;
  1621. if (kev->uChar.AsciiChar != 0) {
  1622. for (j = 0; j < kev->wRepeatCount; j++) {
  1623. if (qemu_chr_be_can_write(chr)) {
  1624. uint8_t c = kev->uChar.AsciiChar;
  1625. qemu_chr_be_write(chr, &c, 1);
  1626. }
  1627. }
  1628. }
  1629. }
  1630. }
  1631. }
  1632. static DWORD WINAPI win_stdio_thread(LPVOID param)
  1633. {
  1634. CharDriverState *chr = param;
  1635. WinStdioCharState *stdio = chr->opaque;
  1636. int ret;
  1637. DWORD dwSize;
  1638. while (1) {
  1639. /* Wait for one byte */
  1640. ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
  1641. /* Exit in case of error, continue if nothing read */
  1642. if (!ret) {
  1643. break;
  1644. }
  1645. if (!dwSize) {
  1646. continue;
  1647. }
  1648. /* Some terminal emulator returns \r\n for Enter, just pass \n */
  1649. if (stdio->win_stdio_buf == '\r') {
  1650. continue;
  1651. }
  1652. /* Signal the main thread and wait until the byte was eaten */
  1653. if (!SetEvent(stdio->hInputReadyEvent)) {
  1654. break;
  1655. }
  1656. if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
  1657. != WAIT_OBJECT_0) {
  1658. break;
  1659. }
  1660. }
  1661. qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
  1662. return 0;
  1663. }
  1664. static void win_stdio_thread_wait_func(void *opaque)
  1665. {
  1666. CharDriverState *chr = opaque;
  1667. WinStdioCharState *stdio = chr->opaque;
  1668. if (qemu_chr_be_can_write(chr)) {
  1669. qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
  1670. }
  1671. SetEvent(stdio->hInputDoneEvent);
  1672. }
  1673. static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
  1674. {
  1675. WinStdioCharState *stdio = chr->opaque;
  1676. DWORD dwMode = 0;
  1677. GetConsoleMode(stdio->hStdIn, &dwMode);
  1678. if (echo) {
  1679. SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
  1680. } else {
  1681. SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
  1682. }
  1683. }
  1684. static void win_stdio_close(CharDriverState *chr)
  1685. {
  1686. WinStdioCharState *stdio = chr->opaque;
  1687. if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
  1688. CloseHandle(stdio->hInputReadyEvent);
  1689. }
  1690. if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
  1691. CloseHandle(stdio->hInputDoneEvent);
  1692. }
  1693. if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
  1694. TerminateThread(stdio->hInputThread, 0);
  1695. }
  1696. g_free(chr->opaque);
  1697. g_free(chr);
  1698. stdio_nb_clients--;
  1699. }
  1700. static CharDriverState *qemu_chr_open_win_stdio(QemuOpts *opts)
  1701. {
  1702. CharDriverState *chr;
  1703. WinStdioCharState *stdio;
  1704. DWORD dwMode;
  1705. int is_console = 0;
  1706. if (stdio_nb_clients >= STDIO_MAX_CLIENTS
  1707. || ((display_type != DT_NOGRAPHIC) && (stdio_nb_clients != 0))) {
  1708. return NULL;
  1709. }
  1710. chr = g_malloc0(sizeof(CharDriverState));
  1711. stdio = g_malloc0(sizeof(WinStdioCharState));
  1712. stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  1713. if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
  1714. fprintf(stderr, "cannot open stdio: invalid handle\n");
  1715. exit(1);
  1716. }
  1717. is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
  1718. chr->opaque = stdio;
  1719. chr->chr_write = win_stdio_write;
  1720. chr->chr_close = win_stdio_close;
  1721. if (stdio_nb_clients == 0) {
  1722. if (is_console) {
  1723. if (qemu_add_wait_object(stdio->hStdIn,
  1724. win_stdio_wait_func, chr)) {
  1725. fprintf(stderr, "qemu_add_wait_object: failed\n");
  1726. }
  1727. } else {
  1728. DWORD dwId;
  1729. stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1730. stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1731. stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
  1732. chr, 0, &dwId);
  1733. if (stdio->hInputThread == INVALID_HANDLE_VALUE
  1734. || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
  1735. || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
  1736. fprintf(stderr, "cannot create stdio thread or event\n");
  1737. exit(1);
  1738. }
  1739. if (qemu_add_wait_object(stdio->hInputReadyEvent,
  1740. win_stdio_thread_wait_func, chr)) {
  1741. fprintf(stderr, "qemu_add_wait_object: failed\n");
  1742. }
  1743. }
  1744. }
  1745. dwMode |= ENABLE_LINE_INPUT;
  1746. stdio_clients[stdio_nb_clients++] = chr;
  1747. if (stdio_nb_clients == 1 && is_console) {
  1748. /* set the terminal in raw mode */
  1749. /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
  1750. dwMode |= ENABLE_PROCESSED_INPUT;
  1751. }
  1752. SetConsoleMode(stdio->hStdIn, dwMode);
  1753. chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
  1754. qemu_chr_fe_set_echo(chr, false);
  1755. return chr;
  1756. }
  1757. #endif /* !_WIN32 */
  1758. /***********************************************************/
  1759. /* UDP Net console */
  1760. typedef struct {
  1761. int fd;
  1762. uint8_t buf[READ_BUF_LEN];
  1763. int bufcnt;
  1764. int bufptr;
  1765. int max_size;
  1766. } NetCharDriver;
  1767. static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1768. {
  1769. NetCharDriver *s = chr->opaque;
  1770. return send(s->fd, (const void *)buf, len, 0);
  1771. }
  1772. static int udp_chr_read_poll(void *opaque)
  1773. {
  1774. CharDriverState *chr = opaque;
  1775. NetCharDriver *s = chr->opaque;
  1776. s->max_size = qemu_chr_be_can_write(chr);
  1777. /* If there were any stray characters in the queue process them
  1778. * first
  1779. */
  1780. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1781. qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
  1782. s->bufptr++;
  1783. s->max_size = qemu_chr_be_can_write(chr);
  1784. }
  1785. return s->max_size;
  1786. }
  1787. static void udp_chr_read(void *opaque)
  1788. {
  1789. CharDriverState *chr = opaque;
  1790. NetCharDriver *s = chr->opaque;
  1791. if (s->max_size == 0)
  1792. return;
  1793. s->bufcnt = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
  1794. s->bufptr = s->bufcnt;
  1795. if (s->bufcnt <= 0)
  1796. return;
  1797. s->bufptr = 0;
  1798. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1799. qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
  1800. s->bufptr++;
  1801. s->max_size = qemu_chr_be_can_write(chr);
  1802. }
  1803. }
  1804. static void udp_chr_update_read_handler(CharDriverState *chr)
  1805. {
  1806. NetCharDriver *s = chr->opaque;
  1807. if (s->fd >= 0) {
  1808. qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
  1809. udp_chr_read, NULL, chr);
  1810. }
  1811. }
  1812. static void udp_chr_close(CharDriverState *chr)
  1813. {
  1814. NetCharDriver *s = chr->opaque;
  1815. if (s->fd >= 0) {
  1816. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  1817. closesocket(s->fd);
  1818. }
  1819. g_free(s);
  1820. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1821. }
  1822. static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
  1823. {
  1824. CharDriverState *chr = NULL;
  1825. NetCharDriver *s = NULL;
  1826. Error *local_err = NULL;
  1827. int fd = -1;
  1828. chr = g_malloc0(sizeof(CharDriverState));
  1829. s = g_malloc0(sizeof(NetCharDriver));
  1830. fd = inet_dgram_opts(opts, &local_err);
  1831. if (fd < 0) {
  1832. goto return_err;
  1833. }
  1834. s->fd = fd;
  1835. s->bufcnt = 0;
  1836. s->bufptr = 0;
  1837. chr->opaque = s;
  1838. chr->chr_write = udp_chr_write;
  1839. chr->chr_update_read_handler = udp_chr_update_read_handler;
  1840. chr->chr_close = udp_chr_close;
  1841. return chr;
  1842. return_err:
  1843. if (local_err) {
  1844. qerror_report_err(local_err);
  1845. error_free(local_err);
  1846. }
  1847. g_free(chr);
  1848. g_free(s);
  1849. if (fd >= 0) {
  1850. closesocket(fd);
  1851. }
  1852. return NULL;
  1853. }
  1854. /***********************************************************/
  1855. /* TCP Net console */
  1856. typedef struct {
  1857. int fd, listen_fd;
  1858. int connected;
  1859. int max_size;
  1860. int do_telnetopt;
  1861. int do_nodelay;
  1862. int is_unix;
  1863. int msgfd;
  1864. } TCPCharDriver;
  1865. static void tcp_chr_accept(void *opaque);
  1866. static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1867. {
  1868. TCPCharDriver *s = chr->opaque;
  1869. if (s->connected) {
  1870. return send_all(s->fd, buf, len);
  1871. } else {
  1872. /* XXX: indicate an error ? */
  1873. return len;
  1874. }
  1875. }
  1876. static int tcp_chr_read_poll(void *opaque)
  1877. {
  1878. CharDriverState *chr = opaque;
  1879. TCPCharDriver *s = chr->opaque;
  1880. if (!s->connected)
  1881. return 0;
  1882. s->max_size = qemu_chr_be_can_write(chr);
  1883. return s->max_size;
  1884. }
  1885. #define IAC 255
  1886. #define IAC_BREAK 243
  1887. static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
  1888. TCPCharDriver *s,
  1889. uint8_t *buf, int *size)
  1890. {
  1891. /* Handle any telnet client's basic IAC options to satisfy char by
  1892. * char mode with no echo. All IAC options will be removed from
  1893. * the buf and the do_telnetopt variable will be used to track the
  1894. * state of the width of the IAC information.
  1895. *
  1896. * IAC commands come in sets of 3 bytes with the exception of the
  1897. * "IAC BREAK" command and the double IAC.
  1898. */
  1899. int i;
  1900. int j = 0;
  1901. for (i = 0; i < *size; i++) {
  1902. if (s->do_telnetopt > 1) {
  1903. if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
  1904. /* Double IAC means send an IAC */
  1905. if (j != i)
  1906. buf[j] = buf[i];
  1907. j++;
  1908. s->do_telnetopt = 1;
  1909. } else {
  1910. if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
  1911. /* Handle IAC break commands by sending a serial break */
  1912. qemu_chr_be_event(chr, CHR_EVENT_BREAK);
  1913. s->do_telnetopt++;
  1914. }
  1915. s->do_telnetopt++;
  1916. }
  1917. if (s->do_telnetopt >= 4) {
  1918. s->do_telnetopt = 1;
  1919. }
  1920. } else {
  1921. if ((unsigned char)buf[i] == IAC) {
  1922. s->do_telnetopt = 2;
  1923. } else {
  1924. if (j != i)
  1925. buf[j] = buf[i];
  1926. j++;
  1927. }
  1928. }
  1929. }
  1930. *size = j;
  1931. }
  1932. static int tcp_get_msgfd(CharDriverState *chr)
  1933. {
  1934. TCPCharDriver *s = chr->opaque;
  1935. int fd = s->msgfd;
  1936. s->msgfd = -1;
  1937. return fd;
  1938. }
  1939. #ifndef _WIN32
  1940. static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
  1941. {
  1942. TCPCharDriver *s = chr->opaque;
  1943. struct cmsghdr *cmsg;
  1944. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  1945. int fd;
  1946. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
  1947. cmsg->cmsg_level != SOL_SOCKET ||
  1948. cmsg->cmsg_type != SCM_RIGHTS)
  1949. continue;
  1950. fd = *((int *)CMSG_DATA(cmsg));
  1951. if (fd < 0)
  1952. continue;
  1953. #ifndef MSG_CMSG_CLOEXEC
  1954. qemu_set_cloexec(fd);
  1955. #endif
  1956. if (s->msgfd != -1)
  1957. close(s->msgfd);
  1958. s->msgfd = fd;
  1959. }
  1960. }
  1961. static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
  1962. {
  1963. TCPCharDriver *s = chr->opaque;
  1964. struct msghdr msg = { NULL, };
  1965. struct iovec iov[1];
  1966. union {
  1967. struct cmsghdr cmsg;
  1968. char control[CMSG_SPACE(sizeof(int))];
  1969. } msg_control;
  1970. int flags = 0;
  1971. ssize_t ret;
  1972. iov[0].iov_base = buf;
  1973. iov[0].iov_len = len;
  1974. msg.msg_iov = iov;
  1975. msg.msg_iovlen = 1;
  1976. msg.msg_control = &msg_control;
  1977. msg.msg_controllen = sizeof(msg_control);
  1978. #ifdef MSG_CMSG_CLOEXEC
  1979. flags |= MSG_CMSG_CLOEXEC;
  1980. #endif
  1981. ret = recvmsg(s->fd, &msg, flags);
  1982. if (ret > 0 && s->is_unix) {
  1983. unix_process_msgfd(chr, &msg);
  1984. }
  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. if (s->fd >= 0) {
  2035. qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
  2036. tcp_chr_read, NULL, chr);
  2037. }
  2038. qemu_chr_generic_open(chr);
  2039. }
  2040. #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
  2041. static void tcp_chr_telnet_init(int fd)
  2042. {
  2043. char buf[3];
  2044. /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
  2045. IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
  2046. send(fd, (char *)buf, 3, 0);
  2047. IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
  2048. send(fd, (char *)buf, 3, 0);
  2049. IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
  2050. send(fd, (char *)buf, 3, 0);
  2051. IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
  2052. send(fd, (char *)buf, 3, 0);
  2053. }
  2054. static void socket_set_nodelay(int fd)
  2055. {
  2056. int val = 1;
  2057. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
  2058. }
  2059. static int tcp_chr_add_client(CharDriverState *chr, int fd)
  2060. {
  2061. TCPCharDriver *s = chr->opaque;
  2062. if (s->fd != -1)
  2063. return -1;
  2064. socket_set_nonblock(fd);
  2065. if (s->do_nodelay)
  2066. socket_set_nodelay(fd);
  2067. s->fd = fd;
  2068. qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
  2069. tcp_chr_connect(chr);
  2070. return 0;
  2071. }
  2072. static void tcp_chr_accept(void *opaque)
  2073. {
  2074. CharDriverState *chr = opaque;
  2075. TCPCharDriver *s = chr->opaque;
  2076. struct sockaddr_in saddr;
  2077. #ifndef _WIN32
  2078. struct sockaddr_un uaddr;
  2079. #endif
  2080. struct sockaddr *addr;
  2081. socklen_t len;
  2082. int fd;
  2083. for(;;) {
  2084. #ifndef _WIN32
  2085. if (s->is_unix) {
  2086. len = sizeof(uaddr);
  2087. addr = (struct sockaddr *)&uaddr;
  2088. } else
  2089. #endif
  2090. {
  2091. len = sizeof(saddr);
  2092. addr = (struct sockaddr *)&saddr;
  2093. }
  2094. fd = qemu_accept(s->listen_fd, addr, &len);
  2095. if (fd < 0 && errno != EINTR) {
  2096. return;
  2097. } else if (fd >= 0) {
  2098. if (s->do_telnetopt)
  2099. tcp_chr_telnet_init(fd);
  2100. break;
  2101. }
  2102. }
  2103. if (tcp_chr_add_client(chr, fd) < 0)
  2104. close(fd);
  2105. }
  2106. static void tcp_chr_close(CharDriverState *chr)
  2107. {
  2108. TCPCharDriver *s = chr->opaque;
  2109. if (s->fd >= 0) {
  2110. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  2111. closesocket(s->fd);
  2112. }
  2113. if (s->listen_fd >= 0) {
  2114. qemu_set_fd_handler2(s->listen_fd, NULL, NULL, NULL, NULL);
  2115. closesocket(s->listen_fd);
  2116. }
  2117. g_free(s);
  2118. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  2119. }
  2120. static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
  2121. {
  2122. CharDriverState *chr = NULL;
  2123. TCPCharDriver *s = NULL;
  2124. Error *local_err = NULL;
  2125. int fd = -1;
  2126. int is_listen;
  2127. int is_waitconnect;
  2128. int do_nodelay;
  2129. int is_unix;
  2130. int is_telnet;
  2131. is_listen = qemu_opt_get_bool(opts, "server", 0);
  2132. is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
  2133. is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
  2134. do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
  2135. is_unix = qemu_opt_get(opts, "path") != NULL;
  2136. if (!is_listen)
  2137. is_waitconnect = 0;
  2138. chr = g_malloc0(sizeof(CharDriverState));
  2139. s = g_malloc0(sizeof(TCPCharDriver));
  2140. if (is_unix) {
  2141. if (is_listen) {
  2142. fd = unix_listen_opts(opts, &local_err);
  2143. } else {
  2144. fd = unix_connect_opts(opts, &local_err, NULL, NULL);
  2145. }
  2146. } else {
  2147. if (is_listen) {
  2148. fd = inet_listen_opts(opts, 0, &local_err);
  2149. } else {
  2150. fd = inet_connect_opts(opts, &local_err, NULL, NULL);
  2151. }
  2152. }
  2153. if (fd < 0) {
  2154. goto fail;
  2155. }
  2156. if (!is_waitconnect)
  2157. socket_set_nonblock(fd);
  2158. s->connected = 0;
  2159. s->fd = -1;
  2160. s->listen_fd = -1;
  2161. s->msgfd = -1;
  2162. s->is_unix = is_unix;
  2163. s->do_nodelay = do_nodelay && !is_unix;
  2164. chr->opaque = s;
  2165. chr->chr_write = tcp_chr_write;
  2166. chr->chr_close = tcp_chr_close;
  2167. chr->get_msgfd = tcp_get_msgfd;
  2168. chr->chr_add_client = tcp_chr_add_client;
  2169. if (is_listen) {
  2170. s->listen_fd = fd;
  2171. qemu_set_fd_handler2(s->listen_fd, NULL, tcp_chr_accept, NULL, chr);
  2172. if (is_telnet)
  2173. s->do_telnetopt = 1;
  2174. } else {
  2175. s->connected = 1;
  2176. s->fd = fd;
  2177. socket_set_nodelay(fd);
  2178. tcp_chr_connect(chr);
  2179. }
  2180. /* for "info chardev" monitor command */
  2181. chr->filename = g_malloc(256);
  2182. if (is_unix) {
  2183. snprintf(chr->filename, 256, "unix:%s%s",
  2184. qemu_opt_get(opts, "path"),
  2185. qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
  2186. } else if (is_telnet) {
  2187. snprintf(chr->filename, 256, "telnet:%s:%s%s",
  2188. qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
  2189. qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
  2190. } else {
  2191. snprintf(chr->filename, 256, "tcp:%s:%s%s",
  2192. qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
  2193. qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
  2194. }
  2195. if (is_listen && is_waitconnect) {
  2196. printf("QEMU waiting for connection on: %s\n",
  2197. chr->filename);
  2198. tcp_chr_accept(chr);
  2199. socket_set_nonblock(s->listen_fd);
  2200. }
  2201. return chr;
  2202. fail:
  2203. if (local_err) {
  2204. qerror_report_err(local_err);
  2205. error_free(local_err);
  2206. }
  2207. if (fd >= 0) {
  2208. closesocket(fd);
  2209. }
  2210. g_free(s);
  2211. g_free(chr);
  2212. return NULL;
  2213. }
  2214. /***********************************************************/
  2215. /* Memory chardev */
  2216. typedef struct {
  2217. size_t outbuf_size;
  2218. size_t outbuf_capacity;
  2219. uint8_t *outbuf;
  2220. } MemoryDriver;
  2221. static int mem_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  2222. {
  2223. MemoryDriver *d = chr->opaque;
  2224. /* TODO: the QString implementation has the same code, we should
  2225. * introduce a generic way to do this in cutils.c */
  2226. if (d->outbuf_capacity < d->outbuf_size + len) {
  2227. /* grow outbuf */
  2228. d->outbuf_capacity += len;
  2229. d->outbuf_capacity *= 2;
  2230. d->outbuf = g_realloc(d->outbuf, d->outbuf_capacity);
  2231. }
  2232. memcpy(d->outbuf + d->outbuf_size, buf, len);
  2233. d->outbuf_size += len;
  2234. return len;
  2235. }
  2236. void qemu_chr_init_mem(CharDriverState *chr)
  2237. {
  2238. MemoryDriver *d;
  2239. d = g_malloc(sizeof(*d));
  2240. d->outbuf_size = 0;
  2241. d->outbuf_capacity = 4096;
  2242. d->outbuf = g_malloc0(d->outbuf_capacity);
  2243. memset(chr, 0, sizeof(*chr));
  2244. chr->opaque = d;
  2245. chr->chr_write = mem_chr_write;
  2246. }
  2247. QString *qemu_chr_mem_to_qs(CharDriverState *chr)
  2248. {
  2249. MemoryDriver *d = chr->opaque;
  2250. return qstring_from_substr((char *) d->outbuf, 0, d->outbuf_size - 1);
  2251. }
  2252. /* NOTE: this driver can not be closed with qemu_chr_delete()! */
  2253. void qemu_chr_close_mem(CharDriverState *chr)
  2254. {
  2255. MemoryDriver *d = chr->opaque;
  2256. g_free(d->outbuf);
  2257. g_free(chr->opaque);
  2258. chr->opaque = NULL;
  2259. chr->chr_write = NULL;
  2260. }
  2261. size_t qemu_chr_mem_osize(const CharDriverState *chr)
  2262. {
  2263. const MemoryDriver *d = chr->opaque;
  2264. return d->outbuf_size;
  2265. }
  2266. QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
  2267. {
  2268. char host[65], port[33], width[8], height[8];
  2269. int pos;
  2270. const char *p;
  2271. QemuOpts *opts;
  2272. Error *local_err = NULL;
  2273. opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
  2274. if (error_is_set(&local_err)) {
  2275. qerror_report_err(local_err);
  2276. error_free(local_err);
  2277. return NULL;
  2278. }
  2279. if (strstart(filename, "mon:", &p)) {
  2280. filename = p;
  2281. qemu_opt_set(opts, "mux", "on");
  2282. }
  2283. if (strcmp(filename, "null") == 0 ||
  2284. strcmp(filename, "pty") == 0 ||
  2285. strcmp(filename, "msmouse") == 0 ||
  2286. strcmp(filename, "braille") == 0 ||
  2287. strcmp(filename, "stdio") == 0) {
  2288. qemu_opt_set(opts, "backend", filename);
  2289. return opts;
  2290. }
  2291. if (strstart(filename, "vc", &p)) {
  2292. qemu_opt_set(opts, "backend", "vc");
  2293. if (*p == ':') {
  2294. if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
  2295. /* pixels */
  2296. qemu_opt_set(opts, "width", width);
  2297. qemu_opt_set(opts, "height", height);
  2298. } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
  2299. /* chars */
  2300. qemu_opt_set(opts, "cols", width);
  2301. qemu_opt_set(opts, "rows", height);
  2302. } else {
  2303. goto fail;
  2304. }
  2305. }
  2306. return opts;
  2307. }
  2308. if (strcmp(filename, "con:") == 0) {
  2309. qemu_opt_set(opts, "backend", "console");
  2310. return opts;
  2311. }
  2312. if (strstart(filename, "COM", NULL)) {
  2313. qemu_opt_set(opts, "backend", "serial");
  2314. qemu_opt_set(opts, "path", filename);
  2315. return opts;
  2316. }
  2317. if (strstart(filename, "file:", &p)) {
  2318. qemu_opt_set(opts, "backend", "file");
  2319. qemu_opt_set(opts, "path", p);
  2320. return opts;
  2321. }
  2322. if (strstart(filename, "pipe:", &p)) {
  2323. qemu_opt_set(opts, "backend", "pipe");
  2324. qemu_opt_set(opts, "path", p);
  2325. return opts;
  2326. }
  2327. if (strstart(filename, "tcp:", &p) ||
  2328. strstart(filename, "telnet:", &p)) {
  2329. if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
  2330. host[0] = 0;
  2331. if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
  2332. goto fail;
  2333. }
  2334. qemu_opt_set(opts, "backend", "socket");
  2335. qemu_opt_set(opts, "host", host);
  2336. qemu_opt_set(opts, "port", port);
  2337. if (p[pos] == ',') {
  2338. if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
  2339. goto fail;
  2340. }
  2341. if (strstart(filename, "telnet:", &p))
  2342. qemu_opt_set(opts, "telnet", "on");
  2343. return opts;
  2344. }
  2345. if (strstart(filename, "udp:", &p)) {
  2346. qemu_opt_set(opts, "backend", "udp");
  2347. if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
  2348. host[0] = 0;
  2349. if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
  2350. goto fail;
  2351. }
  2352. }
  2353. qemu_opt_set(opts, "host", host);
  2354. qemu_opt_set(opts, "port", port);
  2355. if (p[pos] == '@') {
  2356. p += pos + 1;
  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, "localaddr", host);
  2364. qemu_opt_set(opts, "localport", port);
  2365. }
  2366. return opts;
  2367. }
  2368. if (strstart(filename, "unix:", &p)) {
  2369. qemu_opt_set(opts, "backend", "socket");
  2370. if (qemu_opts_do_parse(opts, p, "path") != 0)
  2371. goto fail;
  2372. return opts;
  2373. }
  2374. if (strstart(filename, "/dev/parport", NULL) ||
  2375. strstart(filename, "/dev/ppi", NULL)) {
  2376. qemu_opt_set(opts, "backend", "parport");
  2377. qemu_opt_set(opts, "path", filename);
  2378. return opts;
  2379. }
  2380. if (strstart(filename, "/dev/", NULL)) {
  2381. qemu_opt_set(opts, "backend", "tty");
  2382. qemu_opt_set(opts, "path", filename);
  2383. return opts;
  2384. }
  2385. fail:
  2386. qemu_opts_del(opts);
  2387. return NULL;
  2388. }
  2389. static const struct {
  2390. const char *name;
  2391. CharDriverState *(*open)(QemuOpts *opts);
  2392. } backend_table[] = {
  2393. { .name = "null", .open = qemu_chr_open_null },
  2394. { .name = "socket", .open = qemu_chr_open_socket },
  2395. { .name = "udp", .open = qemu_chr_open_udp },
  2396. { .name = "msmouse", .open = qemu_chr_open_msmouse },
  2397. { .name = "vc", .open = text_console_init },
  2398. #ifdef _WIN32
  2399. { .name = "file", .open = qemu_chr_open_win_file_out },
  2400. { .name = "pipe", .open = qemu_chr_open_win_pipe },
  2401. { .name = "console", .open = qemu_chr_open_win_con },
  2402. { .name = "serial", .open = qemu_chr_open_win },
  2403. { .name = "stdio", .open = qemu_chr_open_win_stdio },
  2404. #else
  2405. { .name = "file", .open = qemu_chr_open_file_out },
  2406. { .name = "pipe", .open = qemu_chr_open_pipe },
  2407. { .name = "pty", .open = qemu_chr_open_pty },
  2408. { .name = "stdio", .open = qemu_chr_open_stdio },
  2409. #endif
  2410. #ifdef CONFIG_BRLAPI
  2411. { .name = "braille", .open = chr_baum_init },
  2412. #endif
  2413. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  2414. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  2415. || defined(__FreeBSD_kernel__)
  2416. { .name = "tty", .open = qemu_chr_open_tty },
  2417. #endif
  2418. #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
  2419. || defined(__FreeBSD_kernel__)
  2420. { .name = "parport", .open = qemu_chr_open_pp },
  2421. #endif
  2422. #ifdef CONFIG_SPICE
  2423. { .name = "spicevmc", .open = qemu_chr_open_spice },
  2424. #endif
  2425. };
  2426. CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
  2427. void (*init)(struct CharDriverState *s))
  2428. {
  2429. CharDriverState *chr;
  2430. int i;
  2431. if (qemu_opts_id(opts) == NULL) {
  2432. fprintf(stderr, "chardev: no id specified\n");
  2433. return NULL;
  2434. }
  2435. if (qemu_opt_get(opts, "backend") == NULL) {
  2436. fprintf(stderr, "chardev: \"%s\" missing backend\n",
  2437. qemu_opts_id(opts));
  2438. return NULL;
  2439. }
  2440. for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
  2441. if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
  2442. break;
  2443. }
  2444. if (i == ARRAY_SIZE(backend_table)) {
  2445. fprintf(stderr, "chardev: backend \"%s\" not found\n",
  2446. qemu_opt_get(opts, "backend"));
  2447. return NULL;
  2448. }
  2449. chr = backend_table[i].open(opts);
  2450. if (!chr) {
  2451. fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
  2452. qemu_opt_get(opts, "backend"));
  2453. return NULL;
  2454. }
  2455. if (!chr->filename)
  2456. chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
  2457. chr->init = init;
  2458. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  2459. if (qemu_opt_get_bool(opts, "mux", 0)) {
  2460. CharDriverState *base = chr;
  2461. int len = strlen(qemu_opts_id(opts)) + 6;
  2462. base->label = g_malloc(len);
  2463. snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
  2464. chr = qemu_chr_open_mux(base);
  2465. chr->filename = base->filename;
  2466. chr->avail_connections = MAX_MUX;
  2467. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  2468. } else {
  2469. chr->avail_connections = 1;
  2470. }
  2471. chr->label = g_strdup(qemu_opts_id(opts));
  2472. return chr;
  2473. }
  2474. CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
  2475. {
  2476. const char *p;
  2477. CharDriverState *chr;
  2478. QemuOpts *opts;
  2479. if (strstart(filename, "chardev:", &p)) {
  2480. return qemu_chr_find(p);
  2481. }
  2482. opts = qemu_chr_parse_compat(label, filename);
  2483. if (!opts)
  2484. return NULL;
  2485. chr = qemu_chr_new_from_opts(opts, init);
  2486. if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
  2487. monitor_init(chr, MONITOR_USE_READLINE);
  2488. }
  2489. qemu_opts_del(opts);
  2490. return chr;
  2491. }
  2492. void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
  2493. {
  2494. if (chr->chr_set_echo) {
  2495. chr->chr_set_echo(chr, echo);
  2496. }
  2497. }
  2498. void qemu_chr_fe_open(struct CharDriverState *chr)
  2499. {
  2500. if (chr->chr_guest_open) {
  2501. chr->chr_guest_open(chr);
  2502. }
  2503. }
  2504. void qemu_chr_fe_close(struct CharDriverState *chr)
  2505. {
  2506. if (chr->chr_guest_close) {
  2507. chr->chr_guest_close(chr);
  2508. }
  2509. }
  2510. void qemu_chr_delete(CharDriverState *chr)
  2511. {
  2512. QTAILQ_REMOVE(&chardevs, chr, next);
  2513. if (chr->chr_close)
  2514. chr->chr_close(chr);
  2515. g_free(chr->filename);
  2516. g_free(chr->label);
  2517. g_free(chr);
  2518. }
  2519. ChardevInfoList *qmp_query_chardev(Error **errp)
  2520. {
  2521. ChardevInfoList *chr_list = NULL;
  2522. CharDriverState *chr;
  2523. QTAILQ_FOREACH(chr, &chardevs, next) {
  2524. ChardevInfoList *info = g_malloc0(sizeof(*info));
  2525. info->value = g_malloc0(sizeof(*info->value));
  2526. info->value->label = g_strdup(chr->label);
  2527. info->value->filename = g_strdup(chr->filename);
  2528. info->next = chr_list;
  2529. chr_list = info;
  2530. }
  2531. return chr_list;
  2532. }
  2533. CharDriverState *qemu_chr_find(const char *name)
  2534. {
  2535. CharDriverState *chr;
  2536. QTAILQ_FOREACH(chr, &chardevs, next) {
  2537. if (strcmp(chr->label, name) != 0)
  2538. continue;
  2539. return chr;
  2540. }
  2541. return NULL;
  2542. }
  2543. /* Get a character (serial) device interface. */
  2544. CharDriverState *qemu_char_get_next_serial(void)
  2545. {
  2546. static int next_serial;
  2547. /* FIXME: This function needs to go away: use chardev properties! */
  2548. return serial_hds[next_serial++];
  2549. }