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_flush( struct fd
*fd
, struct async
*async
);
102 static int console_input_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
104 static const struct fd_ops console_input_fd_ops
=
106 default_fd_get_poll_events
, /* get_poll_events */
107 default_poll_event
, /* poll_event */
108 console_get_fd_type
, /* get_fd_type */
109 no_fd_read
, /* read */
110 no_fd_write
, /* write */
111 console_input_flush
, /* flush */
112 no_fd_get_file_info
, /* get_file_info */
113 no_fd_get_volume_info
, /* get_volume_info */
114 console_input_ioctl
, /* ioctl */
115 default_fd_queue_async
, /* queue_async */
116 default_fd_reselect_async
/* reselect_async */
119 struct console_host_ioctl
121 unsigned int code
; /* ioctl code */
122 int output
; /* output id for screen buffer ioctls */
123 struct async
*async
; /* ioctl async */
124 struct list entry
; /* list entry */
127 struct console_server
129 struct object obj
; /* object header */
130 struct fd
*fd
; /* pseudo-fd for ioctls */
131 struct console_input
*console
; /* attached console */
132 struct list queue
; /* ioctl queue */
133 struct list read_queue
; /* blocking read queue */
134 int busy
; /* flag if server processing an ioctl */
135 int term_fd
; /* UNIX terminal fd */
136 struct termios termios
; /* original termios */
139 static void console_server_dump( struct object
*obj
, int verbose
);
140 static void console_server_destroy( struct object
*obj
);
141 static int console_server_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
142 static struct fd
*console_server_get_fd( struct object
*obj
);
143 static struct object
*console_server_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
144 static struct object
*console_server_open_file( struct object
*obj
, unsigned int access
,
145 unsigned int sharing
, unsigned int options
);
147 static const struct object_ops console_server_ops
=
149 sizeof(struct console_server
), /* size */
150 console_server_dump
, /* dump */
151 no_get_type
, /* get_type */
152 add_queue
, /* add_queue */
153 remove_queue
, /* remove_queue */
154 console_server_signaled
, /* signaled */
155 no_satisfied
, /* satisfied */
156 no_signal
, /* signal */
157 console_server_get_fd
, /* get_fd */
158 default_fd_map_access
, /* map_access */
159 default_get_sd
, /* get_sd */
160 default_set_sd
, /* set_sd */
161 no_get_full_name
, /* get_full_name */
162 console_server_lookup_name
, /* lookup_name */
163 no_link_name
, /* link_name */
164 NULL
, /* unlink_name */
165 console_server_open_file
, /* open_file */
166 no_kernel_obj_list
, /* get_kernel_obj_list */
167 fd_close_handle
, /* close_handle */
168 console_server_destroy
/* destroy */
171 static int console_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
173 static const struct fd_ops console_server_fd_ops
=
175 default_fd_get_poll_events
, /* get_poll_events */
176 default_poll_event
, /* poll_event */
177 console_get_fd_type
, /* get_fd_type */
178 no_fd_read
, /* read */
179 no_fd_write
, /* write */
180 no_fd_flush
, /* flush */
181 no_fd_get_file_info
, /* get_file_info */
182 no_fd_get_volume_info
, /* get_volume_info */
183 console_server_ioctl
, /* ioctl */
184 default_fd_queue_async
, /* queue_async */
185 default_fd_reselect_async
/* reselect_async */
193 short int pitch_family
;
195 data_size_t face_len
;
200 struct object obj
; /* object header */
201 struct list entry
; /* entry in list of all screen buffers */
202 struct console_input
*input
; /* associated console input */
203 unsigned int id
; /* buffer id */
204 struct fd
*fd
; /* for bare console, attached output fd */
205 struct async_queue ioctl_q
; /* ioctl queue */
208 static void screen_buffer_dump( struct object
*obj
, int verbose
);
209 static void screen_buffer_destroy( struct object
*obj
);
210 static int screen_buffer_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
211 static struct fd
*screen_buffer_get_fd( struct object
*obj
);
212 static struct object
*screen_buffer_open_file( struct object
*obj
, unsigned int access
,
213 unsigned int sharing
, unsigned int options
);
215 static const struct object_ops screen_buffer_ops
=
217 sizeof(struct screen_buffer
), /* size */
218 screen_buffer_dump
, /* dump */
219 no_get_type
, /* get_type */
220 screen_buffer_add_queue
, /* add_queue */
221 NULL
, /* remove_queue */
223 NULL
, /* satisfied */
224 no_signal
, /* signal */
225 screen_buffer_get_fd
, /* get_fd */
226 default_fd_map_access
, /* map_access */
227 default_get_sd
, /* get_sd */
228 default_set_sd
, /* set_sd */
229 no_get_full_name
, /* get_full_name */
230 no_lookup_name
, /* lookup_name */
231 no_link_name
, /* link_name */
232 NULL
, /* unlink_name */
233 screen_buffer_open_file
, /* open_file */
234 no_kernel_obj_list
, /* get_kernel_obj_list */
235 no_close_handle
, /* close_handle */
236 screen_buffer_destroy
/* destroy */
239 static int screen_buffer_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
241 static const struct fd_ops screen_buffer_fd_ops
=
243 default_fd_get_poll_events
, /* get_poll_events */
244 default_poll_event
, /* poll_event */
245 console_get_fd_type
, /* get_fd_type */
246 no_fd_read
, /* read */
247 no_fd_write
, /* write */
248 no_fd_flush
, /* flush */
249 no_fd_get_file_info
, /* get_file_info */
250 no_fd_get_volume_info
, /* get_volume_info */
251 screen_buffer_ioctl
, /* ioctl */
252 default_fd_queue_async
, /* queue_async */
253 default_fd_reselect_async
/* reselect_async */
256 static struct object_type
*console_device_get_type( struct object
*obj
);
257 static void console_device_dump( struct object
*obj
, int verbose
);
258 static struct object
*console_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
259 static struct object
*console_device_open_file( struct object
*obj
, unsigned int access
,
260 unsigned int sharing
, unsigned int options
);
262 static const struct object_ops console_device_ops
=
264 sizeof(struct object
), /* size */
265 console_device_dump
, /* dump */
266 console_device_get_type
, /* get_type */
267 no_add_queue
, /* add_queue */
268 NULL
, /* remove_queue */
270 no_satisfied
, /* satisfied */
271 no_signal
, /* signal */
272 no_get_fd
, /* get_fd */
273 default_fd_map_access
, /* map_access */
274 default_get_sd
, /* get_sd */
275 default_set_sd
, /* set_sd */
276 default_get_full_name
, /* get_full_name */
277 console_device_lookup_name
, /* lookup_name */
278 directory_link_name
, /* link_name */
279 default_unlink_name
, /* unlink_name */
280 console_device_open_file
, /* open_file */
281 no_kernel_obj_list
, /* get_kernel_obj_list */
282 no_close_handle
, /* close_handle */
283 no_destroy
/* destroy */
286 static void input_device_dump( struct object
*obj
, int verbose
);
287 static struct object
*input_device_open_file( struct object
*obj
, unsigned int access
,
288 unsigned int sharing
, unsigned int options
);
289 static int input_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
290 static struct fd
*input_device_get_fd( struct object
*obj
);
292 static const struct object_ops input_device_ops
=
294 sizeof(struct object
), /* size */
295 input_device_dump
, /* dump */
296 console_device_get_type
, /* get_type */
297 input_device_add_queue
, /* add_queue */
298 NULL
, /* remove_queue */
300 no_satisfied
, /* satisfied */
301 no_signal
, /* signal */
302 input_device_get_fd
, /* get_fd */
303 no_map_access
, /* map_access */
304 default_get_sd
, /* get_sd */
305 default_set_sd
, /* set_sd */
306 no_get_full_name
, /* get_full_name */
307 no_lookup_name
, /* lookup_name */
308 directory_link_name
, /* link_name */
309 default_unlink_name
, /* unlink_name */
310 input_device_open_file
, /* open_file */
311 no_kernel_obj_list
, /* get_kernel_obj_list */
312 no_close_handle
, /* close_handle */
313 no_destroy
/* destroy */
316 static void output_device_dump( struct object
*obj
, int verbose
);
317 static int output_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
);
318 static struct fd
*output_device_get_fd( struct object
*obj
);
319 static struct object
*output_device_open_file( struct object
*obj
, unsigned int access
,
320 unsigned int sharing
, unsigned int options
);
322 static const struct object_ops output_device_ops
=
324 sizeof(struct object
), /* size */
325 output_device_dump
, /* dump */
326 console_device_get_type
, /* get_type */
327 output_device_add_queue
, /* add_queue */
328 NULL
, /* remove_queue */
330 no_satisfied
, /* satisfied */
331 no_signal
, /* signal */
332 output_device_get_fd
, /* get_fd */
333 no_map_access
, /* map_access */
334 default_get_sd
, /* get_sd */
335 default_set_sd
, /* set_sd */
336 no_get_full_name
, /* get_full_name */
337 no_lookup_name
, /* lookup_name */
338 directory_link_name
, /* link_name */
339 default_unlink_name
, /* unlink_name */
340 output_device_open_file
, /* open_file */
341 no_kernel_obj_list
, /* get_kernel_obj_list */
342 no_close_handle
, /* close_handle */
343 no_destroy
/* destroy */
346 struct console_connection
348 struct object obj
; /* object header */
349 struct fd
*fd
; /* pseudo-fd for ioctls */
352 static void console_connection_dump( struct object
*obj
, int verbose
);
353 static struct fd
*console_connection_get_fd( struct object
*obj
);
354 static struct object
*console_connection_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
);
355 static struct object
*console_connection_open_file( struct object
*obj
, unsigned int access
,
356 unsigned int sharing
, unsigned int options
);
357 static int console_connection_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
358 static void console_connection_destroy( struct object
*obj
);
360 static const struct object_ops console_connection_ops
=
362 sizeof(struct console_connection
),/* size */
363 console_connection_dump
, /* dump */
364 console_device_get_type
, /* get_type */
365 no_add_queue
, /* add_queue */
366 NULL
, /* remove_queue */
368 no_satisfied
, /* satisfied */
369 no_signal
, /* signal */
370 console_connection_get_fd
, /* get_fd */
371 no_map_access
, /* map_access */
372 default_get_sd
, /* get_sd */
373 default_set_sd
, /* set_sd */
374 no_get_full_name
, /* get_full_name */
375 console_connection_lookup_name
, /* lookup_name */
376 directory_link_name
, /* link_name */
377 default_unlink_name
, /* unlink_name */
378 console_connection_open_file
, /* open_file */
379 no_kernel_obj_list
, /* get_kernel_obj_list */
380 console_connection_close_handle
, /* close_handle */
381 console_connection_destroy
/* destroy */
384 static int console_connection_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
386 static const struct fd_ops console_connection_fd_ops
=
388 default_fd_get_poll_events
, /* get_poll_events */
389 default_poll_event
, /* poll_event */
390 console_get_fd_type
, /* get_fd_type */
391 no_fd_read
, /* read */
392 no_fd_write
, /* write */
393 no_fd_flush
, /* flush */
394 no_fd_get_file_info
, /* get_file_info */
395 no_fd_get_volume_info
, /* get_volume_info */
396 console_connection_ioctl
, /* ioctl */
397 default_fd_queue_async
, /* queue_async */
398 default_fd_reselect_async
/* reselect_async */
401 static struct list screen_buffer_list
= LIST_INIT(screen_buffer_list
);
403 static int console_input_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
405 struct console_input
*console
= (struct console_input
*)obj
;
406 return console
->signaled
;
409 static struct fd
*console_input_get_fd( struct object
* obj
)
411 struct console_input
*console_input
= (struct console_input
*)obj
;
412 assert( obj
->ops
== &console_input_ops
);
413 return (struct fd
*)grab_object( console_input
->fd
);
416 static enum server_fd_type
console_get_fd_type( struct fd
*fd
)
421 static struct object
*create_console_input(void)
423 struct console_input
*console_input
;
425 if (!(console_input
= alloc_object( &console_input_ops
)))
428 console_input
->renderer
= NULL
;
429 console_input
->signaled
= 0;
430 console_input
->num_proc
= 0;
431 console_input
->active
= NULL
;
432 console_input
->server
= NULL
;
433 console_input
->fd
= NULL
;
434 console_input
->last_id
= 0;
435 init_async_queue( &console_input
->ioctl_q
);
436 init_async_queue( &console_input
->read_q
);
438 console_input
->fd
= alloc_pseudo_fd( &console_input_fd_ops
, &console_input
->obj
,
439 FILE_SYNCHRONOUS_IO_NONALERT
);
440 if (!console_input
->fd
)
442 release_object( console_input
);
445 allow_fd_caching( console_input
->fd
);
446 return &console_input
->obj
;
449 static void console_host_ioctl_terminate( struct console_host_ioctl
*call
, unsigned int status
)
453 async_terminate( call
->async
, status
);
454 release_object( call
->async
);
459 static int queue_host_ioctl( struct console_server
*server
, unsigned int code
, unsigned int output
,
460 struct async
*async
, struct async_queue
*queue
)
462 struct console_host_ioctl
*ioctl
;
464 if (!(ioctl
= mem_alloc( sizeof(*ioctl
) ))) return 0;
466 ioctl
->output
= output
;
470 ioctl
->async
= (struct async
*)grab_object( async
);
471 queue_async( queue
, async
);
473 list_add_tail( &server
->queue
, &ioctl
->entry
);
474 wake_up( &server
->obj
, 0 );
475 if (async
) set_error( STATUS_PENDING
);
479 static void disconnect_console_server( struct console_server
*server
)
481 while (!list_empty( &server
->queue
))
483 struct console_host_ioctl
*call
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
484 list_remove( &call
->entry
);
485 console_host_ioctl_terminate( call
, STATUS_CANCELLED
);
487 while (!list_empty( &server
->read_queue
))
489 struct console_host_ioctl
*call
= LIST_ENTRY( list_head( &server
->read_queue
), struct console_host_ioctl
, entry
);
490 list_remove( &call
->entry
);
491 console_host_ioctl_terminate( call
, STATUS_CANCELLED
);
494 if (server
->term_fd
!= -1)
496 tcsetattr( server
->term_fd
, TCSANOW
, &server
->termios
);
497 close( server
->term_fd
);
498 server
->term_fd
= -1;
503 assert( server
->console
->server
== server
);
504 server
->console
->server
= NULL
;
505 server
->console
= NULL
;
506 wake_up( &server
->obj
, 0 );
510 static void set_active_screen_buffer( struct console_input
*console_input
, struct screen_buffer
*screen_buffer
)
512 if (console_input
->active
== screen_buffer
) return;
513 if (console_input
->active
) release_object( console_input
->active
);
514 console_input
->active
= (struct screen_buffer
*)grab_object( screen_buffer
);
516 if (console_input
->server
) queue_host_ioctl( console_input
->server
, IOCTL_CONDRV_ACTIVATE
,
517 screen_buffer
->id
, NULL
, NULL
);
520 static struct object
*create_console_output( struct console_input
*console_input
)
522 struct screen_buffer
*screen_buffer
;
524 if (console_input
->last_id
== ~0)
526 set_error( STATUS_NO_MEMORY
);
530 if (!(screen_buffer
= alloc_object( &screen_buffer_ops
)))
533 screen_buffer
->id
= ++console_input
->last_id
;
534 screen_buffer
->input
= console_input
;
535 init_async_queue( &screen_buffer
->ioctl_q
);
536 list_add_head( &screen_buffer_list
, &screen_buffer
->entry
);
538 screen_buffer
->fd
= alloc_pseudo_fd( &screen_buffer_fd_ops
, &screen_buffer
->obj
,
539 FILE_SYNCHRONOUS_IO_NONALERT
);
540 if (!screen_buffer
->fd
)
542 release_object( screen_buffer
);
545 allow_fd_caching(screen_buffer
->fd
);
547 if (console_input
->server
) queue_host_ioctl( console_input
->server
, IOCTL_CONDRV_INIT_OUTPUT
,
548 screen_buffer
->id
, NULL
, NULL
);
549 if (!console_input
->active
) set_active_screen_buffer( console_input
, screen_buffer
);
550 return &screen_buffer
->obj
;
553 /* free the console for this process */
554 int free_console( struct process
*process
)
556 struct console_input
* console
= process
->console
;
558 if (!console
) return 0;
560 process
->console
= NULL
;
562 release_object( console
);
567 /* let process inherit the console from parent... this handle two cases :
568 * 1/ generic console inheritance
569 * 2/ parent is a renderer which launches process, and process should attach to the console
572 obj_handle_t
inherit_console( struct thread
*parent_thread
, obj_handle_t handle
, struct process
*process
,
573 obj_handle_t hconin
)
575 struct console_input
*console
= NULL
;
577 if (handle
) return duplicate_handle( current
->process
, handle
, process
, 0, 0, DUP_HANDLE_SAME_ACCESS
);
579 /* if parent is a renderer, then attach current process to its console
582 if (hconin
&& parent_thread
)
584 /* FIXME: should we check some access rights ? */
585 if (!(console
= (struct console_input
*)get_handle_obj( parent_thread
->process
, hconin
,
586 0, &console_input_ops
)))
587 clear_error(); /* ignore error */
589 if (!console
) return 0;
591 process
->console
= console
;
593 return alloc_handle( process
, process
->console
,
594 SYNCHRONIZE
| GENERIC_READ
| GENERIC_WRITE
, 0 );
597 struct thread
*console_get_renderer( struct console_input
*console
)
599 return console
->renderer
;
602 struct console_signal_info
604 struct console_input
*console
;
609 static int propagate_console_signal_cb(struct process
*process
, void *user
)
611 struct console_signal_info
* csi
= (struct console_signal_info
*)user
;
613 if (process
->console
== csi
->console
&& process
->running_threads
&&
614 (!csi
->group
|| process
->group_id
== csi
->group
))
616 /* find a suitable thread to signal */
617 struct thread
*thread
;
618 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
620 if (send_thread_signal( thread
, csi
->signal
)) break;
626 static void propagate_console_signal( struct console_input
*console
,
627 int sig
, process_id_t group_id
)
629 struct console_signal_info csi
;
633 set_error( STATUS_INVALID_PARAMETER
);
636 /* FIXME: should support the other events (like CTRL_BREAK) */
637 if (sig
!= CTRL_C_EVENT
)
639 set_error( STATUS_NOT_IMPLEMENTED
);
642 csi
.console
= console
;
644 csi
.group
= group_id
;
646 enum_processes(propagate_console_signal_cb
, &csi
);
650 static void console_input_dump( struct object
*obj
, int verbose
)
652 struct console_input
*console
= (struct console_input
*)obj
;
653 assert( obj
->ops
== &console_input_ops
);
654 fprintf( stderr
, "Console input active=%p server=%p\n",
655 console
->active
, console
->server
);
658 static void console_input_destroy( struct object
*obj
)
660 struct console_input
*console_in
= (struct console_input
*)obj
;
661 struct screen_buffer
*curr
;
663 assert( obj
->ops
== &console_input_ops
);
665 if (console_in
->server
)
667 assert( console_in
->server
->console
== console_in
);
668 disconnect_console_server( console_in
->server
);
671 if (console_in
->active
) release_object( console_in
->active
);
672 console_in
->active
= NULL
;
674 LIST_FOR_EACH_ENTRY( curr
, &screen_buffer_list
, struct screen_buffer
, entry
)
676 if (curr
->input
== console_in
) curr
->input
= NULL
;
679 free_async_queue( &console_in
->ioctl_q
);
680 free_async_queue( &console_in
->read_q
);
682 release_object( console_in
->fd
);
685 static struct object
*create_console_connection( struct console_input
*console
)
687 struct console_connection
*connection
;
689 if (current
->process
->console
)
691 set_error( STATUS_ACCESS_DENIED
);
695 if (!(connection
= alloc_object( &console_connection_ops
))) return NULL
;
696 if (!(connection
->fd
= alloc_pseudo_fd( &console_connection_fd_ops
, &connection
->obj
, 0 )))
698 release_object( connection
);
704 current
->process
->console
= (struct console_input
*)grab_object( console
);
708 return &connection
->obj
;
711 static struct object
*console_input_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
713 struct console_input
*console
= (struct console_input
*)obj
;
714 static const WCHAR connectionW
[] = {'C','o','n','n','e','c','t','i','o','n'};
715 assert( obj
->ops
== &console_input_ops
);
717 if (name
->len
== sizeof(connectionW
) && !memcmp( name
->str
, connectionW
, name
->len
))
720 return create_console_connection( console
);
726 static struct object
*console_input_open_file( struct object
*obj
, unsigned int access
,
727 unsigned int sharing
, unsigned int options
)
729 return grab_object( obj
);
732 static void screen_buffer_dump( struct object
*obj
, int verbose
)
734 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
735 assert( obj
->ops
== &screen_buffer_ops
);
737 fprintf(stderr
, "Console screen buffer input=%p\n", screen_buffer
->input
);
740 static void screen_buffer_destroy( struct object
*obj
)
742 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
744 assert( obj
->ops
== &screen_buffer_ops
);
746 list_remove( &screen_buffer
->entry
);
747 if (screen_buffer
->input
&& screen_buffer
->input
->server
)
748 queue_host_ioctl( screen_buffer
->input
->server
, IOCTL_CONDRV_CLOSE_OUTPUT
,
749 screen_buffer
->id
, NULL
, NULL
);
750 if (screen_buffer
->fd
) release_object( screen_buffer
->fd
);
751 free_async_queue( &screen_buffer
->ioctl_q
);
754 static struct object
*screen_buffer_open_file( struct object
*obj
, unsigned int access
,
755 unsigned int sharing
, unsigned int options
)
757 return grab_object( obj
);
760 static int screen_buffer_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
762 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
763 if (!screen_buffer
->input
)
765 set_error( STATUS_ACCESS_DENIED
);
768 return add_queue( &screen_buffer
->input
->obj
, entry
);
771 static struct fd
*screen_buffer_get_fd( struct object
*obj
)
773 struct screen_buffer
*screen_buffer
= (struct screen_buffer
*)obj
;
774 assert( obj
->ops
== &screen_buffer_ops
);
775 if (screen_buffer
->fd
)
776 return (struct fd
*)grab_object( screen_buffer
->fd
);
777 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
781 static void console_server_dump( struct object
*obj
, int verbose
)
783 assert( obj
->ops
== &console_server_ops
);
784 fprintf( stderr
, "Console server\n" );
787 static void console_server_destroy( struct object
*obj
)
789 struct console_server
*server
= (struct console_server
*)obj
;
790 assert( obj
->ops
== &console_server_ops
);
791 disconnect_console_server( server
);
792 if (server
->fd
) release_object( server
->fd
);
795 static struct object
*console_server_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
797 struct console_server
*server
= (struct console_server
*)obj
;
798 static const WCHAR referenceW
[] = {'R','e','f','e','r','e','n','c','e'};
799 assert( obj
->ops
== &console_server_ops
);
801 if (name
->len
== sizeof(referenceW
) && !memcmp( name
->str
, referenceW
, name
->len
))
803 struct screen_buffer
*screen_buffer
;
807 set_error( STATUS_INVALID_HANDLE
);
810 if (!(server
->console
= (struct console_input
*)create_console_input())) return NULL
;
811 if (!(screen_buffer
= (struct screen_buffer
*)create_console_output( server
->console
)))
813 release_object( server
->console
);
814 server
->console
= NULL
;
817 release_object( screen_buffer
);
818 server
->console
->server
= server
;
820 return &server
->console
->obj
;
826 static int console_server_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
828 struct console_server
*server
= (struct console_server
*)obj
;
829 assert( obj
->ops
== &console_server_ops
);
830 return !server
->console
|| !list_empty( &server
->queue
);
833 static struct fd
*console_server_get_fd( struct object
* obj
)
835 struct console_server
*server
= (struct console_server
*)obj
;
836 assert( obj
->ops
== &console_server_ops
);
837 return (struct fd
*)grab_object( server
->fd
);
840 static struct object
*console_server_open_file( struct object
*obj
, unsigned int access
,
841 unsigned int sharing
, unsigned int options
)
843 return grab_object( obj
);
846 static struct object
*create_console_server( void )
848 struct console_server
*server
;
850 if (!(server
= alloc_object( &console_server_ops
))) return NULL
;
851 server
->console
= NULL
;
853 server
->term_fd
= -1;
854 list_init( &server
->queue
);
855 list_init( &server
->read_queue
);
856 server
->fd
= alloc_pseudo_fd( &console_server_fd_ops
, &server
->obj
, FILE_SYNCHRONOUS_IO_NONALERT
);
859 release_object( server
);
862 allow_fd_caching(server
->fd
);
867 static int is_blocking_read_ioctl( unsigned int code
)
869 return code
== IOCTL_CONDRV_READ_INPUT
|| code
== IOCTL_CONDRV_READ_CONSOLE
;
872 static int console_input_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
874 struct console_input
*console
= get_fd_user( fd
);
878 case IOCTL_CONDRV_CTRL_EVENT
:
880 const struct condrv_ctrl_event
*event
= get_req_data();
882 if (get_req_data_size() != sizeof(*event
))
884 set_error( STATUS_INVALID_PARAMETER
);
887 group
= event
->group_id
? event
->group_id
: current
->process
->group_id
;
890 set_error( STATUS_INVALID_PARAMETER
);
893 propagate_console_signal( console
, event
->event
, group
);
898 if (!console
->server
|| code
>> 16 != FILE_DEVICE_CONSOLE
)
900 set_error( STATUS_INVALID_HANDLE
);
903 return queue_host_ioctl( console
->server
, code
, 0, async
, &console
->ioctl_q
);
907 static int console_input_flush( struct fd
*fd
, struct async
*async
)
909 struct console_input
*console
= get_fd_user( fd
);
911 if (!console
->server
)
913 set_error( STATUS_INVALID_HANDLE
);
916 return queue_host_ioctl( console
->server
, IOCTL_CONDRV_FLUSH
, 0, NULL
, NULL
);
919 static int screen_buffer_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
921 struct screen_buffer
*screen_buffer
= get_fd_user( fd
);
925 case IOCTL_CONDRV_ACTIVATE
:
926 if (!screen_buffer
->input
)
928 set_error( STATUS_INVALID_HANDLE
);
932 set_active_screen_buffer( screen_buffer
->input
, screen_buffer
);
936 if (!screen_buffer
->input
|| !screen_buffer
->input
->server
|| code
>> 16 != FILE_DEVICE_CONSOLE
||
937 is_blocking_read_ioctl( code
))
939 set_error( STATUS_INVALID_HANDLE
);
942 return queue_host_ioctl( screen_buffer
->input
->server
, code
, screen_buffer
->id
,
943 async
, &screen_buffer
->ioctl_q
);
947 static int console_connection_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
949 struct console_connection
*console_connection
= get_fd_user( fd
);
953 case IOCTL_CONDRV_BIND_PID
:
955 struct process
*process
;
957 if (get_req_data_size() != sizeof(unsigned int))
959 set_error( STATUS_INVALID_PARAMETER
);
962 if (current
->process
->console
)
964 set_error( STATUS_INVALID_HANDLE
);
968 pid
= *(unsigned int *)get_req_data();
969 if (pid
== ATTACH_PARENT_PROCESS
) pid
= current
->process
->parent_id
;
970 if (!(process
= get_process_from_id( pid
))) return 0;
972 if (process
->console
)
974 current
->process
->console
= (struct console_input
*)grab_object( process
->console
);
975 process
->console
->num_proc
++;
977 else set_error( STATUS_ACCESS_DENIED
);
978 release_object( process
);
983 return default_fd_ioctl( console_connection
->fd
, code
, async
);
987 static int console_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
989 struct console_server
*server
= get_fd_user( fd
);
993 case IOCTL_CONDRV_CTRL_EVENT
:
995 const struct condrv_ctrl_event
*event
= get_req_data();
996 if (get_req_data_size() != sizeof(*event
))
998 set_error( STATUS_INVALID_PARAMETER
);
1001 if (!server
->console
)
1003 set_error( STATUS_INVALID_HANDLE
);
1006 propagate_console_signal( server
->console
, event
->event
, event
->group_id
);
1007 return !get_error();
1010 case IOCTL_CONDRV_SETUP_INPUT
:
1012 struct termios term
;
1013 obj_handle_t handle
;
1017 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1019 set_error( STATUS_INVALID_PARAMETER
);
1022 if (server
->term_fd
!= -1)
1024 tcsetattr( server
->term_fd
, TCSANOW
, &server
->termios
);
1025 close( server
->term_fd
);
1026 server
->term_fd
= -1;
1028 handle
= *(unsigned int *)get_req_data();
1029 if (!handle
) return 1;
1030 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
)))
1034 unix_fd
= get_file_unix_fd( file
);
1035 release_object( file
);
1037 if (tcgetattr( unix_fd
, &server
->termios
))
1042 term
= server
->termios
;
1043 term
.c_lflag
&= ~(ECHO
| ECHONL
| ICANON
| IEXTEN
);
1044 term
.c_iflag
&= ~(BRKINT
| ICRNL
| INPCK
| ISTRIP
| IXON
);
1045 term
.c_cflag
&= ~(CSIZE
| PARENB
);
1046 term
.c_cflag
|= CS8
;
1047 term
.c_cc
[VMIN
] = 1;
1048 term
.c_cc
[VTIME
] = 0;
1049 if (tcsetattr( unix_fd
, TCSANOW
, &term
) || (server
->term_fd
= dup( unix_fd
)) == -1)
1058 set_error( STATUS_INVALID_HANDLE
);
1063 static void console_connection_dump( struct object
*obj
, int verbose
)
1065 fputs( "console connection\n", stderr
);
1068 static struct fd
*console_connection_get_fd( struct object
*obj
)
1070 struct console_connection
*connection
= (struct console_connection
*)obj
;
1071 return (struct fd
*)grab_object( connection
->fd
);
1074 static struct object
*console_connection_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1076 static const WCHAR referenceW
[] = {'R','e','f','e','r','e','n','c','e'};
1078 if (name
->len
== sizeof(referenceW
) && !memcmp( name
->str
, referenceW
, name
->len
))
1080 if (!current
->process
->console
)
1082 set_error( STATUS_INVALID_HANDLE
);
1086 return grab_object( current
->process
->console
);
1092 static struct object
*console_connection_open_file( struct object
*obj
, unsigned int access
,
1093 unsigned int sharing
, unsigned int options
)
1095 return grab_object( obj
);
1098 static int console_connection_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
1100 free_console( process
);
1104 static void console_connection_destroy( struct object
*obj
)
1106 struct console_connection
*connection
= (struct console_connection
*)obj
;
1107 if (connection
->fd
) release_object( connection
->fd
);
1110 static struct object_type
*console_device_get_type( struct object
*obj
)
1112 static const WCHAR name
[] = {'D','e','v','i','c','e'};
1113 static const struct unicode_str str
= { name
, sizeof(name
) };
1114 return get_object_type( &str
);
1117 static void console_device_dump( struct object
*obj
, int verbose
)
1119 fputs( "Console device\n", stderr
);
1122 static struct object
*console_device_lookup_name( struct object
*obj
, struct unicode_str
*name
, unsigned int attr
)
1124 static const WCHAR connectionW
[] = {'C','o','n','n','e','c','t','i','o','n'};
1125 static const WCHAR consoleW
[] = {'C','o','n','s','o','l','e'};
1126 static const WCHAR current_inW
[] = {'C','u','r','r','e','n','t','I','n'};
1127 static const WCHAR current_outW
[] = {'C','u','r','r','e','n','t','O','u','t'};
1128 static const WCHAR inputW
[] = {'I','n','p','u','t'};
1129 static const WCHAR outputW
[] = {'O','u','t','p','u','t'};
1130 static const WCHAR screen_bufferW
[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1131 static const WCHAR serverW
[] = {'S','e','r','v','e','r'};
1133 if (name
->len
== sizeof(current_inW
) && !memcmp( name
->str
, current_inW
, name
->len
))
1135 if (!current
->process
->console
)
1137 set_error( STATUS_INVALID_HANDLE
);
1141 return grab_object( current
->process
->console
);
1144 if (name
->len
== sizeof(current_outW
) && !memcmp( name
->str
, current_outW
, name
->len
))
1146 if (!current
->process
->console
|| !current
->process
->console
->active
)
1148 set_error( STATUS_INVALID_HANDLE
);
1152 return grab_object( current
->process
->console
->active
);
1155 if (name
->len
== sizeof(consoleW
) && !memcmp( name
->str
, consoleW
, name
->len
))
1158 return grab_object( obj
);
1161 if (name
->len
== sizeof(inputW
) && !memcmp( name
->str
, inputW
, name
->len
))
1164 return alloc_object( &input_device_ops
);
1167 if (name
->len
== sizeof(outputW
) && !memcmp( name
->str
, outputW
, name
->len
))
1170 return alloc_object( &output_device_ops
);
1173 if (name
->len
== sizeof(screen_bufferW
) && !memcmp( name
->str
, screen_bufferW
, name
->len
))
1175 if (!current
->process
->console
)
1177 set_error( STATUS_INVALID_HANDLE
);
1181 return create_console_output( current
->process
->console
);
1184 if (name
->len
== sizeof(serverW
) && !memcmp( name
->str
, serverW
, name
->len
))
1187 return create_console_server();
1190 if (name
->len
== sizeof(connectionW
) && !memcmp( name
->str
, connectionW
, name
->len
))
1193 return create_console_connection( NULL
);
1199 static struct object
*console_device_open_file( struct object
*obj
, unsigned int access
,
1200 unsigned int sharing
, unsigned int options
)
1203 access
= default_fd_map_access( obj
, access
);
1204 is_output
= access
& FILE_WRITE_DATA
;
1205 if (!current
->process
->console
|| (is_output
&& !current
->process
->console
))
1207 set_error( STATUS_INVALID_HANDLE
);
1210 if (is_output
&& (access
& FILE_READ_DATA
))
1212 set_error( STATUS_INVALID_PARAMETER
);
1215 return is_output
? grab_object( current
->process
->console
->active
) : grab_object( current
->process
->console
);
1218 static void input_device_dump( struct object
*obj
, int verbose
)
1220 fputs( "console Input device\n", stderr
);
1223 static int input_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1225 if (!current
->process
->console
)
1227 set_error( STATUS_ACCESS_DENIED
);
1230 return add_queue( ¤t
->process
->console
->obj
, entry
);
1233 static struct fd
*input_device_get_fd( struct object
*obj
)
1235 if (!current
->process
->console
)
1237 set_error( STATUS_ACCESS_DENIED
);
1240 return get_obj_fd( ¤t
->process
->console
->obj
);
1243 static struct object
*input_device_open_file( struct object
*obj
, unsigned int access
,
1244 unsigned int sharing
, unsigned int options
)
1246 return grab_object( obj
);
1249 static void output_device_dump( struct object
*obj
, int verbose
)
1251 fputs( "console Output device\n", stderr
);
1254 static int output_device_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1256 if (!current
->process
->console
|| !current
->process
->console
->active
)
1258 set_error( STATUS_ACCESS_DENIED
);
1261 return add_queue( ¤t
->process
->console
->obj
, entry
);
1264 static struct fd
*output_device_get_fd( struct object
*obj
)
1266 if (!current
->process
->console
|| !current
->process
->console
->active
)
1268 set_error( STATUS_ACCESS_DENIED
);
1272 return get_obj_fd( ¤t
->process
->console
->active
->obj
);
1275 static struct object
*output_device_open_file( struct object
*obj
, unsigned int access
,
1276 unsigned int sharing
, unsigned int options
)
1278 return grab_object( obj
);
1281 struct object
*create_console_device( struct object
*root
, const struct unicode_str
*name
,
1282 unsigned int attr
, const struct security_descriptor
*sd
)
1284 return create_named_object( root
, &console_device_ops
, name
, attr
, sd
);
1287 /* retrieve the next pending console ioctl request */
1288 DECL_HANDLER(get_next_console_request
)
1290 struct console_host_ioctl
*ioctl
= NULL
, *next
;
1291 struct console_server
*server
;
1292 struct iosb
*iosb
= NULL
;
1294 server
= (struct console_server
*)get_handle_obj( current
->process
, req
->handle
, 0, &console_server_ops
);
1295 if (!server
) return;
1297 if (!server
->console
)
1299 set_error( STATUS_INVALID_HANDLE
);
1300 release_object( server
);
1304 if (!req
->signal
) server
->console
->signaled
= 0;
1305 else if (!server
->console
->signaled
)
1307 server
->console
->signaled
= 1;
1308 wake_up( &server
->console
->obj
, 0 );
1313 /* set result of current pending ioctl */
1314 if (list_empty( &server
->read_queue
))
1316 set_error( STATUS_INVALID_HANDLE
);
1317 release_object( server
);
1321 ioctl
= LIST_ENTRY( list_head( &server
->read_queue
), struct console_host_ioctl
, entry
);
1322 list_remove( &ioctl
->entry
);
1323 list_move_tail( &server
->queue
, &server
->read_queue
);
1325 else if (server
->busy
)
1327 /* set result of previous ioctl */
1328 ioctl
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
1329 list_remove( &ioctl
->entry
);
1334 unsigned int status
= req
->status
;
1335 if (status
== STATUS_PENDING
) status
= STATUS_INVALID_PARAMETER
;
1338 iosb
= async_get_iosb( ioctl
->async
);
1339 iosb
->status
= status
;
1340 iosb
->out_size
= min( iosb
->out_size
, get_req_data_size() );
1343 if ((iosb
->out_data
= memdup( get_req_data(), iosb
->out_size
)))
1345 iosb
->result
= iosb
->out_size
;
1346 status
= STATUS_ALERTED
;
1350 iosb
->status
= STATUS_NO_MEMORY
;
1355 console_host_ioctl_terminate( ioctl
, status
);
1356 if (iosb
) release_object( iosb
);
1360 release_object( server
);
1366 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1367 * move it to read queue for execution after current read is complete. move all blocking
1368 * ioctl at the same time to preserve their order. */
1369 if (!list_empty( &server
->queue
) && !list_empty( &server
->read_queue
))
1371 ioctl
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
1372 if (is_blocking_read_ioctl( ioctl
->code
))
1374 LIST_FOR_EACH_ENTRY_SAFE( ioctl
, next
, &server
->queue
, struct console_host_ioctl
, entry
)
1376 if (!is_blocking_read_ioctl( ioctl
->code
)) continue;
1377 list_remove( &ioctl
->entry
);
1378 list_add_tail( &server
->read_queue
, &ioctl
->entry
);
1383 /* return the next ioctl */
1384 if (!list_empty( &server
->queue
))
1386 ioctl
= LIST_ENTRY( list_head( &server
->queue
), struct console_host_ioctl
, entry
);
1387 iosb
= ioctl
->async
? async_get_iosb( ioctl
->async
) : NULL
;
1389 if (!iosb
|| get_reply_max_size() >= iosb
->in_size
)
1391 reply
->code
= ioctl
->code
;
1392 reply
->output
= ioctl
->output
;
1396 reply
->out_size
= iosb
->out_size
;
1397 set_reply_data_ptr( iosb
->in_data
, iosb
->in_size
);
1398 iosb
->in_data
= NULL
;
1401 if (is_blocking_read_ioctl( ioctl
->code
))
1403 list_remove( &ioctl
->entry
);
1404 assert( list_empty( &server
->read_queue
));
1405 list_add_tail( &server
->read_queue
, &ioctl
->entry
);
1407 else server
->busy
= 1;
1411 reply
->out_size
= iosb
->in_size
;
1412 set_error( STATUS_BUFFER_OVERFLOW
);
1414 if (iosb
) release_object( iosb
);
1418 set_error( STATUS_PENDING
);
1421 release_object( server
);