console.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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. struct console_process_list
  638. {
  639. unsigned int size;
  640. unsigned int count;
  641. process_id_t *processes;
  642. struct console *console;
  643. };
  644. static int console_process_list_cb(struct process *process, void *user)
  645. {
  646. struct console_process_list *cpl = user;
  647. if (process->console == cpl->console)
  648. {
  649. if (cpl->count < cpl->size) cpl->processes[cpl->count] = process->id;
  650. cpl->count++;
  651. }
  652. return 0;
  653. }
  654. /* dumb dump */
  655. static void console_dump( struct object *obj, int verbose )
  656. {
  657. struct console *console = (struct console *)obj;
  658. assert( obj->ops == &console_ops );
  659. fprintf( stderr, "Console input active=%p server=%p\n",
  660. console->active, console->server );
  661. }
  662. static void console_destroy( struct object *obj )
  663. {
  664. struct console *console = (struct console *)obj;
  665. struct screen_buffer *curr;
  666. assert( obj->ops == &console_ops );
  667. if (console->server)
  668. {
  669. assert( console->server->console == console );
  670. disconnect_console_server( console->server );
  671. }
  672. if (console->active) release_object( console->active );
  673. console->active = NULL;
  674. LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
  675. {
  676. if (curr->input == console) curr->input = NULL;
  677. }
  678. free_async_queue( &console->ioctl_q );
  679. free_async_queue( &console->read_q );
  680. if (console->fd)
  681. release_object( console->fd );
  682. }
  683. static struct object *create_console_connection( struct console *console )
  684. {
  685. struct console_connection *connection;
  686. if (current->process->console)
  687. {
  688. set_error( STATUS_ACCESS_DENIED );
  689. return NULL;
  690. }
  691. if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
  692. if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
  693. {
  694. release_object( connection );
  695. return NULL;
  696. }
  697. if (console)
  698. current->process->console = (struct console *)grab_object( console );
  699. return &connection->obj;
  700. }
  701. static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
  702. unsigned int attr, struct object *root )
  703. {
  704. struct console *console = (struct console *)obj;
  705. static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
  706. assert( obj->ops == &console_ops );
  707. if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
  708. {
  709. name->len = 0;
  710. return create_console_connection( console );
  711. }
  712. return NULL;
  713. }
  714. static struct object *console_open_file( struct object *obj, unsigned int access,
  715. unsigned int sharing, unsigned int options )
  716. {
  717. return grab_object( obj );
  718. }
  719. static void screen_buffer_dump( struct object *obj, int verbose )
  720. {
  721. struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
  722. assert( obj->ops == &screen_buffer_ops );
  723. fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
  724. }
  725. static void screen_buffer_destroy( struct object *obj )
  726. {
  727. struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
  728. assert( obj->ops == &screen_buffer_ops );
  729. list_remove( &screen_buffer->entry );
  730. if (screen_buffer->input && screen_buffer->input->server)
  731. queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
  732. screen_buffer->id, NULL, NULL );
  733. if (screen_buffer->fd) release_object( screen_buffer->fd );
  734. free_async_queue( &screen_buffer->ioctl_q );
  735. }
  736. static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
  737. unsigned int sharing, unsigned int options )
  738. {
  739. return grab_object( obj );
  740. }
  741. static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
  742. {
  743. struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
  744. if (!screen_buffer->input)
  745. {
  746. set_error( STATUS_ACCESS_DENIED );
  747. return 0;
  748. }
  749. return console_add_queue( &screen_buffer->input->obj, entry );
  750. }
  751. static struct fd *screen_buffer_get_fd( struct object *obj )
  752. {
  753. struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
  754. assert( obj->ops == &screen_buffer_ops );
  755. if (screen_buffer->fd)
  756. return (struct fd*)grab_object( screen_buffer->fd );
  757. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  758. return NULL;
  759. }
  760. static void console_server_dump( struct object *obj, int verbose )
  761. {
  762. assert( obj->ops == &console_server_ops );
  763. fprintf( stderr, "Console server\n" );
  764. }
  765. static void console_server_destroy( struct object *obj )
  766. {
  767. struct console_server *server = (struct console_server *)obj;
  768. assert( obj->ops == &console_server_ops );
  769. disconnect_console_server( server );
  770. if (server->fd) release_object( server->fd );
  771. }
  772. static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
  773. unsigned int attr, struct object *root )
  774. {
  775. struct console_server *server = (struct console_server*)obj;
  776. static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
  777. assert( obj->ops == &console_server_ops );
  778. if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
  779. {
  780. struct screen_buffer *screen_buffer;
  781. name->len = 0;
  782. if (server->console)
  783. {
  784. set_error( STATUS_INVALID_HANDLE );
  785. return 0;
  786. }
  787. if (!(server->console = (struct console *)create_console())) return NULL;
  788. if (!(screen_buffer = (struct screen_buffer *)create_screen_buffer( server->console )))
  789. {
  790. release_object( server->console );
  791. server->console = NULL;
  792. return NULL;
  793. }
  794. release_object( screen_buffer );
  795. server->console->server = server;
  796. return &server->console->obj;
  797. }
  798. return NULL;
  799. }
  800. static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
  801. {
  802. struct console_server *server = (struct console_server*)obj;
  803. assert( obj->ops == &console_server_ops );
  804. return !server->console || !list_empty( &server->queue );
  805. }
  806. static struct fd *console_server_get_fd( struct object* obj )
  807. {
  808. struct console_server *server = (struct console_server*)obj;
  809. assert( obj->ops == &console_server_ops );
  810. return (struct fd *)grab_object( server->fd );
  811. }
  812. static struct object *console_server_open_file( struct object *obj, unsigned int access,
  813. unsigned int sharing, unsigned int options )
  814. {
  815. return grab_object( obj );
  816. }
  817. static struct object *create_console_server( void )
  818. {
  819. struct console_server *server;
  820. if (!(server = alloc_object( &console_server_ops ))) return NULL;
  821. server->console = NULL;
  822. server->busy = 0;
  823. server->once_input = 0;
  824. server->term_fd = -1;
  825. list_init( &server->queue );
  826. list_init( &server->read_queue );
  827. server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
  828. if (!server->fd)
  829. {
  830. release_object( server );
  831. return NULL;
  832. }
  833. allow_fd_caching(server->fd);
  834. return &server->obj;
  835. }
  836. static int is_blocking_read_ioctl( unsigned int code )
  837. {
  838. switch (code)
  839. {
  840. case IOCTL_CONDRV_READ_INPUT:
  841. case IOCTL_CONDRV_READ_CONSOLE:
  842. case IOCTL_CONDRV_READ_FILE:
  843. return 1;
  844. default:
  845. return 0;
  846. }
  847. }
  848. static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  849. {
  850. struct console *console = get_fd_user( fd );
  851. switch (code)
  852. {
  853. case IOCTL_CONDRV_CTRL_EVENT:
  854. {
  855. const struct condrv_ctrl_event *event = get_req_data();
  856. process_id_t group;
  857. if (get_req_data_size() != sizeof(*event))
  858. {
  859. set_error( STATUS_INVALID_PARAMETER );
  860. return;
  861. }
  862. group = event->group_id ? event->group_id : current->process->group_id;
  863. if (!group)
  864. {
  865. set_error( STATUS_INVALID_PARAMETER );
  866. return;
  867. }
  868. propagate_console_signal( console, event->event, group );
  869. return;
  870. }
  871. case IOCTL_CONDRV_GET_PROCESS_LIST:
  872. {
  873. struct console_process_list cpl;
  874. if (get_reply_max_size() < sizeof(unsigned int))
  875. {
  876. set_error( STATUS_INVALID_PARAMETER );
  877. return;
  878. }
  879. cpl.count = 0;
  880. cpl.size = 0;
  881. cpl.console = console;
  882. enum_processes( console_process_list_cb, &cpl );
  883. if (cpl.count * sizeof(process_id_t) > get_reply_max_size())
  884. {
  885. set_reply_data( &cpl.count, sizeof(cpl.count) );
  886. set_error( STATUS_BUFFER_TOO_SMALL );
  887. return;
  888. }
  889. cpl.size = cpl.count;
  890. cpl.count = 0;
  891. if ((cpl.processes = set_reply_data_size( cpl.size * sizeof(process_id_t) )))
  892. enum_processes( console_process_list_cb, &cpl );
  893. return;
  894. }
  895. default:
  896. if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
  897. {
  898. set_error( STATUS_INVALID_HANDLE );
  899. return;
  900. }
  901. queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
  902. }
  903. }
  904. static void console_read( struct fd *fd, struct async *async, file_pos_t pos )
  905. {
  906. struct console *console = get_fd_user( fd );
  907. if (!console->server)
  908. {
  909. set_error( STATUS_INVALID_HANDLE );
  910. return;
  911. }
  912. queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
  913. }
  914. static void console_flush( struct fd *fd, struct async *async )
  915. {
  916. struct console *console = get_fd_user( fd );
  917. if (!console->server)
  918. {
  919. set_error( STATUS_INVALID_HANDLE );
  920. return;
  921. }
  922. queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
  923. }
  924. static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
  925. {
  926. struct screen_buffer *screen_buffer = get_fd_user( fd );
  927. if (!screen_buffer->input || !screen_buffer->input->server)
  928. {
  929. set_error( STATUS_INVALID_HANDLE );
  930. return;
  931. }
  932. queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
  933. screen_buffer->id, async, &screen_buffer->ioctl_q );
  934. }
  935. static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  936. {
  937. struct screen_buffer *screen_buffer = get_fd_user( fd );
  938. switch (code)
  939. {
  940. case IOCTL_CONDRV_ACTIVATE:
  941. if (!screen_buffer->input)
  942. {
  943. set_error( STATUS_INVALID_HANDLE );
  944. return;
  945. }
  946. set_active_screen_buffer( screen_buffer->input, screen_buffer );
  947. return;
  948. default:
  949. if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
  950. is_blocking_read_ioctl( code ))
  951. {
  952. set_error( STATUS_INVALID_HANDLE );
  953. return;
  954. }
  955. queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
  956. async, &screen_buffer->ioctl_q );
  957. }
  958. }
  959. static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  960. {
  961. struct console_connection *console_connection = get_fd_user( fd );
  962. switch (code)
  963. {
  964. case IOCTL_CONDRV_BIND_PID:
  965. {
  966. struct process *process;
  967. unsigned int pid;
  968. if (get_req_data_size() != sizeof(unsigned int))
  969. {
  970. set_error( STATUS_INVALID_PARAMETER );
  971. return;
  972. }
  973. if (current->process->console)
  974. {
  975. set_error( STATUS_INVALID_HANDLE );
  976. return;
  977. }
  978. pid = *(unsigned int *)get_req_data();
  979. if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
  980. if (!(process = get_process_from_id( pid ))) return;
  981. if (process->console)
  982. current->process->console = (struct console *)grab_object( process->console );
  983. else set_error( STATUS_ACCESS_DENIED );
  984. release_object( process );
  985. return;
  986. }
  987. default:
  988. default_fd_ioctl( console_connection->fd, code, async );
  989. }
  990. }
  991. static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  992. {
  993. struct console_server *server = get_fd_user( fd );
  994. switch (code)
  995. {
  996. case IOCTL_CONDRV_CTRL_EVENT:
  997. {
  998. const struct condrv_ctrl_event *event = get_req_data();
  999. if (get_req_data_size() != sizeof(*event))
  1000. {
  1001. set_error( STATUS_INVALID_PARAMETER );
  1002. return;
  1003. }
  1004. if (!server->console)
  1005. {
  1006. set_error( STATUS_INVALID_HANDLE );
  1007. return;
  1008. }
  1009. propagate_console_signal( server->console, event->event, event->group_id );
  1010. return;
  1011. }
  1012. case IOCTL_CONDRV_SETUP_INPUT:
  1013. {
  1014. struct termios term;
  1015. obj_handle_t handle;
  1016. struct file *file;
  1017. int unix_fd;
  1018. if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
  1019. {
  1020. set_error( STATUS_INVALID_PARAMETER );
  1021. return;
  1022. }
  1023. if (server->term_fd != -1)
  1024. {
  1025. tcsetattr( server->term_fd, TCSANOW, &server->termios );
  1026. close( server->term_fd );
  1027. server->term_fd = -1;
  1028. }
  1029. handle = *(unsigned int *)get_req_data();
  1030. if (!handle) return;
  1031. if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
  1032. {
  1033. return;
  1034. }
  1035. unix_fd = get_file_unix_fd( file );
  1036. release_object( file );
  1037. if (tcgetattr( unix_fd, &server->termios ))
  1038. {
  1039. file_set_error();
  1040. return;
  1041. }
  1042. term = server->termios;
  1043. term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
  1044. term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  1045. term.c_cflag &= ~(CSIZE | PARENB);
  1046. term.c_cflag |= CS8;
  1047. term.c_cc[VMIN] = 1;
  1048. term.c_cc[VTIME] = 0;
  1049. if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
  1050. file_set_error();
  1051. return;
  1052. }
  1053. default:
  1054. set_error( STATUS_INVALID_HANDLE );
  1055. return;
  1056. }
  1057. }
  1058. static void console_connection_dump( struct object *obj, int verbose )
  1059. {
  1060. fputs( "console connection\n", stderr );
  1061. }
  1062. static struct fd *console_connection_get_fd( struct object *obj )
  1063. {
  1064. struct console_connection *connection = (struct console_connection *)obj;
  1065. return (struct fd *)grab_object( connection->fd );
  1066. }
  1067. static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
  1068. unsigned int attr, struct object *root )
  1069. {
  1070. static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
  1071. if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
  1072. {
  1073. if (!current->process->console)
  1074. {
  1075. set_error( STATUS_INVALID_HANDLE );
  1076. return NULL;
  1077. }
  1078. name->len = 0;
  1079. return grab_object( current->process->console );
  1080. }
  1081. return NULL;
  1082. }
  1083. static struct object *console_connection_open_file( struct object *obj, unsigned int access,
  1084. unsigned int sharing, unsigned int options )
  1085. {
  1086. return grab_object( obj );
  1087. }
  1088. static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  1089. {
  1090. struct console *console = process->console;
  1091. if (console)
  1092. {
  1093. process->console = NULL;
  1094. release_object( console );
  1095. }
  1096. return 1;
  1097. }
  1098. static void console_connection_destroy( struct object *obj )
  1099. {
  1100. struct console_connection *connection = (struct console_connection *)obj;
  1101. if (connection->fd) release_object( connection->fd );
  1102. }
  1103. static void console_device_dump( struct object *obj, int verbose )
  1104. {
  1105. fputs( "Console device\n", stderr );
  1106. }
  1107. static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
  1108. unsigned int attr, struct object *root )
  1109. {
  1110. static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
  1111. static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
  1112. static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
  1113. static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
  1114. static const WCHAR inputW[] = {'I','n','p','u','t'};
  1115. static const WCHAR outputW[] = {'O','u','t','p','u','t'};
  1116. static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
  1117. static const WCHAR serverW[] = {'S','e','r','v','e','r'};
  1118. if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
  1119. {
  1120. if (!current->process->console)
  1121. {
  1122. set_error( STATUS_INVALID_HANDLE );
  1123. return NULL;
  1124. }
  1125. name->len = 0;
  1126. return grab_object( current->process->console );
  1127. }
  1128. if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
  1129. {
  1130. if (!current->process->console || !current->process->console->active)
  1131. {
  1132. set_error( STATUS_INVALID_HANDLE );
  1133. return NULL;
  1134. }
  1135. name->len = 0;
  1136. return grab_object( current->process->console->active );
  1137. }
  1138. if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
  1139. {
  1140. name->len = 0;
  1141. return grab_object( obj );
  1142. }
  1143. if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
  1144. {
  1145. struct console_input *console_input;
  1146. name->len = 0;
  1147. if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
  1148. console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
  1149. FILE_SYNCHRONOUS_IO_NONALERT );
  1150. if (!console_input->fd)
  1151. {
  1152. release_object( console_input );
  1153. return NULL;
  1154. }
  1155. return &console_input->obj;
  1156. }
  1157. if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
  1158. {
  1159. struct console_output *console_output;
  1160. name->len = 0;
  1161. if (!(console_output = alloc_object( &console_output_ops ))) return NULL;
  1162. console_output->fd = alloc_pseudo_fd( &console_output_fd_ops, &console_output->obj,
  1163. FILE_SYNCHRONOUS_IO_NONALERT );
  1164. if (!console_output->fd)
  1165. {
  1166. release_object( console_output );
  1167. return NULL;
  1168. }
  1169. return &console_output->obj;
  1170. }
  1171. if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
  1172. {
  1173. if (!current->process->console)
  1174. {
  1175. set_error( STATUS_INVALID_HANDLE );
  1176. return NULL;
  1177. }
  1178. name->len = 0;
  1179. return create_screen_buffer( current->process->console );
  1180. }
  1181. if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
  1182. {
  1183. name->len = 0;
  1184. return create_console_server();
  1185. }
  1186. if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
  1187. {
  1188. name->len = 0;
  1189. return create_console_connection( NULL );
  1190. }
  1191. return NULL;
  1192. }
  1193. static struct object *console_device_open_file( struct object *obj, unsigned int access,
  1194. unsigned int sharing, unsigned int options )
  1195. {
  1196. int is_output;
  1197. access = default_map_access( obj, access );
  1198. is_output = access & FILE_WRITE_DATA;
  1199. if (!current->process->console || (is_output && !current->process->console))
  1200. {
  1201. set_error( STATUS_INVALID_HANDLE );
  1202. return NULL;
  1203. }
  1204. if (is_output && (access & FILE_READ_DATA))
  1205. {
  1206. set_error( STATUS_INVALID_PARAMETER );
  1207. return NULL;
  1208. }
  1209. return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
  1210. }
  1211. static void console_input_dump( struct object *obj, int verbose )
  1212. {
  1213. fputs( "console Input device\n", stderr );
  1214. }
  1215. static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
  1216. {
  1217. if (!current->process->console)
  1218. {
  1219. set_error( STATUS_ACCESS_DENIED );
  1220. return 0;
  1221. }
  1222. return console_add_queue( &current->process->console->obj, entry );
  1223. }
  1224. static struct fd *console_input_get_fd( struct object *obj )
  1225. {
  1226. struct console_input *console_input = (struct console_input *)obj;
  1227. assert( obj->ops == &console_input_ops );
  1228. return (struct fd *)grab_object( console_input->fd );
  1229. }
  1230. static struct object *console_input_open_file( struct object *obj, unsigned int access,
  1231. unsigned int sharing, unsigned int options )
  1232. {
  1233. return grab_object( obj );
  1234. }
  1235. static void console_input_destroy( struct object *obj )
  1236. {
  1237. struct console_input *console_input = (struct console_input *)obj;
  1238. assert( obj->ops == &console_input_ops );
  1239. if (console_input->fd) release_object( console_input->fd );
  1240. }
  1241. static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1242. {
  1243. struct console *console = current->process->console;
  1244. if (!console)
  1245. {
  1246. set_error( STATUS_INVALID_HANDLE );
  1247. return;
  1248. }
  1249. console_ioctl( console->fd, code, async );
  1250. }
  1251. static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
  1252. {
  1253. struct console *console = current->process->console;
  1254. if (!console)
  1255. {
  1256. set_error( STATUS_INVALID_HANDLE );
  1257. return;
  1258. }
  1259. console_read( console->fd, async, pos );
  1260. }
  1261. static void console_input_flush( struct fd *fd, struct async *async )
  1262. {
  1263. struct console *console = current->process->console;
  1264. if (!console)
  1265. {
  1266. set_error( STATUS_INVALID_HANDLE );
  1267. return;
  1268. }
  1269. console_flush( console->fd, async );
  1270. }
  1271. static void console_output_dump( struct object *obj, int verbose )
  1272. {
  1273. fputs( "console Output device\n", stderr );
  1274. }
  1275. static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry )
  1276. {
  1277. if (!current->process->console || !current->process->console->active)
  1278. {
  1279. set_error( STATUS_ACCESS_DENIED );
  1280. return 0;
  1281. }
  1282. return console_add_queue( &current->process->console->obj, entry );
  1283. }
  1284. static struct fd *console_output_get_fd( struct object *obj )
  1285. {
  1286. struct console_output *console_output = (struct console_output *)obj;
  1287. assert( obj->ops == &console_output_ops );
  1288. return (struct fd *)grab_object( console_output->fd );
  1289. }
  1290. static struct object *console_output_open_file( struct object *obj, unsigned int access,
  1291. unsigned int sharing, unsigned int options )
  1292. {
  1293. return grab_object( obj );
  1294. }
  1295. static void console_output_destroy( struct object *obj )
  1296. {
  1297. struct console_output *console_output = (struct console_output *)obj;
  1298. assert( obj->ops == &console_output_ops );
  1299. if (console_output->fd) release_object( console_output->fd );
  1300. }
  1301. static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1302. {
  1303. struct console *console = current->process->console;
  1304. if (!console || !console->active)
  1305. {
  1306. set_error( STATUS_INVALID_HANDLE );
  1307. return;
  1308. }
  1309. screen_buffer_ioctl( console->active->fd, code, async );
  1310. }
  1311. static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos )
  1312. {
  1313. struct console *console = current->process->console;
  1314. if (!console || !console->active)
  1315. {
  1316. set_error( STATUS_INVALID_HANDLE );
  1317. return;
  1318. }
  1319. screen_buffer_write( console->active->fd, async, pos );
  1320. }
  1321. struct object *create_console_device( struct object *root, const struct unicode_str *name,
  1322. unsigned int attr, const struct security_descriptor *sd )
  1323. {
  1324. return create_named_object( root, &console_device_ops, name, attr, sd );
  1325. }
  1326. /* retrieve the next pending console ioctl request */
  1327. DECL_HANDLER(get_next_console_request)
  1328. {
  1329. struct console_host_ioctl *ioctl = NULL, *next;
  1330. struct console_server *server;
  1331. struct iosb *iosb = NULL;
  1332. server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
  1333. if (!server) return;
  1334. if (!server->console)
  1335. {
  1336. set_error( STATUS_INVALID_HANDLE );
  1337. release_object( server );
  1338. return;
  1339. }
  1340. if (!server->console->renderer) server->console->renderer = current;
  1341. if (!req->signal) server->console->signaled = 0;
  1342. else if (!server->console->signaled)
  1343. {
  1344. server->console->signaled = 1;
  1345. wake_up( &server->console->obj, 0 );
  1346. }
  1347. if (req->read)
  1348. {
  1349. /* set result of current pending ioctl */
  1350. if (list_empty( &server->read_queue ))
  1351. {
  1352. set_error( STATUS_INVALID_HANDLE );
  1353. release_object( server );
  1354. return;
  1355. }
  1356. ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
  1357. list_remove( &ioctl->entry );
  1358. list_move_tail( &server->queue, &server->read_queue );
  1359. }
  1360. else if (server->busy)
  1361. {
  1362. /* set result of previous ioctl */
  1363. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1364. list_remove( &ioctl->entry );
  1365. }
  1366. if (ioctl)
  1367. {
  1368. struct async *async = ioctl->async;
  1369. unsigned int status = req->status;
  1370. if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
  1371. if (async)
  1372. {
  1373. iosb = async_get_iosb( async );
  1374. if (iosb->status == STATUS_PENDING)
  1375. {
  1376. data_size_t out_size = min( iosb->out_size, get_req_data_size() );
  1377. data_size_t result = ioctl->code == IOCTL_CONDRV_WRITE_FILE ? iosb->in_size : out_size;
  1378. async_request_complete_alloc( async, status, result, out_size, get_req_data() );
  1379. }
  1380. release_object( async );
  1381. }
  1382. free( ioctl );
  1383. if (iosb) release_object( iosb );
  1384. if (req->read)
  1385. {
  1386. release_object( server );
  1387. return;
  1388. }
  1389. server->busy = 0;
  1390. }
  1391. /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
  1392. * move it to read queue for execution after current read is complete. move all blocking
  1393. * ioctl at the same time to preserve their order. */
  1394. if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
  1395. {
  1396. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1397. if (is_blocking_read_ioctl( ioctl->code ))
  1398. {
  1399. LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
  1400. {
  1401. if (!is_blocking_read_ioctl( ioctl->code )) continue;
  1402. list_remove( &ioctl->entry );
  1403. list_add_tail( &server->read_queue, &ioctl->entry );
  1404. }
  1405. }
  1406. }
  1407. /* return the next ioctl */
  1408. if (!list_empty( &server->queue ))
  1409. {
  1410. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1411. iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
  1412. if (!iosb || get_reply_max_size() >= iosb->in_size)
  1413. {
  1414. reply->code = ioctl->code;
  1415. reply->output = ioctl->output;
  1416. if (iosb)
  1417. {
  1418. reply->out_size = iosb->out_size;
  1419. set_reply_data_ptr( iosb->in_data, iosb->in_size );
  1420. iosb->in_data = NULL;
  1421. }
  1422. if (is_blocking_read_ioctl( ioctl->code ))
  1423. {
  1424. list_remove( &ioctl->entry );
  1425. assert( list_empty( &server->read_queue ));
  1426. list_add_tail( &server->read_queue, &ioctl->entry );
  1427. }
  1428. else server->busy = 1;
  1429. }
  1430. else
  1431. {
  1432. reply->out_size = iosb->in_size;
  1433. set_error( STATUS_BUFFER_OVERFLOW );
  1434. }
  1435. if (iosb) release_object( iosb );
  1436. }
  1437. else
  1438. {
  1439. set_error( STATUS_PENDING );
  1440. }
  1441. release_object( server );
  1442. }