console.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. /*
  2. * Server-side console management
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. * 2001 Eric Pouech
  6. * Copyright 2020 Jacek Caban for CodeWeavers
  7. *
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "config.h"
  24. #include <assert.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <sys/ioctl.h>
  30. #include <termios.h>
  31. #include "ntstatus.h"
  32. #define WIN32_NO_STATUS
  33. #include "handle.h"
  34. #include "process.h"
  35. #include "request.h"
  36. #include "file.h"
  37. #include "unicode.h"
  38. #include "wincon.h"
  39. #include "winternl.h"
  40. #include "wine/condrv.h"
  41. struct screen_buffer;
  42. struct history_line
  43. {
  44. data_size_t len;
  45. WCHAR text[1];
  46. };
  47. struct console
  48. {
  49. struct object obj; /* object header */
  50. int signaled; /* is console signaled */
  51. struct thread *renderer; /* console renderer thread */
  52. struct screen_buffer *active; /* active screen buffer */
  53. struct console_server *server; /* console server object */
  54. unsigned int last_id; /* id of last created console buffer */
  55. struct fd *fd; /* for bare console, attached input fd */
  56. struct async_queue ioctl_q; /* ioctl queue */
  57. struct async_queue read_q; /* read queue */
  58. };
  59. static void console_dump( struct object *obj, int verbose );
  60. static void console_destroy( struct object *obj );
  61. static int console_signaled( struct object *obj, struct wait_queue_entry *entry );
  62. static struct fd *console_get_fd( struct object *obj );
  63. static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
  64. unsigned int attr, struct object *root );
  65. static struct object *console_open_file( struct object *obj, unsigned int access,
  66. unsigned int sharing, unsigned int options );
  67. static int console_add_queue( struct object *obj, struct wait_queue_entry *entry );
  68. static const struct object_ops console_ops =
  69. {
  70. sizeof(struct console), /* size */
  71. &file_type, /* type */
  72. console_dump, /* dump */
  73. console_add_queue, /* add_queue */
  74. remove_queue, /* remove_queue */
  75. console_signaled, /* signaled */
  76. no_satisfied, /* satisfied */
  77. no_signal, /* signal */
  78. console_get_fd, /* get_fd */
  79. default_map_access, /* map_access */
  80. default_get_sd, /* get_sd */
  81. default_set_sd, /* set_sd */
  82. no_get_full_name, /* get_full_name */
  83. console_lookup_name, /* lookup_name */
  84. no_link_name, /* link_name */
  85. NULL, /* unlink_name */
  86. console_open_file, /* open_file */
  87. no_kernel_obj_list, /* get_kernel_obj_list */
  88. no_close_handle, /* close_handle */
  89. console_destroy /* destroy */
  90. };
  91. static enum server_fd_type console_get_fd_type( struct fd *fd );
  92. static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
  93. static void console_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class );
  94. static void console_read( struct fd *fd, struct async *async, file_pos_t pos );
  95. static void console_flush( struct fd *fd, struct async *async );
  96. static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  97. static const struct fd_ops console_fd_ops =
  98. {
  99. default_fd_get_poll_events, /* get_poll_events */
  100. default_poll_event, /* poll_event */
  101. console_get_fd_type, /* get_fd_type */
  102. console_read, /* read */
  103. no_fd_write, /* write */
  104. console_flush, /* flush */
  105. console_get_file_info, /* get_file_info */
  106. console_get_volume_info, /* get_volume_info */
  107. console_ioctl, /* ioctl */
  108. default_fd_cancel_async, /* cancel_async */
  109. default_fd_queue_async, /* queue_async */
  110. default_fd_reselect_async /* reselect_async */
  111. };
  112. struct console_host_ioctl
  113. {
  114. unsigned int code; /* ioctl code */
  115. int output; /* output id for screen buffer ioctls */
  116. struct async *async; /* ioctl async */
  117. struct list entry; /* list entry */
  118. };
  119. struct console_server
  120. {
  121. struct object obj; /* object header */
  122. struct fd *fd; /* pseudo-fd for ioctls */
  123. struct console *console; /* attached console */
  124. struct list queue; /* ioctl queue */
  125. struct list read_queue; /* blocking read queue */
  126. unsigned int busy : 1; /* flag if server processing an ioctl */
  127. unsigned int once_input : 1; /* flag if input thread has already been requested */
  128. int term_fd; /* UNIX terminal fd */
  129. struct termios termios; /* original termios */
  130. };
  131. static void console_server_dump( struct object *obj, int verbose );
  132. static void console_server_destroy( struct object *obj );
  133. static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
  134. static struct fd *console_server_get_fd( struct object *obj );
  135. static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
  136. unsigned int attr, struct object *root );
  137. static struct object *console_server_open_file( struct object *obj, unsigned int access,
  138. unsigned int sharing, unsigned int options );
  139. static const struct object_ops console_server_ops =
  140. {
  141. sizeof(struct console_server), /* size */
  142. &file_type, /* type */
  143. console_server_dump, /* dump */
  144. add_queue, /* add_queue */
  145. remove_queue, /* remove_queue */
  146. console_server_signaled, /* signaled */
  147. no_satisfied, /* satisfied */
  148. no_signal, /* signal */
  149. console_server_get_fd, /* get_fd */
  150. default_map_access, /* map_access */
  151. default_get_sd, /* get_sd */
  152. default_set_sd, /* set_sd */
  153. no_get_full_name, /* get_full_name */
  154. console_server_lookup_name, /* lookup_name */
  155. no_link_name, /* link_name */
  156. NULL, /* unlink_name */
  157. console_server_open_file, /* open_file */
  158. no_kernel_obj_list, /* get_kernel_obj_list */
  159. no_close_handle, /* close_handle */
  160. console_server_destroy /* destroy */
  161. };
  162. static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  163. static const struct fd_ops console_server_fd_ops =
  164. {
  165. default_fd_get_poll_events, /* get_poll_events */
  166. default_poll_event, /* poll_event */
  167. console_get_fd_type, /* get_fd_type */
  168. no_fd_read, /* read */
  169. no_fd_write, /* write */
  170. no_fd_flush, /* flush */
  171. no_fd_get_file_info, /* get_file_info */
  172. no_fd_get_volume_info, /* get_volume_info */
  173. console_server_ioctl, /* ioctl */
  174. default_fd_cancel_async, /* cancel_async */
  175. default_fd_queue_async, /* queue_async */
  176. default_fd_reselect_async /* reselect_async */
  177. };
  178. struct font_info
  179. {
  180. short int width;
  181. short int height;
  182. short int weight;
  183. short int pitch_family;
  184. WCHAR *face_name;
  185. data_size_t face_len;
  186. };
  187. struct screen_buffer
  188. {
  189. struct object obj; /* object header */
  190. struct list entry; /* entry in list of all screen buffers */
  191. struct console *input; /* associated console input */
  192. unsigned int id; /* buffer id */
  193. struct fd *fd; /* for bare console, attached output fd */
  194. struct async_queue ioctl_q; /* ioctl queue */
  195. };
  196. static void screen_buffer_dump( struct object *obj, int verbose );
  197. static void screen_buffer_destroy( struct object *obj );
  198. static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
  199. static struct fd *screen_buffer_get_fd( struct object *obj );
  200. static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
  201. unsigned int sharing, unsigned int options );
  202. static const struct object_ops screen_buffer_ops =
  203. {
  204. sizeof(struct screen_buffer), /* size */
  205. &file_type, /* type */
  206. screen_buffer_dump, /* dump */
  207. screen_buffer_add_queue, /* add_queue */
  208. NULL, /* remove_queue */
  209. NULL, /* signaled */
  210. NULL, /* satisfied */
  211. no_signal, /* signal */
  212. screen_buffer_get_fd, /* get_fd */
  213. default_map_access, /* map_access */
  214. default_get_sd, /* get_sd */
  215. default_set_sd, /* set_sd */
  216. no_get_full_name, /* get_full_name */
  217. no_lookup_name, /* lookup_name */
  218. no_link_name, /* link_name */
  219. NULL, /* unlink_name */
  220. screen_buffer_open_file, /* open_file */
  221. no_kernel_obj_list, /* get_kernel_obj_list */
  222. no_close_handle, /* close_handle */
  223. screen_buffer_destroy /* destroy */
  224. };
  225. static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos );
  226. static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  227. static const struct fd_ops screen_buffer_fd_ops =
  228. {
  229. default_fd_get_poll_events, /* get_poll_events */
  230. default_poll_event, /* poll_event */
  231. console_get_fd_type, /* get_fd_type */
  232. no_fd_read, /* read */
  233. screen_buffer_write, /* write */
  234. no_fd_flush, /* flush */
  235. console_get_file_info, /* get_file_info */
  236. console_get_volume_info, /* get_volume_info */
  237. screen_buffer_ioctl, /* ioctl */
  238. default_fd_cancel_async, /* cancel_async */
  239. default_fd_queue_async, /* queue_async */
  240. default_fd_reselect_async /* reselect_async */
  241. };
  242. static void console_device_dump( struct object *obj, int verbose );
  243. static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
  244. unsigned int attr, struct object *root );
  245. static struct object *console_device_open_file( struct object *obj, unsigned int access,
  246. unsigned int sharing, unsigned int options );
  247. static const struct object_ops console_device_ops =
  248. {
  249. sizeof(struct object), /* size */
  250. &device_type, /* type */
  251. console_device_dump, /* dump */
  252. no_add_queue, /* add_queue */
  253. NULL, /* remove_queue */
  254. NULL, /* signaled */
  255. no_satisfied, /* satisfied */
  256. no_signal, /* signal */
  257. no_get_fd, /* get_fd */
  258. default_map_access, /* map_access */
  259. default_get_sd, /* get_sd */
  260. default_set_sd, /* set_sd */
  261. default_get_full_name, /* get_full_name */
  262. console_device_lookup_name, /* lookup_name */
  263. directory_link_name, /* link_name */
  264. default_unlink_name, /* unlink_name */
  265. console_device_open_file, /* open_file */
  266. no_kernel_obj_list, /* get_kernel_obj_list */
  267. no_close_handle, /* close_handle */
  268. no_destroy /* destroy */
  269. };
  270. struct console_input
  271. {
  272. struct object obj; /* object header */
  273. struct fd *fd; /* pseudo-fd */
  274. };
  275. static void console_input_dump( struct object *obj, int verbose );
  276. static struct object *console_input_open_file( struct object *obj, unsigned int access,
  277. unsigned int sharing, unsigned int options );
  278. static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry );
  279. static struct fd *console_input_get_fd( struct object *obj );
  280. static void console_input_destroy( struct object *obj );
  281. static const struct object_ops console_input_ops =
  282. {
  283. sizeof(struct console_input), /* size */
  284. &device_type, /* type */
  285. console_input_dump, /* dump */
  286. console_input_add_queue, /* add_queue */
  287. NULL, /* remove_queue */
  288. NULL, /* signaled */
  289. no_satisfied, /* satisfied */
  290. no_signal, /* signal */
  291. console_input_get_fd, /* get_fd */
  292. default_map_access, /* map_access */
  293. default_get_sd, /* get_sd */
  294. default_set_sd, /* set_sd */
  295. no_get_full_name, /* get_full_name */
  296. no_lookup_name, /* lookup_name */
  297. directory_link_name, /* link_name */
  298. default_unlink_name, /* unlink_name */
  299. console_input_open_file, /* open_file */
  300. no_kernel_obj_list, /* get_kernel_obj_list */
  301. no_close_handle, /* close_handle */
  302. console_input_destroy /* destroy */
  303. };
  304. static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos );
  305. static void console_input_flush( struct fd *fd, struct async *async );
  306. static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  307. static const struct fd_ops console_input_fd_ops =
  308. {
  309. default_fd_get_poll_events, /* get_poll_events */
  310. default_poll_event, /* poll_event */
  311. console_get_fd_type, /* get_fd_type */
  312. console_input_read, /* read */
  313. no_fd_write, /* write */
  314. console_input_flush, /* flush */
  315. console_get_file_info, /* get_file_info */
  316. console_get_volume_info, /* get_volume_info */
  317. console_input_ioctl, /* ioctl */
  318. default_fd_cancel_async, /* cancel_async */
  319. default_fd_queue_async, /* queue_async */
  320. default_fd_reselect_async /* reselect_async */
  321. };
  322. struct console_output
  323. {
  324. struct object obj; /* object header */
  325. struct fd *fd; /* pseudo-fd */
  326. };
  327. static void console_output_dump( struct object *obj, int verbose );
  328. static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry );
  329. static struct fd *console_output_get_fd( struct object *obj );
  330. static struct object *console_output_open_file( struct object *obj, unsigned int access,
  331. unsigned int sharing, unsigned int options );
  332. static void console_output_destroy( struct object *obj );
  333. static const struct object_ops console_output_ops =
  334. {
  335. sizeof(struct console_output), /* size */
  336. &device_type, /* type */
  337. console_output_dump, /* dump */
  338. console_output_add_queue, /* add_queue */
  339. NULL, /* remove_queue */
  340. NULL, /* signaled */
  341. no_satisfied, /* satisfied */
  342. no_signal, /* signal */
  343. console_output_get_fd, /* get_fd */
  344. default_map_access, /* map_access */
  345. default_get_sd, /* get_sd */
  346. default_set_sd, /* set_sd */
  347. no_get_full_name, /* get_full_name */
  348. no_lookup_name, /* lookup_name */
  349. directory_link_name, /* link_name */
  350. default_unlink_name, /* unlink_name */
  351. console_output_open_file, /* open_file */
  352. no_kernel_obj_list, /* get_kernel_obj_list */
  353. no_close_handle, /* close_handle */
  354. console_output_destroy /* destroy */
  355. };
  356. static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos );
  357. static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  358. static const struct fd_ops console_output_fd_ops =
  359. {
  360. default_fd_get_poll_events, /* get_poll_events */
  361. default_poll_event, /* poll_event */
  362. console_get_fd_type, /* get_fd_type */
  363. no_fd_read, /* read */
  364. console_output_write, /* write */
  365. no_fd_flush, /* flush */
  366. console_get_file_info, /* get_file_info */
  367. console_get_volume_info, /* get_volume_info */
  368. console_output_ioctl, /* ioctl */
  369. default_fd_cancel_async, /* cancel_async */
  370. default_fd_queue_async, /* queue_async */
  371. default_fd_reselect_async /* reselect_async */
  372. };
  373. struct console_connection
  374. {
  375. struct object obj; /* object header */
  376. struct fd *fd; /* pseudo-fd for ioctls */
  377. };
  378. static void console_connection_dump( struct object *obj, int verbose );
  379. static struct fd *console_connection_get_fd( struct object *obj );
  380. static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
  381. unsigned int attr, struct object *root );
  382. static struct object *console_connection_open_file( struct object *obj, unsigned int access,
  383. unsigned int sharing, unsigned int options );
  384. static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
  385. static void console_connection_destroy( struct object *obj );
  386. static const struct object_ops console_connection_ops =
  387. {
  388. sizeof(struct console_connection),/* size */
  389. &device_type, /* type */
  390. console_connection_dump, /* dump */
  391. no_add_queue, /* add_queue */
  392. NULL, /* remove_queue */
  393. NULL, /* signaled */
  394. no_satisfied, /* satisfied */
  395. no_signal, /* signal */
  396. console_connection_get_fd, /* get_fd */
  397. default_map_access, /* map_access */
  398. default_get_sd, /* get_sd */
  399. default_set_sd, /* set_sd */
  400. no_get_full_name, /* get_full_name */
  401. console_connection_lookup_name, /* lookup_name */
  402. directory_link_name, /* link_name */
  403. default_unlink_name, /* unlink_name */
  404. console_connection_open_file, /* open_file */
  405. no_kernel_obj_list, /* get_kernel_obj_list */
  406. console_connection_close_handle, /* close_handle */
  407. console_connection_destroy /* destroy */
  408. };
  409. static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
  410. static const struct fd_ops console_connection_fd_ops =
  411. {
  412. default_fd_get_poll_events, /* get_poll_events */
  413. default_poll_event, /* poll_event */
  414. console_get_fd_type, /* get_fd_type */
  415. no_fd_read, /* read */
  416. no_fd_write, /* write */
  417. no_fd_flush, /* flush */
  418. no_fd_get_file_info, /* get_file_info */
  419. no_fd_get_volume_info, /* get_volume_info */
  420. console_connection_ioctl, /* ioctl */
  421. default_fd_cancel_async, /* cancel_async */
  422. default_fd_queue_async, /* queue_async */
  423. default_fd_reselect_async /* reselect_async */
  424. };
  425. static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
  426. static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
  427. struct async *async, struct async_queue *queue );
  428. static int console_add_queue( struct object *obj, struct wait_queue_entry *entry )
  429. {
  430. struct console *console = (struct console*)obj;
  431. assert( obj->ops == &console_ops );
  432. /* before waiting, ensure conhost's input thread has been started */
  433. if (console->server && !console->server->once_input)
  434. {
  435. console->server->once_input = 1;
  436. if (console->server->term_fd == -1)
  437. queue_host_ioctl( console->server, IOCTL_CONDRV_PEEK, 0, NULL, NULL );
  438. }
  439. return add_queue( &console->obj, entry );
  440. }
  441. static int console_signaled( struct object *obj, struct wait_queue_entry *entry )
  442. {
  443. struct console *console = (struct console*)obj;
  444. return console->signaled;
  445. }
  446. static struct fd *console_get_fd( struct object *obj )
  447. {
  448. struct console *console = (struct console *)obj;
  449. assert( obj->ops == &console_ops );
  450. return (struct fd *)grab_object( console->fd );
  451. }
  452. static enum server_fd_type console_get_fd_type( struct fd *fd )
  453. {
  454. return FD_TYPE_CHAR;
  455. }
  456. static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
  457. {
  458. set_error( STATUS_INVALID_DEVICE_REQUEST );
  459. }
  460. static void console_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
  461. {
  462. switch (info_class)
  463. {
  464. case FileFsDeviceInformation:
  465. {
  466. static const FILE_FS_DEVICE_INFORMATION device_info =
  467. {
  468. FILE_DEVICE_CONSOLE,
  469. FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
  470. };
  471. if (get_reply_max_size() >= sizeof(device_info))
  472. set_reply_data( &device_info, sizeof(device_info) );
  473. else
  474. set_error( STATUS_BUFFER_TOO_SMALL );
  475. break;
  476. }
  477. default:
  478. set_error( STATUS_NOT_IMPLEMENTED );
  479. }
  480. }
  481. static struct object *create_console(void)
  482. {
  483. struct console *console;
  484. if (!(console = alloc_object( &console_ops )))
  485. return NULL;
  486. console->renderer = NULL;
  487. console->signaled = 0;
  488. console->active = NULL;
  489. console->server = NULL;
  490. console->fd = NULL;
  491. console->last_id = 0;
  492. init_async_queue( &console->ioctl_q );
  493. init_async_queue( &console->read_q );
  494. console->fd = alloc_pseudo_fd( &console_fd_ops, &console->obj, FILE_SYNCHRONOUS_IO_NONALERT );
  495. if (!console->fd)
  496. {
  497. release_object( console );
  498. return NULL;
  499. }
  500. allow_fd_caching( console->fd );
  501. return &console->obj;
  502. }
  503. static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
  504. {
  505. if (call->async)
  506. {
  507. async_terminate( call->async, status );
  508. release_object( call->async );
  509. }
  510. free( call );
  511. }
  512. static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
  513. struct async *async, struct async_queue *queue )
  514. {
  515. struct console_host_ioctl *ioctl;
  516. if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
  517. ioctl->code = code;
  518. ioctl->output = output;
  519. ioctl->async = NULL;
  520. if (async)
  521. {
  522. ioctl->async = (struct async *)grab_object( async );
  523. queue_async( queue, async );
  524. }
  525. list_add_tail( &server->queue, &ioctl->entry );
  526. wake_up( &server->obj, 0 );
  527. if (async) set_error( STATUS_PENDING );
  528. return 1;
  529. }
  530. static void disconnect_console_server( struct console_server *server )
  531. {
  532. while (!list_empty( &server->queue ))
  533. {
  534. struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  535. list_remove( &call->entry );
  536. console_host_ioctl_terminate( call, STATUS_CANCELLED );
  537. }
  538. while (!list_empty( &server->read_queue ))
  539. {
  540. struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
  541. list_remove( &call->entry );
  542. console_host_ioctl_terminate( call, STATUS_CANCELLED );
  543. }
  544. if (server->term_fd != -1)
  545. {
  546. tcsetattr( server->term_fd, TCSANOW, &server->termios );
  547. close( server->term_fd );
  548. server->term_fd = -1;
  549. }
  550. if (server->console)
  551. {
  552. assert( server->console->server == server );
  553. server->console->server = NULL;
  554. server->console = NULL;
  555. wake_up( &server->obj, 0 );
  556. }
  557. }
  558. static void set_active_screen_buffer( struct console *console, struct screen_buffer *screen_buffer )
  559. {
  560. if (console->active == screen_buffer) return;
  561. if (console->active) release_object( console->active );
  562. console->active = (struct screen_buffer *)grab_object( screen_buffer );
  563. if (console->server) queue_host_ioctl( console->server, IOCTL_CONDRV_ACTIVATE,
  564. screen_buffer->id, NULL, NULL );
  565. }
  566. static struct object *create_screen_buffer( struct console *console )
  567. {
  568. struct screen_buffer *screen_buffer;
  569. if (console->last_id == ~0)
  570. {
  571. set_error( STATUS_NO_MEMORY );
  572. return NULL;
  573. }
  574. if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
  575. return NULL;
  576. screen_buffer->id = ++console->last_id;
  577. screen_buffer->input = console;
  578. init_async_queue( &screen_buffer->ioctl_q );
  579. list_add_head( &screen_buffer_list, &screen_buffer->entry );
  580. screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
  581. FILE_SYNCHRONOUS_IO_NONALERT );
  582. if (!screen_buffer->fd)
  583. {
  584. release_object( screen_buffer );
  585. return NULL;
  586. }
  587. allow_fd_caching(screen_buffer->fd);
  588. if (console->server) queue_host_ioctl( console->server, IOCTL_CONDRV_INIT_OUTPUT,
  589. screen_buffer->id, NULL, NULL );
  590. if (!console->active) set_active_screen_buffer( console, screen_buffer );
  591. return &screen_buffer->obj;
  592. }
  593. struct thread *console_get_renderer( struct console *console )
  594. {
  595. return console->renderer;
  596. }
  597. struct console_signal_info
  598. {
  599. struct console *console;
  600. process_id_t group;
  601. int signal;
  602. };
  603. static int propagate_console_signal_cb(struct process *process, void *user)
  604. {
  605. struct console_signal_info* csi = (struct console_signal_info*)user;
  606. if (process->console == csi->console && (!csi->group || process->group_id == csi->group))
  607. {
  608. /* find a suitable thread to signal */
  609. struct thread *thread;
  610. LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
  611. {
  612. if (send_thread_signal( thread, csi->signal )) break;
  613. }
  614. }
  615. return FALSE;
  616. }
  617. static void propagate_console_signal( struct console *console,
  618. int sig, process_id_t group_id )
  619. {
  620. struct console_signal_info csi;
  621. if (!console)
  622. {
  623. set_error( STATUS_INVALID_PARAMETER );
  624. return;
  625. }
  626. /* FIXME: should support the other events (like CTRL_BREAK) */
  627. if (sig != CTRL_C_EVENT)
  628. {
  629. set_error( STATUS_NOT_IMPLEMENTED );
  630. return;
  631. }
  632. csi.console = console;
  633. csi.signal = SIGINT;
  634. csi.group = group_id;
  635. enum_processes(propagate_console_signal_cb, &csi);
  636. }
  637. /* dumb dump */
  638. static void console_dump( struct object *obj, int verbose )
  639. {
  640. struct console *console = (struct console *)obj;
  641. assert( obj->ops == &console_ops );
  642. fprintf( stderr, "Console input active=%p server=%p\n",
  643. console->active, console->server );
  644. }
  645. static void console_destroy( struct object *obj )
  646. {
  647. struct console *console = (struct console *)obj;
  648. struct screen_buffer *curr;
  649. assert( obj->ops == &console_ops );
  650. if (console->server)
  651. {
  652. assert( console->server->console == console );
  653. disconnect_console_server( console->server );
  654. }
  655. if (console->active) release_object( console->active );
  656. console->active = NULL;
  657. LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
  658. {
  659. if (curr->input == console) curr->input = NULL;
  660. }
  661. free_async_queue( &console->ioctl_q );
  662. free_async_queue( &console->read_q );
  663. if (console->fd)
  664. release_object( console->fd );
  665. }
  666. static struct object *create_console_connection( struct console *console )
  667. {
  668. struct console_connection *connection;
  669. if (current->process->console)
  670. {
  671. set_error( STATUS_ACCESS_DENIED );
  672. return NULL;
  673. }
  674. if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
  675. if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
  676. {
  677. release_object( connection );
  678. return NULL;
  679. }
  680. if (console)
  681. current->process->console = (struct console *)grab_object( console );
  682. return &connection->obj;
  683. }
  684. static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
  685. unsigned int attr, struct object *root )
  686. {
  687. struct console *console = (struct console *)obj;
  688. static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
  689. assert( obj->ops == &console_ops );
  690. if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
  691. {
  692. name->len = 0;
  693. return create_console_connection( console );
  694. }
  695. return NULL;
  696. }
  697. static struct object *console_open_file( struct object *obj, unsigned int access,
  698. unsigned int sharing, unsigned int options )
  699. {
  700. return grab_object( obj );
  701. }
  702. static void screen_buffer_dump( struct object *obj, int verbose )
  703. {
  704. struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
  705. assert( obj->ops == &screen_buffer_ops );
  706. fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
  707. }
  708. static void screen_buffer_destroy( struct object *obj )
  709. {
  710. struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
  711. assert( obj->ops == &screen_buffer_ops );
  712. list_remove( &screen_buffer->entry );
  713. if (screen_buffer->input && screen_buffer->input->server)
  714. queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
  715. screen_buffer->id, NULL, NULL );
  716. if (screen_buffer->fd) release_object( screen_buffer->fd );
  717. free_async_queue( &screen_buffer->ioctl_q );
  718. }
  719. static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
  720. unsigned int sharing, unsigned int options )
  721. {
  722. return grab_object( obj );
  723. }
  724. static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
  725. {
  726. struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
  727. if (!screen_buffer->input)
  728. {
  729. set_error( STATUS_ACCESS_DENIED );
  730. return 0;
  731. }
  732. return add_queue( &screen_buffer->input->obj, entry );
  733. }
  734. static struct fd *screen_buffer_get_fd( struct object *obj )
  735. {
  736. struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
  737. assert( obj->ops == &screen_buffer_ops );
  738. if (screen_buffer->fd)
  739. return (struct fd*)grab_object( screen_buffer->fd );
  740. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  741. return NULL;
  742. }
  743. static void console_server_dump( struct object *obj, int verbose )
  744. {
  745. assert( obj->ops == &console_server_ops );
  746. fprintf( stderr, "Console server\n" );
  747. }
  748. static void console_server_destroy( struct object *obj )
  749. {
  750. struct console_server *server = (struct console_server *)obj;
  751. assert( obj->ops == &console_server_ops );
  752. disconnect_console_server( server );
  753. if (server->fd) release_object( server->fd );
  754. }
  755. static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
  756. unsigned int attr, struct object *root )
  757. {
  758. struct console_server *server = (struct console_server*)obj;
  759. static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
  760. assert( obj->ops == &console_server_ops );
  761. if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
  762. {
  763. struct screen_buffer *screen_buffer;
  764. name->len = 0;
  765. if (server->console)
  766. {
  767. set_error( STATUS_INVALID_HANDLE );
  768. return 0;
  769. }
  770. if (!(server->console = (struct console *)create_console())) return NULL;
  771. if (!(screen_buffer = (struct screen_buffer *)create_screen_buffer( server->console )))
  772. {
  773. release_object( server->console );
  774. server->console = NULL;
  775. return NULL;
  776. }
  777. release_object( screen_buffer );
  778. server->console->server = server;
  779. return &server->console->obj;
  780. }
  781. return NULL;
  782. }
  783. static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
  784. {
  785. struct console_server *server = (struct console_server*)obj;
  786. assert( obj->ops == &console_server_ops );
  787. return !server->console || !list_empty( &server->queue );
  788. }
  789. static struct fd *console_server_get_fd( struct object* obj )
  790. {
  791. struct console_server *server = (struct console_server*)obj;
  792. assert( obj->ops == &console_server_ops );
  793. return (struct fd *)grab_object( server->fd );
  794. }
  795. static struct object *console_server_open_file( struct object *obj, unsigned int access,
  796. unsigned int sharing, unsigned int options )
  797. {
  798. return grab_object( obj );
  799. }
  800. static struct object *create_console_server( void )
  801. {
  802. struct console_server *server;
  803. if (!(server = alloc_object( &console_server_ops ))) return NULL;
  804. server->console = NULL;
  805. server->busy = 0;
  806. server->once_input = 0;
  807. server->term_fd = -1;
  808. list_init( &server->queue );
  809. list_init( &server->read_queue );
  810. server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
  811. if (!server->fd)
  812. {
  813. release_object( server );
  814. return NULL;
  815. }
  816. allow_fd_caching(server->fd);
  817. return &server->obj;
  818. }
  819. static int is_blocking_read_ioctl( unsigned int code )
  820. {
  821. switch (code)
  822. {
  823. case IOCTL_CONDRV_READ_INPUT:
  824. case IOCTL_CONDRV_READ_CONSOLE:
  825. case IOCTL_CONDRV_READ_FILE:
  826. return 1;
  827. default:
  828. return 0;
  829. }
  830. }
  831. static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  832. {
  833. struct console *console = get_fd_user( fd );
  834. switch (code)
  835. {
  836. case IOCTL_CONDRV_CTRL_EVENT:
  837. {
  838. const struct condrv_ctrl_event *event = get_req_data();
  839. process_id_t group;
  840. if (get_req_data_size() != sizeof(*event))
  841. {
  842. set_error( STATUS_INVALID_PARAMETER );
  843. return;
  844. }
  845. group = event->group_id ? event->group_id : current->process->group_id;
  846. if (!group)
  847. {
  848. set_error( STATUS_INVALID_PARAMETER );
  849. return;
  850. }
  851. propagate_console_signal( console, event->event, group );
  852. return;
  853. }
  854. default:
  855. if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
  856. {
  857. set_error( STATUS_INVALID_HANDLE );
  858. return;
  859. }
  860. queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
  861. }
  862. }
  863. static void console_read( struct fd *fd, struct async *async, file_pos_t pos )
  864. {
  865. struct console *console = get_fd_user( fd );
  866. if (!console->server)
  867. {
  868. set_error( STATUS_INVALID_HANDLE );
  869. return;
  870. }
  871. queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
  872. }
  873. static void console_flush( struct fd *fd, struct async *async )
  874. {
  875. struct console *console = get_fd_user( fd );
  876. if (!console->server)
  877. {
  878. set_error( STATUS_INVALID_HANDLE );
  879. return;
  880. }
  881. queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
  882. }
  883. static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
  884. {
  885. struct screen_buffer *screen_buffer = get_fd_user( fd );
  886. if (!screen_buffer->input || !screen_buffer->input->server)
  887. {
  888. set_error( STATUS_INVALID_HANDLE );
  889. return;
  890. }
  891. queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
  892. screen_buffer->id, async, &screen_buffer->ioctl_q );
  893. }
  894. static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  895. {
  896. struct screen_buffer *screen_buffer = get_fd_user( fd );
  897. switch (code)
  898. {
  899. case IOCTL_CONDRV_ACTIVATE:
  900. if (!screen_buffer->input)
  901. {
  902. set_error( STATUS_INVALID_HANDLE );
  903. return;
  904. }
  905. set_active_screen_buffer( screen_buffer->input, screen_buffer );
  906. return;
  907. default:
  908. if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
  909. is_blocking_read_ioctl( code ))
  910. {
  911. set_error( STATUS_INVALID_HANDLE );
  912. return;
  913. }
  914. queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
  915. async, &screen_buffer->ioctl_q );
  916. }
  917. }
  918. static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  919. {
  920. struct console_connection *console_connection = get_fd_user( fd );
  921. switch (code)
  922. {
  923. case IOCTL_CONDRV_BIND_PID:
  924. {
  925. struct process *process;
  926. unsigned int pid;
  927. if (get_req_data_size() != sizeof(unsigned int))
  928. {
  929. set_error( STATUS_INVALID_PARAMETER );
  930. return;
  931. }
  932. if (current->process->console)
  933. {
  934. set_error( STATUS_INVALID_HANDLE );
  935. return;
  936. }
  937. pid = *(unsigned int *)get_req_data();
  938. if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
  939. if (!(process = get_process_from_id( pid ))) return;
  940. if (process->console)
  941. current->process->console = (struct console *)grab_object( process->console );
  942. else set_error( STATUS_ACCESS_DENIED );
  943. release_object( process );
  944. return;
  945. }
  946. default:
  947. default_fd_ioctl( console_connection->fd, code, async );
  948. }
  949. }
  950. static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  951. {
  952. struct console_server *server = get_fd_user( fd );
  953. switch (code)
  954. {
  955. case IOCTL_CONDRV_CTRL_EVENT:
  956. {
  957. const struct condrv_ctrl_event *event = get_req_data();
  958. if (get_req_data_size() != sizeof(*event))
  959. {
  960. set_error( STATUS_INVALID_PARAMETER );
  961. return;
  962. }
  963. if (!server->console)
  964. {
  965. set_error( STATUS_INVALID_HANDLE );
  966. return;
  967. }
  968. propagate_console_signal( server->console, event->event, event->group_id );
  969. return;
  970. }
  971. case IOCTL_CONDRV_SETUP_INPUT:
  972. {
  973. struct termios term;
  974. obj_handle_t handle;
  975. struct file *file;
  976. int unix_fd;
  977. if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
  978. {
  979. set_error( STATUS_INVALID_PARAMETER );
  980. return;
  981. }
  982. if (server->term_fd != -1)
  983. {
  984. tcsetattr( server->term_fd, TCSANOW, &server->termios );
  985. close( server->term_fd );
  986. server->term_fd = -1;
  987. }
  988. handle = *(unsigned int *)get_req_data();
  989. if (!handle) return;
  990. if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
  991. {
  992. return;
  993. }
  994. unix_fd = get_file_unix_fd( file );
  995. release_object( file );
  996. if (tcgetattr( unix_fd, &server->termios ))
  997. {
  998. file_set_error();
  999. return;
  1000. }
  1001. term = server->termios;
  1002. term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
  1003. term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  1004. term.c_cflag &= ~(CSIZE | PARENB);
  1005. term.c_cflag |= CS8;
  1006. term.c_cc[VMIN] = 1;
  1007. term.c_cc[VTIME] = 0;
  1008. if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
  1009. file_set_error();
  1010. return;
  1011. }
  1012. default:
  1013. set_error( STATUS_INVALID_HANDLE );
  1014. return;
  1015. }
  1016. }
  1017. static void console_connection_dump( struct object *obj, int verbose )
  1018. {
  1019. fputs( "console connection\n", stderr );
  1020. }
  1021. static struct fd *console_connection_get_fd( struct object *obj )
  1022. {
  1023. struct console_connection *connection = (struct console_connection *)obj;
  1024. return (struct fd *)grab_object( connection->fd );
  1025. }
  1026. static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
  1027. unsigned int attr, struct object *root )
  1028. {
  1029. static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
  1030. if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
  1031. {
  1032. if (!current->process->console)
  1033. {
  1034. set_error( STATUS_INVALID_HANDLE );
  1035. return NULL;
  1036. }
  1037. name->len = 0;
  1038. return grab_object( current->process->console );
  1039. }
  1040. return NULL;
  1041. }
  1042. static struct object *console_connection_open_file( struct object *obj, unsigned int access,
  1043. unsigned int sharing, unsigned int options )
  1044. {
  1045. return grab_object( obj );
  1046. }
  1047. static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  1048. {
  1049. struct console *console = process->console;
  1050. if (console)
  1051. {
  1052. process->console = NULL;
  1053. release_object( console );
  1054. }
  1055. return 1;
  1056. }
  1057. static void console_connection_destroy( struct object *obj )
  1058. {
  1059. struct console_connection *connection = (struct console_connection *)obj;
  1060. if (connection->fd) release_object( connection->fd );
  1061. }
  1062. static void console_device_dump( struct object *obj, int verbose )
  1063. {
  1064. fputs( "Console device\n", stderr );
  1065. }
  1066. static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
  1067. unsigned int attr, struct object *root )
  1068. {
  1069. static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
  1070. static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
  1071. static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
  1072. static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
  1073. static const WCHAR inputW[] = {'I','n','p','u','t'};
  1074. static const WCHAR outputW[] = {'O','u','t','p','u','t'};
  1075. static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
  1076. static const WCHAR serverW[] = {'S','e','r','v','e','r'};
  1077. if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
  1078. {
  1079. if (!current->process->console)
  1080. {
  1081. set_error( STATUS_INVALID_HANDLE );
  1082. return NULL;
  1083. }
  1084. name->len = 0;
  1085. return grab_object( current->process->console );
  1086. }
  1087. if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
  1088. {
  1089. if (!current->process->console || !current->process->console->active)
  1090. {
  1091. set_error( STATUS_INVALID_HANDLE );
  1092. return NULL;
  1093. }
  1094. name->len = 0;
  1095. return grab_object( current->process->console->active );
  1096. }
  1097. if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
  1098. {
  1099. name->len = 0;
  1100. return grab_object( obj );
  1101. }
  1102. if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
  1103. {
  1104. struct console_input *console_input;
  1105. name->len = 0;
  1106. if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
  1107. console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
  1108. FILE_SYNCHRONOUS_IO_NONALERT );
  1109. if (!console_input->fd)
  1110. {
  1111. release_object( console_input );
  1112. return NULL;
  1113. }
  1114. return &console_input->obj;
  1115. }
  1116. if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
  1117. {
  1118. struct console_output *console_output;
  1119. name->len = 0;
  1120. if (!(console_output = alloc_object( &console_output_ops ))) return NULL;
  1121. console_output->fd = alloc_pseudo_fd( &console_output_fd_ops, &console_output->obj,
  1122. FILE_SYNCHRONOUS_IO_NONALERT );
  1123. if (!console_output->fd)
  1124. {
  1125. release_object( console_output );
  1126. return NULL;
  1127. }
  1128. return &console_output->obj;
  1129. }
  1130. if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
  1131. {
  1132. if (!current->process->console)
  1133. {
  1134. set_error( STATUS_INVALID_HANDLE );
  1135. return NULL;
  1136. }
  1137. name->len = 0;
  1138. return create_screen_buffer( current->process->console );
  1139. }
  1140. if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
  1141. {
  1142. name->len = 0;
  1143. return create_console_server();
  1144. }
  1145. if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
  1146. {
  1147. name->len = 0;
  1148. return create_console_connection( NULL );
  1149. }
  1150. return NULL;
  1151. }
  1152. static struct object *console_device_open_file( struct object *obj, unsigned int access,
  1153. unsigned int sharing, unsigned int options )
  1154. {
  1155. int is_output;
  1156. access = default_map_access( obj, access );
  1157. is_output = access & FILE_WRITE_DATA;
  1158. if (!current->process->console || (is_output && !current->process->console))
  1159. {
  1160. set_error( STATUS_INVALID_HANDLE );
  1161. return NULL;
  1162. }
  1163. if (is_output && (access & FILE_READ_DATA))
  1164. {
  1165. set_error( STATUS_INVALID_PARAMETER );
  1166. return NULL;
  1167. }
  1168. return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
  1169. }
  1170. static void console_input_dump( struct object *obj, int verbose )
  1171. {
  1172. fputs( "console Input device\n", stderr );
  1173. }
  1174. static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
  1175. {
  1176. if (!current->process->console)
  1177. {
  1178. set_error( STATUS_ACCESS_DENIED );
  1179. return 0;
  1180. }
  1181. return console_add_queue( &current->process->console->obj, entry );
  1182. }
  1183. static struct fd *console_input_get_fd( struct object *obj )
  1184. {
  1185. struct console_input *console_input = (struct console_input *)obj;
  1186. assert( obj->ops == &console_input_ops );
  1187. return (struct fd *)grab_object( console_input->fd );
  1188. }
  1189. static struct object *console_input_open_file( struct object *obj, unsigned int access,
  1190. unsigned int sharing, unsigned int options )
  1191. {
  1192. return grab_object( obj );
  1193. }
  1194. static void console_input_destroy( struct object *obj )
  1195. {
  1196. struct console_input *console_input = (struct console_input *)obj;
  1197. assert( obj->ops == &console_input_ops );
  1198. if (console_input->fd) release_object( console_input->fd );
  1199. }
  1200. static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1201. {
  1202. struct console *console = current->process->console;
  1203. if (!console)
  1204. {
  1205. set_error( STATUS_INVALID_HANDLE );
  1206. return;
  1207. }
  1208. console_ioctl( console->fd, code, async );
  1209. }
  1210. static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
  1211. {
  1212. struct console *console = current->process->console;
  1213. if (!console)
  1214. {
  1215. set_error( STATUS_INVALID_HANDLE );
  1216. return;
  1217. }
  1218. console_read( console->fd, async, pos );
  1219. }
  1220. static void console_input_flush( struct fd *fd, struct async *async )
  1221. {
  1222. struct console *console = current->process->console;
  1223. if (!console)
  1224. {
  1225. set_error( STATUS_INVALID_HANDLE );
  1226. return;
  1227. }
  1228. console_flush( console->fd, async );
  1229. }
  1230. static void console_output_dump( struct object *obj, int verbose )
  1231. {
  1232. fputs( "console Output device\n", stderr );
  1233. }
  1234. static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry )
  1235. {
  1236. if (!current->process->console || !current->process->console->active)
  1237. {
  1238. set_error( STATUS_ACCESS_DENIED );
  1239. return 0;
  1240. }
  1241. return add_queue( &current->process->console->obj, entry );
  1242. }
  1243. static struct fd *console_output_get_fd( struct object *obj )
  1244. {
  1245. struct console_output *console_output = (struct console_output *)obj;
  1246. assert( obj->ops == &console_output_ops );
  1247. return (struct fd *)grab_object( console_output->fd );
  1248. }
  1249. static struct object *console_output_open_file( struct object *obj, unsigned int access,
  1250. unsigned int sharing, unsigned int options )
  1251. {
  1252. return grab_object( obj );
  1253. }
  1254. static void console_output_destroy( struct object *obj )
  1255. {
  1256. struct console_output *console_output = (struct console_output *)obj;
  1257. assert( obj->ops == &console_output_ops );
  1258. if (console_output->fd) release_object( console_output->fd );
  1259. }
  1260. static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1261. {
  1262. struct console *console = current->process->console;
  1263. if (!console || !console->active)
  1264. {
  1265. set_error( STATUS_INVALID_HANDLE );
  1266. return;
  1267. }
  1268. screen_buffer_ioctl( console->active->fd, code, async );
  1269. }
  1270. static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos )
  1271. {
  1272. struct console *console = current->process->console;
  1273. if (!console || !console->active)
  1274. {
  1275. set_error( STATUS_INVALID_HANDLE );
  1276. return;
  1277. }
  1278. screen_buffer_write( console->active->fd, async, pos );
  1279. }
  1280. struct object *create_console_device( struct object *root, const struct unicode_str *name,
  1281. unsigned int attr, const struct security_descriptor *sd )
  1282. {
  1283. return create_named_object( root, &console_device_ops, name, attr, sd );
  1284. }
  1285. /* retrieve the next pending console ioctl request */
  1286. DECL_HANDLER(get_next_console_request)
  1287. {
  1288. struct console_host_ioctl *ioctl = NULL, *next;
  1289. struct console_server *server;
  1290. struct iosb *iosb = NULL;
  1291. server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
  1292. if (!server) return;
  1293. if (!server->console)
  1294. {
  1295. set_error( STATUS_INVALID_HANDLE );
  1296. release_object( server );
  1297. return;
  1298. }
  1299. if (!server->console->renderer) server->console->renderer = current;
  1300. if (!req->signal) server->console->signaled = 0;
  1301. else if (!server->console->signaled)
  1302. {
  1303. server->console->signaled = 1;
  1304. wake_up( &server->console->obj, 0 );
  1305. }
  1306. if (req->read)
  1307. {
  1308. /* set result of current pending ioctl */
  1309. if (list_empty( &server->read_queue ))
  1310. {
  1311. set_error( STATUS_INVALID_HANDLE );
  1312. release_object( server );
  1313. return;
  1314. }
  1315. ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
  1316. list_remove( &ioctl->entry );
  1317. list_move_tail( &server->queue, &server->read_queue );
  1318. }
  1319. else if (server->busy)
  1320. {
  1321. /* set result of previous ioctl */
  1322. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1323. list_remove( &ioctl->entry );
  1324. }
  1325. if (ioctl)
  1326. {
  1327. struct async *async = ioctl->async;
  1328. unsigned int status = req->status;
  1329. if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
  1330. if (async)
  1331. {
  1332. iosb = async_get_iosb( async );
  1333. if (iosb->status == STATUS_PENDING)
  1334. {
  1335. data_size_t out_size = min( iosb->out_size, get_req_data_size() );
  1336. data_size_t result = ioctl->code == IOCTL_CONDRV_WRITE_FILE ? iosb->in_size : out_size;
  1337. async_request_complete_alloc( async, status, result, out_size, get_req_data() );
  1338. }
  1339. release_object( async );
  1340. }
  1341. free( ioctl );
  1342. if (iosb) release_object( iosb );
  1343. if (req->read)
  1344. {
  1345. release_object( server );
  1346. return;
  1347. }
  1348. server->busy = 0;
  1349. }
  1350. /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
  1351. * move it to read queue for execution after current read is complete. move all blocking
  1352. * ioctl at the same time to preserve their order. */
  1353. if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
  1354. {
  1355. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1356. if (is_blocking_read_ioctl( ioctl->code ))
  1357. {
  1358. LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
  1359. {
  1360. if (!is_blocking_read_ioctl( ioctl->code )) continue;
  1361. list_remove( &ioctl->entry );
  1362. list_add_tail( &server->read_queue, &ioctl->entry );
  1363. }
  1364. }
  1365. }
  1366. /* return the next ioctl */
  1367. if (!list_empty( &server->queue ))
  1368. {
  1369. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1370. iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
  1371. if (!iosb || get_reply_max_size() >= iosb->in_size)
  1372. {
  1373. reply->code = ioctl->code;
  1374. reply->output = ioctl->output;
  1375. if (iosb)
  1376. {
  1377. reply->out_size = iosb->out_size;
  1378. set_reply_data_ptr( iosb->in_data, iosb->in_size );
  1379. iosb->in_data = NULL;
  1380. }
  1381. if (is_blocking_read_ioctl( ioctl->code ))
  1382. {
  1383. list_remove( &ioctl->entry );
  1384. assert( list_empty( &server->read_queue ));
  1385. list_add_tail( &server->read_queue, &ioctl->entry );
  1386. }
  1387. else server->busy = 1;
  1388. }
  1389. else
  1390. {
  1391. reply->out_size = iosb->in_size;
  1392. set_error( STATUS_BUFFER_OVERFLOW );
  1393. }
  1394. if (iosb) release_object( iosb );
  1395. }
  1396. else
  1397. {
  1398. set_error( STATUS_PENDING );
  1399. }
  1400. release_object( server );
  1401. }