console.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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_CONSOLE_CONTROL:
  843. case IOCTL_CONDRV_READ_FILE:
  844. return 1;
  845. default:
  846. return 0;
  847. }
  848. }
  849. static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  850. {
  851. struct console *console = get_fd_user( fd );
  852. switch (code)
  853. {
  854. case IOCTL_CONDRV_CTRL_EVENT:
  855. {
  856. const struct condrv_ctrl_event *event = get_req_data();
  857. process_id_t group;
  858. if (get_req_data_size() != sizeof(*event))
  859. {
  860. set_error( STATUS_INVALID_PARAMETER );
  861. return;
  862. }
  863. group = event->group_id ? event->group_id : current->process->group_id;
  864. if (!group)
  865. {
  866. set_error( STATUS_INVALID_PARAMETER );
  867. return;
  868. }
  869. propagate_console_signal( console, event->event, group );
  870. return;
  871. }
  872. case IOCTL_CONDRV_GET_PROCESS_LIST:
  873. {
  874. struct console_process_list cpl;
  875. if (get_reply_max_size() < sizeof(unsigned int))
  876. {
  877. set_error( STATUS_INVALID_PARAMETER );
  878. return;
  879. }
  880. cpl.count = 0;
  881. cpl.size = 0;
  882. cpl.console = console;
  883. enum_processes( console_process_list_cb, &cpl );
  884. if (cpl.count * sizeof(process_id_t) > get_reply_max_size())
  885. {
  886. set_reply_data( &cpl.count, sizeof(cpl.count) );
  887. set_error( STATUS_BUFFER_TOO_SMALL );
  888. return;
  889. }
  890. cpl.size = cpl.count;
  891. cpl.count = 0;
  892. if ((cpl.processes = set_reply_data_size( cpl.size * sizeof(process_id_t) )))
  893. enum_processes( console_process_list_cb, &cpl );
  894. return;
  895. }
  896. default:
  897. if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
  898. {
  899. set_error( STATUS_INVALID_HANDLE );
  900. return;
  901. }
  902. queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
  903. }
  904. }
  905. static void console_read( struct fd *fd, struct async *async, file_pos_t pos )
  906. {
  907. struct console *console = get_fd_user( fd );
  908. if (!console->server)
  909. {
  910. set_error( STATUS_INVALID_HANDLE );
  911. return;
  912. }
  913. queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
  914. }
  915. static void console_flush( struct fd *fd, struct async *async )
  916. {
  917. struct console *console = get_fd_user( fd );
  918. if (!console->server)
  919. {
  920. set_error( STATUS_INVALID_HANDLE );
  921. return;
  922. }
  923. queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
  924. }
  925. static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
  926. {
  927. struct screen_buffer *screen_buffer = get_fd_user( fd );
  928. if (!screen_buffer->input || !screen_buffer->input->server)
  929. {
  930. set_error( STATUS_INVALID_HANDLE );
  931. return;
  932. }
  933. queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
  934. screen_buffer->id, async, &screen_buffer->ioctl_q );
  935. }
  936. static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  937. {
  938. struct screen_buffer *screen_buffer = get_fd_user( fd );
  939. switch (code)
  940. {
  941. case IOCTL_CONDRV_ACTIVATE:
  942. if (!screen_buffer->input)
  943. {
  944. set_error( STATUS_INVALID_HANDLE );
  945. return;
  946. }
  947. set_active_screen_buffer( screen_buffer->input, screen_buffer );
  948. return;
  949. default:
  950. if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
  951. is_blocking_read_ioctl( code ))
  952. {
  953. set_error( STATUS_INVALID_HANDLE );
  954. return;
  955. }
  956. queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
  957. async, &screen_buffer->ioctl_q );
  958. }
  959. }
  960. static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  961. {
  962. struct console_connection *console_connection = get_fd_user( fd );
  963. switch (code)
  964. {
  965. case IOCTL_CONDRV_BIND_PID:
  966. {
  967. struct process *process;
  968. unsigned int pid;
  969. if (get_req_data_size() != sizeof(unsigned int))
  970. {
  971. set_error( STATUS_INVALID_PARAMETER );
  972. return;
  973. }
  974. if (current->process->console)
  975. {
  976. set_error( STATUS_INVALID_HANDLE );
  977. return;
  978. }
  979. pid = *(unsigned int *)get_req_data();
  980. if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
  981. if (!(process = get_process_from_id( pid ))) return;
  982. if (process->console)
  983. current->process->console = (struct console *)grab_object( process->console );
  984. else set_error( STATUS_ACCESS_DENIED );
  985. release_object( process );
  986. return;
  987. }
  988. default:
  989. default_fd_ioctl( console_connection->fd, code, async );
  990. }
  991. }
  992. static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  993. {
  994. struct console_server *server = get_fd_user( fd );
  995. switch (code)
  996. {
  997. case IOCTL_CONDRV_CTRL_EVENT:
  998. {
  999. const struct condrv_ctrl_event *event = get_req_data();
  1000. if (get_req_data_size() != sizeof(*event))
  1001. {
  1002. set_error( STATUS_INVALID_PARAMETER );
  1003. return;
  1004. }
  1005. if (!server->console)
  1006. {
  1007. set_error( STATUS_INVALID_HANDLE );
  1008. return;
  1009. }
  1010. propagate_console_signal( server->console, event->event, event->group_id );
  1011. return;
  1012. }
  1013. case IOCTL_CONDRV_SETUP_INPUT:
  1014. {
  1015. struct termios term;
  1016. obj_handle_t handle;
  1017. struct file *file;
  1018. int unix_fd;
  1019. if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
  1020. {
  1021. set_error( STATUS_INVALID_PARAMETER );
  1022. return;
  1023. }
  1024. if (server->term_fd != -1)
  1025. {
  1026. tcsetattr( server->term_fd, TCSANOW, &server->termios );
  1027. close( server->term_fd );
  1028. server->term_fd = -1;
  1029. }
  1030. handle = *(unsigned int *)get_req_data();
  1031. if (!handle) return;
  1032. if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
  1033. {
  1034. return;
  1035. }
  1036. unix_fd = get_file_unix_fd( file );
  1037. release_object( file );
  1038. if (tcgetattr( unix_fd, &server->termios ))
  1039. {
  1040. file_set_error();
  1041. return;
  1042. }
  1043. term = server->termios;
  1044. term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
  1045. term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  1046. term.c_cflag &= ~(CSIZE | PARENB);
  1047. term.c_cflag |= CS8;
  1048. term.c_cc[VMIN] = 1;
  1049. term.c_cc[VTIME] = 0;
  1050. if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
  1051. file_set_error();
  1052. return;
  1053. }
  1054. default:
  1055. set_error( STATUS_INVALID_HANDLE );
  1056. return;
  1057. }
  1058. }
  1059. static void console_connection_dump( struct object *obj, int verbose )
  1060. {
  1061. fputs( "console connection\n", stderr );
  1062. }
  1063. static struct fd *console_connection_get_fd( struct object *obj )
  1064. {
  1065. struct console_connection *connection = (struct console_connection *)obj;
  1066. return (struct fd *)grab_object( connection->fd );
  1067. }
  1068. static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
  1069. unsigned int attr, struct object *root )
  1070. {
  1071. static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
  1072. if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
  1073. {
  1074. if (!current->process->console)
  1075. {
  1076. set_error( STATUS_INVALID_HANDLE );
  1077. return NULL;
  1078. }
  1079. name->len = 0;
  1080. return grab_object( current->process->console );
  1081. }
  1082. return NULL;
  1083. }
  1084. static struct object *console_connection_open_file( struct object *obj, unsigned int access,
  1085. unsigned int sharing, unsigned int options )
  1086. {
  1087. return grab_object( obj );
  1088. }
  1089. static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  1090. {
  1091. struct console *console = process->console;
  1092. if (console)
  1093. {
  1094. process->console = NULL;
  1095. release_object( console );
  1096. }
  1097. return 1;
  1098. }
  1099. static void console_connection_destroy( struct object *obj )
  1100. {
  1101. struct console_connection *connection = (struct console_connection *)obj;
  1102. if (connection->fd) release_object( connection->fd );
  1103. }
  1104. static void console_device_dump( struct object *obj, int verbose )
  1105. {
  1106. fputs( "Console device\n", stderr );
  1107. }
  1108. static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
  1109. unsigned int attr, struct object *root )
  1110. {
  1111. static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
  1112. static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
  1113. static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
  1114. static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
  1115. static const WCHAR inputW[] = {'I','n','p','u','t'};
  1116. static const WCHAR outputW[] = {'O','u','t','p','u','t'};
  1117. static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
  1118. static const WCHAR serverW[] = {'S','e','r','v','e','r'};
  1119. if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, 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. if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
  1130. {
  1131. if (!current->process->console || !current->process->console->active)
  1132. {
  1133. set_error( STATUS_INVALID_HANDLE );
  1134. return NULL;
  1135. }
  1136. name->len = 0;
  1137. return grab_object( current->process->console->active );
  1138. }
  1139. if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
  1140. {
  1141. name->len = 0;
  1142. return grab_object( obj );
  1143. }
  1144. if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
  1145. {
  1146. struct console_input *console_input;
  1147. name->len = 0;
  1148. if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
  1149. console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
  1150. FILE_SYNCHRONOUS_IO_NONALERT );
  1151. if (!console_input->fd)
  1152. {
  1153. release_object( console_input );
  1154. return NULL;
  1155. }
  1156. return &console_input->obj;
  1157. }
  1158. if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
  1159. {
  1160. struct console_output *console_output;
  1161. name->len = 0;
  1162. if (!(console_output = alloc_object( &console_output_ops ))) return NULL;
  1163. console_output->fd = alloc_pseudo_fd( &console_output_fd_ops, &console_output->obj,
  1164. FILE_SYNCHRONOUS_IO_NONALERT );
  1165. if (!console_output->fd)
  1166. {
  1167. release_object( console_output );
  1168. return NULL;
  1169. }
  1170. return &console_output->obj;
  1171. }
  1172. if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
  1173. {
  1174. if (!current->process->console)
  1175. {
  1176. set_error( STATUS_INVALID_HANDLE );
  1177. return NULL;
  1178. }
  1179. name->len = 0;
  1180. return create_screen_buffer( current->process->console );
  1181. }
  1182. if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
  1183. {
  1184. name->len = 0;
  1185. return create_console_server();
  1186. }
  1187. if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
  1188. {
  1189. name->len = 0;
  1190. return create_console_connection( NULL );
  1191. }
  1192. return NULL;
  1193. }
  1194. static struct object *console_device_open_file( struct object *obj, unsigned int access,
  1195. unsigned int sharing, unsigned int options )
  1196. {
  1197. int is_output;
  1198. access = default_map_access( obj, access );
  1199. is_output = access & FILE_WRITE_DATA;
  1200. if (!current->process->console || (is_output && !current->process->console))
  1201. {
  1202. set_error( STATUS_INVALID_HANDLE );
  1203. return NULL;
  1204. }
  1205. if (is_output && (access & FILE_READ_DATA))
  1206. {
  1207. set_error( STATUS_INVALID_PARAMETER );
  1208. return NULL;
  1209. }
  1210. return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
  1211. }
  1212. static void console_input_dump( struct object *obj, int verbose )
  1213. {
  1214. fputs( "console Input device\n", stderr );
  1215. }
  1216. static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
  1217. {
  1218. if (!current->process->console)
  1219. {
  1220. set_error( STATUS_ACCESS_DENIED );
  1221. return 0;
  1222. }
  1223. return console_add_queue( &current->process->console->obj, entry );
  1224. }
  1225. static struct fd *console_input_get_fd( struct object *obj )
  1226. {
  1227. struct console_input *console_input = (struct console_input *)obj;
  1228. assert( obj->ops == &console_input_ops );
  1229. return (struct fd *)grab_object( console_input->fd );
  1230. }
  1231. static struct object *console_input_open_file( struct object *obj, unsigned int access,
  1232. unsigned int sharing, unsigned int options )
  1233. {
  1234. return grab_object( obj );
  1235. }
  1236. static void console_input_destroy( struct object *obj )
  1237. {
  1238. struct console_input *console_input = (struct console_input *)obj;
  1239. assert( obj->ops == &console_input_ops );
  1240. if (console_input->fd) release_object( console_input->fd );
  1241. }
  1242. static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1243. {
  1244. struct console *console = current->process->console;
  1245. if (!console)
  1246. {
  1247. set_error( STATUS_INVALID_HANDLE );
  1248. return;
  1249. }
  1250. console_ioctl( console->fd, code, async );
  1251. }
  1252. static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
  1253. {
  1254. struct console *console = current->process->console;
  1255. if (!console)
  1256. {
  1257. set_error( STATUS_INVALID_HANDLE );
  1258. return;
  1259. }
  1260. console_read( console->fd, async, pos );
  1261. }
  1262. static void console_input_flush( struct fd *fd, struct async *async )
  1263. {
  1264. struct console *console = current->process->console;
  1265. if (!console)
  1266. {
  1267. set_error( STATUS_INVALID_HANDLE );
  1268. return;
  1269. }
  1270. console_flush( console->fd, async );
  1271. }
  1272. static void console_output_dump( struct object *obj, int verbose )
  1273. {
  1274. fputs( "console Output device\n", stderr );
  1275. }
  1276. static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry )
  1277. {
  1278. if (!current->process->console || !current->process->console->active)
  1279. {
  1280. set_error( STATUS_ACCESS_DENIED );
  1281. return 0;
  1282. }
  1283. return console_add_queue( &current->process->console->obj, entry );
  1284. }
  1285. static struct fd *console_output_get_fd( struct object *obj )
  1286. {
  1287. struct console_output *console_output = (struct console_output *)obj;
  1288. assert( obj->ops == &console_output_ops );
  1289. return (struct fd *)grab_object( console_output->fd );
  1290. }
  1291. static struct object *console_output_open_file( struct object *obj, unsigned int access,
  1292. unsigned int sharing, unsigned int options )
  1293. {
  1294. return grab_object( obj );
  1295. }
  1296. static void console_output_destroy( struct object *obj )
  1297. {
  1298. struct console_output *console_output = (struct console_output *)obj;
  1299. assert( obj->ops == &console_output_ops );
  1300. if (console_output->fd) release_object( console_output->fd );
  1301. }
  1302. static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
  1303. {
  1304. struct console *console = current->process->console;
  1305. if (!console || !console->active)
  1306. {
  1307. set_error( STATUS_INVALID_HANDLE );
  1308. return;
  1309. }
  1310. screen_buffer_ioctl( console->active->fd, code, async );
  1311. }
  1312. static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos )
  1313. {
  1314. struct console *console = current->process->console;
  1315. if (!console || !console->active)
  1316. {
  1317. set_error( STATUS_INVALID_HANDLE );
  1318. return;
  1319. }
  1320. screen_buffer_write( console->active->fd, async, pos );
  1321. }
  1322. struct object *create_console_device( struct object *root, const struct unicode_str *name,
  1323. unsigned int attr, const struct security_descriptor *sd )
  1324. {
  1325. return create_named_object( root, &console_device_ops, name, attr, sd );
  1326. }
  1327. /* retrieve the next pending console ioctl request */
  1328. DECL_HANDLER(get_next_console_request)
  1329. {
  1330. struct console_host_ioctl *ioctl = NULL, *next;
  1331. struct console_server *server;
  1332. struct iosb *iosb = NULL;
  1333. server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
  1334. if (!server) return;
  1335. if (!server->console)
  1336. {
  1337. set_error( STATUS_INVALID_HANDLE );
  1338. release_object( server );
  1339. return;
  1340. }
  1341. if (!server->console->renderer) server->console->renderer = current;
  1342. if (!req->signal) server->console->signaled = 0;
  1343. else if (!server->console->signaled)
  1344. {
  1345. server->console->signaled = 1;
  1346. wake_up( &server->console->obj, 0 );
  1347. }
  1348. if (req->read)
  1349. {
  1350. /* set result of current pending ioctl */
  1351. if (list_empty( &server->read_queue ))
  1352. {
  1353. set_error( STATUS_INVALID_HANDLE );
  1354. release_object( server );
  1355. return;
  1356. }
  1357. ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
  1358. list_remove( &ioctl->entry );
  1359. list_move_tail( &server->queue, &server->read_queue );
  1360. }
  1361. else if (server->busy)
  1362. {
  1363. /* set result of previous ioctl */
  1364. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1365. list_remove( &ioctl->entry );
  1366. }
  1367. if (ioctl)
  1368. {
  1369. struct async *async = ioctl->async;
  1370. unsigned int status = req->status;
  1371. if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
  1372. if (async)
  1373. {
  1374. iosb = async_get_iosb( async );
  1375. if (iosb->status == STATUS_PENDING)
  1376. {
  1377. data_size_t out_size = min( iosb->out_size, get_req_data_size() );
  1378. data_size_t result = ioctl->code == IOCTL_CONDRV_WRITE_FILE ? iosb->in_size : out_size;
  1379. async_request_complete_alloc( async, status, result, out_size, get_req_data() );
  1380. }
  1381. release_object( async );
  1382. }
  1383. free( ioctl );
  1384. if (iosb) release_object( iosb );
  1385. if (req->read)
  1386. {
  1387. release_object( server );
  1388. return;
  1389. }
  1390. server->busy = 0;
  1391. }
  1392. /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
  1393. * move it to read queue for execution after current read is complete. move all blocking
  1394. * ioctl at the same time to preserve their order. */
  1395. if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
  1396. {
  1397. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1398. if (is_blocking_read_ioctl( ioctl->code ))
  1399. {
  1400. LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
  1401. {
  1402. if (!is_blocking_read_ioctl( ioctl->code )) continue;
  1403. list_remove( &ioctl->entry );
  1404. list_add_tail( &server->read_queue, &ioctl->entry );
  1405. }
  1406. }
  1407. }
  1408. /* return the next ioctl */
  1409. if (!list_empty( &server->queue ))
  1410. {
  1411. ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
  1412. iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
  1413. if (!iosb || get_reply_max_size() >= iosb->in_size)
  1414. {
  1415. reply->code = ioctl->code;
  1416. reply->output = ioctl->output;
  1417. if (iosb)
  1418. {
  1419. reply->out_size = iosb->out_size;
  1420. set_reply_data_ptr( iosb->in_data, iosb->in_size );
  1421. iosb->in_data = NULL;
  1422. }
  1423. if (is_blocking_read_ioctl( ioctl->code ))
  1424. {
  1425. list_remove( &ioctl->entry );
  1426. assert( list_empty( &server->read_queue ));
  1427. list_add_tail( &server->read_queue, &ioctl->entry );
  1428. }
  1429. else server->busy = 1;
  1430. }
  1431. else
  1432. {
  1433. reply->out_size = iosb->in_size;
  1434. set_error( STATUS_BUFFER_OVERFLOW );
  1435. }
  1436. if (iosb) release_object( iosb );
  1437. }
  1438. else
  1439. {
  1440. set_error( STATUS_PENDING );
  1441. }
  1442. release_object( server );
  1443. }