qemu-char.c 69 KB

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