Sweden-Number/server/console.c

1680 lines
60 KiB
C
Raw Normal View History

/*
* Server-side console management
*
* Copyright (C) 1998 Alexandre Julliard
* 2001 Eric Pouech
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
2005-11-28 17:32:54 +01:00
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "handle.h"
#include "process.h"
#include "request.h"
#include "file.h"
#include "unicode.h"
#include "wincon.h"
2005-11-28 17:32:54 +01:00
#include "winternl.h"
#include "wine/condrv.h"
struct screen_buffer;
struct console_input_events;
struct history_line
{
data_size_t len;
WCHAR text[1];
};
struct console_input
{
struct object obj; /* object header */
int num_proc; /* number of processes attached to this console */
struct thread *renderer; /* console renderer thread */
int mode; /* input mode */
struct screen_buffer *active; /* active screen buffer */
int recnum; /* number of input records */
INPUT_RECORD *records; /* input records */
struct console_input_events *evt; /* synchronization event with renderer */
struct console_server *server; /* console server object */
WCHAR *title; /* console title */
data_size_t title_len; /* length of console title */
struct history_line **history; /* lines history */
int history_size; /* number of entries in history array */
int history_index; /* number of used entries in history array */
int history_mode; /* mode of history (non zero means remove doubled strings */
int edition_mode; /* index to edition mode flavors */
int input_cp; /* console input codepage */
int output_cp; /* console output codepage */
user_handle_t win; /* window handle if backend supports it */
unsigned int last_id; /* id of last created console buffer */
struct event *event; /* event to wait on for input queue */
struct fd *fd; /* for bare console, attached input fd */
struct async_queue ioctl_q; /* ioctl queue */
struct async_queue read_q; /* read queue */
};
static void console_input_dump( struct object *obj, int verbose );
static void console_input_destroy( struct object *obj );
static struct fd *console_input_get_fd( struct object *obj );
static struct object *console_input_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
static struct object *console_input_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static const struct object_ops console_input_ops =
{
sizeof(struct console_input), /* size */
console_input_dump, /* dump */
no_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
console_input_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
console_input_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
console_input_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
console_input_destroy /* destroy */
};
static enum server_fd_type console_get_fd_type( struct fd *fd );
static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct fd_ops console_input_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
console_get_fd_type, /* get_fd_type */
no_fd_read, /* read */
no_fd_write, /* write */
no_fd_flush, /* flush */
no_fd_get_file_info, /* get_file_info */
no_fd_get_volume_info, /* get_volume_info */
console_input_ioctl, /* ioctl */
default_fd_queue_async, /* queue_async */
default_fd_reselect_async /* reselect_async */
};
static void console_input_events_dump( struct object *obj, int verbose );
static void console_input_events_destroy( struct object *obj );
static struct fd *console_input_events_get_fd( struct object *obj );
static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
2002-06-01 01:06:46 +02:00
struct console_input_events
{
struct object obj; /* object header */
struct fd *fd; /* pseudo-fd for ioctls */
struct console_input *console; /* attached console */
int num_alloc; /* number of allocated events */
int num_used; /* number of actually used events */
struct condrv_renderer_event *events;
struct async_queue read_q; /* read queue */
};
static const struct object_ops console_input_events_ops =
{
sizeof(struct console_input_events), /* size */
console_input_events_dump, /* dump */
no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
NULL, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
console_input_events_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
console_input_events_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
console_input_events_destroy /* destroy */
};
static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct fd_ops console_input_events_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
console_get_fd_type, /* get_fd_type */
no_fd_read, /* read */
no_fd_write, /* write */
no_fd_flush, /* flush */
no_fd_get_file_info, /* get_file_info */
no_fd_get_volume_info, /* get_volume_info */
console_input_events_ioctl, /* ioctl */
default_fd_queue_async, /* queue_async */
default_fd_reselect_async /* reselect_async */
};
struct console_host_ioctl
{
unsigned int code; /* ioctl code */
int output; /* output id for screen buffer ioctls */
struct async *async; /* ioctl async */
struct list entry; /* list entry */
};
struct console_server
{
struct object obj; /* object header */
struct fd *fd; /* pseudo-fd for ioctls */
struct console_input *console; /* attached console */
struct list queue; /* ioctl queue */
struct list read_queue; /* blocking read queue */
int busy; /* flag if server processing an ioctl */
int term_fd; /* UNIX terminal fd */
struct termios termios; /* original termios */
};
static void console_server_dump( struct object *obj, int verbose );
static void console_server_destroy( struct object *obj );
static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry );
static struct fd *console_server_get_fd( struct object *obj );
static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
static struct object *console_server_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static const struct object_ops console_server_ops =
{
sizeof(struct console_server), /* size */
console_server_dump, /* dump */
no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
console_server_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
console_server_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
console_server_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
console_server_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
fd_close_handle, /* close_handle */
console_server_destroy /* destroy */
};
static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct fd_ops console_server_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
console_get_fd_type, /* get_fd_type */
no_fd_read, /* read */
no_fd_write, /* write */
no_fd_flush, /* flush */
no_fd_get_file_info, /* get_file_info */
no_fd_get_volume_info, /* get_volume_info */
console_server_ioctl, /* ioctl */
default_fd_queue_async, /* queue_async */
default_fd_reselect_async /* reselect_async */
};
struct font_info
{
short int width;
short int height;
short int weight;
short int pitch_family;
WCHAR *face_name;
data_size_t face_len;
};
struct screen_buffer
{
struct object obj; /* object header */
struct list entry; /* entry in list of all screen buffers */
struct console_input *input; /* associated console input */
unsigned int id; /* buffer id */
unsigned int mode; /* output mode */
int cursor_size; /* size of cursor (percentage filled) */
int cursor_visible;/* cursor visibility flag */
int cursor_x; /* position of cursor */
int cursor_y; /* position of cursor */
int width; /* size (w-h) of the screen buffer */
int height;
int max_width; /* size (w-h) of the window given font size */
int max_height;
char_info_t *data; /* the data for each cell - a width x height matrix */
unsigned short attr; /* default fill attributes (screen colors) */
unsigned short popup_attr; /* pop-up color attributes */
unsigned int color_map[16]; /* color table */
rectangle_t win; /* current visible window on the screen buffer *
* as seen in wineconsole */
struct font_info font; /* console font information */
struct fd *fd; /* for bare console, attached output fd */
struct async_queue ioctl_q; /* ioctl queue */
};
static void screen_buffer_dump( struct object *obj, int verbose );
static void screen_buffer_destroy( struct object *obj );
static struct fd *screen_buffer_get_fd( struct object *obj );
static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static const struct object_ops screen_buffer_ops =
{
sizeof(struct screen_buffer), /* size */
screen_buffer_dump, /* dump */
no_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* satisfied */
no_signal, /* signal */
screen_buffer_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
screen_buffer_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
screen_buffer_destroy /* destroy */
};
static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct fd_ops screen_buffer_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
console_get_fd_type, /* get_fd_type */
no_fd_read, /* read */
no_fd_write, /* write */
no_fd_flush, /* flush */
no_fd_get_file_info, /* get_file_info */
no_fd_get_volume_info, /* get_volume_info */
screen_buffer_ioctl, /* ioctl */
default_fd_queue_async, /* queue_async */
default_fd_reselect_async /* reselect_async */
};
static struct object_type *console_device_get_type( struct object *obj );
static void console_device_dump( struct object *obj, int verbose );
static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
static struct object *console_device_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static const struct object_ops console_device_ops =
{
sizeof(struct object), /* size */
console_device_dump, /* dump */
console_device_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
default_get_full_name, /* get_full_name */
console_device_lookup_name, /* lookup_name */
directory_link_name, /* link_name */
default_unlink_name, /* unlink_name */
console_device_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
no_destroy /* destroy */
};
struct console_connection
{
struct object obj; /* object header */
struct fd *fd; /* pseudo-fd for ioctls */
};
static void console_connection_dump( struct object *obj, int verbose );
static struct fd *console_connection_get_fd( struct object *obj );
static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr );
static struct object *console_connection_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
static void console_connection_destroy( struct object *obj );
static const struct object_ops console_connection_ops =
{
sizeof(struct console_connection),/* size */
console_connection_dump, /* dump */
console_device_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
console_connection_get_fd, /* get_fd */
no_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_get_full_name, /* get_full_name */
console_connection_lookup_name, /* lookup_name */
directory_link_name, /* link_name */
default_unlink_name, /* unlink_name */
console_connection_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
console_connection_close_handle, /* close_handle */
console_connection_destroy /* destroy */
};
static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct fd_ops console_connection_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
console_get_fd_type, /* get_fd_type */
no_fd_read, /* read */
no_fd_write, /* write */
no_fd_flush, /* flush */
no_fd_get_file_info, /* get_file_info */
no_fd_get_volume_info, /* get_volume_info */
console_connection_ioctl, /* ioctl */
default_fd_queue_async, /* queue_async */
default_fd_reselect_async /* reselect_async */
};
static struct list screen_buffer_list = LIST_INIT(screen_buffer_list);
static const char_info_t empty_char_info = { ' ', 0x000f }; /* white on black space */
static struct fd *console_input_get_fd( struct object* obj )
{
struct console_input *console_input = (struct console_input*)obj;
assert( obj->ops == &console_input_ops );
return (struct fd *)grab_object( console_input->fd );
}
static enum server_fd_type console_get_fd_type( struct fd *fd )
{
return FD_TYPE_CHAR;
}
/* dumps the renderer events of a console */
static void console_input_events_dump( struct object *obj, int verbose )
{
struct console_input_events *evts = (struct console_input_events *)obj;
assert( obj->ops == &console_input_events_ops );
2002-06-01 01:06:46 +02:00
fprintf( stderr, "Console input events: %d/%d events\n",
evts->num_used, evts->num_alloc );
}
/* destroys the renderer events of a console */
static void console_input_events_destroy( struct object *obj )
{
struct console_input_events *evts = (struct console_input_events *)obj;
assert( obj->ops == &console_input_events_ops );
if (evts->console) evts->console->evt = NULL;
free_async_queue( &evts->read_q );
if (evts->fd) release_object( evts->fd );
free( evts->events );
}
static struct fd *console_input_events_get_fd( struct object* obj )
{
struct console_input_events *evts = (struct console_input_events*)obj;
assert( obj->ops == &console_input_events_ops );
return (struct fd*)grab_object( evts->fd );
}
static struct object *console_input_events_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
return grab_object( obj );
}
/* retrieves events from the console's renderer events list */
static int get_renderer_events( struct console_input_events* evts, struct async *async )
{
struct iosb *iosb = async_get_iosb( async );
data_size_t num;
num = min( iosb->out_size / sizeof(evts->events[0]), evts->num_used );
if (num && !(iosb->out_data = malloc( num * sizeof(evts->events[0] ))))
{
async_terminate( async, STATUS_NO_MEMORY );
release_object( iosb );
return 0;
}
iosb->status = STATUS_SUCCESS;
iosb->out_size = iosb->result = num * sizeof(evts->events[0]);
if (num) memcpy( iosb->out_data, evts->events, iosb->result );
release_object( iosb );
async_terminate( async, STATUS_ALERTED );
if (num && num < evts->num_used)
{
memmove( &evts->events[0], &evts->events[num],
(evts->num_used - num) * sizeof(evts->events[0]) );
}
evts->num_used -= num;
return 1;
}
/* add an event to the console's renderer events list */
static void console_input_events_append( struct console_input* console,
struct condrv_renderer_event* evt)
{
struct console_input_events* evts;
int collapsed = FALSE;
struct async *async;
if (!(evts = console->evt)) return;
/* to be done even when evt has been generated by the renderer ? */
/* try to collapse evt into current queue's events */
if (evts->num_used)
{
struct condrv_renderer_event* last = &evts->events[evts->num_used - 1];
if (last->event == CONSOLE_RENDERER_UPDATE_EVENT &&
evt->event == CONSOLE_RENDERER_UPDATE_EVENT)
{
/* if two update events overlap, collapse them into a single one */
if (last->u.update.bottom + 1 >= evt->u.update.top &&
evt->u.update.bottom + 1 >= last->u.update.top)
{
last->u.update.top = min(last->u.update.top, evt->u.update.top);
last->u.update.bottom = max(last->u.update.bottom, evt->u.update.bottom);
collapsed = TRUE;
}
}
}
if (!collapsed)
{
if (evts->num_used == evts->num_alloc)
{
evts->num_alloc += 16;
evts->events = realloc( evts->events, evts->num_alloc * sizeof(*evt) );
assert(evts->events);
}
evts->events[evts->num_used++] = *evt;
}
while (evts->num_used && (async = find_pending_async( &evts->read_q )))
{
get_renderer_events( evts, async );
release_object( async );
}
}
static struct object *create_console_input_events(void)
{
struct console_input_events* evt;
if (!(evt = alloc_object( &console_input_events_ops ))) return NULL;
evt->console = NULL;
evt->num_alloc = evt->num_used = 0;
evt->events = NULL;
init_async_queue( &evt->read_q );
if (!(evt->fd = alloc_pseudo_fd( &console_input_events_fd_ops, &evt->obj, 0 )))
{
release_object( evt );
return NULL;
}
return &evt->obj;
}
static struct object *create_console_input(void)
{
struct console_input *console_input;
if (!(console_input = alloc_object( &console_input_ops )))
return NULL;
console_input->renderer = NULL;
console_input->mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | ENABLE_INSERT_MODE |
ENABLE_EXTENDED_FLAGS;
console_input->num_proc = 0;
console_input->active = NULL;
console_input->recnum = 0;
console_input->records = NULL;
console_input->evt = NULL;
console_input->server = NULL;
console_input->title = NULL;
console_input->title_len = 0;
console_input->history_size = 50;
console_input->history = calloc( console_input->history_size, sizeof(*console_input->history) );
console_input->history_index = 0;
console_input->history_mode = 0;
console_input->edition_mode = 0;
console_input->input_cp = 0;
console_input->output_cp = 0;
console_input->win = 0;
console_input->event = create_event( NULL, NULL, 0, 1, 0, NULL );
console_input->fd = NULL;
console_input->last_id = 0;
init_async_queue( &console_input->ioctl_q );
init_async_queue( &console_input->read_q );
if (!console_input->history || !console_input->event)
{
console_input->history_size = 0;
release_object( console_input );
return NULL;
}
console_input->fd = alloc_pseudo_fd( &console_input_fd_ops, &console_input->obj,
FILE_SYNCHRONOUS_IO_NONALERT );
if (!console_input->fd)
{
release_object( console_input );
return NULL;
}
allow_fd_caching( console_input->fd );
return &console_input->obj;
}
static void console_host_ioctl_terminate( struct console_host_ioctl *call, unsigned int status )
{
if (call->async)
{
async_terminate( call->async, status );
release_object( call->async );
}
free( call );
}
static int queue_host_ioctl( struct console_server *server, unsigned int code, unsigned int output,
struct async *async, struct async_queue *queue )
{
struct console_host_ioctl *ioctl;
if (!(ioctl = mem_alloc( sizeof(*ioctl) ))) return 0;
ioctl->code = code;
ioctl->output = output;
ioctl->async = NULL;
if (async)
{
ioctl->async = (struct async *)grab_object( async );
queue_async( queue, async );
}
list_add_tail( &server->queue, &ioctl->entry );
wake_up( &server->obj, 0 );
if (async) set_error( STATUS_PENDING );
return 0;
}
static void disconnect_console_server( struct console_server *server )
{
while (!list_empty( &server->queue ))
{
struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
list_remove( &call->entry );
console_host_ioctl_terminate( call, STATUS_CANCELLED );
}
while (!list_empty( &server->read_queue ))
{
struct console_host_ioctl *call = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
list_remove( &call->entry );
console_host_ioctl_terminate( call, STATUS_CANCELLED );
}
if (server->term_fd != -1)
{
tcsetattr( server->term_fd, TCSANOW, &server->termios );
close( server->term_fd );
server->term_fd = -1;
}
if (server->console)
{
assert( server->console->server == server );
server->console->server = NULL;
server->console = NULL;
wake_up( &server->obj, 0 );
}
}
static void set_active_screen_buffer( struct console_input *console_input, struct screen_buffer *screen_buffer )
{
struct condrv_renderer_event evt;
if (console_input->active == screen_buffer) return;
if (console_input->active) release_object( console_input->active );
console_input->active = (struct screen_buffer *)grab_object( screen_buffer );
if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_ACTIVATE,
screen_buffer->id, NULL, NULL );
evt.event = CONSOLE_RENDERER_SB_RESIZE_EVENT;
evt.u.resize.width = screen_buffer->width;
evt.u.resize.height = screen_buffer->height;
console_input_events_append( console_input, &evt );
evt.event = CONSOLE_RENDERER_DISPLAY_EVENT;
evt.u.display.left = screen_buffer->win.left;
evt.u.display.top = screen_buffer->win.top;
evt.u.display.width = screen_buffer->win.right - screen_buffer->win.left + 1;
evt.u.display.height = screen_buffer->win.bottom - screen_buffer->win.top + 1;
console_input_events_append( console_input, &evt );
evt.event = CONSOLE_RENDERER_UPDATE_EVENT;
evt.u.update.top = 0;
evt.u.update.bottom = screen_buffer->height - 1;
console_input_events_append( console_input, &evt );
evt.event = CONSOLE_RENDERER_CURSOR_GEOM_EVENT;
evt.u.cursor_geom.size = screen_buffer->cursor_size;
evt.u.cursor_geom.visible = screen_buffer->cursor_visible;
console_input_events_append( console_input, &evt );
evt.event = CONSOLE_RENDERER_CURSOR_POS_EVENT;
evt.u.cursor_pos.x = screen_buffer->cursor_x;
evt.u.cursor_pos.y = screen_buffer->cursor_y;
console_input_events_append( console_input, &evt );
}
static struct object *create_console_output( struct console_input *console_input )
{
struct screen_buffer *screen_buffer;
int i;
if (console_input->last_id == ~0)
{
set_error( STATUS_NO_MEMORY );
return NULL;
}
if (!(screen_buffer = alloc_object( &screen_buffer_ops )))
return NULL;
screen_buffer->id = ++console_input->last_id;
screen_buffer->mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
screen_buffer->input = console_input;
screen_buffer->cursor_size = 100;
screen_buffer->cursor_visible = 1;
screen_buffer->width = 80;
screen_buffer->height = 150;
screen_buffer->max_width = 80;
screen_buffer->max_height = 25;
screen_buffer->cursor_x = 0;
screen_buffer->cursor_y = 0;
screen_buffer->attr = 0x0F;
screen_buffer->popup_attr = 0xF5;
screen_buffer->win.left = 0;
screen_buffer->win.right = screen_buffer->max_width - 1;
screen_buffer->win.top = 0;
screen_buffer->win.bottom = screen_buffer->max_height - 1;
screen_buffer->data = NULL;
screen_buffer->font.width = 0;
screen_buffer->font.height = 0;
screen_buffer->font.weight = FW_NORMAL;
screen_buffer->font.pitch_family = FIXED_PITCH | FF_DONTCARE;
screen_buffer->font.face_name = NULL;
screen_buffer->font.face_len = 0;
memset( screen_buffer->color_map, 0, sizeof(screen_buffer->color_map) );
init_async_queue( &screen_buffer->ioctl_q );
list_add_head( &screen_buffer_list, &screen_buffer->entry );
screen_buffer->fd = alloc_pseudo_fd( &screen_buffer_fd_ops, &screen_buffer->obj,
FILE_SYNCHRONOUS_IO_NONALERT );
if (!screen_buffer->fd)
{
release_object( screen_buffer );
return NULL;
}
allow_fd_caching(screen_buffer->fd);
if (!(screen_buffer->data = malloc( screen_buffer->width * screen_buffer->height *
sizeof(*screen_buffer->data) )))
{
release_object( screen_buffer );
return NULL;
}
/* clear the first row */
for (i = 0; i < screen_buffer->width; i++) screen_buffer->data[i] = empty_char_info;
/* and copy it to all other rows */
for (i = 1; i < screen_buffer->height; i++)
memcpy( &screen_buffer->data[i * screen_buffer->width], screen_buffer->data,
screen_buffer->width * sizeof(char_info_t) );
if (console_input->server) queue_host_ioctl( console_input->server, IOCTL_CONDRV_INIT_OUTPUT,
screen_buffer->id, NULL, NULL );
if (!console_input->active) set_active_screen_buffer( console_input, screen_buffer );
return &screen_buffer->obj;
}
/* free the console for this process */
int free_console( struct process *process )
{
struct console_input* console = process->console;
if (!console) return 0;
process->console = NULL;
if (--console->num_proc == 0 && console->renderer)
{
/* all processes have terminated... tell the renderer to terminate too */
struct condrv_renderer_event evt;
evt.event = CONSOLE_RENDERER_EXIT_EVENT;
memset(&evt.u, 0, sizeof(evt.u));
console_input_events_append( console, &evt );
}
release_object( console );
return 1;
}
/* let process inherit the console from parent... this handle two cases :
* 1/ generic console inheritance
* 2/ parent is a renderer which launches process, and process should attach to the console
* rendered by parent
*/
obj_handle_t inherit_console( struct thread *parent_thread, obj_handle_t handle, struct process *process,
obj_handle_t hconin )
{
struct console_input *console = NULL;
if (handle) return duplicate_handle( current->process, handle, process, 0, 0, DUP_HANDLE_SAME_ACCESS );
/* if parent is a renderer, then attach current process to its console
* a bit hacky....
*/
if (hconin && parent_thread)
{
/* FIXME: should we check some access rights ? */
if (!(console = (struct console_input *)get_handle_obj( parent_thread->process, hconin,
0, &console_input_ops )))
clear_error(); /* ignore error */
}
if (!console) return 0;
process->console = console;
console->num_proc++;
return alloc_handle( process, process->console,
SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE, 0 );
}
struct thread *console_get_renderer( struct console_input *console )
{
return console->renderer;
}
struct console_signal_info
{
struct console_input *console;
process_id_t group;
int signal;
};
static int propagate_console_signal_cb(struct process *process, void *user)
{
struct console_signal_info* csi = (struct console_signal_info*)user;
if (process->console == csi->console && process->running_threads &&
(!csi->group || process->group_id == csi->group))
{
/* find a suitable thread to signal */
struct thread *thread;
LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
{
if (send_thread_signal( thread, csi->signal )) break;
}
}
return FALSE;
}
static void propagate_console_signal( struct console_input *console,
int sig, process_id_t group_id )
{
struct console_signal_info csi;
if (!console)
{
set_error( STATUS_INVALID_PARAMETER );
return;
}
/* FIXME: should support the other events (like CTRL_BREAK) */
if (sig != CTRL_C_EVENT)
{
set_error( STATUS_NOT_IMPLEMENTED );
return;
}
csi.console = console;
csi.signal = SIGINT;
csi.group = group_id;
enum_processes(propagate_console_signal_cb, &csi);
}
/* dumb dump */
static void console_input_dump( struct object *obj, int verbose )
{
struct console_input *console = (struct console_input *)obj;
assert( obj->ops == &console_input_ops );
2002-06-01 01:06:46 +02:00
fprintf( stderr, "Console input active=%p evt=%p\n",
console->active, console->evt );
}
static void console_input_destroy( struct object *obj )
{
struct console_input* console_in = (struct console_input *)obj;
struct screen_buffer* curr;
int i;
assert( obj->ops == &console_input_ops );
if (console_in->server)
{
assert( console_in->server->console == console_in );
disconnect_console_server( console_in->server );
}
free( console_in->title );
free( console_in->records );
if (console_in->active) release_object( console_in->active );
console_in->active = NULL;
LIST_FOR_EACH_ENTRY( curr, &screen_buffer_list, struct screen_buffer, entry )
{
if (curr->input == console_in) curr->input = NULL;
}
free_async_queue( &console_in->ioctl_q );
free_async_queue( &console_in->read_q );
if (console_in->evt)
console_in->evt->console = NULL;
if (console_in->event)
release_object( console_in->event );
if (console_in->fd)
release_object( console_in->fd );
for (i = 0; i < console_in->history_size; i++)
free( console_in->history[i] );
free( console_in->history );
}
static struct object *create_console_connection( struct console_input *console )
{
struct console_connection *connection;
if (current->process->console)
{
set_error( STATUS_ACCESS_DENIED );
return NULL;
}
if (!(connection = alloc_object( &console_connection_ops ))) return NULL;
if (!(connection->fd = alloc_pseudo_fd( &console_connection_fd_ops, &connection->obj, 0 )))
{
release_object( connection );
return NULL;
}
if (console)
{
current->process->console = (struct console_input *)grab_object( console );
console->num_proc++;
}
return &connection->obj;
}
static struct object *console_input_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
{
struct console_input *console = (struct console_input *)obj;
static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
assert( obj->ops == &console_input_ops );
if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
{
name->len = 0;
return create_console_connection( console );
}
return NULL;
}
static struct object *console_input_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
return grab_object( obj );
}
static void screen_buffer_dump( struct object *obj, int verbose )
{
struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
assert( obj->ops == &screen_buffer_ops );
fprintf(stderr, "Console screen buffer input=%p\n", screen_buffer->input );
}
static void screen_buffer_destroy( struct object *obj )
{
struct screen_buffer *screen_buffer = (struct screen_buffer *)obj;
assert( obj->ops == &screen_buffer_ops );
list_remove( &screen_buffer->entry );
if (screen_buffer->input && screen_buffer->input->server)
queue_host_ioctl( screen_buffer->input->server, IOCTL_CONDRV_CLOSE_OUTPUT,
screen_buffer->id, NULL, NULL );
if (screen_buffer->fd) release_object( screen_buffer->fd );
free_async_queue( &screen_buffer->ioctl_q );
free( screen_buffer->data );
free( screen_buffer->font.face_name );
}
static struct object *screen_buffer_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
return grab_object( obj );
}
static struct fd *screen_buffer_get_fd( struct object *obj )
{
struct screen_buffer *screen_buffer = (struct screen_buffer*)obj;
assert( obj->ops == &screen_buffer_ops );
if (screen_buffer->fd)
return (struct fd*)grab_object( screen_buffer->fd );
set_error( STATUS_OBJECT_TYPE_MISMATCH );
return NULL;
}
static void console_server_dump( struct object *obj, int verbose )
{
assert( obj->ops == &console_server_ops );
fprintf( stderr, "Console server\n" );
}
static void console_server_destroy( struct object *obj )
{
struct console_server *server = (struct console_server *)obj;
assert( obj->ops == &console_server_ops );
disconnect_console_server( server );
if (server->fd) release_object( server->fd );
}
static struct object *console_server_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
{
struct console_server *server = (struct console_server*)obj;
static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
assert( obj->ops == &console_server_ops );
if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
{
struct screen_buffer *screen_buffer;
name->len = 0;
if (server->console)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
if (!(server->console = (struct console_input *)create_console_input())) return NULL;
if (!(screen_buffer = (struct screen_buffer *)create_console_output( server->console )))
{
release_object( server->console );
server->console = NULL;
return NULL;
}
release_object( screen_buffer );
server->console->server = server;
return &server->console->obj;
}
return NULL;
}
static int console_server_signaled( struct object *obj, struct wait_queue_entry *entry )
{
struct console_server *server = (struct console_server*)obj;
assert( obj->ops == &console_server_ops );
return !server->console || !list_empty( &server->queue );
}
static struct fd *console_server_get_fd( struct object* obj )
{
struct console_server *server = (struct console_server*)obj;
assert( obj->ops == &console_server_ops );
return (struct fd *)grab_object( server->fd );
}
static struct object *console_server_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
return grab_object( obj );
}
static struct object *create_console_server( void )
{
struct console_server *server;
if (!(server = alloc_object( &console_server_ops ))) return NULL;
server->console = NULL;
server->busy = 0;
server->term_fd = -1;
list_init( &server->queue );
list_init( &server->read_queue );
server->fd = alloc_pseudo_fd( &console_server_fd_ops, &server->obj, FILE_SYNCHRONOUS_IO_NONALERT );
if (!server->fd)
{
release_object( server );
return NULL;
}
allow_fd_caching(server->fd);
return &server->obj;
}
static int is_blocking_read_ioctl( unsigned int code )
{
return code == IOCTL_CONDRV_READ_INPUT || code == IOCTL_CONDRV_READ_CONSOLE;
}
static int console_input_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
{
struct console_input *console = get_fd_user( fd );
switch (code)
{
case IOCTL_CONDRV_CTRL_EVENT:
{
const struct condrv_ctrl_event *event = get_req_data();
process_id_t group;
if (get_req_data_size() != sizeof(*event))
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
group = event->group_id ? event->group_id : current->process->group_id;
if (!group)
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
propagate_console_signal( console, event->event, group );
return !get_error();
}
default:
if (!console->server || code >> 16 != FILE_DEVICE_CONSOLE)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
return queue_host_ioctl( console->server, code, 0, async, &console->ioctl_q );
}
}
static int screen_buffer_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
{
struct screen_buffer *screen_buffer = get_fd_user( fd );
switch (code)
{
case IOCTL_CONDRV_ACTIVATE:
if (!screen_buffer->input)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
set_active_screen_buffer( screen_buffer->input, screen_buffer );
return 1;
default:
if (!screen_buffer->input || !screen_buffer->input->server || code >> 16 != FILE_DEVICE_CONSOLE)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
return queue_host_ioctl( screen_buffer->input->server, code, screen_buffer->id,
async, &screen_buffer->ioctl_q );
}
}
static int console_input_events_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
{
struct console_input_events *evts = get_fd_user( fd );
switch (code)
{
case IOCTL_CONDRV_GET_RENDERER_EVENTS:
set_error( STATUS_PENDING );
if (evts->num_used) return get_renderer_events( evts, async );
queue_async( &evts->read_q, async );
return 1;
case IOCTL_CONDRV_ATTACH_RENDERER:
{
struct console_input *console_input;
if (get_req_data_size() != sizeof(condrv_handle_t))
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
console_input = (struct console_input *)get_handle_obj( current->process, *(condrv_handle_t *)get_req_data(),
0, &console_input_ops );
if (!console_input) return 0;
if (!console_input->evt && !evts->console)
{
console_input->evt = evts;
console_input->renderer = current;
evts->console = console_input;
}
else set_error( STATUS_INVALID_HANDLE );
release_object( console_input );
return !get_error();
}
case IOCTL_CONDRV_SCROLL:
case IOCTL_CONDRV_SET_MODE:
case IOCTL_CONDRV_WRITE_OUTPUT:
case IOCTL_CONDRV_READ_OUTPUT:
case IOCTL_CONDRV_FILL_OUTPUT:
case IOCTL_CONDRV_GET_OUTPUT_INFO:
case IOCTL_CONDRV_SET_OUTPUT_INFO:
if (!evts->console || !evts->console->active)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
return screen_buffer_ioctl( evts->console->active->fd, code, async );
default:
if (!evts->console)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
return console_input_ioctl( evts->console->fd, code, async );
}
}
static int console_connection_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
{
struct console_connection *console_connection = get_fd_user( fd );
switch (code)
{
case IOCTL_CONDRV_BIND_PID:
{
struct process *process;
unsigned int pid;
if (get_req_data_size() != sizeof(unsigned int))
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
if (current->process->console)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
pid = *(unsigned int *)get_req_data();
if (pid == ATTACH_PARENT_PROCESS) pid = current->process->parent_id;
if (!(process = get_process_from_id( pid ))) return 0;
if (process->console)
{
current->process->console = (struct console_input *)grab_object( process->console );
process->console->num_proc++;
}
else set_error( STATUS_ACCESS_DENIED );
release_object( process );
return !get_error();
}
default:
return default_fd_ioctl( console_connection->fd, code, async );
}
}
static int console_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
{
struct console_server *server = get_fd_user( fd );
switch (code)
{
case IOCTL_CONDRV_CTRL_EVENT:
{
const struct condrv_ctrl_event *event = get_req_data();
if (get_req_data_size() != sizeof(*event))
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
if (!server->console)
{
set_error( STATUS_INVALID_HANDLE );
return 0;
}
propagate_console_signal( server->console, event->event, event->group_id );
return !get_error();
}
case IOCTL_CONDRV_SETUP_INPUT:
{
struct termios term;
obj_handle_t handle;
struct file *file;
int unix_fd;
if (get_req_data_size() != sizeof(unsigned int) || get_reply_max_size())
{
set_error( STATUS_INVALID_PARAMETER );
return 0;
}
if (server->term_fd != -1)
{
tcsetattr( server->term_fd, TCSANOW, &server->termios );
close( server->term_fd );
server->term_fd = -1;
}
handle = *(unsigned int *)get_req_data();
if (!handle) return 1;
if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA )))
{
return 0;
}
unix_fd = get_file_unix_fd( file );
release_object( file );
if (tcgetattr( unix_fd, &server->termios ))
{
file_set_error();
return 0;
}
term = server->termios;
term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
term.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
term.c_cflag &= ~(CSIZE | PARENB);
term.c_cflag |= CS8;
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
if (tcsetattr( unix_fd, TCSANOW, &term ) || (server->term_fd = dup( unix_fd )) == -1)
{
file_set_error();
return 0;
}
return 1;
}
default:
set_error( STATUS_INVALID_HANDLE );
return 0;
}
}
static void console_connection_dump( struct object *obj, int verbose )
{
fputs( "console connection\n", stderr );
}
static struct fd *console_connection_get_fd( struct object *obj )
{
struct console_connection *connection = (struct console_connection *)obj;
return (struct fd *)grab_object( connection->fd );
}
static struct object *console_connection_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
{
static const WCHAR referenceW[] = {'R','e','f','e','r','e','n','c','e'};
if (name->len == sizeof(referenceW) && !memcmp( name->str, referenceW, name->len ))
{
if (!current->process->console)
{
set_error( STATUS_INVALID_HANDLE );
return NULL;
}
name->len = 0;
return grab_object( current->process->console );
}
return NULL;
}
static struct object *console_connection_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
return grab_object( obj );
}
static int console_connection_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
{
free_console( process );
return 1;
}
static void console_connection_destroy( struct object *obj )
{
struct console_connection *connection = (struct console_connection *)obj;
if (connection->fd) release_object( connection->fd );
}
static struct object_type *console_device_get_type( struct object *obj )
{
static const WCHAR name[] = {'D','e','v','i','c','e'};
static const struct unicode_str str = { name, sizeof(name) };
return get_object_type( &str );
}
static void console_device_dump( struct object *obj, int verbose )
{
fputs( "Console device\n", stderr );
}
static struct object *console_device_lookup_name( struct object *obj, struct unicode_str *name, unsigned int attr )
{
static const WCHAR connectionW[] = {'C','o','n','n','e','c','t','i','o','n'};
static const WCHAR consoleW[] = {'C','o','n','s','o','l','e'};
static const WCHAR current_inW[] = {'C','u','r','r','e','n','t','I','n'};
static const WCHAR current_outW[] = {'C','u','r','r','e','n','t','O','u','t'};
static const WCHAR rendererW[] = {'R','e','n','d','e','r','e','r'};
static const WCHAR screen_bufferW[] = {'S','c','r','e','e','n','B','u','f','f','e','r'};
static const WCHAR serverW[] = {'S','e','r','v','e','r'};
if (name->len == sizeof(current_inW) && !memcmp( name->str, current_inW, name->len ))
{
if (!current->process->console)
{
set_error( STATUS_INVALID_HANDLE );
return NULL;
}
name->len = 0;
return grab_object( current->process->console );
}
if (name->len == sizeof(current_outW) && !memcmp( name->str, current_outW, name->len ))
{
if (!current->process->console || !current->process->console->active)
{
set_error( STATUS_INVALID_HANDLE );
return NULL;
}
name->len = 0;
return grab_object( current->process->console->active );
}
if (name->len == sizeof(consoleW) && !memcmp( name->str, consoleW, name->len ))
{
name->len = 0;
return grab_object( obj );
}
if (name->len == sizeof(rendererW) && !memcmp( name->str, rendererW, name->len ))
{
name->len = 0;
return create_console_input_events();
}
if (name->len == sizeof(screen_bufferW) && !memcmp( name->str, screen_bufferW, name->len ))
{
if (!current->process->console)
{
set_error( STATUS_INVALID_HANDLE );
return NULL;
}
name->len = 0;
return create_console_output( current->process->console );
}
if (name->len == sizeof(serverW) && !memcmp( name->str, serverW, name->len ))
{
name->len = 0;
return create_console_server();
}
if (name->len == sizeof(connectionW) && !memcmp( name->str, connectionW, name->len ))
{
name->len = 0;
return create_console_connection( NULL );
}
return NULL;
}
static struct object *console_device_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
int is_output;
access = default_fd_map_access( obj, access );
is_output = access & FILE_WRITE_DATA;
if (!current->process->console || (is_output && !current->process->console))
{
set_error( STATUS_INVALID_HANDLE );
return NULL;
}
if (is_output && (access & FILE_READ_DATA))
{
set_error( STATUS_INVALID_PARAMETER );
return NULL;
}
return is_output ? grab_object( current->process->console->active ) : grab_object( current->process->console );
}
struct object *create_console_device( struct object *root, const struct unicode_str *name,
unsigned int attr, const struct security_descriptor *sd )
{
return create_named_object( root, &console_device_ops, name, attr, sd );
}
/* get console which renderer is 'current' */
static int cgwe_enum( struct process* process, void* user)
{
if (process->console && process->console->renderer == current)
{
*(struct console_input**)user = (struct console_input *)grab_object( process->console );
return 1;
}
return 0;
}
DECL_HANDLER(get_console_wait_event)
{
struct console_input* console = NULL;
struct object *obj;
if (req->handle)
{
if (!(obj = get_handle_obj( current->process, req->handle, FILE_READ_PROPERTIES, NULL ))) return;
if (obj->ops == &console_input_ops)
console = (struct console_input *)grab_object( obj );
else if (obj->ops == &screen_buffer_ops)
console = (struct console_input *)grab_object( ((struct screen_buffer *)obj)->input );
else
set_error( STATUS_OBJECT_TYPE_MISMATCH );
release_object( obj );
}
else if (current->process->console)
console = (struct console_input *)grab_object( current->process->console );
else enum_processes(cgwe_enum, &console);
if (console)
{
reply->event = alloc_handle( current->process, console->event, EVENT_ALL_ACCESS, 0 );
release_object( console );
}
else set_error( STATUS_INVALID_PARAMETER );
}
/* retrieve the next pending console ioctl request */
DECL_HANDLER(get_next_console_request)
{
struct console_host_ioctl *ioctl = NULL, *next;
struct console_server *server;
struct iosb *iosb = NULL;
server = (struct console_server *)get_handle_obj( current->process, req->handle, 0, &console_server_ops );
if (!server) return;
if (!server->console)
{
set_error( STATUS_INVALID_HANDLE );
release_object( server );
return;
}
if (req->signal) set_event( server->console->event);
else reset_event( server->console->event );
if (req->read)
{
/* set result of current pending ioctl */
if (list_empty( &server->read_queue ))
{
set_error( STATUS_INVALID_HANDLE );
release_object( server );
return;
}
ioctl = LIST_ENTRY( list_head( &server->read_queue ), struct console_host_ioctl, entry );
list_remove( &ioctl->entry );
list_move_tail( &server->queue, &server->read_queue );
}
else if (server->busy)
{
/* set result of previous ioctl */
ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
list_remove( &ioctl->entry );
}
if (ioctl)
{
unsigned int status = req->status;
if (status == STATUS_PENDING) status = STATUS_INVALID_PARAMETER;
if (ioctl->async)
{
iosb = async_get_iosb( ioctl->async );
iosb->status = status;
iosb->out_size = min( iosb->out_size, get_req_data_size() );
if (iosb->out_size)
{
if ((iosb->out_data = memdup( get_req_data(), iosb->out_size )))
{
iosb->result = iosb->out_size;
status = STATUS_ALERTED;
}
else if (!status)
{
iosb->status = STATUS_NO_MEMORY;
iosb->out_size = 0;
}
}
}
console_host_ioctl_terminate( ioctl, status );
if (iosb) release_object( iosb );
if (req->read)
{
release_object( server );
return;
}
server->busy = 0;
}
/* if we have a blocking read ioctl in queue head and previous blocking read is still waiting,
* move it to read queue for execution after current read is complete. move all blocking
* ioctl at the same time to preserve their order. */
if (!list_empty( &server->queue ) && !list_empty( &server->read_queue ))
{
ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
if (is_blocking_read_ioctl( ioctl->code ))
{
LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &server->queue, struct console_host_ioctl, entry )
{
if (!is_blocking_read_ioctl( ioctl->code )) continue;
list_remove( &ioctl->entry );
list_add_tail( &server->read_queue, &ioctl->entry );
}
}
}
/* return the next ioctl */
if (!list_empty( &server->queue ))
{
ioctl = LIST_ENTRY( list_head( &server->queue ), struct console_host_ioctl, entry );
iosb = ioctl->async ? async_get_iosb( ioctl->async ) : NULL;
if (!iosb || get_reply_max_size() >= iosb->in_size)
{
reply->code = ioctl->code;
reply->output = ioctl->output;
if (iosb)
{
reply->out_size = iosb->out_size;
set_reply_data_ptr( iosb->in_data, iosb->in_size );
iosb->in_data = NULL;
}
if (is_blocking_read_ioctl( ioctl->code ))
{
list_remove( &ioctl->entry );
assert( list_empty( &server->read_queue ));
list_add_tail( &server->read_queue, &ioctl->entry );
}
else server->busy = 1;
}
else
{
reply->out_size = iosb->in_size;
set_error( STATUS_BUFFER_OVERFLOW );
}
if (iosb) release_object( iosb );
}
else
{
set_error( STATUS_PENDING );
}
release_object( server );
}