console.c 60 KB

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