server: Support FileFsDeviceInformation queries on console handles.
[wine.git] / server / console.c
blob799d31c6fdefbebe9b545ec3f87a2fe7bdb3d5fa
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 void console_get_volume_info( struct fd *fd, unsigned int info_class );
103 static int console_input_read( struct fd *fd, struct async *async, file_pos_t pos );
104 static int console_input_flush( struct fd *fd, struct async *async );
105 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
107 static const struct fd_ops console_input_fd_ops =
109 default_fd_get_poll_events, /* get_poll_events */
110 default_poll_event, /* poll_event */
111 console_get_fd_type, /* get_fd_type */
112 console_input_read, /* read */
113 no_fd_write, /* write */
114 console_input_flush, /* flush */
115 console_get_file_info, /* get_file_info */
116 console_get_volume_info, /* get_volume_info */
117 console_input_ioctl, /* ioctl */
118 default_fd_queue_async, /* queue_async */
119 default_fd_reselect_async /* reselect_async */
122 struct console_host_ioctl
124 unsigned int code; /* ioctl code */
125 int output; /* output id for screen buffer ioctls */
126 struct async *async; /* ioctl async */
127 struct list entry; /* list entry */
130 struct console_server
132 struct object obj; /* object header */
133 struct fd *fd; /* pseudo-fd for ioctls */
134 struct console_input *console; /* attached console */
135 struct list queue; /* ioctl queue */
136 struct list read_queue; /* blocking read queue */
137 int busy; /* flag if server processing an ioctl */
138 int term_fd; /* UNIX terminal fd */
139 struct termios termios; /* original termios */
142 static void console_server_dump( struct object *obj, int verbose );
143 static void console_server_destroy( struct object *obj );
144 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
145 static struct fd *console_server_get_fd( struct object *obj );
146 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
147 static struct object *console_server_open_file( struct object *obj, unsigned int access,
148 unsigned int sharing, unsigned int options );
150 static const struct object_ops console_server_ops =
152 sizeof(struct console_server), /* size */
153 console_server_dump, /* dump */
154 no_get_type, /* get_type */
155 add_queue, /* add_queue */
156 remove_queue, /* remove_queue */
157 console_server_signaled, /* signaled */
158 no_satisfied, /* satisfied */
159 no_signal, /* signal */
160 console_server_get_fd, /* get_fd */
161 default_fd_map_access, /* map_access */
162 default_get_sd, /* get_sd */
163 default_set_sd, /* set_sd */
164 no_get_full_name, /* get_full_name */
165 console_server_lookup_name, /* lookup_name */
166 no_link_name, /* link_name */
167 NULL, /* unlink_name */
168 console_server_open_file, /* open_file */
169 no_kernel_obj_list, /* get_kernel_obj_list */
170 fd_close_handle, /* close_handle */
171 console_server_destroy /* destroy */
174 static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
176 static const struct fd_ops console_server_fd_ops =
178 default_fd_get_poll_events, /* get_poll_events */
179 default_poll_event, /* poll_event */
180 console_get_fd_type, /* get_fd_type */
181 no_fd_read, /* read */
182 no_fd_write, /* write */
183 no_fd_flush, /* flush */
184 no_fd_get_file_info, /* get_file_info */
185 no_fd_get_volume_info, /* get_volume_info */
186 console_server_ioctl, /* ioctl */
187 default_fd_queue_async, /* queue_async */
188 default_fd_reselect_async /* reselect_async */
191 struct font_info
193 short int width;
194 short int height;
195 short int weight;
196 short int pitch_family;
197 WCHAR *face_name;
198 data_size_t face_len;
201 struct screen_buffer
203 struct object obj; /* object header */
204 struct list entry; /* entry in list of all screen buffers */
205 struct console_input *input; /* associated console input */
206 unsigned int id; /* buffer id */
207 struct fd *fd; /* for bare console, attached output fd */
208 struct async_queue ioctl_q; /* ioctl queue */
211 static void screen_buffer_dump( struct object *obj, int verbose );
212 static void screen_buffer_destroy( struct object *obj );
213 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
214 static struct fd *screen_buffer_get_fd( struct object *obj );
215 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
216 unsigned int sharing, unsigned int options );
218 static const struct object_ops screen_buffer_ops =
220 sizeof(struct screen_buffer), /* size */
221 screen_buffer_dump, /* dump */
222 no_get_type, /* get_type */
223 screen_buffer_add_queue, /* add_queue */
224 NULL, /* remove_queue */
225 NULL, /* signaled */
226 NULL, /* satisfied */
227 no_signal, /* signal */
228 screen_buffer_get_fd, /* get_fd */
229 default_fd_map_access, /* map_access */
230 default_get_sd, /* get_sd */
231 default_set_sd, /* set_sd */
232 no_get_full_name, /* get_full_name */
233 no_lookup_name, /* lookup_name */
234 no_link_name, /* link_name */
235 NULL, /* unlink_name */
236 screen_buffer_open_file, /* open_file */
237 no_kernel_obj_list, /* get_kernel_obj_list */
238 no_close_handle, /* close_handle */
239 screen_buffer_destroy /* destroy */
242 static int screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos );
243 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
245 static const struct fd_ops screen_buffer_fd_ops =
247 default_fd_get_poll_events, /* get_poll_events */
248 default_poll_event, /* poll_event */
249 console_get_fd_type, /* get_fd_type */
250 no_fd_read, /* read */
251 screen_buffer_write, /* write */
252 no_fd_flush, /* flush */
253 console_get_file_info, /* get_file_info */
254 console_get_volume_info, /* get_volume_info */
255 screen_buffer_ioctl, /* ioctl */
256 default_fd_queue_async, /* queue_async */
257 default_fd_reselect_async /* reselect_async */
260 static struct object_type *console_device_get_type( struct object *obj );
261 static void console_device_dump( struct object *obj, int verbose );
262 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
263 static struct object *console_device_open_file( struct object *obj, unsigned int access,
264 unsigned int sharing, unsigned int options );
266 static const struct object_ops console_device_ops =
268 sizeof(struct object), /* size */
269 console_device_dump, /* dump */
270 console_device_get_type, /* get_type */
271 no_add_queue, /* add_queue */
272 NULL, /* remove_queue */
273 NULL, /* signaled */
274 no_satisfied, /* satisfied */
275 no_signal, /* signal */
276 no_get_fd, /* get_fd */
277 default_fd_map_access, /* map_access */
278 default_get_sd, /* get_sd */
279 default_set_sd, /* set_sd */
280 default_get_full_name, /* get_full_name */
281 console_device_lookup_name, /* lookup_name */
282 directory_link_name, /* link_name */
283 default_unlink_name, /* unlink_name */
284 console_device_open_file, /* open_file */
285 no_kernel_obj_list, /* get_kernel_obj_list */
286 no_close_handle, /* close_handle */
287 no_destroy /* destroy */
290 static void input_device_dump( struct object *obj, int verbose );
291 static struct object *input_device_open_file( struct object *obj, unsigned int access,
292 unsigned int sharing, unsigned int options );
293 static int input_device_add_queue( struct object *obj, struct wait_queue_entry *entry );
294 static struct fd *input_device_get_fd( struct object *obj );
296 static const struct object_ops input_device_ops =
298 sizeof(struct object), /* size */
299 input_device_dump, /* dump */
300 console_device_get_type, /* get_type */
301 input_device_add_queue, /* add_queue */
302 NULL, /* remove_queue */
303 NULL, /* signaled */
304 no_satisfied, /* satisfied */
305 no_signal, /* signal */
306 input_device_get_fd, /* get_fd */
307 no_map_access, /* map_access */
308 default_get_sd, /* get_sd */
309 default_set_sd, /* set_sd */
310 no_get_full_name, /* get_full_name */
311 no_lookup_name, /* lookup_name */
312 directory_link_name, /* link_name */
313 default_unlink_name, /* unlink_name */
314 input_device_open_file, /* open_file */
315 no_kernel_obj_list, /* get_kernel_obj_list */
316 no_close_handle, /* close_handle */
317 no_destroy /* destroy */
320 static void output_device_dump( struct object *obj, int verbose );
321 static int output_device_add_queue( struct object *obj, struct wait_queue_entry *entry );
322 static struct fd *output_device_get_fd( struct object *obj );
323 static struct object *output_device_open_file( struct object *obj, unsigned int access,
324 unsigned int sharing, unsigned int options );
326 static const struct object_ops output_device_ops =
328 sizeof(struct object), /* size */
329 output_device_dump, /* dump */
330 console_device_get_type, /* get_type */
331 output_device_add_queue, /* add_queue */
332 NULL, /* remove_queue */
333 NULL, /* signaled */
334 no_satisfied, /* satisfied */
335 no_signal, /* signal */
336 output_device_get_fd, /* get_fd */
337 no_map_access, /* map_access */
338 default_get_sd, /* get_sd */
339 default_set_sd, /* set_sd */
340 no_get_full_name, /* get_full_name */
341 no_lookup_name, /* lookup_name */
342 directory_link_name, /* link_name */
343 default_unlink_name, /* unlink_name */
344 output_device_open_file, /* open_file */
345 no_kernel_obj_list, /* get_kernel_obj_list */
346 no_close_handle, /* close_handle */
347 no_destroy /* destroy */
350 struct console_connection
352 struct object obj; /* object header */
353 struct fd *fd; /* pseudo-fd for ioctls */
356 static void console_connection_dump( struct object *obj, int verbose );
357 static struct fd *console_connection_get_fd( struct object *obj );
358 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
359 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
360 unsigned int sharing, unsigned int options );
361 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
362 static void console_connection_destroy( struct object *obj );
364 static const struct object_ops console_connection_ops =
366 sizeof(struct console_connection),/* size */
367 console_connection_dump, /* dump */
368 console_device_get_type, /* get_type */
369 no_add_queue, /* add_queue */
370 NULL, /* remove_queue */
371 NULL, /* signaled */
372 no_satisfied, /* satisfied */
373 no_signal, /* signal */
374 console_connection_get_fd, /* get_fd */
375 no_map_access, /* map_access */
376 default_get_sd, /* get_sd */
377 default_set_sd, /* set_sd */
378 no_get_full_name, /* get_full_name */
379 console_connection_lookup_name, /* lookup_name */
380 directory_link_name, /* link_name */
381 default_unlink_name, /* unlink_name */
382 console_connection_open_file, /* open_file */
383 no_kernel_obj_list, /* get_kernel_obj_list */
384 console_connection_close_handle, /* close_handle */
385 console_connection_destroy /* destroy */
388 static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
390 static const struct fd_ops console_connection_fd_ops =
392 default_fd_get_poll_events, /* get_poll_events */
393 default_poll_event, /* poll_event */
394 console_get_fd_type, /* get_fd_type */
395 no_fd_read, /* read */
396 no_fd_write, /* write */
397 no_fd_flush, /* flush */
398 no_fd_get_file_info, /* get_file_info */
399 no_fd_get_volume_info, /* get_volume_info */
400 console_connection_ioctl, /* ioctl */
401 default_fd_queue_async, /* queue_async */
402 default_fd_reselect_async /* reselect_async */
405 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
407 static int console_input_signaled( struct object *obj, struct wait_queue_entry *entry )
409 struct console_input *console = (struct console_input*)obj;
410 return console->signaled;
413 static struct fd *console_input_get_fd( struct object* obj )
415 struct console_input *console_input = (struct console_input*)obj;
416 assert( obj->ops == &console_input_ops );
417 return (struct fd *)grab_object( console_input->fd );
420 static enum server_fd_type console_get_fd_type( struct fd *fd )
422 return FD_TYPE_CHAR;
425 static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
427 set_error( STATUS_INVALID_DEVICE_REQUEST );
430 static void console_get_volume_info( struct fd *fd, unsigned int info_class )
432 switch (info_class)
434 case FileFsDeviceInformation:
436 static const FILE_FS_DEVICE_INFORMATION device_info =
438 FILE_DEVICE_CONSOLE,
439 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
441 if (get_reply_max_size() >= sizeof(device_info))
442 set_reply_data( &device_info, sizeof(device_info) );
443 else
444 set_error( STATUS_BUFFER_TOO_SMALL );
445 break;
447 default:
448 set_error( STATUS_NOT_IMPLEMENTED );
452 static struct object *create_console_input(void)
454 struct console_input *console_input;
456 if (!(console_input = alloc_object( &console_input_ops )))
457 return NULL;
459 console_input->renderer = NULL;
460 console_input->signaled = 0;
461 console_input->num_proc = 0;
462 console_input->active = NULL;
463 console_input->server = NULL;
464 console_input->fd = NULL;
465 console_input->last_id = 0;
466 init_async_queue( &console_input->ioctl_q );
467 init_async_queue( &console_input->read_q );
469 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
470 FILE_SYNCHRONOUS_IO_NONALERT );
471 if (!console_input->fd)
473 release_object( console_input );
474 return NULL;
476 allow_fd_caching( console_input->fd );
477 return &console_input->obj;
480 static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
482 if (call->async)
484 async_terminate( call->async, status );
485 release_object( call->async );
487 free( call );
490 static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
491 struct async *async, struct async_queue *queue )
493 struct console_host_ioctl *ioctl;
495 if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
496 ioctl->code = code;
497 ioctl->output = output;
498 ioctl->async = NULL;
499 if (async)
501 ioctl->async = (struct async *)grab_object( async );
502 queue_async( queue, async );
504 list_add_tail( &server->queue, &ioctl->entry );
505 wake_up( &server->obj, 0 );
506 if (async) set_error( STATUS_PENDING );
507 return 1;
510 static void disconnect_console_server( struct console_server *server )
512 while (!list_empty( &server->queue ))
514 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
515 list_remove( &call->entry );
516 console_host_ioctl_terminate( call, STATUS_CANCELLED );
518 while (!list_empty( &server->read_queue ))
520 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
521 list_remove( &call->entry );
522 console_host_ioctl_terminate( call, STATUS_CANCELLED );
525 if (server->term_fd != -1)
527 tcsetattr( server->term_fd, TCSANOW, &server->termios );
528 close( server->term_fd );
529 server->term_fd = -1;
532 if (server->console)
534 assert( server->console->server == server );
535 server->console->server = NULL;
536 server->console = NULL;
537 wake_up( &server->obj, 0 );
541 static void set_active_screen_buffer( struct console_input *console_input, struct screen_buffer *screen_buffer )
543 if (console_input->active == screen_buffer) return;
544 if (console_input->active) release_object( console_input->active );
545 console_input->active = (struct screen_buffer *)grab_object( screen_buffer );
547 if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_ACTIVATE,
548 screen_buffer->id, NULL, NULL );
551 static struct object *create_console_output( struct console_input *console_input )
553 struct screen_buffer *screen_buffer;
555 if (console_input->last_id == ~0)
557 set_error( STATUS_NO_MEMORY );
558 return NULL;
561 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
562 return NULL;
564 screen_buffer->id = ++console_input->last_id;
565 screen_buffer->input = console_input;
566 init_async_queue( &screen_buffer->ioctl_q );
567 list_add_head( &screen_buffer_list, &screen_buffer->entry );
569 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
570 FILE_SYNCHRONOUS_IO_NONALERT );
571 if (!screen_buffer->fd)
573 release_object( screen_buffer );
574 return NULL;
576 allow_fd_caching(screen_buffer->fd);
578 if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_INIT_OUTPUT,
579 screen_buffer->id, NULL, NULL );
580 if (!console_input->active) set_active_screen_buffer( console_input, screen_buffer );
581 return &screen_buffer->obj;
584 /* free the console for this process */
585 int free_console( struct process *process )
587 struct console_input* console = process->console;
589 if (!console) return 0;
591 process->console = NULL;
592 console->num_proc--;
593 release_object( console );
595 return 1;
598 /* let process inherit the console from parent... this handle two cases :
599 * 1/ generic console inheritance
600 * 2/ parent is a renderer which launches process, and process should attach to the console
601 * rendered by parent
603 obj_handle_t inherit_console( struct thread *parent_thread, obj_handle_t handle, struct process *process,
604 obj_handle_t hconin )
606 struct console_input *console = NULL;
608 if (handle) return duplicate_handle( current->process, handle, process, 0, 0, DUP_HANDLE_SAME_ACCESS );
610 /* if parent is a renderer, then attach current process to its console
611 * a bit hacky....
613 if (hconin && parent_thread)
615 /* FIXME: should we check some access rights ? */
616 if (!(console = (struct console_input *)get_handle_obj( parent_thread->process, hconin,
617 0, &console_input_ops )))
618 clear_error(); /* ignore error */
620 if (!console) return 0;
622 process->console = console;
623 console->num_proc++;
624 return alloc_handle( process, process->console,
625 SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE, 0 );
628 struct thread *console_get_renderer( struct console_input *console )
630 return console->renderer;
633 struct console_signal_info
635 struct console_input *console;
636 process_id_t group;
637 int signal;
640 static int propagate_console_signal_cb(struct process *process, void *user)
642 struct console_signal_info* csi = (struct console_signal_info*)user;
644 if (process->console == csi->console && process->running_threads &&
645 (!csi->group || process->group_id == csi->group))
647 /* find a suitable thread to signal */
648 struct thread *thread;
649 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
651 if (send_thread_signal( thread, csi->signal )) break;
654 return FALSE;
657 static void propagate_console_signal( struct console_input *console,
658 int sig, process_id_t group_id )
660 struct console_signal_info csi;
662 if (!console)
664 set_error( STATUS_INVALID_PARAMETER );
665 return;
667 /* FIXME: should support the other events (like CTRL_BREAK) */
668 if (sig != CTRL_C_EVENT)
670 set_error( STATUS_NOT_IMPLEMENTED );
671 return;
673 csi.console = console;
674 csi.signal = SIGINT;
675 csi.group = group_id;
677 enum_processes(propagate_console_signal_cb, &csi);
680 /* dumb dump */
681 static void console_input_dump( struct object *obj, int verbose )
683 struct console_input *console = (struct console_input *)obj;
684 assert( obj->ops == &console_input_ops );
685 fprintf( stderr, "Console input active=%p server=%p\n",
686 console->active, console->server );
689 static void console_input_destroy( struct object *obj )
691 struct console_input *console_in = (struct console_input *)obj;
692 struct screen_buffer *curr;
694 assert( obj->ops == &console_input_ops );
696 if (console_in->server)
698 assert( console_in->server->console == console_in );
699 disconnect_console_server( console_in->server );
702 if (console_in->active) release_object( console_in->active );
703 console_in->active = NULL;
705 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
707 if (curr->input == console_in) curr->input = NULL;
710 free_async_queue( &console_in->ioctl_q );
711 free_async_queue( &console_in->read_q );
712 if (console_in->fd)
713 release_object( console_in->fd );
716 static struct object *create_console_connection( struct console_input *console )
718 struct console_connection *connection;
720 if (current->process->console)
722 set_error( STATUS_ACCESS_DENIED );
723 return NULL;
726 if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
727 if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
729 release_object( connection );
730 return NULL;
733 if (console)
735 current->process->console = (struct console_input *)grab_object( console );
736 console->num_proc++;
739 return &connection->obj;
742 static struct object *console_input_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
744 struct console_input *console = (struct console_input *)obj;
745 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
746 assert( obj->ops == &console_input_ops );
748 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
750 name->len = 0;
751 return create_console_connection( console );
754 return NULL;
757 static struct object *console_input_open_file( struct object *obj, unsigned int access,
758 unsigned int sharing, unsigned int options )
760 return grab_object( obj );
763 static void screen_buffer_dump( struct object *obj, int verbose )
765 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
766 assert( obj->ops == &screen_buffer_ops );
768 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
771 static void screen_buffer_destroy( struct object *obj )
773 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
775 assert( obj->ops == &screen_buffer_ops );
777 list_remove( &screen_buffer->entry );
778 if (screen_buffer->input && screen_buffer->input->server)
779 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
780 screen_buffer->id, NULL, NULL );
781 if (screen_buffer->fd) release_object( screen_buffer->fd );
782 free_async_queue( &screen_buffer->ioctl_q );
785 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
786 unsigned int sharing, unsigned int options )
788 return grab_object( obj );
791 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
793 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
794 if (!screen_buffer->input)
796 set_error( STATUS_ACCESS_DENIED );
797 return 0;
799 return add_queue( &screen_buffer->input->obj, entry );
802 static struct fd *screen_buffer_get_fd( struct object *obj )
804 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
805 assert( obj->ops == &screen_buffer_ops );
806 if (screen_buffer->fd)
807 return (struct fd*)grab_object( screen_buffer->fd );
808 set_error( STATUS_OBJECT_TYPE_MISMATCH );
809 return NULL;
812 static void console_server_dump( struct object *obj, int verbose )
814 assert( obj->ops == &console_server_ops );
815 fprintf( stderr, "Console server\n" );
818 static void console_server_destroy( struct object *obj )
820 struct console_server *server = (struct console_server *)obj;
821 assert( obj->ops == &console_server_ops );
822 disconnect_console_server( server );
823 if (server->fd) release_object( server->fd );
826 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
828 struct console_server *server = (struct console_server*)obj;
829 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
830 assert( obj->ops == &console_server_ops );
832 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
834 struct screen_buffer *screen_buffer;
835 name->len = 0;
836 if (server->console)
838 set_error( STATUS_INVALID_HANDLE );
839 return 0;
841 if (!(server->console = (struct console_input *)create_console_input())) return NULL;
842 if (!(screen_buffer = (struct screen_buffer *)create_console_output( server->console )))
844 release_object( server->console );
845 server->console = NULL;
846 return NULL;
848 release_object( screen_buffer );
849 server->console->server = server;
851 return &server->console->obj;
854 return NULL;
857 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
859 struct console_server *server = (struct console_server*)obj;
860 assert( obj->ops == &console_server_ops );
861 return !server->console || !list_empty( &server->queue );
864 static struct fd *console_server_get_fd( struct object* obj )
866 struct console_server *server = (struct console_server*)obj;
867 assert( obj->ops == &console_server_ops );
868 return (struct fd *)grab_object( server->fd );
871 static struct object *console_server_open_file( struct object *obj, unsigned int access,
872 unsigned int sharing, unsigned int options )
874 return grab_object( obj );
877 static struct object *create_console_server( void )
879 struct console_server *server;
881 if (!(server = alloc_object( &console_server_ops ))) return NULL;
882 server->console = NULL;
883 server->busy = 0;
884 server->term_fd = -1;
885 list_init( &server->queue );
886 list_init( &server->read_queue );
887 server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
888 if (!server->fd)
890 release_object( server );
891 return NULL;
893 allow_fd_caching(server->fd);
895 return &server->obj;
898 static int is_blocking_read_ioctl( unsigned int code )
900 switch (code)
902 case IOCTL_CONDRV_READ_INPUT:
903 case IOCTL_CONDRV_READ_CONSOLE:
904 case IOCTL_CONDRV_READ_FILE:
905 return 1;
906 default:
907 return 0;
911 static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
913 struct console_input *console = get_fd_user( fd );
915 switch (code)
917 case IOCTL_CONDRV_CTRL_EVENT:
919 const struct condrv_ctrl_event *event = get_req_data();
920 process_id_t group;
921 if (get_req_data_size() != sizeof(*event))
923 set_error( STATUS_INVALID_PARAMETER );
924 return 0;
926 group = event->group_id ? event->group_id : current->process->group_id;
927 if (!group)
929 set_error( STATUS_INVALID_PARAMETER );
930 return 0;
932 propagate_console_signal( console, event->event, group );
933 return !get_error();
936 default:
937 if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
939 set_error( STATUS_INVALID_HANDLE );
940 return 0;
942 return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
946 static int console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
948 struct console_input *console = get_fd_user( fd );
950 if (!console->server)
952 set_error( STATUS_INVALID_HANDLE );
953 return 0;
955 return queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
958 static int console_input_flush( struct fd *fd, struct async *async )
960 struct console_input *console = get_fd_user( fd );
962 if (!console->server)
964 set_error( STATUS_INVALID_HANDLE );
965 return 0;
967 return queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
970 static int screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
972 struct screen_buffer *screen_buffer = get_fd_user( fd );
973 struct iosb *iosb;
975 if (!screen_buffer->input || !screen_buffer->input->server)
977 set_error( STATUS_INVALID_HANDLE );
978 return 0;
981 if (!queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
982 screen_buffer->id, async, &screen_buffer->ioctl_q ))
983 return 0;
985 /* we can't use default async handling, because write result is not
986 * compatible with ioctl result */
987 iosb = async_get_iosb( async );
988 iosb->status = STATUS_SUCCESS;
989 iosb->result = iosb->in_size;
990 async_terminate( async, iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
991 release_object( iosb );
992 return 1;
995 static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
997 struct screen_buffer *screen_buffer = get_fd_user( fd );
999 switch (code)
1001 case IOCTL_CONDRV_ACTIVATE:
1002 if (!screen_buffer->input)
1004 set_error( STATUS_INVALID_HANDLE );
1005 return 0;
1008 set_active_screen_buffer( screen_buffer->input, screen_buffer );
1009 return 1;
1011 default:
1012 if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
1013 is_blocking_read_ioctl( code ))
1015 set_error( STATUS_INVALID_HANDLE );
1016 return 0;
1018 return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
1019 async, &screen_buffer->ioctl_q );
1023 static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1025 struct console_connection *console_connection = get_fd_user( fd );
1027 switch (code)
1029 case IOCTL_CONDRV_BIND_PID:
1031 struct process *process;
1032 unsigned int pid;
1033 if (get_req_data_size() != sizeof(unsigned int))
1035 set_error( STATUS_INVALID_PARAMETER );
1036 return 0;
1038 if (current->process->console)
1040 set_error( STATUS_INVALID_HANDLE );
1041 return 0;
1044 pid = *(unsigned int *)get_req_data();
1045 if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
1046 if (!(process = get_process_from_id( pid ))) return 0;
1048 if (process->console)
1050 current->process->console = (struct console_input *)grab_object( process->console );
1051 process->console->num_proc++;
1053 else set_error( STATUS_ACCESS_DENIED );
1054 release_object( process );
1055 return !get_error();
1058 default:
1059 return default_fd_ioctl( console_connection->fd, code, async );
1063 static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1065 struct console_server *server = get_fd_user( fd );
1067 switch (code)
1069 case IOCTL_CONDRV_CTRL_EVENT:
1071 const struct condrv_ctrl_event *event = get_req_data();
1072 if (get_req_data_size() != sizeof(*event))
1074 set_error( STATUS_INVALID_PARAMETER );
1075 return 0;
1077 if (!server->console)
1079 set_error( STATUS_INVALID_HANDLE );
1080 return 0;
1082 propagate_console_signal( server->console, event->event, event->group_id );
1083 return !get_error();
1086 case IOCTL_CONDRV_SETUP_INPUT:
1088 struct termios term;
1089 obj_handle_t handle;
1090 struct file *file;
1091 int unix_fd;
1093 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1095 set_error( STATUS_INVALID_PARAMETER );
1096 return 0;
1098 if (server->term_fd != -1)
1100 tcsetattr( server->term_fd, TCSANOW, &server->termios );
1101 close( server->term_fd );
1102 server->term_fd = -1;
1104 handle = *(unsigned int *)get_req_data();
1105 if (!handle) return 1;
1106 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
1108 return 0;
1110 unix_fd = get_file_unix_fd( file );
1111 release_object( file );
1113 if (tcgetattr( unix_fd, &server->termios ))
1115 file_set_error();
1116 return 0;
1118 term = server->termios;
1119 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
1120 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
1121 term.c_cflag &= ~(CSIZE | PARENB);
1122 term.c_cflag |= CS8;
1123 term.c_cc[VMIN] = 1;
1124 term.c_cc[VTIME] = 0;
1125 if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
1127 file_set_error();
1128 return 0;
1130 return 1;
1133 default:
1134 set_error( STATUS_INVALID_HANDLE );
1135 return 0;
1139 static void console_connection_dump( struct object *obj, int verbose )
1141 fputs( "console connection\n", stderr );
1144 static struct fd *console_connection_get_fd( struct object *obj )
1146 struct console_connection *connection = (struct console_connection *)obj;
1147 return (struct fd *)grab_object( connection->fd );
1150 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1152 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1154 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1156 if (!current->process->console)
1158 set_error( STATUS_INVALID_HANDLE );
1159 return NULL;
1161 name->len = 0;
1162 return grab_object( current->process->console );
1165 return NULL;
1168 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
1169 unsigned int sharing, unsigned int options )
1171 return grab_object( obj );
1174 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1176 free_console( process );
1177 return 1;
1180 static void console_connection_destroy( struct object *obj )
1182 struct console_connection *connection = (struct console_connection *)obj;
1183 if (connection->fd) release_object( connection->fd );
1186 static struct object_type *console_device_get_type( struct object *obj )
1188 static const WCHAR name[] = {'D','e','v','i','c','e'};
1189 static const struct unicode_str str = { name, sizeof(name) };
1190 return get_object_type( &str );
1193 static void console_device_dump( struct object *obj, int verbose )
1195 fputs( "Console device\n", stderr );
1198 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
1200 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
1201 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1202 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1203 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1204 static const WCHAR inputW[] = {'I','n','p','u','t'};
1205 static const WCHAR outputW[] = {'O','u','t','p','u','t'};
1206 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1207 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
1209 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1211 if (!current->process->console)
1213 set_error( STATUS_INVALID_HANDLE );
1214 return NULL;
1216 name->len = 0;
1217 return grab_object( current->process->console );
1220 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1222 if (!current->process->console || !current->process->console->active)
1224 set_error( STATUS_INVALID_HANDLE );
1225 return NULL;
1227 name->len = 0;
1228 return grab_object( current->process->console->active );
1231 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1233 name->len = 0;
1234 return grab_object( obj );
1237 if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
1239 name->len = 0;
1240 return alloc_object( &input_device_ops );
1243 if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
1245 name->len = 0;
1246 return alloc_object( &output_device_ops );
1249 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1251 if (!current->process->console)
1253 set_error( STATUS_INVALID_HANDLE );
1254 return NULL;
1256 name->len = 0;
1257 return create_console_output( current->process->console );
1260 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
1262 name->len = 0;
1263 return create_console_server();
1266 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
1268 name->len = 0;
1269 return create_console_connection( NULL );
1272 return NULL;
1275 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1276 unsigned int sharing, unsigned int options )
1278 int is_output;
1279 access = default_fd_map_access( obj, access );
1280 is_output = access & FILE_WRITE_DATA;
1281 if (!current->process->console || (is_output && !current->process->console))
1283 set_error( STATUS_INVALID_HANDLE );
1284 return NULL;
1286 if (is_output && (access & FILE_READ_DATA))
1288 set_error( STATUS_INVALID_PARAMETER );
1289 return NULL;
1291 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1294 static void input_device_dump( struct object *obj, int verbose )
1296 fputs( "console Input device\n", stderr );
1299 static int input_device_add_queue( struct object *obj, struct wait_queue_entry *entry )
1301 if (!current->process->console)
1303 set_error( STATUS_ACCESS_DENIED );
1304 return 0;
1306 return add_queue( &current->process->console->obj, entry );
1309 static struct fd *input_device_get_fd( struct object *obj )
1311 if (!current->process->console)
1313 set_error( STATUS_ACCESS_DENIED );
1314 return 0;
1316 return get_obj_fd( &current->process->console->obj );
1319 static struct object *input_device_open_file( struct object *obj, unsigned int access,
1320 unsigned int sharing, unsigned int options )
1322 return grab_object( obj );
1325 static void output_device_dump( struct object *obj, int verbose )
1327 fputs( "console Output device\n", stderr );
1330 static int output_device_add_queue( struct object *obj, struct wait_queue_entry *entry )
1332 if (!current->process->console || !current->process->console->active)
1334 set_error( STATUS_ACCESS_DENIED );
1335 return 0;
1337 return add_queue( &current->process->console->obj, entry );
1340 static struct fd *output_device_get_fd( struct object *obj )
1342 if (!current->process->console || !current->process->console->active)
1344 set_error( STATUS_ACCESS_DENIED );
1345 return NULL;
1348 return get_obj_fd( &current->process->console->active->obj );
1351 static struct object *output_device_open_file( struct object *obj, unsigned int access,
1352 unsigned int sharing, unsigned int options )
1354 return grab_object( obj );
1357 struct object *create_console_device( struct object *root, const struct unicode_str *name,
1358 unsigned int attr, const struct security_descriptor *sd )
1360 return create_named_object( root, &console_device_ops, name, attr, sd );
1363 /* retrieve the next pending console ioctl request */
1364 DECL_HANDLER(get_next_console_request)
1366 struct console_host_ioctl *ioctl = NULL, *next;
1367 struct console_server *server;
1368 struct iosb *iosb = NULL;
1370 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
1371 if (!server) return;
1373 if (!server->console)
1375 set_error( STATUS_INVALID_HANDLE );
1376 release_object( server );
1377 return;
1380 if (!req->signal) server->console->signaled = 0;
1381 else if (!server->console->signaled)
1383 server->console->signaled = 1;
1384 wake_up( &server->console->obj, 0 );
1387 if (req->read)
1389 /* set result of current pending ioctl */
1390 if (list_empty( &server->read_queue ))
1392 set_error( STATUS_INVALID_HANDLE );
1393 release_object( server );
1394 return;
1397 ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
1398 list_remove( &ioctl->entry );
1399 list_move_tail( &server->queue, &server->read_queue );
1401 else if (server->busy)
1403 /* set result of previous ioctl */
1404 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1405 list_remove( &ioctl->entry );
1408 if (ioctl)
1410 unsigned int status = req->status;
1411 if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
1412 if (ioctl->async)
1414 iosb = async_get_iosb( ioctl->async );
1415 if (iosb->status == STATUS_PENDING)
1417 iosb->status = status;
1418 iosb->out_size = min( iosb->out_size, get_req_data_size() );
1419 if (iosb->out_size)
1421 if ((iosb->out_data = memdup( get_req_data(), iosb->out_size )))
1423 iosb->result = iosb->out_size;
1424 status = STATUS_ALERTED;
1426 else if (!status)
1428 iosb->status = STATUS_NO_MEMORY;
1429 iosb->out_size = 0;
1433 else
1435 release_object( ioctl->async );
1436 ioctl->async = NULL;
1439 console_host_ioctl_terminate( ioctl, status );
1440 if (iosb) release_object( iosb );
1442 if (req->read)
1444 release_object( server );
1445 return;
1447 server->busy = 0;
1450 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1451 * move it to read queue for execution after current read is complete. move all blocking
1452 * ioctl at the same time to preserve their order. */
1453 if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
1455 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1456 if (is_blocking_read_ioctl( ioctl->code ))
1458 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
1460 if (!is_blocking_read_ioctl( ioctl->code )) continue;
1461 list_remove( &ioctl->entry );
1462 list_add_tail( &server->read_queue, &ioctl->entry );
1467 /* return the next ioctl */
1468 if (!list_empty( &server->queue ))
1470 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1471 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
1473 if (!iosb || get_reply_max_size() >= iosb->in_size)
1475 reply->code = ioctl->code;
1476 reply->output = ioctl->output;
1478 if (iosb)
1480 reply->out_size = iosb->out_size;
1481 set_reply_data_ptr( iosb->in_data, iosb->in_size );
1482 iosb->in_data = NULL;
1485 if (is_blocking_read_ioctl( ioctl->code ))
1487 list_remove( &ioctl->entry );
1488 assert( list_empty( &server->read_queue ));
1489 list_add_tail( &server->read_queue, &ioctl->entry );
1491 else server->busy = 1;
1493 else
1495 reply->out_size = iosb->in_size;
1496 set_error( STATUS_BUFFER_OVERFLOW );
1498 if (iosb) release_object( iosb );
1500 else
1502 set_error( STATUS_PENDING );
1505 release_object( server );