2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
6 * Copyright 2020 Jacek Caban for CodeWeavers
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.
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.
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
25 #include "wine/port.h"
32 #include <sys/ioctl.h>
36 #define WIN32_NO_STATUS
44 #include "wine/condrv.h"
56 struct object obj
; /* object header */
57 int signaled
; /* is console signaled */
58 int num_proc
; /* number of processes attached to this console */
59 struct thread
*renderer
; /* console renderer thread */
60 struct screen_buffer
*active
; /* active screen buffer */
61 struct console_server
*server
; /* console server object */
62 unsigned int last_id
; /* id of last created console buffer */
63 struct fd
*fd
; /* for bare console, attached input fd */
64 struct async_queue ioctl_q
; /* ioctl queue */
65 struct async_queue read_q
; /* read queue */
68 static void console_input_dump( struct object
*obj
, int verbose
);
69 static void console_input_destroy( struct object
*obj
);
70 static int console_input_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
71 static struct fd
*console_input_get_fd( struct object
*obj
);
72 static struct object
*console_input_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
73 static struct object
*console_input_open_file( struct object
*obj
, unsigned int access
,
74 unsigned int sharing
, unsigned int options
);
76 static const struct object_ops console_input_ops
=
78 sizeof(struct console_input
), /* size */
79 console_input_dump
, /* dump */
80 no_get_type
, /* get_type */
81 add_queue
, /* add_queue */
82 remove_queue
, /* remove_queue */
83 console_input_signaled
, /* signaled */
84 no_satisfied
, /* satisfied */
85 no_signal
, /* signal */
86 console_input_get_fd
, /* get_fd */
87 default_fd_map_access
, /* map_access */
88 default_get_sd
, /* get_sd */
89 default_set_sd
, /* set_sd */
90 no_get_full_name
, /* get_full_name */
91 console_input_lookup_name
, /* lookup_name */
92 no_link_name
, /* link_name */
93 NULL
, /* unlink_name */
94 console_input_open_file
, /* open_file */
95 no_kernel_obj_list
, /* get_kernel_obj_list */
96 no_close_handle
, /* close_handle */
97 console_input_destroy
/* destroy */
100 static enum server_fd_type
console_get_fd_type( struct fd
*fd
);
101 static int console_input_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
102 static int console_input_flush( struct fd
*fd
, struct async
*async
);
103 static int console_input_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
105 static const struct fd_ops console_input_fd_ops
=
107 default_fd_get_poll_events
, /* get_poll_events */
108 default_poll_event
, /* poll_event */
109 console_get_fd_type
, /* get_fd_type */
110 console_input_read
, /* read */
111 no_fd_write
, /* write */
112 console_input_flush
, /* flush */
113 no_fd_get_file_info
, /* get_file_info */
114 no_fd_get_volume_info
, /* get_volume_info */
115 console_input_ioctl
, /* ioctl */
116 default_fd_queue_async
, /* queue_async */
117 default_fd_reselect_async
/* reselect_async */
120 struct console_host_ioctl
122 unsigned int code
; /* ioctl code */
123 int output
; /* output id for screen buffer ioctls */
124 struct async
*async
; /* ioctl async */
125 struct list entry
; /* list entry */
128 struct console_server
130 struct object obj
; /* object header */
131 struct fd
*fd
; /* pseudo-fd for ioctls */
132 struct console_input
*console
; /* attached console */
133 struct list queue
; /* ioctl queue */
134 struct list read_queue
; /* blocking read queue */
135 int busy
; /* flag if server processing an ioctl */
136 int term_fd
; /* UNIX terminal fd */
137 struct termios termios
; /* original termios */
140 static void console_server_dump( struct object
*obj
, int verbose
);
141 static void console_server_destroy( struct object
*obj
);
142 static int console_server_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
143 static struct fd
*console_server_get_fd( struct object
*obj
);
144 static struct object
*console_server_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
145 static struct object
*console_server_open_file( struct object
*obj
, unsigned int access
,
146 unsigned int sharing
, unsigned int options
);
148 static const struct object_ops console_server_ops
=
150 sizeof(struct console_server
), /* size */
151 console_server_dump
, /* dump */
152 no_get_type
, /* get_type */
153 add_queue
, /* add_queue */
154 remove_queue
, /* remove_queue */
155 console_server_signaled
, /* signaled */
156 no_satisfied
, /* satisfied */
157 no_signal
, /* signal */
158 console_server_get_fd
, /* get_fd */
159 default_fd_map_access
, /* map_access */
160 default_get_sd
, /* get_sd */
161 default_set_sd
, /* set_sd */
162 no_get_full_name
, /* get_full_name */
163 console_server_lookup_name
, /* lookup_name */
164 no_link_name
, /* link_name */
165 NULL
, /* unlink_name */
166 console_server_open_file
, /* open_file */
167 no_kernel_obj_list
, /* get_kernel_obj_list */
168 fd_close_handle
, /* close_handle */
169 console_server_destroy
/* destroy */
172 static int console_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
174 static const struct fd_ops console_server_fd_ops
=
176 default_fd_get_poll_events
, /* get_poll_events */
177 default_poll_event
, /* poll_event */
178 console_get_fd_type
, /* get_fd_type */
179 no_fd_read
, /* read */
180 no_fd_write
, /* write */
181 no_fd_flush
, /* flush */
182 no_fd_get_file_info
, /* get_file_info */
183 no_fd_get_volume_info
, /* get_volume_info */
184 console_server_ioctl
, /* ioctl */
185 default_fd_queue_async
, /* queue_async */
186 default_fd_reselect_async
/* reselect_async */
194 short int pitch_family
;
196 data_size_t face_len
;
201 struct object obj
; /* object header */
202 struct list entry
; /* entry in list of all screen buffers */
203 struct console_input
*input
; /* associated console input */
204 unsigned int id
; /* buffer id */
205 struct fd
*fd
; /* for bare console, attached output fd */
206 struct async_queue ioctl_q
; /* ioctl queue */
209 static void screen_buffer_dump( struct object
*obj
, int verbose
);
210 static void screen_buffer_destroy( struct object
*obj
);
211 static int screen_buffer_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
212 static struct fd
*screen_buffer_get_fd( struct object
*obj
);
213 static struct object
*screen_buffer_open_file( struct object
*obj
, unsigned int access
,
214 unsigned int sharing
, unsigned int options
);
216 static const struct object_ops screen_buffer_ops
=
218 sizeof(struct screen_buffer
), /* size */
219 screen_buffer_dump
, /* dump */
220 no_get_type
, /* get_type */
221 screen_buffer_add_queue
, /* add_queue */
222 NULL
, /* remove_queue */
224 NULL
, /* satisfied */
225 no_signal
, /* signal */
226 screen_buffer_get_fd
, /* get_fd */
227 default_fd_map_access
, /* map_access */
228 default_get_sd
, /* get_sd */
229 default_set_sd
, /* set_sd */
230 no_get_full_name
, /* get_full_name */
231 no_lookup_name
, /* lookup_name */
232 no_link_name
, /* link_name */
233 NULL
, /* unlink_name */
234 screen_buffer_open_file
, /* open_file */
235 no_kernel_obj_list
, /* get_kernel_obj_list */
236 no_close_handle
, /* close_handle */
237 screen_buffer_destroy
/* destroy */
240 static int screen_buffer_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
241 static int screen_buffer_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
243 static const struct fd_ops screen_buffer_fd_ops
=
245 default_fd_get_poll_events
, /* get_poll_events */
246 default_poll_event
, /* poll_event */
247 console_get_fd_type
, /* get_fd_type */
248 no_fd_read
, /* read */
249 screen_buffer_write
, /* write */
250 no_fd_flush
, /* flush */
251 no_fd_get_file_info
, /* get_file_info */
252 no_fd_get_volume_info
, /* get_volume_info */
253 screen_buffer_ioctl
, /* ioctl */
254 default_fd_queue_async
, /* queue_async */
255 default_fd_reselect_async
/* reselect_async */
258 static struct object_type
*console_device_get_type( struct object
*obj
);
259 static void console_device_dump( struct object
*obj
, int verbose
);
260 static struct object
*console_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
261 static struct object
*console_device_open_file( struct object
*obj
, unsigned int access
,
262 unsigned int sharing
, unsigned int options
);
264 static const struct object_ops console_device_ops
=
266 sizeof(struct object
), /* size */
267 console_device_dump
, /* dump */
268 console_device_get_type
, /* get_type */
269 no_add_queue
, /* add_queue */
270 NULL
, /* remove_queue */
272 no_satisfied
, /* satisfied */
273 no_signal
, /* signal */
274 no_get_fd
, /* get_fd */
275 default_fd_map_access
, /* map_access */
276 default_get_sd
, /* get_sd */
277 default_set_sd
, /* set_sd */
278 default_get_full_name
, /* get_full_name */
279 console_device_lookup_name
, /* lookup_name */
280 directory_link_name
, /* link_name */
281 default_unlink_name
, /* unlink_name */
282 console_device_open_file
, /* open_file */
283 no_kernel_obj_list
, /* get_kernel_obj_list */
284 no_close_handle
, /* close_handle */
285 no_destroy
/* destroy */
288 static void input_device_dump( struct object
*obj
, int verbose
);
289 static struct object
*input_device_open_file( struct object
*obj
, unsigned int access
,
290 unsigned int sharing
, unsigned int options
);
291 static int input_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
292 static struct fd
*input_device_get_fd( struct object
*obj
);
294 static const struct object_ops input_device_ops
=
296 sizeof(struct object
), /* size */
297 input_device_dump
, /* dump */
298 console_device_get_type
, /* get_type */
299 input_device_add_queue
, /* add_queue */
300 NULL
, /* remove_queue */
302 no_satisfied
, /* satisfied */
303 no_signal
, /* signal */
304 input_device_get_fd
, /* get_fd */
305 no_map_access
, /* map_access */
306 default_get_sd
, /* get_sd */
307 default_set_sd
, /* set_sd */
308 no_get_full_name
, /* get_full_name */
309 no_lookup_name
, /* lookup_name */
310 directory_link_name
, /* link_name */
311 default_unlink_name
, /* unlink_name */
312 input_device_open_file
, /* open_file */
313 no_kernel_obj_list
, /* get_kernel_obj_list */
314 no_close_handle
, /* close_handle */
315 no_destroy
/* destroy */
318 static void output_device_dump( struct object
*obj
, int verbose
);
319 static int output_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
320 static struct fd
*output_device_get_fd( struct object
*obj
);
321 static struct object
*output_device_open_file( struct object
*obj
, unsigned int access
,
322 unsigned int sharing
, unsigned int options
);
324 static const struct object_ops output_device_ops
=
326 sizeof(struct object
), /* size */
327 output_device_dump
, /* dump */
328 console_device_get_type
, /* get_type */
329 output_device_add_queue
, /* add_queue */
330 NULL
, /* remove_queue */
332 no_satisfied
, /* satisfied */
333 no_signal
, /* signal */
334 output_device_get_fd
, /* get_fd */
335 no_map_access
, /* map_access */
336 default_get_sd
, /* get_sd */
337 default_set_sd
, /* set_sd */
338 no_get_full_name
, /* get_full_name */
339 no_lookup_name
, /* lookup_name */
340 directory_link_name
, /* link_name */
341 default_unlink_name
, /* unlink_name */
342 output_device_open_file
, /* open_file */
343 no_kernel_obj_list
, /* get_kernel_obj_list */
344 no_close_handle
, /* close_handle */
345 no_destroy
/* destroy */
348 struct console_connection
350 struct object obj
; /* object header */
351 struct fd
*fd
; /* pseudo-fd for ioctls */
354 static void console_connection_dump( struct object
*obj
, int verbose
);
355 static struct fd
*console_connection_get_fd( struct object
*obj
);
356 static struct object
*console_connection_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
357 static struct object
*console_connection_open_file( struct object
*obj
, unsigned int access
,
358 unsigned int sharing
, unsigned int options
);
359 static int console_connection_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
360 static void console_connection_destroy( struct object
*obj
);
362 static const struct object_ops console_connection_ops
=
364 sizeof(struct console_connection
),/* size */
365 console_connection_dump
, /* dump */
366 console_device_get_type
, /* get_type */
367 no_add_queue
, /* add_queue */
368 NULL
, /* remove_queue */
370 no_satisfied
, /* satisfied */
371 no_signal
, /* signal */
372 console_connection_get_fd
, /* get_fd */
373 no_map_access
, /* map_access */
374 default_get_sd
, /* get_sd */
375 default_set_sd
, /* set_sd */
376 no_get_full_name
, /* get_full_name */
377 console_connection_lookup_name
, /* lookup_name */
378 directory_link_name
, /* link_name */
379 default_unlink_name
, /* unlink_name */
380 console_connection_open_file
, /* open_file */
381 no_kernel_obj_list
, /* get_kernel_obj_list */
382 console_connection_close_handle
, /* close_handle */
383 console_connection_destroy
/* destroy */
386 static int console_connection_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
388 static const struct fd_ops console_connection_fd_ops
=
390 default_fd_get_poll_events
, /* get_poll_events */
391 default_poll_event
, /* poll_event */
392 console_get_fd_type
, /* get_fd_type */
393 no_fd_read
, /* read */
394 no_fd_write
, /* write */
395 no_fd_flush
, /* flush */
396 no_fd_get_file_info
, /* get_file_info */
397 no_fd_get_volume_info
, /* get_volume_info */
398 console_connection_ioctl
, /* ioctl */
399 default_fd_queue_async
, /* queue_async */
400 default_fd_reselect_async
/* reselect_async */
403 static struct list screen_buffer_list
= LIST_INIT(screen_buffer_list
);
405 static int console_input_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
407 struct console_input
*console
= (struct console_input
*)obj
;
408 return console
->signaled
;
411 static struct fd
*console_input_get_fd( struct object
* obj
)
413 struct console_input
*console_input
= (struct console_input
*)obj
;
414 assert( obj
->ops
== &console_input_ops
);
415 return (struct fd
*)grab_object( console_input
->fd
);
418 static enum server_fd_type
console_get_fd_type( struct fd
*fd
)
423 static struct object
*create_console_input(void)
425 struct console_input
*console_input
;
427 if (!(console_input
= alloc_object( &console_input_ops
)))
430 console_input
->renderer
= NULL
;
431 console_input
->signaled
= 0;
432 console_input
->num_proc
= 0;
433 console_input
->active
= NULL
;
434 console_input
->server
= NULL
;
435 console_input
->fd
= NULL
;
436 console_input
->last_id
= 0;
437 init_async_queue( &console_input
->ioctl_q
);
438 init_async_queue( &console_input
->read_q
);
440 console_input
->fd
= alloc_pseudo_fd( &console_input_fd_ops
, &console_input
->obj
,
441 FILE_SYNCHRONOUS_IO_NONALERT
);
442 if (!console_input
->fd
)
444 release_object( console_input
);
447 allow_fd_caching( console_input
->fd
);
448 return &console_input
->obj
;
451 static void console_host_ioctl_terminate( struct console_host_ioctl
*call
, unsigned int status
)
455 async_terminate( call
->async
, status
);
456 release_object( call
->async
);
461 static int queue_host_ioctl( struct console_server
*server
, unsigned int code
, unsigned int output
,
462 struct async
*async
, struct async_queue
*queue
)
464 struct console_host_ioctl
*ioctl
;
466 if (!(ioctl
= mem_alloc( sizeof(*ioctl
) ))) return 0;
468 ioctl
->output
= output
;
472 ioctl
->async
= (struct async
*)grab_object( async
);
473 queue_async( queue
, async
);
475 list_add_tail( &server
->queue
, &ioctl
->entry
);
476 wake_up( &server
->obj
, 0 );
477 if (async
) set_error( STATUS_PENDING
);
481 static void disconnect_console_server( struct console_server
*server
)
483 while (!list_empty( &server
->queue
))
485 struct console_host_ioctl
*call
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
486 list_remove( &call
->entry
);
487 console_host_ioctl_terminate( call
, STATUS_CANCELLED
);
489 while (!list_empty( &server
->read_queue
))
491 struct console_host_ioctl
*call
= LIST_ENTRY( list_head( &server
->read_queue
), struct console_host_ioctl
, entry
);
492 list_remove( &call
->entry
);
493 console_host_ioctl_terminate( call
, STATUS_CANCELLED
);
496 if (server
->term_fd
!= -1)
498 tcsetattr( server
->term_fd
, TCSANOW
, &server
->termios
);
499 close( server
->term_fd
);
500 server
->term_fd
= -1;
505 assert( server
->console
->server
== server
);
506 server
->console
->server
= NULL
;
507 server
->console
= NULL
;
508 wake_up( &server
->obj
, 0 );
512 static void set_active_screen_buffer( struct console_input
*console_input
, struct screen_buffer
*screen_buffer
)
514 if (console_input
->active
== screen_buffer
) return;
515 if (console_input
->active
) release_object( console_input
->active
);
516 console_input
->active
= (struct screen_buffer
*)grab_object( screen_buffer
);
518 if (console_input
->server
) queue_host_ioctl( console_input
->server
, IOCTL_CONDRV_ACTIVATE
,
519 screen_buffer
->id
, NULL
, NULL
);
522 static struct object
*create_console_output( struct console_input
*console_input
)
524 struct screen_buffer
*screen_buffer
;
526 if (console_input
->last_id
== ~0)
528 set_error( STATUS_NO_MEMORY
);
532 if (!(screen_buffer
= alloc_object( &screen_buffer_ops
)))
535 screen_buffer
->id
= ++console_input
->last_id
;
536 screen_buffer
->input
= console_input
;
537 init_async_queue( &screen_buffer
->ioctl_q
);
538 list_add_head( &screen_buffer_list
, &screen_buffer
->entry
);
540 screen_buffer
->fd
= alloc_pseudo_fd( &screen_buffer_fd_ops
, &screen_buffer
->obj
,
541 FILE_SYNCHRONOUS_IO_NONALERT
);
542 if (!screen_buffer
->fd
)
544 release_object( screen_buffer
);
547 allow_fd_caching(screen_buffer
->fd
);
549 if (console_input
->server
) queue_host_ioctl( console_input
->server
, IOCTL_CONDRV_INIT_OUTPUT
,
550 screen_buffer
->id
, NULL
, NULL
);
551 if (!console_input
->active
) set_active_screen_buffer( console_input
, screen_buffer
);
552 return &screen_buffer
->obj
;
555 /* free the console for this process */
556 int free_console( struct process
*process
)
558 struct console_input
* console
= process
->console
;
560 if (!console
) return 0;
562 process
->console
= NULL
;
564 release_object( console
);
569 /* let process inherit the console from parent... this handle two cases :
570 * 1/ generic console inheritance
571 * 2/ parent is a renderer which launches process, and process should attach to the console
574 obj_handle_t
inherit_console( struct thread
*parent_thread
, obj_handle_t handle
, struct process
*process
,
575 obj_handle_t hconin
)
577 struct console_input
*console
= NULL
;
579 if (handle
) return duplicate_handle( current
->process
, handle
, process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
581 /* if parent is a renderer, then attach current process to its console
584 if (hconin
&& parent_thread
)
586 /* FIXME: should we check some access rights ? */
587 if (!(console
= (struct console_input
*)get_handle_obj( parent_thread
->process
, hconin
,
588 0, &console_input_ops
)))
589 clear_error(); /* ignore error */
591 if (!console
) return 0;
593 process
->console
= console
;
595 return alloc_handle( process
, process
->console
,
596 SYNCHRONIZE
| GENERIC_READ
| GENERIC_WRITE
, 0 );
599 struct thread
*console_get_renderer( struct console_input
*console
)
601 return console
->renderer
;
604 struct console_signal_info
606 struct console_input
*console
;
611 static int propagate_console_signal_cb(struct process
*process
, void *user
)
613 struct console_signal_info
* csi
= (struct console_signal_info
*)user
;
615 if (process
->console
== csi
->console
&& process
->running_threads
&&
616 (!csi
->group
|| process
->group_id
== csi
->group
))
618 /* find a suitable thread to signal */
619 struct thread
*thread
;
620 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
622 if (send_thread_signal( thread
, csi
->signal
)) break;
628 static void propagate_console_signal( struct console_input
*console
,
629 int sig
, process_id_t group_id
)
631 struct console_signal_info csi
;
635 set_error( STATUS_INVALID_PARAMETER
);
638 /* FIXME: should support the other events (like CTRL_BREAK) */
639 if (sig
!= CTRL_C_EVENT
)
641 set_error( STATUS_NOT_IMPLEMENTED
);
644 csi
.console
= console
;
646 csi
.group
= group_id
;
648 enum_processes(propagate_console_signal_cb
, &csi
);
652 static void console_input_dump( struct object
*obj
, int verbose
)
654 struct console_input
*console
= (struct console_input
*)obj
;
655 assert( obj
->ops
== &console_input_ops
);
656 fprintf( stderr
, "Console input active=%p server=%p\n",
657 console
->active
, console
->server
);
660 static void console_input_destroy( struct object
*obj
)
662 struct console_input
*console_in
= (struct console_input
*)obj
;
663 struct screen_buffer
*curr
;
665 assert( obj
->ops
== &console_input_ops
);
667 if (console_in
->server
)
669 assert( console_in
->server
->console
== console_in
);
670 disconnect_console_server( console_in
->server
);
673 if (console_in
->active
) release_object( console_in
->active
);
674 console_in
->active
= NULL
;
676 LIST_FOR_EACH_ENTRY( curr
, &screen_buffer_list
, struct screen_buffer
, entry
)
678 if (curr
->input
== console_in
) curr
->input
= NULL
;
681 free_async_queue( &console_in
->ioctl_q
);
682 free_async_queue( &console_in
->read_q
);
684 release_object( console_in
->fd
);
687 static struct object
*create_console_connection( struct console_input
*console
)
689 struct console_connection
*connection
;
691 if (current
->process
->console
)
693 set_error( STATUS_ACCESS_DENIED
);
697 if (!(connection
= alloc_object( &console_connection_ops
))) return NULL
;
698 if (!(connection
->fd
= alloc_pseudo_fd( &console_connection_fd_ops
, &connection
->obj
, 0 )))
700 release_object( connection
);
706 current
->process
->console
= (struct console_input
*)grab_object( console
);
710 return &connection
->obj
;
713 static struct object
*console_input_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
715 struct console_input
*console
= (struct console_input
*)obj
;
716 static const WCHAR connectionW
[] = {'C','o','n','n','e','c','t','i','o','n'};
717 assert( obj
->ops
== &console_input_ops
);
719 if (name
->len
== sizeof(connectionW
) && !memcmp( name
->str
, connectionW
, name
->len
))
722 return create_console_connection( console
);
728 static struct object
*console_input_open_file( struct object
*obj
, unsigned int access
,
729 unsigned int sharing
, unsigned int options
)
731 return grab_object( obj
);
734 static void screen_buffer_dump( struct object
*obj
, int verbose
)
736 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
737 assert( obj
->ops
== &screen_buffer_ops
);
739 fprintf(stderr
, "Console screen buffer input=%p\n", screen_buffer
->input
);
742 static void screen_buffer_destroy( struct object
*obj
)
744 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
746 assert( obj
->ops
== &screen_buffer_ops
);
748 list_remove( &screen_buffer
->entry
);
749 if (screen_buffer
->input
&& screen_buffer
->input
->server
)
750 queue_host_ioctl( screen_buffer
->input
->server
, IOCTL_CONDRV_CLOSE_OUTPUT
,
751 screen_buffer
->id
, NULL
, NULL
);
752 if (screen_buffer
->fd
) release_object( screen_buffer
->fd
);
753 free_async_queue( &screen_buffer
->ioctl_q
);
756 static struct object
*screen_buffer_open_file( struct object
*obj
, unsigned int access
,
757 unsigned int sharing
, unsigned int options
)
759 return grab_object( obj
);
762 static int screen_buffer_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
764 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
765 if (!screen_buffer
->input
)
767 set_error( STATUS_ACCESS_DENIED
);
770 return add_queue( &screen_buffer
->input
->obj
, entry
);
773 static struct fd
*screen_buffer_get_fd( struct object
*obj
)
775 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
776 assert( obj
->ops
== &screen_buffer_ops
);
777 if (screen_buffer
->fd
)
778 return (struct fd
*)grab_object( screen_buffer
->fd
);
779 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
783 static void console_server_dump( struct object
*obj
, int verbose
)
785 assert( obj
->ops
== &console_server_ops
);
786 fprintf( stderr
, "Console server\n" );
789 static void console_server_destroy( struct object
*obj
)
791 struct console_server
*server
= (struct console_server
*)obj
;
792 assert( obj
->ops
== &console_server_ops
);
793 disconnect_console_server( server
);
794 if (server
->fd
) release_object( server
->fd
);
797 static struct object
*console_server_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
799 struct console_server
*server
= (struct console_server
*)obj
;
800 static const WCHAR referenceW
[] = {'R','e','f','e','r','e','n','c','e'};
801 assert( obj
->ops
== &console_server_ops
);
803 if (name
->len
== sizeof(referenceW
) && !memcmp( name
->str
, referenceW
, name
->len
))
805 struct screen_buffer
*screen_buffer
;
809 set_error( STATUS_INVALID_HANDLE
);
812 if (!(server
->console
= (struct console_input
*)create_console_input())) return NULL
;
813 if (!(screen_buffer
= (struct screen_buffer
*)create_console_output( server
->console
)))
815 release_object( server
->console
);
816 server
->console
= NULL
;
819 release_object( screen_buffer
);
820 server
->console
->server
= server
;
822 return &server
->console
->obj
;
828 static int console_server_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
830 struct console_server
*server
= (struct console_server
*)obj
;
831 assert( obj
->ops
== &console_server_ops
);
832 return !server
->console
|| !list_empty( &server
->queue
);
835 static struct fd
*console_server_get_fd( struct object
* obj
)
837 struct console_server
*server
= (struct console_server
*)obj
;
838 assert( obj
->ops
== &console_server_ops
);
839 return (struct fd
*)grab_object( server
->fd
);
842 static struct object
*console_server_open_file( struct object
*obj
, unsigned int access
,
843 unsigned int sharing
, unsigned int options
)
845 return grab_object( obj
);
848 static struct object
*create_console_server( void )
850 struct console_server
*server
;
852 if (!(server
= alloc_object( &console_server_ops
))) return NULL
;
853 server
->console
= NULL
;
855 server
->term_fd
= -1;
856 list_init( &server
->queue
);
857 list_init( &server
->read_queue
);
858 server
->fd
= alloc_pseudo_fd( &console_server_fd_ops
, &server
->obj
, FILE_SYNCHRONOUS_IO_NONALERT
);
861 release_object( server
);
864 allow_fd_caching(server
->fd
);
869 static int is_blocking_read_ioctl( unsigned int code
)
873 case IOCTL_CONDRV_READ_INPUT
:
874 case IOCTL_CONDRV_READ_CONSOLE
:
875 case IOCTL_CONDRV_READ_FILE
:
882 static int console_input_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
884 struct console_input
*console
= get_fd_user( fd
);
888 case IOCTL_CONDRV_CTRL_EVENT
:
890 const struct condrv_ctrl_event
*event
= get_req_data();
892 if (get_req_data_size() != sizeof(*event
))
894 set_error( STATUS_INVALID_PARAMETER
);
897 group
= event
->group_id
? event
->group_id
: current
->process
->group_id
;
900 set_error( STATUS_INVALID_PARAMETER
);
903 propagate_console_signal( console
, event
->event
, group
);
908 if (!console
->server
|| code
>> 16 != FILE_DEVICE_CONSOLE
)
910 set_error( STATUS_INVALID_HANDLE
);
913 return queue_host_ioctl( console
->server
, code
, 0, async
, &console
->ioctl_q
);
917 static int console_input_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
919 struct console_input
*console
= get_fd_user( fd
);
921 if (!console
->server
)
923 set_error( STATUS_INVALID_HANDLE
);
926 return queue_host_ioctl( console
->server
, IOCTL_CONDRV_READ_FILE
, 0, async
, &console
->ioctl_q
);
929 static int console_input_flush( struct fd
*fd
, struct async
*async
)
931 struct console_input
*console
= get_fd_user( fd
);
933 if (!console
->server
)
935 set_error( STATUS_INVALID_HANDLE
);
938 return queue_host_ioctl( console
->server
, IOCTL_CONDRV_FLUSH
, 0, NULL
, NULL
);
941 static int screen_buffer_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
943 struct screen_buffer
*screen_buffer
= get_fd_user( fd
);
946 if (!screen_buffer
->input
|| !screen_buffer
->input
->server
)
948 set_error( STATUS_INVALID_HANDLE
);
952 if (!queue_host_ioctl( screen_buffer
->input
->server
, IOCTL_CONDRV_WRITE_FILE
,
953 screen_buffer
->id
, async
, &screen_buffer
->ioctl_q
))
956 /* we can't use default async handling, because write result is not
957 * compatible with ioctl result */
958 iosb
= async_get_iosb( async
);
959 iosb
->status
= STATUS_SUCCESS
;
960 iosb
->result
= iosb
->in_size
;
961 async_terminate( async
, iosb
->result
? STATUS_ALERTED
: STATUS_SUCCESS
);
962 release_object( iosb
);
966 static int screen_buffer_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
968 struct screen_buffer
*screen_buffer
= get_fd_user( fd
);
972 case IOCTL_CONDRV_ACTIVATE
:
973 if (!screen_buffer
->input
)
975 set_error( STATUS_INVALID_HANDLE
);
979 set_active_screen_buffer( screen_buffer
->input
, screen_buffer
);
983 if (!screen_buffer
->input
|| !screen_buffer
->input
->server
|| code
>> 16 != FILE_DEVICE_CONSOLE
||
984 is_blocking_read_ioctl( code
))
986 set_error( STATUS_INVALID_HANDLE
);
989 return queue_host_ioctl( screen_buffer
->input
->server
, code
, screen_buffer
->id
,
990 async
, &screen_buffer
->ioctl_q
);
994 static int console_connection_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
996 struct console_connection
*console_connection
= get_fd_user( fd
);
1000 case IOCTL_CONDRV_BIND_PID
:
1002 struct process
*process
;
1004 if (get_req_data_size() != sizeof(unsigned int))
1006 set_error( STATUS_INVALID_PARAMETER
);
1009 if (current
->process
->console
)
1011 set_error( STATUS_INVALID_HANDLE
);
1015 pid
= *(unsigned int *)get_req_data();
1016 if (pid
== ATTACH_PARENT_PROCESS
) pid
= current
->process
->parent_id
;
1017 if (!(process
= get_process_from_id( pid
))) return 0;
1019 if (process
->console
)
1021 current
->process
->console
= (struct console_input
*)grab_object( process
->console
);
1022 process
->console
->num_proc
++;
1024 else set_error( STATUS_ACCESS_DENIED
);
1025 release_object( process
);
1026 return !get_error();
1030 return default_fd_ioctl( console_connection
->fd
, code
, async
);
1034 static int console_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1036 struct console_server
*server
= get_fd_user( fd
);
1040 case IOCTL_CONDRV_CTRL_EVENT
:
1042 const struct condrv_ctrl_event
*event
= get_req_data();
1043 if (get_req_data_size() != sizeof(*event
))
1045 set_error( STATUS_INVALID_PARAMETER
);
1048 if (!server
->console
)
1050 set_error( STATUS_INVALID_HANDLE
);
1053 propagate_console_signal( server
->console
, event
->event
, event
->group_id
);
1054 return !get_error();
1057 case IOCTL_CONDRV_SETUP_INPUT
:
1059 struct termios term
;
1060 obj_handle_t handle
;
1064 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1066 set_error( STATUS_INVALID_PARAMETER
);
1069 if (server
->term_fd
!= -1)
1071 tcsetattr( server
->term_fd
, TCSANOW
, &server
->termios
);
1072 close( server
->term_fd
);
1073 server
->term_fd
= -1;
1075 handle
= *(unsigned int *)get_req_data();
1076 if (!handle
) return 1;
1077 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
)))
1081 unix_fd
= get_file_unix_fd( file
);
1082 release_object( file
);
1084 if (tcgetattr( unix_fd
, &server
->termios
))
1089 term
= server
->termios
;
1090 term
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1091 term
.c_iflag
&= ~(BRKINT
| ICRNL
| INPCK
| ISTRIP
| IXON
);
1092 term
.c_cflag
&= ~(CSIZE
| PARENB
);
1093 term
.c_cflag
|= CS8
;
1094 term
.c_cc
[VMIN
] = 1;
1095 term
.c_cc
[VTIME
] = 0;
1096 if (tcsetattr( unix_fd
, TCSANOW
, &term
) || (server
->term_fd
= dup( unix_fd
)) == -1)
1105 set_error( STATUS_INVALID_HANDLE
);
1110 static void console_connection_dump( struct object
*obj
, int verbose
)
1112 fputs( "console connection\n", stderr
);
1115 static struct fd
*console_connection_get_fd( struct object
*obj
)
1117 struct console_connection
*connection
= (struct console_connection
*)obj
;
1118 return (struct fd
*)grab_object( connection
->fd
);
1121 static struct object
*console_connection_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1123 static const WCHAR referenceW
[] = {'R','e','f','e','r','e','n','c','e'};
1125 if (name
->len
== sizeof(referenceW
) && !memcmp( name
->str
, referenceW
, name
->len
))
1127 if (!current
->process
->console
)
1129 set_error( STATUS_INVALID_HANDLE
);
1133 return grab_object( current
->process
->console
);
1139 static struct object
*console_connection_open_file( struct object
*obj
, unsigned int access
,
1140 unsigned int sharing
, unsigned int options
)
1142 return grab_object( obj
);
1145 static int console_connection_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1147 free_console( process
);
1151 static void console_connection_destroy( struct object
*obj
)
1153 struct console_connection
*connection
= (struct console_connection
*)obj
;
1154 if (connection
->fd
) release_object( connection
->fd
);
1157 static struct object_type
*console_device_get_type( struct object
*obj
)
1159 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1160 static const struct unicode_str str
= { name
, sizeof(name
) };
1161 return get_object_type( &str
);
1164 static void console_device_dump( struct object
*obj
, int verbose
)
1166 fputs( "Console device\n", stderr
);
1169 static struct object
*console_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1171 static const WCHAR connectionW
[] = {'C','o','n','n','e','c','t','i','o','n'};
1172 static const WCHAR consoleW
[] = {'C','o','n','s','o','l','e'};
1173 static const WCHAR current_inW
[] = {'C','u','r','r','e','n','t','I','n'};
1174 static const WCHAR current_outW
[] = {'C','u','r','r','e','n','t','O','u','t'};
1175 static const WCHAR inputW
[] = {'I','n','p','u','t'};
1176 static const WCHAR outputW
[] = {'O','u','t','p','u','t'};
1177 static const WCHAR screen_bufferW
[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1178 static const WCHAR serverW
[] = {'S','e','r','v','e','r'};
1180 if (name
->len
== sizeof(current_inW
) && !memcmp( name
->str
, current_inW
, name
->len
))
1182 if (!current
->process
->console
)
1184 set_error( STATUS_INVALID_HANDLE
);
1188 return grab_object( current
->process
->console
);
1191 if (name
->len
== sizeof(current_outW
) && !memcmp( name
->str
, current_outW
, name
->len
))
1193 if (!current
->process
->console
|| !current
->process
->console
->active
)
1195 set_error( STATUS_INVALID_HANDLE
);
1199 return grab_object( current
->process
->console
->active
);
1202 if (name
->len
== sizeof(consoleW
) && !memcmp( name
->str
, consoleW
, name
->len
))
1205 return grab_object( obj
);
1208 if (name
->len
== sizeof(inputW
) && !memcmp( name
->str
, inputW
, name
->len
))
1211 return alloc_object( &input_device_ops
);
1214 if (name
->len
== sizeof(outputW
) && !memcmp( name
->str
, outputW
, name
->len
))
1217 return alloc_object( &output_device_ops
);
1220 if (name
->len
== sizeof(screen_bufferW
) && !memcmp( name
->str
, screen_bufferW
, name
->len
))
1222 if (!current
->process
->console
)
1224 set_error( STATUS_INVALID_HANDLE
);
1228 return create_console_output( current
->process
->console
);
1231 if (name
->len
== sizeof(serverW
) && !memcmp( name
->str
, serverW
, name
->len
))
1234 return create_console_server();
1237 if (name
->len
== sizeof(connectionW
) && !memcmp( name
->str
, connectionW
, name
->len
))
1240 return create_console_connection( NULL
);
1246 static struct object
*console_device_open_file( struct object
*obj
, unsigned int access
,
1247 unsigned int sharing
, unsigned int options
)
1250 access
= default_fd_map_access( obj
, access
);
1251 is_output
= access
& FILE_WRITE_DATA
;
1252 if (!current
->process
->console
|| (is_output
&& !current
->process
->console
))
1254 set_error( STATUS_INVALID_HANDLE
);
1257 if (is_output
&& (access
& FILE_READ_DATA
))
1259 set_error( STATUS_INVALID_PARAMETER
);
1262 return is_output
? grab_object( current
->process
->console
->active
) : grab_object( current
->process
->console
);
1265 static void input_device_dump( struct object
*obj
, int verbose
)
1267 fputs( "console Input device\n", stderr
);
1270 static int input_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1272 if (!current
->process
->console
)
1274 set_error( STATUS_ACCESS_DENIED
);
1277 return add_queue( ¤t
->process
->console
->obj
, entry
);
1280 static struct fd
*input_device_get_fd( struct object
*obj
)
1282 if (!current
->process
->console
)
1284 set_error( STATUS_ACCESS_DENIED
);
1287 return get_obj_fd( ¤t
->process
->console
->obj
);
1290 static struct object
*input_device_open_file( struct object
*obj
, unsigned int access
,
1291 unsigned int sharing
, unsigned int options
)
1293 return grab_object( obj
);
1296 static void output_device_dump( struct object
*obj
, int verbose
)
1298 fputs( "console Output device\n", stderr
);
1301 static int output_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1303 if (!current
->process
->console
|| !current
->process
->console
->active
)
1305 set_error( STATUS_ACCESS_DENIED
);
1308 return add_queue( ¤t
->process
->console
->obj
, entry
);
1311 static struct fd
*output_device_get_fd( struct object
*obj
)
1313 if (!current
->process
->console
|| !current
->process
->console
->active
)
1315 set_error( STATUS_ACCESS_DENIED
);
1319 return get_obj_fd( ¤t
->process
->console
->active
->obj
);
1322 static struct object
*output_device_open_file( struct object
*obj
, unsigned int access
,
1323 unsigned int sharing
, unsigned int options
)
1325 return grab_object( obj
);
1328 struct object
*create_console_device( struct object
*root
, const struct unicode_str
*name
,
1329 unsigned int attr
, const struct security_descriptor
*sd
)
1331 return create_named_object( root
, &console_device_ops
, name
, attr
, sd
);
1334 /* retrieve the next pending console ioctl request */
1335 DECL_HANDLER(get_next_console_request
)
1337 struct console_host_ioctl
*ioctl
= NULL
, *next
;
1338 struct console_server
*server
;
1339 struct iosb
*iosb
= NULL
;
1341 server
= (struct console_server
*)get_handle_obj( current
->process
, req
->handle
, 0, &console_server_ops
);
1342 if (!server
) return;
1344 if (!server
->console
)
1346 set_error( STATUS_INVALID_HANDLE
);
1347 release_object( server
);
1351 if (!req
->signal
) server
->console
->signaled
= 0;
1352 else if (!server
->console
->signaled
)
1354 server
->console
->signaled
= 1;
1355 wake_up( &server
->console
->obj
, 0 );
1360 /* set result of current pending ioctl */
1361 if (list_empty( &server
->read_queue
))
1363 set_error( STATUS_INVALID_HANDLE
);
1364 release_object( server
);
1368 ioctl
= LIST_ENTRY( list_head( &server
->read_queue
), struct console_host_ioctl
, entry
);
1369 list_remove( &ioctl
->entry
);
1370 list_move_tail( &server
->queue
, &server
->read_queue
);
1372 else if (server
->busy
)
1374 /* set result of previous ioctl */
1375 ioctl
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
1376 list_remove( &ioctl
->entry
);
1381 unsigned int status
= req
->status
;
1382 if (status
== STATUS_PENDING
) status
= STATUS_INVALID_PARAMETER
;
1385 iosb
= async_get_iosb( ioctl
->async
);
1386 if (iosb
->status
== STATUS_PENDING
)
1388 iosb
->status
= status
;
1389 iosb
->out_size
= min( iosb
->out_size
, get_req_data_size() );
1392 if ((iosb
->out_data
= memdup( get_req_data(), iosb
->out_size
)))
1394 iosb
->result
= iosb
->out_size
;
1395 status
= STATUS_ALERTED
;
1399 iosb
->status
= STATUS_NO_MEMORY
;
1406 release_object( ioctl
->async
);
1407 ioctl
->async
= NULL
;
1410 console_host_ioctl_terminate( ioctl
, status
);
1411 if (iosb
) release_object( iosb
);
1415 release_object( server
);
1421 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1422 * move it to read queue for execution after current read is complete. move all blocking
1423 * ioctl at the same time to preserve their order. */
1424 if (!list_empty( &server
->queue
) && !list_empty( &server
->read_queue
))
1426 ioctl
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
1427 if (is_blocking_read_ioctl( ioctl
->code
))
1429 LIST_FOR_EACH_ENTRY_SAFE( ioctl
, next
, &server
->queue
, struct console_host_ioctl
, entry
)
1431 if (!is_blocking_read_ioctl( ioctl
->code
)) continue;
1432 list_remove( &ioctl
->entry
);
1433 list_add_tail( &server
->read_queue
, &ioctl
->entry
);
1438 /* return the next ioctl */
1439 if (!list_empty( &server
->queue
))
1441 ioctl
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
1442 iosb
= ioctl
->async
? async_get_iosb( ioctl
->async
) : NULL
;
1444 if (!iosb
|| get_reply_max_size() >= iosb
->in_size
)
1446 reply
->code
= ioctl
->code
;
1447 reply
->output
= ioctl
->output
;
1451 reply
->out_size
= iosb
->out_size
;
1452 set_reply_data_ptr( iosb
->in_data
, iosb
->in_size
);
1453 iosb
->in_data
= NULL
;
1456 if (is_blocking_read_ioctl( ioctl
->code
))
1458 list_remove( &ioctl
->entry
);
1459 assert( list_empty( &server
->read_queue
));
1460 list_add_tail( &server
->read_queue
, &ioctl
->entry
);
1462 else server
->busy
= 1;
1466 reply
->out_size
= iosb
->in_size
;
1467 set_error( STATUS_BUFFER_OVERFLOW
);
1469 if (iosb
) release_object( iosb
);
1473 set_error( STATUS_PENDING
);
1476 release_object( server
);