#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_VFS_H
+/*
+ * Solaris defines its system list in sys/list.h.
+ * This need to be workaround it here.
+ */
+#define list SYSLIST
+#define list_next SYSLIST_NEXT
+#define list_prev SYSLIST_PREV
+#define list_head SYSLIST_HEAD
+#define list_tail SYSLIST_TAIL
+#define list_move_tail SYSLIST_MOVE_TAIL
+#define list_remove SYSLIST_REMOVE
#include <sys/vfs.h>
+#undef list
+#undef list_next
+#undef list_prev
+#undef list_head
+#undef list_tail
+#undef list_move_tail
+#undef list_remove
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
+#ifdef HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif
#ifdef HAVE_SYS_EVENT_H
#include <sys/event.h>
#undef LIST_INIT
#include "request.h"
#include "winternl.h"
+#include "winioctl.h"
#if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
# include <sys/epoll.h>
struct object *user; /* object using this file descriptor */
struct list locks; /* list of locks on this fd */
unsigned int access; /* file access (FILE_READ_DATA etc.) */
+ unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
unsigned int sharing; /* file sharing mode */
int unix_fd; /* unix file descriptor */
+ unsigned int no_fd_status;/* status to return when unix_fd is -1 */
+ int signaled :1; /* is the fd signaled? */
int fs_locks :1; /* can we use filesystem locks for this fd? */
- int unmounted :1;/* has the device been unmounted? */
int poll_index; /* index of fd in poll array */
- struct list read_q; /* async readers of this fd */
- struct list write_q; /* async writers of this fd */
+ struct async_queue *read_q; /* async readers of this fd */
+ struct async_queue *write_q; /* async writers of this fd */
+ struct async_queue *wait_q; /* other async waiters of this fd */
+ struct completion *completion; /* completion object attached to this fd */
+ apc_param_t comp_key; /* completion key to set in completion events */
};
static void fd_dump( struct object *obj, int verbose );
{
sizeof(struct fd), /* size */
fd_dump, /* dump */
+ no_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_signal, /* signal */
no_get_fd, /* get_fd */
no_map_access, /* map_access */
+ default_get_sd, /* get_sd */
+ default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_open_file, /* open_file */
no_close_handle, /* close_handle */
{
sizeof(struct device), /* size */
device_dump, /* dump */
+ no_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_signal, /* signal */
no_get_fd, /* get_fd */
no_map_access, /* map_access */
+ default_get_sd, /* get_sd */
+ default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_open_file, /* open_file */
no_close_handle, /* close_handle */
{
sizeof(struct inode), /* size */
inode_dump, /* dump */
+ no_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_signal, /* signal */
no_get_fd, /* get_fd */
no_map_access, /* map_access */
+ default_get_sd, /* get_sd */
+ default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_open_file, /* open_file */
no_close_handle, /* close_handle */
{
sizeof(struct file_lock), /* size */
file_lock_dump, /* dump */
+ no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
file_lock_signaled, /* signaled */
no_signal, /* signal */
no_get_fd, /* get_fd */
no_map_access, /* map_access */
+ default_get_sd, /* get_sd */
+ default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_open_file, /* open_file */
no_close_handle, /* close_handle */
struct timeout_user
{
struct list entry; /* entry in sorted timeout list */
- struct timeval when; /* timeout expiry (absolute time) */
+ timeout_t when; /* timeout expiry (absolute time) */
timeout_callback callback; /* callback function */
void *private; /* callback private data */
};
static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
-struct timeval current_time;
+timeout_t current_time;
+
+static inline void set_current_time(void)
+{
+ static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
+ struct timeval now;
+ gettimeofday( &now, NULL );
+ current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
+}
/* add a timeout user */
-struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
- void *private )
+struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
{
struct timeout_user *user;
struct list *ptr;
if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
- user->when = *when;
+ user->when = (when > 0) ? when : current_time - when;
user->callback = func;
user->private = private;
LIST_FOR_EACH( ptr, &timeout_list )
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
- if (!time_before( &timeout->when, when )) break;
+ if (timeout->when >= user->when) break;
}
list_add_before( ptr, &user->entry );
return user;
free( user );
}
-/* add a timeout in milliseconds to an absolute time */
-void add_timeout( struct timeval *when, int timeout )
+/* return a text description of a timeout for debugging purposes */
+const char *get_timeout_str( timeout_t timeout )
{
- if (timeout)
+ static char buffer[64];
+ long secs, nsecs;
+
+ if (!timeout) return "0";
+ if (timeout == TIMEOUT_INFINITE) return "infinite";
+
+ if (timeout < 0) /* relative */
{
- long sec = timeout / 1000;
- if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
+ secs = -timeout / TICKS_PER_SEC;
+ nsecs = -timeout % TICKS_PER_SEC;
+ sprintf( buffer, "+%ld.%07ld", secs, nsecs );
+ }
+ else /* absolute */
+ {
+ secs = (timeout - current_time) / TICKS_PER_SEC;
+ nsecs = (timeout - current_time) % TICKS_PER_SEC;
+ if (nsecs < 0)
{
- when->tv_usec -= 1000000;
- when->tv_sec++;
+ nsecs += TICKS_PER_SEC;
+ secs--;
}
- when->tv_sec += sec;
+ if (secs >= 0)
+ sprintf( buffer, "%x%08x (+%ld.%07ld)",
+ (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
+ else
+ sprintf( buffer, "%x%08x (-%ld.%07ld)",
+ (unsigned int)(timeout >> 32), (unsigned int)timeout,
+ -(secs + 1), TICKS_PER_SEC - nsecs );
}
+ return buffer;
}
static int get_next_timeout(void);
+static inline void fd_poll_event( struct fd *fd, int event )
+{
+ fd->fd_ops->poll_event( fd, event );
+}
+
#ifdef USE_EPOLL
static int epoll_fd = -1;
if (epoll_fd == -1) break; /* an error occurred with epoll */
ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
- gettimeofday( ¤t_time, NULL );
+ set_current_time();
/* put the events into the pollfd array first, like poll does */
for (i = 0; i < ret; i++)
static inline void init_epoll(void)
{
-#ifndef __APPLE__ /* kqueue support is broken in the MacOS kernel so we can't use it */
- kqueue_fd = kqueue();
+#ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
+ int mib[2];
+ char release[32];
+ size_t len = sizeof(release);
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_OSRELEASE;
+ if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
+ if (atoi(release) < 9) return;
#endif
+ kqueue_fd = kqueue();
}
static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
}
else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
- gettimeofday( ¤t_time, NULL );
+ set_current_time();
/* put the events into the pollfd array first, like poll does */
for (i = 0; i < ret; i++)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
- if (!time_before( ¤t_time, &timeout->when ))
+ if (timeout->when <= current_time)
{
list_remove( &timeout->entry );
list_add_tail( &expired_list, &timeout->entry );
if ((ptr = list_head( &timeout_list )) != NULL)
{
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
- int diff = (timeout->when.tv_sec - current_time.tv_sec) * 1000
- + (timeout->when.tv_usec - current_time.tv_usec + 999) / 1000;
+ int diff = (timeout->when - current_time + 9999) / 10000;
if (diff < 0) diff = 0;
return diff;
}
{
int i, ret, timeout;
- gettimeofday( ¤t_time, NULL );
+ set_current_time();
+ server_start_time = current_time;
main_loop_epoll();
/* fall through to normal poll loop */
if (!active_users) break; /* last user removed by a timeout */
ret = poll( pollfd, nb_users, timeout );
- gettimeofday( ¤t_time, NULL );
+ set_current_time();
if (ret > 0)
{
struct statfs stfs;
if (fstatfs( unix_fd, &stfs ) == -1) return 0;
- return (!strncmp("cd9660", stfs.f_fstypename, sizeof(stfs.f_fstypename)) ||
- !strncmp("udf", stfs.f_fstypename, sizeof(stfs.f_fstypename)));
+ return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
#elif defined(__NetBSD__)
struct statvfs stfs;
if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
- return (!strncmp("cd9660", stfs.f_fstypename, sizeof(stfs.f_fstypename)) ||
- !strncmp("udf", stfs.f_fstypename, sizeof(stfs.f_fstypename)));
+ return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
#elif defined(sun)
# include <sys/dkio.h>
# include <sys/vtoc.h>
{
struct file_lock *lock;
- if (!fd->inode) /* not a regular file */
- {
- set_error( STATUS_INVALID_HANDLE );
- return NULL;
- }
-
if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
lock->shared = shared;
lock->start = start;
struct list *ptr;
file_pos_t end = start + count;
+ if (!fd->inode) /* not a regular file */
+ {
+ set_error( STATUS_INVALID_DEVICE_REQUEST );
+ return 0;
+ }
+
/* don't allow wrapping locks */
if (end && end < start)
{
static void fd_dump( struct object *obj, int verbose )
{
struct fd *fd = (struct fd *)obj;
- fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
+ fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
fprintf( stderr, "\n" );
}
{
struct fd *fd = (struct fd *)obj;
- async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
- async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
+ free_async_queue( fd->read_q );
+ free_async_queue( fd->write_q );
+ free_async_queue( fd->wait_q );
+ if (fd->completion) release_object( fd->completion );
remove_fd_locks( fd );
list_remove( &fd->inode_entry );
if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
{
assert( fd->inode );
- async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
- async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
+ async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
+ async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
if (fd->poll_index != -1) set_fd_events( fd, -1 );
if (fd->unix_fd != -1) close( fd->unix_fd );
fd->unix_fd = -1;
- fd->unmounted = 1;
+ fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
fd->closed->unix_fd = -1;
fd->closed->unlink[0] = 0;
fd->inode = NULL;
fd->closed = NULL;
fd->access = 0;
+ fd->options = 0;
fd->sharing = 0;
fd->unix_fd = -1;
+ fd->signaled = 1;
fd->fs_locks = 1;
- fd->unmounted = 0;
fd->poll_index = -1;
+ fd->read_q = NULL;
+ fd->write_q = NULL;
+ fd->wait_q = NULL;
+ fd->completion = NULL;
list_init( &fd->inode_entry );
list_init( &fd->locks );
- list_init( &fd->read_q );
- list_init( &fd->write_q );
if ((fd->poll_index = add_poll_user( fd )) == -1)
{
}
/* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
-struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user )
+struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
{
struct fd *fd = alloc_object( &fd_ops );
fd->inode = NULL;
fd->closed = NULL;
fd->access = 0;
+ fd->options = options;
fd->sharing = 0;
fd->unix_fd = -1;
+ fd->signaled = 0;
fd->fs_locks = 0;
- fd->unmounted = 0;
fd->poll_index = -1;
+ fd->read_q = NULL;
+ fd->write_q = NULL;
+ fd->wait_q = NULL;
+ fd->completion = NULL;
+ fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
list_init( &fd->inode_entry );
list_init( &fd->locks );
- list_init( &fd->read_q );
- list_init( &fd->write_q );
return fd;
}
+/* set the status to return when the fd has no associated unix fd */
+void set_no_fd_status( struct fd *fd, unsigned int status )
+{
+ fd->no_fd_status = status;
+}
+
/* check if the desired access is possible without violating */
/* the sharing mode of other opens of the same file */
static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
if (!(fd = alloc_fd_object())) return NULL;
+ fd->options = options;
if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
{
/* if we tried to open a directory for write access, retry read-only */
if (errno != EISDIR ||
!(access & FILE_UNIX_WRITE_ACCESS) ||
- (fd->unix_fd = open( name, O_RDONLY | (flags & ~O_TRUNC), *mode )) == -1)
+ (fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode )) == -1)
{
file_set_error();
goto error;
/* create an fd for an anonymous file */
/* if the function fails the unix fd is closed */
-struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
+struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
+ unsigned int options )
{
struct fd *fd = alloc_fd_object();
{
set_fd_user( fd, fd_user_ops, user );
fd->unix_fd = unix_fd;
+ fd->options = options;
return fd;
}
close( unix_fd );
return fd->user;
}
+/* retrieve the opening options for the fd */
+unsigned int get_fd_options( struct fd *fd )
+{
+ return fd->options;
+}
+
/* retrieve the unix fd for an object */
int get_unix_fd( struct fd *fd )
{
- if (fd->unix_fd == -1)
- {
- if (fd->unmounted) set_error( STATUS_VOLUME_DISMOUNTED );
- else set_error( STATUS_BAD_DEVICE_TYPE );
- }
+ if (fd->unix_fd == -1) set_error( fd->no_fd_status );
return fd->unix_fd;
}
return (fd->inode && fd->inode->device->removable);
}
-/* handler for close_handle that refuses to close fd-associated handles in other processes */
-int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
+/* set or clear the fd signaled state */
+void set_fd_signaled( struct fd *fd, int signaled )
{
- return (!current || current->process == process);
+ fd->signaled = signaled;
+ if (signaled) wake_up( fd->user, 0 );
}
-/* callback for event happening in the main poll() loop */
-void fd_poll_event( struct fd *fd, int event )
+/* set or clear the fd signaled state */
+int is_fd_signaled( struct fd *fd )
+{
+ return fd->signaled;
+}
+
+/* handler for close_handle that refuses to close fd-associated handles in other processes */
+int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
{
- return fd->fd_ops->poll_event( fd, event );
+ return (!current || current->process == process);
}
/* check if events are pending and if yes return which one(s) */
struct pollfd pfd;
if (fd->unix_fd == -1) return POLLERR;
+ if (fd->inode) return events; /* regular files are always signaled */
pfd.fd = fd->unix_fd;
pfd.events = events;
return pfd.revents;
}
-/* default add_queue() routine for objects that poll() on an fd */
-int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
-{
- struct fd *fd = get_obj_fd( obj );
-
- if (!fd) return 0;
- if (!fd->inode && list_empty( &obj->wait_queue )) /* first on the queue */
- set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
- add_queue( obj, entry );
- release_object( fd );
- return 1;
-}
-
-/* default remove_queue() routine for objects that poll() on an fd */
-void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
-{
- struct fd *fd = get_obj_fd( obj );
-
- grab_object( obj );
- remove_queue( obj, entry );
- if (!fd->inode && list_empty( &obj->wait_queue )) /* last on the queue is gone */
- set_fd_events( fd, 0 );
- release_object( obj );
- release_object( fd );
-}
-
/* default signaled() routine for objects that poll() on an fd */
int default_fd_signaled( struct object *obj, struct thread *thread )
{
- int events, ret;
struct fd *fd = get_obj_fd( obj );
-
- if (fd->inode) ret = 1; /* regular files are always signaled */
- else
- {
- events = fd->fd_ops->get_poll_events( fd );
- ret = check_fd_events( fd, events ) != 0;
-
- if (ret)
- {
- /* stop waiting on select() if we are signaled */
- set_fd_events( fd, 0 );
- }
- else if (!list_empty( &obj->wait_queue ))
- {
- /* restart waiting on poll() if we are no longer signaled */
- set_fd_events( fd, events );
- }
- }
+ int ret = fd->signaled;
release_object( fd );
return ret;
}
+/* default map_access() routine for objects that behave like an fd */
+unsigned int default_fd_map_access( struct object *obj, unsigned int access )
+{
+ if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
+ if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
+ if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
+ if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
+ return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
+}
+
int default_fd_get_poll_events( struct fd *fd )
{
int events = 0;
- if (!list_empty( &fd->read_q ))
- events |= POLLIN;
- if (!list_empty( &fd->write_q ))
- events |= POLLOUT;
-
+ if (async_waiting( fd->read_q )) events |= POLLIN;
+ if (async_waiting( fd->write_q )) events |= POLLOUT;
return events;
}
/* default handler for poll() events */
void default_poll_event( struct fd *fd, int event )
{
- if (!list_empty( &fd->read_q ) && (POLLIN & event) )
- {
- async_terminate_head( &fd->read_q, STATUS_ALERTED );
- return;
- }
- if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
- {
- async_terminate_head( &fd->write_q, STATUS_ALERTED );
- return;
- }
+ if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED );
+ if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED );
/* if an error occurred, stop polling this fd to avoid busy-looping */
if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
- wake_up( fd->user, 0 );
+ else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
}
-void fd_queue_async_timeout( struct fd *fd, const async_data_t *data, int type, int count,
- const struct timeval *timeout )
+struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type )
{
- struct list *queue;
- int events, flags;
-
- fd->fd_ops->get_file_info( fd, &flags );
- if (!(flags & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
- {
- set_error( STATUS_INVALID_HANDLE );
- return;
- }
+ struct async_queue *queue;
+ struct async *async;
switch (type)
{
case ASYNC_TYPE_READ:
- queue = &fd->read_q;
+ if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL;
+ queue = fd->read_q;
break;
case ASYNC_TYPE_WRITE:
- queue = &fd->write_q;
+ if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL;
+ queue = fd->write_q;
+ break;
+ case ASYNC_TYPE_WAIT:
+ if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL;
+ queue = fd->wait_q;
break;
default:
- set_error( STATUS_INVALID_PARAMETER );
- return;
+ queue = NULL;
+ assert(0);
}
- if (!create_async( current, timeout, queue, data )) return;
- set_error( STATUS_PENDING );
-
- /* Check if the new pending request can be served immediately */
- events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
- if (events) fd->fd_ops->poll_event( fd, events );
-
- set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
+ if ((async = create_async( current, queue, data )) && type != ASYNC_TYPE_WAIT)
+ {
+ if (!fd->inode)
+ set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
+ else /* regular files are always ready for read and write */
+ async_wake_up( queue, STATUS_ALERTED );
+ }
+ return async;
}
-void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
+void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
{
- fd_queue_async_timeout( fd, data, type, count, NULL );
+ switch (type)
+ {
+ case ASYNC_TYPE_READ:
+ async_wake_up( fd->read_q, status );
+ break;
+ case ASYNC_TYPE_WRITE:
+ async_wake_up( fd->write_q, status );
+ break;
+ case ASYNC_TYPE_WAIT:
+ async_wake_up( fd->wait_q, status );
+ break;
+ default:
+ assert(0);
+ }
}
-void default_fd_cancel_async( struct fd *fd )
+void fd_reselect_async( struct fd *fd, struct async_queue *queue )
{
- async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
- async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
+ fd->fd_ops->reselect_async( fd, queue );
}
-/* default flush() routine */
-int no_flush( struct fd *fd, struct event **event )
+void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
{
- set_error( STATUS_OBJECT_TYPE_MISMATCH );
- return 0;
+ struct async *async;
+
+ if ((async = fd_queue_async( fd, data, type )))
+ {
+ release_object( async );
+ set_error( STATUS_PENDING );
+ }
}
-/* default get_file_info() routine */
-enum server_fd_type no_get_file_info( struct fd *fd, int *flags )
+/* default reselect_async() fd routine */
+void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
{
- *flags = 0;
- return FD_TYPE_INVALID;
+ if (queue != fd->wait_q)
+ {
+ int poll_events = fd->fd_ops->get_poll_events( fd );
+ int events = check_fd_events( fd, poll_events );
+ if (events) fd->fd_ops->poll_event( fd, events );
+ else set_fd_events( fd, poll_events );
+ }
}
-/* default queue_async() routine */
-void no_queue_async( struct fd *fd, const async_data_t *data, int type, int count)
+/* default cancel_async() fd routine */
+void default_fd_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
{
- set_error( STATUS_OBJECT_TYPE_MISMATCH );
+ int n = 0;
+
+ n += async_wake_up_by( fd->read_q, process, thread, iosb, STATUS_CANCELLED );
+ n += async_wake_up_by( fd->write_q, process, thread, iosb, STATUS_CANCELLED );
+ n += async_wake_up_by( fd->wait_q, process, thread, iosb, STATUS_CANCELLED );
+ if (!n && iosb)
+ set_error( STATUS_NOT_FOUND );
}
-/* default cancel_async() routine */
-void no_cancel_async( struct fd *fd )
+/* default flush() routine */
+void no_flush( struct fd *fd, struct event **event )
{
set_error( STATUS_OBJECT_TYPE_MISMATCH );
}
release_object( device );
}
+/* default ioctl() routine */
+obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
+ int blocking, const void *data, data_size_t size )
+{
+ switch(code)
+ {
+ case FSCTL_DISMOUNT_VOLUME:
+ unmount_device( fd );
+ return 0;
+ default:
+ set_error( STATUS_NOT_SUPPORTED );
+ return 0;
+ }
+}
+
/* same as get_handle_obj but retrieve the struct fd associated to the object */
static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
unsigned int access )
return fd;
}
+struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
+{
+ *p_key = fd->comp_key;
+ return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
+}
+
+void fd_copy_completion( struct fd *src, struct fd *dst )
+{
+ assert( !dst->completion );
+ dst->completion = fd_get_completion( src, &dst->comp_key );
+}
+
/* flush a file buffers */
DECL_HANDLER(flush_file)
{
{
struct fd *fd;
- if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
+ if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
{
- reply->type = fd->fd_ops->get_file_info( fd, &reply->flags );
- if (reply->type != FD_TYPE_INVALID)
+ int unix_fd = get_unix_fd( fd );
+ if (unix_fd != -1)
{
- if (is_fd_removable(fd)) reply->flags |= FD_FLAG_REMOVABLE;
- if (!req->cached)
- {
- int unix_fd = get_unix_fd( fd );
- if (unix_fd != -1) send_client_fd( current->process, unix_fd, req->handle );
- }
+ reply->type = fd->fd_ops->get_fd_type( fd );
+ reply->removable = is_fd_removable(fd);
+ reply->options = fd->options;
+ reply->access = get_handle_access( current->process, req->handle );
+ send_client_fd( current->process, unix_fd, req->handle );
}
- else set_error( STATUS_OBJECT_TYPE_MISMATCH );
release_object( fd );
}
}
-/* get ready to unmount a Unix device */
-DECL_HANDLER(unmount_device)
+/* perform an ioctl on a file */
+DECL_HANDLER(ioctl)
{
- struct fd *fd;
+ unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
+ struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
- if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
+ if (fd)
{
- unmount_device( fd );
+ reply->wait = fd->fd_ops->ioctl( fd, req->code, &req->async, req->blocking,
+ get_req_data(), get_req_data_size() );
+ reply->options = fd->options;
release_object( fd );
}
}
/* create / reschedule an async I/O */
DECL_HANDLER(register_async)
{
- struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
+ unsigned int access;
+ struct fd *fd;
- /*
- * The queue_async method must do the following:
- *
- * 1. Get the async_queue for the request of given type.
- * 2. Create a new asynchronous request for the selected queue
- * 3. Carry out any operations necessary to adjust the object's poll events
- * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
- * 4. When the async request is triggered, then send back (with a proper APC)
- * the trigger (STATUS_ALERTED) to the thread that posted the request.
- * See also the implementations in file.c, serial.c, and sock.c.
- */
+ switch(req->type)
+ {
+ case ASYNC_TYPE_READ:
+ access = FILE_READ_DATA;
+ break;
+ case ASYNC_TYPE_WRITE:
+ access = FILE_WRITE_DATA;
+ break;
+ default:
+ set_error( STATUS_INVALID_PARAMETER );
+ return;
+ }
- if (fd)
+ if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
{
- fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
+ if (get_unix_fd( fd ) != -1) fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
release_object( fd );
}
}
DECL_HANDLER(cancel_async)
{
struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
+ struct thread *thread = req->only_thread ? current : NULL;
+
if (fd)
{
- /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
- * NtCancelIoFile() will force the pending APC to be run. Since,
- * Windows only guarantees that the current thread will have no async
- * operation on the current fd when NtCancelIoFile returns, this shall
- * do the work.
- */
- fd->fd_ops->cancel_async( fd );
+ if (get_unix_fd( fd ) != -1) fd->fd_ops->cancel_async( fd, current->process, thread, req->iosb );
release_object( fd );
- }
+ }
+}
+
+/* attach completion object to a fd */
+DECL_HANDLER(set_completion_info)
+{
+ struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
+
+ if (fd)
+ {
+ if (!(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) && !fd->completion)
+ {
+ fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
+ fd->comp_key = req->ckey;
+ }
+ else set_error( STATUS_INVALID_PARAMETER );
+ release_object( fd );
+ }
+}
+
+/* push new completion msg into a completion queue attached to the fd */
+DECL_HANDLER(add_fd_completion)
+{
+ struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
+ if (fd)
+ {
+ if (fd->completion)
+ add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
+ release_object( fd );
+ }
}