2
0

qemu-char.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  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 "console.h"
  27. #include "sysemu.h"
  28. #include "qemu-timer.h"
  29. #include "qemu-char.h"
  30. #include "block.h"
  31. #include "hw/usb.h"
  32. #include "hw/baum.h"
  33. #include "hw/msmouse.h"
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <signal.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. #ifdef __NetBSD__
  52. #include <net/if_tap.h>
  53. #endif
  54. #ifdef __linux__
  55. #include <linux/if_tun.h>
  56. #endif
  57. #include <arpa/inet.h>
  58. #include <dirent.h>
  59. #include <netdb.h>
  60. #include <sys/select.h>
  61. #ifdef _BSD
  62. #include <sys/stat.h>
  63. #ifdef __FreeBSD__
  64. #include <libutil.h>
  65. #include <dev/ppbus/ppi.h>
  66. #include <dev/ppbus/ppbconf.h>
  67. #else
  68. #include <util.h>
  69. #endif
  70. #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
  71. #include <freebsd/stdlib.h>
  72. #else
  73. #ifdef __linux__
  74. #include <pty.h>
  75. #include <linux/ppdev.h>
  76. #include <linux/parport.h>
  77. #endif
  78. #ifdef __sun__
  79. #include <sys/stat.h>
  80. #include <sys/ethernet.h>
  81. #include <sys/sockio.h>
  82. #include <netinet/arp.h>
  83. #include <netinet/in.h>
  84. #include <netinet/in_systm.h>
  85. #include <netinet/ip.h>
  86. #include <netinet/ip_icmp.h> // must come after ip.h
  87. #include <netinet/udp.h>
  88. #include <netinet/tcp.h>
  89. #include <net/if.h>
  90. #include <syslog.h>
  91. #include <stropts.h>
  92. #endif
  93. #endif
  94. #endif
  95. #include "qemu_socket.h"
  96. /***********************************************************/
  97. /* character device */
  98. static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
  99. TAILQ_HEAD_INITIALIZER(chardevs);
  100. static int initial_reset_issued;
  101. static void qemu_chr_event(CharDriverState *s, int event)
  102. {
  103. if (!s->chr_event)
  104. return;
  105. s->chr_event(s->handler_opaque, event);
  106. }
  107. static void qemu_chr_reset_bh(void *opaque)
  108. {
  109. CharDriverState *s = opaque;
  110. qemu_chr_event(s, CHR_EVENT_RESET);
  111. qemu_bh_delete(s->bh);
  112. s->bh = NULL;
  113. }
  114. void qemu_chr_reset(CharDriverState *s)
  115. {
  116. if (s->bh == NULL && initial_reset_issued) {
  117. s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
  118. qemu_bh_schedule(s->bh);
  119. }
  120. }
  121. void qemu_chr_initial_reset(void)
  122. {
  123. CharDriverState *chr;
  124. initial_reset_issued = 1;
  125. TAILQ_FOREACH(chr, &chardevs, next) {
  126. qemu_chr_reset(chr);
  127. }
  128. }
  129. int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
  130. {
  131. return s->chr_write(s, buf, len);
  132. }
  133. int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
  134. {
  135. if (!s->chr_ioctl)
  136. return -ENOTSUP;
  137. return s->chr_ioctl(s, cmd, arg);
  138. }
  139. int qemu_chr_can_read(CharDriverState *s)
  140. {
  141. if (!s->chr_can_read)
  142. return 0;
  143. return s->chr_can_read(s->handler_opaque);
  144. }
  145. void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
  146. {
  147. s->chr_read(s->handler_opaque, buf, len);
  148. }
  149. void qemu_chr_accept_input(CharDriverState *s)
  150. {
  151. if (s->chr_accept_input)
  152. s->chr_accept_input(s);
  153. }
  154. void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
  155. {
  156. char buf[4096];
  157. va_list ap;
  158. va_start(ap, fmt);
  159. vsnprintf(buf, sizeof(buf), fmt, ap);
  160. qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
  161. va_end(ap);
  162. }
  163. void qemu_chr_send_event(CharDriverState *s, int event)
  164. {
  165. if (s->chr_send_event)
  166. s->chr_send_event(s, event);
  167. }
  168. void qemu_chr_add_handlers(CharDriverState *s,
  169. IOCanRWHandler *fd_can_read,
  170. IOReadHandler *fd_read,
  171. IOEventHandler *fd_event,
  172. void *opaque)
  173. {
  174. s->chr_can_read = fd_can_read;
  175. s->chr_read = fd_read;
  176. s->chr_event = fd_event;
  177. s->handler_opaque = opaque;
  178. if (s->chr_update_read_handler)
  179. s->chr_update_read_handler(s);
  180. }
  181. static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  182. {
  183. return len;
  184. }
  185. static CharDriverState *qemu_chr_open_null(void)
  186. {
  187. CharDriverState *chr;
  188. chr = qemu_mallocz(sizeof(CharDriverState));
  189. chr->chr_write = null_chr_write;
  190. return chr;
  191. }
  192. /* MUX driver for serial I/O splitting */
  193. static int term_timestamps;
  194. static int64_t term_timestamps_start;
  195. #define MAX_MUX 4
  196. #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
  197. #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
  198. typedef struct {
  199. IOCanRWHandler *chr_can_read[MAX_MUX];
  200. IOReadHandler *chr_read[MAX_MUX];
  201. IOEventHandler *chr_event[MAX_MUX];
  202. void *ext_opaque[MAX_MUX];
  203. CharDriverState *drv;
  204. int mux_cnt;
  205. int term_got_escape;
  206. int max_size;
  207. /* Intermediate input buffer allows to catch escape sequences even if the
  208. currently active device is not accepting any input - but only until it
  209. is full as well. */
  210. unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
  211. int prod[MAX_MUX];
  212. int cons[MAX_MUX];
  213. } MuxDriver;
  214. static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  215. {
  216. MuxDriver *d = chr->opaque;
  217. int ret;
  218. if (!term_timestamps) {
  219. ret = d->drv->chr_write(d->drv, buf, len);
  220. } else {
  221. int i;
  222. ret = 0;
  223. for(i = 0; i < len; i++) {
  224. ret += d->drv->chr_write(d->drv, buf+i, 1);
  225. if (buf[i] == '\n') {
  226. char buf1[64];
  227. int64_t ti;
  228. int secs;
  229. ti = qemu_get_clock(rt_clock);
  230. if (term_timestamps_start == -1)
  231. term_timestamps_start = ti;
  232. ti -= term_timestamps_start;
  233. secs = ti / 1000;
  234. snprintf(buf1, sizeof(buf1),
  235. "[%02d:%02d:%02d.%03d] ",
  236. secs / 3600,
  237. (secs / 60) % 60,
  238. secs % 60,
  239. (int)(ti % 1000));
  240. d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
  241. }
  242. }
  243. }
  244. return ret;
  245. }
  246. static const char * const mux_help[] = {
  247. "% h print this help\n\r",
  248. "% x exit emulator\n\r",
  249. "% s save disk data back to file (if -snapshot)\n\r",
  250. "% t toggle console timestamps\n\r"
  251. "% b send break (magic sysrq)\n\r",
  252. "% c switch between console and monitor\n\r",
  253. "% % sends %\n\r",
  254. NULL
  255. };
  256. int term_escape_char = 0x01; /* ctrl-a is used for escape */
  257. static void mux_print_help(CharDriverState *chr)
  258. {
  259. int i, j;
  260. char ebuf[15] = "Escape-Char";
  261. char cbuf[50] = "\n\r";
  262. if (term_escape_char > 0 && term_escape_char < 26) {
  263. snprintf(cbuf, sizeof(cbuf), "\n\r");
  264. snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
  265. } else {
  266. snprintf(cbuf, sizeof(cbuf),
  267. "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
  268. term_escape_char);
  269. }
  270. chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
  271. for (i = 0; mux_help[i] != NULL; i++) {
  272. for (j=0; mux_help[i][j] != '\0'; j++) {
  273. if (mux_help[i][j] == '%')
  274. chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
  275. else
  276. chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
  277. }
  278. }
  279. }
  280. static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
  281. {
  282. if (d->term_got_escape) {
  283. d->term_got_escape = 0;
  284. if (ch == term_escape_char)
  285. goto send_char;
  286. switch(ch) {
  287. case '?':
  288. case 'h':
  289. mux_print_help(chr);
  290. break;
  291. case 'x':
  292. {
  293. const char *term = "QEMU: Terminated\n\r";
  294. chr->chr_write(chr,(uint8_t *)term,strlen(term));
  295. exit(0);
  296. break;
  297. }
  298. case 's':
  299. {
  300. int i;
  301. for (i = 0; i < nb_drives; i++) {
  302. bdrv_commit(drives_table[i].bdrv);
  303. }
  304. }
  305. break;
  306. case 'b':
  307. qemu_chr_event(chr, CHR_EVENT_BREAK);
  308. break;
  309. case 'c':
  310. /* Switch to the next registered device */
  311. chr->focus++;
  312. if (chr->focus >= d->mux_cnt)
  313. chr->focus = 0;
  314. break;
  315. case 't':
  316. term_timestamps = !term_timestamps;
  317. term_timestamps_start = -1;
  318. break;
  319. }
  320. } else if (ch == term_escape_char) {
  321. d->term_got_escape = 1;
  322. } else {
  323. send_char:
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. static void mux_chr_accept_input(CharDriverState *chr)
  329. {
  330. int m = chr->focus;
  331. MuxDriver *d = chr->opaque;
  332. while (d->prod[m] != d->cons[m] &&
  333. d->chr_can_read[m] &&
  334. d->chr_can_read[m](d->ext_opaque[m])) {
  335. d->chr_read[m](d->ext_opaque[m],
  336. &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
  337. }
  338. }
  339. static int mux_chr_can_read(void *opaque)
  340. {
  341. CharDriverState *chr = opaque;
  342. MuxDriver *d = chr->opaque;
  343. int m = chr->focus;
  344. if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
  345. return 1;
  346. if (d->chr_can_read[m])
  347. return d->chr_can_read[m](d->ext_opaque[m]);
  348. return 0;
  349. }
  350. static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
  351. {
  352. CharDriverState *chr = opaque;
  353. MuxDriver *d = chr->opaque;
  354. int m = chr->focus;
  355. int i;
  356. mux_chr_accept_input (opaque);
  357. for(i = 0; i < size; i++)
  358. if (mux_proc_byte(chr, d, buf[i])) {
  359. if (d->prod[m] == d->cons[m] &&
  360. d->chr_can_read[m] &&
  361. d->chr_can_read[m](d->ext_opaque[m]))
  362. d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
  363. else
  364. d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
  365. }
  366. }
  367. static void mux_chr_event(void *opaque, int event)
  368. {
  369. CharDriverState *chr = opaque;
  370. MuxDriver *d = chr->opaque;
  371. int i;
  372. /* Send the event to all registered listeners */
  373. for (i = 0; i < d->mux_cnt; i++)
  374. if (d->chr_event[i])
  375. d->chr_event[i](d->ext_opaque[i], event);
  376. }
  377. static void mux_chr_update_read_handler(CharDriverState *chr)
  378. {
  379. MuxDriver *d = chr->opaque;
  380. if (d->mux_cnt >= MAX_MUX) {
  381. fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
  382. return;
  383. }
  384. d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
  385. d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
  386. d->chr_read[d->mux_cnt] = chr->chr_read;
  387. d->chr_event[d->mux_cnt] = chr->chr_event;
  388. /* Fix up the real driver with mux routines */
  389. if (d->mux_cnt == 0) {
  390. qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
  391. mux_chr_event, chr);
  392. }
  393. chr->focus = d->mux_cnt;
  394. d->mux_cnt++;
  395. }
  396. static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
  397. {
  398. CharDriverState *chr;
  399. MuxDriver *d;
  400. chr = qemu_mallocz(sizeof(CharDriverState));
  401. d = qemu_mallocz(sizeof(MuxDriver));
  402. chr->opaque = d;
  403. d->drv = drv;
  404. chr->focus = -1;
  405. chr->chr_write = mux_chr_write;
  406. chr->chr_update_read_handler = mux_chr_update_read_handler;
  407. chr->chr_accept_input = mux_chr_accept_input;
  408. return chr;
  409. }
  410. #ifdef _WIN32
  411. int send_all(int fd, const void *buf, int len1)
  412. {
  413. int ret, len;
  414. len = len1;
  415. while (len > 0) {
  416. ret = send(fd, buf, len, 0);
  417. if (ret < 0) {
  418. errno = WSAGetLastError();
  419. if (errno != WSAEWOULDBLOCK) {
  420. return -1;
  421. }
  422. } else if (ret == 0) {
  423. break;
  424. } else {
  425. buf += ret;
  426. len -= ret;
  427. }
  428. }
  429. return len1 - len;
  430. }
  431. #else
  432. static int unix_write(int fd, const uint8_t *buf, int len1)
  433. {
  434. int ret, len;
  435. len = len1;
  436. while (len > 0) {
  437. ret = write(fd, buf, len);
  438. if (ret < 0) {
  439. if (errno != EINTR && errno != EAGAIN)
  440. return -1;
  441. } else if (ret == 0) {
  442. break;
  443. } else {
  444. buf += ret;
  445. len -= ret;
  446. }
  447. }
  448. return len1 - len;
  449. }
  450. int send_all(int fd, const void *buf, int len1)
  451. {
  452. return unix_write(fd, buf, len1);
  453. }
  454. #endif /* !_WIN32 */
  455. #ifndef _WIN32
  456. typedef struct {
  457. int fd_in, fd_out;
  458. int max_size;
  459. } FDCharDriver;
  460. #define STDIO_MAX_CLIENTS 1
  461. static int stdio_nb_clients = 0;
  462. static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  463. {
  464. FDCharDriver *s = chr->opaque;
  465. return send_all(s->fd_out, buf, len);
  466. }
  467. static int fd_chr_read_poll(void *opaque)
  468. {
  469. CharDriverState *chr = opaque;
  470. FDCharDriver *s = chr->opaque;
  471. s->max_size = qemu_chr_can_read(chr);
  472. return s->max_size;
  473. }
  474. static void fd_chr_read(void *opaque)
  475. {
  476. CharDriverState *chr = opaque;
  477. FDCharDriver *s = chr->opaque;
  478. int size, len;
  479. uint8_t buf[1024];
  480. len = sizeof(buf);
  481. if (len > s->max_size)
  482. len = s->max_size;
  483. if (len == 0)
  484. return;
  485. size = read(s->fd_in, buf, len);
  486. if (size == 0) {
  487. /* FD has been closed. Remove it from the active list. */
  488. qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
  489. return;
  490. }
  491. if (size > 0) {
  492. qemu_chr_read(chr, buf, size);
  493. }
  494. }
  495. static void fd_chr_update_read_handler(CharDriverState *chr)
  496. {
  497. FDCharDriver *s = chr->opaque;
  498. if (s->fd_in >= 0) {
  499. if (nographic && s->fd_in == 0) {
  500. } else {
  501. qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
  502. fd_chr_read, NULL, chr);
  503. }
  504. }
  505. }
  506. static void fd_chr_close(struct CharDriverState *chr)
  507. {
  508. FDCharDriver *s = chr->opaque;
  509. if (s->fd_in >= 0) {
  510. if (nographic && s->fd_in == 0) {
  511. } else {
  512. qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
  513. }
  514. }
  515. qemu_free(s);
  516. }
  517. /* open a character device to a unix fd */
  518. static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
  519. {
  520. CharDriverState *chr;
  521. FDCharDriver *s;
  522. chr = qemu_mallocz(sizeof(CharDriverState));
  523. s = qemu_mallocz(sizeof(FDCharDriver));
  524. s->fd_in = fd_in;
  525. s->fd_out = fd_out;
  526. chr->opaque = s;
  527. chr->chr_write = fd_chr_write;
  528. chr->chr_update_read_handler = fd_chr_update_read_handler;
  529. chr->chr_close = fd_chr_close;
  530. qemu_chr_reset(chr);
  531. return chr;
  532. }
  533. static CharDriverState *qemu_chr_open_file_out(const char *file_out)
  534. {
  535. int fd_out;
  536. TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
  537. if (fd_out < 0)
  538. return NULL;
  539. return qemu_chr_open_fd(-1, fd_out);
  540. }
  541. static CharDriverState *qemu_chr_open_pipe(const char *filename)
  542. {
  543. int fd_in, fd_out;
  544. char filename_in[256], filename_out[256];
  545. snprintf(filename_in, 256, "%s.in", filename);
  546. snprintf(filename_out, 256, "%s.out", filename);
  547. TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
  548. TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
  549. if (fd_in < 0 || fd_out < 0) {
  550. if (fd_in >= 0)
  551. close(fd_in);
  552. if (fd_out >= 0)
  553. close(fd_out);
  554. TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
  555. if (fd_in < 0)
  556. return NULL;
  557. }
  558. return qemu_chr_open_fd(fd_in, fd_out);
  559. }
  560. /* for STDIO, we handle the case where several clients use it
  561. (nographic mode) */
  562. #define TERM_FIFO_MAX_SIZE 1
  563. static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
  564. static int term_fifo_size;
  565. static int stdio_read_poll(void *opaque)
  566. {
  567. CharDriverState *chr = opaque;
  568. /* try to flush the queue if needed */
  569. if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
  570. qemu_chr_read(chr, term_fifo, 1);
  571. term_fifo_size = 0;
  572. }
  573. /* see if we can absorb more chars */
  574. if (term_fifo_size == 0)
  575. return 1;
  576. else
  577. return 0;
  578. }
  579. static void stdio_read(void *opaque)
  580. {
  581. int size;
  582. uint8_t buf[1];
  583. CharDriverState *chr = opaque;
  584. size = read(0, buf, 1);
  585. if (size == 0) {
  586. /* stdin has been closed. Remove it from the active list. */
  587. qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
  588. return;
  589. }
  590. if (size > 0) {
  591. if (qemu_chr_can_read(chr) > 0) {
  592. qemu_chr_read(chr, buf, 1);
  593. } else if (term_fifo_size == 0) {
  594. term_fifo[term_fifo_size++] = buf[0];
  595. }
  596. }
  597. }
  598. /* init terminal so that we can grab keys */
  599. static struct termios oldtty;
  600. static int old_fd0_flags;
  601. static int term_atexit_done;
  602. static void term_exit(void)
  603. {
  604. tcsetattr (0, TCSANOW, &oldtty);
  605. fcntl(0, F_SETFL, old_fd0_flags);
  606. }
  607. static void term_init(void)
  608. {
  609. struct termios tty;
  610. tcgetattr (0, &tty);
  611. oldtty = tty;
  612. old_fd0_flags = fcntl(0, F_GETFL);
  613. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  614. |INLCR|IGNCR|ICRNL|IXON);
  615. tty.c_oflag |= OPOST;
  616. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  617. /* if graphical mode, we allow Ctrl-C handling */
  618. if (nographic)
  619. tty.c_lflag &= ~ISIG;
  620. tty.c_cflag &= ~(CSIZE|PARENB);
  621. tty.c_cflag |= CS8;
  622. tty.c_cc[VMIN] = 1;
  623. tty.c_cc[VTIME] = 0;
  624. tcsetattr (0, TCSANOW, &tty);
  625. if (!term_atexit_done++)
  626. atexit(term_exit);
  627. fcntl(0, F_SETFL, O_NONBLOCK);
  628. }
  629. static void qemu_chr_close_stdio(struct CharDriverState *chr)
  630. {
  631. term_exit();
  632. stdio_nb_clients--;
  633. qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
  634. fd_chr_close(chr);
  635. }
  636. static CharDriverState *qemu_chr_open_stdio(void)
  637. {
  638. CharDriverState *chr;
  639. if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
  640. return NULL;
  641. chr = qemu_chr_open_fd(0, 1);
  642. chr->chr_close = qemu_chr_close_stdio;
  643. qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
  644. stdio_nb_clients++;
  645. term_init();
  646. return chr;
  647. }
  648. #ifdef __sun__
  649. /* Once Solaris has openpty(), this is going to be removed. */
  650. int openpty(int *amaster, int *aslave, char *name,
  651. struct termios *termp, struct winsize *winp)
  652. {
  653. const char *slave;
  654. int mfd = -1, sfd = -1;
  655. *amaster = *aslave = -1;
  656. mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  657. if (mfd < 0)
  658. goto err;
  659. if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
  660. goto err;
  661. if ((slave = ptsname(mfd)) == NULL)
  662. goto err;
  663. if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
  664. goto err;
  665. if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
  666. (termp != NULL && tcgetattr(sfd, termp) < 0))
  667. goto err;
  668. if (amaster)
  669. *amaster = mfd;
  670. if (aslave)
  671. *aslave = sfd;
  672. if (winp)
  673. ioctl(sfd, TIOCSWINSZ, winp);
  674. return 0;
  675. err:
  676. if (sfd != -1)
  677. close(sfd);
  678. close(mfd);
  679. return -1;
  680. }
  681. void cfmakeraw (struct termios *termios_p)
  682. {
  683. termios_p->c_iflag &=
  684. ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  685. termios_p->c_oflag &= ~OPOST;
  686. termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  687. termios_p->c_cflag &= ~(CSIZE|PARENB);
  688. termios_p->c_cflag |= CS8;
  689. termios_p->c_cc[VMIN] = 0;
  690. termios_p->c_cc[VTIME] = 0;
  691. }
  692. #endif
  693. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  694. || defined(__NetBSD__) || defined(__OpenBSD__)
  695. typedef struct {
  696. int fd;
  697. int connected;
  698. int polling;
  699. int read_bytes;
  700. QEMUTimer *timer;
  701. } PtyCharDriver;
  702. static void pty_chr_update_read_handler(CharDriverState *chr);
  703. static void pty_chr_state(CharDriverState *chr, int connected);
  704. static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  705. {
  706. PtyCharDriver *s = chr->opaque;
  707. if (!s->connected) {
  708. /* guest sends data, check for (re-)connect */
  709. pty_chr_update_read_handler(chr);
  710. return 0;
  711. }
  712. return send_all(s->fd, buf, len);
  713. }
  714. static int pty_chr_read_poll(void *opaque)
  715. {
  716. CharDriverState *chr = opaque;
  717. PtyCharDriver *s = chr->opaque;
  718. s->read_bytes = qemu_chr_can_read(chr);
  719. return s->read_bytes;
  720. }
  721. static void pty_chr_read(void *opaque)
  722. {
  723. CharDriverState *chr = opaque;
  724. PtyCharDriver *s = chr->opaque;
  725. int size, len;
  726. uint8_t buf[1024];
  727. len = sizeof(buf);
  728. if (len > s->read_bytes)
  729. len = s->read_bytes;
  730. if (len == 0)
  731. return;
  732. size = read(s->fd, buf, len);
  733. if ((size == -1 && errno == EIO) ||
  734. (size == 0)) {
  735. pty_chr_state(chr, 0);
  736. return;
  737. }
  738. if (size > 0) {
  739. pty_chr_state(chr, 1);
  740. qemu_chr_read(chr, buf, size);
  741. }
  742. }
  743. static void pty_chr_update_read_handler(CharDriverState *chr)
  744. {
  745. PtyCharDriver *s = chr->opaque;
  746. qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
  747. pty_chr_read, NULL, chr);
  748. s->polling = 1;
  749. /*
  750. * Short timeout here: just need wait long enougth that qemu makes
  751. * it through the poll loop once. When reconnected we want a
  752. * short timeout so we notice it almost instantly. Otherwise
  753. * read() gives us -EIO instantly, making pty_chr_state() reset the
  754. * timeout to the normal (much longer) poll interval before the
  755. * timer triggers.
  756. */
  757. qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
  758. }
  759. static void pty_chr_state(CharDriverState *chr, int connected)
  760. {
  761. PtyCharDriver *s = chr->opaque;
  762. if (!connected) {
  763. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  764. s->connected = 0;
  765. s->polling = 0;
  766. /* (re-)connect poll interval for idle guests: once per second.
  767. * We check more frequently in case the guests sends data to
  768. * the virtual device linked to our pty. */
  769. qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
  770. } else {
  771. if (!s->connected)
  772. qemu_chr_reset(chr);
  773. s->connected = 1;
  774. }
  775. }
  776. static void pty_chr_timer(void *opaque)
  777. {
  778. struct CharDriverState *chr = opaque;
  779. PtyCharDriver *s = chr->opaque;
  780. if (s->connected)
  781. return;
  782. if (s->polling) {
  783. /* If we arrive here without polling being cleared due
  784. * read returning -EIO, then we are (re-)connected */
  785. pty_chr_state(chr, 1);
  786. return;
  787. }
  788. /* Next poll ... */
  789. pty_chr_update_read_handler(chr);
  790. }
  791. static void pty_chr_close(struct CharDriverState *chr)
  792. {
  793. PtyCharDriver *s = chr->opaque;
  794. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  795. close(s->fd);
  796. qemu_del_timer(s->timer);
  797. qemu_free_timer(s->timer);
  798. qemu_free(s);
  799. }
  800. static CharDriverState *qemu_chr_open_pty(void)
  801. {
  802. CharDriverState *chr;
  803. PtyCharDriver *s;
  804. struct termios tty;
  805. int slave_fd, len;
  806. #if defined(__OpenBSD__)
  807. char pty_name[PATH_MAX];
  808. #define q_ptsname(x) pty_name
  809. #else
  810. char *pty_name = NULL;
  811. #define q_ptsname(x) ptsname(x)
  812. #endif
  813. chr = qemu_mallocz(sizeof(CharDriverState));
  814. s = qemu_mallocz(sizeof(PtyCharDriver));
  815. if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
  816. return NULL;
  817. }
  818. /* Set raw attributes on the pty. */
  819. tcgetattr(slave_fd, &tty);
  820. cfmakeraw(&tty);
  821. tcsetattr(slave_fd, TCSAFLUSH, &tty);
  822. close(slave_fd);
  823. len = strlen(q_ptsname(s->fd)) + 5;
  824. chr->filename = qemu_malloc(len);
  825. snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
  826. fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
  827. chr->opaque = s;
  828. chr->chr_write = pty_chr_write;
  829. chr->chr_update_read_handler = pty_chr_update_read_handler;
  830. chr->chr_close = pty_chr_close;
  831. s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
  832. return chr;
  833. }
  834. static void tty_serial_init(int fd, int speed,
  835. int parity, int data_bits, int stop_bits)
  836. {
  837. struct termios tty;
  838. speed_t spd;
  839. #if 0
  840. printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
  841. speed, parity, data_bits, stop_bits);
  842. #endif
  843. tcgetattr (fd, &tty);
  844. #define MARGIN 1.1
  845. if (speed <= 50 * MARGIN)
  846. spd = B50;
  847. else if (speed <= 75 * MARGIN)
  848. spd = B75;
  849. else if (speed <= 300 * MARGIN)
  850. spd = B300;
  851. else if (speed <= 600 * MARGIN)
  852. spd = B600;
  853. else if (speed <= 1200 * MARGIN)
  854. spd = B1200;
  855. else if (speed <= 2400 * MARGIN)
  856. spd = B2400;
  857. else if (speed <= 4800 * MARGIN)
  858. spd = B4800;
  859. else if (speed <= 9600 * MARGIN)
  860. spd = B9600;
  861. else if (speed <= 19200 * MARGIN)
  862. spd = B19200;
  863. else if (speed <= 38400 * MARGIN)
  864. spd = B38400;
  865. else if (speed <= 57600 * MARGIN)
  866. spd = B57600;
  867. else if (speed <= 115200 * MARGIN)
  868. spd = B115200;
  869. else
  870. spd = B115200;
  871. cfsetispeed(&tty, spd);
  872. cfsetospeed(&tty, spd);
  873. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  874. |INLCR|IGNCR|ICRNL|IXON);
  875. tty.c_oflag |= OPOST;
  876. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
  877. tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
  878. switch(data_bits) {
  879. default:
  880. case 8:
  881. tty.c_cflag |= CS8;
  882. break;
  883. case 7:
  884. tty.c_cflag |= CS7;
  885. break;
  886. case 6:
  887. tty.c_cflag |= CS6;
  888. break;
  889. case 5:
  890. tty.c_cflag |= CS5;
  891. break;
  892. }
  893. switch(parity) {
  894. default:
  895. case 'N':
  896. break;
  897. case 'E':
  898. tty.c_cflag |= PARENB;
  899. break;
  900. case 'O':
  901. tty.c_cflag |= PARENB | PARODD;
  902. break;
  903. }
  904. if (stop_bits == 2)
  905. tty.c_cflag |= CSTOPB;
  906. tcsetattr (fd, TCSANOW, &tty);
  907. }
  908. static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
  909. {
  910. FDCharDriver *s = chr->opaque;
  911. switch(cmd) {
  912. case CHR_IOCTL_SERIAL_SET_PARAMS:
  913. {
  914. QEMUSerialSetParams *ssp = arg;
  915. tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
  916. ssp->data_bits, ssp->stop_bits);
  917. }
  918. break;
  919. case CHR_IOCTL_SERIAL_SET_BREAK:
  920. {
  921. int enable = *(int *)arg;
  922. if (enable)
  923. tcsendbreak(s->fd_in, 1);
  924. }
  925. break;
  926. case CHR_IOCTL_SERIAL_GET_TIOCM:
  927. {
  928. int sarg = 0;
  929. int *targ = (int *)arg;
  930. ioctl(s->fd_in, TIOCMGET, &sarg);
  931. *targ = 0;
  932. if (sarg & TIOCM_CTS)
  933. *targ |= CHR_TIOCM_CTS;
  934. if (sarg & TIOCM_CAR)
  935. *targ |= CHR_TIOCM_CAR;
  936. if (sarg & TIOCM_DSR)
  937. *targ |= CHR_TIOCM_DSR;
  938. if (sarg & TIOCM_RI)
  939. *targ |= CHR_TIOCM_RI;
  940. if (sarg & TIOCM_DTR)
  941. *targ |= CHR_TIOCM_DTR;
  942. if (sarg & TIOCM_RTS)
  943. *targ |= CHR_TIOCM_RTS;
  944. }
  945. break;
  946. case CHR_IOCTL_SERIAL_SET_TIOCM:
  947. {
  948. int sarg = *(int *)arg;
  949. int targ = 0;
  950. ioctl(s->fd_in, TIOCMGET, &targ);
  951. targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
  952. | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
  953. if (sarg & CHR_TIOCM_CTS)
  954. targ |= TIOCM_CTS;
  955. if (sarg & CHR_TIOCM_CAR)
  956. targ |= TIOCM_CAR;
  957. if (sarg & CHR_TIOCM_DSR)
  958. targ |= TIOCM_DSR;
  959. if (sarg & CHR_TIOCM_RI)
  960. targ |= TIOCM_RI;
  961. if (sarg & CHR_TIOCM_DTR)
  962. targ |= TIOCM_DTR;
  963. if (sarg & CHR_TIOCM_RTS)
  964. targ |= TIOCM_RTS;
  965. ioctl(s->fd_in, TIOCMSET, &targ);
  966. }
  967. break;
  968. default:
  969. return -ENOTSUP;
  970. }
  971. return 0;
  972. }
  973. static CharDriverState *qemu_chr_open_tty(const char *filename)
  974. {
  975. CharDriverState *chr;
  976. int fd;
  977. TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
  978. tty_serial_init(fd, 115200, 'N', 8, 1);
  979. chr = qemu_chr_open_fd(fd, fd);
  980. if (!chr) {
  981. close(fd);
  982. return NULL;
  983. }
  984. chr->chr_ioctl = tty_serial_ioctl;
  985. qemu_chr_reset(chr);
  986. return chr;
  987. }
  988. #else /* ! __linux__ && ! __sun__ */
  989. static CharDriverState *qemu_chr_open_pty(void)
  990. {
  991. return NULL;
  992. }
  993. #endif /* __linux__ || __sun__ */
  994. #if defined(__linux__)
  995. typedef struct {
  996. int fd;
  997. int mode;
  998. } ParallelCharDriver;
  999. static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
  1000. {
  1001. if (s->mode != mode) {
  1002. int m = mode;
  1003. if (ioctl(s->fd, PPSETMODE, &m) < 0)
  1004. return 0;
  1005. s->mode = mode;
  1006. }
  1007. return 1;
  1008. }
  1009. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1010. {
  1011. ParallelCharDriver *drv = chr->opaque;
  1012. int fd = drv->fd;
  1013. uint8_t b;
  1014. switch(cmd) {
  1015. case CHR_IOCTL_PP_READ_DATA:
  1016. if (ioctl(fd, PPRDATA, &b) < 0)
  1017. return -ENOTSUP;
  1018. *(uint8_t *)arg = b;
  1019. break;
  1020. case CHR_IOCTL_PP_WRITE_DATA:
  1021. b = *(uint8_t *)arg;
  1022. if (ioctl(fd, PPWDATA, &b) < 0)
  1023. return -ENOTSUP;
  1024. break;
  1025. case CHR_IOCTL_PP_READ_CONTROL:
  1026. if (ioctl(fd, PPRCONTROL, &b) < 0)
  1027. return -ENOTSUP;
  1028. /* Linux gives only the lowest bits, and no way to know data
  1029. direction! For better compatibility set the fixed upper
  1030. bits. */
  1031. *(uint8_t *)arg = b | 0xc0;
  1032. break;
  1033. case CHR_IOCTL_PP_WRITE_CONTROL:
  1034. b = *(uint8_t *)arg;
  1035. if (ioctl(fd, PPWCONTROL, &b) < 0)
  1036. return -ENOTSUP;
  1037. break;
  1038. case CHR_IOCTL_PP_READ_STATUS:
  1039. if (ioctl(fd, PPRSTATUS, &b) < 0)
  1040. return -ENOTSUP;
  1041. *(uint8_t *)arg = b;
  1042. break;
  1043. case CHR_IOCTL_PP_DATA_DIR:
  1044. if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
  1045. return -ENOTSUP;
  1046. break;
  1047. case CHR_IOCTL_PP_EPP_READ_ADDR:
  1048. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1049. struct ParallelIOArg *parg = arg;
  1050. int n = read(fd, parg->buffer, parg->count);
  1051. if (n != parg->count) {
  1052. return -EIO;
  1053. }
  1054. }
  1055. break;
  1056. case CHR_IOCTL_PP_EPP_READ:
  1057. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1058. struct ParallelIOArg *parg = arg;
  1059. int n = read(fd, parg->buffer, parg->count);
  1060. if (n != parg->count) {
  1061. return -EIO;
  1062. }
  1063. }
  1064. break;
  1065. case CHR_IOCTL_PP_EPP_WRITE_ADDR:
  1066. if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
  1067. struct ParallelIOArg *parg = arg;
  1068. int n = write(fd, parg->buffer, parg->count);
  1069. if (n != parg->count) {
  1070. return -EIO;
  1071. }
  1072. }
  1073. break;
  1074. case CHR_IOCTL_PP_EPP_WRITE:
  1075. if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
  1076. struct ParallelIOArg *parg = arg;
  1077. int n = write(fd, parg->buffer, parg->count);
  1078. if (n != parg->count) {
  1079. return -EIO;
  1080. }
  1081. }
  1082. break;
  1083. default:
  1084. return -ENOTSUP;
  1085. }
  1086. return 0;
  1087. }
  1088. static void pp_close(CharDriverState *chr)
  1089. {
  1090. ParallelCharDriver *drv = chr->opaque;
  1091. int fd = drv->fd;
  1092. pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
  1093. ioctl(fd, PPRELEASE);
  1094. close(fd);
  1095. qemu_free(drv);
  1096. }
  1097. static CharDriverState *qemu_chr_open_pp(const char *filename)
  1098. {
  1099. CharDriverState *chr;
  1100. ParallelCharDriver *drv;
  1101. int fd;
  1102. TFR(fd = open(filename, O_RDWR));
  1103. if (fd < 0)
  1104. return NULL;
  1105. if (ioctl(fd, PPCLAIM) < 0) {
  1106. close(fd);
  1107. return NULL;
  1108. }
  1109. drv = qemu_mallocz(sizeof(ParallelCharDriver));
  1110. drv->fd = fd;
  1111. drv->mode = IEEE1284_MODE_COMPAT;
  1112. chr = qemu_mallocz(sizeof(CharDriverState));
  1113. chr->chr_write = null_chr_write;
  1114. chr->chr_ioctl = pp_ioctl;
  1115. chr->chr_close = pp_close;
  1116. chr->opaque = drv;
  1117. qemu_chr_reset(chr);
  1118. return chr;
  1119. }
  1120. #endif /* __linux__ */
  1121. #if defined(__FreeBSD__)
  1122. static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
  1123. {
  1124. int fd = (int)chr->opaque;
  1125. uint8_t b;
  1126. switch(cmd) {
  1127. case CHR_IOCTL_PP_READ_DATA:
  1128. if (ioctl(fd, PPIGDATA, &b) < 0)
  1129. return -ENOTSUP;
  1130. *(uint8_t *)arg = b;
  1131. break;
  1132. case CHR_IOCTL_PP_WRITE_DATA:
  1133. b = *(uint8_t *)arg;
  1134. if (ioctl(fd, PPISDATA, &b) < 0)
  1135. return -ENOTSUP;
  1136. break;
  1137. case CHR_IOCTL_PP_READ_CONTROL:
  1138. if (ioctl(fd, PPIGCTRL, &b) < 0)
  1139. return -ENOTSUP;
  1140. *(uint8_t *)arg = b;
  1141. break;
  1142. case CHR_IOCTL_PP_WRITE_CONTROL:
  1143. b = *(uint8_t *)arg;
  1144. if (ioctl(fd, PPISCTRL, &b) < 0)
  1145. return -ENOTSUP;
  1146. break;
  1147. case CHR_IOCTL_PP_READ_STATUS:
  1148. if (ioctl(fd, PPIGSTATUS, &b) < 0)
  1149. return -ENOTSUP;
  1150. *(uint8_t *)arg = b;
  1151. break;
  1152. default:
  1153. return -ENOTSUP;
  1154. }
  1155. return 0;
  1156. }
  1157. static CharDriverState *qemu_chr_open_pp(const char *filename)
  1158. {
  1159. CharDriverState *chr;
  1160. int fd;
  1161. fd = open(filename, O_RDWR);
  1162. if (fd < 0)
  1163. return NULL;
  1164. chr = qemu_mallocz(sizeof(CharDriverState));
  1165. chr->opaque = (void *)fd;
  1166. chr->chr_write = null_chr_write;
  1167. chr->chr_ioctl = pp_ioctl;
  1168. return chr;
  1169. }
  1170. #endif
  1171. #else /* _WIN32 */
  1172. typedef struct {
  1173. int max_size;
  1174. HANDLE hcom, hrecv, hsend;
  1175. OVERLAPPED orecv, osend;
  1176. BOOL fpipe;
  1177. DWORD len;
  1178. } WinCharState;
  1179. #define NSENDBUF 2048
  1180. #define NRECVBUF 2048
  1181. #define MAXCONNECT 1
  1182. #define NTIMEOUT 5000
  1183. static int win_chr_poll(void *opaque);
  1184. static int win_chr_pipe_poll(void *opaque);
  1185. static void win_chr_close(CharDriverState *chr)
  1186. {
  1187. WinCharState *s = chr->opaque;
  1188. if (s->hsend) {
  1189. CloseHandle(s->hsend);
  1190. s->hsend = NULL;
  1191. }
  1192. if (s->hrecv) {
  1193. CloseHandle(s->hrecv);
  1194. s->hrecv = NULL;
  1195. }
  1196. if (s->hcom) {
  1197. CloseHandle(s->hcom);
  1198. s->hcom = NULL;
  1199. }
  1200. if (s->fpipe)
  1201. qemu_del_polling_cb(win_chr_pipe_poll, chr);
  1202. else
  1203. qemu_del_polling_cb(win_chr_poll, chr);
  1204. }
  1205. static int win_chr_init(CharDriverState *chr, const char *filename)
  1206. {
  1207. WinCharState *s = chr->opaque;
  1208. COMMCONFIG comcfg;
  1209. COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
  1210. COMSTAT comstat;
  1211. DWORD size;
  1212. DWORD err;
  1213. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1214. if (!s->hsend) {
  1215. fprintf(stderr, "Failed CreateEvent\n");
  1216. goto fail;
  1217. }
  1218. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1219. if (!s->hrecv) {
  1220. fprintf(stderr, "Failed CreateEvent\n");
  1221. goto fail;
  1222. }
  1223. s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
  1224. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  1225. if (s->hcom == INVALID_HANDLE_VALUE) {
  1226. fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
  1227. s->hcom = NULL;
  1228. goto fail;
  1229. }
  1230. if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
  1231. fprintf(stderr, "Failed SetupComm\n");
  1232. goto fail;
  1233. }
  1234. ZeroMemory(&comcfg, sizeof(COMMCONFIG));
  1235. size = sizeof(COMMCONFIG);
  1236. GetDefaultCommConfig(filename, &comcfg, &size);
  1237. comcfg.dcb.DCBlength = sizeof(DCB);
  1238. CommConfigDialog(filename, NULL, &comcfg);
  1239. if (!SetCommState(s->hcom, &comcfg.dcb)) {
  1240. fprintf(stderr, "Failed SetCommState\n");
  1241. goto fail;
  1242. }
  1243. if (!SetCommMask(s->hcom, EV_ERR)) {
  1244. fprintf(stderr, "Failed SetCommMask\n");
  1245. goto fail;
  1246. }
  1247. cto.ReadIntervalTimeout = MAXDWORD;
  1248. if (!SetCommTimeouts(s->hcom, &cto)) {
  1249. fprintf(stderr, "Failed SetCommTimeouts\n");
  1250. goto fail;
  1251. }
  1252. if (!ClearCommError(s->hcom, &err, &comstat)) {
  1253. fprintf(stderr, "Failed ClearCommError\n");
  1254. goto fail;
  1255. }
  1256. qemu_add_polling_cb(win_chr_poll, chr);
  1257. return 0;
  1258. fail:
  1259. win_chr_close(chr);
  1260. return -1;
  1261. }
  1262. static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
  1263. {
  1264. WinCharState *s = chr->opaque;
  1265. DWORD len, ret, size, err;
  1266. len = len1;
  1267. ZeroMemory(&s->osend, sizeof(s->osend));
  1268. s->osend.hEvent = s->hsend;
  1269. while (len > 0) {
  1270. if (s->hsend)
  1271. ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
  1272. else
  1273. ret = WriteFile(s->hcom, buf, len, &size, NULL);
  1274. if (!ret) {
  1275. err = GetLastError();
  1276. if (err == ERROR_IO_PENDING) {
  1277. ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
  1278. if (ret) {
  1279. buf += size;
  1280. len -= size;
  1281. } else {
  1282. break;
  1283. }
  1284. } else {
  1285. break;
  1286. }
  1287. } else {
  1288. buf += size;
  1289. len -= size;
  1290. }
  1291. }
  1292. return len1 - len;
  1293. }
  1294. static int win_chr_read_poll(CharDriverState *chr)
  1295. {
  1296. WinCharState *s = chr->opaque;
  1297. s->max_size = qemu_chr_can_read(chr);
  1298. return s->max_size;
  1299. }
  1300. static void win_chr_readfile(CharDriverState *chr)
  1301. {
  1302. WinCharState *s = chr->opaque;
  1303. int ret, err;
  1304. uint8_t buf[1024];
  1305. DWORD size;
  1306. ZeroMemory(&s->orecv, sizeof(s->orecv));
  1307. s->orecv.hEvent = s->hrecv;
  1308. ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
  1309. if (!ret) {
  1310. err = GetLastError();
  1311. if (err == ERROR_IO_PENDING) {
  1312. ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
  1313. }
  1314. }
  1315. if (size > 0) {
  1316. qemu_chr_read(chr, buf, size);
  1317. }
  1318. }
  1319. static void win_chr_read(CharDriverState *chr)
  1320. {
  1321. WinCharState *s = chr->opaque;
  1322. if (s->len > s->max_size)
  1323. s->len = s->max_size;
  1324. if (s->len == 0)
  1325. return;
  1326. win_chr_readfile(chr);
  1327. }
  1328. static int win_chr_poll(void *opaque)
  1329. {
  1330. CharDriverState *chr = opaque;
  1331. WinCharState *s = chr->opaque;
  1332. COMSTAT status;
  1333. DWORD comerr;
  1334. ClearCommError(s->hcom, &comerr, &status);
  1335. if (status.cbInQue > 0) {
  1336. s->len = status.cbInQue;
  1337. win_chr_read_poll(chr);
  1338. win_chr_read(chr);
  1339. return 1;
  1340. }
  1341. return 0;
  1342. }
  1343. static CharDriverState *qemu_chr_open_win(const char *filename)
  1344. {
  1345. CharDriverState *chr;
  1346. WinCharState *s;
  1347. chr = qemu_mallocz(sizeof(CharDriverState));
  1348. s = qemu_mallocz(sizeof(WinCharState));
  1349. chr->opaque = s;
  1350. chr->chr_write = win_chr_write;
  1351. chr->chr_close = win_chr_close;
  1352. if (win_chr_init(chr, filename) < 0) {
  1353. free(s);
  1354. free(chr);
  1355. return NULL;
  1356. }
  1357. qemu_chr_reset(chr);
  1358. return chr;
  1359. }
  1360. static int win_chr_pipe_poll(void *opaque)
  1361. {
  1362. CharDriverState *chr = opaque;
  1363. WinCharState *s = chr->opaque;
  1364. DWORD size;
  1365. PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
  1366. if (size > 0) {
  1367. s->len = size;
  1368. win_chr_read_poll(chr);
  1369. win_chr_read(chr);
  1370. return 1;
  1371. }
  1372. return 0;
  1373. }
  1374. static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
  1375. {
  1376. WinCharState *s = chr->opaque;
  1377. OVERLAPPED ov;
  1378. int ret;
  1379. DWORD size;
  1380. char openname[256];
  1381. s->fpipe = TRUE;
  1382. s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
  1383. if (!s->hsend) {
  1384. fprintf(stderr, "Failed CreateEvent\n");
  1385. goto fail;
  1386. }
  1387. s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
  1388. if (!s->hrecv) {
  1389. fprintf(stderr, "Failed CreateEvent\n");
  1390. goto fail;
  1391. }
  1392. snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
  1393. s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  1394. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
  1395. PIPE_WAIT,
  1396. MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
  1397. if (s->hcom == INVALID_HANDLE_VALUE) {
  1398. fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
  1399. s->hcom = NULL;
  1400. goto fail;
  1401. }
  1402. ZeroMemory(&ov, sizeof(ov));
  1403. ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  1404. ret = ConnectNamedPipe(s->hcom, &ov);
  1405. if (ret) {
  1406. fprintf(stderr, "Failed ConnectNamedPipe\n");
  1407. goto fail;
  1408. }
  1409. ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
  1410. if (!ret) {
  1411. fprintf(stderr, "Failed GetOverlappedResult\n");
  1412. if (ov.hEvent) {
  1413. CloseHandle(ov.hEvent);
  1414. ov.hEvent = NULL;
  1415. }
  1416. goto fail;
  1417. }
  1418. if (ov.hEvent) {
  1419. CloseHandle(ov.hEvent);
  1420. ov.hEvent = NULL;
  1421. }
  1422. qemu_add_polling_cb(win_chr_pipe_poll, chr);
  1423. return 0;
  1424. fail:
  1425. win_chr_close(chr);
  1426. return -1;
  1427. }
  1428. static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
  1429. {
  1430. CharDriverState *chr;
  1431. WinCharState *s;
  1432. chr = qemu_mallocz(sizeof(CharDriverState));
  1433. s = qemu_mallocz(sizeof(WinCharState));
  1434. chr->opaque = s;
  1435. chr->chr_write = win_chr_write;
  1436. chr->chr_close = win_chr_close;
  1437. if (win_chr_pipe_init(chr, filename) < 0) {
  1438. free(s);
  1439. free(chr);
  1440. return NULL;
  1441. }
  1442. qemu_chr_reset(chr);
  1443. return chr;
  1444. }
  1445. static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
  1446. {
  1447. CharDriverState *chr;
  1448. WinCharState *s;
  1449. chr = qemu_mallocz(sizeof(CharDriverState));
  1450. s = qemu_mallocz(sizeof(WinCharState));
  1451. s->hcom = fd_out;
  1452. chr->opaque = s;
  1453. chr->chr_write = win_chr_write;
  1454. qemu_chr_reset(chr);
  1455. return chr;
  1456. }
  1457. static CharDriverState *qemu_chr_open_win_con(const char *filename)
  1458. {
  1459. return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
  1460. }
  1461. static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
  1462. {
  1463. HANDLE fd_out;
  1464. fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  1465. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  1466. if (fd_out == INVALID_HANDLE_VALUE)
  1467. return NULL;
  1468. return qemu_chr_open_win_file(fd_out);
  1469. }
  1470. #endif /* !_WIN32 */
  1471. /***********************************************************/
  1472. /* UDP Net console */
  1473. typedef struct {
  1474. int fd;
  1475. struct sockaddr_in daddr;
  1476. uint8_t buf[1024];
  1477. int bufcnt;
  1478. int bufptr;
  1479. int max_size;
  1480. } NetCharDriver;
  1481. static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1482. {
  1483. NetCharDriver *s = chr->opaque;
  1484. return sendto(s->fd, buf, len, 0,
  1485. (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
  1486. }
  1487. static int udp_chr_read_poll(void *opaque)
  1488. {
  1489. CharDriverState *chr = opaque;
  1490. NetCharDriver *s = chr->opaque;
  1491. s->max_size = qemu_chr_can_read(chr);
  1492. /* If there were any stray characters in the queue process them
  1493. * first
  1494. */
  1495. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1496. qemu_chr_read(chr, &s->buf[s->bufptr], 1);
  1497. s->bufptr++;
  1498. s->max_size = qemu_chr_can_read(chr);
  1499. }
  1500. return s->max_size;
  1501. }
  1502. static void udp_chr_read(void *opaque)
  1503. {
  1504. CharDriverState *chr = opaque;
  1505. NetCharDriver *s = chr->opaque;
  1506. if (s->max_size == 0)
  1507. return;
  1508. s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
  1509. s->bufptr = s->bufcnt;
  1510. if (s->bufcnt <= 0)
  1511. return;
  1512. s->bufptr = 0;
  1513. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  1514. qemu_chr_read(chr, &s->buf[s->bufptr], 1);
  1515. s->bufptr++;
  1516. s->max_size = qemu_chr_can_read(chr);
  1517. }
  1518. }
  1519. static void udp_chr_update_read_handler(CharDriverState *chr)
  1520. {
  1521. NetCharDriver *s = chr->opaque;
  1522. if (s->fd >= 0) {
  1523. qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
  1524. udp_chr_read, NULL, chr);
  1525. }
  1526. }
  1527. static void udp_chr_close(CharDriverState *chr)
  1528. {
  1529. NetCharDriver *s = chr->opaque;
  1530. if (s->fd >= 0) {
  1531. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1532. closesocket(s->fd);
  1533. }
  1534. qemu_free(s);
  1535. }
  1536. static CharDriverState *qemu_chr_open_udp(const char *def)
  1537. {
  1538. CharDriverState *chr = NULL;
  1539. NetCharDriver *s = NULL;
  1540. int fd = -1;
  1541. struct sockaddr_in saddr;
  1542. chr = qemu_mallocz(sizeof(CharDriverState));
  1543. s = qemu_mallocz(sizeof(NetCharDriver));
  1544. fd = socket(PF_INET, SOCK_DGRAM, 0);
  1545. if (fd < 0) {
  1546. perror("socket(PF_INET, SOCK_DGRAM)");
  1547. goto return_err;
  1548. }
  1549. if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
  1550. printf("Could not parse: %s\n", def);
  1551. goto return_err;
  1552. }
  1553. if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
  1554. {
  1555. perror("bind");
  1556. goto return_err;
  1557. }
  1558. s->fd = fd;
  1559. s->bufcnt = 0;
  1560. s->bufptr = 0;
  1561. chr->opaque = s;
  1562. chr->chr_write = udp_chr_write;
  1563. chr->chr_update_read_handler = udp_chr_update_read_handler;
  1564. chr->chr_close = udp_chr_close;
  1565. return chr;
  1566. return_err:
  1567. if (chr)
  1568. free(chr);
  1569. if (s)
  1570. free(s);
  1571. if (fd >= 0)
  1572. closesocket(fd);
  1573. return NULL;
  1574. }
  1575. /***********************************************************/
  1576. /* TCP Net console */
  1577. typedef struct {
  1578. int fd, listen_fd;
  1579. int connected;
  1580. int max_size;
  1581. int do_telnetopt;
  1582. int do_nodelay;
  1583. int is_unix;
  1584. } TCPCharDriver;
  1585. static void tcp_chr_accept(void *opaque);
  1586. static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  1587. {
  1588. TCPCharDriver *s = chr->opaque;
  1589. if (s->connected) {
  1590. return send_all(s->fd, buf, len);
  1591. } else {
  1592. /* XXX: indicate an error ? */
  1593. return len;
  1594. }
  1595. }
  1596. static int tcp_chr_read_poll(void *opaque)
  1597. {
  1598. CharDriverState *chr = opaque;
  1599. TCPCharDriver *s = chr->opaque;
  1600. if (!s->connected)
  1601. return 0;
  1602. s->max_size = qemu_chr_can_read(chr);
  1603. return s->max_size;
  1604. }
  1605. #define IAC 255
  1606. #define IAC_BREAK 243
  1607. static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
  1608. TCPCharDriver *s,
  1609. uint8_t *buf, int *size)
  1610. {
  1611. /* Handle any telnet client's basic IAC options to satisfy char by
  1612. * char mode with no echo. All IAC options will be removed from
  1613. * the buf and the do_telnetopt variable will be used to track the
  1614. * state of the width of the IAC information.
  1615. *
  1616. * IAC commands come in sets of 3 bytes with the exception of the
  1617. * "IAC BREAK" command and the double IAC.
  1618. */
  1619. int i;
  1620. int j = 0;
  1621. for (i = 0; i < *size; i++) {
  1622. if (s->do_telnetopt > 1) {
  1623. if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
  1624. /* Double IAC means send an IAC */
  1625. if (j != i)
  1626. buf[j] = buf[i];
  1627. j++;
  1628. s->do_telnetopt = 1;
  1629. } else {
  1630. if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
  1631. /* Handle IAC break commands by sending a serial break */
  1632. qemu_chr_event(chr, CHR_EVENT_BREAK);
  1633. s->do_telnetopt++;
  1634. }
  1635. s->do_telnetopt++;
  1636. }
  1637. if (s->do_telnetopt >= 4) {
  1638. s->do_telnetopt = 1;
  1639. }
  1640. } else {
  1641. if ((unsigned char)buf[i] == IAC) {
  1642. s->do_telnetopt = 2;
  1643. } else {
  1644. if (j != i)
  1645. buf[j] = buf[i];
  1646. j++;
  1647. }
  1648. }
  1649. }
  1650. *size = j;
  1651. }
  1652. static void tcp_chr_read(void *opaque)
  1653. {
  1654. CharDriverState *chr = opaque;
  1655. TCPCharDriver *s = chr->opaque;
  1656. uint8_t buf[1024];
  1657. int len, size;
  1658. if (!s->connected || s->max_size <= 0)
  1659. return;
  1660. len = sizeof(buf);
  1661. if (len > s->max_size)
  1662. len = s->max_size;
  1663. size = recv(s->fd, buf, len, 0);
  1664. if (size == 0) {
  1665. /* connection closed */
  1666. s->connected = 0;
  1667. if (s->listen_fd >= 0) {
  1668. qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
  1669. }
  1670. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1671. closesocket(s->fd);
  1672. s->fd = -1;
  1673. } else if (size > 0) {
  1674. if (s->do_telnetopt)
  1675. tcp_chr_process_IAC_bytes(chr, s, buf, &size);
  1676. if (size > 0)
  1677. qemu_chr_read(chr, buf, size);
  1678. }
  1679. }
  1680. static void tcp_chr_connect(void *opaque)
  1681. {
  1682. CharDriverState *chr = opaque;
  1683. TCPCharDriver *s = chr->opaque;
  1684. s->connected = 1;
  1685. qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
  1686. tcp_chr_read, NULL, chr);
  1687. qemu_chr_reset(chr);
  1688. }
  1689. #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
  1690. static void tcp_chr_telnet_init(int fd)
  1691. {
  1692. char buf[3];
  1693. /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
  1694. IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
  1695. send(fd, (char *)buf, 3, 0);
  1696. IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
  1697. send(fd, (char *)buf, 3, 0);
  1698. IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
  1699. send(fd, (char *)buf, 3, 0);
  1700. IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
  1701. send(fd, (char *)buf, 3, 0);
  1702. }
  1703. static void socket_set_nodelay(int fd)
  1704. {
  1705. int val = 1;
  1706. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
  1707. }
  1708. static void tcp_chr_accept(void *opaque)
  1709. {
  1710. CharDriverState *chr = opaque;
  1711. TCPCharDriver *s = chr->opaque;
  1712. struct sockaddr_in saddr;
  1713. #ifndef _WIN32
  1714. struct sockaddr_un uaddr;
  1715. #endif
  1716. struct sockaddr *addr;
  1717. socklen_t len;
  1718. int fd;
  1719. for(;;) {
  1720. #ifndef _WIN32
  1721. if (s->is_unix) {
  1722. len = sizeof(uaddr);
  1723. addr = (struct sockaddr *)&uaddr;
  1724. } else
  1725. #endif
  1726. {
  1727. len = sizeof(saddr);
  1728. addr = (struct sockaddr *)&saddr;
  1729. }
  1730. fd = accept(s->listen_fd, addr, &len);
  1731. if (fd < 0 && errno != EINTR) {
  1732. return;
  1733. } else if (fd >= 0) {
  1734. if (s->do_telnetopt)
  1735. tcp_chr_telnet_init(fd);
  1736. break;
  1737. }
  1738. }
  1739. socket_set_nonblock(fd);
  1740. if (s->do_nodelay)
  1741. socket_set_nodelay(fd);
  1742. s->fd = fd;
  1743. qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
  1744. tcp_chr_connect(chr);
  1745. }
  1746. static void tcp_chr_close(CharDriverState *chr)
  1747. {
  1748. TCPCharDriver *s = chr->opaque;
  1749. if (s->fd >= 0) {
  1750. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1751. closesocket(s->fd);
  1752. }
  1753. if (s->listen_fd >= 0) {
  1754. qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
  1755. closesocket(s->listen_fd);
  1756. }
  1757. qemu_free(s);
  1758. }
  1759. static CharDriverState *qemu_chr_open_tcp(const char *host_str,
  1760. int is_telnet,
  1761. int is_unix)
  1762. {
  1763. CharDriverState *chr = NULL;
  1764. TCPCharDriver *s = NULL;
  1765. int fd = -1, offset = 0;
  1766. int is_listen = 0;
  1767. int is_waitconnect = 1;
  1768. int do_nodelay = 0;
  1769. const char *ptr;
  1770. ptr = host_str;
  1771. while((ptr = strchr(ptr,','))) {
  1772. ptr++;
  1773. if (!strncmp(ptr,"server",6)) {
  1774. is_listen = 1;
  1775. } else if (!strncmp(ptr,"nowait",6)) {
  1776. is_waitconnect = 0;
  1777. } else if (!strncmp(ptr,"nodelay",6)) {
  1778. do_nodelay = 1;
  1779. } else if (!strncmp(ptr,"to=",3)) {
  1780. /* nothing, inet_listen() parses this one */;
  1781. } else if (!strncmp(ptr,"ipv4",4)) {
  1782. /* nothing, inet_connect() and inet_listen() parse this one */;
  1783. } else if (!strncmp(ptr,"ipv6",4)) {
  1784. /* nothing, inet_connect() and inet_listen() parse this one */;
  1785. } else {
  1786. printf("Unknown option: %s\n", ptr);
  1787. goto fail;
  1788. }
  1789. }
  1790. if (!is_listen)
  1791. is_waitconnect = 0;
  1792. chr = qemu_mallocz(sizeof(CharDriverState));
  1793. s = qemu_mallocz(sizeof(TCPCharDriver));
  1794. if (is_listen) {
  1795. chr->filename = qemu_malloc(256);
  1796. if (is_unix) {
  1797. pstrcpy(chr->filename, 256, "unix:");
  1798. } else if (is_telnet) {
  1799. pstrcpy(chr->filename, 256, "telnet:");
  1800. } else {
  1801. pstrcpy(chr->filename, 256, "tcp:");
  1802. }
  1803. offset = strlen(chr->filename);
  1804. }
  1805. if (is_unix) {
  1806. if (is_listen) {
  1807. fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
  1808. } else {
  1809. fd = unix_connect(host_str);
  1810. }
  1811. } else {
  1812. if (is_listen) {
  1813. fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
  1814. SOCK_STREAM, 0);
  1815. } else {
  1816. fd = inet_connect(host_str, SOCK_STREAM);
  1817. }
  1818. }
  1819. if (fd < 0)
  1820. goto fail;
  1821. if (!is_waitconnect)
  1822. socket_set_nonblock(fd);
  1823. s->connected = 0;
  1824. s->fd = -1;
  1825. s->listen_fd = -1;
  1826. s->is_unix = is_unix;
  1827. s->do_nodelay = do_nodelay && !is_unix;
  1828. chr->opaque = s;
  1829. chr->chr_write = tcp_chr_write;
  1830. chr->chr_close = tcp_chr_close;
  1831. if (is_listen) {
  1832. s->listen_fd = fd;
  1833. qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
  1834. if (is_telnet)
  1835. s->do_telnetopt = 1;
  1836. } else {
  1837. s->connected = 1;
  1838. s->fd = fd;
  1839. socket_set_nodelay(fd);
  1840. tcp_chr_connect(chr);
  1841. }
  1842. if (is_listen && is_waitconnect) {
  1843. printf("QEMU waiting for connection on: %s\n",
  1844. chr->filename ? chr->filename : host_str);
  1845. tcp_chr_accept(chr);
  1846. socket_set_nonblock(s->listen_fd);
  1847. }
  1848. return chr;
  1849. fail:
  1850. if (fd >= 0)
  1851. closesocket(fd);
  1852. qemu_free(s);
  1853. qemu_free(chr);
  1854. return NULL;
  1855. }
  1856. CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
  1857. {
  1858. const char *p;
  1859. CharDriverState *chr;
  1860. if (!strcmp(filename, "vc")) {
  1861. chr = text_console_init(0);
  1862. } else
  1863. if (strstart(filename, "vc:", &p)) {
  1864. chr = text_console_init(p);
  1865. } else
  1866. if (!strcmp(filename, "null")) {
  1867. chr = qemu_chr_open_null();
  1868. } else
  1869. if (strstart(filename, "tcp:", &p)) {
  1870. chr = qemu_chr_open_tcp(p, 0, 0);
  1871. } else
  1872. if (strstart(filename, "telnet:", &p)) {
  1873. chr = qemu_chr_open_tcp(p, 1, 0);
  1874. } else
  1875. if (strstart(filename, "udp:", &p)) {
  1876. chr = qemu_chr_open_udp(p);
  1877. } else
  1878. if (strstart(filename, "mon:", &p)) {
  1879. chr = qemu_chr_open(label, p, NULL);
  1880. if (chr) {
  1881. chr = qemu_chr_open_mux(chr);
  1882. monitor_init(chr, !nographic);
  1883. } else {
  1884. printf("Unable to open driver: %s\n", p);
  1885. }
  1886. } else if (!strcmp(filename, "msmouse")) {
  1887. chr = qemu_chr_open_msmouse();
  1888. } else
  1889. #ifndef _WIN32
  1890. if (strstart(filename, "unix:", &p)) {
  1891. chr = qemu_chr_open_tcp(p, 0, 1);
  1892. } else if (strstart(filename, "file:", &p)) {
  1893. chr = qemu_chr_open_file_out(p);
  1894. } else if (strstart(filename, "pipe:", &p)) {
  1895. chr = qemu_chr_open_pipe(p);
  1896. } else if (!strcmp(filename, "pty")) {
  1897. chr = qemu_chr_open_pty();
  1898. } else if (!strcmp(filename, "stdio")) {
  1899. chr = qemu_chr_open_stdio();
  1900. } else
  1901. #if defined(__linux__)
  1902. if (strstart(filename, "/dev/parport", NULL)) {
  1903. chr = qemu_chr_open_pp(filename);
  1904. } else
  1905. #elif defined(__FreeBSD__)
  1906. if (strstart(filename, "/dev/ppi", NULL)) {
  1907. chr = qemu_chr_open_pp(filename);
  1908. } else
  1909. #endif
  1910. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  1911. || defined(__NetBSD__) || defined(__OpenBSD__)
  1912. if (strstart(filename, "/dev/", NULL)) {
  1913. chr = qemu_chr_open_tty(filename);
  1914. } else
  1915. #endif
  1916. #else /* !_WIN32 */
  1917. if (strstart(filename, "COM", NULL)) {
  1918. chr = qemu_chr_open_win(filename);
  1919. } else
  1920. if (strstart(filename, "pipe:", &p)) {
  1921. chr = qemu_chr_open_win_pipe(p);
  1922. } else
  1923. if (strstart(filename, "con:", NULL)) {
  1924. chr = qemu_chr_open_win_con(filename);
  1925. } else
  1926. if (strstart(filename, "file:", &p)) {
  1927. chr = qemu_chr_open_win_file_out(p);
  1928. } else
  1929. #endif
  1930. #ifdef CONFIG_BRLAPI
  1931. if (!strcmp(filename, "braille")) {
  1932. chr = chr_baum_init();
  1933. } else
  1934. #endif
  1935. {
  1936. chr = NULL;
  1937. }
  1938. if (chr) {
  1939. if (!chr->filename)
  1940. chr->filename = qemu_strdup(filename);
  1941. chr->init = init;
  1942. chr->label = qemu_strdup(label);
  1943. TAILQ_INSERT_TAIL(&chardevs, chr, next);
  1944. }
  1945. return chr;
  1946. }
  1947. void qemu_chr_close(CharDriverState *chr)
  1948. {
  1949. TAILQ_REMOVE(&chardevs, chr, next);
  1950. if (chr->chr_close)
  1951. chr->chr_close(chr);
  1952. qemu_free(chr->filename);
  1953. qemu_free(chr->label);
  1954. qemu_free(chr);
  1955. }
  1956. void qemu_chr_info(void)
  1957. {
  1958. CharDriverState *chr;
  1959. TAILQ_FOREACH(chr, &chardevs, next) {
  1960. term_printf("%s: filename=%s\n", chr->label, chr->filename);
  1961. }
  1962. }