ntdll: Add RtlDosPathNameToRelativeNtPathName_U.
[wine.git] / server / console.c
blob5f3f50d006f15ab9adbea4d1793f25eb6be821ed
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 );
74 static int console_add_queue( struct object *obj, struct wait_queue_entry *entry );
76 static const struct object_ops console_ops =
78 sizeof(struct console), /* size */
79 &file_type, /* type */
80 console_dump, /* dump */
81 console_add_queue, /* add_queue */
82 remove_queue, /* remove_queue */
83 console_signaled, /* signaled */
84 no_satisfied, /* satisfied */
85 no_signal, /* signal */
86 console_get_fd, /* get_fd */
87 default_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_lookup_name, /* lookup_name */
92 no_link_name, /* link_name */
93 NULL, /* unlink_name */
94 console_open_file, /* open_file */
95 no_kernel_obj_list, /* get_kernel_obj_list */
96 no_close_handle, /* close_handle */
97 console_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, struct async *async, unsigned int info_class );
103 static void console_read( struct fd *fd, struct async *async, file_pos_t pos );
104 static void console_flush( struct fd *fd, struct async *async );
105 static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
107 static const struct fd_ops console_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_read, /* read */
113 no_fd_write, /* write */
114 console_flush, /* flush */
115 console_get_file_info, /* get_file_info */
116 console_get_volume_info, /* get_volume_info */
117 console_ioctl, /* ioctl */
118 default_fd_cancel_async, /* cancel_async */
119 default_fd_queue_async, /* queue_async */
120 default_fd_reselect_async /* reselect_async */
123 struct console_host_ioctl
125 unsigned int code; /* ioctl code */
126 int output; /* output id for screen buffer ioctls */
127 struct async *async; /* ioctl async */
128 struct list entry; /* list entry */
131 struct console_server
133 struct object obj; /* object header */
134 struct fd *fd; /* pseudo-fd for ioctls */
135 struct console *console; /* attached console */
136 struct list queue; /* ioctl queue */
137 struct list read_queue; /* blocking read queue */
138 unsigned int busy : 1; /* flag if server processing an ioctl */
139 unsigned int once_input : 1; /* flag if input thread has already been requested */
140 int term_fd; /* UNIX terminal fd */
141 struct termios termios; /* original termios */
144 static void console_server_dump( struct object *obj, int verbose );
145 static void console_server_destroy( struct object *obj );
146 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
147 static struct fd *console_server_get_fd( struct object *obj );
148 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
149 unsigned int attr, struct object *root );
150 static struct object *console_server_open_file( struct object *obj, unsigned int access,
151 unsigned int sharing, unsigned int options );
153 static const struct object_ops console_server_ops =
155 sizeof(struct console_server), /* size */
156 &file_type, /* type */
157 console_server_dump, /* dump */
158 add_queue, /* add_queue */
159 remove_queue, /* remove_queue */
160 console_server_signaled, /* signaled */
161 no_satisfied, /* satisfied */
162 no_signal, /* signal */
163 console_server_get_fd, /* get_fd */
164 default_map_access, /* map_access */
165 default_get_sd, /* get_sd */
166 default_set_sd, /* set_sd */
167 no_get_full_name, /* get_full_name */
168 console_server_lookup_name, /* lookup_name */
169 no_link_name, /* link_name */
170 NULL, /* unlink_name */
171 console_server_open_file, /* open_file */
172 no_kernel_obj_list, /* get_kernel_obj_list */
173 no_close_handle, /* close_handle */
174 console_server_destroy /* destroy */
177 static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
179 static const struct fd_ops console_server_fd_ops =
181 default_fd_get_poll_events, /* get_poll_events */
182 default_poll_event, /* poll_event */
183 console_get_fd_type, /* get_fd_type */
184 no_fd_read, /* read */
185 no_fd_write, /* write */
186 no_fd_flush, /* flush */
187 no_fd_get_file_info, /* get_file_info */
188 no_fd_get_volume_info, /* get_volume_info */
189 console_server_ioctl, /* ioctl */
190 default_fd_cancel_async, /* cancel_async */
191 default_fd_queue_async, /* queue_async */
192 default_fd_reselect_async /* reselect_async */
195 struct font_info
197 short int width;
198 short int height;
199 short int weight;
200 short int pitch_family;
201 WCHAR *face_name;
202 data_size_t face_len;
205 struct screen_buffer
207 struct object obj; /* object header */
208 struct list entry; /* entry in list of all screen buffers */
209 struct console *input; /* associated console input */
210 unsigned int id; /* buffer id */
211 struct fd *fd; /* for bare console, attached output fd */
212 struct async_queue ioctl_q; /* ioctl queue */
215 static void screen_buffer_dump( struct object *obj, int verbose );
216 static void screen_buffer_destroy( struct object *obj );
217 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry );
218 static struct fd *screen_buffer_get_fd( struct object *obj );
219 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
220 unsigned int sharing, unsigned int options );
222 static const struct object_ops screen_buffer_ops =
224 sizeof(struct screen_buffer), /* size */
225 &file_type, /* type */
226 screen_buffer_dump, /* dump */
227 screen_buffer_add_queue, /* add_queue */
228 NULL, /* remove_queue */
229 NULL, /* signaled */
230 NULL, /* satisfied */
231 no_signal, /* signal */
232 screen_buffer_get_fd, /* get_fd */
233 default_map_access, /* map_access */
234 default_get_sd, /* get_sd */
235 default_set_sd, /* set_sd */
236 no_get_full_name, /* get_full_name */
237 no_lookup_name, /* lookup_name */
238 no_link_name, /* link_name */
239 NULL, /* unlink_name */
240 screen_buffer_open_file, /* open_file */
241 no_kernel_obj_list, /* get_kernel_obj_list */
242 no_close_handle, /* close_handle */
243 screen_buffer_destroy /* destroy */
246 static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos );
247 static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
249 static const struct fd_ops screen_buffer_fd_ops =
251 default_fd_get_poll_events, /* get_poll_events */
252 default_poll_event, /* poll_event */
253 console_get_fd_type, /* get_fd_type */
254 no_fd_read, /* read */
255 screen_buffer_write, /* write */
256 no_fd_flush, /* flush */
257 console_get_file_info, /* get_file_info */
258 console_get_volume_info, /* get_volume_info */
259 screen_buffer_ioctl, /* ioctl */
260 default_fd_cancel_async, /* cancel_async */
261 default_fd_queue_async, /* queue_async */
262 default_fd_reselect_async /* reselect_async */
265 static void console_device_dump( struct object *obj, int verbose );
266 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
267 unsigned int attr, struct object *root );
268 static struct object *console_device_open_file( struct object *obj, unsigned int access,
269 unsigned int sharing, unsigned int options );
271 static const struct object_ops console_device_ops =
273 sizeof(struct object), /* size */
274 &device_type, /* type */
275 console_device_dump, /* dump */
276 no_add_queue, /* add_queue */
277 NULL, /* remove_queue */
278 NULL, /* signaled */
279 no_satisfied, /* satisfied */
280 no_signal, /* signal */
281 no_get_fd, /* get_fd */
282 default_map_access, /* map_access */
283 default_get_sd, /* get_sd */
284 default_set_sd, /* set_sd */
285 default_get_full_name, /* get_full_name */
286 console_device_lookup_name, /* lookup_name */
287 directory_link_name, /* link_name */
288 default_unlink_name, /* unlink_name */
289 console_device_open_file, /* open_file */
290 no_kernel_obj_list, /* get_kernel_obj_list */
291 no_close_handle, /* close_handle */
292 no_destroy /* destroy */
295 struct console_input
297 struct object obj; /* object header */
298 struct fd *fd; /* pseudo-fd */
301 static void console_input_dump( struct object *obj, int verbose );
302 static struct object *console_input_open_file( struct object *obj, unsigned int access,
303 unsigned int sharing, unsigned int options );
304 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry );
305 static struct fd *console_input_get_fd( struct object *obj );
306 static void console_input_destroy( struct object *obj );
308 static const struct object_ops console_input_ops =
310 sizeof(struct console_input), /* size */
311 &device_type, /* type */
312 console_input_dump, /* dump */
313 console_input_add_queue, /* add_queue */
314 NULL, /* remove_queue */
315 NULL, /* signaled */
316 no_satisfied, /* satisfied */
317 no_signal, /* signal */
318 console_input_get_fd, /* get_fd */
319 default_map_access, /* map_access */
320 default_get_sd, /* get_sd */
321 default_set_sd, /* set_sd */
322 no_get_full_name, /* get_full_name */
323 no_lookup_name, /* lookup_name */
324 directory_link_name, /* link_name */
325 default_unlink_name, /* unlink_name */
326 console_input_open_file, /* open_file */
327 no_kernel_obj_list, /* get_kernel_obj_list */
328 no_close_handle, /* close_handle */
329 console_input_destroy /* destroy */
332 static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos );
333 static void console_input_flush( struct fd *fd, struct async *async );
334 static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
336 static const struct fd_ops console_input_fd_ops =
338 default_fd_get_poll_events, /* get_poll_events */
339 default_poll_event, /* poll_event */
340 console_get_fd_type, /* get_fd_type */
341 console_input_read, /* read */
342 no_fd_write, /* write */
343 console_input_flush, /* flush */
344 console_get_file_info, /* get_file_info */
345 console_get_volume_info, /* get_volume_info */
346 console_input_ioctl, /* ioctl */
347 default_fd_cancel_async, /* cancel_async */
348 default_fd_queue_async, /* queue_async */
349 default_fd_reselect_async /* reselect_async */
352 struct console_output
354 struct object obj; /* object header */
355 struct fd *fd; /* pseudo-fd */
358 static void console_output_dump( struct object *obj, int verbose );
359 static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry );
360 static struct fd *console_output_get_fd( struct object *obj );
361 static struct object *console_output_open_file( struct object *obj, unsigned int access,
362 unsigned int sharing, unsigned int options );
363 static void console_output_destroy( struct object *obj );
365 static const struct object_ops console_output_ops =
367 sizeof(struct console_output), /* size */
368 &device_type, /* type */
369 console_output_dump, /* dump */
370 console_output_add_queue, /* add_queue */
371 NULL, /* remove_queue */
372 NULL, /* signaled */
373 no_satisfied, /* satisfied */
374 no_signal, /* signal */
375 console_output_get_fd, /* get_fd */
376 default_map_access, /* map_access */
377 default_get_sd, /* get_sd */
378 default_set_sd, /* set_sd */
379 no_get_full_name, /* get_full_name */
380 no_lookup_name, /* lookup_name */
381 directory_link_name, /* link_name */
382 default_unlink_name, /* unlink_name */
383 console_output_open_file, /* open_file */
384 no_kernel_obj_list, /* get_kernel_obj_list */
385 no_close_handle, /* close_handle */
386 console_output_destroy /* destroy */
389 static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos );
390 static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
392 static const struct fd_ops console_output_fd_ops =
394 default_fd_get_poll_events, /* get_poll_events */
395 default_poll_event, /* poll_event */
396 console_get_fd_type, /* get_fd_type */
397 no_fd_read, /* read */
398 console_output_write, /* write */
399 no_fd_flush, /* flush */
400 console_get_file_info, /* get_file_info */
401 console_get_volume_info, /* get_volume_info */
402 console_output_ioctl, /* ioctl */
403 default_fd_cancel_async, /* cancel_async */
404 default_fd_queue_async, /* queue_async */
405 default_fd_reselect_async /* reselect_async */
408 struct console_connection
410 struct object obj; /* object header */
411 struct fd *fd; /* pseudo-fd for ioctls */
414 static void console_connection_dump( struct object *obj, int verbose );
415 static struct fd *console_connection_get_fd( struct object *obj );
416 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
417 unsigned int attr, struct object *root );
418 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
419 unsigned int sharing, unsigned int options );
420 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
421 static void console_connection_destroy( struct object *obj );
423 static const struct object_ops console_connection_ops =
425 sizeof(struct console_connection),/* size */
426 &device_type, /* type */
427 console_connection_dump, /* dump */
428 no_add_queue, /* add_queue */
429 NULL, /* remove_queue */
430 NULL, /* signaled */
431 no_satisfied, /* satisfied */
432 no_signal, /* signal */
433 console_connection_get_fd, /* get_fd */
434 default_map_access, /* map_access */
435 default_get_sd, /* get_sd */
436 default_set_sd, /* set_sd */
437 no_get_full_name, /* get_full_name */
438 console_connection_lookup_name, /* lookup_name */
439 directory_link_name, /* link_name */
440 default_unlink_name, /* unlink_name */
441 console_connection_open_file, /* open_file */
442 no_kernel_obj_list, /* get_kernel_obj_list */
443 console_connection_close_handle, /* close_handle */
444 console_connection_destroy /* destroy */
447 static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
449 static const struct fd_ops console_connection_fd_ops =
451 default_fd_get_poll_events, /* get_poll_events */
452 default_poll_event, /* poll_event */
453 console_get_fd_type, /* get_fd_type */
454 no_fd_read, /* read */
455 no_fd_write, /* write */
456 no_fd_flush, /* flush */
457 no_fd_get_file_info, /* get_file_info */
458 no_fd_get_volume_info, /* get_volume_info */
459 console_connection_ioctl, /* ioctl */
460 default_fd_cancel_async, /* cancel_async */
461 default_fd_queue_async, /* queue_async */
462 default_fd_reselect_async /* reselect_async */
465 static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
467 static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
468 struct async *async, struct async_queue *queue );
470 static int console_add_queue( struct object *obj, struct wait_queue_entry *entry )
472 struct console *console = (struct console*)obj;
473 assert( obj->ops == &console_ops );
474 /* before waiting, ensure conhost's input thread has been started */
475 if (console->server && !console->server->once_input)
477 console->server->once_input = 1;
478 if (console->server->term_fd == -1)
479 queue_host_ioctl( console->server, IOCTL_CONDRV_PEEK, 0, NULL, NULL );
481 return add_queue( &console->obj, entry );
484 static int console_signaled( struct object *obj, struct wait_queue_entry *entry )
486 struct console *console = (struct console*)obj;
487 return console->signaled;
490 static struct fd *console_get_fd( struct object *obj )
492 struct console *console = (struct console *)obj;
493 assert( obj->ops == &console_ops );
494 return (struct fd *)grab_object( console->fd );
497 static enum server_fd_type console_get_fd_type( struct fd *fd )
499 return FD_TYPE_CHAR;
502 static void console_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
504 set_error( STATUS_INVALID_DEVICE_REQUEST );
507 static void console_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
509 switch (info_class)
511 case FileFsDeviceInformation:
513 static const FILE_FS_DEVICE_INFORMATION device_info =
515 FILE_DEVICE_CONSOLE,
516 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
518 if (get_reply_max_size() >= sizeof(device_info))
519 set_reply_data( &device_info, sizeof(device_info) );
520 else
521 set_error( STATUS_BUFFER_TOO_SMALL );
522 break;
524 default:
525 set_error( STATUS_NOT_IMPLEMENTED );
529 static struct object *create_console(void)
531 struct console *console;
533 if (!(console = alloc_object( &console_ops )))
534 return NULL;
536 console->renderer = NULL;
537 console->signaled = 0;
538 console->active = NULL;
539 console->server = NULL;
540 console->fd = NULL;
541 console->last_id = 0;
542 init_async_queue( &console->ioctl_q );
543 init_async_queue( &console->read_q );
545 console->fd = alloc_pseudo_fd( &console_fd_ops, &console->obj, FILE_SYNCHRONOUS_IO_NONALERT );
546 if (!console->fd)
548 release_object( console );
549 return NULL;
551 allow_fd_caching( console->fd );
552 return &console->obj;
555 static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
557 if (call->async)
559 async_terminate( call->async, status );
560 release_object( call->async );
562 free( call );
565 static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
566 struct async *async, struct async_queue *queue )
568 struct console_host_ioctl *ioctl;
570 if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
571 ioctl->code = code;
572 ioctl->output = output;
573 ioctl->async = NULL;
574 if (async)
576 ioctl->async = (struct async *)grab_object( async );
577 queue_async( queue, async );
579 list_add_tail( &server->queue, &ioctl->entry );
580 wake_up( &server->obj, 0 );
581 if (async) set_error( STATUS_PENDING );
582 return 1;
585 static void disconnect_console_server( struct console_server *server )
587 while (!list_empty( &server->queue ))
589 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
590 list_remove( &call->entry );
591 console_host_ioctl_terminate( call, STATUS_CANCELLED );
593 while (!list_empty( &server->read_queue ))
595 struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
596 list_remove( &call->entry );
597 console_host_ioctl_terminate( call, STATUS_CANCELLED );
600 if (server->term_fd != -1)
602 tcsetattr( server->term_fd, TCSANOW, &server->termios );
603 close( server->term_fd );
604 server->term_fd = -1;
607 if (server->console)
609 assert( server->console->server == server );
610 server->console->server = NULL;
611 server->console = NULL;
612 wake_up( &server->obj, 0 );
616 static void set_active_screen_buffer( struct console *console, struct screen_buffer *screen_buffer )
618 if (console->active == screen_buffer) return;
619 if (console->active) release_object( console->active );
620 console->active = (struct screen_buffer *)grab_object( screen_buffer );
622 if (console->server) queue_host_ioctl( console->server, IOCTL_CONDRV_ACTIVATE,
623 screen_buffer->id, NULL, NULL );
626 static struct object *create_screen_buffer( struct console *console )
628 struct screen_buffer *screen_buffer;
630 if (console->last_id == ~0)
632 set_error( STATUS_NO_MEMORY );
633 return NULL;
636 if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
637 return NULL;
639 screen_buffer->id = ++console->last_id;
640 screen_buffer->input = console;
641 init_async_queue( &screen_buffer->ioctl_q );
642 list_add_head( &screen_buffer_list, &screen_buffer->entry );
644 screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
645 FILE_SYNCHRONOUS_IO_NONALERT );
646 if (!screen_buffer->fd)
648 release_object( screen_buffer );
649 return NULL;
651 allow_fd_caching(screen_buffer->fd);
653 if (console->server) queue_host_ioctl( console->server, IOCTL_CONDRV_INIT_OUTPUT,
654 screen_buffer->id, NULL, NULL );
655 if (!console->active) set_active_screen_buffer( console, screen_buffer );
656 return &screen_buffer->obj;
659 struct thread *console_get_renderer( struct console *console )
661 return console->renderer;
664 struct console_signal_info
666 struct console *console;
667 process_id_t group;
668 int signal;
671 static int propagate_console_signal_cb(struct process *process, void *user)
673 struct console_signal_info* csi = (struct console_signal_info*)user;
675 if (process->console == csi->console && (!csi->group || process->group_id == csi->group))
677 /* find a suitable thread to signal */
678 struct thread *thread;
679 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
681 if (send_thread_signal( thread, csi->signal )) break;
684 return FALSE;
687 static void propagate_console_signal( struct console *console,
688 int sig, process_id_t group_id )
690 struct console_signal_info csi;
692 if (!console)
694 set_error( STATUS_INVALID_PARAMETER );
695 return;
697 /* FIXME: should support the other events (like CTRL_BREAK) */
698 if (sig != CTRL_C_EVENT)
700 set_error( STATUS_NOT_IMPLEMENTED );
701 return;
703 csi.console = console;
704 csi.signal = SIGINT;
705 csi.group = group_id;
707 enum_processes(propagate_console_signal_cb, &csi);
710 struct console_process_list
712 unsigned int size;
713 unsigned int count;
714 process_id_t *processes;
715 struct console *console;
718 static int console_process_list_cb(struct process *process, void *user)
720 struct console_process_list *cpl = user;
722 if (process->console == cpl->console)
724 if (cpl->count < cpl->size) cpl->processes[cpl->count] = process->id;
725 cpl->count++;
728 return 0;
731 /* dumb dump */
732 static void console_dump( struct object *obj, int verbose )
734 struct console *console = (struct console *)obj;
735 assert( obj->ops == &console_ops );
736 fprintf( stderr, "Console input active=%p server=%p\n",
737 console->active, console->server );
740 static void console_destroy( struct object *obj )
742 struct console *console = (struct console *)obj;
743 struct screen_buffer *curr;
745 assert( obj->ops == &console_ops );
747 if (console->server)
749 assert( console->server->console == console );
750 disconnect_console_server( console->server );
753 if (console->active) release_object( console->active );
754 console->active = NULL;
756 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
758 if (curr->input == console) curr->input = NULL;
761 free_async_queue( &console->ioctl_q );
762 free_async_queue( &console->read_q );
763 if (console->fd)
764 release_object( console->fd );
767 static struct object *create_console_connection( struct console *console )
769 struct console_connection *connection;
771 if (current->process->console)
773 set_error( STATUS_ACCESS_DENIED );
774 return NULL;
777 if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
778 if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
780 release_object( connection );
781 return NULL;
784 if (console)
785 current->process->console = (struct console *)grab_object( console );
787 return &connection->obj;
790 static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
791 unsigned int attr, struct object *root )
793 struct console *console = (struct console *)obj;
794 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
795 assert( obj->ops == &console_ops );
797 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
799 name->len = 0;
800 return create_console_connection( console );
803 return NULL;
806 static struct object *console_open_file( struct object *obj, unsigned int access,
807 unsigned int sharing, unsigned int options )
809 return grab_object( obj );
812 static void screen_buffer_dump( struct object *obj, int verbose )
814 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
815 assert( obj->ops == &screen_buffer_ops );
817 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
820 static void screen_buffer_destroy( struct object *obj )
822 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
824 assert( obj->ops == &screen_buffer_ops );
826 list_remove( &screen_buffer->entry );
827 if (screen_buffer->input && screen_buffer->input->server)
828 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
829 screen_buffer->id, NULL, NULL );
830 if (screen_buffer->fd) release_object( screen_buffer->fd );
831 free_async_queue( &screen_buffer->ioctl_q );
834 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
835 unsigned int sharing, unsigned int options )
837 return grab_object( obj );
840 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
842 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
843 if (!screen_buffer->input)
845 set_error( STATUS_ACCESS_DENIED );
846 return 0;
848 return console_add_queue( &screen_buffer->input->obj, entry );
851 static struct fd *screen_buffer_get_fd( struct object *obj )
853 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
854 assert( obj->ops == &screen_buffer_ops );
855 if (screen_buffer->fd)
856 return (struct fd*)grab_object( screen_buffer->fd );
857 set_error( STATUS_OBJECT_TYPE_MISMATCH );
858 return NULL;
861 static void console_server_dump( struct object *obj, int verbose )
863 assert( obj->ops == &console_server_ops );
864 fprintf( stderr, "Console server\n" );
867 static void console_server_destroy( struct object *obj )
869 struct console_server *server = (struct console_server *)obj;
870 assert( obj->ops == &console_server_ops );
871 disconnect_console_server( server );
872 if (server->fd) release_object( server->fd );
875 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
876 unsigned int attr, struct object *root )
878 struct console_server *server = (struct console_server*)obj;
879 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
880 assert( obj->ops == &console_server_ops );
882 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
884 struct screen_buffer *screen_buffer;
885 name->len = 0;
886 if (server->console)
888 set_error( STATUS_INVALID_HANDLE );
889 return 0;
891 if (!(server->console = (struct console *)create_console())) return NULL;
892 if (!(screen_buffer = (struct screen_buffer *)create_screen_buffer( server->console )))
894 release_object( server->console );
895 server->console = NULL;
896 return NULL;
898 release_object( screen_buffer );
899 server->console->server = server;
901 return &server->console->obj;
904 return NULL;
907 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
909 struct console_server *server = (struct console_server*)obj;
910 assert( obj->ops == &console_server_ops );
911 return !server->console || !list_empty( &server->queue );
914 static struct fd *console_server_get_fd( struct object* obj )
916 struct console_server *server = (struct console_server*)obj;
917 assert( obj->ops == &console_server_ops );
918 return (struct fd *)grab_object( server->fd );
921 static struct object *console_server_open_file( struct object *obj, unsigned int access,
922 unsigned int sharing, unsigned int options )
924 return grab_object( obj );
927 static struct object *create_console_server( void )
929 struct console_server *server;
931 if (!(server = alloc_object( &console_server_ops ))) return NULL;
932 server->console = NULL;
933 server->busy = 0;
934 server->once_input = 0;
935 server->term_fd = -1;
936 list_init( &server->queue );
937 list_init( &server->read_queue );
938 server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
939 if (!server->fd)
941 release_object( server );
942 return NULL;
944 allow_fd_caching(server->fd);
946 return &server->obj;
949 static int is_blocking_read_ioctl( unsigned int code )
951 switch (code)
953 case IOCTL_CONDRV_READ_INPUT:
954 case IOCTL_CONDRV_READ_CONSOLE:
955 case IOCTL_CONDRV_READ_CONSOLE_CONTROL:
956 case IOCTL_CONDRV_READ_FILE:
957 return 1;
958 default:
959 return 0;
963 static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
965 struct console *console = get_fd_user( fd );
967 switch (code)
969 case IOCTL_CONDRV_CTRL_EVENT:
971 const struct condrv_ctrl_event *event = get_req_data();
972 process_id_t group;
973 if (get_req_data_size() != sizeof(*event))
975 set_error( STATUS_INVALID_PARAMETER );
976 return;
978 group = event->group_id ? event->group_id : current->process->group_id;
979 if (!group)
981 set_error( STATUS_INVALID_PARAMETER );
982 return;
984 propagate_console_signal( console, event->event, group );
985 return;
988 case IOCTL_CONDRV_GET_PROCESS_LIST:
990 struct console_process_list cpl;
991 if (get_reply_max_size() < sizeof(unsigned int))
993 set_error( STATUS_INVALID_PARAMETER );
994 return;
997 cpl.count = 0;
998 cpl.size = 0;
999 cpl.console = console;
1000 enum_processes( console_process_list_cb, &cpl );
1001 if (cpl.count * sizeof(process_id_t) > get_reply_max_size())
1003 set_reply_data( &cpl.count, sizeof(cpl.count) );
1004 set_error( STATUS_BUFFER_TOO_SMALL );
1005 return;
1008 cpl.size = cpl.count;
1009 cpl.count = 0;
1010 if ((cpl.processes = set_reply_data_size( cpl.size * sizeof(process_id_t) )))
1011 enum_processes( console_process_list_cb, &cpl );
1012 return;
1015 default:
1016 if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
1018 set_error( STATUS_INVALID_HANDLE );
1019 return;
1021 queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1025 static void console_read( struct fd *fd, struct async *async, file_pos_t pos )
1027 struct console *console = get_fd_user( fd );
1029 if (!console->server)
1031 set_error( STATUS_INVALID_HANDLE );
1032 return;
1034 queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
1037 static void console_flush( struct fd *fd, struct async *async )
1039 struct console *console = get_fd_user( fd );
1041 if (!console->server)
1043 set_error( STATUS_INVALID_HANDLE );
1044 return;
1046 queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
1049 static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
1051 struct screen_buffer *screen_buffer = get_fd_user( fd );
1053 if (!screen_buffer->input || !screen_buffer->input->server)
1055 set_error( STATUS_INVALID_HANDLE );
1056 return;
1059 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
1060 screen_buffer->id, async, &screen_buffer->ioctl_q );
1063 static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1065 struct screen_buffer *screen_buffer = get_fd_user( fd );
1067 switch (code)
1069 case IOCTL_CONDRV_ACTIVATE:
1070 if (!screen_buffer->input)
1072 set_error( STATUS_INVALID_HANDLE );
1073 return;
1076 set_active_screen_buffer( screen_buffer->input, screen_buffer );
1077 return;
1079 default:
1080 if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
1081 is_blocking_read_ioctl( code ))
1083 set_error( STATUS_INVALID_HANDLE );
1084 return;
1086 queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
1087 async, &screen_buffer->ioctl_q );
1091 static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1093 struct console_connection *console_connection = get_fd_user( fd );
1095 switch (code)
1097 case IOCTL_CONDRV_BIND_PID:
1099 struct process *process;
1100 unsigned int pid;
1101 if (get_req_data_size() != sizeof(unsigned int))
1103 set_error( STATUS_INVALID_PARAMETER );
1104 return;
1106 if (current->process->console)
1108 set_error( STATUS_INVALID_HANDLE );
1109 return;
1112 pid = *(unsigned int *)get_req_data();
1113 if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
1114 if (!(process = get_process_from_id( pid ))) return;
1116 if (process->console)
1117 current->process->console = (struct console *)grab_object( process->console );
1118 else set_error( STATUS_ACCESS_DENIED );
1119 release_object( process );
1120 return;
1123 default:
1124 default_fd_ioctl( console_connection->fd, code, async );
1128 static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1130 struct console_server *server = get_fd_user( fd );
1132 switch (code)
1134 case IOCTL_CONDRV_CTRL_EVENT:
1136 const struct condrv_ctrl_event *event = get_req_data();
1137 if (get_req_data_size() != sizeof(*event))
1139 set_error( STATUS_INVALID_PARAMETER );
1140 return;
1142 if (!server->console)
1144 set_error( STATUS_INVALID_HANDLE );
1145 return;
1147 propagate_console_signal( server->console, event->event, event->group_id );
1148 return;
1151 case IOCTL_CONDRV_SETUP_INPUT:
1153 struct termios term;
1154 obj_handle_t handle;
1155 struct file *file;
1156 int unix_fd;
1158 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1160 set_error( STATUS_INVALID_PARAMETER );
1161 return;
1163 if (server->term_fd != -1)
1165 tcsetattr( server->term_fd, TCSANOW, &server->termios );
1166 close( server->term_fd );
1167 server->term_fd = -1;
1169 handle = *(unsigned int *)get_req_data();
1170 if (!handle) return;
1171 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
1173 return;
1175 unix_fd = get_file_unix_fd( file );
1176 release_object( file );
1178 if (tcgetattr( unix_fd, &server->termios ))
1180 file_set_error();
1181 return;
1183 term = server->termios;
1184 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
1185 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
1186 term.c_cflag &= ~(CSIZE | PARENB);
1187 term.c_cflag |= CS8;
1188 term.c_cc[VMIN] = 1;
1189 term.c_cc[VTIME] = 0;
1190 if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
1191 file_set_error();
1192 return;
1195 default:
1196 set_error( STATUS_INVALID_HANDLE );
1197 return;
1201 static void console_connection_dump( struct object *obj, int verbose )
1203 fputs( "console connection\n", stderr );
1206 static struct fd *console_connection_get_fd( struct object *obj )
1208 struct console_connection *connection = (struct console_connection *)obj;
1209 return (struct fd *)grab_object( connection->fd );
1212 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
1213 unsigned int attr, struct object *root )
1215 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1217 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1219 if (!current->process->console)
1221 set_error( STATUS_INVALID_HANDLE );
1222 return NULL;
1224 name->len = 0;
1225 return grab_object( current->process->console );
1228 return NULL;
1231 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
1232 unsigned int sharing, unsigned int options )
1234 return grab_object( obj );
1237 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1239 struct console *console = process->console;
1241 if (console)
1243 process->console = NULL;
1244 release_object( console );
1246 return 1;
1249 static void console_connection_destroy( struct object *obj )
1251 struct console_connection *connection = (struct console_connection *)obj;
1252 if (connection->fd) release_object( connection->fd );
1255 static void console_device_dump( struct object *obj, int verbose )
1257 fputs( "Console device\n", stderr );
1260 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
1261 unsigned int attr, struct object *root )
1263 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
1264 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1265 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1266 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1267 static const WCHAR inputW[] = {'I','n','p','u','t'};
1268 static const WCHAR outputW[] = {'O','u','t','p','u','t'};
1269 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1270 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
1272 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1274 if (!current->process->console)
1276 set_error( STATUS_INVALID_HANDLE );
1277 return NULL;
1279 name->len = 0;
1280 return grab_object( current->process->console );
1283 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1285 if (!current->process->console || !current->process->console->active)
1287 set_error( STATUS_INVALID_HANDLE );
1288 return NULL;
1290 name->len = 0;
1291 return grab_object( current->process->console->active );
1294 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1296 name->len = 0;
1297 return grab_object( obj );
1300 if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
1302 struct console_input *console_input;
1303 name->len = 0;
1304 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
1305 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
1306 FILE_SYNCHRONOUS_IO_NONALERT );
1307 if (!console_input->fd)
1309 release_object( console_input );
1310 return NULL;
1312 return &console_input->obj;
1315 if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
1317 struct console_output *console_output;
1318 name->len = 0;
1319 if (!(console_output = alloc_object( &console_output_ops ))) return NULL;
1320 console_output->fd = alloc_pseudo_fd( &console_output_fd_ops, &console_output->obj,
1321 FILE_SYNCHRONOUS_IO_NONALERT );
1322 if (!console_output->fd)
1324 release_object( console_output );
1325 return NULL;
1327 return &console_output->obj;
1330 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1332 if (!current->process->console)
1334 set_error( STATUS_INVALID_HANDLE );
1335 return NULL;
1337 name->len = 0;
1338 return create_screen_buffer( current->process->console );
1341 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
1343 name->len = 0;
1344 return create_console_server();
1347 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
1349 name->len = 0;
1350 return create_console_connection( NULL );
1353 return NULL;
1356 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1357 unsigned int sharing, unsigned int options )
1359 int is_output;
1360 access = default_map_access( obj, access );
1361 is_output = access & FILE_WRITE_DATA;
1362 if (!current->process->console || (is_output && !current->process->console))
1364 set_error( STATUS_INVALID_HANDLE );
1365 return NULL;
1367 if (is_output && (access & FILE_READ_DATA))
1369 set_error( STATUS_INVALID_PARAMETER );
1370 return NULL;
1372 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1375 static void console_input_dump( struct object *obj, int verbose )
1377 fputs( "console Input device\n", stderr );
1380 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
1382 if (!current->process->console)
1384 set_error( STATUS_ACCESS_DENIED );
1385 return 0;
1387 return console_add_queue( &current->process->console->obj, entry );
1390 static struct fd *console_input_get_fd( struct object *obj )
1392 struct console_input *console_input = (struct console_input *)obj;
1393 assert( obj->ops == &console_input_ops );
1394 return (struct fd *)grab_object( console_input->fd );
1397 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1398 unsigned int sharing, unsigned int options )
1400 return grab_object( obj );
1403 static void console_input_destroy( struct object *obj )
1405 struct console_input *console_input = (struct console_input *)obj;
1407 assert( obj->ops == &console_input_ops );
1408 if (console_input->fd) release_object( console_input->fd );
1411 static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1413 struct console *console = current->process->console;
1415 if (!console)
1417 set_error( STATUS_INVALID_HANDLE );
1418 return;
1420 console_ioctl( console->fd, code, async );
1423 static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
1425 struct console *console = current->process->console;
1427 if (!console)
1429 set_error( STATUS_INVALID_HANDLE );
1430 return;
1432 console_read( console->fd, async, pos );
1435 static void console_input_flush( struct fd *fd, struct async *async )
1437 struct console *console = current->process->console;
1439 if (!console)
1441 set_error( STATUS_INVALID_HANDLE );
1442 return;
1444 console_flush( console->fd, async );
1447 static void console_output_dump( struct object *obj, int verbose )
1449 fputs( "console Output device\n", stderr );
1452 static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry )
1454 if (!current->process->console || !current->process->console->active)
1456 set_error( STATUS_ACCESS_DENIED );
1457 return 0;
1459 return console_add_queue( &current->process->console->obj, entry );
1462 static struct fd *console_output_get_fd( struct object *obj )
1464 struct console_output *console_output = (struct console_output *)obj;
1465 assert( obj->ops == &console_output_ops );
1466 return (struct fd *)grab_object( console_output->fd );
1469 static struct object *console_output_open_file( struct object *obj, unsigned int access,
1470 unsigned int sharing, unsigned int options )
1472 return grab_object( obj );
1475 static void console_output_destroy( struct object *obj )
1477 struct console_output *console_output = (struct console_output *)obj;
1479 assert( obj->ops == &console_output_ops );
1480 if (console_output->fd) release_object( console_output->fd );
1483 static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1485 struct console *console = current->process->console;
1487 if (!console || !console->active)
1489 set_error( STATUS_INVALID_HANDLE );
1490 return;
1492 screen_buffer_ioctl( console->active->fd, code, async );
1495 static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos )
1497 struct console *console = current->process->console;
1499 if (!console || !console->active)
1501 set_error( STATUS_INVALID_HANDLE );
1502 return;
1504 screen_buffer_write( console->active->fd, async, pos );
1507 struct object *create_console_device( struct object *root, const struct unicode_str *name,
1508 unsigned int attr, const struct security_descriptor *sd )
1510 return create_named_object( root, &console_device_ops, name, attr, sd );
1513 /* retrieve the next pending console ioctl request */
1514 DECL_HANDLER(get_next_console_request)
1516 struct console_host_ioctl *ioctl = NULL, *next;
1517 struct console_server *server;
1518 struct iosb *iosb = NULL;
1520 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
1521 if (!server) return;
1523 if (!server->console)
1525 set_error( STATUS_INVALID_HANDLE );
1526 release_object( server );
1527 return;
1530 if (!server->console->renderer) server->console->renderer = current;
1532 if (!req->signal) server->console->signaled = 0;
1533 else if (!server->console->signaled)
1535 server->console->signaled = 1;
1536 wake_up( &server->console->obj, 0 );
1539 if (req->read)
1541 /* set result of current pending ioctl */
1542 if (list_empty( &server->read_queue ))
1544 set_error( STATUS_INVALID_HANDLE );
1545 release_object( server );
1546 return;
1549 ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
1550 list_remove( &ioctl->entry );
1551 list_move_tail( &server->queue, &server->read_queue );
1553 else if (server->busy)
1555 /* set result of previous ioctl */
1556 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1557 list_remove( &ioctl->entry );
1560 if (ioctl)
1562 struct async *async = ioctl->async;
1563 unsigned int status = req->status;
1565 if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
1566 if (async)
1568 iosb = async_get_iosb( async );
1569 if (iosb->status == STATUS_PENDING)
1571 data_size_t out_size = min( iosb->out_size, get_req_data_size() );
1572 data_size_t result = ioctl->code == IOCTL_CONDRV_WRITE_FILE ? iosb->in_size : out_size;
1573 async_request_complete_alloc( async, status, result, out_size, get_req_data() );
1576 release_object( async );
1578 free( ioctl );
1579 if (iosb) release_object( iosb );
1581 if (req->read)
1583 release_object( server );
1584 return;
1586 server->busy = 0;
1589 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1590 * move it to read queue for execution after current read is complete. move all blocking
1591 * ioctl at the same time to preserve their order. */
1592 if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
1594 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1595 if (is_blocking_read_ioctl( ioctl->code ))
1597 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
1599 if (!is_blocking_read_ioctl( ioctl->code )) continue;
1600 list_remove( &ioctl->entry );
1601 list_add_tail( &server->read_queue, &ioctl->entry );
1606 /* return the next ioctl */
1607 if (!list_empty( &server->queue ))
1609 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1610 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
1612 if (!iosb || get_reply_max_size() >= iosb->in_size)
1614 reply->code = ioctl->code;
1615 reply->output = ioctl->output;
1617 if (iosb)
1619 reply->out_size = iosb->out_size;
1620 set_reply_data_ptr( iosb->in_data, iosb->in_size );
1621 iosb->in_data = NULL;
1624 if (is_blocking_read_ioctl( ioctl->code ))
1626 list_remove( &ioctl->entry );
1627 assert( list_empty( &server->read_queue ));
1628 list_add_tail( &server->read_queue, &ioctl->entry );
1630 else server->busy = 1;
1632 else
1634 reply->out_size = iosb->in_size;
1635 set_error( STATUS_BUFFER_OVERFLOW );
1637 if (iosb) release_object( iosb );
1639 else
1641 set_error( STATUS_PENDING );
1644 release_object( server );