qemu-char.c 88 KB

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