2
0

qemu-char.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "monitor/monitor.h"
  26. #include "ui/console.h"
  27. #include "sysemu/sysemu.h"
  28. #include "qemu/timer.h"
  29. #include "sysemu/char.h"
  30. #include "hw/usb.h"
  31. #include "qmp-commands.h"
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <time.h>
  35. #include <errno.h>
  36. #include <sys/time.h>
  37. #include <zlib.h>
  38. #ifndef _WIN32
  39. #include <sys/times.h>
  40. #include <sys/wait.h>
  41. #include <termios.h>
  42. #include <sys/mman.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/resource.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <net/if.h>
  48. #include <arpa/inet.h>
  49. #include <dirent.h>
  50. #include <netdb.h>
  51. #include <sys/select.h>
  52. #ifdef CONFIG_BSD
  53. #include <sys/stat.h>
  54. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  55. #include <dev/ppbus/ppi.h>
  56. #include <dev/ppbus/ppbconf.h>
  57. #elif defined(__DragonFly__)
  58. #include <dev/misc/ppi/ppi.h>
  59. #include <bus/ppbus/ppbconf.h>
  60. #endif
  61. #else
  62. #ifdef __linux__
  63. #include <linux/ppdev.h>
  64. #include <linux/parport.h>
  65. #endif
  66. #ifdef __sun__
  67. #include <sys/stat.h>
  68. #include <sys/ethernet.h>
  69. #include <sys/sockio.h>
  70. #include <netinet/arp.h>
  71. #include <netinet/in.h>
  72. #include <netinet/in_systm.h>
  73. #include <netinet/ip.h>
  74. #include <netinet/ip_icmp.h> // must come after ip.h
  75. #include <netinet/udp.h>
  76. #include <netinet/tcp.h>
  77. #endif
  78. #endif
  79. #endif
  80. #include "qemu/sockets.h"
  81. #include "ui/qemu-spice.h"
  82. #define READ_BUF_LEN 4096
  83. /***********************************************************/
  84. /* character device */
  85. static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
  86. QTAILQ_HEAD_INITIALIZER(chardevs);
  87. void qemu_chr_be_event(CharDriverState *s, int event)
  88. {
  89. /* Keep track if the char device is open */
  90. switch (event) {
  91. case CHR_EVENT_OPENED:
  92. s->be_open = 1;
  93. break;
  94. case CHR_EVENT_CLOSED:
  95. s->be_open = 0;
  96. break;
  97. }
  98. if (!s->chr_event)
  99. return;
  100. s->chr_event(s->handler_opaque, event);
  101. }
  102. void qemu_chr_be_generic_open(CharDriverState *s)
  103. {
  104. qemu_chr_be_event(s, CHR_EVENT_OPENED);
  105. }
  106. int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
  107. {
  108. return s->chr_write(s, buf, len);
  109. }
  110. int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
  111. {
  112. int offset = 0;
  113. int res;
  114. while (offset < len) {
  115. do {
  116. res = s->chr_write(s, buf + offset, len - offset);
  117. if (res == -1 && errno == EAGAIN) {
  118. g_usleep(100);
  119. }
  120. } while (res == -1 && errno == EAGAIN);
  121. if (res == 0) {
  122. break;
  123. }
  124. if (res < 0) {
  125. return res;
  126. }
  127. offset += res;
  128. }
  129. return offset;
  130. }
  131. int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
  132. {
  133. if (!s->chr_ioctl)
  134. return -ENOTSUP;
  135. return s->chr_ioctl(s, cmd, arg);
  136. }
  137. int qemu_chr_be_can_write(CharDriverState *s)
  138. {
  139. if (!s->chr_can_read)
  140. return 0;
  141. return s->chr_can_read(s->handler_opaque);
  142. }
  143. void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
  144. {
  145. if (s->chr_read) {
  146. s->chr_read(s->handler_opaque, buf, len);
  147. }
  148. }
  149. int qemu_chr_fe_get_msgfd(CharDriverState *s)
  150. {
  151. return s->get_msgfd ? s->get_msgfd(s) : -1;
  152. }
  153. int qemu_chr_add_client(CharDriverState *s, int fd)
  154. {
  155. return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
  156. }
  157. void qemu_chr_accept_input(CharDriverState *s)
  158. {
  159. if (s->chr_accept_input)
  160. s->chr_accept_input(s);
  161. qemu_notify_event();
  162. }
  163. void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
  164. {
  165. char buf[READ_BUF_LEN];
  166. va_list ap;
  167. va_start(ap, fmt);
  168. vsnprintf(buf, sizeof(buf), fmt, ap);
  169. qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
  170. va_end(ap);
  171. }
  172. static void remove_fd_in_watch(CharDriverState *chr);
  173. void qemu_chr_add_handlers(CharDriverState *s,
  174. IOCanReadHandler *fd_can_read,
  175. IOReadHandler *fd_read,
  176. IOEventHandler *fd_event,
  177. void *opaque)
  178. {
  179. int fe_open;
  180. if (!opaque && !fd_can_read && !fd_read && !fd_event) {
  181. fe_open = 0;
  182. remove_fd_in_watch(s);
  183. } else {
  184. fe_open = 1;
  185. }
  186. s->chr_can_read = fd_can_read;
  187. s->chr_read = fd_read;
  188. s->chr_event = fd_event;
  189. s->handler_opaque = opaque;
  190. if (fe_open && s->chr_update_read_handler)
  191. s->chr_update_read_handler(s);
  192. if (!s->explicit_fe_open) {
  193. qemu_chr_fe_set_open(s, fe_open);
  194. }
  195. /* We're connecting to an already opened device, so let's make sure we
  196. also get the open event */
  197. if (fe_open && s->be_open) {
  198. qemu_chr_be_generic_open(s);
  199. }
  200. }
  201. static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  202. {
  203. return len;
  204. }
  205. static CharDriverState *qemu_chr_open_null(void)
  206. {
  207. CharDriverState *chr;
  208. chr = g_malloc0(sizeof(CharDriverState));
  209. chr->chr_write = null_chr_write;
  210. chr->explicit_be_open = true;
  211. return chr;
  212. }
  213. /* MUX driver for serial I/O splitting */
  214. #define MAX_MUX 4
  215. #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
  216. #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
  217. typedef struct {
  218. IOCanReadHandler *chr_can_read[MAX_MUX];
  219. IOReadHandler *chr_read[MAX_MUX];
  220. IOEventHandler *chr_event[MAX_MUX];
  221. void *ext_opaque[MAX_MUX];
  222. CharDriverState *drv;
  223. int focus;
  224. int mux_cnt;
  225. int term_got_escape;
  226. int max_size;
  227. /* Intermediate input buffer allows to catch escape sequences even if the
  228. currently active device is not accepting any input - but only until it
  229. is full as well. */
  230. unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
  231. int prod[MAX_MUX];
  232. int cons[MAX_MUX];
  233. int timestamps;
  234. int linestart;
  235. int64_t timestamps_start;
  236. } MuxDriver;
  237. static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  238. {
  239. MuxDriver *d = chr->opaque;
  240. int ret;
  241. if (!d->timestamps) {
  242. ret = d->drv->chr_write(d->drv, buf, len);
  243. } else {
  244. int i;
  245. ret = 0;
  246. for (i = 0; i < len; i++) {
  247. if (d->linestart) {
  248. char buf1[64];
  249. int64_t ti;
  250. int secs;
  251. ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  252. if (d->timestamps_start == -1)
  253. d->timestamps_start = ti;
  254. ti -= d->timestamps_start;
  255. secs = ti / 1000;
  256. snprintf(buf1, sizeof(buf1),
  257. "[%02d:%02d:%02d.%03d] ",
  258. secs / 3600,
  259. (secs / 60) % 60,
  260. secs % 60,
  261. (int)(ti % 1000));
  262. d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
  263. d->linestart = 0;
  264. }
  265. ret += d->drv->chr_write(d->drv, buf+i, 1);
  266. if (buf[i] == '\n') {
  267. d->linestart = 1;
  268. }
  269. }
  270. }
  271. return ret;
  272. }
  273. static const char * const mux_help[] = {
  274. "% h print this help\n\r",
  275. "% x exit emulator\n\r",
  276. "% s save disk data back to file (if -snapshot)\n\r",
  277. "% t toggle console timestamps\n\r"
  278. "% b send break (magic sysrq)\n\r",
  279. "% c switch between console and monitor\n\r",
  280. "% % sends %\n\r",
  281. NULL
  282. };
  283. int term_escape_char = 0x01; /* ctrl-a is used for escape */
  284. static void mux_print_help(CharDriverState *chr)
  285. {
  286. int i, j;
  287. char ebuf[15] = "Escape-Char";
  288. char cbuf[50] = "\n\r";
  289. if (term_escape_char > 0 && term_escape_char < 26) {
  290. snprintf(cbuf, sizeof(cbuf), "\n\r");
  291. snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
  292. } else {
  293. snprintf(cbuf, sizeof(cbuf),
  294. "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
  295. term_escape_char);
  296. }
  297. chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
  298. for (i = 0; mux_help[i] != NULL; i++) {
  299. for (j=0; mux_help[i][j] != '\0'; j++) {
  300. if (mux_help[i][j] == '%')
  301. chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
  302. else
  303. chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
  304. }
  305. }
  306. }
  307. static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
  308. {
  309. if (d->chr_event[mux_nr])
  310. d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
  311. }
  312. static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
  313. {
  314. if (d->term_got_escape) {
  315. d->term_got_escape = 0;
  316. if (ch == term_escape_char)
  317. goto send_char;
  318. switch(ch) {
  319. case '?':
  320. case 'h':
  321. mux_print_help(chr);
  322. break;
  323. case 'x':
  324. {
  325. const char *term = "QEMU: Terminated\n\r";
  326. chr->chr_write(chr,(uint8_t *)term,strlen(term));
  327. exit(0);
  328. break;
  329. }
  330. case 's':
  331. bdrv_commit_all();
  332. break;
  333. case 'b':
  334. qemu_chr_be_event(chr, CHR_EVENT_BREAK);
  335. break;
  336. case 'c':
  337. /* Switch to the next registered device */
  338. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
  339. d->focus++;
  340. if (d->focus >= d->mux_cnt)
  341. d->focus = 0;
  342. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
  343. break;
  344. case 't':
  345. d->timestamps = !d->timestamps;
  346. d->timestamps_start = -1;
  347. d->linestart = 0;
  348. break;
  349. }
  350. } else if (ch == term_escape_char) {
  351. d->term_got_escape = 1;
  352. } else {
  353. send_char:
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. static void mux_chr_accept_input(CharDriverState *chr)
  359. {
  360. MuxDriver *d = chr->opaque;
  361. int m = d->focus;
  362. while (d->prod[m] != d->cons[m] &&
  363. d->chr_can_read[m] &&
  364. d->chr_can_read[m](d->ext_opaque[m])) {
  365. d->chr_read[m](d->ext_opaque[m],
  366. &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
  367. }
  368. }
  369. static int mux_chr_can_read(void *opaque)
  370. {
  371. CharDriverState *chr = opaque;
  372. MuxDriver *d = chr->opaque;
  373. int m = d->focus;
  374. if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
  375. return 1;
  376. if (d->chr_can_read[m])
  377. return d->chr_can_read[m](d->ext_opaque[m]);
  378. return 0;
  379. }
  380. static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
  381. {
  382. CharDriverState *chr = opaque;
  383. MuxDriver *d = chr->opaque;
  384. int m = d->focus;
  385. int i;
  386. mux_chr_accept_input (opaque);
  387. for(i = 0; i < size; i++)
  388. if (mux_proc_byte(chr, d, buf[i])) {
  389. if (d->prod[m] == d->cons[m] &&
  390. d->chr_can_read[m] &&
  391. d->chr_can_read[m](d->ext_opaque[m]))
  392. d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
  393. else
  394. d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
  395. }
  396. }
  397. static void mux_chr_event(void *opaque, int event)
  398. {
  399. CharDriverState *chr = opaque;
  400. MuxDriver *d = chr->opaque;
  401. int i;
  402. /* Send the event to all registered listeners */
  403. for (i = 0; i < d->mux_cnt; i++)
  404. mux_chr_send_event(d, i, event);
  405. }
  406. static void mux_chr_update_read_handler(CharDriverState *chr)
  407. {
  408. MuxDriver *d = chr->opaque;
  409. if (d->mux_cnt >= MAX_MUX) {
  410. fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
  411. return;
  412. }
  413. d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
  414. d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
  415. d->chr_read[d->mux_cnt] = chr->chr_read;
  416. d->chr_event[d->mux_cnt] = chr->chr_event;
  417. /* Fix up the real driver with mux routines */
  418. if (d->mux_cnt == 0) {
  419. qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
  420. mux_chr_event, chr);
  421. }
  422. if (d->focus != -1) {
  423. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
  424. }
  425. d->focus = d->mux_cnt;
  426. d->mux_cnt++;
  427. mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
  428. }
  429. static bool muxes_realized;
  430. /**
  431. * Called after processing of default and command-line-specified
  432. * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
  433. * to a mux chardev. This is done here to ensure that
  434. * output/prompts/banners are only displayed for the FE that has
  435. * focus when initial command-line processing/machine init is
  436. * completed.
  437. *
  438. * After this point, any new FE attached to any new or existing
  439. * mux will receive CHR_EVENT_OPENED notifications for the BE
  440. * immediately.
  441. */
  442. static void muxes_realize_done(Notifier *notifier, void *unused)
  443. {
  444. CharDriverState *chr;
  445. QTAILQ_FOREACH(chr, &chardevs, next) {
  446. if (chr->is_mux) {
  447. MuxDriver *d = chr->opaque;
  448. int i;
  449. /* send OPENED to all already-attached FEs */
  450. for (i = 0; i < d->mux_cnt; i++) {
  451. mux_chr_send_event(d, i, CHR_EVENT_OPENED);
  452. }
  453. /* mark mux as OPENED so any new FEs will immediately receive
  454. * OPENED event
  455. */
  456. qemu_chr_be_generic_open(chr);
  457. }
  458. }
  459. muxes_realized = true;
  460. }
  461. static Notifier muxes_realize_notify = {
  462. .notify = muxes_realize_done,
  463. };
  464. static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
  465. {
  466. CharDriverState *chr;
  467. MuxDriver *d;
  468. chr = g_malloc0(sizeof(CharDriverState));
  469. d = g_malloc0(sizeof(MuxDriver));
  470. chr->opaque = d;
  471. d->drv = drv;
  472. d->focus = -1;
  473. chr->chr_write = mux_chr_write;
  474. chr->chr_update_read_handler = mux_chr_update_read_handler;
  475. chr->chr_accept_input = mux_chr_accept_input;
  476. /* Frontend guest-open / -close notification is not support with muxes */
  477. chr->chr_set_fe_open = NULL;
  478. /* only default to opened state if we've realized the initial
  479. * set of muxes
  480. */
  481. chr->explicit_be_open = muxes_realized ? 0 : 1;
  482. chr->is_mux = 1;
  483. return chr;
  484. }
  485. #ifdef _WIN32
  486. int send_all(int fd, const void *buf, int len1)
  487. {
  488. int ret, len;
  489. len = len1;
  490. while (len > 0) {
  491. ret = send(fd, buf, len, 0);
  492. if (ret < 0) {
  493. errno = WSAGetLastError();
  494. if (errno != WSAEWOULDBLOCK) {
  495. return -1;
  496. }
  497. } else if (ret == 0) {
  498. break;
  499. } else {
  500. buf += ret;
  501. len -= ret;
  502. }
  503. }
  504. return len1 - len;
  505. }
  506. #else
  507. int send_all(int fd, const void *_buf, int len1)
  508. {
  509. int ret, len;
  510. const uint8_t *buf = _buf;
  511. len = len1;
  512. while (len > 0) {
  513. ret = write(fd, buf, len);
  514. if (ret < 0) {
  515. if (errno != EINTR && errno != EAGAIN)
  516. return -1;
  517. } else if (ret == 0) {
  518. break;
  519. } else {
  520. buf += ret;
  521. len -= ret;
  522. }
  523. }
  524. return len1 - len;
  525. }
  526. int recv_all(int fd, void *_buf, int len1, bool single_read)
  527. {
  528. int ret, len;
  529. uint8_t *buf = _buf;
  530. len = len1;
  531. while ((len > 0) && (ret = read(fd, buf, len)) != 0) {
  532. if (ret < 0) {
  533. if (errno != EINTR && errno != EAGAIN) {
  534. return -1;
  535. }
  536. continue;
  537. } else {
  538. if (single_read) {
  539. return ret;
  540. }
  541. buf += ret;
  542. len -= ret;
  543. }
  544. }
  545. return len1 - len;
  546. }
  547. #endif /* !_WIN32 */
  548. typedef struct IOWatchPoll
  549. {
  550. GSource parent;
  551. GIOChannel *channel;
  552. GSource *src;
  553. IOCanReadHandler *fd_can_read;
  554. GSourceFunc fd_read;
  555. void *opaque;
  556. } IOWatchPoll;
  557. static IOWatchPoll *io_watch_poll_from_source(GSource *source)
  558. {
  559. return container_of(source, IOWatchPoll, parent);
  560. }
  561. static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
  562. {
  563. IOWatchPoll *iwp = io_watch_poll_from_source(source);
  564. bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
  565. bool was_active = iwp->src != NULL;
  566. if (was_active == now_active) {
  567. return FALSE;
  568. }
  569. if (now_active) {
  570. iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
  571. g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
  572. g_source_attach(iwp->src, NULL);
  573. } else {
  574. g_source_destroy(iwp->src);
  575. g_source_unref(iwp->src);
  576. iwp->src = NULL;
  577. }
  578. return FALSE;
  579. }
  580. static gboolean io_watch_poll_check(GSource *source)
  581. {
  582. return FALSE;
  583. }
  584. static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
  585. gpointer user_data)
  586. {
  587. abort();
  588. }
  589. static void io_watch_poll_finalize(GSource *source)
  590. {
  591. /* Due to a glib bug, removing the last reference to a source
  592. * inside a finalize callback causes recursive locking (and a
  593. * deadlock). This is not a problem inside other callbacks,
  594. * including dispatch callbacks, so we call io_remove_watch_poll
  595. * to remove this source. At this point, iwp->src must
  596. * be NULL, or we would leak it.
  597. *
  598. * This would be solved much more elegantly by child sources,
  599. * but we support older glib versions that do not have them.
  600. */
  601. IOWatchPoll *iwp = io_watch_poll_from_source(source);
  602. assert(iwp->src == NULL);
  603. }
  604. static GSourceFuncs io_watch_poll_funcs = {
  605. .prepare = io_watch_poll_prepare,
  606. .check = io_watch_poll_check,
  607. .dispatch = io_watch_poll_dispatch,
  608. .finalize = io_watch_poll_finalize,
  609. };
  610. /* Can only be used for read */
  611. static guint io_add_watch_poll(GIOChannel *channel,
  612. IOCanReadHandler *fd_can_read,
  613. GIOFunc fd_read,
  614. gpointer user_data)
  615. {
  616. IOWatchPoll *iwp;
  617. int tag;
  618. iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll));
  619. iwp->fd_can_read = fd_can_read;
  620. iwp->opaque = user_data;
  621. iwp->channel = channel;
  622. iwp->fd_read = (GSourceFunc) fd_read;
  623. iwp->src = NULL;
  624. tag = g_source_attach(&iwp->parent, NULL);
  625. g_source_unref(&iwp->parent);
  626. return tag;
  627. }
  628. static void io_remove_watch_poll(guint tag)
  629. {
  630. GSource *source;
  631. IOWatchPoll *iwp;
  632. g_return_if_fail (tag > 0);
  633. source = g_main_context_find_source_by_id(NULL, tag);
  634. g_return_if_fail (source != NULL);
  635. iwp = io_watch_poll_from_source(source);
  636. if (iwp->src) {
  637. g_source_destroy(iwp->src);
  638. g_source_unref(iwp->src);
  639. iwp->src = NULL;
  640. }
  641. g_source_destroy(&iwp->parent);
  642. }
  643. static void remove_fd_in_watch(CharDriverState *chr)
  644. {
  645. if (chr->fd_in_tag) {
  646. io_remove_watch_poll(chr->fd_in_tag);
  647. chr->fd_in_tag = 0;
  648. }
  649. }
  650. #ifndef _WIN32
  651. static GIOChannel *io_channel_from_fd(int fd)
  652. {
  653. GIOChannel *chan;
  654. if (fd == -1) {
  655. return NULL;
  656. }
  657. chan = g_io_channel_unix_new(fd);
  658. g_io_channel_set_encoding(chan, NULL, NULL);
  659. g_io_channel_set_buffered(chan, FALSE);
  660. return chan;
  661. }
  662. #endif
  663. static GIOChannel *io_channel_from_socket(int fd)
  664. {
  665. GIOChannel *chan;
  666. if (fd == -1) {
  667. return NULL;
  668. }
  669. #ifdef _WIN32
  670. chan = g_io_channel_win32_new_socket(fd);
  671. #else
  672. chan = g_io_channel_unix_new(fd);
  673. #endif
  674. g_io_channel_set_encoding(chan, NULL, NULL);
  675. g_io_channel_set_buffered(chan, FALSE);
  676. return chan;
  677. }
  678. static int io_channel_send(GIOChannel *fd, const void *buf, size_t len)
  679. {
  680. size_t offset = 0;
  681. GIOStatus status = G_IO_STATUS_NORMAL;
  682. while (offset < len && status == G_IO_STATUS_NORMAL) {
  683. gsize bytes_written = 0;
  684. status = g_io_channel_write_chars(fd, buf + offset, len - offset,
  685. &bytes_written, NULL);
  686. offset += bytes_written;
  687. }
  688. if (offset > 0) {
  689. return offset;
  690. }
  691. switch (status) {
  692. case G_IO_STATUS_NORMAL:
  693. g_assert(len == 0);
  694. return 0;
  695. case G_IO_STATUS_AGAIN:
  696. errno = EAGAIN;
  697. return -1;
  698. default:
  699. break;
  700. }
  701. errno = EINVAL;
  702. return -1;
  703. }
  704. #ifndef _WIN32
  705. typedef struct FDCharDriver {
  706. CharDriverState *chr;
  707. GIOChannel *fd_in, *fd_out;
  708. int max_size;
  709. QTAILQ_ENTRY(FDCharDriver) node;
  710. } FDCharDriver;
  711. static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  712. {
  713. FDCharDriver *s = chr->opaque;
  714. return io_channel_send(s->fd_out, buf, len);
  715. }
  716. static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
  717. {
  718. CharDriverState *chr = opaque;
  719. FDCharDriver *s = chr->opaque;
  720. int len;
  721. uint8_t buf[READ_BUF_LEN];
  722. GIOStatus status;
  723. gsize bytes_read;
  724. len = sizeof(buf);
  725. if (len > s->max_size) {
  726. len = s->max_size;
  727. }
  728. if (len == 0) {
  729. return TRUE;
  730. }
  731. status = g_io_channel_read_chars(chan, (gchar *)buf,
  732. len, &bytes_read, NULL);
  733. if (status == G_IO_STATUS_EOF) {
  734. remove_fd_in_watch(chr);
  735. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  736. return FALSE;
  737. }
  738. if (status == G_IO_STATUS_NORMAL) {
  739. qemu_chr_be_write(chr, buf, bytes_read);
  740. }
  741. return TRUE;
  742. }
  743. static int fd_chr_read_poll(void *opaque)
  744. {
  745. CharDriverState *chr = opaque;
  746. FDCharDriver *s = chr->opaque;
  747. s->max_size = qemu_chr_be_can_write(chr);
  748. return s->max_size;
  749. }
  750. static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
  751. {
  752. FDCharDriver *s = chr->opaque;
  753. return g_io_create_watch(s->fd_out, cond);
  754. }
  755. static void fd_chr_update_read_handler(CharDriverState *chr)
  756. {
  757. FDCharDriver *s = chr->opaque;
  758. remove_fd_in_watch(chr);
  759. if (s->fd_in) {
  760. chr->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll,
  761. fd_chr_read, chr);
  762. }
  763. }
  764. static void fd_chr_close(struct CharDriverState *chr)
  765. {
  766. FDCharDriver *s = chr->opaque;
  767. remove_fd_in_watch(chr);
  768. if (s->fd_in) {
  769. g_io_channel_unref(s->fd_in);
  770. }
  771. if (s->fd_out) {
  772. g_io_channel_unref(s->fd_out);
  773. }
  774. g_free(s);
  775. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  776. }
  777. /* open a character device to a unix fd */
  778. static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
  779. {
  780. CharDriverState *chr;
  781. FDCharDriver *s;
  782. chr = g_malloc0(sizeof(CharDriverState));
  783. s = g_malloc0(sizeof(FDCharDriver));
  784. s->fd_in = io_channel_from_fd(fd_in);
  785. s->fd_out = io_channel_from_fd(fd_out);
  786. fcntl(fd_out, F_SETFL, O_NONBLOCK);
  787. s->chr = chr;
  788. chr->opaque = s;
  789. chr->chr_add_watch = fd_chr_add_watch;
  790. chr->chr_write = fd_chr_write;
  791. chr->chr_update_read_handler = fd_chr_update_read_handler;
  792. chr->chr_close = fd_chr_close;
  793. return chr;
  794. }
  795. static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
  796. {
  797. int fd_in, fd_out;
  798. char filename_in[256], filename_out[256];
  799. const char *filename = opts->device;
  800. if (filename == NULL) {
  801. fprintf(stderr, "chardev: pipe: no filename given\n");
  802. return NULL;
  803. }
  804. snprintf(filename_in, 256, "%s.in", filename);
  805. snprintf(filename_out, 256, "%s.out", filename);
  806. TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
  807. TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
  808. if (fd_in < 0 || fd_out < 0) {
  809. if (fd_in >= 0)
  810. close(fd_in);
  811. if (fd_out >= 0)
  812. close(fd_out);
  813. TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
  814. if (fd_in < 0) {
  815. return NULL;
  816. }
  817. }
  818. return qemu_chr_open_fd(fd_in, fd_out);
  819. }
  820. /* init terminal so that we can grab keys */
  821. static struct termios oldtty;
  822. static int old_fd0_flags;
  823. static bool stdio_allow_signal;
  824. static void term_exit(void)
  825. {
  826. tcsetattr (0, TCSANOW, &oldtty);
  827. fcntl(0, F_SETFL, old_fd0_flags);
  828. }
  829. static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
  830. {
  831. struct termios tty;
  832. tty = oldtty;
  833. if (!echo) {
  834. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  835. |INLCR|IGNCR|ICRNL|IXON);
  836. tty.c_oflag |= OPOST;
  837. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  838. tty.c_cflag &= ~(CSIZE|PARENB);
  839. tty.c_cflag |= CS8;
  840. tty.c_cc[VMIN] = 1;
  841. tty.c_cc[VTIME] = 0;
  842. }
  843. if (!stdio_allow_signal)
  844. tty.c_lflag &= ~ISIG;
  845. tcsetattr (0, TCSANOW, &tty);
  846. }
  847. static void qemu_chr_close_stdio(struct CharDriverState *chr)
  848. {
  849. term_exit();
  850. fd_chr_close(chr);
  851. }
  852. static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
  853. {
  854. CharDriverState *chr;
  855. if (is_daemonized()) {
  856. error_report("cannot use stdio with -daemonize");
  857. return NULL;
  858. }
  859. old_fd0_flags = fcntl(0, F_GETFL);
  860. tcgetattr (0, &oldtty);
  861. fcntl(0, F_SETFL, O_NONBLOCK);
  862. atexit(term_exit);
  863. chr = qemu_chr_open_fd(0, 1);
  864. chr->chr_close = qemu_chr_close_stdio;
  865. chr->chr_set_echo = qemu_chr_set_echo_stdio;
  866. if (opts->has_signal) {
  867. stdio_allow_signal = opts->signal;
  868. }
  869. qemu_chr_fe_set_echo(chr, false);
  870. return chr;
  871. }
  872. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  873. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  874. || defined(__GLIBC__)
  875. #define HAVE_CHARDEV_TTY 1
  876. typedef struct {
  877. GIOChannel *fd;
  878. int connected;
  879. int read_bytes;
  880. guint timer_tag;
  881. } PtyCharDriver;
  882. static void pty_chr_update_read_handler(CharDriverState *chr);
  883. static void pty_chr_state(CharDriverState *chr, int connected);
  884. static gboolean pty_chr_timer(gpointer opaque)
  885. {
  886. struct CharDriverState *chr = opaque;
  887. PtyCharDriver *s = chr->opaque;
  888. s->timer_tag = 0;
  889. if (!s->connected) {
  890. /* Next poll ... */
  891. pty_chr_update_read_handler(chr);
  892. }
  893. return FALSE;
  894. }
  895. static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
  896. {
  897. PtyCharDriver *s = chr->opaque;
  898. if (s->timer_tag) {
  899. g_source_remove(s->timer_tag);
  900. s->timer_tag = 0;
  901. }
  902. if (ms == 1000) {
  903. s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
  904. } else {
  905. s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
  906. }
  907. }
  908. static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  909. {
  910. PtyCharDriver *s = chr->opaque;
  911. if (!s->connected) {
  912. /* guest sends data, check for (re-)connect */
  913. pty_chr_update_read_handler(chr);
  914. return 0;
  915. }
  916. return io_channel_send(s->fd, buf, len);
  917. }
  918. static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
  919. {
  920. PtyCharDriver *s = chr->opaque;
  921. return g_io_create_watch(s->fd, cond);
  922. }
  923. static int pty_chr_read_poll(void *opaque)
  924. {
  925. CharDriverState *chr = opaque;
  926. PtyCharDriver *s = chr->opaque;
  927. s->read_bytes = qemu_chr_be_can_write(chr);
  928. return s->read_bytes;
  929. }
  930. static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
  931. {
  932. CharDriverState *chr = opaque;
  933. PtyCharDriver *s = chr->opaque;
  934. gsize size, len;
  935. uint8_t buf[READ_BUF_LEN];
  936. GIOStatus status;
  937. len = sizeof(buf);
  938. if (len > s->read_bytes)
  939. len = s->read_bytes;
  940. if (len == 0) {
  941. return TRUE;
  942. }
  943. status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
  944. if (status != G_IO_STATUS_NORMAL) {
  945. pty_chr_state(chr, 0);
  946. return FALSE;
  947. } else {
  948. pty_chr_state(chr, 1);
  949. qemu_chr_be_write(chr, buf, size);
  950. }
  951. return TRUE;
  952. }
  953. static void pty_chr_update_read_handler(CharDriverState *chr)
  954. {
  955. PtyCharDriver *s = chr->opaque;
  956. GPollFD pfd;
  957. pfd.fd = g_io_channel_unix_get_fd(s->fd);
  958. pfd.events = G_IO_OUT;
  959. pfd.revents = 0;
  960. g_poll(&pfd, 1, 0);
  961. if (pfd.revents & G_IO_HUP) {
  962. pty_chr_state(chr, 0);
  963. } else {
  964. pty_chr_state(chr, 1);
  965. }
  966. }
  967. static void pty_chr_state(CharDriverState *chr, int connected)
  968. {
  969. PtyCharDriver *s = chr->opaque;
  970. if (!connected) {
  971. remove_fd_in_watch(chr);
  972. s->connected = 0;
  973. /* (re-)connect poll interval for idle guests: once per second.
  974. * We check more frequently in case the guests sends data to
  975. * the virtual device linked to our pty. */
  976. pty_chr_rearm_timer(chr, 1000);
  977. } else {
  978. if (s->timer_tag) {
  979. g_source_remove(s->timer_tag);
  980. s->timer_tag = 0;
  981. }
  982. if (!s->connected) {
  983. s->connected = 1;
  984. qemu_chr_be_generic_open(chr);
  985. }
  986. if (!chr->fd_in_tag) {
  987. chr->fd_in_tag = io_add_watch_poll(s->fd, pty_chr_read_poll,
  988. pty_chr_read, chr);
  989. }
  990. }
  991. }
  992. static void pty_chr_close(struct CharDriverState *chr)
  993. {
  994. PtyCharDriver *s = chr->opaque;
  995. int fd;
  996. remove_fd_in_watch(chr);
  997. fd = g_io_channel_unix_get_fd(s->fd);
  998. g_io_channel_unref(s->fd);
  999. close(fd);
  1000. if (s->timer_tag) {
  1001. g_source_remove(s->timer_tag);
  1002. s->timer_tag = 0;
  1003. }
  1004. g_free(s);
  1005. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1006. }
  1007. static CharDriverState *qemu_chr_open_pty(const char *id,
  1008. ChardevReturn *ret)
  1009. {
  1010. CharDriverState *chr;
  1011. PtyCharDriver *s;
  1012. int master_fd, slave_fd;
  1013. char pty_name[PATH_MAX];
  1014. master_fd = qemu_openpty_raw(&slave_fd, pty_name);
  1015. if (master_fd < 0) {
  1016. return NULL;
  1017. }
  1018. close(slave_fd);
  1019. chr = g_malloc0(sizeof(CharDriverState));
  1020. chr->filename = g_strdup_printf("pty:%s", pty_name);
  1021. ret->pty = g_strdup(pty_name);
  1022. ret->has_pty = true;
  1023. fprintf(stderr, "char device redirected to %s (label %s)\n",
  1024. pty_name, id);
  1025. s = g_malloc0(sizeof(PtyCharDriver));
  1026. chr->opaque = s;
  1027. chr->chr_write = pty_chr_write;
  1028. chr->chr_update_read_handler = pty_chr_update_read_handler;
  1029. chr->chr_close = pty_chr_close;
  1030. chr->chr_add_watch = pty_chr_add_watch;
  1031. chr->explicit_be_open = true;
  1032. s->fd = io_channel_from_fd(master_fd);
  1033. s->timer_tag = 0;
  1034. return chr;
  1035. }
  1036. static void tty_serial_init(int fd, int speed,
  1037. int parity, int data_bits, int stop_bits)
  1038. {
  1039. struct termios tty;
  1040. speed_t spd;
  1041. #if 0
  1042. printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
  1043. speed, parity, data_bits, stop_bits);
  1044. #endif
  1045. tcgetattr (fd, &tty);
  1046. #define check_speed(val) if (speed <= val) { spd = B##val; break; }
  1047. speed = speed * 10 / 11;
  1048. do {
  1049. check_speed(50);
  1050. check_speed(75);
  1051. check_speed(110);
  1052. check_speed(134);
  1053. check_speed(150);
  1054. check_speed(200);
  1055. check_speed(300);
  1056. check_speed(600);
  1057. check_speed(1200);
  1058. check_speed(1800);
  1059. check_speed(2400);
  1060. check_speed(4800);
  1061. check_speed(9600);
  1062. check_speed(19200);
  1063. check_speed(38400);
  1064. /* Non-Posix values follow. They may be unsupported on some systems. */
  1065. check_speed(57600);
  1066. check_speed(115200);
  1067. #ifdef B230400
  1068. check_speed(230400);
  1069. #endif
  1070. #ifdef B460800
  1071. check_speed(460800);
  1072. #endif
  1073. #ifdef B500000
  1074. check_speed(500000);
  1075. #endif
  1076. #ifdef B576000
  1077. check_speed(576000);
  1078. #endif
  1079. #ifdef B921600
  1080. check_speed(921600);
  1081. #endif
  1082. #ifdef B1000000
  1083. check_speed(1000000);
  1084. #endif
  1085. #ifdef B1152000
  1086. check_speed(1152000);
  1087. #endif
  1088. #ifdef B1500000
  1089. check_speed(1500000);
  1090. #endif
  1091. #ifdef B2000000
  1092. check_speed(2000000);
  1093. #endif
  1094. #ifdef B2500000
  1095. check_speed(2500000);
  1096. #endif
  1097. #ifdef B3000000
  1098. check_speed(3000000);
  1099. #endif
  1100. #ifdef B3500000
  1101. check_speed(3500000);
  1102. #endif
  1103. #ifdef B4000000
  1104. check_speed(4000000);
  1105. #endif
  1106. spd = B115200;
  1107. } while (0);
  1108. cfsetispeed(&tty, spd);
  1109. cfsetospeed(&tty, spd);
  1110. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  1111. |INLCR|IGNCR|ICRNL|IXON);
  1112. tty.c_oflag |= OPOST;
  1113. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
  1114. tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
  1115. switch(data_bits) {
  1116. default:
  1117. case 8:
  1118. tty.c_cflag |= CS8;
  1119. break;
  1120. case 7:
  1121. tty.c_cflag |= CS7;
  1122. break;
  1123. case 6:
  1124. tty.c_cflag |= CS6;
  1125. break;
  1126. case 5:
  1127. tty.c_cflag |= CS5;
  1128. break;
  1129. }
  1130. switch(parity) {
  1131. default:
  1132. case 'N':
  1133. break;
  1134. case 'E':
  1135. tty.c_cflag |= PARENB;
  1136. break;
  1137. case 'O':
  1138. tty.c_cflag |= PARENB | PARODD;
  1139. break;
  1140. }
  1141. if (stop_bits == 2)
  1142. tty.c_cflag |= CSTOPB;
  1143. tcsetattr (fd, TCSANOW, &tty);
  1144. }
  1145. static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
  1146. {
  1147. FDCharDriver *s = chr->opaque;
  1148. switch(cmd) {
  1149. case CHR_IOCTL_SERIAL_SET_PARAMS:
  1150. {
  1151. QEMUSerialSetParams *ssp = arg;
  1152. tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
  1153. ssp->speed, ssp->parity,
  1154. ssp->data_bits, ssp->stop_bits);
  1155. }
  1156. break;
  1157. case CHR_IOCTL_SERIAL_SET_BREAK:
  1158. {
  1159. int enable = *(int *)arg;
  1160. if (enable) {
  1161. tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
  1162. }
  1163. }
  1164. break;
  1165. case CHR_IOCTL_SERIAL_GET_TIOCM:
  1166. {
  1167. int sarg = 0;
  1168. int *targ = (int *)arg;
  1169. ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
  1170. *targ = 0;
  1171. if (sarg & TIOCM_CTS)
  1172. *targ |= CHR_TIOCM_CTS;
  1173. if (sarg & TIOCM_CAR)
  1174. *targ |= CHR_TIOCM_CAR;
  1175. if (sarg & TIOCM_DSR)
  1176. *targ |= CHR_TIOCM_DSR;
  1177. if (sarg & TIOCM_RI)
  1178. *targ |= CHR_TIOCM_RI;
  1179. if (sarg & TIOCM_DTR)
  1180. *targ |= CHR_TIOCM_DTR;
  1181. if (sarg & TIOCM_RTS)
  1182. *targ |= CHR_TIOCM_RTS;
  1183. }
  1184. break;
  1185. case CHR_IOCTL_SERIAL_SET_TIOCM:
  1186. {
  1187. int sarg = *(int *)arg;
  1188. int targ = 0;
  1189. ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
  1190. targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
  1191. | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
  1192. if (sarg & CHR_TIOCM_CTS)
  1193. targ |= TIOCM_CTS;
  1194. if (sarg & CHR_TIOCM_CAR)
  1195. targ |= TIOCM_CAR;
  1196. if (sarg & CHR_TIOCM_DSR)
  1197. targ |= TIOCM_DSR;
  1198. if (sarg & CHR_TIOCM_RI)
  1199. targ |= TIOCM_RI;
  1200. if (sarg & CHR_TIOCM_DTR)
  1201. targ |= TIOCM_DTR;
  1202. if (sarg & CHR_TIOCM_RTS)
  1203. targ |= TIOCM_RTS;
  1204. ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
  1205. }
  1206. break;
  1207. default:
  1208. return -ENOTSUP;
  1209. }
  1210. return 0;
  1211. }
  1212. static void qemu_chr_close_tty(CharDriverState *chr)
  1213. {
  1214. FDCharDriver *s = chr->opaque;
  1215. int fd = -1;
  1216. if (s) {
  1217. fd = g_io_channel_unix_get_fd(s->fd_in);
  1218. }
  1219. fd_chr_close(chr);
  1220. if (fd >= 0) {
  1221. close(fd);
  1222. }
  1223. }
  1224. static CharDriverState *qemu_chr_open_tty_fd(int fd)
  1225. {
  1226. CharDriverState *chr;
  1227. tty_serial_init(fd, 115200, 'N', 8, 1);
  1228. chr = qemu_chr_open_fd(fd, fd);
  1229. chr->chr_ioctl = tty_serial_ioctl;
  1230. chr->chr_close = qemu_chr_close_tty;
  1231. return chr;
  1232. }
  1233. #endif /* __linux__ || __sun__ */
  1234. #if defined(__linux__)
  1235. #define HAVE_CHARDEV_PARPORT 1
  1236. typedef struct {
  1237. int fd;
  1238. int mode;
  1239. } ParallelCharDriver;
  1240. static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
  1241. {
  1242. if (s->mode != mode) {
  1243. int m = mode;
  1244. if (ioctl(s->fd, PPSETMODE, &m) < 0)
  1245. return 0;
  1246. s->mode = mode;
  1247. }
  1248. return 1;
  1249. }
  1250. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1251. {
  1252. ParallelCharDriver *drv = chr->opaque;
  1253. int fd = drv->fd;
  1254. uint8_t b;
  1255. switch(cmd) {
  1256. case CHR_IOCTL_PP_READ_DATA:
  1257. if (ioctl(fd, PPRDATA, &b) < 0)
  1258. return -ENOTSUP;
  1259. *(uint8_t *)arg = b;
  1260. break;
  1261. case CHR_IOCTL_PP_WRITE_DATA:
  1262. b = *(uint8_t *)arg;
  1263. if (ioctl(fd, PPWDATA, &b) < 0)
  1264. return -ENOTSUP;
  1265. break;
  1266. case CHR_IOCTL_PP_READ_CONTROL:
  1267. if (ioctl(fd, PPRCONTROL, &b) < 0)
  1268. return -ENOTSUP;
  1269. /* Linux gives only the lowest bits, and no way to know data
  1270. direction! For better compatibility set the fixed upper
  1271. bits. */
  1272. *(uint8_t *)arg = b | 0xc0;
  1273. break;
  1274. case CHR_IOCTL_PP_WRITE_CONTROL:
  1275. b = *(uint8_t *)arg;
  1276. if (ioctl(fd, PPWCONTROL, &b) < 0)
  1277. return -ENOTSUP;
  1278. break;
  1279. case CHR_IOCTL_PP_READ_STATUS:
  1280. if (ioctl(fd, PPRSTATUS, &b) < 0)
  1281. return -ENOTSUP;
  1282. *(uint8_t *)arg = b;
  1283. break;
  1284. case CHR_IOCTL_PP_DATA_DIR:
  1285. if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
  1286. return -ENOTSUP;
  1287. break;
  1288. case CHR_IOCTL_PP_EPP_READ_ADDR:
  1289. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1290. struct ParallelIOArg *parg = arg;
  1291. int n = read(fd, parg->buffer, parg->count);
  1292. if (n != parg->count) {
  1293. return -EIO;
  1294. }
  1295. }
  1296. break;
  1297. case CHR_IOCTL_PP_EPP_READ:
  1298. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1299. struct ParallelIOArg *parg = arg;
  1300. int n = read(fd, parg->buffer, parg->count);
  1301. if (n != parg->count) {
  1302. return -EIO;
  1303. }
  1304. }
  1305. break;
  1306. case CHR_IOCTL_PP_EPP_WRITE_ADDR:
  1307. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1308. struct ParallelIOArg *parg = arg;
  1309. int n = write(fd, parg->buffer, parg->count);
  1310. if (n != parg->count) {
  1311. return -EIO;
  1312. }
  1313. }
  1314. break;
  1315. case CHR_IOCTL_PP_EPP_WRITE:
  1316. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1317. struct ParallelIOArg *parg = arg;
  1318. int n = write(fd, parg->buffer, parg->count);
  1319. if (n != parg->count) {
  1320. return -EIO;
  1321. }
  1322. }
  1323. break;
  1324. default:
  1325. return -ENOTSUP;
  1326. }
  1327. return 0;
  1328. }
  1329. static void pp_close(CharDriverState *chr)
  1330. {
  1331. ParallelCharDriver *drv = chr->opaque;
  1332. int fd = drv->fd;
  1333. pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
  1334. ioctl(fd, PPRELEASE);
  1335. close(fd);
  1336. g_free(drv);
  1337. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1338. }
  1339. static CharDriverState *qemu_chr_open_pp_fd(int fd)
  1340. {
  1341. CharDriverState *chr;
  1342. ParallelCharDriver *drv;
  1343. if (ioctl(fd, PPCLAIM) < 0) {
  1344. close(fd);
  1345. return NULL;
  1346. }
  1347. drv = g_malloc0(sizeof(ParallelCharDriver));
  1348. drv->fd = fd;
  1349. drv->mode = IEEE1284_MODE_COMPAT;
  1350. chr = g_malloc0(sizeof(CharDriverState));
  1351. chr->chr_write = null_chr_write;
  1352. chr->chr_ioctl = pp_ioctl;
  1353. chr->chr_close = pp_close;
  1354. chr->opaque = drv;
  1355. return chr;
  1356. }
  1357. #endif /* __linux__ */
  1358. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  1359. #define HAVE_CHARDEV_PARPORT 1
  1360. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1361. {
  1362. int fd = (int)(intptr_t)chr->opaque;
  1363. uint8_t b;
  1364. switch(cmd) {
  1365. case CHR_IOCTL_PP_READ_DATA:
  1366. if (ioctl(fd, PPIGDATA, &b) < 0)
  1367. return -ENOTSUP;
  1368. *(uint8_t *)arg = b;
  1369. break;
  1370. case CHR_IOCTL_PP_WRITE_DATA:
  1371. b = *(uint8_t *)arg;
  1372. if (ioctl(fd, PPISDATA, &b) < 0)
  1373. return -ENOTSUP;
  1374. break;
  1375. case CHR_IOCTL_PP_READ_CONTROL:
  1376. if (ioctl(fd, PPIGCTRL, &b) < 0)
  1377. return -ENOTSUP;
  1378. *(uint8_t *)arg = b;
  1379. break;
  1380. case CHR_IOCTL_PP_WRITE_CONTROL:
  1381. b = *(uint8_t *)arg;
  1382. if (ioctl(fd, PPISCTRL, &b) < 0)
  1383. return -ENOTSUP;
  1384. break;
  1385. case CHR_IOCTL_PP_READ_STATUS:
  1386. if (ioctl(fd, PPIGSTATUS, &b) < 0)
  1387. return -ENOTSUP;
  1388. *(uint8_t *)arg = b;
  1389. break;
  1390. default:
  1391. return -ENOTSUP;
  1392. }
  1393. return 0;
  1394. }
  1395. static CharDriverState *qemu_chr_open_pp_fd(int fd)
  1396. {
  1397. CharDriverState *chr;
  1398. chr = g_malloc0(sizeof(CharDriverState));
  1399. chr->opaque = (void *)(intptr_t)fd;
  1400. chr->chr_write = null_chr_write;
  1401. chr->chr_ioctl = pp_ioctl;
  1402. chr->explicit_be_open = true;
  1403. return chr;
  1404. }
  1405. #endif
  1406. #else /* _WIN32 */
  1407. typedef struct {
  1408. int max_size;
  1409. HANDLE hcom, hrecv, hsend;
  1410. OVERLAPPED orecv, osend;
  1411. BOOL fpipe;
  1412. DWORD len;
  1413. } WinCharState;
  1414. typedef struct {
  1415. HANDLE hStdIn;
  1416. HANDLE hInputReadyEvent;
  1417. HANDLE hInputDoneEvent;
  1418. HANDLE hInputThread;
  1419. uint8_t win_stdio_buf;
  1420. } WinStdioCharState;
  1421. #define NSENDBUF 2048
  1422. #define NRECVBUF 2048
  1423. #define MAXCONNECT 1
  1424. #define NTIMEOUT 5000
  1425. static int win_chr_poll(void *opaque);
  1426. static int win_chr_pipe_poll(void *opaque);
  1427. static void win_chr_close(CharDriverState *chr)
  1428. {
  1429. WinCharState *s = chr->opaque;
  1430. if (s->hsend) {
  1431. CloseHandle(s->hsend);
  1432. s->hsend = NULL;
  1433. }
  1434. if (s->hrecv) {
  1435. CloseHandle(s->hrecv);
  1436. s->hrecv = NULL;
  1437. }
  1438. if (s->hcom) {
  1439. CloseHandle(s->hcom);
  1440. s->hcom = NULL;
  1441. }
  1442. if (s->fpipe)
  1443. qemu_del_polling_cb(win_chr_pipe_poll, chr);
  1444. else
  1445. qemu_del_polling_cb(win_chr_poll, chr);
  1446. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1447. }
  1448. static int win_chr_init(CharDriverState *chr, const char *filename)
  1449. {
  1450. WinCharState *s = chr->opaque;
  1451. COMMCONFIG comcfg;
  1452. COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
  1453. COMSTAT comstat;
  1454. DWORD size;
  1455. DWORD err;
  1456. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1457. if (!s->hsend) {
  1458. fprintf(stderr, "Failed CreateEvent\n");
  1459. goto fail;
  1460. }
  1461. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1462. if (!s->hrecv) {
  1463. fprintf(stderr, "Failed CreateEvent\n");
  1464. goto fail;
  1465. }
  1466. s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
  1467. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  1468. if (s->hcom == INVALID_HANDLE_VALUE) {
  1469. fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
  1470. s->hcom = NULL;
  1471. goto fail;
  1472. }
  1473. if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
  1474. fprintf(stderr, "Failed SetupComm\n");
  1475. goto fail;
  1476. }
  1477. ZeroMemory(&comcfg, sizeof(COMMCONFIG));
  1478. size = sizeof(COMMCONFIG);
  1479. GetDefaultCommConfig(filename, &comcfg, &size);
  1480. comcfg.dcb.DCBlength = sizeof(DCB);
  1481. CommConfigDialog(filename, NULL, &comcfg);
  1482. if (!SetCommState(s->hcom, &comcfg.dcb)) {
  1483. fprintf(stderr, "Failed SetCommState\n");
  1484. goto fail;
  1485. }
  1486. if (!SetCommMask(s->hcom, EV_ERR)) {
  1487. fprintf(stderr, "Failed SetCommMask\n");
  1488. goto fail;
  1489. }
  1490. cto.ReadIntervalTimeout = MAXDWORD;
  1491. if (!SetCommTimeouts(s->hcom, &cto)) {
  1492. fprintf(stderr, "Failed SetCommTimeouts\n");
  1493. goto fail;
  1494. }
  1495. if (!ClearCommError(s->hcom, &err, &comstat)) {
  1496. fprintf(stderr, "Failed ClearCommError\n");
  1497. goto fail;
  1498. }
  1499. qemu_add_polling_cb(win_chr_poll, chr);
  1500. return 0;
  1501. fail:
  1502. win_chr_close(chr);
  1503. return -1;
  1504. }
  1505. static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
  1506. {
  1507. WinCharState *s = chr->opaque;
  1508. DWORD len, ret, size, err;
  1509. len = len1;
  1510. ZeroMemory(&s->osend, sizeof(s->osend));
  1511. s->osend.hEvent = s->hsend;
  1512. while (len > 0) {
  1513. if (s->hsend)
  1514. ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
  1515. else
  1516. ret = WriteFile(s->hcom, buf, len, &size, NULL);
  1517. if (!ret) {
  1518. err = GetLastError();
  1519. if (err == ERROR_IO_PENDING) {
  1520. ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
  1521. if (ret) {
  1522. buf += size;
  1523. len -= size;
  1524. } else {
  1525. break;
  1526. }
  1527. } else {
  1528. break;
  1529. }
  1530. } else {
  1531. buf += size;
  1532. len -= size;
  1533. }
  1534. }
  1535. return len1 - len;
  1536. }
  1537. static int win_chr_read_poll(CharDriverState *chr)
  1538. {
  1539. WinCharState *s = chr->opaque;
  1540. s->max_size = qemu_chr_be_can_write(chr);
  1541. return s->max_size;
  1542. }
  1543. static void win_chr_readfile(CharDriverState *chr)
  1544. {
  1545. WinCharState *s = chr->opaque;
  1546. int ret, err;
  1547. uint8_t buf[READ_BUF_LEN];
  1548. DWORD size;
  1549. ZeroMemory(&s->orecv, sizeof(s->orecv));
  1550. s->orecv.hEvent = s->hrecv;
  1551. ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
  1552. if (!ret) {
  1553. err = GetLastError();
  1554. if (err == ERROR_IO_PENDING) {
  1555. ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
  1556. }
  1557. }
  1558. if (size > 0) {
  1559. qemu_chr_be_write(chr, buf, size);
  1560. }
  1561. }
  1562. static void win_chr_read(CharDriverState *chr)
  1563. {
  1564. WinCharState *s = chr->opaque;
  1565. if (s->len > s->max_size)
  1566. s->len = s->max_size;
  1567. if (s->len == 0)
  1568. return;
  1569. win_chr_readfile(chr);
  1570. }
  1571. static int win_chr_poll(void *opaque)
  1572. {
  1573. CharDriverState *chr = opaque;
  1574. WinCharState *s = chr->opaque;
  1575. COMSTAT status;
  1576. DWORD comerr;
  1577. ClearCommError(s->hcom, &comerr, &status);
  1578. if (status.cbInQue > 0) {
  1579. s->len = status.cbInQue;
  1580. win_chr_read_poll(chr);
  1581. win_chr_read(chr);
  1582. return 1;
  1583. }
  1584. return 0;
  1585. }
  1586. static CharDriverState *qemu_chr_open_win_path(const char *filename)
  1587. {
  1588. CharDriverState *chr;
  1589. WinCharState *s;
  1590. chr = g_malloc0(sizeof(CharDriverState));
  1591. s = g_malloc0(sizeof(WinCharState));
  1592. chr->opaque = s;
  1593. chr->chr_write = win_chr_write;
  1594. chr->chr_close = win_chr_close;
  1595. if (win_chr_init(chr, filename) < 0) {
  1596. g_free(s);
  1597. g_free(chr);
  1598. return NULL;
  1599. }
  1600. return chr;
  1601. }
  1602. static int win_chr_pipe_poll(void *opaque)
  1603. {
  1604. CharDriverState *chr = opaque;
  1605. WinCharState *s = chr->opaque;
  1606. DWORD size;
  1607. PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
  1608. if (size > 0) {
  1609. s->len = size;
  1610. win_chr_read_poll(chr);
  1611. win_chr_read(chr);
  1612. return 1;
  1613. }
  1614. return 0;
  1615. }
  1616. static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
  1617. {
  1618. WinCharState *s = chr->opaque;
  1619. OVERLAPPED ov;
  1620. int ret;
  1621. DWORD size;
  1622. char openname[256];
  1623. s->fpipe = TRUE;
  1624. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1625. if (!s->hsend) {
  1626. fprintf(stderr, "Failed CreateEvent\n");
  1627. goto fail;
  1628. }
  1629. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1630. if (!s->hrecv) {
  1631. fprintf(stderr, "Failed CreateEvent\n");
  1632. goto fail;
  1633. }
  1634. snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
  1635. s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  1636. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
  1637. PIPE_WAIT,
  1638. MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
  1639. if (s->hcom == INVALID_HANDLE_VALUE) {
  1640. fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
  1641. s->hcom = NULL;
  1642. goto fail;
  1643. }
  1644. ZeroMemory(&ov, sizeof(ov));
  1645. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  1646. ret = ConnectNamedPipe(s->hcom, &ov);
  1647. if (ret) {
  1648. fprintf(stderr, "Failed ConnectNamedPipe\n");
  1649. goto fail;
  1650. }
  1651. ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
  1652. if (!ret) {
  1653. fprintf(stderr, "Failed GetOverlappedResult\n");
  1654. if (ov.hEvent) {
  1655. CloseHandle(ov.hEvent);
  1656. ov.hEvent = NULL;
  1657. }
  1658. goto fail;
  1659. }
  1660. if (ov.hEvent) {
  1661. CloseHandle(ov.hEvent);
  1662. ov.hEvent = NULL;
  1663. }
  1664. qemu_add_polling_cb(win_chr_pipe_poll, chr);
  1665. return 0;
  1666. fail:
  1667. win_chr_close(chr);
  1668. return -1;
  1669. }
  1670. static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
  1671. {
  1672. const char *filename = opts->device;
  1673. CharDriverState *chr;
  1674. WinCharState *s;
  1675. chr = g_malloc0(sizeof(CharDriverState));
  1676. s = g_malloc0(sizeof(WinCharState));
  1677. chr->opaque = s;
  1678. chr->chr_write = win_chr_write;
  1679. chr->chr_close = win_chr_close;
  1680. if (win_chr_pipe_init(chr, filename) < 0) {
  1681. g_free(s);
  1682. g_free(chr);
  1683. return NULL;
  1684. }
  1685. return chr;
  1686. }
  1687. static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
  1688. {
  1689. CharDriverState *chr;
  1690. WinCharState *s;
  1691. chr = g_malloc0(sizeof(CharDriverState));
  1692. s = g_malloc0(sizeof(WinCharState));
  1693. s->hcom = fd_out;
  1694. chr->opaque = s;
  1695. chr->chr_write = win_chr_write;
  1696. return chr;
  1697. }
  1698. static CharDriverState *qemu_chr_open_win_con(void)
  1699. {
  1700. return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
  1701. }
  1702. static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len)
  1703. {
  1704. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  1705. DWORD dwSize;
  1706. int len1;
  1707. len1 = len;
  1708. while (len1 > 0) {
  1709. if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) {
  1710. break;
  1711. }
  1712. buf += dwSize;
  1713. len1 -= dwSize;
  1714. }
  1715. return len - len1;
  1716. }
  1717. static void win_stdio_wait_func(void *opaque)
  1718. {
  1719. CharDriverState *chr = opaque;
  1720. WinStdioCharState *stdio = chr->opaque;
  1721. INPUT_RECORD buf[4];
  1722. int ret;
  1723. DWORD dwSize;
  1724. int i;
  1725. ret = ReadConsoleInput(stdio->hStdIn, buf, ARRAY_SIZE(buf), &dwSize);
  1726. if (!ret) {
  1727. /* Avoid error storm */
  1728. qemu_del_wait_object(stdio->hStdIn, NULL, NULL);
  1729. return;
  1730. }
  1731. for (i = 0; i < dwSize; i++) {
  1732. KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent;
  1733. if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) {
  1734. int j;
  1735. if (kev->uChar.AsciiChar != 0) {
  1736. for (j = 0; j < kev->wRepeatCount; j++) {
  1737. if (qemu_chr_be_can_write(chr)) {
  1738. uint8_t c = kev->uChar.AsciiChar;
  1739. qemu_chr_be_write(chr, &c, 1);
  1740. }
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. static DWORD WINAPI win_stdio_thread(LPVOID param)
  1747. {
  1748. CharDriverState *chr = param;
  1749. WinStdioCharState *stdio = chr->opaque;
  1750. int ret;
  1751. DWORD dwSize;
  1752. while (1) {
  1753. /* Wait for one byte */
  1754. ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL);
  1755. /* Exit in case of error, continue if nothing read */
  1756. if (!ret) {
  1757. break;
  1758. }
  1759. if (!dwSize) {
  1760. continue;
  1761. }
  1762. /* Some terminal emulator returns \r\n for Enter, just pass \n */
  1763. if (stdio->win_stdio_buf == '\r') {
  1764. continue;
  1765. }
  1766. /* Signal the main thread and wait until the byte was eaten */
  1767. if (!SetEvent(stdio->hInputReadyEvent)) {
  1768. break;
  1769. }
  1770. if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE)
  1771. != WAIT_OBJECT_0) {
  1772. break;
  1773. }
  1774. }
  1775. qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL);
  1776. return 0;
  1777. }
  1778. static void win_stdio_thread_wait_func(void *opaque)
  1779. {
  1780. CharDriverState *chr = opaque;
  1781. WinStdioCharState *stdio = chr->opaque;
  1782. if (qemu_chr_be_can_write(chr)) {
  1783. qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1);
  1784. }
  1785. SetEvent(stdio->hInputDoneEvent);
  1786. }
  1787. static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo)
  1788. {
  1789. WinStdioCharState *stdio = chr->opaque;
  1790. DWORD dwMode = 0;
  1791. GetConsoleMode(stdio->hStdIn, &dwMode);
  1792. if (echo) {
  1793. SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT);
  1794. } else {
  1795. SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT);
  1796. }
  1797. }
  1798. static void win_stdio_close(CharDriverState *chr)
  1799. {
  1800. WinStdioCharState *stdio = chr->opaque;
  1801. if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) {
  1802. CloseHandle(stdio->hInputReadyEvent);
  1803. }
  1804. if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) {
  1805. CloseHandle(stdio->hInputDoneEvent);
  1806. }
  1807. if (stdio->hInputThread != INVALID_HANDLE_VALUE) {
  1808. TerminateThread(stdio->hInputThread, 0);
  1809. }
  1810. g_free(chr->opaque);
  1811. g_free(chr);
  1812. }
  1813. static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts)
  1814. {
  1815. CharDriverState *chr;
  1816. WinStdioCharState *stdio;
  1817. DWORD dwMode;
  1818. int is_console = 0;
  1819. chr = g_malloc0(sizeof(CharDriverState));
  1820. stdio = g_malloc0(sizeof(WinStdioCharState));
  1821. stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE);
  1822. if (stdio->hStdIn == INVALID_HANDLE_VALUE) {
  1823. fprintf(stderr, "cannot open stdio: invalid handle\n");
  1824. exit(1);
  1825. }
  1826. is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0;
  1827. chr->opaque = stdio;
  1828. chr->chr_write = win_stdio_write;
  1829. chr->chr_close = win_stdio_close;
  1830. if (is_console) {
  1831. if (qemu_add_wait_object(stdio->hStdIn,
  1832. win_stdio_wait_func, chr)) {
  1833. fprintf(stderr, "qemu_add_wait_object: failed\n");
  1834. }
  1835. } else {
  1836. DWORD dwId;
  1837. stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1838. stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  1839. stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread,
  1840. chr, 0, &dwId);
  1841. if (stdio->hInputThread == INVALID_HANDLE_VALUE
  1842. || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE
  1843. || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) {
  1844. fprintf(stderr, "cannot create stdio thread or event\n");
  1845. exit(1);
  1846. }
  1847. if (qemu_add_wait_object(stdio->hInputReadyEvent,
  1848. win_stdio_thread_wait_func, chr)) {
  1849. fprintf(stderr, "qemu_add_wait_object: failed\n");
  1850. }
  1851. }
  1852. dwMode |= ENABLE_LINE_INPUT;
  1853. if (is_console) {
  1854. /* set the terminal in raw mode */
  1855. /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */
  1856. dwMode |= ENABLE_PROCESSED_INPUT;
  1857. }
  1858. SetConsoleMode(stdio->hStdIn, dwMode);
  1859. chr->chr_set_echo = qemu_chr_set_echo_win_stdio;
  1860. qemu_chr_fe_set_echo(chr, false);
  1861. return chr;
  1862. }
  1863. #endif /* !_WIN32 */
  1864. /***********************************************************/
  1865. /* UDP Net console */
  1866. typedef struct {
  1867. int fd;
  1868. GIOChannel *chan;
  1869. uint8_t buf[READ_BUF_LEN];
  1870. int bufcnt;
  1871. int bufptr;
  1872. int max_size;
  1873. } NetCharDriver;
  1874. static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1875. {
  1876. NetCharDriver *s = chr->opaque;
  1877. gsize bytes_written;
  1878. GIOStatus status;
  1879. status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL);
  1880. if (status == G_IO_STATUS_EOF) {
  1881. return 0;
  1882. } else if (status != G_IO_STATUS_NORMAL) {
  1883. return -1;
  1884. }
  1885. return bytes_written;
  1886. }
  1887. static int udp_chr_read_poll(void *opaque)
  1888. {
  1889. CharDriverState *chr = opaque;
  1890. NetCharDriver *s = chr->opaque;
  1891. s->max_size = qemu_chr_be_can_write(chr);
  1892. /* If there were any stray characters in the queue process them
  1893. * first
  1894. */
  1895. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1896. qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
  1897. s->bufptr++;
  1898. s->max_size = qemu_chr_be_can_write(chr);
  1899. }
  1900. return s->max_size;
  1901. }
  1902. static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
  1903. {
  1904. CharDriverState *chr = opaque;
  1905. NetCharDriver *s = chr->opaque;
  1906. gsize bytes_read = 0;
  1907. GIOStatus status;
  1908. if (s->max_size == 0) {
  1909. return TRUE;
  1910. }
  1911. status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf),
  1912. &bytes_read, NULL);
  1913. s->bufcnt = bytes_read;
  1914. s->bufptr = s->bufcnt;
  1915. if (status != G_IO_STATUS_NORMAL) {
  1916. remove_fd_in_watch(chr);
  1917. return FALSE;
  1918. }
  1919. s->bufptr = 0;
  1920. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1921. qemu_chr_be_write(chr, &s->buf[s->bufptr], 1);
  1922. s->bufptr++;
  1923. s->max_size = qemu_chr_be_can_write(chr);
  1924. }
  1925. return TRUE;
  1926. }
  1927. static void udp_chr_update_read_handler(CharDriverState *chr)
  1928. {
  1929. NetCharDriver *s = chr->opaque;
  1930. remove_fd_in_watch(chr);
  1931. if (s->chan) {
  1932. chr->fd_in_tag = io_add_watch_poll(s->chan, udp_chr_read_poll,
  1933. udp_chr_read, chr);
  1934. }
  1935. }
  1936. static void udp_chr_close(CharDriverState *chr)
  1937. {
  1938. NetCharDriver *s = chr->opaque;
  1939. remove_fd_in_watch(chr);
  1940. if (s->chan) {
  1941. g_io_channel_unref(s->chan);
  1942. closesocket(s->fd);
  1943. }
  1944. g_free(s);
  1945. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  1946. }
  1947. static CharDriverState *qemu_chr_open_udp_fd(int fd)
  1948. {
  1949. CharDriverState *chr = NULL;
  1950. NetCharDriver *s = NULL;
  1951. chr = g_malloc0(sizeof(CharDriverState));
  1952. s = g_malloc0(sizeof(NetCharDriver));
  1953. s->fd = fd;
  1954. s->chan = io_channel_from_socket(s->fd);
  1955. s->bufcnt = 0;
  1956. s->bufptr = 0;
  1957. chr->opaque = s;
  1958. chr->chr_write = udp_chr_write;
  1959. chr->chr_update_read_handler = udp_chr_update_read_handler;
  1960. chr->chr_close = udp_chr_close;
  1961. /* be isn't opened until we get a connection */
  1962. chr->explicit_be_open = true;
  1963. return chr;
  1964. }
  1965. static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
  1966. {
  1967. Error *local_err = NULL;
  1968. int fd = -1;
  1969. fd = inet_dgram_opts(opts, &local_err);
  1970. if (fd < 0) {
  1971. qerror_report_err(local_err);
  1972. error_free(local_err);
  1973. return NULL;
  1974. }
  1975. return qemu_chr_open_udp_fd(fd);
  1976. }
  1977. /***********************************************************/
  1978. /* TCP Net console */
  1979. typedef struct {
  1980. GIOChannel *chan, *listen_chan;
  1981. guint listen_tag;
  1982. int fd, listen_fd;
  1983. int connected;
  1984. int max_size;
  1985. int do_telnetopt;
  1986. int do_nodelay;
  1987. int is_unix;
  1988. int msgfd;
  1989. } TCPCharDriver;
  1990. static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque);
  1991. static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1992. {
  1993. TCPCharDriver *s = chr->opaque;
  1994. if (s->connected) {
  1995. return io_channel_send(s->chan, buf, len);
  1996. } else {
  1997. /* XXX: indicate an error ? */
  1998. return len;
  1999. }
  2000. }
  2001. static int tcp_chr_read_poll(void *opaque)
  2002. {
  2003. CharDriverState *chr = opaque;
  2004. TCPCharDriver *s = chr->opaque;
  2005. if (!s->connected)
  2006. return 0;
  2007. s->max_size = qemu_chr_be_can_write(chr);
  2008. return s->max_size;
  2009. }
  2010. #define IAC 255
  2011. #define IAC_BREAK 243
  2012. static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
  2013. TCPCharDriver *s,
  2014. uint8_t *buf, int *size)
  2015. {
  2016. /* Handle any telnet client's basic IAC options to satisfy char by
  2017. * char mode with no echo. All IAC options will be removed from
  2018. * the buf and the do_telnetopt variable will be used to track the
  2019. * state of the width of the IAC information.
  2020. *
  2021. * IAC commands come in sets of 3 bytes with the exception of the
  2022. * "IAC BREAK" command and the double IAC.
  2023. */
  2024. int i;
  2025. int j = 0;
  2026. for (i = 0; i < *size; i++) {
  2027. if (s->do_telnetopt > 1) {
  2028. if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
  2029. /* Double IAC means send an IAC */
  2030. if (j != i)
  2031. buf[j] = buf[i];
  2032. j++;
  2033. s->do_telnetopt = 1;
  2034. } else {
  2035. if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
  2036. /* Handle IAC break commands by sending a serial break */
  2037. qemu_chr_be_event(chr, CHR_EVENT_BREAK);
  2038. s->do_telnetopt++;
  2039. }
  2040. s->do_telnetopt++;
  2041. }
  2042. if (s->do_telnetopt >= 4) {
  2043. s->do_telnetopt = 1;
  2044. }
  2045. } else {
  2046. if ((unsigned char)buf[i] == IAC) {
  2047. s->do_telnetopt = 2;
  2048. } else {
  2049. if (j != i)
  2050. buf[j] = buf[i];
  2051. j++;
  2052. }
  2053. }
  2054. }
  2055. *size = j;
  2056. }
  2057. static int tcp_get_msgfd(CharDriverState *chr)
  2058. {
  2059. TCPCharDriver *s = chr->opaque;
  2060. int fd = s->msgfd;
  2061. s->msgfd = -1;
  2062. return fd;
  2063. }
  2064. #ifndef _WIN32
  2065. static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
  2066. {
  2067. TCPCharDriver *s = chr->opaque;
  2068. struct cmsghdr *cmsg;
  2069. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  2070. int fd;
  2071. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
  2072. cmsg->cmsg_level != SOL_SOCKET ||
  2073. cmsg->cmsg_type != SCM_RIGHTS)
  2074. continue;
  2075. fd = *((int *)CMSG_DATA(cmsg));
  2076. if (fd < 0)
  2077. continue;
  2078. /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
  2079. qemu_set_block(fd);
  2080. #ifndef MSG_CMSG_CLOEXEC
  2081. qemu_set_cloexec(fd);
  2082. #endif
  2083. if (s->msgfd != -1)
  2084. close(s->msgfd);
  2085. s->msgfd = fd;
  2086. }
  2087. }
  2088. static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
  2089. {
  2090. TCPCharDriver *s = chr->opaque;
  2091. struct msghdr msg = { NULL, };
  2092. struct iovec iov[1];
  2093. union {
  2094. struct cmsghdr cmsg;
  2095. char control[CMSG_SPACE(sizeof(int))];
  2096. } msg_control;
  2097. int flags = 0;
  2098. ssize_t ret;
  2099. iov[0].iov_base = buf;
  2100. iov[0].iov_len = len;
  2101. msg.msg_iov = iov;
  2102. msg.msg_iovlen = 1;
  2103. msg.msg_control = &msg_control;
  2104. msg.msg_controllen = sizeof(msg_control);
  2105. #ifdef MSG_CMSG_CLOEXEC
  2106. flags |= MSG_CMSG_CLOEXEC;
  2107. #endif
  2108. ret = recvmsg(s->fd, &msg, flags);
  2109. if (ret > 0 && s->is_unix) {
  2110. unix_process_msgfd(chr, &msg);
  2111. }
  2112. return ret;
  2113. }
  2114. #else
  2115. static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
  2116. {
  2117. TCPCharDriver *s = chr->opaque;
  2118. return qemu_recv(s->fd, buf, len, 0);
  2119. }
  2120. #endif
  2121. static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond)
  2122. {
  2123. TCPCharDriver *s = chr->opaque;
  2124. return g_io_create_watch(s->chan, cond);
  2125. }
  2126. static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
  2127. {
  2128. CharDriverState *chr = opaque;
  2129. TCPCharDriver *s = chr->opaque;
  2130. uint8_t buf[READ_BUF_LEN];
  2131. int len, size;
  2132. if (!s->connected || s->max_size <= 0) {
  2133. return TRUE;
  2134. }
  2135. len = sizeof(buf);
  2136. if (len > s->max_size)
  2137. len = s->max_size;
  2138. size = tcp_chr_recv(chr, (void *)buf, len);
  2139. if (size == 0) {
  2140. /* connection closed */
  2141. s->connected = 0;
  2142. if (s->listen_chan) {
  2143. s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
  2144. }
  2145. remove_fd_in_watch(chr);
  2146. g_io_channel_unref(s->chan);
  2147. s->chan = NULL;
  2148. closesocket(s->fd);
  2149. s->fd = -1;
  2150. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  2151. } else if (size > 0) {
  2152. if (s->do_telnetopt)
  2153. tcp_chr_process_IAC_bytes(chr, s, buf, &size);
  2154. if (size > 0)
  2155. qemu_chr_be_write(chr, buf, size);
  2156. }
  2157. return TRUE;
  2158. }
  2159. #ifndef _WIN32
  2160. CharDriverState *qemu_chr_open_eventfd(int eventfd)
  2161. {
  2162. return qemu_chr_open_fd(eventfd, eventfd);
  2163. }
  2164. #endif
  2165. static void tcp_chr_connect(void *opaque)
  2166. {
  2167. CharDriverState *chr = opaque;
  2168. TCPCharDriver *s = chr->opaque;
  2169. s->connected = 1;
  2170. if (s->chan) {
  2171. chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
  2172. tcp_chr_read, chr);
  2173. }
  2174. qemu_chr_be_generic_open(chr);
  2175. }
  2176. static void tcp_chr_update_read_handler(CharDriverState *chr)
  2177. {
  2178. TCPCharDriver *s = chr->opaque;
  2179. remove_fd_in_watch(chr);
  2180. if (s->chan) {
  2181. chr->fd_in_tag = io_add_watch_poll(s->chan, tcp_chr_read_poll,
  2182. tcp_chr_read, chr);
  2183. }
  2184. }
  2185. #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
  2186. static void tcp_chr_telnet_init(int fd)
  2187. {
  2188. char buf[3];
  2189. /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
  2190. IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
  2191. send(fd, (char *)buf, 3, 0);
  2192. IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
  2193. send(fd, (char *)buf, 3, 0);
  2194. IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
  2195. send(fd, (char *)buf, 3, 0);
  2196. IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
  2197. send(fd, (char *)buf, 3, 0);
  2198. }
  2199. static int tcp_chr_add_client(CharDriverState *chr, int fd)
  2200. {
  2201. TCPCharDriver *s = chr->opaque;
  2202. if (s->fd != -1)
  2203. return -1;
  2204. qemu_set_nonblock(fd);
  2205. if (s->do_nodelay)
  2206. socket_set_nodelay(fd);
  2207. s->fd = fd;
  2208. s->chan = io_channel_from_socket(fd);
  2209. if (s->listen_tag) {
  2210. g_source_remove(s->listen_tag);
  2211. s->listen_tag = 0;
  2212. }
  2213. tcp_chr_connect(chr);
  2214. return 0;
  2215. }
  2216. static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque)
  2217. {
  2218. CharDriverState *chr = opaque;
  2219. TCPCharDriver *s = chr->opaque;
  2220. struct sockaddr_in saddr;
  2221. #ifndef _WIN32
  2222. struct sockaddr_un uaddr;
  2223. #endif
  2224. struct sockaddr *addr;
  2225. socklen_t len;
  2226. int fd;
  2227. for(;;) {
  2228. #ifndef _WIN32
  2229. if (s->is_unix) {
  2230. len = sizeof(uaddr);
  2231. addr = (struct sockaddr *)&uaddr;
  2232. } else
  2233. #endif
  2234. {
  2235. len = sizeof(saddr);
  2236. addr = (struct sockaddr *)&saddr;
  2237. }
  2238. fd = qemu_accept(s->listen_fd, addr, &len);
  2239. if (fd < 0 && errno != EINTR) {
  2240. s->listen_tag = 0;
  2241. return FALSE;
  2242. } else if (fd >= 0) {
  2243. if (s->do_telnetopt)
  2244. tcp_chr_telnet_init(fd);
  2245. break;
  2246. }
  2247. }
  2248. if (tcp_chr_add_client(chr, fd) < 0)
  2249. close(fd);
  2250. return TRUE;
  2251. }
  2252. static void tcp_chr_close(CharDriverState *chr)
  2253. {
  2254. TCPCharDriver *s = chr->opaque;
  2255. if (s->fd >= 0) {
  2256. remove_fd_in_watch(chr);
  2257. if (s->chan) {
  2258. g_io_channel_unref(s->chan);
  2259. }
  2260. closesocket(s->fd);
  2261. }
  2262. if (s->listen_fd >= 0) {
  2263. if (s->listen_tag) {
  2264. g_source_remove(s->listen_tag);
  2265. s->listen_tag = 0;
  2266. }
  2267. if (s->listen_chan) {
  2268. g_io_channel_unref(s->listen_chan);
  2269. }
  2270. closesocket(s->listen_fd);
  2271. }
  2272. g_free(s);
  2273. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  2274. }
  2275. static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay,
  2276. bool is_listen, bool is_telnet,
  2277. bool is_waitconnect,
  2278. Error **errp)
  2279. {
  2280. CharDriverState *chr = NULL;
  2281. TCPCharDriver *s = NULL;
  2282. char host[NI_MAXHOST], serv[NI_MAXSERV];
  2283. const char *left = "", *right = "";
  2284. struct sockaddr_storage ss;
  2285. socklen_t ss_len = sizeof(ss);
  2286. memset(&ss, 0, ss_len);
  2287. if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) {
  2288. error_setg_errno(errp, errno, "getsockname");
  2289. return NULL;
  2290. }
  2291. chr = g_malloc0(sizeof(CharDriverState));
  2292. s = g_malloc0(sizeof(TCPCharDriver));
  2293. s->connected = 0;
  2294. s->fd = -1;
  2295. s->listen_fd = -1;
  2296. s->msgfd = -1;
  2297. chr->filename = g_malloc(256);
  2298. switch (ss.ss_family) {
  2299. #ifndef _WIN32
  2300. case AF_UNIX:
  2301. s->is_unix = 1;
  2302. snprintf(chr->filename, 256, "unix:%s%s",
  2303. ((struct sockaddr_un *)(&ss))->sun_path,
  2304. is_listen ? ",server" : "");
  2305. break;
  2306. #endif
  2307. case AF_INET6:
  2308. left = "[";
  2309. right = "]";
  2310. /* fall through */
  2311. case AF_INET:
  2312. s->do_nodelay = do_nodelay;
  2313. getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host),
  2314. serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
  2315. snprintf(chr->filename, 256, "%s:%s%s%s:%s%s",
  2316. is_telnet ? "telnet" : "tcp",
  2317. left, host, right, serv,
  2318. is_listen ? ",server" : "");
  2319. break;
  2320. }
  2321. chr->opaque = s;
  2322. chr->chr_write = tcp_chr_write;
  2323. chr->chr_close = tcp_chr_close;
  2324. chr->get_msgfd = tcp_get_msgfd;
  2325. chr->chr_add_client = tcp_chr_add_client;
  2326. chr->chr_add_watch = tcp_chr_add_watch;
  2327. chr->chr_update_read_handler = tcp_chr_update_read_handler;
  2328. /* be isn't opened until we get a connection */
  2329. chr->explicit_be_open = true;
  2330. if (is_listen) {
  2331. s->listen_fd = fd;
  2332. s->listen_chan = io_channel_from_socket(s->listen_fd);
  2333. s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr);
  2334. if (is_telnet) {
  2335. s->do_telnetopt = 1;
  2336. }
  2337. } else {
  2338. s->connected = 1;
  2339. s->fd = fd;
  2340. socket_set_nodelay(fd);
  2341. s->chan = io_channel_from_socket(s->fd);
  2342. tcp_chr_connect(chr);
  2343. }
  2344. if (is_listen && is_waitconnect) {
  2345. fprintf(stderr, "QEMU waiting for connection on: %s\n",
  2346. chr->filename);
  2347. tcp_chr_accept(s->listen_chan, G_IO_IN, chr);
  2348. qemu_set_nonblock(s->listen_fd);
  2349. }
  2350. return chr;
  2351. }
  2352. static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
  2353. {
  2354. CharDriverState *chr = NULL;
  2355. Error *local_err = NULL;
  2356. int fd = -1;
  2357. bool is_listen = qemu_opt_get_bool(opts, "server", false);
  2358. bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
  2359. bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
  2360. bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
  2361. bool is_unix = qemu_opt_get(opts, "path") != NULL;
  2362. if (is_unix) {
  2363. if (is_listen) {
  2364. fd = unix_listen_opts(opts, &local_err);
  2365. } else {
  2366. fd = unix_connect_opts(opts, &local_err, NULL, NULL);
  2367. }
  2368. } else {
  2369. if (is_listen) {
  2370. fd = inet_listen_opts(opts, 0, &local_err);
  2371. } else {
  2372. fd = inet_connect_opts(opts, &local_err, NULL, NULL);
  2373. }
  2374. }
  2375. if (fd < 0) {
  2376. goto fail;
  2377. }
  2378. if (!is_waitconnect)
  2379. qemu_set_nonblock(fd);
  2380. chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet,
  2381. is_waitconnect, &local_err);
  2382. if (local_err) {
  2383. goto fail;
  2384. }
  2385. return chr;
  2386. fail:
  2387. if (local_err) {
  2388. qerror_report_err(local_err);
  2389. error_free(local_err);
  2390. }
  2391. if (fd >= 0) {
  2392. closesocket(fd);
  2393. }
  2394. if (chr) {
  2395. g_free(chr->opaque);
  2396. g_free(chr);
  2397. }
  2398. return NULL;
  2399. }
  2400. /*********************************************************/
  2401. /* Ring buffer chardev */
  2402. typedef struct {
  2403. size_t size;
  2404. size_t prod;
  2405. size_t cons;
  2406. uint8_t *cbuf;
  2407. } RingBufCharDriver;
  2408. static size_t ringbuf_count(const CharDriverState *chr)
  2409. {
  2410. const RingBufCharDriver *d = chr->opaque;
  2411. return d->prod - d->cons;
  2412. }
  2413. static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  2414. {
  2415. RingBufCharDriver *d = chr->opaque;
  2416. int i;
  2417. if (!buf || (len < 0)) {
  2418. return -1;
  2419. }
  2420. for (i = 0; i < len; i++ ) {
  2421. d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
  2422. if (d->prod - d->cons > d->size) {
  2423. d->cons = d->prod - d->size;
  2424. }
  2425. }
  2426. return 0;
  2427. }
  2428. static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len)
  2429. {
  2430. RingBufCharDriver *d = chr->opaque;
  2431. int i;
  2432. for (i = 0; i < len && d->cons != d->prod; i++) {
  2433. buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
  2434. }
  2435. return i;
  2436. }
  2437. static void ringbuf_chr_close(struct CharDriverState *chr)
  2438. {
  2439. RingBufCharDriver *d = chr->opaque;
  2440. g_free(d->cbuf);
  2441. g_free(d);
  2442. chr->opaque = NULL;
  2443. }
  2444. static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,
  2445. Error **errp)
  2446. {
  2447. CharDriverState *chr;
  2448. RingBufCharDriver *d;
  2449. chr = g_malloc0(sizeof(CharDriverState));
  2450. d = g_malloc(sizeof(*d));
  2451. d->size = opts->has_size ? opts->size : 65536;
  2452. /* The size must be power of 2 */
  2453. if (d->size & (d->size - 1)) {
  2454. error_setg(errp, "size of ringbuf chardev must be power of two");
  2455. goto fail;
  2456. }
  2457. d->prod = 0;
  2458. d->cons = 0;
  2459. d->cbuf = g_malloc0(d->size);
  2460. chr->opaque = d;
  2461. chr->chr_write = ringbuf_chr_write;
  2462. chr->chr_close = ringbuf_chr_close;
  2463. return chr;
  2464. fail:
  2465. g_free(d);
  2466. g_free(chr);
  2467. return NULL;
  2468. }
  2469. static bool chr_is_ringbuf(const CharDriverState *chr)
  2470. {
  2471. return chr->chr_write == ringbuf_chr_write;
  2472. }
  2473. void qmp_ringbuf_write(const char *device, const char *data,
  2474. bool has_format, enum DataFormat format,
  2475. Error **errp)
  2476. {
  2477. CharDriverState *chr;
  2478. const uint8_t *write_data;
  2479. int ret;
  2480. gsize write_count;
  2481. chr = qemu_chr_find(device);
  2482. if (!chr) {
  2483. error_setg(errp, "Device '%s' not found", device);
  2484. return;
  2485. }
  2486. if (!chr_is_ringbuf(chr)) {
  2487. error_setg(errp,"%s is not a ringbuf device", device);
  2488. return;
  2489. }
  2490. if (has_format && (format == DATA_FORMAT_BASE64)) {
  2491. write_data = g_base64_decode(data, &write_count);
  2492. } else {
  2493. write_data = (uint8_t *)data;
  2494. write_count = strlen(data);
  2495. }
  2496. ret = ringbuf_chr_write(chr, write_data, write_count);
  2497. if (write_data != (uint8_t *)data) {
  2498. g_free((void *)write_data);
  2499. }
  2500. if (ret < 0) {
  2501. error_setg(errp, "Failed to write to device %s", device);
  2502. return;
  2503. }
  2504. }
  2505. char *qmp_ringbuf_read(const char *device, int64_t size,
  2506. bool has_format, enum DataFormat format,
  2507. Error **errp)
  2508. {
  2509. CharDriverState *chr;
  2510. uint8_t *read_data;
  2511. size_t count;
  2512. char *data;
  2513. chr = qemu_chr_find(device);
  2514. if (!chr) {
  2515. error_setg(errp, "Device '%s' not found", device);
  2516. return NULL;
  2517. }
  2518. if (!chr_is_ringbuf(chr)) {
  2519. error_setg(errp,"%s is not a ringbuf device", device);
  2520. return NULL;
  2521. }
  2522. if (size <= 0) {
  2523. error_setg(errp, "size must be greater than zero");
  2524. return NULL;
  2525. }
  2526. count = ringbuf_count(chr);
  2527. size = size > count ? count : size;
  2528. read_data = g_malloc(size + 1);
  2529. ringbuf_chr_read(chr, read_data, size);
  2530. if (has_format && (format == DATA_FORMAT_BASE64)) {
  2531. data = g_base64_encode(read_data, size);
  2532. g_free(read_data);
  2533. } else {
  2534. /*
  2535. * FIXME should read only complete, valid UTF-8 characters up
  2536. * to @size bytes. Invalid sequences should be replaced by a
  2537. * suitable replacement character. Except when (and only
  2538. * when) ring buffer lost characters since last read, initial
  2539. * continuation characters should be dropped.
  2540. */
  2541. read_data[size] = 0;
  2542. data = (char *)read_data;
  2543. }
  2544. return data;
  2545. }
  2546. QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
  2547. {
  2548. char host[65], port[33], width[8], height[8];
  2549. int pos;
  2550. const char *p;
  2551. QemuOpts *opts;
  2552. Error *local_err = NULL;
  2553. opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
  2554. if (local_err) {
  2555. qerror_report_err(local_err);
  2556. error_free(local_err);
  2557. return NULL;
  2558. }
  2559. if (strstart(filename, "mon:", &p)) {
  2560. filename = p;
  2561. qemu_opt_set(opts, "mux", "on");
  2562. if (strcmp(filename, "stdio") == 0) {
  2563. /* Monitor is muxed to stdio: do not exit on Ctrl+C by default
  2564. * but pass it to the guest. Handle this only for compat syntax,
  2565. * for -chardev syntax we have special option for this.
  2566. * This is what -nographic did, redirecting+muxing serial+monitor
  2567. * to stdio causing Ctrl+C to be passed to guest. */
  2568. qemu_opt_set(opts, "signal", "off");
  2569. }
  2570. }
  2571. if (strcmp(filename, "null") == 0 ||
  2572. strcmp(filename, "pty") == 0 ||
  2573. strcmp(filename, "msmouse") == 0 ||
  2574. strcmp(filename, "braille") == 0 ||
  2575. strcmp(filename, "stdio") == 0) {
  2576. qemu_opt_set(opts, "backend", filename);
  2577. return opts;
  2578. }
  2579. if (strstart(filename, "vc", &p)) {
  2580. qemu_opt_set(opts, "backend", "vc");
  2581. if (*p == ':') {
  2582. if (sscanf(p+1, "%7[0-9]x%7[0-9]", width, height) == 2) {
  2583. /* pixels */
  2584. qemu_opt_set(opts, "width", width);
  2585. qemu_opt_set(opts, "height", height);
  2586. } else if (sscanf(p+1, "%7[0-9]Cx%7[0-9]C", width, height) == 2) {
  2587. /* chars */
  2588. qemu_opt_set(opts, "cols", width);
  2589. qemu_opt_set(opts, "rows", height);
  2590. } else {
  2591. goto fail;
  2592. }
  2593. }
  2594. return opts;
  2595. }
  2596. if (strcmp(filename, "con:") == 0) {
  2597. qemu_opt_set(opts, "backend", "console");
  2598. return opts;
  2599. }
  2600. if (strstart(filename, "COM", NULL)) {
  2601. qemu_opt_set(opts, "backend", "serial");
  2602. qemu_opt_set(opts, "path", filename);
  2603. return opts;
  2604. }
  2605. if (strstart(filename, "file:", &p)) {
  2606. qemu_opt_set(opts, "backend", "file");
  2607. qemu_opt_set(opts, "path", p);
  2608. return opts;
  2609. }
  2610. if (strstart(filename, "pipe:", &p)) {
  2611. qemu_opt_set(opts, "backend", "pipe");
  2612. qemu_opt_set(opts, "path", p);
  2613. return opts;
  2614. }
  2615. if (strstart(filename, "tcp:", &p) ||
  2616. strstart(filename, "telnet:", &p)) {
  2617. if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
  2618. host[0] = 0;
  2619. if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
  2620. goto fail;
  2621. }
  2622. qemu_opt_set(opts, "backend", "socket");
  2623. qemu_opt_set(opts, "host", host);
  2624. qemu_opt_set(opts, "port", port);
  2625. if (p[pos] == ',') {
  2626. if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
  2627. goto fail;
  2628. }
  2629. if (strstart(filename, "telnet:", &p))
  2630. qemu_opt_set(opts, "telnet", "on");
  2631. return opts;
  2632. }
  2633. if (strstart(filename, "udp:", &p)) {
  2634. qemu_opt_set(opts, "backend", "udp");
  2635. if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
  2636. host[0] = 0;
  2637. if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
  2638. goto fail;
  2639. }
  2640. }
  2641. qemu_opt_set(opts, "host", host);
  2642. qemu_opt_set(opts, "port", port);
  2643. if (p[pos] == '@') {
  2644. p += pos + 1;
  2645. if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
  2646. host[0] = 0;
  2647. if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
  2648. goto fail;
  2649. }
  2650. }
  2651. qemu_opt_set(opts, "localaddr", host);
  2652. qemu_opt_set(opts, "localport", port);
  2653. }
  2654. return opts;
  2655. }
  2656. if (strstart(filename, "unix:", &p)) {
  2657. qemu_opt_set(opts, "backend", "socket");
  2658. if (qemu_opts_do_parse(opts, p, "path") != 0)
  2659. goto fail;
  2660. return opts;
  2661. }
  2662. if (strstart(filename, "/dev/parport", NULL) ||
  2663. strstart(filename, "/dev/ppi", NULL)) {
  2664. qemu_opt_set(opts, "backend", "parport");
  2665. qemu_opt_set(opts, "path", filename);
  2666. return opts;
  2667. }
  2668. if (strstart(filename, "/dev/", NULL)) {
  2669. qemu_opt_set(opts, "backend", "tty");
  2670. qemu_opt_set(opts, "path", filename);
  2671. return opts;
  2672. }
  2673. fail:
  2674. qemu_opts_del(opts);
  2675. return NULL;
  2676. }
  2677. static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
  2678. Error **errp)
  2679. {
  2680. const char *path = qemu_opt_get(opts, "path");
  2681. if (path == NULL) {
  2682. error_setg(errp, "chardev: file: no filename given");
  2683. return;
  2684. }
  2685. backend->file = g_new0(ChardevFile, 1);
  2686. backend->file->out = g_strdup(path);
  2687. }
  2688. static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend,
  2689. Error **errp)
  2690. {
  2691. backend->stdio = g_new0(ChardevStdio, 1);
  2692. backend->stdio->has_signal = true;
  2693. backend->stdio->signal = qemu_opt_get_bool(opts, "signal", true);
  2694. }
  2695. static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend,
  2696. Error **errp)
  2697. {
  2698. const char *device = qemu_opt_get(opts, "path");
  2699. if (device == NULL) {
  2700. error_setg(errp, "chardev: serial/tty: no device path given");
  2701. return;
  2702. }
  2703. backend->serial = g_new0(ChardevHostdev, 1);
  2704. backend->serial->device = g_strdup(device);
  2705. }
  2706. static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend,
  2707. Error **errp)
  2708. {
  2709. const char *device = qemu_opt_get(opts, "path");
  2710. if (device == NULL) {
  2711. error_setg(errp, "chardev: parallel: no device path given");
  2712. return;
  2713. }
  2714. backend->parallel = g_new0(ChardevHostdev, 1);
  2715. backend->parallel->device = g_strdup(device);
  2716. }
  2717. static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,
  2718. Error **errp)
  2719. {
  2720. const char *device = qemu_opt_get(opts, "path");
  2721. if (device == NULL) {
  2722. error_setg(errp, "chardev: pipe: no device path given");
  2723. return;
  2724. }
  2725. backend->pipe = g_new0(ChardevHostdev, 1);
  2726. backend->pipe->device = g_strdup(device);
  2727. }
  2728. static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
  2729. Error **errp)
  2730. {
  2731. int val;
  2732. backend->ringbuf = g_new0(ChardevRingbuf, 1);
  2733. val = qemu_opt_get_size(opts, "size", 0);
  2734. if (val != 0) {
  2735. backend->ringbuf->has_size = true;
  2736. backend->ringbuf->size = val;
  2737. }
  2738. }
  2739. static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
  2740. Error **errp)
  2741. {
  2742. const char *chardev = qemu_opt_get(opts, "chardev");
  2743. if (chardev == NULL) {
  2744. error_setg(errp, "chardev: mux: no chardev given");
  2745. return;
  2746. }
  2747. backend->mux = g_new0(ChardevMux, 1);
  2748. backend->mux->chardev = g_strdup(chardev);
  2749. }
  2750. typedef struct CharDriver {
  2751. const char *name;
  2752. /* old, pre qapi */
  2753. CharDriverState *(*open)(QemuOpts *opts);
  2754. /* new, qapi-based */
  2755. ChardevBackendKind kind;
  2756. void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
  2757. } CharDriver;
  2758. static GSList *backends;
  2759. void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *))
  2760. {
  2761. CharDriver *s;
  2762. s = g_malloc0(sizeof(*s));
  2763. s->name = g_strdup(name);
  2764. s->open = open;
  2765. backends = g_slist_append(backends, s);
  2766. }
  2767. void register_char_driver_qapi(const char *name, ChardevBackendKind kind,
  2768. void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp))
  2769. {
  2770. CharDriver *s;
  2771. s = g_malloc0(sizeof(*s));
  2772. s->name = g_strdup(name);
  2773. s->kind = kind;
  2774. s->parse = parse;
  2775. backends = g_slist_append(backends, s);
  2776. }
  2777. CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
  2778. void (*init)(struct CharDriverState *s),
  2779. Error **errp)
  2780. {
  2781. CharDriver *cd;
  2782. CharDriverState *chr;
  2783. GSList *i;
  2784. if (qemu_opts_id(opts) == NULL) {
  2785. error_setg(errp, "chardev: no id specified");
  2786. goto err;
  2787. }
  2788. if (qemu_opt_get(opts, "backend") == NULL) {
  2789. error_setg(errp, "chardev: \"%s\" missing backend",
  2790. qemu_opts_id(opts));
  2791. goto err;
  2792. }
  2793. for (i = backends; i; i = i->next) {
  2794. cd = i->data;
  2795. if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) {
  2796. break;
  2797. }
  2798. }
  2799. if (i == NULL) {
  2800. error_setg(errp, "chardev: backend \"%s\" not found",
  2801. qemu_opt_get(opts, "backend"));
  2802. goto err;
  2803. }
  2804. if (!cd->open) {
  2805. /* using new, qapi init */
  2806. ChardevBackend *backend = g_new0(ChardevBackend, 1);
  2807. ChardevReturn *ret = NULL;
  2808. const char *id = qemu_opts_id(opts);
  2809. char *bid = NULL;
  2810. if (qemu_opt_get_bool(opts, "mux", 0)) {
  2811. bid = g_strdup_printf("%s-base", id);
  2812. }
  2813. chr = NULL;
  2814. backend->kind = cd->kind;
  2815. if (cd->parse) {
  2816. cd->parse(opts, backend, errp);
  2817. if (error_is_set(errp)) {
  2818. goto qapi_out;
  2819. }
  2820. }
  2821. ret = qmp_chardev_add(bid ? bid : id, backend, errp);
  2822. if (error_is_set(errp)) {
  2823. goto qapi_out;
  2824. }
  2825. if (bid) {
  2826. qapi_free_ChardevBackend(backend);
  2827. qapi_free_ChardevReturn(ret);
  2828. backend = g_new0(ChardevBackend, 1);
  2829. backend->mux = g_new0(ChardevMux, 1);
  2830. backend->kind = CHARDEV_BACKEND_KIND_MUX;
  2831. backend->mux->chardev = g_strdup(bid);
  2832. ret = qmp_chardev_add(id, backend, errp);
  2833. if (error_is_set(errp)) {
  2834. chr = qemu_chr_find(bid);
  2835. qemu_chr_delete(chr);
  2836. chr = NULL;
  2837. goto qapi_out;
  2838. }
  2839. }
  2840. chr = qemu_chr_find(id);
  2841. chr->opts = opts;
  2842. qapi_out:
  2843. qapi_free_ChardevBackend(backend);
  2844. qapi_free_ChardevReturn(ret);
  2845. g_free(bid);
  2846. return chr;
  2847. }
  2848. chr = cd->open(opts);
  2849. if (!chr) {
  2850. error_setg(errp, "chardev: opening backend \"%s\" failed",
  2851. qemu_opt_get(opts, "backend"));
  2852. goto err;
  2853. }
  2854. if (!chr->filename)
  2855. chr->filename = g_strdup(qemu_opt_get(opts, "backend"));
  2856. chr->init = init;
  2857. /* if we didn't create the chardev via qmp_chardev_add, we
  2858. * need to send the OPENED event here
  2859. */
  2860. if (!chr->explicit_be_open) {
  2861. qemu_chr_be_event(chr, CHR_EVENT_OPENED);
  2862. }
  2863. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  2864. if (qemu_opt_get_bool(opts, "mux", 0)) {
  2865. CharDriverState *base = chr;
  2866. int len = strlen(qemu_opts_id(opts)) + 6;
  2867. base->label = g_malloc(len);
  2868. snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
  2869. chr = qemu_chr_open_mux(base);
  2870. chr->filename = base->filename;
  2871. chr->avail_connections = MAX_MUX;
  2872. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  2873. } else {
  2874. chr->avail_connections = 1;
  2875. }
  2876. chr->label = g_strdup(qemu_opts_id(opts));
  2877. chr->opts = opts;
  2878. return chr;
  2879. err:
  2880. qemu_opts_del(opts);
  2881. return NULL;
  2882. }
  2883. CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
  2884. {
  2885. const char *p;
  2886. CharDriverState *chr;
  2887. QemuOpts *opts;
  2888. Error *err = NULL;
  2889. if (strstart(filename, "chardev:", &p)) {
  2890. return qemu_chr_find(p);
  2891. }
  2892. opts = qemu_chr_parse_compat(label, filename);
  2893. if (!opts)
  2894. return NULL;
  2895. chr = qemu_chr_new_from_opts(opts, init, &err);
  2896. if (err) {
  2897. error_report("%s", error_get_pretty(err));
  2898. error_free(err);
  2899. }
  2900. if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
  2901. qemu_chr_fe_claim_no_fail(chr);
  2902. monitor_init(chr, MONITOR_USE_READLINE);
  2903. }
  2904. return chr;
  2905. }
  2906. void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
  2907. {
  2908. if (chr->chr_set_echo) {
  2909. chr->chr_set_echo(chr, echo);
  2910. }
  2911. }
  2912. void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
  2913. {
  2914. if (chr->fe_open == fe_open) {
  2915. return;
  2916. }
  2917. chr->fe_open = fe_open;
  2918. if (chr->chr_set_fe_open) {
  2919. chr->chr_set_fe_open(chr, fe_open);
  2920. }
  2921. }
  2922. void qemu_chr_fe_event(struct CharDriverState *chr, int event)
  2923. {
  2924. if (chr->chr_fe_event) {
  2925. chr->chr_fe_event(chr, event);
  2926. }
  2927. }
  2928. int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond,
  2929. GIOFunc func, void *user_data)
  2930. {
  2931. GSource *src;
  2932. guint tag;
  2933. if (s->chr_add_watch == NULL) {
  2934. return -ENOSYS;
  2935. }
  2936. src = s->chr_add_watch(s, cond);
  2937. g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
  2938. tag = g_source_attach(src, NULL);
  2939. g_source_unref(src);
  2940. return tag;
  2941. }
  2942. int qemu_chr_fe_claim(CharDriverState *s)
  2943. {
  2944. if (s->avail_connections < 1) {
  2945. return -1;
  2946. }
  2947. s->avail_connections--;
  2948. return 0;
  2949. }
  2950. void qemu_chr_fe_claim_no_fail(CharDriverState *s)
  2951. {
  2952. if (qemu_chr_fe_claim(s) != 0) {
  2953. fprintf(stderr, "%s: error chardev \"%s\" already used\n",
  2954. __func__, s->label);
  2955. exit(1);
  2956. }
  2957. }
  2958. void qemu_chr_fe_release(CharDriverState *s)
  2959. {
  2960. s->avail_connections++;
  2961. }
  2962. void qemu_chr_delete(CharDriverState *chr)
  2963. {
  2964. QTAILQ_REMOVE(&chardevs, chr, next);
  2965. if (chr->chr_close) {
  2966. chr->chr_close(chr);
  2967. }
  2968. g_free(chr->filename);
  2969. g_free(chr->label);
  2970. if (chr->opts) {
  2971. qemu_opts_del(chr->opts);
  2972. }
  2973. g_free(chr);
  2974. }
  2975. ChardevInfoList *qmp_query_chardev(Error **errp)
  2976. {
  2977. ChardevInfoList *chr_list = NULL;
  2978. CharDriverState *chr;
  2979. QTAILQ_FOREACH(chr, &chardevs, next) {
  2980. ChardevInfoList *info = g_malloc0(sizeof(*info));
  2981. info->value = g_malloc0(sizeof(*info->value));
  2982. info->value->label = g_strdup(chr->label);
  2983. info->value->filename = g_strdup(chr->filename);
  2984. info->next = chr_list;
  2985. chr_list = info;
  2986. }
  2987. return chr_list;
  2988. }
  2989. ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
  2990. {
  2991. ChardevBackendInfoList *backend_list = NULL;
  2992. CharDriver *c = NULL;
  2993. GSList *i = NULL;
  2994. for (i = backends; i; i = i->next) {
  2995. ChardevBackendInfoList *info = g_malloc0(sizeof(*info));
  2996. c = i->data;
  2997. info->value = g_malloc0(sizeof(*info->value));
  2998. info->value->name = g_strdup(c->name);
  2999. info->next = backend_list;
  3000. backend_list = info;
  3001. }
  3002. return backend_list;
  3003. }
  3004. CharDriverState *qemu_chr_find(const char *name)
  3005. {
  3006. CharDriverState *chr;
  3007. QTAILQ_FOREACH(chr, &chardevs, next) {
  3008. if (strcmp(chr->label, name) != 0)
  3009. continue;
  3010. return chr;
  3011. }
  3012. return NULL;
  3013. }
  3014. /* Get a character (serial) device interface. */
  3015. CharDriverState *qemu_char_get_next_serial(void)
  3016. {
  3017. static int next_serial;
  3018. CharDriverState *chr;
  3019. /* FIXME: This function needs to go away: use chardev properties! */
  3020. while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) {
  3021. chr = serial_hds[next_serial++];
  3022. qemu_chr_fe_claim_no_fail(chr);
  3023. return chr;
  3024. }
  3025. return NULL;
  3026. }
  3027. QemuOptsList qemu_chardev_opts = {
  3028. .name = "chardev",
  3029. .implied_opt_name = "backend",
  3030. .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
  3031. .desc = {
  3032. {
  3033. .name = "backend",
  3034. .type = QEMU_OPT_STRING,
  3035. },{
  3036. .name = "path",
  3037. .type = QEMU_OPT_STRING,
  3038. },{
  3039. .name = "host",
  3040. .type = QEMU_OPT_STRING,
  3041. },{
  3042. .name = "port",
  3043. .type = QEMU_OPT_STRING,
  3044. },{
  3045. .name = "localaddr",
  3046. .type = QEMU_OPT_STRING,
  3047. },{
  3048. .name = "localport",
  3049. .type = QEMU_OPT_STRING,
  3050. },{
  3051. .name = "to",
  3052. .type = QEMU_OPT_NUMBER,
  3053. },{
  3054. .name = "ipv4",
  3055. .type = QEMU_OPT_BOOL,
  3056. },{
  3057. .name = "ipv6",
  3058. .type = QEMU_OPT_BOOL,
  3059. },{
  3060. .name = "wait",
  3061. .type = QEMU_OPT_BOOL,
  3062. },{
  3063. .name = "server",
  3064. .type = QEMU_OPT_BOOL,
  3065. },{
  3066. .name = "delay",
  3067. .type = QEMU_OPT_BOOL,
  3068. },{
  3069. .name = "telnet",
  3070. .type = QEMU_OPT_BOOL,
  3071. },{
  3072. .name = "width",
  3073. .type = QEMU_OPT_NUMBER,
  3074. },{
  3075. .name = "height",
  3076. .type = QEMU_OPT_NUMBER,
  3077. },{
  3078. .name = "cols",
  3079. .type = QEMU_OPT_NUMBER,
  3080. },{
  3081. .name = "rows",
  3082. .type = QEMU_OPT_NUMBER,
  3083. },{
  3084. .name = "mux",
  3085. .type = QEMU_OPT_BOOL,
  3086. },{
  3087. .name = "signal",
  3088. .type = QEMU_OPT_BOOL,
  3089. },{
  3090. .name = "name",
  3091. .type = QEMU_OPT_STRING,
  3092. },{
  3093. .name = "debug",
  3094. .type = QEMU_OPT_NUMBER,
  3095. },{
  3096. .name = "size",
  3097. .type = QEMU_OPT_SIZE,
  3098. },{
  3099. .name = "chardev",
  3100. .type = QEMU_OPT_STRING,
  3101. },
  3102. { /* end of list */ }
  3103. },
  3104. };
  3105. #ifdef _WIN32
  3106. static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
  3107. {
  3108. HANDLE out;
  3109. if (file->has_in) {
  3110. error_setg(errp, "input file not supported");
  3111. return NULL;
  3112. }
  3113. out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  3114. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  3115. if (out == INVALID_HANDLE_VALUE) {
  3116. error_setg(errp, "open %s failed", file->out);
  3117. return NULL;
  3118. }
  3119. return qemu_chr_open_win_file(out);
  3120. }
  3121. static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
  3122. Error **errp)
  3123. {
  3124. return qemu_chr_open_win_path(serial->device);
  3125. }
  3126. static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
  3127. Error **errp)
  3128. {
  3129. error_setg(errp, "character device backend type 'parallel' not supported");
  3130. return NULL;
  3131. }
  3132. #else /* WIN32 */
  3133. static int qmp_chardev_open_file_source(char *src, int flags,
  3134. Error **errp)
  3135. {
  3136. int fd = -1;
  3137. TFR(fd = qemu_open(src, flags, 0666));
  3138. if (fd == -1) {
  3139. error_setg_file_open(errp, errno, src);
  3140. }
  3141. return fd;
  3142. }
  3143. static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp)
  3144. {
  3145. int flags, in = -1, out = -1;
  3146. flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY;
  3147. out = qmp_chardev_open_file_source(file->out, flags, errp);
  3148. if (error_is_set(errp)) {
  3149. return NULL;
  3150. }
  3151. if (file->has_in) {
  3152. flags = O_RDONLY;
  3153. in = qmp_chardev_open_file_source(file->in, flags, errp);
  3154. if (error_is_set(errp)) {
  3155. qemu_close(out);
  3156. return NULL;
  3157. }
  3158. }
  3159. return qemu_chr_open_fd(in, out);
  3160. }
  3161. static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial,
  3162. Error **errp)
  3163. {
  3164. #ifdef HAVE_CHARDEV_TTY
  3165. int fd;
  3166. fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp);
  3167. if (error_is_set(errp)) {
  3168. return NULL;
  3169. }
  3170. qemu_set_nonblock(fd);
  3171. return qemu_chr_open_tty_fd(fd);
  3172. #else
  3173. error_setg(errp, "character device backend type 'serial' not supported");
  3174. return NULL;
  3175. #endif
  3176. }
  3177. static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel,
  3178. Error **errp)
  3179. {
  3180. #ifdef HAVE_CHARDEV_PARPORT
  3181. int fd;
  3182. fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp);
  3183. if (error_is_set(errp)) {
  3184. return NULL;
  3185. }
  3186. return qemu_chr_open_pp_fd(fd);
  3187. #else
  3188. error_setg(errp, "character device backend type 'parallel' not supported");
  3189. return NULL;
  3190. #endif
  3191. }
  3192. #endif /* WIN32 */
  3193. static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
  3194. Error **errp)
  3195. {
  3196. SocketAddress *addr = sock->addr;
  3197. bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
  3198. bool is_listen = sock->has_server ? sock->server : true;
  3199. bool is_telnet = sock->has_telnet ? sock->telnet : false;
  3200. bool is_waitconnect = sock->has_wait ? sock->wait : false;
  3201. int fd;
  3202. if (is_listen) {
  3203. fd = socket_listen(addr, errp);
  3204. } else {
  3205. fd = socket_connect(addr, errp, NULL, NULL);
  3206. }
  3207. if (error_is_set(errp)) {
  3208. return NULL;
  3209. }
  3210. return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen,
  3211. is_telnet, is_waitconnect, errp);
  3212. }
  3213. static CharDriverState *qmp_chardev_open_udp(ChardevUdp *udp,
  3214. Error **errp)
  3215. {
  3216. int fd;
  3217. fd = socket_dgram(udp->remote, udp->local, errp);
  3218. if (error_is_set(errp)) {
  3219. return NULL;
  3220. }
  3221. return qemu_chr_open_udp_fd(fd);
  3222. }
  3223. ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
  3224. Error **errp)
  3225. {
  3226. ChardevReturn *ret = g_new0(ChardevReturn, 1);
  3227. CharDriverState *base, *chr = NULL;
  3228. chr = qemu_chr_find(id);
  3229. if (chr) {
  3230. error_setg(errp, "Chardev '%s' already exists", id);
  3231. g_free(ret);
  3232. return NULL;
  3233. }
  3234. switch (backend->kind) {
  3235. case CHARDEV_BACKEND_KIND_FILE:
  3236. chr = qmp_chardev_open_file(backend->file, errp);
  3237. break;
  3238. case CHARDEV_BACKEND_KIND_SERIAL:
  3239. chr = qmp_chardev_open_serial(backend->serial, errp);
  3240. break;
  3241. case CHARDEV_BACKEND_KIND_PARALLEL:
  3242. chr = qmp_chardev_open_parallel(backend->parallel, errp);
  3243. break;
  3244. case CHARDEV_BACKEND_KIND_PIPE:
  3245. chr = qemu_chr_open_pipe(backend->pipe);
  3246. break;
  3247. case CHARDEV_BACKEND_KIND_SOCKET:
  3248. chr = qmp_chardev_open_socket(backend->socket, errp);
  3249. break;
  3250. case CHARDEV_BACKEND_KIND_UDP:
  3251. chr = qmp_chardev_open_udp(backend->udp, errp);
  3252. break;
  3253. #ifdef HAVE_CHARDEV_TTY
  3254. case CHARDEV_BACKEND_KIND_PTY:
  3255. chr = qemu_chr_open_pty(id, ret);
  3256. break;
  3257. #endif
  3258. case CHARDEV_BACKEND_KIND_NULL:
  3259. chr = qemu_chr_open_null();
  3260. break;
  3261. case CHARDEV_BACKEND_KIND_MUX:
  3262. base = qemu_chr_find(backend->mux->chardev);
  3263. if (base == NULL) {
  3264. error_setg(errp, "mux: base chardev %s not found",
  3265. backend->mux->chardev);
  3266. break;
  3267. }
  3268. chr = qemu_chr_open_mux(base);
  3269. break;
  3270. case CHARDEV_BACKEND_KIND_MSMOUSE:
  3271. chr = qemu_chr_open_msmouse();
  3272. break;
  3273. #ifdef CONFIG_BRLAPI
  3274. case CHARDEV_BACKEND_KIND_BRAILLE:
  3275. chr = chr_baum_init();
  3276. break;
  3277. #endif
  3278. case CHARDEV_BACKEND_KIND_STDIO:
  3279. chr = qemu_chr_open_stdio(backend->stdio);
  3280. break;
  3281. #ifdef _WIN32
  3282. case CHARDEV_BACKEND_KIND_CONSOLE:
  3283. chr = qemu_chr_open_win_con();
  3284. break;
  3285. #endif
  3286. #ifdef CONFIG_SPICE
  3287. case CHARDEV_BACKEND_KIND_SPICEVMC:
  3288. chr = qemu_chr_open_spice_vmc(backend->spicevmc->type);
  3289. break;
  3290. case CHARDEV_BACKEND_KIND_SPICEPORT:
  3291. chr = qemu_chr_open_spice_port(backend->spiceport->fqdn);
  3292. break;
  3293. #endif
  3294. case CHARDEV_BACKEND_KIND_VC:
  3295. chr = vc_init(backend->vc);
  3296. break;
  3297. case CHARDEV_BACKEND_KIND_RINGBUF:
  3298. case CHARDEV_BACKEND_KIND_MEMORY:
  3299. chr = qemu_chr_open_ringbuf(backend->ringbuf, errp);
  3300. break;
  3301. default:
  3302. error_setg(errp, "unknown chardev backend (%d)", backend->kind);
  3303. break;
  3304. }
  3305. if (chr == NULL && !error_is_set(errp)) {
  3306. error_setg(errp, "Failed to create chardev");
  3307. }
  3308. if (chr) {
  3309. chr->label = g_strdup(id);
  3310. chr->avail_connections =
  3311. (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1;
  3312. if (!chr->filename) {
  3313. chr->filename = g_strdup(ChardevBackendKind_lookup[backend->kind]);
  3314. }
  3315. if (!chr->explicit_be_open) {
  3316. qemu_chr_be_event(chr, CHR_EVENT_OPENED);
  3317. }
  3318. QTAILQ_INSERT_TAIL(&chardevs, chr, next);
  3319. return ret;
  3320. } else {
  3321. g_free(ret);
  3322. return NULL;
  3323. }
  3324. }
  3325. void qmp_chardev_remove(const char *id, Error **errp)
  3326. {
  3327. CharDriverState *chr;
  3328. chr = qemu_chr_find(id);
  3329. if (NULL == chr) {
  3330. error_setg(errp, "Chardev '%s' not found", id);
  3331. return;
  3332. }
  3333. if (chr->chr_can_read || chr->chr_read ||
  3334. chr->chr_event || chr->handler_opaque) {
  3335. error_setg(errp, "Chardev '%s' is busy", id);
  3336. return;
  3337. }
  3338. qemu_chr_delete(chr);
  3339. }
  3340. static void register_types(void)
  3341. {
  3342. register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL);
  3343. register_char_driver("socket", qemu_chr_open_socket);
  3344. register_char_driver("udp", qemu_chr_open_udp);
  3345. register_char_driver_qapi("ringbuf", CHARDEV_BACKEND_KIND_RINGBUF,
  3346. qemu_chr_parse_ringbuf);
  3347. register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE,
  3348. qemu_chr_parse_file_out);
  3349. register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO,
  3350. qemu_chr_parse_stdio);
  3351. register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL,
  3352. qemu_chr_parse_serial);
  3353. register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL,
  3354. qemu_chr_parse_serial);
  3355. register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL,
  3356. qemu_chr_parse_parallel);
  3357. register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL,
  3358. qemu_chr_parse_parallel);
  3359. register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL);
  3360. register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL);
  3361. register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE,
  3362. qemu_chr_parse_pipe);
  3363. register_char_driver_qapi("mux", CHARDEV_BACKEND_KIND_MUX,
  3364. qemu_chr_parse_mux);
  3365. /* Bug-compatibility: */
  3366. register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY,
  3367. qemu_chr_parse_ringbuf);
  3368. /* this must be done after machine init, since we register FEs with muxes
  3369. * as part of realize functions like serial_isa_realizefn when -nographic
  3370. * is specified
  3371. */
  3372. qemu_add_machine_init_done_notifier(&muxes_realize_notify);
  3373. }
  3374. type_init(register_types);