2007-03-20 11:23:59 -07:00
|
|
|
/*
|
|
|
|
* Server-side async I/O support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* 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 <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
|
|
|
|
#include "object.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "request.h"
|
2016-12-01 04:10:23 -07:00
|
|
|
#include "process.h"
|
|
|
|
#include "handle.h"
|
2007-03-20 11:23:59 -07:00
|
|
|
|
|
|
|
struct async
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct thread *thread; /* owning thread */
|
2007-04-03 10:36:07 -07:00
|
|
|
struct list queue_entry; /* entry in async queue list */
|
2016-12-01 04:10:23 -07:00
|
|
|
struct list process_entry; /* entry in process list */
|
2007-04-03 10:36:07 -07:00
|
|
|
struct async_queue *queue; /* queue containing this async */
|
2017-06-26 03:19:28 -07:00
|
|
|
struct fd *fd; /* fd associated with an unqueued async */
|
2007-03-20 11:23:59 -07:00
|
|
|
struct timeout_user *timeout;
|
2007-04-03 10:12:31 -07:00
|
|
|
unsigned int timeout_status; /* status to report upon timeout */
|
2007-03-21 06:27:52 -07:00
|
|
|
struct event *event;
|
2007-03-20 12:21:12 -07:00
|
|
|
async_data_t data; /* data for async I/O call */
|
2016-12-04 13:21:46 -07:00
|
|
|
struct iosb *iosb; /* I/O status block */
|
2017-06-26 03:17:36 -07:00
|
|
|
obj_handle_t wait_handle; /* pre-allocated wait handle */
|
2019-05-23 10:36:06 -07:00
|
|
|
unsigned int signaled :1;
|
2019-08-08 06:24:18 -07:00
|
|
|
unsigned int pending :1; /* request successfully queued, but pending */
|
2019-05-23 10:36:06 -07:00
|
|
|
unsigned int direct_result :1;/* a flag if we're passing result directly from request instead of APC */
|
2021-08-31 17:23:50 -07:00
|
|
|
unsigned int alerted :1; /* fd is signaled, but we are waiting for client-side I/O */
|
2021-08-31 17:23:51 -07:00
|
|
|
unsigned int terminated :1; /* async has been terminated */
|
2021-09-02 17:08:46 -07:00
|
|
|
unsigned int unknown_status :1; /* initial status is not known yet */
|
2017-07-04 06:26:05 -07:00
|
|
|
struct completion *completion; /* completion associated with fd */
|
|
|
|
apc_param_t comp_key; /* completion key associated with fd */
|
2018-10-25 05:56:08 -07:00
|
|
|
unsigned int comp_flags; /* completion flags */
|
2021-02-08 16:40:42 -07:00
|
|
|
async_completion_callback completion_callback; /* callback to be called on completion */
|
|
|
|
void *completion_callback_private; /* argument to completion_callback */
|
2007-03-20 11:23:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void async_dump( struct object *obj, int verbose );
|
2015-05-08 01:32:06 -07:00
|
|
|
static int async_signaled( struct object *obj, struct wait_queue_entry *entry );
|
2017-06-26 03:17:51 -07:00
|
|
|
static void async_satisfied( struct object * obj, struct wait_queue_entry *entry );
|
2007-03-20 11:23:59 -07:00
|
|
|
static void async_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops async_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct async), /* size */
|
2021-02-04 05:44:18 -07:00
|
|
|
&no_type, /* type */
|
2007-03-20 11:23:59 -07:00
|
|
|
async_dump, /* dump */
|
2015-05-08 01:32:06 -07:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
|
|
|
async_signaled, /* signaled */
|
2017-06-26 03:17:51 -07:00
|
|
|
async_satisfied, /* satisfied */
|
2007-03-20 11:23:59 -07:00
|
|
|
no_signal, /* signal */
|
|
|
|
no_get_fd, /* get_fd */
|
2021-02-05 04:10:44 -07:00
|
|
|
default_map_access, /* map_access */
|
2007-10-03 05:10:37 -07:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 07:52:31 -07:00
|
|
|
no_get_full_name, /* get_full_name */
|
2007-03-20 11:23:59 -07:00
|
|
|
no_lookup_name, /* lookup_name */
|
2016-02-04 05:07:19 -07:00
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
2007-03-22 03:44:29 -07:00
|
|
|
no_open_file, /* open_file */
|
2019-03-25 06:37:09 -07:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2007-03-20 11:23:59 -07:00
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
async_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
2007-04-10 08:07:27 -07:00
|
|
|
static inline void async_reselect( struct async *async )
|
|
|
|
{
|
2017-07-04 06:25:51 -07:00
|
|
|
if (async->queue && async->fd) fd_reselect_async( async->fd, async->queue );
|
2007-04-10 08:07:27 -07:00
|
|
|
}
|
|
|
|
|
2007-03-20 11:23:59 -07:00
|
|
|
static void async_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct async *async = (struct async *)obj;
|
|
|
|
assert( obj->ops == &async_ops );
|
|
|
|
fprintf( stderr, "Async thread=%p\n", async->thread );
|
|
|
|
}
|
|
|
|
|
2015-05-08 01:32:06 -07:00
|
|
|
static int async_signaled( struct object *obj, struct wait_queue_entry *entry )
|
|
|
|
{
|
|
|
|
struct async *async = (struct async *)obj;
|
|
|
|
assert( obj->ops == &async_ops );
|
|
|
|
return async->signaled;
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:17:51 -07:00
|
|
|
static void async_satisfied( struct object *obj, struct wait_queue_entry *entry )
|
|
|
|
{
|
|
|
|
struct async *async = (struct async *)obj;
|
|
|
|
assert( obj->ops == &async_ops );
|
|
|
|
|
2021-08-31 17:23:49 -07:00
|
|
|
/* we only return an async handle for asyncs created via create_request_async() */
|
|
|
|
assert( async->iosb );
|
|
|
|
|
2017-06-26 03:18:08 -07:00
|
|
|
if (async->direct_result)
|
|
|
|
{
|
|
|
|
async_set_result( &async->obj, async->iosb->status, async->iosb->result );
|
|
|
|
async->direct_result = 0;
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:23:49 -07:00
|
|
|
set_wait_status( entry, async->iosb->status );
|
2021-05-21 20:08:46 -07:00
|
|
|
|
2017-06-26 03:17:51 -07:00
|
|
|
/* close wait handle here to avoid extra server round trip */
|
|
|
|
if (async->wait_handle)
|
|
|
|
{
|
|
|
|
close_handle( async->thread->process, async->wait_handle );
|
|
|
|
async->wait_handle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-20 11:23:59 -07:00
|
|
|
static void async_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct async *async = (struct async *)obj;
|
|
|
|
assert( obj->ops == &async_ops );
|
|
|
|
|
2016-12-01 04:10:23 -07:00
|
|
|
list_remove( &async->process_entry );
|
2017-02-15 14:12:28 -07:00
|
|
|
|
|
|
|
if (async->queue)
|
|
|
|
{
|
|
|
|
list_remove( &async->queue_entry );
|
|
|
|
async_reselect( async );
|
|
|
|
}
|
2017-07-04 06:25:51 -07:00
|
|
|
else if (async->fd) release_object( async->fd );
|
2007-04-10 08:07:27 -07:00
|
|
|
|
2007-03-20 11:23:59 -07:00
|
|
|
if (async->timeout) remove_timeout_user( async->timeout );
|
2017-07-04 06:26:05 -07:00
|
|
|
if (async->completion) release_object( async->completion );
|
2007-03-21 06:27:52 -07:00
|
|
|
if (async->event) release_object( async->event );
|
2016-12-04 13:21:46 -07:00
|
|
|
if (async->iosb) release_object( async->iosb );
|
2007-03-20 11:23:59 -07:00
|
|
|
release_object( async->thread );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* notifies client thread of new status of its async request */
|
2007-05-08 11:37:21 -07:00
|
|
|
void async_terminate( struct async *async, unsigned int status )
|
2007-03-20 11:23:59 -07:00
|
|
|
{
|
2021-09-01 15:28:41 -07:00
|
|
|
struct iosb *iosb = async->iosb;
|
|
|
|
|
2021-08-31 17:23:51 -07:00
|
|
|
if (async->terminated) return;
|
2007-04-10 08:07:27 -07:00
|
|
|
|
2021-08-31 17:23:51 -07:00
|
|
|
async->terminated = 1;
|
2016-12-04 13:21:46 -07:00
|
|
|
if (async->iosb && async->iosb->status == STATUS_PENDING) async->iosb->status = status;
|
2021-08-31 17:23:50 -07:00
|
|
|
if (status == STATUS_ALERTED)
|
|
|
|
async->alerted = 1;
|
2015-05-06 01:10:31 -07:00
|
|
|
|
2021-08-31 17:23:47 -07:00
|
|
|
/* if no APC could be queued (e.g. the process is terminated),
|
|
|
|
* thread_queue_apc() may trigger async_set_result(), which may drop the
|
|
|
|
* last reference to the async, so grab a temporary reference here */
|
|
|
|
grab_object( async );
|
|
|
|
|
2017-06-26 03:18:08 -07:00
|
|
|
if (!async->direct_result)
|
2015-05-06 01:10:31 -07:00
|
|
|
{
|
2021-07-05 09:19:59 -07:00
|
|
|
apc_call_t data;
|
|
|
|
|
|
|
|
memset( &data, 0, sizeof(data) );
|
|
|
|
data.type = APC_ASYNC_IO;
|
|
|
|
data.async_io.user = async->data.user;
|
|
|
|
data.async_io.sb = async->data.iosb;
|
2021-09-01 15:28:41 -07:00
|
|
|
|
|
|
|
/* if the result is nonzero or there is output data, the client needs to
|
|
|
|
* make an extra request to retrieve them; use STATUS_ALERTED to signal
|
|
|
|
* this case */
|
|
|
|
if (iosb && (iosb->result || iosb->out_data))
|
|
|
|
data.async_io.status = STATUS_ALERTED;
|
|
|
|
else
|
|
|
|
data.async_io.status = status;
|
|
|
|
|
2021-07-05 09:19:59 -07:00
|
|
|
thread_queue_apc( async->thread->process, async->thread, &async->obj, &data );
|
2015-05-06 01:10:31 -07:00
|
|
|
}
|
|
|
|
|
2007-04-10 08:07:27 -07:00
|
|
|
async_reselect( async );
|
2021-08-31 17:23:47 -07:00
|
|
|
|
|
|
|
release_object( async );
|
2007-03-20 11:23:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* callback for timeout on an async request */
|
|
|
|
static void async_timeout( void *private )
|
|
|
|
{
|
|
|
|
struct async *async = private;
|
|
|
|
|
|
|
|
async->timeout = NULL;
|
2007-04-03 10:12:31 -07:00
|
|
|
async_terminate( async, async->timeout_status );
|
2007-03-20 11:23:59 -07:00
|
|
|
}
|
|
|
|
|
2007-04-03 10:36:07 -07:00
|
|
|
/* free an async queue, cancelling all async operations */
|
|
|
|
void free_async_queue( struct async_queue *queue )
|
|
|
|
{
|
2017-07-04 06:26:19 -07:00
|
|
|
struct async *async, *next;
|
2017-07-04 06:25:51 -07:00
|
|
|
|
2017-07-04 06:26:19 -07:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( async, next, &queue->queue, struct async, queue_entry )
|
2017-07-04 06:26:05 -07:00
|
|
|
{
|
2017-07-10 03:51:05 -07:00
|
|
|
if (!async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
|
2017-07-04 06:26:05 -07:00
|
|
|
async->fd = NULL;
|
2017-07-04 06:26:19 -07:00
|
|
|
async_terminate( async, STATUS_HANDLES_CLOSED );
|
|
|
|
async->queue = NULL;
|
2018-02-26 11:14:24 -07:00
|
|
|
release_object( &async->obj );
|
2017-07-04 06:26:05 -07:00
|
|
|
}
|
2007-04-03 10:36:07 -07:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:12:28 -07:00
|
|
|
void queue_async( struct async_queue *queue, struct async *async )
|
|
|
|
{
|
2017-07-04 06:25:51 -07:00
|
|
|
/* fd will be set to NULL in free_async_queue when fd is destroyed */
|
2017-06-26 03:19:28 -07:00
|
|
|
release_object( async->fd );
|
2017-02-15 14:12:28 -07:00
|
|
|
|
2017-07-04 06:26:19 -07:00
|
|
|
async->queue = queue;
|
2017-02-15 14:12:28 -07:00
|
|
|
grab_object( async );
|
|
|
|
list_add_tail( &queue->queue, &async->queue_entry );
|
|
|
|
|
2017-07-21 06:19:37 -07:00
|
|
|
set_fd_signaled( async->fd, 0 );
|
2017-02-15 14:12:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-20 11:23:59 -07:00
|
|
|
/* create an async on a given queue of a fd */
|
2017-06-26 03:19:28 -07:00
|
|
|
struct async *create_async( struct fd *fd, struct thread *thread, const async_data_t *data, struct iosb *iosb )
|
2007-03-20 11:23:59 -07:00
|
|
|
{
|
2007-03-21 06:27:52 -07:00
|
|
|
struct event *event = NULL;
|
|
|
|
struct async *async;
|
2007-03-20 11:23:59 -07:00
|
|
|
|
2007-03-21 06:27:52 -07:00
|
|
|
if (data->event && !(event = get_event_obj( thread->process, data->event, EVENT_MODIFY_STATE )))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(async = alloc_object( &async_ops )))
|
|
|
|
{
|
|
|
|
if (event) release_object( event );
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-20 11:23:59 -07:00
|
|
|
|
2017-06-26 03:18:08 -07:00
|
|
|
async->thread = (struct thread *)grab_object( thread );
|
|
|
|
async->event = event;
|
|
|
|
async->data = *data;
|
|
|
|
async->timeout = NULL;
|
|
|
|
async->queue = NULL;
|
2017-06-26 03:19:28 -07:00
|
|
|
async->fd = (struct fd *)grab_object( fd );
|
2017-06-26 03:18:08 -07:00
|
|
|
async->signaled = 0;
|
2019-05-23 10:36:06 -07:00
|
|
|
async->pending = 1;
|
2017-06-26 03:18:08 -07:00
|
|
|
async->wait_handle = 0;
|
|
|
|
async->direct_result = 0;
|
2021-08-31 17:23:50 -07:00
|
|
|
async->alerted = 0;
|
2021-08-31 17:23:51 -07:00
|
|
|
async->terminated = 0;
|
2021-09-02 17:08:46 -07:00
|
|
|
async->unknown_status = 0;
|
2017-07-10 03:51:05 -07:00
|
|
|
async->completion = fd_get_completion( fd, &async->comp_key );
|
2018-10-25 05:56:08 -07:00
|
|
|
async->comp_flags = 0;
|
2021-02-08 16:40:42 -07:00
|
|
|
async->completion_callback = NULL;
|
|
|
|
async->completion_callback_private = NULL;
|
2007-03-20 11:23:59 -07:00
|
|
|
|
2016-12-04 13:21:46 -07:00
|
|
|
if (iosb) async->iosb = (struct iosb *)grab_object( iosb );
|
|
|
|
else async->iosb = NULL;
|
|
|
|
|
2017-02-10 08:05:39 -07:00
|
|
|
list_add_head( &thread->process->asyncs, &async->process_entry );
|
2007-03-21 06:27:52 -07:00
|
|
|
if (event) reset_event( event );
|
2017-02-15 14:12:28 -07:00
|
|
|
|
2017-07-10 03:51:05 -07:00
|
|
|
if (async->completion && data->apc)
|
|
|
|
{
|
|
|
|
release_object( async );
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-20 11:23:59 -07:00
|
|
|
return async;
|
|
|
|
}
|
|
|
|
|
2019-05-23 10:36:27 -07:00
|
|
|
void set_async_pending( struct async *async, int signal )
|
|
|
|
{
|
2021-08-31 17:23:51 -07:00
|
|
|
if (!async->terminated)
|
2019-05-23 10:36:27 -07:00
|
|
|
{
|
|
|
|
async->pending = 1;
|
2021-09-02 17:08:46 -07:00
|
|
|
async->unknown_status = 0;
|
2019-05-23 10:36:27 -07:00
|
|
|
if (signal && !async->signaled)
|
|
|
|
{
|
|
|
|
async->signaled = 1;
|
|
|
|
wake_up( &async->obj, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:17:36 -07:00
|
|
|
/* return async object status and wait handle to client */
|
2021-09-02 17:08:49 -07:00
|
|
|
obj_handle_t async_handoff( struct async *async, data_size_t *result, int force_blocking )
|
2017-06-26 03:17:36 -07:00
|
|
|
{
|
2021-09-02 17:08:46 -07:00
|
|
|
if (async->unknown_status)
|
|
|
|
{
|
|
|
|
/* even the initial status is not known yet */
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return async->wait_handle;
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:08:49 -07:00
|
|
|
if (!async->pending && NT_ERROR( get_error() ))
|
2017-06-26 03:17:36 -07:00
|
|
|
{
|
|
|
|
close_handle( async->thread->process, async->wait_handle );
|
|
|
|
async->wait_handle = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:19:40 -07:00
|
|
|
if (get_error() != STATUS_PENDING)
|
|
|
|
{
|
|
|
|
/* status and data are already set and returned */
|
|
|
|
async_terminate( async, get_error() );
|
|
|
|
}
|
|
|
|
else if (async->iosb->status != STATUS_PENDING)
|
2017-06-26 03:17:36 -07:00
|
|
|
{
|
2017-06-26 03:19:40 -07:00
|
|
|
/* result is already available in iosb, return it */
|
2017-06-26 03:18:08 -07:00
|
|
|
if (async->iosb->out_data)
|
|
|
|
{
|
|
|
|
set_reply_data_ptr( async->iosb->out_data, async->iosb->out_size );
|
|
|
|
async->iosb->out_data = NULL;
|
|
|
|
}
|
2017-06-26 03:19:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (async->iosb->status != STATUS_PENDING)
|
|
|
|
{
|
2017-06-26 03:18:23 -07:00
|
|
|
if (result) *result = async->iosb->result;
|
2017-06-26 03:18:08 -07:00
|
|
|
async->signaled = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
async->direct_result = 0;
|
2019-05-23 10:36:06 -07:00
|
|
|
async->pending = 1;
|
2018-10-31 08:45:04 -07:00
|
|
|
if (!force_blocking && async->fd && is_fd_overlapped( async->fd ))
|
2017-06-26 03:18:08 -07:00
|
|
|
{
|
|
|
|
close_handle( async->thread->process, async->wait_handle);
|
|
|
|
async->wait_handle = 0;
|
|
|
|
}
|
2017-06-26 03:17:36 -07:00
|
|
|
}
|
2017-06-26 03:18:08 -07:00
|
|
|
set_error( async->iosb->status );
|
2017-06-26 03:17:51 -07:00
|
|
|
return async->wait_handle;
|
2017-06-26 03:17:36 -07:00
|
|
|
}
|
|
|
|
|
2021-09-01 15:28:38 -07:00
|
|
|
/* complete a request-based async with a pre-allocated buffer */
|
|
|
|
void async_request_complete( struct async *async, unsigned int status, data_size_t result,
|
|
|
|
data_size_t out_size, void *out_data )
|
|
|
|
{
|
|
|
|
struct iosb *iosb = async_get_iosb( async );
|
|
|
|
|
|
|
|
/* the async may have already been canceled */
|
|
|
|
if (iosb->status != STATUS_PENDING)
|
|
|
|
{
|
|
|
|
release_object( iosb );
|
|
|
|
free( out_data );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iosb->status = status;
|
|
|
|
iosb->result = result;
|
|
|
|
iosb->out_data = out_data;
|
|
|
|
iosb->out_size = out_size;
|
|
|
|
|
|
|
|
release_object( iosb );
|
|
|
|
|
|
|
|
async_terminate( async, status );
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:28:39 -07:00
|
|
|
/* complete a request-based async */
|
|
|
|
void async_request_complete_alloc( struct async *async, unsigned int status, data_size_t result,
|
|
|
|
data_size_t out_size, const void *out_data )
|
|
|
|
{
|
|
|
|
void *out_data_copy = NULL;
|
|
|
|
|
|
|
|
if (out_size && !(out_data_copy = memdup( out_data, out_size )))
|
|
|
|
{
|
|
|
|
async_terminate( async, STATUS_NO_MEMORY );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async_request_complete( async, status, result, out_size, out_data_copy );
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:08:46 -07:00
|
|
|
/* mark an async as having unknown initial status */
|
|
|
|
void async_set_unknown_status( struct async *async )
|
|
|
|
{
|
|
|
|
async->unknown_status = 1;
|
|
|
|
async->direct_result = 0;
|
|
|
|
}
|
|
|
|
|
2007-04-02 11:41:59 -07:00
|
|
|
/* set the timeout of an async operation */
|
2007-04-17 11:08:59 -07:00
|
|
|
void async_set_timeout( struct async *async, timeout_t timeout, unsigned int status )
|
2007-04-02 11:41:59 -07:00
|
|
|
{
|
|
|
|
if (async->timeout) remove_timeout_user( async->timeout );
|
2007-04-17 11:08:59 -07:00
|
|
|
if (timeout != TIMEOUT_INFINITE) async->timeout = add_timeout_user( timeout, async_timeout, async );
|
2007-04-02 11:41:59 -07:00
|
|
|
else async->timeout = NULL;
|
2007-04-03 10:12:31 -07:00
|
|
|
async->timeout_status = status;
|
2007-04-02 11:41:59 -07:00
|
|
|
}
|
|
|
|
|
2021-02-08 16:40:42 -07:00
|
|
|
/* set a callback to be notified when the async is completed */
|
|
|
|
void async_set_completion_callback( struct async *async, async_completion_callback func, void *private )
|
|
|
|
{
|
|
|
|
async->completion_callback = func;
|
|
|
|
async->completion_callback_private = private;
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:19:28 -07:00
|
|
|
static void add_async_completion( struct async *async, apc_param_t cvalue, unsigned int status,
|
2013-08-23 03:37:51 -07:00
|
|
|
apc_param_t information )
|
2012-06-05 06:35:27 -07:00
|
|
|
{
|
2017-07-04 06:26:05 -07:00
|
|
|
if (async->fd && !async->completion) async->completion = fd_get_completion( async->fd, &async->comp_key );
|
|
|
|
if (async->completion) add_completion( async->completion, async->comp_key, cvalue, status, information );
|
2012-06-05 06:35:27 -07:00
|
|
|
}
|
|
|
|
|
2007-03-21 06:28:23 -07:00
|
|
|
/* store the result of the client-side async callback */
|
2017-04-10 04:21:41 -07:00
|
|
|
void async_set_result( struct object *obj, unsigned int status, apc_param_t total )
|
2007-03-21 06:28:23 -07:00
|
|
|
{
|
|
|
|
struct async *async = (struct async *)obj;
|
|
|
|
|
|
|
|
if (obj->ops != &async_ops) return; /* in case the client messed up the APC results */
|
|
|
|
|
2021-08-31 17:23:51 -07:00
|
|
|
assert( async->terminated ); /* it must have been woken up if we get a result */
|
2007-04-10 08:07:27 -07:00
|
|
|
|
2021-09-01 15:28:36 -07:00
|
|
|
if (async->alerted && status == STATUS_PENDING) /* restart it */
|
2007-03-21 06:28:23 -07:00
|
|
|
{
|
2021-09-01 15:28:36 -07:00
|
|
|
async->terminated = 0;
|
|
|
|
async->alerted = 0;
|
|
|
|
async_reselect( async );
|
2007-03-21 06:28:23 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-04-10 08:07:27 -07:00
|
|
|
if (async->timeout) remove_timeout_user( async->timeout );
|
|
|
|
async->timeout = NULL;
|
2021-08-31 17:23:51 -07:00
|
|
|
async->terminated = 1;
|
2021-08-31 17:23:49 -07:00
|
|
|
if (async->iosb) async->iosb->status = status;
|
2015-03-06 22:26:51 -07:00
|
|
|
|
2017-04-10 04:21:41 -07:00
|
|
|
if (async->data.apc)
|
2007-03-27 07:42:27 -07:00
|
|
|
{
|
|
|
|
apc_call_t data;
|
2007-12-27 06:50:35 -07:00
|
|
|
memset( &data, 0, sizeof(data) );
|
2021-05-25 04:03:49 -07:00
|
|
|
data.type = APC_USER;
|
|
|
|
data.user.func = async->data.apc;
|
|
|
|
data.user.args[0] = async->data.apc_context;
|
|
|
|
data.user.args[1] = async->data.iosb;
|
|
|
|
data.user.args[2] = 0;
|
2017-11-06 20:10:02 -07:00
|
|
|
thread_queue_apc( NULL, async->thread, NULL, &data );
|
2007-03-27 07:42:27 -07:00
|
|
|
}
|
2019-05-23 10:36:06 -07:00
|
|
|
else if (async->data.apc_context && (async->pending ||
|
2018-10-25 05:56:08 -07:00
|
|
|
!(async->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
|
|
|
|
{
|
2017-06-26 03:19:28 -07:00
|
|
|
add_async_completion( async, async->data.apc_context, status, total );
|
2018-10-25 05:56:08 -07:00
|
|
|
}
|
2017-06-26 03:19:28 -07:00
|
|
|
|
2007-03-21 06:28:23 -07:00
|
|
|
if (async->event) set_event( async->event );
|
2017-06-26 03:19:28 -07:00
|
|
|
else if (async->fd) set_fd_signaled( async->fd, 1 );
|
2017-06-26 03:18:08 -07:00
|
|
|
if (!async->signaled)
|
|
|
|
{
|
|
|
|
async->signaled = 1;
|
|
|
|
wake_up( &async->obj, 0 );
|
|
|
|
}
|
2021-02-08 16:40:42 -07:00
|
|
|
|
|
|
|
if (async->completion_callback)
|
|
|
|
async->completion_callback( async->completion_callback_private );
|
|
|
|
async->completion_callback = NULL;
|
2021-08-31 17:23:47 -07:00
|
|
|
|
|
|
|
async_reselect( async );
|
|
|
|
|
|
|
|
if (async->queue)
|
|
|
|
{
|
|
|
|
async->fd = NULL;
|
|
|
|
list_remove( &async->queue_entry );
|
|
|
|
async->queue = NULL;
|
|
|
|
release_object( async );
|
|
|
|
}
|
2007-03-21 06:28:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-02 11:09:29 -07:00
|
|
|
/* check if an async operation is waiting to be alerted */
|
|
|
|
int async_waiting( struct async_queue *queue )
|
2007-03-20 11:23:59 -07:00
|
|
|
{
|
2007-04-10 08:07:27 -07:00
|
|
|
struct list *ptr;
|
|
|
|
struct async *async;
|
|
|
|
|
|
|
|
if (!(ptr = list_head( &queue->queue ))) return 0;
|
|
|
|
async = LIST_ENTRY( ptr, struct async, queue_entry );
|
2021-08-31 17:23:51 -07:00
|
|
|
return !async->terminated;
|
2007-03-20 11:23:59 -07:00
|
|
|
}
|
|
|
|
|
2016-12-01 04:10:23 -07:00
|
|
|
static int cancel_async( struct process *process, struct object *obj, struct thread *thread, client_ptr_t iosb )
|
2009-08-08 21:08:27 -07:00
|
|
|
{
|
2016-12-01 04:10:23 -07:00
|
|
|
struct async *async;
|
2009-08-08 21:08:27 -07:00
|
|
|
int woken = 0;
|
|
|
|
|
2016-12-01 04:10:23 -07:00
|
|
|
restart:
|
|
|
|
LIST_FOR_EACH_ENTRY( async, &process->asyncs, struct async, process_entry )
|
2009-08-08 21:08:27 -07:00
|
|
|
{
|
2021-08-31 17:23:51 -07:00
|
|
|
if (async->terminated) continue;
|
2021-08-21 12:29:51 -07:00
|
|
|
if ((!obj || (get_fd_user( async->fd ) == obj)) &&
|
2016-12-01 04:10:23 -07:00
|
|
|
(!thread || async->thread == thread) &&
|
|
|
|
(!iosb || async->data.iosb == iosb))
|
2009-08-08 21:08:27 -07:00
|
|
|
{
|
2021-09-03 21:11:38 -07:00
|
|
|
fd_cancel_async( async->fd, async );
|
2009-08-08 21:08:27 -07:00
|
|
|
woken++;
|
2016-12-01 04:10:23 -07:00
|
|
|
goto restart;
|
2009-08-08 21:08:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return woken;
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:10:47 -07:00
|
|
|
void cancel_process_asyncs( struct process *process )
|
|
|
|
{
|
|
|
|
cancel_async( process, NULL, NULL, 0 );
|
|
|
|
}
|
|
|
|
|
2007-04-02 11:09:29 -07:00
|
|
|
/* wake up async operations on the queue */
|
|
|
|
void async_wake_up( struct async_queue *queue, unsigned int status )
|
2007-03-20 11:23:59 -07:00
|
|
|
{
|
|
|
|
struct list *ptr, *next;
|
|
|
|
|
2007-04-02 11:09:29 -07:00
|
|
|
LIST_FOR_EACH_SAFE( ptr, next, &queue->queue )
|
2007-03-20 11:23:59 -07:00
|
|
|
{
|
|
|
|
struct async *async = LIST_ENTRY( ptr, struct async, queue_entry );
|
|
|
|
async_terminate( async, status );
|
2007-04-02 11:09:29 -07:00
|
|
|
if (status == STATUS_ALERTED) break; /* only wake up the first one */
|
2007-03-20 11:23:59 -07:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 04:10:23 -07:00
|
|
|
|
2016-12-04 13:21:25 -07:00
|
|
|
static void iosb_dump( struct object *obj, int verbose );
|
|
|
|
static void iosb_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops iosb_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct iosb), /* size */
|
2021-02-04 05:44:18 -07:00
|
|
|
&no_type, /* type */
|
2016-12-04 13:21:25 -07:00
|
|
|
iosb_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
|
|
|
no_signal, /* signal */
|
|
|
|
no_get_fd, /* get_fd */
|
2021-02-05 04:10:44 -07:00
|
|
|
default_map_access, /* map_access */
|
2016-12-04 13:21:25 -07:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 07:52:31 -07:00
|
|
|
no_get_full_name, /* get_full_name */
|
2016-12-04 13:21:25 -07:00
|
|
|
no_lookup_name, /* lookup_name */
|
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
|
|
|
no_open_file, /* open_file */
|
2019-03-25 06:37:09 -07:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2016-12-04 13:21:25 -07:00
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
iosb_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void iosb_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
assert( obj->ops == &iosb_ops );
|
|
|
|
fprintf( stderr, "I/O status block\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iosb_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct iosb *iosb = (struct iosb *)obj;
|
|
|
|
|
|
|
|
free( iosb->in_data );
|
|
|
|
free( iosb->out_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate iosb struct */
|
2021-09-01 15:28:37 -07:00
|
|
|
static struct iosb *create_iosb( const void *in_data, data_size_t in_size, data_size_t out_size )
|
2016-12-04 13:21:25 -07:00
|
|
|
{
|
|
|
|
struct iosb *iosb;
|
|
|
|
|
|
|
|
if (!(iosb = alloc_object( &iosb_ops ))) return NULL;
|
|
|
|
|
|
|
|
iosb->status = STATUS_PENDING;
|
|
|
|
iosb->result = 0;
|
|
|
|
iosb->in_size = in_size;
|
|
|
|
iosb->in_data = NULL;
|
|
|
|
iosb->out_size = out_size;
|
|
|
|
iosb->out_data = NULL;
|
|
|
|
|
|
|
|
if (in_size && !(iosb->in_data = memdup( in_data, in_size )))
|
|
|
|
{
|
|
|
|
release_object( iosb );
|
|
|
|
iosb = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iosb;
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:28:37 -07:00
|
|
|
/* create an async associated with iosb for async-based requests
|
|
|
|
* returned async must be passed to async_handoff */
|
|
|
|
struct async *create_request_async( struct fd *fd, unsigned int comp_flags, const async_data_t *data )
|
|
|
|
{
|
|
|
|
struct async *async;
|
|
|
|
struct iosb *iosb;
|
|
|
|
|
|
|
|
if (!(iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
async = create_async( fd, current, data, iosb );
|
|
|
|
release_object( iosb );
|
|
|
|
if (async)
|
|
|
|
{
|
|
|
|
if (!(async->wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
|
|
|
|
{
|
|
|
|
release_object( async );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
async->pending = 0;
|
|
|
|
async->direct_result = 1;
|
|
|
|
async->comp_flags = comp_flags;
|
|
|
|
}
|
|
|
|
return async;
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:12:37 -07:00
|
|
|
struct iosb *async_get_iosb( struct async *async )
|
|
|
|
{
|
|
|
|
return async->iosb ? (struct iosb *)grab_object( async->iosb ) : NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-19 09:21:19 -07:00
|
|
|
struct thread *async_get_thread( struct async *async )
|
|
|
|
{
|
|
|
|
return async->thread;
|
|
|
|
}
|
|
|
|
|
2017-03-23 07:41:43 -07:00
|
|
|
/* find the first pending async in queue */
|
|
|
|
struct async *find_pending_async( struct async_queue *queue )
|
|
|
|
{
|
|
|
|
struct async *async;
|
2017-07-04 06:26:26 -07:00
|
|
|
LIST_FOR_EACH_ENTRY( async, &queue->queue, struct async, queue_entry )
|
2021-08-31 17:23:51 -07:00
|
|
|
if (!async->terminated) return (struct async *)grab_object( async );
|
2017-03-23 07:41:43 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:10:23 -07:00
|
|
|
/* cancels all async I/O */
|
|
|
|
DECL_HANDLER(cancel_async)
|
|
|
|
{
|
|
|
|
struct object *obj = get_handle_obj( current->process, req->handle, 0, NULL );
|
|
|
|
struct thread *thread = req->only_thread ? current : NULL;
|
|
|
|
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
int count = cancel_async( current->process, obj, thread, req->iosb );
|
|
|
|
if (!count && req->iosb) set_error( STATUS_NOT_FOUND );
|
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 13:22:05 -07:00
|
|
|
|
|
|
|
/* get async result from associated iosb */
|
|
|
|
DECL_HANDLER(get_async_result)
|
|
|
|
{
|
|
|
|
struct iosb *iosb = NULL;
|
|
|
|
struct async *async;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( async, ¤t->process->asyncs, struct async, process_entry )
|
2017-04-10 04:21:31 -07:00
|
|
|
if (async->data.user == req->user_arg)
|
2016-12-04 13:22:05 -07:00
|
|
|
{
|
|
|
|
iosb = async->iosb;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iosb)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iosb->out_data)
|
|
|
|
{
|
|
|
|
data_size_t size = min( iosb->out_size, get_reply_max_size() );
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
set_reply_data_ptr( iosb->out_data, size );
|
|
|
|
iosb->out_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply->size = iosb->result;
|
|
|
|
set_error( iosb->status );
|
|
|
|
}
|