qemu-char.c 106 KB

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