wbemprox: Add a stub MSSMBios_RawSMBiosTables implementation.
[wine.git] / server / console.c
blob977c428cf9f0e8e64e9bd4afe14e58b415776653
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"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <termios.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "handle.h"
37 #include "process.h"
38 #include "request.h"
39 #include "file.h"
40 #include "unicode.h"
41 #include "wincon.h"
42 #include "winternl.h"
43 #include "wine/condrv.h"
45 struct screen_buffer;
47 struct history_line
49 data_size_t len;
50 WCHAR text[1];
53 struct console
55 struct object obj; /* object header */
56 int signaled; /* is console signaled */
57 struct thread *renderer; /* console renderer thread */
58 struct screen_buffer *active; /* active screen buffer */
59 struct console_server *server; /* console server object */
60 unsigned int last_id; /* id of last created console buffer */
61 struct fd *fd; /* for bare console, attached input fd */
62 struct async_queue ioctl_q; /* ioctl queue */
63 struct async_queue read_q; /* read queue */
66 static void console_dump( struct object *obj, int verbose );
67 static void console_destroy( struct object *obj );
68 static int console_signaled( struct object *obj, struct wait_queue_entry *entry );
69 static struct fd *console_get_fd( struct object *obj );
70 static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
71 unsigned int attr, struct object *root );
72 static struct object *console_open_file( struct object *obj, unsigned int access,
73 unsigned int sharing, unsigned int options );
75 static const struct object_ops console_ops =
77 sizeof(struct console), /* size */
78 &file_type, /* type */
79 console_dump, /* dump */
80 add_queue, /* add_queue */
81 remove_queue, /* remove_queue */
82 console_signaled, /* signaled */
83 no_satisfied, /* satisfied */
84 no_signal, /* signal */
85 console_get_fd, /* get_fd */
86 default_map_access, /* map_access */
87 default_get_sd, /* get_sd */
88 default_set_sd, /* set_sd */
89 no_get_full_name, /* get_full_name */
90 console_lookup_name, /* lookup_name */
91 no_link_name, /* link_name */
92 NULL, /* unlink_name */
93 console_open_file, /* open_file */
94 no_kernel_obj_list, /* get_kernel_obj_list */
95 no_close_handle, /* close_handle */
96 console_destroy /* destroy */
99 static enum server_fd_type console_get_fd_type( struct fd *fd );
100 static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
101 static void console_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class );
102 static void console_read( struct fd *fd, struct async *async, file_pos_t pos );
103 static void console_flush( struct fd *fd, struct async *async );
104 static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
106 static const struct fd_ops console_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_read, /* read */
112 no_fd_write, /* write */
113 console_flush, /* flush */
114 console_get_file_info, /* get_file_info */
115 console_get_volume_info, /* get_volume_info */
116 console_ioctl, /* ioctl */
117 default_fd_cancel_async, /* cancel_async */
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 *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,
147 unsigned int attr, struct object *root );
148 static struct object *console_server_open_file( struct object *obj, unsigned int access,
149 unsigned int sharing, unsigned int options );
151 static const struct object_ops console_server_ops =
153 sizeof(struct console_server), /* size */
154 &file_type, /* type */
155 console_server_dump, /* dump */
156 add_queue, /* add_queue */
157 remove_queue, /* remove_queue */
158 console_server_signaled, /* signaled */
159 no_satisfied, /* satisfied */
160 no_signal, /* signal */
161 console_server_get_fd, /* get_fd */
162 default_map_access, /* map_access */
163 default_get_sd, /* get_sd */
164 default_set_sd, /* set_sd */
165 no_get_full_name, /* get_full_name */
166 console_server_lookup_name, /* lookup_name */
167 no_link_name, /* link_name */
168 NULL, /* unlink_name */
169 console_server_open_file, /* open_file */
170 no_kernel_obj_list, /* get_kernel_obj_list */
171 no_close_handle, /* close_handle */
172 console_server_destroy /* destroy */
175 static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
177 static const struct fd_ops console_server_fd_ops =
179 default_fd_get_poll_events, /* get_poll_events */
180 default_poll_event, /* poll_event */
181 console_get_fd_type, /* get_fd_type */
182 no_fd_read, /* read */
183 no_fd_write, /* write */
184 no_fd_flush, /* flush */
185 no_fd_get_file_info, /* get_file_info */
186 no_fd_get_volume_info, /* get_volume_info */
187 console_server_ioctl, /* ioctl */
188 default_fd_cancel_async, /* cancel_async */
189 default_fd_queue_async, /* queue_async */
190 default_fd_reselect_async /* reselect_async */
193 struct font_info
195 short int width;
196 short int height;
197 short int weight;
198 short int pitch_family;
199 WCHAR *face_name;
200 data_size_t face_len;
203 struct screen_buffer
205 struct object obj; /* object header */
206 struct list entry; /* entry in list of all screen buffers */
207 struct console *input; /* associated console input */
208 unsigned int id; /* buffer id */
209 struct fd *fd; /* for bare console, attached output fd */
210 struct async_queue ioctl_q; /* ioctl queue */
213 static void screen_buffer_dump( struct object *obj, int verbose );
214 static void screen_buffer_destroy( struct object *obj );
215 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
216 static struct fd *screen_buffer_get_fd( struct object *obj );
217 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
218 unsigned int sharing, unsigned int options );
220 static const struct object_ops screen_buffer_ops =
222 sizeof(struct screen_buffer), /* size */
223 &file_type, /* type */
224 screen_buffer_dump, /* dump */
225 screen_buffer_add_queue, /* add_queue */
226 NULL, /* remove_queue */
227 NULL, /* signaled */
228 NULL, /* satisfied */
229 no_signal, /* signal */
230 screen_buffer_get_fd, /* get_fd */
231 default_map_access, /* map_access */
232 default_get_sd, /* get_sd */
233 default_set_sd, /* set_sd */
234 no_get_full_name, /* get_full_name */
235 no_lookup_name, /* lookup_name */
236 no_link_name, /* link_name */
237 NULL, /* unlink_name */
238 screen_buffer_open_file, /* open_file */
239 no_kernel_obj_list, /* get_kernel_obj_list */
240 no_close_handle, /* close_handle */
241 screen_buffer_destroy /* destroy */
244 static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos );
245 static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
247 static const struct fd_ops screen_buffer_fd_ops =
249 default_fd_get_poll_events, /* get_poll_events */
250 default_poll_event, /* poll_event */
251 console_get_fd_type, /* get_fd_type */
252 no_fd_read, /* read */
253 screen_buffer_write, /* write */
254 no_fd_flush, /* flush */
255 console_get_file_info, /* get_file_info */
256 console_get_volume_info, /* get_volume_info */
257 screen_buffer_ioctl, /* ioctl */
258 default_fd_cancel_async, /* cancel_async */
259 default_fd_queue_async, /* queue_async */
260 default_fd_reselect_async /* reselect_async */
263 static void console_device_dump( struct object *obj, int verbose );
264 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
265 unsigned int attr, struct object *root );
266 static struct object *console_device_open_file( struct object *obj, unsigned int access,
267 unsigned int sharing, unsigned int options );
269 static const struct object_ops console_device_ops =
271 sizeof(struct object), /* size */
272 &device_type, /* type */
273 console_device_dump, /* dump */
274 no_add_queue, /* add_queue */
275 NULL, /* remove_queue */
276 NULL, /* signaled */
277 no_satisfied, /* satisfied */
278 no_signal, /* signal */
279 no_get_fd, /* get_fd */
280 default_map_access, /* map_access */
281 default_get_sd, /* get_sd */
282 default_set_sd, /* set_sd */
283 default_get_full_name, /* get_full_name */
284 console_device_lookup_name, /* lookup_name */
285 directory_link_name, /* link_name */
286 default_unlink_name, /* unlink_name */
287 console_device_open_file, /* open_file */
288 no_kernel_obj_list, /* get_kernel_obj_list */
289 no_close_handle, /* close_handle */
290 no_destroy /* destroy */
293 struct console_input
295 struct object obj; /* object header */
296 struct fd *fd; /* pseudo-fd */
299 static void console_input_dump( struct object *obj, int verbose );
300 static struct object *console_input_open_file( struct object *obj, unsigned int access,
301 unsigned int sharing, unsigned int options );
302 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry );
303 static struct fd *console_input_get_fd( struct object *obj );
304 static void console_input_destroy( struct object *obj );
306 static const struct object_ops console_input_ops =
308 sizeof(struct console_input), /* size */
309 &device_type, /* type */
310 console_input_dump, /* dump */
311 console_input_add_queue, /* add_queue */
312 NULL, /* remove_queue */
313 NULL, /* signaled */
314 no_satisfied, /* satisfied */
315 no_signal, /* signal */
316 console_input_get_fd, /* get_fd */
317 default_map_access, /* map_access */
318 default_get_sd, /* get_sd */
319 default_set_sd, /* set_sd */
320 no_get_full_name, /* get_full_name */
321 no_lookup_name, /* lookup_name */
322 directory_link_name, /* link_name */
323 default_unlink_name, /* unlink_name */
324 console_input_open_file, /* open_file */
325 no_kernel_obj_list, /* get_kernel_obj_list */
326 no_close_handle, /* close_handle */
327 console_input_destroy /* destroy */
330 static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos );
331 static void console_input_flush( struct fd *fd, struct async *async );
332 static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
334 static const struct fd_ops console_input_fd_ops =
336 default_fd_get_poll_events, /* get_poll_events */
337 default_poll_event, /* poll_event */
338 console_get_fd_type, /* get_fd_type */
339 console_input_read, /* read */
340 no_fd_write, /* write */
341 console_input_flush, /* flush */
342 console_get_file_info, /* get_file_info */
343 console_get_volume_info, /* get_volume_info */
344 console_input_ioctl, /* ioctl */
345 default_fd_cancel_async, /* cancel_async */
346 default_fd_queue_async, /* queue_async */
347 default_fd_reselect_async /* reselect_async */
350 struct console_output
352 struct object obj; /* object header */
353 struct fd *fd; /* pseudo-fd */
356 static void console_output_dump( struct object *obj, int verbose );
357 static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry );
358 static struct fd *console_output_get_fd( struct object *obj );
359 static struct object *console_output_open_file( struct object *obj, unsigned int access,
360 unsigned int sharing, unsigned int options );
361 static void console_output_destroy( struct object *obj );
363 static const struct object_ops console_output_ops =
365 sizeof(struct console_output), /* size */
366 &device_type, /* type */
367 console_output_dump, /* dump */
368 console_output_add_queue, /* add_queue */
369 NULL, /* remove_queue */
370 NULL, /* signaled */
371 no_satisfied, /* satisfied */
372 no_signal, /* signal */
373 console_output_get_fd, /* get_fd */
374 default_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 no_lookup_name, /* lookup_name */
379 directory_link_name, /* link_name */
380 default_unlink_name, /* unlink_name */
381 console_output_open_file, /* open_file */
382 no_kernel_obj_list, /* get_kernel_obj_list */
383 no_close_handle, /* close_handle */
384 console_output_destroy /* destroy */
387 static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos );
388 static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
390 static const struct fd_ops console_output_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 console_output_write, /* write */
397 no_fd_flush, /* flush */
398 console_get_file_info, /* get_file_info */
399 console_get_volume_info, /* get_volume_info */
400 console_output_ioctl, /* ioctl */
401 default_fd_cancel_async, /* cancel_async */
402 default_fd_queue_async, /* queue_async */
403 default_fd_reselect_async /* reselect_async */
406 struct console_connection
408 struct object obj; /* object header */
409 struct fd *fd; /* pseudo-fd for ioctls */
412 static void console_connection_dump( struct object *obj, int verbose );
413 static struct fd *console_connection_get_fd( struct object *obj );
414 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
415 unsigned int attr, struct object *root );
416 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
417 unsigned int sharing, unsigned int options );
418 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
419 static void console_connection_destroy( struct object *obj );
421 static const struct object_ops console_connection_ops =
423 sizeof(struct console_connection),/* size */
424 &device_type, /* type */
425 console_connection_dump, /* dump */
426 no_add_queue, /* add_queue */
427 NULL, /* remove_queue */
428 NULL, /* signaled */
429 no_satisfied, /* satisfied */
430 no_signal, /* signal */
431 console_connection_get_fd, /* get_fd */
432 default_map_access, /* map_access */
433 default_get_sd, /* get_sd */
434 default_set_sd, /* set_sd */
435 no_get_full_name, /* get_full_name */
436 console_connection_lookup_name, /* lookup_name */
437 directory_link_name, /* link_name */
438 default_unlink_name, /* unlink_name */
439 console_connection_open_file, /* open_file */
440 no_kernel_obj_list, /* get_kernel_obj_list */
441 console_connection_close_handle, /* close_handle */
442 console_connection_destroy /* destroy */
445 static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
447 static const struct fd_ops console_connection_fd_ops =
449 default_fd_get_poll_events, /* get_poll_events */
450 default_poll_event, /* poll_event */
451 console_get_fd_type, /* get_fd_type */
452 no_fd_read, /* read */
453 no_fd_write, /* write */
454 no_fd_flush, /* flush */
455 no_fd_get_file_info, /* get_file_info */
456 no_fd_get_volume_info, /* get_volume_info */
457 console_connection_ioctl, /* ioctl */
458 default_fd_cancel_async, /* cancel_async */
459 default_fd_queue_async, /* queue_async */
460 default_fd_reselect_async /* reselect_async */
463 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
465 static int console_signaled( struct object *obj, struct wait_queue_entry *entry )
467 struct console *console = (struct console*)obj;
468 return console->signaled;
471 static struct fd *console_get_fd( struct object *obj )
473 struct console *console = (struct console *)obj;
474 assert( obj->ops == &console_ops );
475 return (struct fd *)grab_object( console->fd );
478 static enum server_fd_type console_get_fd_type( struct fd *fd )
480 return FD_TYPE_CHAR;
483 static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
485 set_error( STATUS_INVALID_DEVICE_REQUEST );
488 static void console_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
490 switch (info_class)
492 case FileFsDeviceInformation:
494 static const FILE_FS_DEVICE_INFORMATION device_info =
496 FILE_DEVICE_CONSOLE,
497 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
499 if (get_reply_max_size() >= sizeof(device_info))
500 set_reply_data( &device_info, sizeof(device_info) );
501 else
502 set_error( STATUS_BUFFER_TOO_SMALL );
503 break;
505 default:
506 set_error( STATUS_NOT_IMPLEMENTED );
510 static struct object *create_console(void)
512 struct console *console;
514 if (!(console = alloc_object( &console_ops )))
515 return NULL;
517 console->renderer = NULL;
518 console->signaled = 0;
519 console->active = NULL;
520 console->server = NULL;
521 console->fd = NULL;
522 console->last_id = 0;
523 init_async_queue( &console->ioctl_q );
524 init_async_queue( &console->read_q );
526 console->fd = alloc_pseudo_fd( &console_fd_ops, &console->obj, FILE_SYNCHRONOUS_IO_NONALERT );
527 if (!console->fd)
529 release_object( console );
530 return NULL;
532 allow_fd_caching( console->fd );
533 return &console->obj;
536 static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
538 if (call->async)
540 async_terminate( call->async, status );
541 release_object( call->async );
543 free( call );
546 static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
547 struct async *async, struct async_queue *queue )
549 struct console_host_ioctl *ioctl;
551 if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
552 ioctl->code = code;
553 ioctl->output = output;
554 ioctl->async = NULL;
555 if (async)
557 ioctl->async = (struct async *)grab_object( async );
558 queue_async( queue, async );
560 list_add_tail( &server->queue, &ioctl->entry );
561 wake_up( &server->obj, 0 );
562 if (async) set_error( STATUS_PENDING );
563 return 1;
566 static void disconnect_console_server( struct console_server *server )
568 while (!list_empty( &server->queue ))
570 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
571 list_remove( &call->entry );
572 console_host_ioctl_terminate( call, STATUS_CANCELLED );
574 while (!list_empty( &server->read_queue ))
576 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
577 list_remove( &call->entry );
578 console_host_ioctl_terminate( call, STATUS_CANCELLED );
581 if (server->term_fd != -1)
583 tcsetattr( server->term_fd, TCSANOW, &server->termios );
584 close( server->term_fd );
585 server->term_fd = -1;
588 if (server->console)
590 assert( server->console->server == server );
591 server->console->server = NULL;
592 server->console = NULL;
593 wake_up( &server->obj, 0 );
597 static void set_active_screen_buffer( struct console *console, struct screen_buffer *screen_buffer )
599 if (console->active == screen_buffer) return;
600 if (console->active) release_object( console->active );
601 console->active = (struct screen_buffer *)grab_object( screen_buffer );
603 if (console->server) queue_host_ioctl( console->server, IOCTL_CONDRV_ACTIVATE,
604 screen_buffer->id, NULL, NULL );
607 static struct object *create_screen_buffer( struct console *console )
609 struct screen_buffer *screen_buffer;
611 if (console->last_id == ~0)
613 set_error( STATUS_NO_MEMORY );
614 return NULL;
617 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
618 return NULL;
620 screen_buffer->id = ++console->last_id;
621 screen_buffer->input = console;
622 init_async_queue( &screen_buffer->ioctl_q );
623 list_add_head( &screen_buffer_list, &screen_buffer->entry );
625 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
626 FILE_SYNCHRONOUS_IO_NONALERT );
627 if (!screen_buffer->fd)
629 release_object( screen_buffer );
630 return NULL;
632 allow_fd_caching(screen_buffer->fd);
634 if (console->server) queue_host_ioctl( console->server, IOCTL_CONDRV_INIT_OUTPUT,
635 screen_buffer->id, NULL, NULL );
636 if (!console->active) set_active_screen_buffer( console, screen_buffer );
637 return &screen_buffer->obj;
640 struct thread *console_get_renderer( struct console *console )
642 return console->renderer;
645 struct console_signal_info
647 struct console *console;
648 process_id_t group;
649 int signal;
652 static int propagate_console_signal_cb(struct process *process, void *user)
654 struct console_signal_info* csi = (struct console_signal_info*)user;
656 if (process->console == csi->console && (!csi->group || process->group_id == csi->group))
658 /* find a suitable thread to signal */
659 struct thread *thread;
660 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
662 if (send_thread_signal( thread, csi->signal )) break;
665 return FALSE;
668 static void propagate_console_signal( struct console *console,
669 int sig, process_id_t group_id )
671 struct console_signal_info csi;
673 if (!console)
675 set_error( STATUS_INVALID_PARAMETER );
676 return;
678 /* FIXME: should support the other events (like CTRL_BREAK) */
679 if (sig != CTRL_C_EVENT)
681 set_error( STATUS_NOT_IMPLEMENTED );
682 return;
684 csi.console = console;
685 csi.signal = SIGINT;
686 csi.group = group_id;
688 enum_processes(propagate_console_signal_cb, &csi);
691 /* dumb dump */
692 static void console_dump( struct object *obj, int verbose )
694 struct console *console = (struct console *)obj;
695 assert( obj->ops == &console_ops );
696 fprintf( stderr, "Console input active=%p server=%p\n",
697 console->active, console->server );
700 static void console_destroy( struct object *obj )
702 struct console *console = (struct console *)obj;
703 struct screen_buffer *curr;
705 assert( obj->ops == &console_ops );
707 if (console->server)
709 assert( console->server->console == console );
710 disconnect_console_server( console->server );
713 if (console->active) release_object( console->active );
714 console->active = NULL;
716 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
718 if (curr->input == console) curr->input = NULL;
721 free_async_queue( &console->ioctl_q );
722 free_async_queue( &console->read_q );
723 if (console->fd)
724 release_object( console->fd );
727 static struct object *create_console_connection( struct console *console )
729 struct console_connection *connection;
731 if (current->process->console)
733 set_error( STATUS_ACCESS_DENIED );
734 return NULL;
737 if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
738 if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
740 release_object( connection );
741 return NULL;
744 if (console)
745 current->process->console = (struct console *)grab_object( console );
747 return &connection->obj;
750 static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
751 unsigned int attr, struct object *root )
753 struct console *console = (struct console *)obj;
754 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
755 assert( obj->ops == &console_ops );
757 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
759 name->len = 0;
760 return create_console_connection( console );
763 return NULL;
766 static struct object *console_open_file( struct object *obj, unsigned int access,
767 unsigned int sharing, unsigned int options )
769 return grab_object( obj );
772 static void screen_buffer_dump( struct object *obj, int verbose )
774 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
775 assert( obj->ops == &screen_buffer_ops );
777 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
780 static void screen_buffer_destroy( struct object *obj )
782 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
784 assert( obj->ops == &screen_buffer_ops );
786 list_remove( &screen_buffer->entry );
787 if (screen_buffer->input && screen_buffer->input->server)
788 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
789 screen_buffer->id, NULL, NULL );
790 if (screen_buffer->fd) release_object( screen_buffer->fd );
791 free_async_queue( &screen_buffer->ioctl_q );
794 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
795 unsigned int sharing, unsigned int options )
797 return grab_object( obj );
800 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
802 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
803 if (!screen_buffer->input)
805 set_error( STATUS_ACCESS_DENIED );
806 return 0;
808 return add_queue( &screen_buffer->input->obj, entry );
811 static struct fd *screen_buffer_get_fd( struct object *obj )
813 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
814 assert( obj->ops == &screen_buffer_ops );
815 if (screen_buffer->fd)
816 return (struct fd*)grab_object( screen_buffer->fd );
817 set_error( STATUS_OBJECT_TYPE_MISMATCH );
818 return NULL;
821 static void console_server_dump( struct object *obj, int verbose )
823 assert( obj->ops == &console_server_ops );
824 fprintf( stderr, "Console server\n" );
827 static void console_server_destroy( struct object *obj )
829 struct console_server *server = (struct console_server *)obj;
830 assert( obj->ops == &console_server_ops );
831 disconnect_console_server( server );
832 if (server->fd) release_object( server->fd );
835 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
836 unsigned int attr, struct object *root )
838 struct console_server *server = (struct console_server*)obj;
839 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
840 assert( obj->ops == &console_server_ops );
842 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
844 struct screen_buffer *screen_buffer;
845 name->len = 0;
846 if (server->console)
848 set_error( STATUS_INVALID_HANDLE );
849 return 0;
851 if (!(server->console = (struct console *)create_console())) return NULL;
852 if (!(screen_buffer = (struct screen_buffer *)create_screen_buffer( server->console )))
854 release_object( server->console );
855 server->console = NULL;
856 return NULL;
858 release_object( screen_buffer );
859 server->console->server = server;
861 return &server->console->obj;
864 return NULL;
867 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
869 struct console_server *server = (struct console_server*)obj;
870 assert( obj->ops == &console_server_ops );
871 return !server->console || !list_empty( &server->queue );
874 static struct fd *console_server_get_fd( struct object* obj )
876 struct console_server *server = (struct console_server*)obj;
877 assert( obj->ops == &console_server_ops );
878 return (struct fd *)grab_object( server->fd );
881 static struct object *console_server_open_file( struct object *obj, unsigned int access,
882 unsigned int sharing, unsigned int options )
884 return grab_object( obj );
887 static struct object *create_console_server( void )
889 struct console_server *server;
891 if (!(server = alloc_object( &console_server_ops ))) return NULL;
892 server->console = NULL;
893 server->busy = 0;
894 server->term_fd = -1;
895 list_init( &server->queue );
896 list_init( &server->read_queue );
897 server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
898 if (!server->fd)
900 release_object( server );
901 return NULL;
903 allow_fd_caching(server->fd);
905 return &server->obj;
908 static int is_blocking_read_ioctl( unsigned int code )
910 switch (code)
912 case IOCTL_CONDRV_READ_INPUT:
913 case IOCTL_CONDRV_READ_CONSOLE:
914 case IOCTL_CONDRV_READ_FILE:
915 return 1;
916 default:
917 return 0;
921 static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
923 struct console *console = get_fd_user( fd );
925 switch (code)
927 case IOCTL_CONDRV_CTRL_EVENT:
929 const struct condrv_ctrl_event *event = get_req_data();
930 process_id_t group;
931 if (get_req_data_size() != sizeof(*event))
933 set_error( STATUS_INVALID_PARAMETER );
934 return;
936 group = event->group_id ? event->group_id : current->process->group_id;
937 if (!group)
939 set_error( STATUS_INVALID_PARAMETER );
940 return;
942 propagate_console_signal( console, event->event, group );
943 return;
946 default:
947 if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
949 set_error( STATUS_INVALID_HANDLE );
950 return;
952 queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
956 static void console_read( struct fd *fd, struct async *async, file_pos_t pos )
958 struct console *console = get_fd_user( fd );
960 if (!console->server)
962 set_error( STATUS_INVALID_HANDLE );
963 return;
965 queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
968 static void console_flush( struct fd *fd, struct async *async )
970 struct console *console = get_fd_user( fd );
972 if (!console->server)
974 set_error( STATUS_INVALID_HANDLE );
975 return;
977 queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
980 static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
982 struct screen_buffer *screen_buffer = get_fd_user( fd );
984 if (!screen_buffer->input || !screen_buffer->input->server)
986 set_error( STATUS_INVALID_HANDLE );
987 return;
990 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
991 screen_buffer->id, async, &screen_buffer->ioctl_q );
994 static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
996 struct screen_buffer *screen_buffer = get_fd_user( fd );
998 switch (code)
1000 case IOCTL_CONDRV_ACTIVATE:
1001 if (!screen_buffer->input)
1003 set_error( STATUS_INVALID_HANDLE );
1004 return;
1007 set_active_screen_buffer( screen_buffer->input, screen_buffer );
1008 return;
1010 default:
1011 if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
1012 is_blocking_read_ioctl( code ))
1014 set_error( STATUS_INVALID_HANDLE );
1015 return;
1017 queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
1018 async, &screen_buffer->ioctl_q );
1022 static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1024 struct console_connection *console_connection = get_fd_user( fd );
1026 switch (code)
1028 case IOCTL_CONDRV_BIND_PID:
1030 struct process *process;
1031 unsigned int pid;
1032 if (get_req_data_size() != sizeof(unsigned int))
1034 set_error( STATUS_INVALID_PARAMETER );
1035 return;
1037 if (current->process->console)
1039 set_error( STATUS_INVALID_HANDLE );
1040 return;
1043 pid = *(unsigned int *)get_req_data();
1044 if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
1045 if (!(process = get_process_from_id( pid ))) return;
1047 if (process->console)
1048 current->process->console = (struct console *)grab_object( process->console );
1049 else set_error( STATUS_ACCESS_DENIED );
1050 release_object( process );
1051 return;
1054 default:
1055 default_fd_ioctl( console_connection->fd, code, async );
1059 static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1061 struct console_server *server = get_fd_user( fd );
1063 switch (code)
1065 case IOCTL_CONDRV_CTRL_EVENT:
1067 const struct condrv_ctrl_event *event = get_req_data();
1068 if (get_req_data_size() != sizeof(*event))
1070 set_error( STATUS_INVALID_PARAMETER );
1071 return;
1073 if (!server->console)
1075 set_error( STATUS_INVALID_HANDLE );
1076 return;
1078 propagate_console_signal( server->console, event->event, event->group_id );
1079 return;
1082 case IOCTL_CONDRV_SETUP_INPUT:
1084 struct termios term;
1085 obj_handle_t handle;
1086 struct file *file;
1087 int unix_fd;
1089 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1091 set_error( STATUS_INVALID_PARAMETER );
1092 return;
1094 if (server->term_fd != -1)
1096 tcsetattr( server->term_fd, TCSANOW, &server->termios );
1097 close( server->term_fd );
1098 server->term_fd = -1;
1100 handle = *(unsigned int *)get_req_data();
1101 if (!handle) return;
1102 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
1104 return;
1106 unix_fd = get_file_unix_fd( file );
1107 release_object( file );
1109 if (tcgetattr( unix_fd, &server->termios ))
1111 file_set_error();
1112 return;
1114 term = server->termios;
1115 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
1116 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
1117 term.c_cflag &= ~(CSIZE | PARENB);
1118 term.c_cflag |= CS8;
1119 term.c_cc[VMIN] = 1;
1120 term.c_cc[VTIME] = 0;
1121 if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
1122 file_set_error();
1123 return;
1126 default:
1127 set_error( STATUS_INVALID_HANDLE );
1128 return;
1132 static void console_connection_dump( struct object *obj, int verbose )
1134 fputs( "console connection\n", stderr );
1137 static struct fd *console_connection_get_fd( struct object *obj )
1139 struct console_connection *connection = (struct console_connection *)obj;
1140 return (struct fd *)grab_object( connection->fd );
1143 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
1144 unsigned int attr, struct object *root )
1146 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1148 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1150 if (!current->process->console)
1152 set_error( STATUS_INVALID_HANDLE );
1153 return NULL;
1155 name->len = 0;
1156 return grab_object( current->process->console );
1159 return NULL;
1162 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
1163 unsigned int sharing, unsigned int options )
1165 return grab_object( obj );
1168 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1170 struct console *console = process->console;
1172 if (console)
1174 process->console = NULL;
1175 release_object( console );
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 void console_device_dump( struct object *obj, int verbose )
1188 fputs( "Console device\n", stderr );
1191 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
1192 unsigned int attr, struct object *root )
1194 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
1195 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1196 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1197 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1198 static const WCHAR inputW[] = {'I','n','p','u','t'};
1199 static const WCHAR outputW[] = {'O','u','t','p','u','t'};
1200 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1201 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
1203 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1205 if (!current->process->console)
1207 set_error( STATUS_INVALID_HANDLE );
1208 return NULL;
1210 name->len = 0;
1211 return grab_object( current->process->console );
1214 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1216 if (!current->process->console || !current->process->console->active)
1218 set_error( STATUS_INVALID_HANDLE );
1219 return NULL;
1221 name->len = 0;
1222 return grab_object( current->process->console->active );
1225 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1227 name->len = 0;
1228 return grab_object( obj );
1231 if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
1233 struct console_input *console_input;
1234 name->len = 0;
1235 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
1236 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
1237 FILE_SYNCHRONOUS_IO_NONALERT );
1238 if (!console_input->fd)
1240 release_object( console_input );
1241 return NULL;
1243 return &console_input->obj;
1246 if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
1248 struct console_output *console_output;
1249 name->len = 0;
1250 if (!(console_output = alloc_object( &console_output_ops ))) return NULL;
1251 console_output->fd = alloc_pseudo_fd( &console_output_fd_ops, &console_output->obj,
1252 FILE_SYNCHRONOUS_IO_NONALERT );
1253 if (!console_output->fd)
1255 release_object( console_output );
1256 return NULL;
1258 return &console_output->obj;
1261 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1263 if (!current->process->console)
1265 set_error( STATUS_INVALID_HANDLE );
1266 return NULL;
1268 name->len = 0;
1269 return create_screen_buffer( current->process->console );
1272 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
1274 name->len = 0;
1275 return create_console_server();
1278 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
1280 name->len = 0;
1281 return create_console_connection( NULL );
1284 return NULL;
1287 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1288 unsigned int sharing, unsigned int options )
1290 int is_output;
1291 access = default_map_access( obj, access );
1292 is_output = access & FILE_WRITE_DATA;
1293 if (!current->process->console || (is_output && !current->process->console))
1295 set_error( STATUS_INVALID_HANDLE );
1296 return NULL;
1298 if (is_output && (access & FILE_READ_DATA))
1300 set_error( STATUS_INVALID_PARAMETER );
1301 return NULL;
1303 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1306 static void console_input_dump( struct object *obj, int verbose )
1308 fputs( "console Input device\n", stderr );
1311 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
1313 if (!current->process->console)
1315 set_error( STATUS_ACCESS_DENIED );
1316 return 0;
1318 return add_queue( &current->process->console->obj, entry );
1321 static struct fd *console_input_get_fd( struct object *obj )
1323 struct console_input *console_input = (struct console_input *)obj;
1324 assert( obj->ops == &console_input_ops );
1325 return (struct fd *)grab_object( console_input->fd );
1328 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1329 unsigned int sharing, unsigned int options )
1331 return grab_object( obj );
1334 static void console_input_destroy( struct object *obj )
1336 struct console_input *console_input = (struct console_input *)obj;
1338 assert( obj->ops == &console_input_ops );
1339 if (console_input->fd) release_object( console_input->fd );
1342 static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1344 struct console *console = current->process->console;
1346 if (!console)
1348 set_error( STATUS_INVALID_HANDLE );
1349 return;
1351 console_ioctl( console->fd, code, async );
1354 static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
1356 struct console *console = current->process->console;
1358 if (!console)
1360 set_error( STATUS_INVALID_HANDLE );
1361 return;
1363 console_read( console->fd, async, pos );
1366 static void console_input_flush( struct fd *fd, struct async *async )
1368 struct console *console = current->process->console;
1370 if (!console)
1372 set_error( STATUS_INVALID_HANDLE );
1373 return;
1375 console_flush( console->fd, async );
1378 static void console_output_dump( struct object *obj, int verbose )
1380 fputs( "console Output device\n", stderr );
1383 static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry )
1385 if (!current->process->console || !current->process->console->active)
1387 set_error( STATUS_ACCESS_DENIED );
1388 return 0;
1390 return add_queue( &current->process->console->obj, entry );
1393 static struct fd *console_output_get_fd( struct object *obj )
1395 struct console_output *console_output = (struct console_output *)obj;
1396 assert( obj->ops == &console_output_ops );
1397 return (struct fd *)grab_object( console_output->fd );
1400 static struct object *console_output_open_file( struct object *obj, unsigned int access,
1401 unsigned int sharing, unsigned int options )
1403 return grab_object( obj );
1406 static void console_output_destroy( struct object *obj )
1408 struct console_output *console_output = (struct console_output *)obj;
1410 assert( obj->ops == &console_output_ops );
1411 if (console_output->fd) release_object( console_output->fd );
1414 static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1416 struct console *console = current->process->console;
1418 if (!console || !console->active)
1420 set_error( STATUS_INVALID_HANDLE );
1421 return;
1423 screen_buffer_ioctl( console->active->fd, code, async );
1426 static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos )
1428 struct console *console = current->process->console;
1430 if (!console || !console->active)
1432 set_error( STATUS_INVALID_HANDLE );
1433 return;
1435 screen_buffer_write( console->active->fd, async, pos );
1438 struct object *create_console_device( struct object *root, const struct unicode_str *name,
1439 unsigned int attr, const struct security_descriptor *sd )
1441 return create_named_object( root, &console_device_ops, name, attr, sd );
1444 /* retrieve the next pending console ioctl request */
1445 DECL_HANDLER(get_next_console_request)
1447 struct console_host_ioctl *ioctl = NULL, *next;
1448 struct console_server *server;
1449 struct iosb *iosb = NULL;
1451 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
1452 if (!server) return;
1454 if (!server->console)
1456 set_error( STATUS_INVALID_HANDLE );
1457 release_object( server );
1458 return;
1461 if (!server->console->renderer) server->console->renderer = current;
1463 if (!req->signal) server->console->signaled = 0;
1464 else if (!server->console->signaled)
1466 server->console->signaled = 1;
1467 wake_up( &server->console->obj, 0 );
1470 if (req->read)
1472 /* set result of current pending ioctl */
1473 if (list_empty( &server->read_queue ))
1475 set_error( STATUS_INVALID_HANDLE );
1476 release_object( server );
1477 return;
1480 ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
1481 list_remove( &ioctl->entry );
1482 list_move_tail( &server->queue, &server->read_queue );
1484 else if (server->busy)
1486 /* set result of previous ioctl */
1487 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1488 list_remove( &ioctl->entry );
1491 if (ioctl)
1493 struct async *async = ioctl->async;
1494 unsigned int status = req->status;
1496 if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
1497 if (async)
1499 iosb = async_get_iosb( async );
1500 if (iosb->status == STATUS_PENDING)
1502 data_size_t out_size = min( iosb->out_size, get_req_data_size() );
1503 data_size_t result = ioctl->code == IOCTL_CONDRV_WRITE_FILE ? iosb->in_size : out_size;
1504 async_request_complete_alloc( async, status, result, out_size, get_req_data() );
1507 release_object( async );
1509 free( ioctl );
1510 if (iosb) release_object( iosb );
1512 if (req->read)
1514 release_object( server );
1515 return;
1517 server->busy = 0;
1520 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1521 * move it to read queue for execution after current read is complete. move all blocking
1522 * ioctl at the same time to preserve their order. */
1523 if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
1525 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1526 if (is_blocking_read_ioctl( ioctl->code ))
1528 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
1530 if (!is_blocking_read_ioctl( ioctl->code )) continue;
1531 list_remove( &ioctl->entry );
1532 list_add_tail( &server->read_queue, &ioctl->entry );
1537 /* return the next ioctl */
1538 if (!list_empty( &server->queue ))
1540 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1541 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
1543 if (!iosb || get_reply_max_size() >= iosb->in_size)
1545 reply->code = ioctl->code;
1546 reply->output = ioctl->output;
1548 if (iosb)
1550 reply->out_size = iosb->out_size;
1551 set_reply_data_ptr( iosb->in_data, iosb->in_size );
1552 iosb->in_data = NULL;
1555 if (is_blocking_read_ioctl( ioctl->code ))
1557 list_remove( &ioctl->entry );
1558 assert( list_empty( &server->read_queue ));
1559 list_add_tail( &server->read_queue, &ioctl->entry );
1561 else server->busy = 1;
1563 else
1565 reply->out_size = iosb->in_size;
1566 set_error( STATUS_BUFFER_OVERFLOW );
1568 if (iosb) release_object( iosb );
1570 else
1572 set_error( STATUS_PENDING );
1575 release_object( server );