2003-01-30 01:26:44 +01:00
|
|
|
/*
|
|
|
|
* Server-side file descriptor management
|
|
|
|
*
|
2003-03-18 06:04:33 +01:00
|
|
|
* Copyright (C) 2000, 2003 Alexandre Julliard
|
2003-01-30 01:26:44 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-01-30 01:26:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
2005-08-03 23:25:10 +02:00
|
|
|
#include "wine/port.h"
|
2003-01-30 01:26:44 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2003-03-18 06:04:33 +01:00
|
|
|
#include <errno.h>
|
2003-03-12 23:38:14 +01:00
|
|
|
#include <fcntl.h>
|
2003-03-18 06:04:33 +01:00
|
|
|
#include <limits.h>
|
2003-02-19 01:33:32 +01:00
|
|
|
#include <signal.h>
|
2004-04-16 06:31:35 +02:00
|
|
|
#include <stdarg.h>
|
2003-01-30 01:26:44 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2005-03-04 13:38:36 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2003-02-19 01:33:32 +01:00
|
|
|
#ifdef HAVE_SYS_POLL_H
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#endif
|
2006-11-01 13:28:05 +01:00
|
|
|
#ifdef HAVE_LINUX_MAJOR_H
|
|
|
|
#include <linux/major.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_VFS_H
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
2006-11-05 06:50:45 +01:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2006-11-01 13:28:05 +01:00
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STATFS_H
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#endif
|
2007-06-15 12:41:43 +02:00
|
|
|
#ifdef HAVE_SYS_SYSCTL_H
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
2006-08-04 22:11:00 +02:00
|
|
|
#ifdef HAVE_SYS_EVENT_H
|
|
|
|
#include <sys/event.h>
|
|
|
|
#undef LIST_INIT
|
|
|
|
#undef LIST_ENTRY
|
|
|
|
#endif
|
2004-09-23 06:48:24 +02:00
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
2003-03-12 23:38:14 +01:00
|
|
|
#include <sys/stat.h>
|
2003-02-19 01:33:32 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2003-01-30 01:26:44 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "object.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "handle.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "request.h"
|
|
|
|
|
2004-04-16 06:31:35 +02:00
|
|
|
#include "winternl.h"
|
2007-04-16 14:54:52 +02:00
|
|
|
#include "winioctl.h"
|
2004-04-16 06:31:35 +02:00
|
|
|
|
2004-09-23 06:48:24 +02:00
|
|
|
#if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
|
2004-10-27 03:03:30 +02:00
|
|
|
# include <sys/epoll.h>
|
2004-09-23 06:48:24 +02:00
|
|
|
# define USE_EPOLL
|
2004-10-27 03:03:30 +02:00
|
|
|
#elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
|
|
|
|
# define USE_EPOLL
|
|
|
|
# define EPOLLIN POLLIN
|
|
|
|
# define EPOLLOUT POLLOUT
|
|
|
|
# define EPOLLERR POLLERR
|
|
|
|
# define EPOLLHUP POLLHUP
|
|
|
|
# define EPOLL_CTL_ADD 1
|
|
|
|
# define EPOLL_CTL_DEL 2
|
|
|
|
# define EPOLL_CTL_MOD 3
|
|
|
|
|
|
|
|
typedef union epoll_data
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
int fd;
|
|
|
|
uint32_t u32;
|
|
|
|
uint64_t u64;
|
|
|
|
} epoll_data_t;
|
|
|
|
|
|
|
|
struct epoll_event
|
|
|
|
{
|
|
|
|
uint32_t events;
|
|
|
|
epoll_data_t data;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SYSCALL_RET(ret) do { \
|
|
|
|
if (ret < 0) { errno = -ret; ret = -1; } \
|
|
|
|
return ret; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
static inline int epoll_create( int size )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
__asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
|
2004-12-02 19:19:25 +01:00
|
|
|
: "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
|
2004-10-27 03:03:30 +02:00
|
|
|
SYSCALL_RET(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
__asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
|
|
|
|
: "=a" (ret)
|
2004-12-02 19:19:25 +01:00
|
|
|
: "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
|
2004-10-27 03:03:30 +02:00
|
|
|
SYSCALL_RET(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
__asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
|
|
|
|
: "=a" (ret)
|
2004-12-02 19:19:25 +01:00
|
|
|
: "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
|
2004-10-27 03:03:30 +02:00
|
|
|
: "memory" );
|
|
|
|
SYSCALL_RET(ret);
|
|
|
|
}
|
|
|
|
#undef SYSCALL_RET
|
|
|
|
|
|
|
|
#endif /* linux && __i386__ && HAVE_STDINT_H */
|
|
|
|
|
2004-09-23 06:48:24 +02:00
|
|
|
|
2003-03-18 06:04:33 +01:00
|
|
|
/* Because of the stupid Posix locking semantics, we need to keep
|
|
|
|
* track of all file descriptors referencing a given file, and not
|
|
|
|
* close a single one until all the locks are gone (sigh).
|
|
|
|
*/
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
/* file descriptor object */
|
|
|
|
|
|
|
|
/* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
|
|
|
|
struct closed_fd
|
|
|
|
{
|
2004-04-02 21:50:49 +02:00
|
|
|
struct list entry; /* entry in inode closed list */
|
2005-07-28 12:48:57 +02:00
|
|
|
int unix_fd; /* the unix file descriptor */
|
2004-04-02 21:50:49 +02:00
|
|
|
char unlink[1]; /* name to unlink on close (if any) */
|
2003-03-12 23:38:14 +01:00
|
|
|
};
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
struct fd
|
|
|
|
{
|
2003-03-12 23:38:14 +01:00
|
|
|
struct object obj; /* object header */
|
|
|
|
const struct fd_ops *fd_ops; /* file descriptor operations */
|
|
|
|
struct inode *inode; /* inode that this fd belongs to */
|
|
|
|
struct list inode_entry; /* entry in inode fd list */
|
|
|
|
struct closed_fd *closed; /* structure to store the unix fd at destroy time */
|
|
|
|
struct object *user; /* object using this file descriptor */
|
2003-03-18 06:04:33 +01:00
|
|
|
struct list locks; /* list of locks on this fd */
|
2005-12-12 16:46:17 +01:00
|
|
|
unsigned int access; /* file access (FILE_READ_DATA etc.) */
|
2007-04-10 22:25:07 +02:00
|
|
|
unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
|
2004-03-27 21:48:42 +01:00
|
|
|
unsigned int sharing; /* file sharing mode */
|
2003-03-12 23:38:14 +01:00
|
|
|
int unix_fd; /* unix file descriptor */
|
2007-04-18 16:05:59 +02:00
|
|
|
unsigned int no_fd_status;/* status to return when unix_fd is -1 */
|
2007-04-04 19:39:29 +02:00
|
|
|
int signaled :1; /* is the fd signaled? */
|
2005-12-12 14:27:45 +01:00
|
|
|
int fs_locks :1; /* can we use filesystem locks for this fd? */
|
2003-03-12 23:38:14 +01:00
|
|
|
int poll_index; /* index of fd in poll array */
|
2007-04-02 20:09:29 +02:00
|
|
|
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 */
|
2003-01-30 01:26:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void fd_dump( struct object *obj, int verbose );
|
|
|
|
static void fd_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops fd_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct fd), /* size */
|
|
|
|
fd_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-02-19 01:33:32 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2005-12-12 14:57:40 +01:00
|
|
|
no_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-01-30 01:26:44 +01:00
|
|
|
fd_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
2005-07-30 21:09:43 +02:00
|
|
|
/* device object */
|
|
|
|
|
|
|
|
#define DEVICE_HASH_SIZE 7
|
|
|
|
#define INODE_HASH_SIZE 17
|
|
|
|
|
|
|
|
struct device
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct list entry; /* entry in device hash list */
|
|
|
|
dev_t dev; /* device number */
|
2005-08-24 20:33:50 +02:00
|
|
|
int removable; /* removable device? (or -1 if unknown) */
|
2005-07-30 21:09:43 +02:00
|
|
|
struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void device_dump( struct object *obj, int verbose );
|
|
|
|
static void device_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops device_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct device), /* size */
|
|
|
|
device_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
|
|
|
no_signal, /* signal */
|
|
|
|
no_get_fd, /* get_fd */
|
2005-12-12 14:57:40 +01:00
|
|
|
no_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2005-07-30 21:09:43 +02:00
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
device_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
/* inode object */
|
|
|
|
|
|
|
|
struct inode
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct list entry; /* inode hash list entry */
|
2005-07-30 21:09:43 +02:00
|
|
|
struct device *device; /* device containing this inode */
|
2003-03-12 23:38:14 +01:00
|
|
|
ino_t ino; /* inode number */
|
|
|
|
struct list open; /* list of open file descriptors */
|
2003-03-18 06:04:33 +01:00
|
|
|
struct list locks; /* list of file locks */
|
2004-04-02 21:50:49 +02:00
|
|
|
struct list closed; /* list of file descriptors to close at destroy time */
|
2003-03-12 23:38:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void inode_dump( struct object *obj, int verbose );
|
|
|
|
static void inode_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops inode_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct inode), /* size */
|
|
|
|
inode_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-03-12 23:38:14 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2005-12-12 14:57:40 +01:00
|
|
|
no_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-03-12 23:38:14 +01:00
|
|
|
inode_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
2003-03-18 06:04:33 +01:00
|
|
|
/* file lock object */
|
|
|
|
|
|
|
|
struct file_lock
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct fd *fd; /* fd owning this lock */
|
|
|
|
struct list fd_entry; /* entry in list of locks on a given fd */
|
|
|
|
struct list inode_entry; /* entry in inode list of locks */
|
|
|
|
int shared; /* shared lock? */
|
|
|
|
file_pos_t start; /* locked region is interval [start;end) */
|
|
|
|
file_pos_t end;
|
|
|
|
struct process *process; /* process owning this lock */
|
|
|
|
struct list proc_entry; /* entry in list of locks owned by the process */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void file_lock_dump( struct object *obj, int verbose );
|
|
|
|
static int file_lock_signaled( struct object *obj, struct thread *thread );
|
|
|
|
|
|
|
|
static const struct object_ops file_lock_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct file_lock), /* size */
|
|
|
|
file_lock_dump, /* dump */
|
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
|
|
|
file_lock_signaled, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-03-18 06:04:33 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2005-12-12 14:57:40 +01:00
|
|
|
no_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-03-18 06:04:33 +01:00
|
|
|
no_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
|
|
|
|
#define FILE_POS_T_MAX (~(file_pos_t)0)
|
|
|
|
|
|
|
|
static file_pos_t max_unix_offset = OFF_T_MAX;
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
#define DUMP_LONG_LONG(val) do { \
|
|
|
|
if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
|
2006-06-15 14:14:31 +02:00
|
|
|
fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
|
2003-03-12 23:38:14 +01:00
|
|
|
else \
|
|
|
|
fprintf( stderr, "%lx", (unsigned long)(val) ); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* timeouts support */
|
|
|
|
|
|
|
|
struct timeout_user
|
|
|
|
{
|
2004-09-08 06:17:31 +02:00
|
|
|
struct list entry; /* entry in sorted timeout list */
|
2007-04-17 20:08:59 +02:00
|
|
|
timeout_t when; /* timeout expiry (absolute time) */
|
2003-02-19 01:33:32 +01:00
|
|
|
timeout_callback callback; /* callback function */
|
|
|
|
void *private; /* callback private data */
|
|
|
|
};
|
|
|
|
|
2004-09-08 06:17:31 +02:00
|
|
|
static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
|
2007-04-17 20:08:59 +02:00
|
|
|
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;
|
|
|
|
}
|
2003-02-19 01:33:32 +01:00
|
|
|
|
|
|
|
/* add a timeout user */
|
2007-04-17 20:08:59 +02:00
|
|
|
struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
|
|
|
struct timeout_user *user;
|
2004-09-08 06:17:31 +02:00
|
|
|
struct list *ptr;
|
2003-02-19 01:33:32 +01:00
|
|
|
|
|
|
|
if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
|
2007-04-17 20:08:59 +02:00
|
|
|
user->when = (when > 0) ? when : current_time - when;
|
2003-02-19 01:33:32 +01:00
|
|
|
user->callback = func;
|
|
|
|
user->private = private;
|
|
|
|
|
|
|
|
/* Now insert it in the linked list */
|
|
|
|
|
2004-09-08 06:17:31 +02:00
|
|
|
LIST_FOR_EACH( ptr, &timeout_list )
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2004-09-08 06:17:31 +02:00
|
|
|
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
|
2007-04-17 20:08:59 +02:00
|
|
|
if (timeout->when >= user->when) break;
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2004-09-08 06:17:31 +02:00
|
|
|
list_add_before( ptr, &user->entry );
|
2003-02-19 01:33:32 +01:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a timeout user */
|
|
|
|
void remove_timeout_user( struct timeout_user *user )
|
|
|
|
{
|
2004-09-08 06:17:31 +02:00
|
|
|
list_remove( &user->entry );
|
2003-02-19 01:33:32 +01:00
|
|
|
free( user );
|
|
|
|
}
|
|
|
|
|
2007-04-17 20:08:59 +02:00
|
|
|
/* return a text description of a timeout for debugging purposes */
|
|
|
|
const char *get_timeout_str( timeout_t timeout )
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2007-04-17 20:08:59 +02:00
|
|
|
static char buffer[64];
|
|
|
|
long secs, nsecs;
|
|
|
|
|
|
|
|
if (!timeout) return "0";
|
|
|
|
if (timeout == TIMEOUT_INFINITE) return "infinite";
|
|
|
|
|
|
|
|
if (timeout < 0) /* relative */
|
|
|
|
{
|
|
|
|
secs = -timeout / TICKS_PER_SEC;
|
|
|
|
nsecs = -timeout % TICKS_PER_SEC;
|
|
|
|
sprintf( buffer, "+%ld.%07ld", secs, nsecs );
|
|
|
|
}
|
|
|
|
else /* absolute */
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2007-04-17 20:08:59 +02:00
|
|
|
secs = (timeout - current_time) / TICKS_PER_SEC;
|
|
|
|
nsecs = (timeout - current_time) % TICKS_PER_SEC;
|
|
|
|
if (nsecs < 0)
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2007-04-17 20:08:59 +02:00
|
|
|
nsecs += TICKS_PER_SEC;
|
|
|
|
secs--;
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2007-04-17 20:08:59 +02:00
|
|
|
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 );
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2007-04-17 20:08:59 +02:00
|
|
|
return buffer;
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* poll support */
|
|
|
|
|
|
|
|
static struct fd **poll_users; /* users array */
|
|
|
|
static struct pollfd *pollfd; /* poll fd array */
|
|
|
|
static int nb_users; /* count of array entries actually in use */
|
|
|
|
static int active_users; /* current number of active users */
|
|
|
|
static int allocated_users; /* count of allocated entries in the array */
|
|
|
|
static struct fd **freelist; /* list of free entries in the array */
|
|
|
|
|
2006-08-03 19:56:55 +02:00
|
|
|
static int get_next_timeout(void);
|
|
|
|
|
2004-09-23 06:48:24 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
|
2006-08-04 22:11:00 +02:00
|
|
|
static int epoll_fd = -1;
|
2006-08-03 19:56:55 +02:00
|
|
|
|
|
|
|
static inline void init_epoll(void)
|
|
|
|
{
|
|
|
|
epoll_fd = epoll_create( 128 );
|
|
|
|
}
|
2004-09-23 06:48:24 +02:00
|
|
|
|
|
|
|
/* set the events that epoll waits for on this fd; helper for set_fd_events */
|
|
|
|
static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
|
|
|
|
{
|
|
|
|
struct epoll_event ev;
|
|
|
|
int ctl;
|
|
|
|
|
|
|
|
if (epoll_fd == -1) return;
|
|
|
|
|
|
|
|
if (events == -1) /* stop waiting on this fd completely */
|
|
|
|
{
|
|
|
|
if (pollfd[user].fd == -1) return; /* already removed */
|
|
|
|
ctl = EPOLL_CTL_DEL;
|
|
|
|
}
|
|
|
|
else if (pollfd[user].fd == -1)
|
|
|
|
{
|
|
|
|
if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
|
|
|
|
ctl = EPOLL_CTL_ADD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pollfd[user].events == events) return; /* nothing to do */
|
|
|
|
ctl = EPOLL_CTL_MOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.events = events;
|
2006-01-27 16:17:51 +01:00
|
|
|
memset(&ev.data, 0, sizeof(ev.data));
|
2004-09-23 06:48:24 +02:00
|
|
|
ev.data.u32 = user;
|
|
|
|
|
|
|
|
if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOMEM) /* not enough memory, give up on epoll */
|
|
|
|
{
|
|
|
|
close( epoll_fd );
|
|
|
|
epoll_fd = -1;
|
|
|
|
}
|
|
|
|
else perror( "epoll_ctl" ); /* should not happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-03 19:56:55 +02:00
|
|
|
static inline void remove_epoll_user( struct fd *fd, int user )
|
|
|
|
{
|
|
|
|
if (epoll_fd == -1) return;
|
2004-09-23 06:48:24 +02:00
|
|
|
|
2006-08-03 19:56:55 +02:00
|
|
|
if (pollfd[user].fd != -1)
|
|
|
|
{
|
|
|
|
struct epoll_event dummy;
|
|
|
|
epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void main_loop_epoll(void)
|
2004-09-23 06:48:24 +02:00
|
|
|
{
|
2006-08-03 19:56:55 +02:00
|
|
|
int i, ret, timeout;
|
|
|
|
struct epoll_event events[128];
|
|
|
|
|
|
|
|
assert( POLLIN == EPOLLIN );
|
|
|
|
assert( POLLOUT == EPOLLOUT );
|
|
|
|
assert( POLLERR == EPOLLERR );
|
|
|
|
assert( POLLHUP == EPOLLHUP );
|
|
|
|
|
|
|
|
if (epoll_fd == -1) return;
|
|
|
|
|
|
|
|
while (active_users)
|
|
|
|
{
|
|
|
|
timeout = get_next_timeout();
|
|
|
|
|
|
|
|
if (!active_users) break; /* last user removed by a timeout */
|
|
|
|
if (epoll_fd == -1) break; /* an error occurred with epoll */
|
|
|
|
|
|
|
|
ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
|
2007-04-17 20:08:59 +02:00
|
|
|
set_current_time();
|
2006-08-03 19:56:55 +02:00
|
|
|
|
|
|
|
/* put the events into the pollfd array first, like poll does */
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
int user = events[i].data.u32;
|
|
|
|
pollfd[user].revents = events[i].events;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read events from the pollfd array, as set_fd_events may modify them */
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
int user = events[i].data.u32;
|
|
|
|
if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
|
|
|
|
}
|
|
|
|
}
|
2004-09-23 06:48:24 +02:00
|
|
|
}
|
|
|
|
|
2006-08-04 22:11:00 +02:00
|
|
|
#elif defined(HAVE_KQUEUE)
|
|
|
|
|
|
|
|
static int kqueue_fd = -1;
|
|
|
|
|
|
|
|
static inline void init_epoll(void)
|
|
|
|
{
|
2007-06-15 12:41:43 +02:00
|
|
|
#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;
|
2006-08-04 22:11:00 +02:00
|
|
|
#endif
|
2007-06-15 12:41:43 +02:00
|
|
|
kqueue_fd = kqueue();
|
2006-08-04 22:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
|
|
|
|
{
|
|
|
|
struct kevent ev[2];
|
|
|
|
|
|
|
|
if (kqueue_fd == -1) return;
|
|
|
|
|
|
|
|
EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user );
|
|
|
|
EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user );
|
|
|
|
|
|
|
|
if (events == -1) /* stop waiting on this fd completely */
|
|
|
|
{
|
|
|
|
if (pollfd[user].fd == -1) return; /* already removed */
|
|
|
|
ev[0].flags |= EV_DELETE;
|
|
|
|
ev[1].flags |= EV_DELETE;
|
|
|
|
}
|
|
|
|
else if (pollfd[user].fd == -1)
|
|
|
|
{
|
|
|
|
if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
|
|
|
|
ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
|
|
|
|
ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pollfd[user].events == events) return; /* nothing to do */
|
|
|
|
ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
|
|
|
|
ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOMEM) /* not enough memory, give up on kqueue */
|
|
|
|
{
|
|
|
|
close( kqueue_fd );
|
|
|
|
kqueue_fd = -1;
|
|
|
|
}
|
|
|
|
else perror( "kevent" ); /* should not happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void remove_epoll_user( struct fd *fd, int user )
|
|
|
|
{
|
|
|
|
if (kqueue_fd == -1) return;
|
|
|
|
|
|
|
|
if (pollfd[user].fd != -1)
|
|
|
|
{
|
|
|
|
struct kevent ev[2];
|
|
|
|
|
|
|
|
EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
|
|
|
|
EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
|
|
|
|
kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void main_loop_epoll(void)
|
|
|
|
{
|
|
|
|
int i, ret, timeout;
|
|
|
|
struct kevent events[128];
|
|
|
|
|
|
|
|
if (kqueue_fd == -1) return;
|
|
|
|
|
|
|
|
while (active_users)
|
|
|
|
{
|
|
|
|
timeout = get_next_timeout();
|
|
|
|
|
|
|
|
if (!active_users) break; /* last user removed by a timeout */
|
|
|
|
if (kqueue_fd == -1) break; /* an error occurred with kqueue */
|
|
|
|
|
|
|
|
if (timeout != -1)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = timeout / 1000;
|
|
|
|
ts.tv_nsec = (timeout % 1000) * 1000000;
|
|
|
|
ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
|
|
|
|
}
|
|
|
|
else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
|
|
|
|
|
2007-04-17 20:08:59 +02:00
|
|
|
set_current_time();
|
2006-08-10 16:42:09 +02:00
|
|
|
|
2006-08-04 22:11:00 +02:00
|
|
|
/* put the events into the pollfd array first, like poll does */
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
long user = (long)events[i].udata;
|
|
|
|
pollfd[user].revents = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
long user = (long)events[i].udata;
|
|
|
|
if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
|
|
|
|
else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
|
|
|
|
if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
|
|
|
|
if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read events from the pollfd array, as set_fd_events may modify them */
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
{
|
|
|
|
long user = (long)events[i].udata;
|
|
|
|
if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
|
|
|
|
pollfd[user].revents = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_KQUEUE */
|
2006-08-03 19:56:55 +02:00
|
|
|
|
|
|
|
static inline void init_epoll(void) { }
|
|
|
|
static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
|
|
|
|
static inline void remove_epoll_user( struct fd *fd, int user ) { }
|
|
|
|
static inline void main_loop_epoll(void) { }
|
|
|
|
|
2004-09-23 06:48:24 +02:00
|
|
|
#endif /* USE_EPOLL */
|
|
|
|
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
/* add a user in the poll array and return its index, or -1 on failure */
|
|
|
|
static int add_poll_user( struct fd *fd )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (freelist)
|
|
|
|
{
|
|
|
|
ret = freelist - poll_users;
|
|
|
|
freelist = (struct fd **)poll_users[ret];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nb_users == allocated_users)
|
|
|
|
{
|
|
|
|
struct fd **newusers;
|
|
|
|
struct pollfd *newpoll;
|
|
|
|
int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
|
|
|
|
if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
|
|
|
|
if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
|
|
|
|
{
|
|
|
|
if (allocated_users)
|
|
|
|
poll_users = newusers;
|
|
|
|
else
|
|
|
|
free( newusers );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
poll_users = newusers;
|
|
|
|
pollfd = newpoll;
|
2006-08-03 19:56:55 +02:00
|
|
|
if (!allocated_users) init_epoll();
|
2003-02-19 01:33:32 +01:00
|
|
|
allocated_users = new_count;
|
|
|
|
}
|
|
|
|
ret = nb_users++;
|
|
|
|
}
|
|
|
|
pollfd[ret].fd = -1;
|
|
|
|
pollfd[ret].events = 0;
|
|
|
|
pollfd[ret].revents = 0;
|
|
|
|
poll_users[ret] = fd;
|
|
|
|
active_users++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a user from the poll list */
|
|
|
|
static void remove_poll_user( struct fd *fd, int user )
|
|
|
|
{
|
|
|
|
assert( user >= 0 );
|
|
|
|
assert( poll_users[user] == fd );
|
2004-09-23 06:48:24 +02:00
|
|
|
|
2006-08-03 19:56:55 +02:00
|
|
|
remove_epoll_user( fd, user );
|
2003-02-19 01:33:32 +01:00
|
|
|
pollfd[user].fd = -1;
|
|
|
|
pollfd[user].events = 0;
|
|
|
|
pollfd[user].revents = 0;
|
|
|
|
poll_users[user] = (struct fd *)freelist;
|
|
|
|
freelist = &poll_users[user];
|
|
|
|
active_users--;
|
|
|
|
}
|
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
/* process pending timeouts and return the time until the next timeout, in milliseconds */
|
|
|
|
static int get_next_timeout(void)
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2004-09-20 21:14:35 +02:00
|
|
|
if (!list_empty( &timeout_list ))
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2004-09-20 21:14:35 +02:00
|
|
|
struct list expired_list, *ptr;
|
2004-09-08 01:28:14 +02:00
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
/* first remove all expired timers from the list */
|
2004-09-08 06:17:31 +02:00
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
list_init( &expired_list );
|
|
|
|
while ((ptr = list_head( &timeout_list )) != NULL)
|
|
|
|
{
|
|
|
|
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
|
2004-09-08 01:28:14 +02:00
|
|
|
|
2007-04-17 20:08:59 +02:00
|
|
|
if (timeout->when <= current_time)
|
2004-09-08 01:28:14 +02:00
|
|
|
{
|
2004-09-08 06:17:31 +02:00
|
|
|
list_remove( &timeout->entry );
|
2004-09-20 21:14:35 +02:00
|
|
|
list_add_tail( &expired_list, &timeout->entry );
|
2004-09-08 01:28:14 +02:00
|
|
|
}
|
2004-09-20 21:14:35 +02:00
|
|
|
else break;
|
|
|
|
}
|
2004-09-08 01:28:14 +02:00
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
/* now call the callback for all the removed timers */
|
2004-09-08 01:28:14 +02:00
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
while ((ptr = list_head( &expired_list )) != NULL)
|
|
|
|
{
|
|
|
|
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
|
|
|
|
list_remove( &timeout->entry );
|
|
|
|
timeout->callback( timeout->private );
|
|
|
|
free( timeout );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ptr = list_head( &timeout_list )) != NULL)
|
|
|
|
{
|
|
|
|
struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
|
2007-04-17 20:08:59 +02:00
|
|
|
int diff = (timeout->when - current_time + 9999) / 10000;
|
2004-09-20 21:14:35 +02:00
|
|
|
if (diff < 0) diff = 0;
|
|
|
|
return diff;
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2004-09-20 21:14:35 +02:00
|
|
|
}
|
|
|
|
return -1; /* no pending timeouts */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* server main poll() loop */
|
|
|
|
void main_loop(void)
|
|
|
|
{
|
|
|
|
int i, ret, timeout;
|
|
|
|
|
2007-04-17 20:08:59 +02:00
|
|
|
set_current_time();
|
|
|
|
server_start_time = current_time;
|
2006-08-10 16:42:09 +02:00
|
|
|
|
2006-08-03 19:56:55 +02:00
|
|
|
main_loop_epoll();
|
2004-09-23 06:48:24 +02:00
|
|
|
/* fall through to normal poll loop */
|
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
while (active_users)
|
|
|
|
{
|
|
|
|
timeout = get_next_timeout();
|
|
|
|
|
|
|
|
if (!active_users) break; /* last user removed by a timeout */
|
2004-09-08 01:28:14 +02:00
|
|
|
|
2004-09-20 21:14:35 +02:00
|
|
|
ret = poll( pollfd, nb_users, timeout );
|
2007-04-17 20:08:59 +02:00
|
|
|
set_current_time();
|
2006-08-10 16:42:09 +02:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < nb_users; i++)
|
|
|
|
{
|
|
|
|
if (pollfd[i].revents)
|
|
|
|
{
|
|
|
|
fd_poll_event( poll_users[i], pollfd[i].revents );
|
|
|
|
if (!--ret) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
|
|
|
|
/****************************************************************/
|
2005-07-30 21:09:43 +02:00
|
|
|
/* device functions */
|
|
|
|
|
|
|
|
static struct list device_hash[DEVICE_HASH_SIZE];
|
|
|
|
|
2006-11-01 13:28:05 +01:00
|
|
|
static int is_device_removable( dev_t dev, int unix_fd )
|
|
|
|
{
|
|
|
|
#if defined(linux) && defined(HAVE_FSTATFS)
|
|
|
|
struct statfs stfs;
|
|
|
|
|
|
|
|
/* check for floppy disk */
|
|
|
|
if (major(dev) == FLOPPY_MAJOR) return 1;
|
|
|
|
|
|
|
|
if (fstatfs( unix_fd, &stfs ) == -1) return 0;
|
|
|
|
return (stfs.f_type == 0x9660 || /* iso9660 */
|
|
|
|
stfs.f_type == 0x9fa1 || /* supermount */
|
|
|
|
stfs.f_type == 0x15013346); /* udf */
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
|
|
|
|
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)));
|
|
|
|
#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)));
|
|
|
|
#elif defined(sun)
|
|
|
|
# include <sys/dkio.h>
|
|
|
|
# include <sys/vtoc.h>
|
|
|
|
struct dk_cinfo dkinf;
|
|
|
|
if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
|
|
|
|
return (dkinf.dki_ctype == DKC_CDROM ||
|
|
|
|
dkinf.dki_ctype == DKC_NCRFLOPPY ||
|
|
|
|
dkinf.dki_ctype == DKC_SMSFLOPPY ||
|
|
|
|
dkinf.dki_ctype == DKC_INTEL82072 ||
|
|
|
|
dkinf.dki_ctype == DKC_INTEL82077);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-07-30 21:09:43 +02:00
|
|
|
/* retrieve the device object for a given fd, creating it if needed */
|
2006-11-01 13:28:05 +01:00
|
|
|
static struct device *get_device( dev_t dev, int unix_fd )
|
2005-07-30 21:09:43 +02:00
|
|
|
{
|
|
|
|
struct device *device;
|
|
|
|
unsigned int i, hash = dev % DEVICE_HASH_SIZE;
|
2003-03-12 23:38:14 +01:00
|
|
|
|
2005-07-30 21:09:43 +02:00
|
|
|
if (device_hash[hash].next)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
|
|
|
|
if (device->dev == dev) return (struct device *)grab_object( device );
|
|
|
|
}
|
|
|
|
else list_init( &device_hash[hash] );
|
|
|
|
|
|
|
|
/* not found, create it */
|
2005-10-27 13:20:50 +02:00
|
|
|
|
2006-11-01 13:28:05 +01:00
|
|
|
if (unix_fd == -1) return NULL;
|
2005-07-30 21:09:43 +02:00
|
|
|
if ((device = alloc_object( &device_ops )))
|
|
|
|
{
|
|
|
|
device->dev = dev;
|
2006-11-01 13:28:05 +01:00
|
|
|
device->removable = is_device_removable( dev, unix_fd );
|
2005-07-30 21:09:43 +02:00
|
|
|
for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
|
|
|
|
list_add_head( &device_hash[hash], &device->entry );
|
|
|
|
}
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct device *device = (struct device *)obj;
|
|
|
|
fprintf( stderr, "Device dev=" );
|
|
|
|
DUMP_LONG_LONG( device->dev );
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
}
|
2003-03-12 23:38:14 +01:00
|
|
|
|
2005-07-30 21:09:43 +02:00
|
|
|
static void device_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct device *device = (struct device *)obj;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < INODE_HASH_SIZE; i++)
|
|
|
|
assert( list_empty(&device->inode_hash[i]) );
|
|
|
|
|
|
|
|
list_remove( &device->entry ); /* remove it from the hash table */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* inode functions */
|
2003-03-12 23:38:14 +01:00
|
|
|
|
2003-03-18 06:04:33 +01:00
|
|
|
/* close all pending file descriptors in the closed list */
|
2005-08-08 17:11:03 +02:00
|
|
|
static void inode_close_pending( struct inode *inode, int keep_unlinks )
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
2004-04-02 21:50:49 +02:00
|
|
|
struct list *ptr = list_head( &inode->closed );
|
|
|
|
|
|
|
|
while (ptr)
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
2004-04-02 21:50:49 +02:00
|
|
|
struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
|
|
|
|
struct list *next = list_next( &inode->closed, ptr );
|
|
|
|
|
2005-07-28 12:48:57 +02:00
|
|
|
if (fd->unix_fd != -1)
|
2004-04-02 21:50:49 +02:00
|
|
|
{
|
2005-07-28 12:48:57 +02:00
|
|
|
close( fd->unix_fd );
|
|
|
|
fd->unix_fd = -1;
|
2004-04-02 21:50:49 +02:00
|
|
|
}
|
2005-08-08 17:11:03 +02:00
|
|
|
if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
|
2004-04-02 21:50:49 +02:00
|
|
|
{
|
|
|
|
list_remove( ptr );
|
|
|
|
free( fd );
|
|
|
|
}
|
|
|
|
ptr = next;
|
2003-03-18 06:04:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
static void inode_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct inode *inode = (struct inode *)obj;
|
2005-07-30 21:09:43 +02:00
|
|
|
fprintf( stderr, "Inode device=%p ino=", inode->device );
|
2003-03-12 23:38:14 +01:00
|
|
|
DUMP_LONG_LONG( inode->ino );
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inode_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct inode *inode = (struct inode *)obj;
|
2004-04-02 21:50:49 +02:00
|
|
|
struct list *ptr;
|
2003-03-12 23:38:14 +01:00
|
|
|
|
2003-03-18 06:04:33 +01:00
|
|
|
assert( list_empty(&inode->open) );
|
|
|
|
assert( list_empty(&inode->locks) );
|
2003-03-12 23:38:14 +01:00
|
|
|
|
|
|
|
list_remove( &inode->entry );
|
2004-04-02 21:50:49 +02:00
|
|
|
|
|
|
|
while ((ptr = list_head( &inode->closed )))
|
|
|
|
{
|
|
|
|
struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
|
|
|
|
list_remove( ptr );
|
2005-07-28 12:48:57 +02:00
|
|
|
if (fd->unix_fd != -1) close( fd->unix_fd );
|
2004-04-02 21:50:49 +02:00
|
|
|
if (fd->unlink[0])
|
|
|
|
{
|
|
|
|
/* make sure it is still the same file */
|
|
|
|
struct stat st;
|
2005-07-30 21:09:43 +02:00
|
|
|
if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
|
2004-04-16 06:31:35 +02:00
|
|
|
{
|
|
|
|
if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
|
|
|
|
else unlink( fd->unlink );
|
|
|
|
}
|
2004-04-02 21:50:49 +02:00
|
|
|
}
|
|
|
|
free( fd );
|
|
|
|
}
|
2005-07-30 21:09:43 +02:00
|
|
|
release_object( inode->device );
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve the inode object for a given fd, creating it if needed */
|
2006-11-01 13:28:05 +01:00
|
|
|
static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
2005-07-30 21:09:43 +02:00
|
|
|
struct device *device;
|
2003-03-12 23:38:14 +01:00
|
|
|
struct inode *inode;
|
2005-07-30 21:09:43 +02:00
|
|
|
unsigned int hash = ino % INODE_HASH_SIZE;
|
2003-03-12 23:38:14 +01:00
|
|
|
|
2006-11-01 13:28:05 +01:00
|
|
|
if (!(device = get_device( dev, unix_fd ))) return NULL;
|
2005-07-30 21:09:43 +02:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
2005-07-30 21:09:43 +02:00
|
|
|
if (inode->ino == ino)
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
2005-07-30 21:09:43 +02:00
|
|
|
release_object( device );
|
|
|
|
return (struct inode *)grab_object( inode );
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found, create it */
|
|
|
|
if ((inode = alloc_object( &inode_ops )))
|
|
|
|
{
|
2005-07-30 21:09:43 +02:00
|
|
|
inode->device = device;
|
2003-03-12 23:38:14 +01:00
|
|
|
inode->ino = ino;
|
|
|
|
list_init( &inode->open );
|
2003-03-18 06:04:33 +01:00
|
|
|
list_init( &inode->locks );
|
2004-04-02 21:50:49 +02:00
|
|
|
list_init( &inode->closed );
|
2005-07-30 21:09:43 +02:00
|
|
|
list_add_head( &device->inode_hash[hash], &inode->entry );
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
2005-07-30 21:09:43 +02:00
|
|
|
else release_object( device );
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2005-07-28 12:48:57 +02:00
|
|
|
/* add fd to the inode list of file descriptors to close */
|
2003-03-12 23:38:14 +01:00
|
|
|
static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
|
|
|
|
{
|
2003-03-18 06:04:33 +01:00
|
|
|
if (!list_empty( &inode->locks ))
|
|
|
|
{
|
2004-04-02 21:50:49 +02:00
|
|
|
list_add_head( &inode->closed, &fd->entry );
|
2003-03-18 06:04:33 +01:00
|
|
|
}
|
2004-04-02 21:50:49 +02:00
|
|
|
else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
|
|
|
|
{
|
2005-07-28 12:48:57 +02:00
|
|
|
if (fd->unix_fd != -1) close( fd->unix_fd );
|
|
|
|
fd->unix_fd = -1;
|
2004-04-02 21:50:49 +02:00
|
|
|
list_add_head( &inode->closed, &fd->entry );
|
|
|
|
}
|
|
|
|
else /* no locks on this inode and no unlink, get rid of the fd */
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
2005-07-28 12:48:57 +02:00
|
|
|
if (fd->unix_fd != -1) close( fd->unix_fd );
|
2003-03-18 06:04:33 +01:00
|
|
|
free( fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* file lock functions */
|
|
|
|
|
|
|
|
static void file_lock_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct file_lock *lock = (struct file_lock *)obj;
|
|
|
|
fprintf( stderr, "Lock %s fd=%p proc=%p start=",
|
|
|
|
lock->shared ? "shared" : "excl", lock->fd, lock->process );
|
|
|
|
DUMP_LONG_LONG( lock->start );
|
|
|
|
fprintf( stderr, " end=" );
|
|
|
|
DUMP_LONG_LONG( lock->end );
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int file_lock_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
|
|
|
struct file_lock *lock = (struct file_lock *)obj;
|
|
|
|
/* lock is signaled if it has lost its owner */
|
|
|
|
return !lock->process;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set (or remove) a Unix lock if possible for the given range */
|
2003-07-03 20:12:02 +02:00
|
|
|
static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
|
|
|
struct flock fl;
|
|
|
|
|
2003-07-03 20:12:02 +02:00
|
|
|
if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
|
2003-03-18 06:04:33 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (start == end) return 1; /* can't set zero-byte lock */
|
|
|
|
if (start > max_unix_offset) return 1; /* ignore it */
|
|
|
|
fl.l_type = type;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = start;
|
|
|
|
if (!end || end > max_unix_offset) fl.l_len = 0;
|
|
|
|
else fl.l_len = end - start;
|
|
|
|
if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
|
|
|
|
|
|
|
|
switch(errno)
|
|
|
|
{
|
2003-07-03 20:12:02 +02:00
|
|
|
case EACCES:
|
|
|
|
/* check whether locks work at all on this file system */
|
|
|
|
if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
|
|
|
|
{
|
|
|
|
set_error( STATUS_FILE_LOCK_CONFLICT );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* fall through */
|
2003-05-11 04:45:33 +02:00
|
|
|
case EIO:
|
|
|
|
case ENOLCK:
|
|
|
|
/* no locking on this fs, just ignore it */
|
2003-07-03 20:12:02 +02:00
|
|
|
fd->fs_locks = 0;
|
2003-05-11 04:45:33 +02:00
|
|
|
return 1;
|
2003-03-18 06:04:33 +01:00
|
|
|
case EAGAIN:
|
|
|
|
set_error( STATUS_FILE_LOCK_CONFLICT );
|
|
|
|
return 0;
|
2003-03-21 22:31:35 +01:00
|
|
|
case EBADF:
|
|
|
|
/* this can happen if we try to set a write lock on a read-only file */
|
|
|
|
/* we just ignore that error */
|
|
|
|
if (fl.l_type == F_WRLCK) return 1;
|
|
|
|
set_error( STATUS_ACCESS_DENIED );
|
|
|
|
return 0;
|
2004-01-02 21:11:35 +01:00
|
|
|
#ifdef EOVERFLOW
|
2003-03-18 06:04:33 +01:00
|
|
|
case EOVERFLOW:
|
2004-01-02 21:11:35 +01:00
|
|
|
#endif
|
2003-05-11 04:45:33 +02:00
|
|
|
case EINVAL:
|
2003-03-18 06:04:33 +01:00
|
|
|
/* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
|
|
|
|
/* in that case we shrink the limit and retry */
|
|
|
|
if (max_unix_offset > INT_MAX)
|
|
|
|
{
|
|
|
|
max_unix_offset = INT_MAX;
|
|
|
|
break; /* retry */
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
file_set_error();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if interval [start;end) overlaps the lock */
|
2007-03-17 11:52:14 +01:00
|
|
|
static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
|
|
|
if (lock->end && start >= lock->end) return 0;
|
|
|
|
if (end && lock->start >= end) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove Unix locks for all bytes in the specified area that are no longer locked */
|
2003-07-03 20:12:02 +02:00
|
|
|
static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
|
2003-03-18 06:04:33 +01:00
|
|
|
{
|
|
|
|
struct hole
|
|
|
|
{
|
|
|
|
struct hole *next;
|
|
|
|
struct hole *prev;
|
|
|
|
file_pos_t start;
|
|
|
|
file_pos_t end;
|
|
|
|
} *first, *cur, *next, *buffer;
|
|
|
|
|
|
|
|
struct list *ptr;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (!fd->inode) return;
|
2003-07-03 20:12:02 +02:00
|
|
|
if (!fd->fs_locks) return;
|
2003-03-18 06:04:33 +01:00
|
|
|
if (start == end || start > max_unix_offset) return;
|
|
|
|
if (!end || end > max_unix_offset) end = max_unix_offset + 1;
|
|
|
|
|
|
|
|
/* count the number of locks overlapping the specified area */
|
|
|
|
|
|
|
|
LIST_FOR_EACH( ptr, &fd->inode->locks )
|
|
|
|
{
|
|
|
|
struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
|
|
|
|
if (lock->start == lock->end) continue;
|
|
|
|
if (lock_overlaps( lock, start, end )) count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count) /* no locks at all, we can unlock everything */
|
|
|
|
{
|
|
|
|
set_unix_lock( fd, start, end, F_UNLCK );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate space for the list of holes */
|
|
|
|
/* max. number of holes is number of locks + 1 */
|
|
|
|
|
|
|
|
if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
|
|
|
|
first = buffer;
|
|
|
|
first->next = NULL;
|
|
|
|
first->prev = NULL;
|
|
|
|
first->start = start;
|
|
|
|
first->end = end;
|
|
|
|
next = first + 1;
|
|
|
|
|
|
|
|
/* build a sorted list of unlocked holes in the specified area */
|
|
|
|
|
|
|
|
LIST_FOR_EACH( ptr, &fd->inode->locks )
|
|
|
|
{
|
|
|
|
struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
|
|
|
|
if (lock->start == lock->end) continue;
|
|
|
|
if (!lock_overlaps( lock, start, end )) continue;
|
|
|
|
|
|
|
|
/* go through all the holes touched by this lock */
|
|
|
|
for (cur = first; cur; cur = cur->next)
|
|
|
|
{
|
|
|
|
if (cur->end <= lock->start) continue; /* hole is before start of lock */
|
|
|
|
if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
|
|
|
|
|
|
|
|
/* now we know that lock is overlapping hole */
|
|
|
|
|
|
|
|
if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
|
|
|
|
{
|
|
|
|
cur->start = lock->end;
|
|
|
|
if (cur->start && cur->start < cur->end) break; /* done with this lock */
|
|
|
|
/* now hole is empty, remove it */
|
|
|
|
if (cur->next) cur->next->prev = cur->prev;
|
|
|
|
if (cur->prev) cur->prev->next = cur->next;
|
|
|
|
else if (!(first = cur->next)) goto done; /* no more holes at all */
|
|
|
|
}
|
|
|
|
else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
|
|
|
|
{
|
|
|
|
cur->end = lock->start;
|
|
|
|
assert( cur->start < cur->end );
|
|
|
|
}
|
|
|
|
else /* lock is in the middle of hole, split hole in two */
|
|
|
|
{
|
|
|
|
next->prev = cur;
|
|
|
|
next->next = cur->next;
|
|
|
|
cur->next = next;
|
|
|
|
next->start = lock->end;
|
|
|
|
next->end = cur->end;
|
|
|
|
cur->end = lock->start;
|
|
|
|
assert( next->start < next->end );
|
|
|
|
assert( cur->end < next->start );
|
|
|
|
next++;
|
|
|
|
break; /* done with this lock */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear Unix locks for all the holes */
|
|
|
|
|
|
|
|
for (cur = first; cur; cur = cur->next)
|
|
|
|
set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
|
|
|
|
|
|
|
|
done:
|
|
|
|
free( buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new lock on a fd */
|
|
|
|
static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
lock->end = end;
|
|
|
|
lock->fd = fd;
|
|
|
|
lock->process = current->process;
|
|
|
|
|
|
|
|
/* now try to set a Unix lock */
|
|
|
|
if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
|
|
|
|
{
|
|
|
|
release_object( lock );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_add_head( &fd->locks, &lock->fd_entry );
|
|
|
|
list_add_head( &fd->inode->locks, &lock->inode_entry );
|
|
|
|
list_add_head( &lock->process->locks, &lock->proc_entry );
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove an existing lock */
|
|
|
|
static void remove_lock( struct file_lock *lock, int remove_unix )
|
|
|
|
{
|
|
|
|
struct inode *inode = lock->fd->inode;
|
|
|
|
|
|
|
|
list_remove( &lock->fd_entry );
|
|
|
|
list_remove( &lock->inode_entry );
|
|
|
|
list_remove( &lock->proc_entry );
|
|
|
|
if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
|
2005-08-08 17:11:03 +02:00
|
|
|
if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
|
2003-03-18 06:04:33 +01:00
|
|
|
lock->process = NULL;
|
|
|
|
wake_up( &lock->obj, 0 );
|
|
|
|
release_object( lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove all locks owned by a given process */
|
|
|
|
void remove_process_locks( struct process *process )
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
while ((ptr = list_head( &process->locks )))
|
|
|
|
{
|
|
|
|
struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
|
|
|
|
remove_lock( lock, 1 ); /* this removes it from the list */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove all locks on a given fd */
|
|
|
|
static void remove_fd_locks( struct fd *fd )
|
|
|
|
{
|
|
|
|
file_pos_t start = FILE_POS_T_MAX, end = 0;
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
while ((ptr = list_head( &fd->locks )))
|
|
|
|
{
|
|
|
|
struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
|
|
|
|
if (lock->start < start) start = lock->start;
|
|
|
|
if (!lock->end || lock->end > end) end = lock->end - 1;
|
|
|
|
remove_lock( lock, 0 );
|
|
|
|
}
|
|
|
|
if (start < end) remove_unix_locks( fd, start, end + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add a lock on an fd */
|
|
|
|
/* returns handle to wait on */
|
|
|
|
obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
file_pos_t end = start + count;
|
|
|
|
|
|
|
|
/* don't allow wrapping locks */
|
|
|
|
if (end && end < start)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if another lock on that file overlaps the area */
|
|
|
|
LIST_FOR_EACH( ptr, &fd->inode->locks )
|
|
|
|
{
|
|
|
|
struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
|
|
|
|
if (!lock_overlaps( lock, start, end )) continue;
|
|
|
|
if (lock->shared && shared) continue;
|
|
|
|
/* found one */
|
|
|
|
if (!wait)
|
|
|
|
{
|
|
|
|
set_error( STATUS_FILE_LOCK_CONFLICT );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found, add it */
|
|
|
|
if (add_lock( fd, shared, start, end )) return 0;
|
|
|
|
if (get_error() == STATUS_FILE_LOCK_CONFLICT)
|
|
|
|
{
|
|
|
|
/* Unix lock conflict -> tell client to wait and retry */
|
|
|
|
if (wait) set_error( STATUS_PENDING );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a lock on an fd */
|
|
|
|
void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
|
|
|
|
{
|
|
|
|
struct list *ptr;
|
|
|
|
file_pos_t end = start + count;
|
|
|
|
|
|
|
|
/* find an existing lock with the exact same parameters */
|
|
|
|
LIST_FOR_EACH( ptr, &fd->locks )
|
|
|
|
{
|
|
|
|
struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
|
|
|
|
if ((lock->start == start) && (lock->end == end))
|
|
|
|
{
|
|
|
|
remove_lock( lock, 1 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_error( STATUS_FILE_LOCK_CONFLICT );
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
/****************************************************************/
|
|
|
|
/* file descriptor functions */
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
static void fd_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct fd *fd = (struct fd *)obj;
|
2007-04-10 22:25:07 +02:00
|
|
|
fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
|
2005-02-03 11:45:18 +01:00
|
|
|
if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
|
|
|
|
fprintf( stderr, "\n" );
|
2003-01-30 01:26:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fd_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct fd *fd = (struct fd *)obj;
|
2003-02-19 01:33:32 +01:00
|
|
|
|
2007-04-03 19:36:07 +02:00
|
|
|
free_async_queue( fd->read_q );
|
|
|
|
free_async_queue( fd->write_q );
|
|
|
|
free_async_queue( fd->wait_q );
|
2005-06-07 22:09:01 +02:00
|
|
|
|
2003-03-18 06:04:33 +01:00
|
|
|
remove_fd_locks( fd );
|
2003-03-12 23:38:14 +01:00
|
|
|
list_remove( &fd->inode_entry );
|
2003-02-19 01:33:32 +01:00
|
|
|
if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
|
2003-03-12 23:38:14 +01:00
|
|
|
if (fd->inode)
|
|
|
|
{
|
|
|
|
inode_add_closed_fd( fd->inode, fd->closed );
|
|
|
|
release_object( fd->inode );
|
|
|
|
}
|
|
|
|
else /* no inode, close it right away */
|
|
|
|
{
|
|
|
|
if (fd->unix_fd != -1) close( fd->unix_fd );
|
|
|
|
}
|
2003-01-30 01:26:44 +01:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
/* set the events that select waits for on this fd */
|
|
|
|
void set_fd_events( struct fd *fd, int events )
|
|
|
|
{
|
|
|
|
int user = fd->poll_index;
|
|
|
|
assert( poll_users[user] == fd );
|
2004-09-23 06:48:24 +02:00
|
|
|
|
|
|
|
set_fd_epoll_events( fd, user, events );
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
if (events == -1) /* stop waiting on this fd completely */
|
|
|
|
{
|
|
|
|
pollfd[user].fd = -1;
|
|
|
|
pollfd[user].events = POLLERR;
|
|
|
|
pollfd[user].revents = 0;
|
|
|
|
}
|
|
|
|
else if (pollfd[user].fd != -1 || !pollfd[user].events)
|
|
|
|
{
|
|
|
|
pollfd[user].fd = fd->unix_fd;
|
|
|
|
pollfd[user].events = events;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
/* prepare an fd for unmounting its corresponding device */
|
|
|
|
static inline void unmount_fd( struct fd *fd )
|
|
|
|
{
|
|
|
|
assert( fd->inode );
|
|
|
|
|
2007-04-02 20:09:29 +02:00
|
|
|
async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
|
|
|
|
async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
|
2005-08-08 17:11:03 +02:00
|
|
|
|
|
|
|
if (fd->poll_index != -1) set_fd_events( fd, -1 );
|
|
|
|
|
|
|
|
if (fd->unix_fd != -1) close( fd->unix_fd );
|
|
|
|
|
|
|
|
fd->unix_fd = -1;
|
2007-04-18 16:05:59 +02:00
|
|
|
fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
|
2005-08-08 17:11:03 +02:00
|
|
|
fd->closed->unix_fd = -1;
|
|
|
|
fd->closed->unlink[0] = 0;
|
|
|
|
|
|
|
|
/* stop using Unix locks on this fd (existing locks have been removed by close) */
|
|
|
|
fd->fs_locks = 0;
|
|
|
|
}
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
/* allocate an fd object, without setting the unix fd yet */
|
2006-01-24 13:30:55 +01:00
|
|
|
static struct fd *alloc_fd_object(void)
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
struct fd *fd = alloc_object( &fd_ops );
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
if (!fd) return NULL;
|
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
fd->fd_ops = NULL;
|
|
|
|
fd->user = NULL;
|
2003-03-12 23:38:14 +01:00
|
|
|
fd->inode = NULL;
|
|
|
|
fd->closed = NULL;
|
2004-03-27 21:48:42 +01:00
|
|
|
fd->access = 0;
|
2007-04-10 22:25:07 +02:00
|
|
|
fd->options = 0;
|
2004-03-27 21:48:42 +01:00
|
|
|
fd->sharing = 0;
|
2003-03-12 23:38:14 +01:00
|
|
|
fd->unix_fd = -1;
|
2007-04-04 19:39:29 +02:00
|
|
|
fd->signaled = 1;
|
2003-07-03 20:12:02 +02:00
|
|
|
fd->fs_locks = 1;
|
2003-02-19 01:33:32 +01:00
|
|
|
fd->poll_index = -1;
|
2007-04-02 20:09:29 +02:00
|
|
|
fd->read_q = NULL;
|
|
|
|
fd->write_q = NULL;
|
|
|
|
fd->wait_q = NULL;
|
2003-03-12 23:38:14 +01:00
|
|
|
list_init( &fd->inode_entry );
|
2003-03-18 06:04:33 +01:00
|
|
|
list_init( &fd->locks );
|
2003-03-12 23:38:14 +01:00
|
|
|
|
|
|
|
if ((fd->poll_index = add_poll_user( fd )) == -1)
|
|
|
|
{
|
|
|
|
release_object( fd );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2005-12-12 14:27:45 +01:00
|
|
|
/* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
|
2007-05-03 16:07:30 +02:00
|
|
|
struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
|
2005-12-12 14:27:45 +01:00
|
|
|
{
|
|
|
|
struct fd *fd = alloc_object( &fd_ops );
|
|
|
|
|
|
|
|
if (!fd) return NULL;
|
|
|
|
|
|
|
|
fd->fd_ops = fd_user_ops;
|
|
|
|
fd->user = user;
|
|
|
|
fd->inode = NULL;
|
|
|
|
fd->closed = NULL;
|
|
|
|
fd->access = 0;
|
2007-05-03 16:07:30 +02:00
|
|
|
fd->options = options;
|
2005-12-12 14:27:45 +01:00
|
|
|
fd->sharing = 0;
|
|
|
|
fd->unix_fd = -1;
|
2007-04-04 19:39:29 +02:00
|
|
|
fd->signaled = 0;
|
2005-12-12 14:27:45 +01:00
|
|
|
fd->fs_locks = 0;
|
|
|
|
fd->poll_index = -1;
|
2007-04-02 20:09:29 +02:00
|
|
|
fd->read_q = NULL;
|
|
|
|
fd->write_q = NULL;
|
|
|
|
fd->wait_q = NULL;
|
2007-04-18 16:05:59 +02:00
|
|
|
fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
|
2005-12-12 14:27:45 +01:00
|
|
|
list_init( &fd->inode_entry );
|
|
|
|
list_init( &fd->locks );
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2007-04-18 16:05:59 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2004-03-27 21:48:42 +01:00
|
|
|
/* 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 )
|
|
|
|
{
|
2004-04-02 21:50:49 +02:00
|
|
|
unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
|
2004-03-27 21:48:42 +01:00
|
|
|
unsigned int existing_access = 0;
|
|
|
|
struct list *ptr;
|
|
|
|
|
|
|
|
/* if access mode is 0, sharing mode is ignored */
|
2004-04-02 21:50:49 +02:00
|
|
|
if (!access) sharing = existing_sharing;
|
2004-03-27 21:48:42 +01:00
|
|
|
fd->access = access;
|
|
|
|
fd->sharing = sharing;
|
|
|
|
|
|
|
|
LIST_FOR_EACH( ptr, &fd->inode->open )
|
|
|
|
{
|
|
|
|
struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
|
|
|
|
if (fd_ptr != fd)
|
|
|
|
{
|
|
|
|
existing_sharing &= fd_ptr->sharing;
|
|
|
|
existing_access |= fd_ptr->access;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0;
|
|
|
|
if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
|
2006-01-27 15:50:38 +01:00
|
|
|
if ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
|
2005-12-12 16:46:17 +01:00
|
|
|
if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0;
|
|
|
|
if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0;
|
2006-01-27 15:50:38 +01:00
|
|
|
if ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)) return 0;
|
2004-03-27 21:48:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
/* sets the user of an fd that previously had no user */
|
|
|
|
void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
|
|
|
|
{
|
|
|
|
assert( fd->fd_ops == NULL );
|
|
|
|
fd->fd_ops = user_ops;
|
|
|
|
fd->user = user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open() wrapper that returns a struct fd with no fd user set */
|
|
|
|
struct fd *open_fd( const char *name, int flags, mode_t *mode, unsigned int access,
|
|
|
|
unsigned int sharing, unsigned int options )
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
struct closed_fd *closed_fd;
|
2006-01-24 13:30:55 +01:00
|
|
|
struct fd *fd;
|
2004-04-16 06:31:35 +02:00
|
|
|
const char *unlink_name = "";
|
2006-01-25 15:06:48 +01:00
|
|
|
int rw_mode;
|
2003-03-12 23:38:14 +01:00
|
|
|
|
2006-01-27 15:50:38 +01:00
|
|
|
if ((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-24 13:30:55 +01:00
|
|
|
if (!(fd = alloc_fd_object())) return NULL;
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2007-04-10 22:25:07 +02:00
|
|
|
fd->options = options;
|
2004-04-16 06:31:35 +02:00
|
|
|
if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
|
2004-04-02 21:50:49 +02:00
|
|
|
if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
|
|
|
release_object( fd );
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-01-25 15:06:48 +01:00
|
|
|
|
2004-04-27 04:27:09 +02:00
|
|
|
/* create the directory if needed */
|
|
|
|
if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
|
|
|
|
{
|
|
|
|
if (mkdir( name, 0777 ) == -1)
|
|
|
|
{
|
|
|
|
if (errno != EEXIST || (flags & O_EXCL))
|
|
|
|
{
|
|
|
|
file_set_error();
|
2006-01-24 13:30:55 +01:00
|
|
|
goto error;
|
2004-04-27 04:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
|
|
|
|
}
|
2006-01-25 15:06:48 +01:00
|
|
|
|
|
|
|
if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
|
|
|
|
{
|
|
|
|
if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
|
|
|
|
else rw_mode = O_WRONLY;
|
|
|
|
}
|
|
|
|
else rw_mode = O_RDONLY;
|
|
|
|
|
|
|
|
if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2006-01-25 15:06:48 +01:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
goto error;
|
|
|
|
}
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2006-01-25 15:06:48 +01:00
|
|
|
|
2005-07-28 12:48:57 +02:00
|
|
|
closed_fd->unix_fd = fd->unix_fd;
|
2004-04-02 21:50:49 +02:00
|
|
|
closed_fd->unlink[0] = 0;
|
2003-03-12 23:38:14 +01:00
|
|
|
fstat( fd->unix_fd, &st );
|
|
|
|
*mode = st.st_mode;
|
|
|
|
|
2004-04-16 06:31:35 +02:00
|
|
|
/* only bother with an inode for normal files and directories */
|
|
|
|
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
2006-11-01 13:28:05 +01:00
|
|
|
struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
|
2003-03-12 23:38:14 +01:00
|
|
|
|
|
|
|
if (!inode)
|
|
|
|
{
|
|
|
|
/* we can close the fd because there are no others open on the same file,
|
|
|
|
* otherwise we wouldn't have failed to allocate a new inode
|
|
|
|
*/
|
2004-04-16 06:31:35 +02:00
|
|
|
goto error;
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
|
|
|
fd->inode = inode;
|
|
|
|
fd->closed = closed_fd;
|
|
|
|
list_add_head( &inode->open, &fd->inode_entry );
|
2004-09-09 02:28:36 +02:00
|
|
|
|
|
|
|
/* check directory options */
|
|
|
|
if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
release_object( fd );
|
|
|
|
set_error( STATUS_NOT_A_DIRECTORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
release_object( fd );
|
|
|
|
set_error( STATUS_FILE_IS_A_DIRECTORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-27 21:48:42 +01:00
|
|
|
if (!check_sharing( fd, access, sharing ))
|
|
|
|
{
|
|
|
|
release_object( fd );
|
|
|
|
set_error( STATUS_SHARING_VIOLATION );
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-04-02 21:50:49 +02:00
|
|
|
strcpy( closed_fd->unlink, unlink_name );
|
2004-05-22 05:15:04 +02:00
|
|
|
if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
2004-09-09 02:28:36 +02:00
|
|
|
else /* special file */
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
2004-09-09 02:28:36 +02:00
|
|
|
if (options & FILE_DIRECTORY_FILE)
|
|
|
|
{
|
|
|
|
set_error( STATUS_NOT_A_DIRECTORY );
|
|
|
|
goto error;
|
|
|
|
}
|
2004-04-02 21:50:49 +02:00
|
|
|
if (unlink_name[0]) /* we can't unlink special files */
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
2004-04-16 06:31:35 +02:00
|
|
|
goto error;
|
2004-04-02 21:50:49 +02:00
|
|
|
}
|
2004-04-16 06:31:35 +02:00
|
|
|
free( closed_fd );
|
2003-03-12 23:38:14 +01:00
|
|
|
}
|
2003-02-19 01:33:32 +01:00
|
|
|
return fd;
|
2004-04-16 06:31:35 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
release_object( fd );
|
|
|
|
free( closed_fd );
|
|
|
|
return NULL;
|
2003-01-30 01:26:44 +01:00
|
|
|
}
|
|
|
|
|
2003-03-12 23:38:14 +01:00
|
|
|
/* create an fd for an anonymous file */
|
|
|
|
/* if the function fails the unix fd is closed */
|
2007-04-10 22:25:07 +02:00
|
|
|
struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
|
|
|
|
unsigned int options )
|
2003-03-12 23:38:14 +01:00
|
|
|
{
|
2006-01-24 13:30:55 +01:00
|
|
|
struct fd *fd = alloc_fd_object();
|
2003-03-12 23:38:14 +01:00
|
|
|
|
|
|
|
if (fd)
|
|
|
|
{
|
2006-01-24 13:30:55 +01:00
|
|
|
set_fd_user( fd, fd_user_ops, user );
|
2003-03-12 23:38:14 +01:00
|
|
|
fd->unix_fd = unix_fd;
|
2007-04-10 22:25:07 +02:00
|
|
|
fd->options = options;
|
2003-03-12 23:38:14 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
close( unix_fd );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
/* retrieve the object that is using an fd */
|
|
|
|
void *get_fd_user( struct fd *fd )
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
return fd->user;
|
|
|
|
}
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2007-04-10 22:25:07 +02:00
|
|
|
/* retrieve the opening options for the fd */
|
|
|
|
unsigned int get_fd_options( struct fd *fd )
|
|
|
|
{
|
|
|
|
return fd->options;
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
/* retrieve the unix fd for an object */
|
|
|
|
int get_unix_fd( struct fd *fd )
|
|
|
|
{
|
2007-04-18 16:05:59 +02:00
|
|
|
if (fd->unix_fd == -1) set_error( fd->no_fd_status );
|
2003-02-19 01:33:32 +01:00
|
|
|
return fd->unix_fd;
|
2003-01-30 01:26:44 +01:00
|
|
|
}
|
|
|
|
|
2004-03-27 21:48:42 +01:00
|
|
|
/* check if two file descriptors point to the same file */
|
|
|
|
int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
|
|
|
|
{
|
|
|
|
return fd1->inode == fd2->inode;
|
|
|
|
}
|
|
|
|
|
2007-01-12 14:42:43 +01:00
|
|
|
/* check if fd is on a removable device */
|
|
|
|
int is_fd_removable( struct fd *fd )
|
|
|
|
{
|
|
|
|
return (fd->inode && fd->inode->device->removable);
|
|
|
|
}
|
|
|
|
|
2007-04-04 19:39:29 +02:00
|
|
|
/* set or clear the fd signaled state */
|
|
|
|
void set_fd_signaled( struct fd *fd, int signaled )
|
|
|
|
{
|
|
|
|
fd->signaled = signaled;
|
|
|
|
if (signaled) wake_up( fd->user, 0 );
|
|
|
|
}
|
|
|
|
|
2006-11-02 20:52:22 +01:00
|
|
|
/* 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 (!current || current->process == process);
|
|
|
|
}
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
/* callback for event happening in the main poll() loop */
|
2003-02-19 01:33:32 +01:00
|
|
|
void fd_poll_event( struct fd *fd, int event )
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
return fd->fd_ops->poll_event( fd, event );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if events are pending and if yes return which one(s) */
|
|
|
|
int check_fd_events( struct fd *fd, int events )
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
if (fd->unix_fd == -1) return POLLERR;
|
2007-04-10 17:07:27 +02:00
|
|
|
if (fd->inode) return events; /* regular files are always signaled */
|
2005-08-08 17:11:03 +02:00
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
pfd.fd = fd->unix_fd;
|
|
|
|
pfd.events = events;
|
|
|
|
if (poll( &pfd, 1, 0 ) <= 0) return 0;
|
|
|
|
return pfd.revents;
|
2003-01-30 01:26:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* default signaled() routine for objects that poll() on an fd */
|
|
|
|
int default_fd_signaled( struct object *obj, struct thread *thread )
|
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
struct fd *fd = get_obj_fd( obj );
|
2007-04-04 19:39:29 +02:00
|
|
|
int ret = fd->signaled;
|
2003-02-19 01:33:32 +01:00
|
|
|
release_object( fd );
|
|
|
|
return ret;
|
2003-01-30 01:26:44 +01:00
|
|
|
}
|
|
|
|
|
2005-06-07 22:09:01 +02:00
|
|
|
int default_fd_get_poll_events( struct fd *fd )
|
|
|
|
{
|
|
|
|
int events = 0;
|
|
|
|
|
2007-04-02 20:09:29 +02:00
|
|
|
if (async_waiting( fd->read_q )) events |= POLLIN;
|
|
|
|
if (async_waiting( fd->write_q )) events |= POLLOUT;
|
2005-06-07 22:09:01 +02:00
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
/* default handler for poll() events */
|
|
|
|
void default_poll_event( struct fd *fd, int event )
|
|
|
|
{
|
2007-04-10 17:07:27 +02:00
|
|
|
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 );
|
2005-06-07 22:09:01 +02:00
|
|
|
|
|
|
|
/* if an error occurred, stop polling this fd to avoid busy-looping */
|
2003-02-19 01:33:32 +01:00
|
|
|
if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
|
2007-04-10 17:07:27 +02:00
|
|
|
else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
|
2003-02-14 21:27:09 +01:00
|
|
|
}
|
|
|
|
|
2007-04-02 20:41:59 +02:00
|
|
|
struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
|
2005-06-07 22:09:01 +02:00
|
|
|
{
|
2007-04-02 20:09:29 +02:00
|
|
|
struct async_queue *queue;
|
2007-04-02 20:41:59 +02:00
|
|
|
struct async *async;
|
2005-06-07 22:09:01 +02:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
2007-04-02 20:41:59 +02:00
|
|
|
if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL;
|
2007-04-02 20:09:29 +02:00
|
|
|
queue = fd->read_q;
|
2005-06-07 22:09:01 +02:00
|
|
|
break;
|
|
|
|
case ASYNC_TYPE_WRITE:
|
2007-04-02 20:41:59 +02:00
|
|
|
if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL;
|
2007-04-02 20:09:29 +02:00
|
|
|
queue = fd->write_q;
|
2005-06-07 22:09:01 +02:00
|
|
|
break;
|
2007-04-02 12:47:52 +02:00
|
|
|
case ASYNC_TYPE_WAIT:
|
2007-04-02 20:41:59 +02:00
|
|
|
if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL;
|
2007-04-02 20:09:29 +02:00
|
|
|
queue = fd->wait_q;
|
2007-04-02 12:47:52 +02:00
|
|
|
break;
|
2005-06-07 22:09:01 +02:00
|
|
|
default:
|
2007-04-20 13:11:54 +02:00
|
|
|
queue = NULL;
|
2007-04-02 20:24:55 +02:00
|
|
|
assert(0);
|
2005-06-07 22:09:01 +02:00
|
|
|
}
|
|
|
|
|
2007-04-18 16:28:01 +02:00
|
|
|
if ((async = create_async( current, queue, data )) && type != ASYNC_TYPE_WAIT)
|
2007-04-02 20:41:59 +02:00
|
|
|
{
|
|
|
|
if (!fd->inode)
|
|
|
|
set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
|
|
|
|
else /* regular files are always ready for read and write */
|
2007-04-18 16:28:01 +02:00
|
|
|
async_wake_up( queue, STATUS_ALERTED );
|
2007-04-02 20:41:59 +02:00
|
|
|
}
|
|
|
|
return async;
|
2005-06-07 22:09:01 +02:00
|
|
|
}
|
|
|
|
|
2007-04-02 20:09:29 +02:00
|
|
|
void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
|
2007-04-02 12:47:52 +02:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
2007-04-02 20:09:29 +02:00
|
|
|
async_wake_up( fd->read_q, status );
|
2007-04-02 12:47:52 +02:00
|
|
|
break;
|
|
|
|
case ASYNC_TYPE_WRITE:
|
2007-04-02 20:09:29 +02:00
|
|
|
async_wake_up( fd->write_q, status );
|
2007-04-02 12:47:52 +02:00
|
|
|
break;
|
|
|
|
case ASYNC_TYPE_WAIT:
|
2007-04-02 20:09:29 +02:00
|
|
|
async_wake_up( fd->wait_q, status );
|
2007-04-02 12:47:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-10 17:07:27 +02:00
|
|
|
void fd_reselect_async( struct fd *fd, struct async_queue *queue )
|
|
|
|
{
|
|
|
|
fd->fd_ops->reselect_async( fd, queue );
|
|
|
|
}
|
|
|
|
|
2007-03-20 20:21:12 +01:00
|
|
|
void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
|
2005-07-14 14:18:05 +02:00
|
|
|
{
|
2007-04-02 20:41:59 +02:00
|
|
|
struct async *async;
|
2007-04-02 12:48:13 +02:00
|
|
|
|
2007-04-02 20:41:59 +02:00
|
|
|
if ((async = fd_queue_async( fd, data, type, count )))
|
|
|
|
{
|
|
|
|
release_object( async );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
}
|
2005-07-14 14:18:05 +02:00
|
|
|
}
|
|
|
|
|
2007-04-10 17:07:27 +02:00
|
|
|
/* default reselect_async() fd routine */
|
|
|
|
void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
|
|
|
|
{
|
|
|
|
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 cancel_async() fd routine */
|
2005-06-07 22:09:01 +02:00
|
|
|
void default_fd_cancel_async( struct fd *fd )
|
|
|
|
{
|
2007-04-02 20:09:29 +02:00
|
|
|
async_wake_up( fd->read_q, STATUS_CANCELLED );
|
|
|
|
async_wake_up( fd->write_q, STATUS_CANCELLED );
|
|
|
|
async_wake_up( fd->wait_q, STATUS_CANCELLED );
|
2005-06-07 22:09:01 +02:00
|
|
|
}
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
/* default flush() routine */
|
2007-03-27 16:51:44 +02:00
|
|
|
void no_flush( struct fd *fd, struct event **event )
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
|
|
|
set_error( STATUS_OBJECT_TYPE_MISMATCH );
|
|
|
|
}
|
|
|
|
|
2006-10-03 14:54:21 +02:00
|
|
|
static inline int is_valid_mounted_device( struct stat *st )
|
|
|
|
{
|
|
|
|
#if defined(linux) || defined(__sun__)
|
|
|
|
return S_ISBLK( st->st_mode );
|
|
|
|
#else
|
|
|
|
/* disks are char devices on *BSD */
|
|
|
|
return S_ISCHR( st->st_mode );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
/* close all Unix file descriptors on a device to allow unmounting it */
|
2005-10-27 13:20:50 +02:00
|
|
|
static void unmount_device( struct fd *device_fd )
|
2005-08-08 17:11:03 +02:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2005-10-27 13:20:50 +02:00
|
|
|
struct stat st;
|
|
|
|
struct device *device;
|
2005-08-08 17:11:03 +02:00
|
|
|
struct inode *inode;
|
|
|
|
struct fd *fd;
|
2005-12-12 14:27:45 +01:00
|
|
|
int unix_fd = get_unix_fd( device_fd );
|
2005-08-08 17:11:03 +02:00
|
|
|
|
2005-12-12 14:27:45 +01:00
|
|
|
if (unix_fd == -1) return;
|
|
|
|
|
2006-10-03 14:54:21 +02:00
|
|
|
if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
|
2005-10-27 13:20:50 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-01 13:28:05 +01:00
|
|
|
if (!(device = get_device( st.st_rdev, -1 ))) return;
|
2005-10-27 13:20:50 +02:00
|
|
|
|
2005-08-08 17:11:03 +02:00
|
|
|
for (i = 0; i < INODE_HASH_SIZE; i++)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
|
|
|
|
{
|
|
|
|
unmount_fd( fd );
|
|
|
|
}
|
|
|
|
inode_close_pending( inode, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* remove it from the hash table */
|
|
|
|
list_remove( &device->entry );
|
|
|
|
list_init( &device->entry );
|
2005-10-27 13:20:50 +02:00
|
|
|
release_object( device );
|
2005-08-08 17:11:03 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 14:54:52 +02:00
|
|
|
/* default ioctl() routine */
|
2007-05-03 17:43:18 +02:00
|
|
|
obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
|
|
|
|
const void *data, data_size_t size )
|
2007-04-16 14:54:52 +02:00
|
|
|
{
|
|
|
|
switch(code)
|
|
|
|
{
|
|
|
|
case FSCTL_DISMOUNT_VOLUME:
|
|
|
|
unmount_device( fd );
|
2007-05-03 17:43:18 +02:00
|
|
|
return 0;
|
2007-04-16 14:54:52 +02:00
|
|
|
default:
|
|
|
|
set_error( STATUS_NOT_SUPPORTED );
|
2007-05-03 17:43:18 +02:00
|
|
|
return 0;
|
2007-04-16 14:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
/* 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 )
|
|
|
|
{
|
|
|
|
struct fd *fd = NULL;
|
|
|
|
struct object *obj;
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
if ((obj = get_handle_obj( process, handle, access, NULL )))
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
2003-04-17 04:14:04 +02:00
|
|
|
fd = get_obj_fd( obj );
|
2003-01-30 01:26:44 +01:00
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flush a file buffers */
|
|
|
|
DECL_HANDLER(flush_file)
|
|
|
|
{
|
|
|
|
struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
|
2003-05-15 06:22:45 +02:00
|
|
|
struct event * event = NULL;
|
2003-01-30 01:26:44 +01:00
|
|
|
|
|
|
|
if (fd)
|
|
|
|
{
|
2003-05-15 06:22:45 +02:00
|
|
|
fd->fd_ops->flush( fd, &event );
|
2005-01-14 20:54:38 +01:00
|
|
|
if ( event )
|
2003-05-15 06:22:45 +02:00
|
|
|
{
|
|
|
|
reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
|
|
|
|
}
|
2003-02-14 21:27:09 +01:00
|
|
|
release_object( fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-12 14:27:45 +01:00
|
|
|
/* open a file object */
|
|
|
|
DECL_HANDLER(open_file_object)
|
|
|
|
{
|
|
|
|
struct unicode_str name;
|
|
|
|
struct directory *root = NULL;
|
2007-03-22 11:52:40 +01:00
|
|
|
struct object *obj, *result;
|
2005-12-12 14:27:45 +01:00
|
|
|
|
|
|
|
get_req_unicode_str( &name );
|
|
|
|
if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((obj = open_object_dir( root, &name, req->attributes, NULL )))
|
|
|
|
{
|
2007-03-22 11:52:40 +01:00
|
|
|
if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
|
2005-12-12 14:27:45 +01:00
|
|
|
{
|
2007-03-22 11:52:40 +01:00
|
|
|
reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
|
|
|
|
release_object( result );
|
2005-12-12 14:27:45 +01:00
|
|
|
}
|
|
|
|
release_object( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (root) release_object( root );
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
/* get a Unix fd to access a file */
|
|
|
|
DECL_HANDLER(get_handle_fd)
|
|
|
|
{
|
|
|
|
struct fd *fd;
|
|
|
|
|
2007-04-10 22:32:46 +02:00
|
|
|
if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
|
2003-02-14 21:27:09 +01:00
|
|
|
{
|
2007-04-18 16:05:59 +02:00
|
|
|
int unix_fd = get_unix_fd( fd );
|
|
|
|
if (unix_fd != -1)
|
2003-02-14 21:27:09 +01:00
|
|
|
{
|
2007-04-18 16:05:59 +02:00
|
|
|
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 );
|
2003-02-14 21:27:09 +01:00
|
|
|
}
|
2003-01-30 01:26:44 +01:00
|
|
|
release_object( fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 14:45:03 +02:00
|
|
|
/* perform an ioctl on a file */
|
|
|
|
DECL_HANDLER(ioctl)
|
|
|
|
{
|
|
|
|
unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
|
|
|
|
struct fd *fd = get_handle_fd_obj( current->process, req->handle, access );
|
|
|
|
|
|
|
|
if (fd)
|
|
|
|
{
|
2007-05-03 17:43:18 +02:00
|
|
|
reply->wait = fd->fd_ops->ioctl( fd, req->code, &req->async,
|
|
|
|
get_req_data(), get_req_data_size() );
|
|
|
|
reply->options = fd->options;
|
2007-04-16 14:45:03 +02:00
|
|
|
release_object( fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-30 01:26:44 +01:00
|
|
|
/* create / reschedule an async I/O */
|
|
|
|
DECL_HANDLER(register_async)
|
|
|
|
{
|
2007-04-02 20:24:55 +02:00
|
|
|
unsigned int access;
|
|
|
|
struct fd *fd;
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2007-04-02 20:24:55 +02:00
|
|
|
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;
|
|
|
|
}
|
2003-01-30 01:26:44 +01:00
|
|
|
|
2007-04-02 20:24:55 +02:00
|
|
|
if ((fd = get_handle_fd_obj( current->process, req->handle, access )))
|
2003-01-30 01:26:44 +01:00
|
|
|
{
|
2007-04-18 16:05:59 +02:00
|
|
|
if (get_unix_fd( fd ) != -1) fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
|
2003-01-30 01:26:44 +01:00
|
|
|
release_object( fd );
|
|
|
|
}
|
|
|
|
}
|
2005-01-14 20:54:38 +01:00
|
|
|
|
|
|
|
/* cancels all async I/O */
|
|
|
|
DECL_HANDLER(cancel_async)
|
|
|
|
{
|
|
|
|
struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
|
2007-04-18 16:05:59 +02:00
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
if (fd)
|
|
|
|
{
|
2007-04-18 16:05:59 +02:00
|
|
|
if (get_unix_fd( fd ) != -1) fd->fd_ops->cancel_async( fd );
|
2005-01-14 20:54:38 +01:00
|
|
|
release_object( fd );
|
2007-04-18 16:05:59 +02:00
|
|
|
}
|
2005-01-14 20:54:38 +01:00
|
|
|
}
|