server: Properly handle NtQueryInformationFile on console handles.
[wine.git] / server / console.c
blobcecd1455d605192add564d14b5936f63052bddc3
1 /*
2 * Server-side console management
4 * Copyright (C) 1998 Alexandre Julliard
5 * 2001 Eric Pouech
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <signal.h>
32 #include <sys/ioctl.h>
33 #include <termios.h>
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "handle.h"
38 #include "process.h"
39 #include "request.h"
40 #include "file.h"
41 #include "unicode.h"
42 #include "wincon.h"
43 #include "winternl.h"
44 #include "wine/condrv.h"
46 struct screen_buffer;
48 struct history_line
50 data_size_t len;
51 WCHAR text[1];
54 struct console_input
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 void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
102 static int console_input_read( struct fd *fd, struct async *async, file_pos_t pos );
103 static int console_input_flush( struct fd *fd, struct async *async );
104 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
106 static const struct fd_ops console_input_fd_ops =
108 default_fd_get_poll_events, /* get_poll_events */
109 default_poll_event, /* poll_event */
110 console_get_fd_type, /* get_fd_type */
111 console_input_read, /* read */
112 no_fd_write, /* write */
113 console_input_flush, /* flush */
114 console_get_file_info, /* get_file_info */
115 no_fd_get_volume_info, /* get_volume_info */
116 console_input_ioctl, /* ioctl */
117 default_fd_queue_async, /* queue_async */
118 default_fd_reselect_async /* reselect_async */
121 struct console_host_ioctl
123 unsigned int code; /* ioctl code */
124 int output; /* output id for screen buffer ioctls */
125 struct async *async; /* ioctl async */
126 struct list entry; /* list entry */
129 struct console_server
131 struct object obj; /* object header */
132 struct fd *fd; /* pseudo-fd for ioctls */
133 struct console_input *console; /* attached console */
134 struct list queue; /* ioctl queue */
135 struct list read_queue; /* blocking read queue */
136 int busy; /* flag if server processing an ioctl */
137 int term_fd; /* UNIX terminal fd */
138 struct termios termios; /* original termios */
141 static void console_server_dump( struct object *obj, int verbose );
142 static void console_server_destroy( struct object *obj );
143 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
144 static struct fd *console_server_get_fd( struct object *obj );
145 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
146 static struct object *console_server_open_file( struct object *obj, unsigned int access,
147 unsigned int sharing, unsigned int options );
149 static const struct object_ops console_server_ops =
151 sizeof(struct console_server), /* size */
152 console_server_dump, /* dump */
153 no_get_type, /* get_type */
154 add_queue, /* add_queue */
155 remove_queue, /* remove_queue */
156 console_server_signaled, /* signaled */
157 no_satisfied, /* satisfied */
158 no_signal, /* signal */
159 console_server_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_get_full_name, /* get_full_name */
164 console_server_lookup_name, /* lookup_name */
165 no_link_name, /* link_name */
166 NULL, /* unlink_name */
167 console_server_open_file, /* open_file */
168 no_kernel_obj_list, /* get_kernel_obj_list */
169 fd_close_handle, /* close_handle */
170 console_server_destroy /* destroy */
173 static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
175 static const struct fd_ops console_server_fd_ops =
177 default_fd_get_poll_events, /* get_poll_events */
178 default_poll_event, /* poll_event */
179 console_get_fd_type, /* get_fd_type */
180 no_fd_read, /* read */
181 no_fd_write, /* write */
182 no_fd_flush, /* flush */
183 no_fd_get_file_info, /* get_file_info */
184 no_fd_get_volume_info, /* get_volume_info */
185 console_server_ioctl, /* ioctl */
186 default_fd_queue_async, /* queue_async */
187 default_fd_reselect_async /* reselect_async */
190 struct font_info
192 short int width;
193 short int height;
194 short int weight;
195 short int pitch_family;
196 WCHAR *face_name;
197 data_size_t face_len;
200 struct screen_buffer
202 struct object obj; /* object header */
203 struct list entry; /* entry in list of all screen buffers */
204 struct console_input *input; /* associated console input */
205 unsigned int id; /* buffer id */
206 struct fd *fd; /* for bare console, attached output fd */
207 struct async_queue ioctl_q; /* ioctl queue */
210 static void screen_buffer_dump( struct object *obj, int verbose );
211 static void screen_buffer_destroy( struct object *obj );
212 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
213 static struct fd *screen_buffer_get_fd( struct object *obj );
214 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
215 unsigned int sharing, unsigned int options );
217 static const struct object_ops screen_buffer_ops =
219 sizeof(struct screen_buffer), /* size */
220 screen_buffer_dump, /* dump */
221 no_get_type, /* get_type */
222 screen_buffer_add_queue, /* add_queue */
223 NULL, /* remove_queue */
224 NULL, /* signaled */
225 NULL, /* satisfied */
226 no_signal, /* signal */
227 screen_buffer_get_fd, /* get_fd */
228 default_fd_map_access, /* map_access */
229 default_get_sd, /* get_sd */
230 default_set_sd, /* set_sd */
231 no_get_full_name, /* get_full_name */
232 no_lookup_name, /* lookup_name */
233 no_link_name, /* link_name */
234 NULL, /* unlink_name */
235 screen_buffer_open_file, /* open_file */
236 no_kernel_obj_list, /* get_kernel_obj_list */
237 no_close_handle, /* close_handle */
238 screen_buffer_destroy /* destroy */
241 static int screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos );
242 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
244 static const struct fd_ops screen_buffer_fd_ops =
246 default_fd_get_poll_events, /* get_poll_events */
247 default_poll_event, /* poll_event */
248 console_get_fd_type, /* get_fd_type */
249 no_fd_read, /* read */
250 screen_buffer_write, /* write */
251 no_fd_flush, /* flush */
252 console_get_file_info, /* get_file_info */
253 no_fd_get_volume_info, /* get_volume_info */
254 screen_buffer_ioctl, /* ioctl */
255 default_fd_queue_async, /* queue_async */
256 default_fd_reselect_async /* reselect_async */
259 static struct object_type *console_device_get_type( struct object *obj );
260 static void console_device_dump( struct object *obj, int verbose );
261 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
262 static struct object *console_device_open_file( struct object *obj, unsigned int access,
263 unsigned int sharing, unsigned int options );
265 static const struct object_ops console_device_ops =
267 sizeof(struct object), /* size */
268 console_device_dump, /* dump */
269 console_device_get_type, /* get_type */
270 no_add_queue, /* add_queue */
271 NULL, /* remove_queue */
272 NULL, /* signaled */
273 no_satisfied, /* satisfied */
274 no_signal, /* signal */
275 no_get_fd, /* get_fd */
276 default_fd_map_access, /* map_access */
277 default_get_sd, /* get_sd */
278 default_set_sd, /* set_sd */
279 default_get_full_name, /* get_full_name */
280 console_device_lookup_name, /* lookup_name */
281 directory_link_name, /* link_name */
282 default_unlink_name, /* unlink_name */
283 console_device_open_file, /* open_file */
284 no_kernel_obj_list, /* get_kernel_obj_list */
285 no_close_handle, /* close_handle */
286 no_destroy /* destroy */
289 static void input_device_dump( struct object *obj, int verbose );
290 static struct object *input_device_open_file( struct object *obj, unsigned int access,
291 unsigned int sharing, unsigned int options );
292 static int input_device_add_queue( struct object *obj, struct wait_queue_entry *entry );
293 static struct fd *input_device_get_fd( struct object *obj );
295 static const struct object_ops input_device_ops =
297 sizeof(struct object), /* size */
298 input_device_dump, /* dump */
299 console_device_get_type, /* get_type */
300 input_device_add_queue, /* add_queue */
301 NULL, /* remove_queue */
302 NULL, /* signaled */
303 no_satisfied, /* satisfied */
304 no_signal, /* signal */
305 input_device_get_fd, /* get_fd */
306 no_map_access, /* map_access */
307 default_get_sd, /* get_sd */
308 default_set_sd, /* set_sd */
309 no_get_full_name, /* get_full_name */
310 no_lookup_name, /* lookup_name */
311 directory_link_name, /* link_name */
312 default_unlink_name, /* unlink_name */
313 input_device_open_file, /* open_file */
314 no_kernel_obj_list, /* get_kernel_obj_list */
315 no_close_handle, /* close_handle */
316 no_destroy /* destroy */
319 static void output_device_dump( struct object *obj, int verbose );
320 static int output_device_add_queue( struct object *obj, struct wait_queue_entry *entry );
321 static struct fd *output_device_get_fd( struct object *obj );
322 static struct object *output_device_open_file( struct object *obj, unsigned int access,
323 unsigned int sharing, unsigned int options );
325 static const struct object_ops output_device_ops =
327 sizeof(struct object), /* size */
328 output_device_dump, /* dump */
329 console_device_get_type, /* get_type */
330 output_device_add_queue, /* add_queue */
331 NULL, /* remove_queue */
332 NULL, /* signaled */
333 no_satisfied, /* satisfied */
334 no_signal, /* signal */
335 output_device_get_fd, /* get_fd */
336 no_map_access, /* map_access */
337 default_get_sd, /* get_sd */
338 default_set_sd, /* set_sd */
339 no_get_full_name, /* get_full_name */
340 no_lookup_name, /* lookup_name */
341 directory_link_name, /* link_name */
342 default_unlink_name, /* unlink_name */
343 output_device_open_file, /* open_file */
344 no_kernel_obj_list, /* get_kernel_obj_list */
345 no_close_handle, /* close_handle */
346 no_destroy /* destroy */
349 struct console_connection
351 struct object obj; /* object header */
352 struct fd *fd; /* pseudo-fd for ioctls */
355 static void console_connection_dump( struct object *obj, int verbose );
356 static struct fd *console_connection_get_fd( struct object *obj );
357 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
358 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
359 unsigned int sharing, unsigned int options );
360 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
361 static void console_connection_destroy( struct object *obj );
363 static const struct object_ops console_connection_ops =
365 sizeof(struct console_connection),/* size */
366 console_connection_dump, /* dump */
367 console_device_get_type, /* get_type */
368 no_add_queue, /* add_queue */
369 NULL, /* remove_queue */
370 NULL, /* signaled */
371 no_satisfied, /* satisfied */
372 no_signal, /* signal */
373 console_connection_get_fd, /* get_fd */
374 no_map_access, /* map_access */
375 default_get_sd, /* get_sd */
376 default_set_sd, /* set_sd */
377 no_get_full_name, /* get_full_name */
378 console_connection_lookup_name, /* lookup_name */
379 directory_link_name, /* link_name */
380 default_unlink_name, /* unlink_name */
381 console_connection_open_file, /* open_file */
382 no_kernel_obj_list, /* get_kernel_obj_list */
383 console_connection_close_handle, /* close_handle */
384 console_connection_destroy /* destroy */
387 static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
389 static const struct fd_ops console_connection_fd_ops =
391 default_fd_get_poll_events, /* get_poll_events */
392 default_poll_event, /* poll_event */
393 console_get_fd_type, /* get_fd_type */
394 no_fd_read, /* read */
395 no_fd_write, /* write */
396 no_fd_flush, /* flush */
397 no_fd_get_file_info, /* get_file_info */
398 no_fd_get_volume_info, /* get_volume_info */
399 console_connection_ioctl, /* ioctl */
400 default_fd_queue_async, /* queue_async */
401 default_fd_reselect_async /* reselect_async */
404 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
406 static int console_input_signaled( struct object *obj, struct wait_queue_entry *entry )
408 struct console_input *console = (struct console_input*)obj;
409 return console->signaled;
412 static struct fd *console_input_get_fd( struct object* obj )
414 struct console_input *console_input = (struct console_input*)obj;
415 assert( obj->ops == &console_input_ops );
416 return (struct fd *)grab_object( console_input->fd );
419 static enum server_fd_type console_get_fd_type( struct fd *fd )
421 return FD_TYPE_CHAR;
424 static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
426 set_error( STATUS_INVALID_DEVICE_REQUEST );
429 static struct object *create_console_input(void)
431 struct console_input *console_input;
433 if (!(console_input = alloc_object( &console_input_ops )))
434 return NULL;
436 console_input->renderer = NULL;
437 console_input->signaled = 0;
438 console_input->num_proc = 0;
439 console_input->active = NULL;
440 console_input->server = NULL;
441 console_input->fd = NULL;
442 console_input->last_id = 0;
443 init_async_queue( &console_input->ioctl_q );
444 init_async_queue( &console_input->read_q );
446 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
447 FILE_SYNCHRONOUS_IO_NONALERT );
448 if (!console_input->fd)
450 release_object( console_input );
451 return NULL;
453 allow_fd_caching( console_input->fd );
454 return &console_input->obj;
457 static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
459 if (call->async)
461 async_terminate( call->async, status );
462 release_object( call->async );
464 free( call );
467 static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
468 struct async *async, struct async_queue *queue )
470 struct console_host_ioctl *ioctl;
472 if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
473 ioctl->code = code;
474 ioctl->output = output;
475 ioctl->async = NULL;
476 if (async)
478 ioctl->async = (struct async *)grab_object( async );
479 queue_async( queue, async );
481 list_add_tail( &server->queue, &ioctl->entry );
482 wake_up( &server->obj, 0 );
483 if (async) set_error( STATUS_PENDING );
484 return 1;
487 static void disconnect_console_server( struct console_server *server )
489 while (!list_empty( &server->queue ))
491 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
492 list_remove( &call->entry );
493 console_host_ioctl_terminate( call, STATUS_CANCELLED );
495 while (!list_empty( &server->read_queue ))
497 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
498 list_remove( &call->entry );
499 console_host_ioctl_terminate( call, STATUS_CANCELLED );
502 if (server->term_fd != -1)
504 tcsetattr( server->term_fd, TCSANOW, &server->termios );
505 close( server->term_fd );
506 server->term_fd = -1;
509 if (server->console)
511 assert( server->console->server == server );
512 server->console->server = NULL;
513 server->console = NULL;
514 wake_up( &server->obj, 0 );
518 static void set_active_screen_buffer( struct console_input *console_input, struct screen_buffer *screen_buffer )
520 if (console_input->active == screen_buffer) return;
521 if (console_input->active) release_object( console_input->active );
522 console_input->active = (struct screen_buffer *)grab_object( screen_buffer );
524 if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_ACTIVATE,
525 screen_buffer->id, NULL, NULL );
528 static struct object *create_console_output( struct console_input *console_input )
530 struct screen_buffer *screen_buffer;
532 if (console_input->last_id == ~0)
534 set_error( STATUS_NO_MEMORY );
535 return NULL;
538 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
539 return NULL;
541 screen_buffer->id = ++console_input->last_id;
542 screen_buffer->input = console_input;
543 init_async_queue( &screen_buffer->ioctl_q );
544 list_add_head( &screen_buffer_list, &screen_buffer->entry );
546 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
547 FILE_SYNCHRONOUS_IO_NONALERT );
548 if (!screen_buffer->fd)
550 release_object( screen_buffer );
551 return NULL;
553 allow_fd_caching(screen_buffer->fd);
555 if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_INIT_OUTPUT,
556 screen_buffer->id, NULL, NULL );
557 if (!console_input->active) set_active_screen_buffer( console_input, screen_buffer );
558 return &screen_buffer->obj;
561 /* free the console for this process */
562 int free_console( struct process *process )
564 struct console_input* console = process->console;
566 if (!console) return 0;
568 process->console = NULL;
569 console->num_proc--;
570 release_object( console );
572 return 1;
575 /* let process inherit the console from parent... this handle two cases :
576 * 1/ generic console inheritance
577 * 2/ parent is a renderer which launches process, and process should attach to the console
578 * rendered by parent
580 obj_handle_t inherit_console( struct thread *parent_thread, obj_handle_t handle, struct process *process,
581 obj_handle_t hconin )
583 struct console_input *console = NULL;
585 if (handle) return duplicate_handle( current->process, handle, process, 0, 0, DUP_HANDLE_SAME_ACCESS );
587 /* if parent is a renderer, then attach current process to its console
588 * a bit hacky....
590 if (hconin && parent_thread)
592 /* FIXME: should we check some access rights ? */
593 if (!(console = (struct console_input *)get_handle_obj( parent_thread->process, hconin,
594 0, &console_input_ops )))
595 clear_error(); /* ignore error */
597 if (!console) return 0;
599 process->console = console;
600 console->num_proc++;
601 return alloc_handle( process, process->console,
602 SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE, 0 );
605 struct thread *console_get_renderer( struct console_input *console )
607 return console->renderer;
610 struct console_signal_info
612 struct console_input *console;
613 process_id_t group;
614 int signal;
617 static int propagate_console_signal_cb(struct process *process, void *user)
619 struct console_signal_info* csi = (struct console_signal_info*)user;
621 if (process->console == csi->console && process->running_threads &&
622 (!csi->group || process->group_id == csi->group))
624 /* find a suitable thread to signal */
625 struct thread *thread;
626 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
628 if (send_thread_signal( thread, csi->signal )) break;
631 return FALSE;
634 static void propagate_console_signal( struct console_input *console,
635 int sig, process_id_t group_id )
637 struct console_signal_info csi;
639 if (!console)
641 set_error( STATUS_INVALID_PARAMETER );
642 return;
644 /* FIXME: should support the other events (like CTRL_BREAK) */
645 if (sig != CTRL_C_EVENT)
647 set_error( STATUS_NOT_IMPLEMENTED );
648 return;
650 csi.console = console;
651 csi.signal = SIGINT;
652 csi.group = group_id;
654 enum_processes(propagate_console_signal_cb, &csi);
657 /* dumb dump */
658 static void console_input_dump( struct object *obj, int verbose )
660 struct console_input *console = (struct console_input *)obj;
661 assert( obj->ops == &console_input_ops );
662 fprintf( stderr, "Console input active=%p server=%p\n",
663 console->active, console->server );
666 static void console_input_destroy( struct object *obj )
668 struct console_input *console_in = (struct console_input *)obj;
669 struct screen_buffer *curr;
671 assert( obj->ops == &console_input_ops );
673 if (console_in->server)
675 assert( console_in->server->console == console_in );
676 disconnect_console_server( console_in->server );
679 if (console_in->active) release_object( console_in->active );
680 console_in->active = NULL;
682 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
684 if (curr->input == console_in) curr->input = NULL;
687 free_async_queue( &console_in->ioctl_q );
688 free_async_queue( &console_in->read_q );
689 if (console_in->fd)
690 release_object( console_in->fd );
693 static struct object *create_console_connection( struct console_input *console )
695 struct console_connection *connection;
697 if (current->process->console)
699 set_error( STATUS_ACCESS_DENIED );
700 return NULL;
703 if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
704 if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
706 release_object( connection );
707 return NULL;
710 if (console)
712 current->process->console = (struct console_input *)grab_object( console );
713 console->num_proc++;
716 return &connection->obj;
719 static struct object *console_input_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
721 struct console_input *console = (struct console_input *)obj;
722 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
723 assert( obj->ops == &console_input_ops );
725 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
727 name->len = 0;
728 return create_console_connection( console );
731 return NULL;
734 static struct object *console_input_open_file( struct object *obj, unsigned int access,
735 unsigned int sharing, unsigned int options )
737 return grab_object( obj );
740 static void screen_buffer_dump( struct object *obj, int verbose )
742 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
743 assert( obj->ops == &screen_buffer_ops );
745 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
748 static void screen_buffer_destroy( struct object *obj )
750 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
752 assert( obj->ops == &screen_buffer_ops );
754 list_remove( &screen_buffer->entry );
755 if (screen_buffer->input && screen_buffer->input->server)
756 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
757 screen_buffer->id, NULL, NULL );
758 if (screen_buffer->fd) release_object( screen_buffer->fd );
759 free_async_queue( &screen_buffer->ioctl_q );
762 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
763 unsigned int sharing, unsigned int options )
765 return grab_object( obj );
768 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
770 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
771 if (!screen_buffer->input)
773 set_error( STATUS_ACCESS_DENIED );
774 return 0;
776 return add_queue( &screen_buffer->input->obj, entry );
779 static struct fd *screen_buffer_get_fd( struct object *obj )
781 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
782 assert( obj->ops == &screen_buffer_ops );
783 if (screen_buffer->fd)
784 return (struct fd*)grab_object( screen_buffer->fd );
785 set_error( STATUS_OBJECT_TYPE_MISMATCH );
786 return NULL;
789 static void console_server_dump( struct object *obj, int verbose )
791 assert( obj->ops == &console_server_ops );
792 fprintf( stderr, "Console server\n" );
795 static void console_server_destroy( struct object *obj )
797 struct console_server *server = (struct console_server *)obj;
798 assert( obj->ops == &console_server_ops );
799 disconnect_console_server( server );
800 if (server->fd) release_object( server->fd );
803 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
805 struct console_server *server = (struct console_server*)obj;
806 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
807 assert( obj->ops == &console_server_ops );
809 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
811 struct screen_buffer *screen_buffer;
812 name->len = 0;
813 if (server->console)
815 set_error( STATUS_INVALID_HANDLE );
816 return 0;
818 if (!(server->console = (struct console_input *)create_console_input())) return NULL;
819 if (!(screen_buffer = (struct screen_buffer *)create_console_output( server->console )))
821 release_object( server->console );
822 server->console = NULL;
823 return NULL;
825 release_object( screen_buffer );
826 server->console->server = server;
828 return &server->console->obj;
831 return NULL;
834 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
836 struct console_server *server = (struct console_server*)obj;
837 assert( obj->ops == &console_server_ops );
838 return !server->console || !list_empty( &server->queue );
841 static struct fd *console_server_get_fd( struct object* obj )
843 struct console_server *server = (struct console_server*)obj;
844 assert( obj->ops == &console_server_ops );
845 return (struct fd *)grab_object( server->fd );
848 static struct object *console_server_open_file( struct object *obj, unsigned int access,
849 unsigned int sharing, unsigned int options )
851 return grab_object( obj );
854 static struct object *create_console_server( void )
856 struct console_server *server;
858 if (!(server = alloc_object( &console_server_ops ))) return NULL;
859 server->console = NULL;
860 server->busy = 0;
861 server->term_fd = -1;
862 list_init( &server->queue );
863 list_init( &server->read_queue );
864 server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
865 if (!server->fd)
867 release_object( server );
868 return NULL;
870 allow_fd_caching(server->fd);
872 return &server->obj;
875 static int is_blocking_read_ioctl( unsigned int code )
877 switch (code)
879 case IOCTL_CONDRV_READ_INPUT:
880 case IOCTL_CONDRV_READ_CONSOLE:
881 case IOCTL_CONDRV_READ_FILE:
882 return 1;
883 default:
884 return 0;
888 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
890 struct console_input *console = get_fd_user( fd );
892 switch (code)
894 case IOCTL_CONDRV_CTRL_EVENT:
896 const struct condrv_ctrl_event *event = get_req_data();
897 process_id_t group;
898 if (get_req_data_size() != sizeof(*event))
900 set_error( STATUS_INVALID_PARAMETER );
901 return 0;
903 group = event->group_id ? event->group_id : current->process->group_id;
904 if (!group)
906 set_error( STATUS_INVALID_PARAMETER );
907 return 0;
909 propagate_console_signal( console, event->event, group );
910 return !get_error();
913 default:
914 if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
916 set_error( STATUS_INVALID_HANDLE );
917 return 0;
919 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
923 static int console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
925 struct console_input *console = get_fd_user( fd );
927 if (!console->server)
929 set_error( STATUS_INVALID_HANDLE );
930 return 0;
932 return queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
935 static int console_input_flush( struct fd *fd, struct async *async )
937 struct console_input *console = get_fd_user( fd );
939 if (!console->server)
941 set_error( STATUS_INVALID_HANDLE );
942 return 0;
944 return queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
947 static int screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
949 struct screen_buffer *screen_buffer = get_fd_user( fd );
950 struct iosb *iosb;
952 if (!screen_buffer->input || !screen_buffer->input->server)
954 set_error( STATUS_INVALID_HANDLE );
955 return 0;
958 if (!queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
959 screen_buffer->id, async, &screen_buffer->ioctl_q ))
960 return 0;
962 /* we can't use default async handling, because write result is not
963 * compatible with ioctl result */
964 iosb = async_get_iosb( async );
965 iosb->status = STATUS_SUCCESS;
966 iosb->result = iosb->in_size;
967 async_terminate( async, iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
968 release_object( iosb );
969 return 1;
972 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
974 struct screen_buffer *screen_buffer = get_fd_user( fd );
976 switch (code)
978 case IOCTL_CONDRV_ACTIVATE:
979 if (!screen_buffer->input)
981 set_error( STATUS_INVALID_HANDLE );
982 return 0;
985 set_active_screen_buffer( screen_buffer->input, screen_buffer );
986 return 1;
988 default:
989 if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
990 is_blocking_read_ioctl( code ))
992 set_error( STATUS_INVALID_HANDLE );
993 return 0;
995 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
996 async, &screen_buffer->ioctl_q );
1000 static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1002 struct console_connection *console_connection = get_fd_user( fd );
1004 switch (code)
1006 case IOCTL_CONDRV_BIND_PID:
1008 struct process *process;
1009 unsigned int pid;
1010 if (get_req_data_size() != sizeof(unsigned int))
1012 set_error( STATUS_INVALID_PARAMETER );
1013 return 0;
1015 if (current->process->console)
1017 set_error( STATUS_INVALID_HANDLE );
1018 return 0;
1021 pid = *(unsigned int *)get_req_data();
1022 if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
1023 if (!(process = get_process_from_id( pid ))) return 0;
1025 if (process->console)
1027 current->process->console = (struct console_input *)grab_object( process->console );
1028 process->console->num_proc++;
1030 else set_error( STATUS_ACCESS_DENIED );
1031 release_object( process );
1032 return !get_error();
1035 default:
1036 return default_fd_ioctl( console_connection->fd, code, async );
1040 static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1042 struct console_server *server = get_fd_user( fd );
1044 switch (code)
1046 case IOCTL_CONDRV_CTRL_EVENT:
1048 const struct condrv_ctrl_event *event = get_req_data();
1049 if (get_req_data_size() != sizeof(*event))
1051 set_error( STATUS_INVALID_PARAMETER );
1052 return 0;
1054 if (!server->console)
1056 set_error( STATUS_INVALID_HANDLE );
1057 return 0;
1059 propagate_console_signal( server->console, event->event, event->group_id );
1060 return !get_error();
1063 case IOCTL_CONDRV_SETUP_INPUT:
1065 struct termios term;
1066 obj_handle_t handle;
1067 struct file *file;
1068 int unix_fd;
1070 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1072 set_error( STATUS_INVALID_PARAMETER );
1073 return 0;
1075 if (server->term_fd != -1)
1077 tcsetattr( server->term_fd, TCSANOW, &server->termios );
1078 close( server->term_fd );
1079 server->term_fd = -1;
1081 handle = *(unsigned int *)get_req_data();
1082 if (!handle) return 1;
1083 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
1085 return 0;
1087 unix_fd = get_file_unix_fd( file );
1088 release_object( file );
1090 if (tcgetattr( unix_fd, &server->termios ))
1092 file_set_error();
1093 return 0;
1095 term = server->termios;
1096 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
1097 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
1098 term.c_cflag &= ~(CSIZE | PARENB);
1099 term.c_cflag |= CS8;
1100 term.c_cc[VMIN] = 1;
1101 term.c_cc[VTIME] = 0;
1102 if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
1104 file_set_error();
1105 return 0;
1107 return 1;
1110 default:
1111 set_error( STATUS_INVALID_HANDLE );
1112 return 0;
1116 static void console_connection_dump( struct object *obj, int verbose )
1118 fputs( "console connection\n", stderr );
1121 static struct fd *console_connection_get_fd( struct object *obj )
1123 struct console_connection *connection = (struct console_connection *)obj;
1124 return (struct fd *)grab_object( connection->fd );
1127 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1129 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1131 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1133 if (!current->process->console)
1135 set_error( STATUS_INVALID_HANDLE );
1136 return NULL;
1138 name->len = 0;
1139 return grab_object( current->process->console );
1142 return NULL;
1145 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
1146 unsigned int sharing, unsigned int options )
1148 return grab_object( obj );
1151 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1153 free_console( process );
1154 return 1;
1157 static void console_connection_destroy( struct object *obj )
1159 struct console_connection *connection = (struct console_connection *)obj;
1160 if (connection->fd) release_object( connection->fd );
1163 static struct object_type *console_device_get_type( struct object *obj )
1165 static const WCHAR name[] = {'D','e','v','i','c','e'};
1166 static const struct unicode_str str = { name, sizeof(name) };
1167 return get_object_type( &str );
1170 static void console_device_dump( struct object *obj, int verbose )
1172 fputs( "Console device\n", stderr );
1175 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1177 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
1178 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1179 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1180 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1181 static const WCHAR inputW[] = {'I','n','p','u','t'};
1182 static const WCHAR outputW[] = {'O','u','t','p','u','t'};
1183 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1184 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
1186 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1188 if (!current->process->console)
1190 set_error( STATUS_INVALID_HANDLE );
1191 return NULL;
1193 name->len = 0;
1194 return grab_object( current->process->console );
1197 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1199 if (!current->process->console || !current->process->console->active)
1201 set_error( STATUS_INVALID_HANDLE );
1202 return NULL;
1204 name->len = 0;
1205 return grab_object( current->process->console->active );
1208 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1210 name->len = 0;
1211 return grab_object( obj );
1214 if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
1216 name->len = 0;
1217 return alloc_object( &input_device_ops );
1220 if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
1222 name->len = 0;
1223 return alloc_object( &output_device_ops );
1226 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1228 if (!current->process->console)
1230 set_error( STATUS_INVALID_HANDLE );
1231 return NULL;
1233 name->len = 0;
1234 return create_console_output( current->process->console );
1237 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
1239 name->len = 0;
1240 return create_console_server();
1243 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
1245 name->len = 0;
1246 return create_console_connection( NULL );
1249 return NULL;
1252 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1253 unsigned int sharing, unsigned int options )
1255 int is_output;
1256 access = default_fd_map_access( obj, access );
1257 is_output = access & FILE_WRITE_DATA;
1258 if (!current->process->console || (is_output && !current->process->console))
1260 set_error( STATUS_INVALID_HANDLE );
1261 return NULL;
1263 if (is_output && (access & FILE_READ_DATA))
1265 set_error( STATUS_INVALID_PARAMETER );
1266 return NULL;
1268 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1271 static void input_device_dump( struct object *obj, int verbose )
1273 fputs( "console Input device\n", stderr );
1276 static int input_device_add_queue( struct object *obj, struct wait_queue_entry *entry )
1278 if (!current->process->console)
1280 set_error( STATUS_ACCESS_DENIED );
1281 return 0;
1283 return add_queue( &current->process->console->obj, entry );
1286 static struct fd *input_device_get_fd( struct object *obj )
1288 if (!current->process->console)
1290 set_error( STATUS_ACCESS_DENIED );
1291 return 0;
1293 return get_obj_fd( &current->process->console->obj );
1296 static struct object *input_device_open_file( struct object *obj, unsigned int access,
1297 unsigned int sharing, unsigned int options )
1299 return grab_object( obj );
1302 static void output_device_dump( struct object *obj, int verbose )
1304 fputs( "console Output device\n", stderr );
1307 static int output_device_add_queue( struct object *obj, struct wait_queue_entry *entry )
1309 if (!current->process->console || !current->process->console->active)
1311 set_error( STATUS_ACCESS_DENIED );
1312 return 0;
1314 return add_queue( &current->process->console->obj, entry );
1317 static struct fd *output_device_get_fd( struct object *obj )
1319 if (!current->process->console || !current->process->console->active)
1321 set_error( STATUS_ACCESS_DENIED );
1322 return NULL;
1325 return get_obj_fd( &current->process->console->active->obj );
1328 static struct object *output_device_open_file( struct object *obj, unsigned int access,
1329 unsigned int sharing, unsigned int options )
1331 return grab_object( obj );
1334 struct object *create_console_device( struct object *root, const struct unicode_str *name,
1335 unsigned int attr, const struct security_descriptor *sd )
1337 return create_named_object( root, &console_device_ops, name, attr, sd );
1340 /* retrieve the next pending console ioctl request */
1341 DECL_HANDLER(get_next_console_request)
1343 struct console_host_ioctl *ioctl = NULL, *next;
1344 struct console_server *server;
1345 struct iosb *iosb = NULL;
1347 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
1348 if (!server) return;
1350 if (!server->console)
1352 set_error( STATUS_INVALID_HANDLE );
1353 release_object( server );
1354 return;
1357 if (!req->signal) server->console->signaled = 0;
1358 else if (!server->console->signaled)
1360 server->console->signaled = 1;
1361 wake_up( &server->console->obj, 0 );
1364 if (req->read)
1366 /* set result of current pending ioctl */
1367 if (list_empty( &server->read_queue ))
1369 set_error( STATUS_INVALID_HANDLE );
1370 release_object( server );
1371 return;
1374 ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
1375 list_remove( &ioctl->entry );
1376 list_move_tail( &server->queue, &server->read_queue );
1378 else if (server->busy)
1380 /* set result of previous ioctl */
1381 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1382 list_remove( &ioctl->entry );
1385 if (ioctl)
1387 unsigned int status = req->status;
1388 if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
1389 if (ioctl->async)
1391 iosb = async_get_iosb( ioctl->async );
1392 if (iosb->status == STATUS_PENDING)
1394 iosb->status = status;
1395 iosb->out_size = min( iosb->out_size, get_req_data_size() );
1396 if (iosb->out_size)
1398 if ((iosb->out_data = memdup( get_req_data(), iosb->out_size )))
1400 iosb->result = iosb->out_size;
1401 status = STATUS_ALERTED;
1403 else if (!status)
1405 iosb->status = STATUS_NO_MEMORY;
1406 iosb->out_size = 0;
1410 else
1412 release_object( ioctl->async );
1413 ioctl->async = NULL;
1416 console_host_ioctl_terminate( ioctl, status );
1417 if (iosb) release_object( iosb );
1419 if (req->read)
1421 release_object( server );
1422 return;
1424 server->busy = 0;
1427 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1428 * move it to read queue for execution after current read is complete. move all blocking
1429 * ioctl at the same time to preserve their order. */
1430 if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
1432 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1433 if (is_blocking_read_ioctl( ioctl->code ))
1435 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
1437 if (!is_blocking_read_ioctl( ioctl->code )) continue;
1438 list_remove( &ioctl->entry );
1439 list_add_tail( &server->read_queue, &ioctl->entry );
1444 /* return the next ioctl */
1445 if (!list_empty( &server->queue ))
1447 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1448 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
1450 if (!iosb || get_reply_max_size() >= iosb->in_size)
1452 reply->code = ioctl->code;
1453 reply->output = ioctl->output;
1455 if (iosb)
1457 reply->out_size = iosb->out_size;
1458 set_reply_data_ptr( iosb->in_data, iosb->in_size );
1459 iosb->in_data = NULL;
1462 if (is_blocking_read_ioctl( ioctl->code ))
1464 list_remove( &ioctl->entry );
1465 assert( list_empty( &server->read_queue ));
1466 list_add_tail( &server->read_queue, &ioctl->entry );
1468 else server->busy = 1;
1470 else
1472 reply->out_size = iosb->in_size;
1473 set_error( STATUS_BUFFER_OVERFLOW );
1475 if (iosb) release_object( iosb );
1477 else
1479 set_error( STATUS_PENDING );
1482 release_object( server );