wined3d: Don't validate the frontbuffer's DRAWABLE location in wined3d_swapchain_resi...
[wine.git] / server / console.c
blobb64283baf4adc05ca87129a19b7f61c8619128d5
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 switch (sig)
699 case CTRL_C_EVENT: csi.signal = SIGINT; break;
700 case CTRL_BREAK_EVENT: csi.signal = SIGQUIT; break;
701 default:
702 /* FIXME: should support the other events */
703 set_error( STATUS_NOT_IMPLEMENTED );
704 return;
706 csi.console = console;
707 csi.group = group_id;
709 enum_processes(propagate_console_signal_cb, &csi);
712 struct console_process_list
714 unsigned int size;
715 unsigned int count;
716 process_id_t *processes;
717 struct console *console;
720 static int console_process_list_cb(struct process *process, void *user)
722 struct console_process_list *cpl = user;
724 if (process->console == cpl->console)
726 if (cpl->count < cpl->size) cpl->processes[cpl->count] = process->id;
727 cpl->count++;
730 return 0;
733 /* dumb dump */
734 static void console_dump( struct object *obj, int verbose )
736 struct console *console = (struct console *)obj;
737 assert( obj->ops == &console_ops );
738 fprintf( stderr, "Console input active=%p server=%p\n",
739 console->active, console->server );
742 static void console_destroy( struct object *obj )
744 struct console *console = (struct console *)obj;
745 struct screen_buffer *curr;
747 assert( obj->ops == &console_ops );
749 if (console->server)
751 assert( console->server->console == console );
752 disconnect_console_server( console->server );
755 if (console->active) release_object( console->active );
756 console->active = NULL;
758 LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
760 if (curr->input == console) curr->input = NULL;
763 free_async_queue( &console->ioctl_q );
764 free_async_queue( &console->read_q );
765 if (console->fd)
766 release_object( console->fd );
769 static struct object *create_console_connection( struct console *console )
771 struct console_connection *connection;
773 if (current->process->console)
775 set_error( STATUS_ACCESS_DENIED );
776 return NULL;
779 if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
780 if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
782 release_object( connection );
783 return NULL;
786 if (console)
787 current->process->console = (struct console *)grab_object( console );
789 return &connection->obj;
792 static struct object *console_lookup_name( struct object *obj, struct unicode_str *name,
793 unsigned int attr, struct object *root )
795 struct console *console = (struct console *)obj;
796 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
797 assert( obj->ops == &console_ops );
799 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
801 name->len = 0;
802 return create_console_connection( console );
805 return NULL;
808 static struct object *console_open_file( struct object *obj, unsigned int access,
809 unsigned int sharing, unsigned int options )
811 return grab_object( obj );
814 static void screen_buffer_dump( struct object *obj, int verbose )
816 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
817 assert( obj->ops == &screen_buffer_ops );
819 fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
822 static void screen_buffer_destroy( struct object *obj )
824 struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
826 assert( obj->ops == &screen_buffer_ops );
828 list_remove( &screen_buffer->entry );
829 if (screen_buffer->input && screen_buffer->input->server)
830 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
831 screen_buffer->id, NULL, NULL );
832 if (screen_buffer->fd) release_object( screen_buffer->fd );
833 free_async_queue( &screen_buffer->ioctl_q );
836 static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
837 unsigned int sharing, unsigned int options )
839 return grab_object( obj );
842 static int screen_buffer_add_queue( struct object *obj, struct wait_queue_entry *entry )
844 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
845 if (!screen_buffer->input)
847 set_error( STATUS_ACCESS_DENIED );
848 return 0;
850 return console_add_queue( &screen_buffer->input->obj, entry );
853 static struct fd *screen_buffer_get_fd( struct object *obj )
855 struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
856 assert( obj->ops == &screen_buffer_ops );
857 if (screen_buffer->fd)
858 return (struct fd*)grab_object( screen_buffer->fd );
859 set_error( STATUS_OBJECT_TYPE_MISMATCH );
860 return NULL;
863 static void console_server_dump( struct object *obj, int verbose )
865 assert( obj->ops == &console_server_ops );
866 fprintf( stderr, "Console server\n" );
869 static void console_server_destroy( struct object *obj )
871 struct console_server *server = (struct console_server *)obj;
872 assert( obj->ops == &console_server_ops );
873 disconnect_console_server( server );
874 if (server->fd) release_object( server->fd );
877 static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name,
878 unsigned int attr, struct object *root )
880 struct console_server *server = (struct console_server*)obj;
881 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
882 assert( obj->ops == &console_server_ops );
884 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
886 struct screen_buffer *screen_buffer;
887 name->len = 0;
888 if (server->console)
890 set_error( STATUS_INVALID_HANDLE );
891 return 0;
893 if (!(server->console = (struct console *)create_console())) return NULL;
894 if (!(screen_buffer = (struct screen_buffer *)create_screen_buffer( server->console )))
896 release_object( server->console );
897 server->console = NULL;
898 return NULL;
900 release_object( screen_buffer );
901 server->console->server = server;
903 return &server->console->obj;
906 return NULL;
909 static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
911 struct console_server *server = (struct console_server*)obj;
912 assert( obj->ops == &console_server_ops );
913 return !server->console || !list_empty( &server->queue );
916 static struct fd *console_server_get_fd( struct object* obj )
918 struct console_server *server = (struct console_server*)obj;
919 assert( obj->ops == &console_server_ops );
920 return (struct fd *)grab_object( server->fd );
923 static struct object *console_server_open_file( struct object *obj, unsigned int access,
924 unsigned int sharing, unsigned int options )
926 return grab_object( obj );
929 static struct object *create_console_server( void )
931 struct console_server *server;
933 if (!(server = alloc_object( &console_server_ops ))) return NULL;
934 server->console = NULL;
935 server->busy = 0;
936 server->once_input = 0;
937 server->term_fd = -1;
938 list_init( &server->queue );
939 list_init( &server->read_queue );
940 server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
941 if (!server->fd)
943 release_object( server );
944 return NULL;
946 allow_fd_caching(server->fd);
948 return &server->obj;
951 static int is_blocking_read_ioctl( unsigned int code )
953 switch (code)
955 case IOCTL_CONDRV_READ_INPUT:
956 case IOCTL_CONDRV_READ_CONSOLE:
957 case IOCTL_CONDRV_READ_CONSOLE_CONTROL:
958 case IOCTL_CONDRV_READ_FILE:
959 return 1;
960 default:
961 return 0;
965 static void console_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
967 struct console *console = get_fd_user( fd );
969 switch (code)
971 case IOCTL_CONDRV_CTRL_EVENT:
973 const struct condrv_ctrl_event *event = get_req_data();
974 process_id_t group;
975 if (get_req_data_size() != sizeof(*event))
977 set_error( STATUS_INVALID_PARAMETER );
978 return;
980 group = event->group_id ? event->group_id : current->process->group_id;
981 if (!group)
983 set_error( STATUS_INVALID_PARAMETER );
984 return;
986 propagate_console_signal( console, event->event, group );
987 return;
990 case IOCTL_CONDRV_GET_PROCESS_LIST:
992 struct console_process_list cpl;
993 if (get_reply_max_size() < sizeof(unsigned int))
995 set_error( STATUS_INVALID_PARAMETER );
996 return;
999 cpl.count = 0;
1000 cpl.size = 0;
1001 cpl.console = console;
1002 enum_processes( console_process_list_cb, &cpl );
1003 if (cpl.count * sizeof(process_id_t) > get_reply_max_size())
1005 set_reply_data( &cpl.count, sizeof(cpl.count) );
1006 set_error( STATUS_BUFFER_TOO_SMALL );
1007 return;
1010 cpl.size = cpl.count;
1011 cpl.count = 0;
1012 if ((cpl.processes = set_reply_data_size( cpl.size * sizeof(process_id_t) )))
1013 enum_processes( console_process_list_cb, &cpl );
1014 return;
1017 default:
1018 if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
1020 set_error( STATUS_INVALID_HANDLE );
1021 return;
1023 queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
1027 static void console_read( struct fd *fd, struct async *async, file_pos_t pos )
1029 struct console *console = get_fd_user( fd );
1031 if (!console->server)
1033 set_error( STATUS_INVALID_HANDLE );
1034 return;
1036 queue_host_ioctl( console->server, IOCTL_CONDRV_READ_FILE, 0, async, &console->ioctl_q );
1039 static void console_flush( struct fd *fd, struct async *async )
1041 struct console *console = get_fd_user( fd );
1043 if (!console->server)
1045 set_error( STATUS_INVALID_HANDLE );
1046 return;
1048 queue_host_ioctl( console->server, IOCTL_CONDRV_FLUSH, 0, NULL, NULL );
1051 static void screen_buffer_write( struct fd *fd, struct async *async, file_pos_t pos )
1053 struct screen_buffer *screen_buffer = get_fd_user( fd );
1055 if (!screen_buffer->input || !screen_buffer->input->server)
1057 set_error( STATUS_INVALID_HANDLE );
1058 return;
1061 queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_WRITE_FILE,
1062 screen_buffer->id, async, &screen_buffer->ioctl_q );
1065 static void screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1067 struct screen_buffer *screen_buffer = get_fd_user( fd );
1069 switch (code)
1071 case IOCTL_CONDRV_ACTIVATE:
1072 if (!screen_buffer->input)
1074 set_error( STATUS_INVALID_HANDLE );
1075 return;
1078 set_active_screen_buffer( screen_buffer->input, screen_buffer );
1079 return;
1081 default:
1082 if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE ||
1083 is_blocking_read_ioctl( code ))
1085 set_error( STATUS_INVALID_HANDLE );
1086 return;
1088 queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
1089 async, &screen_buffer->ioctl_q );
1093 static void console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1095 struct console_connection *console_connection = get_fd_user( fd );
1097 switch (code)
1099 case IOCTL_CONDRV_BIND_PID:
1101 struct process *process;
1102 unsigned int pid;
1103 if (get_req_data_size() != sizeof(unsigned int))
1105 set_error( STATUS_INVALID_PARAMETER );
1106 return;
1108 if (current->process->console)
1110 set_error( STATUS_INVALID_HANDLE );
1111 return;
1114 pid = *(unsigned int *)get_req_data();
1115 if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
1116 if (!(process = get_process_from_id( pid ))) return;
1118 if (process->console)
1119 current->process->console = (struct console *)grab_object( process->console );
1120 else set_error( STATUS_ACCESS_DENIED );
1121 release_object( process );
1122 return;
1125 default:
1126 default_fd_ioctl( console_connection->fd, code, async );
1130 static void console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1132 struct console_server *server = get_fd_user( fd );
1134 switch (code)
1136 case IOCTL_CONDRV_CTRL_EVENT:
1138 const struct condrv_ctrl_event *event = get_req_data();
1139 if (get_req_data_size() != sizeof(*event))
1141 set_error( STATUS_INVALID_PARAMETER );
1142 return;
1144 if (!server->console)
1146 set_error( STATUS_INVALID_HANDLE );
1147 return;
1149 propagate_console_signal( server->console, event->event, event->group_id );
1150 return;
1153 case IOCTL_CONDRV_SETUP_INPUT:
1155 struct termios term;
1156 obj_handle_t handle;
1157 struct file *file;
1158 int unix_fd;
1160 if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
1162 set_error( STATUS_INVALID_PARAMETER );
1163 return;
1165 if (server->term_fd != -1)
1167 tcsetattr( server->term_fd, TCSANOW, &server->termios );
1168 close( server->term_fd );
1169 server->term_fd = -1;
1171 handle = *(unsigned int *)get_req_data();
1172 if (!handle) return;
1173 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
1175 return;
1177 unix_fd = get_file_unix_fd( file );
1178 release_object( file );
1180 if (tcgetattr( unix_fd, &server->termios ))
1182 file_set_error();
1183 return;
1185 term = server->termios;
1186 term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
1187 term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
1188 term.c_cflag &= ~(CSIZE | PARENB);
1189 term.c_cflag |= CS8;
1190 term.c_cc[VMIN] = 1;
1191 term.c_cc[VTIME] = 0;
1192 if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
1193 file_set_error();
1194 return;
1197 default:
1198 set_error( STATUS_INVALID_HANDLE );
1199 return;
1203 static void console_connection_dump( struct object *obj, int verbose )
1205 fputs( "console connection\n", stderr );
1208 static struct fd *console_connection_get_fd( struct object *obj )
1210 struct console_connection *connection = (struct console_connection *)obj;
1211 return (struct fd *)grab_object( connection->fd );
1214 static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name,
1215 unsigned int attr, struct object *root )
1217 static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
1219 if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
1221 if (!current->process->console)
1223 set_error( STATUS_INVALID_HANDLE );
1224 return NULL;
1226 name->len = 0;
1227 return grab_object( current->process->console );
1230 return NULL;
1233 static struct object *console_connection_open_file( struct object *obj, unsigned int access,
1234 unsigned int sharing, unsigned int options )
1236 return grab_object( obj );
1239 static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1241 struct console *console = process->console;
1243 if (console)
1245 process->console = NULL;
1246 release_object( console );
1248 return 1;
1251 static void console_connection_destroy( struct object *obj )
1253 struct console_connection *connection = (struct console_connection *)obj;
1254 if (connection->fd) release_object( connection->fd );
1257 static void console_device_dump( struct object *obj, int verbose )
1259 fputs( "Console device\n", stderr );
1262 static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name,
1263 unsigned int attr, struct object *root )
1265 static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
1266 static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
1267 static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
1268 static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
1269 static const WCHAR inputW[] = {'I','n','p','u','t'};
1270 static const WCHAR outputW[] = {'O','u','t','p','u','t'};
1271 static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
1272 static const WCHAR serverW[] = {'S','e','r','v','e','r'};
1274 if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
1276 if (!current->process->console)
1278 set_error( STATUS_INVALID_HANDLE );
1279 return NULL;
1281 name->len = 0;
1282 return grab_object( current->process->console );
1285 if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
1287 if (!current->process->console || !current->process->console->active)
1289 set_error( STATUS_INVALID_HANDLE );
1290 return NULL;
1292 name->len = 0;
1293 return grab_object( current->process->console->active );
1296 if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
1298 name->len = 0;
1299 return grab_object( obj );
1302 if (name->len == sizeof(inputW) && !memcmp( name->str, inputW, name->len ))
1304 struct console_input *console_input;
1305 name->len = 0;
1306 if (!(console_input = alloc_object( &console_input_ops ))) return NULL;
1307 console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
1308 FILE_SYNCHRONOUS_IO_NONALERT );
1309 if (!console_input->fd)
1311 release_object( console_input );
1312 return NULL;
1314 return &console_input->obj;
1317 if (name->len == sizeof(outputW) && !memcmp( name->str, outputW, name->len ))
1319 struct console_output *console_output;
1320 name->len = 0;
1321 if (!(console_output = alloc_object( &console_output_ops ))) return NULL;
1322 console_output->fd = alloc_pseudo_fd( &console_output_fd_ops, &console_output->obj,
1323 FILE_SYNCHRONOUS_IO_NONALERT );
1324 if (!console_output->fd)
1326 release_object( console_output );
1327 return NULL;
1329 return &console_output->obj;
1332 if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
1334 if (!current->process->console)
1336 set_error( STATUS_INVALID_HANDLE );
1337 return NULL;
1339 name->len = 0;
1340 return create_screen_buffer( current->process->console );
1343 if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
1345 name->len = 0;
1346 return create_console_server();
1349 if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
1351 name->len = 0;
1352 return create_console_connection( NULL );
1355 return NULL;
1358 static struct object *console_device_open_file( struct object *obj, unsigned int access,
1359 unsigned int sharing, unsigned int options )
1361 int is_output;
1362 access = default_map_access( obj, access );
1363 is_output = access & FILE_WRITE_DATA;
1364 if (!current->process->console || (is_output && !current->process->console))
1366 set_error( STATUS_INVALID_HANDLE );
1367 return NULL;
1369 if (is_output && (access & FILE_READ_DATA))
1371 set_error( STATUS_INVALID_PARAMETER );
1372 return NULL;
1374 return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
1377 static void console_input_dump( struct object *obj, int verbose )
1379 fputs( "console Input device\n", stderr );
1382 static int console_input_add_queue( struct object *obj, struct wait_queue_entry *entry )
1384 if (!current->process->console)
1386 set_error( STATUS_ACCESS_DENIED );
1387 return 0;
1389 return console_add_queue( &current->process->console->obj, entry );
1392 static struct fd *console_input_get_fd( struct object *obj )
1394 struct console_input *console_input = (struct console_input *)obj;
1395 assert( obj->ops == &console_input_ops );
1396 return (struct fd *)grab_object( console_input->fd );
1399 static struct object *console_input_open_file( struct object *obj, unsigned int access,
1400 unsigned int sharing, unsigned int options )
1402 return grab_object( obj );
1405 static void console_input_destroy( struct object *obj )
1407 struct console_input *console_input = (struct console_input *)obj;
1409 assert( obj->ops == &console_input_ops );
1410 if (console_input->fd) release_object( console_input->fd );
1413 static void console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1415 struct console *console = current->process->console;
1417 if (!console)
1419 set_error( STATUS_INVALID_HANDLE );
1420 return;
1422 console_ioctl( console->fd, code, async );
1425 static void console_input_read( struct fd *fd, struct async *async, file_pos_t pos )
1427 struct console *console = current->process->console;
1429 if (!console)
1431 set_error( STATUS_INVALID_HANDLE );
1432 return;
1434 console_read( console->fd, async, pos );
1437 static void console_input_flush( struct fd *fd, struct async *async )
1439 struct console *console = current->process->console;
1441 if (!console)
1443 set_error( STATUS_INVALID_HANDLE );
1444 return;
1446 console_flush( console->fd, async );
1449 static void console_output_dump( struct object *obj, int verbose )
1451 fputs( "console Output device\n", stderr );
1454 static int console_output_add_queue( struct object *obj, struct wait_queue_entry *entry )
1456 if (!current->process->console || !current->process->console->active)
1458 set_error( STATUS_ACCESS_DENIED );
1459 return 0;
1461 return console_add_queue( &current->process->console->obj, entry );
1464 static struct fd *console_output_get_fd( struct object *obj )
1466 struct console_output *console_output = (struct console_output *)obj;
1467 assert( obj->ops == &console_output_ops );
1468 return (struct fd *)grab_object( console_output->fd );
1471 static struct object *console_output_open_file( struct object *obj, unsigned int access,
1472 unsigned int sharing, unsigned int options )
1474 return grab_object( obj );
1477 static void console_output_destroy( struct object *obj )
1479 struct console_output *console_output = (struct console_output *)obj;
1481 assert( obj->ops == &console_output_ops );
1482 if (console_output->fd) release_object( console_output->fd );
1485 static void console_output_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1487 struct console *console = current->process->console;
1489 if (!console || !console->active)
1491 set_error( STATUS_INVALID_HANDLE );
1492 return;
1494 screen_buffer_ioctl( console->active->fd, code, async );
1497 static void console_output_write( struct fd *fd, struct async *async, file_pos_t pos )
1499 struct console *console = current->process->console;
1501 if (!console || !console->active)
1503 set_error( STATUS_INVALID_HANDLE );
1504 return;
1506 screen_buffer_write( console->active->fd, async, pos );
1509 struct object *create_console_device( struct object *root, const struct unicode_str *name,
1510 unsigned int attr, const struct security_descriptor *sd )
1512 return create_named_object( root, &console_device_ops, name, attr, sd );
1515 /* retrieve the next pending console ioctl request */
1516 DECL_HANDLER(get_next_console_request)
1518 struct console_host_ioctl *ioctl = NULL, *next;
1519 struct console_server *server;
1520 struct iosb *iosb = NULL;
1522 server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
1523 if (!server) return;
1525 if (!server->console)
1527 set_error( STATUS_INVALID_HANDLE );
1528 release_object( server );
1529 return;
1532 if (!server->console->renderer) server->console->renderer = current;
1534 if (!req->signal) server->console->signaled = 0;
1535 else if (!server->console->signaled)
1537 server->console->signaled = 1;
1538 wake_up( &server->console->obj, 0 );
1541 if (req->read)
1543 /* set result of current pending ioctl */
1544 if (list_empty( &server->read_queue ))
1546 set_error( STATUS_INVALID_HANDLE );
1547 release_object( server );
1548 return;
1551 ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
1552 list_remove( &ioctl->entry );
1553 list_move_tail( &server->queue, &server->read_queue );
1555 else if (server->busy)
1557 /* set result of previous ioctl */
1558 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1559 list_remove( &ioctl->entry );
1562 if (ioctl)
1564 struct async *async = ioctl->async;
1565 unsigned int status = req->status;
1567 if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
1568 if (async)
1570 iosb = async_get_iosb( async );
1571 if (iosb->status == STATUS_PENDING)
1573 data_size_t out_size = min( iosb->out_size, get_req_data_size() );
1574 data_size_t result = ioctl->code == IOCTL_CONDRV_WRITE_FILE ? iosb->in_size : out_size;
1575 async_request_complete_alloc( async, status, result, out_size, get_req_data() );
1578 release_object( async );
1580 free( ioctl );
1581 if (iosb) release_object( iosb );
1583 if (req->read)
1585 release_object( server );
1586 return;
1588 server->busy = 0;
1591 /* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
1592 * move it to read queue for execution after current read is complete. move all blocking
1593 * ioctl at the same time to preserve their order. */
1594 if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
1596 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1597 if (is_blocking_read_ioctl( ioctl->code ))
1599 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
1601 if (!is_blocking_read_ioctl( ioctl->code )) continue;
1602 list_remove( &ioctl->entry );
1603 list_add_tail( &server->read_queue, &ioctl->entry );
1608 /* return the next ioctl */
1609 if (!list_empty( &server->queue ))
1611 ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
1612 iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
1614 if (!iosb || get_reply_max_size() >= iosb->in_size)
1616 reply->code = ioctl->code;
1617 reply->output = ioctl->output;
1619 if (iosb)
1621 reply->out_size = iosb->out_size;
1622 set_reply_data_ptr( iosb->in_data, iosb->in_size );
1623 iosb->in_data = NULL;
1626 if (is_blocking_read_ioctl( ioctl->code ))
1628 list_remove( &ioctl->entry );
1629 assert( list_empty( &server->read_queue ));
1630 list_add_tail( &server->read_queue, &ioctl->entry );
1632 else server->busy = 1;
1634 else
1636 reply->out_size = iosb->in_size;
1637 set_error( STATUS_BUFFER_OVERFLOW );
1639 if (iosb) release_object( iosb );
1641 else
1643 set_error( STATUS_PENDING );
1646 release_object( server );