Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side file descriptor management |
| 3 | * |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000, 2003 Alexandre Julliard |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #include "config.h" |
Alexandre Julliard | 49b7fdc | 2005-08-03 21:25:10 +0000 | [diff] [blame] | 23 | #include "wine/port.h" |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 24 | |
| 25 | #include <assert.h> |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 28 | #include <limits.h> |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 29 | #include <signal.h> |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
Steven Edwards | 5727918 | 2005-03-04 12:38:36 +0000 | [diff] [blame] | 34 | #ifdef HAVE_POLL_H |
| 35 | #include <poll.h> |
| 36 | #endif |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 37 | #ifdef HAVE_SYS_POLL_H |
| 38 | #include <sys/poll.h> |
| 39 | #endif |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 40 | #ifdef HAVE_STDINT_H |
| 41 | #include <stdint.h> |
| 42 | #endif |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 43 | #include <sys/stat.h> |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 44 | #include <sys/time.h> |
| 45 | #include <sys/types.h> |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 46 | #include <unistd.h> |
| 47 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 48 | #include "ntstatus.h" |
| 49 | #define WIN32_NO_STATUS |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 50 | #include "object.h" |
| 51 | #include "file.h" |
| 52 | #include "handle.h" |
| 53 | #include "process.h" |
| 54 | #include "request.h" |
| 55 | |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 56 | #include "winternl.h" |
| 57 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 58 | #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE) |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 59 | # include <sys/epoll.h> |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 60 | # define USE_EPOLL |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 61 | #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H) |
| 62 | # define USE_EPOLL |
| 63 | # define EPOLLIN POLLIN |
| 64 | # define EPOLLOUT POLLOUT |
| 65 | # define EPOLLERR POLLERR |
| 66 | # define EPOLLHUP POLLHUP |
| 67 | # define EPOLL_CTL_ADD 1 |
| 68 | # define EPOLL_CTL_DEL 2 |
| 69 | # define EPOLL_CTL_MOD 3 |
| 70 | |
| 71 | typedef union epoll_data |
| 72 | { |
| 73 | void *ptr; |
| 74 | int fd; |
| 75 | uint32_t u32; |
| 76 | uint64_t u64; |
| 77 | } epoll_data_t; |
| 78 | |
| 79 | struct epoll_event |
| 80 | { |
| 81 | uint32_t events; |
| 82 | epoll_data_t data; |
| 83 | }; |
| 84 | |
| 85 | #define SYSCALL_RET(ret) do { \ |
| 86 | if (ret < 0) { errno = -ret; ret = -1; } \ |
| 87 | return ret; \ |
| 88 | } while(0) |
| 89 | |
| 90 | static inline int epoll_create( int size ) |
| 91 | { |
| 92 | int ret; |
| 93 | __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 94 | : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) ); |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 95 | SYSCALL_RET(ret); |
| 96 | } |
| 97 | |
| 98 | static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event ) |
| 99 | { |
| 100 | int ret; |
| 101 | __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
| 102 | : "=a" (ret) |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 103 | : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) ); |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 104 | SYSCALL_RET(ret); |
| 105 | } |
| 106 | |
| 107 | static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout ) |
| 108 | { |
| 109 | int ret; |
| 110 | __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" |
| 111 | : "=a" (ret) |
Peter Chapman | 505dfde | 2004-12-02 18:19:25 +0000 | [diff] [blame] | 112 | : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout) |
Alexandre Julliard | 626aa33 | 2004-10-27 01:03:30 +0000 | [diff] [blame] | 113 | : "memory" ); |
| 114 | SYSCALL_RET(ret); |
| 115 | } |
| 116 | #undef SYSCALL_RET |
| 117 | |
| 118 | #endif /* linux && __i386__ && HAVE_STDINT_H */ |
| 119 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 120 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 121 | /* Because of the stupid Posix locking semantics, we need to keep |
| 122 | * track of all file descriptors referencing a given file, and not |
| 123 | * close a single one until all the locks are gone (sigh). |
| 124 | */ |
| 125 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 126 | /* file descriptor object */ |
| 127 | |
| 128 | /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */ |
| 129 | struct closed_fd |
| 130 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 131 | struct list entry; /* entry in inode closed list */ |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 132 | int unix_fd; /* the unix file descriptor */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 133 | char unlink[1]; /* name to unlink on close (if any) */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 136 | struct fd |
| 137 | { |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 138 | struct object obj; /* object header */ |
| 139 | const struct fd_ops *fd_ops; /* file descriptor operations */ |
| 140 | struct inode *inode; /* inode that this fd belongs to */ |
| 141 | struct list inode_entry; /* entry in inode fd list */ |
| 142 | struct closed_fd *closed; /* structure to store the unix fd at destroy time */ |
| 143 | struct object *user; /* object using this file descriptor */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 144 | struct list locks; /* list of locks on this fd */ |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 145 | unsigned int access; /* file access (FILE_READ_DATA etc.) */ |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 146 | unsigned int sharing; /* file sharing mode */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 147 | int unix_fd; /* unix file descriptor */ |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 148 | int fs_locks :1; /* can we use filesystem locks for this fd? */ |
| 149 | int unmounted :1;/* has the device been unmounted? */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 150 | int poll_index; /* index of fd in poll array */ |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 151 | struct list read_q; /* async readers of this fd */ |
| 152 | struct list write_q; /* async writers of this fd */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | static void fd_dump( struct object *obj, int verbose ); |
| 156 | static void fd_destroy( struct object *obj ); |
| 157 | |
| 158 | static const struct object_ops fd_ops = |
| 159 | { |
| 160 | sizeof(struct fd), /* size */ |
| 161 | fd_dump, /* dump */ |
| 162 | no_add_queue, /* add_queue */ |
| 163 | NULL, /* remove_queue */ |
| 164 | NULL, /* signaled */ |
| 165 | NULL, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 166 | no_signal, /* signal */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 167 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 168 | no_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 169 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 170 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 171 | fd_destroy /* destroy */ |
| 172 | }; |
| 173 | |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 174 | /* device object */ |
| 175 | |
| 176 | #define DEVICE_HASH_SIZE 7 |
| 177 | #define INODE_HASH_SIZE 17 |
| 178 | |
| 179 | struct device |
| 180 | { |
| 181 | struct object obj; /* object header */ |
| 182 | struct list entry; /* entry in device hash list */ |
| 183 | dev_t dev; /* device number */ |
Alexandre Julliard | f62f6e8 | 2005-08-24 18:33:50 +0000 | [diff] [blame] | 184 | int removable; /* removable device? (or -1 if unknown) */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 185 | struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */ |
| 186 | }; |
| 187 | |
| 188 | static void device_dump( struct object *obj, int verbose ); |
| 189 | static void device_destroy( struct object *obj ); |
| 190 | |
| 191 | static const struct object_ops device_ops = |
| 192 | { |
| 193 | sizeof(struct device), /* size */ |
| 194 | device_dump, /* dump */ |
| 195 | no_add_queue, /* add_queue */ |
| 196 | NULL, /* remove_queue */ |
| 197 | NULL, /* signaled */ |
| 198 | NULL, /* satisfied */ |
| 199 | no_signal, /* signal */ |
| 200 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 201 | no_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 202 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 203 | no_close_handle, /* close_handle */ |
| 204 | device_destroy /* destroy */ |
| 205 | }; |
| 206 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 207 | /* inode object */ |
| 208 | |
| 209 | struct inode |
| 210 | { |
| 211 | struct object obj; /* object header */ |
| 212 | struct list entry; /* inode hash list entry */ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 213 | struct device *device; /* device containing this inode */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 214 | ino_t ino; /* inode number */ |
| 215 | struct list open; /* list of open file descriptors */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 216 | struct list locks; /* list of file locks */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 217 | struct list closed; /* list of file descriptors to close at destroy time */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | static void inode_dump( struct object *obj, int verbose ); |
| 221 | static void inode_destroy( struct object *obj ); |
| 222 | |
| 223 | static const struct object_ops inode_ops = |
| 224 | { |
| 225 | sizeof(struct inode), /* size */ |
| 226 | inode_dump, /* dump */ |
| 227 | no_add_queue, /* add_queue */ |
| 228 | NULL, /* remove_queue */ |
| 229 | NULL, /* signaled */ |
| 230 | NULL, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 231 | no_signal, /* signal */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 232 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 233 | no_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 234 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 235 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 236 | inode_destroy /* destroy */ |
| 237 | }; |
| 238 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 239 | /* file lock object */ |
| 240 | |
| 241 | struct file_lock |
| 242 | { |
| 243 | struct object obj; /* object header */ |
| 244 | struct fd *fd; /* fd owning this lock */ |
| 245 | struct list fd_entry; /* entry in list of locks on a given fd */ |
| 246 | struct list inode_entry; /* entry in inode list of locks */ |
| 247 | int shared; /* shared lock? */ |
| 248 | file_pos_t start; /* locked region is interval [start;end) */ |
| 249 | file_pos_t end; |
| 250 | struct process *process; /* process owning this lock */ |
| 251 | struct list proc_entry; /* entry in list of locks owned by the process */ |
| 252 | }; |
| 253 | |
| 254 | static void file_lock_dump( struct object *obj, int verbose ); |
| 255 | static int file_lock_signaled( struct object *obj, struct thread *thread ); |
| 256 | |
| 257 | static const struct object_ops file_lock_ops = |
| 258 | { |
| 259 | sizeof(struct file_lock), /* size */ |
| 260 | file_lock_dump, /* dump */ |
| 261 | add_queue, /* add_queue */ |
| 262 | remove_queue, /* remove_queue */ |
| 263 | file_lock_signaled, /* signaled */ |
| 264 | no_satisfied, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 265 | no_signal, /* signal */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 266 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 267 | no_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 268 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 269 | no_close_handle, /* close_handle */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 270 | no_destroy /* destroy */ |
| 271 | }; |
| 272 | |
| 273 | |
| 274 | #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1))) |
| 275 | #define FILE_POS_T_MAX (~(file_pos_t)0) |
| 276 | |
| 277 | static file_pos_t max_unix_offset = OFF_T_MAX; |
| 278 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 279 | #define DUMP_LONG_LONG(val) do { \ |
| 280 | if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \ |
| 281 | fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \ |
| 282 | else \ |
| 283 | fprintf( stderr, "%lx", (unsigned long)(val) ); \ |
| 284 | } while (0) |
| 285 | |
| 286 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 287 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 288 | /****************************************************************/ |
| 289 | /* timeouts support */ |
| 290 | |
| 291 | struct timeout_user |
| 292 | { |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 293 | struct list entry; /* entry in sorted timeout list */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 294 | struct timeval when; /* timeout expiry (absolute time) */ |
| 295 | timeout_callback callback; /* callback function */ |
| 296 | void *private; /* callback private data */ |
| 297 | }; |
| 298 | |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 299 | static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 300 | |
| 301 | /* add a timeout user */ |
Eric Pouech | 7001d6e | 2005-03-29 11:40:10 +0000 | [diff] [blame] | 302 | struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func, |
| 303 | void *private ) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 304 | { |
| 305 | struct timeout_user *user; |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 306 | struct list *ptr; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 307 | |
| 308 | if (!(user = mem_alloc( sizeof(*user) ))) return NULL; |
| 309 | user->when = *when; |
| 310 | user->callback = func; |
| 311 | user->private = private; |
| 312 | |
| 313 | /* Now insert it in the linked list */ |
| 314 | |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 315 | LIST_FOR_EACH( ptr, &timeout_list ) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 316 | { |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 317 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
| 318 | if (!time_before( &timeout->when, when )) break; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 319 | } |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 320 | list_add_before( ptr, &user->entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 321 | return user; |
| 322 | } |
| 323 | |
| 324 | /* remove a timeout user */ |
| 325 | void remove_timeout_user( struct timeout_user *user ) |
| 326 | { |
Alexandre Julliard | 15e12da | 2004-09-08 04:17:31 +0000 | [diff] [blame] | 327 | list_remove( &user->entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 328 | free( user ); |
| 329 | } |
| 330 | |
| 331 | /* add a timeout in milliseconds to an absolute time */ |
| 332 | void add_timeout( struct timeval *when, int timeout ) |
| 333 | { |
| 334 | if (timeout) |
| 335 | { |
| 336 | long sec = timeout / 1000; |
| 337 | if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000) |
| 338 | { |
| 339 | when->tv_usec -= 1000000; |
| 340 | when->tv_sec++; |
| 341 | } |
| 342 | when->tv_sec += sec; |
| 343 | } |
| 344 | } |
| 345 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 346 | |
| 347 | /****************************************************************/ |
| 348 | /* poll support */ |
| 349 | |
| 350 | static struct fd **poll_users; /* users array */ |
| 351 | static struct pollfd *pollfd; /* poll fd array */ |
| 352 | static int nb_users; /* count of array entries actually in use */ |
| 353 | static int active_users; /* current number of active users */ |
| 354 | static int allocated_users; /* count of allocated entries in the array */ |
| 355 | static struct fd **freelist; /* list of free entries in the array */ |
| 356 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 357 | #ifdef USE_EPOLL |
| 358 | |
| 359 | static int epoll_fd; |
| 360 | static struct epoll_event *epoll_events; |
| 361 | |
| 362 | /* set the events that epoll waits for on this fd; helper for set_fd_events */ |
| 363 | static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) |
| 364 | { |
| 365 | struct epoll_event ev; |
| 366 | int ctl; |
| 367 | |
| 368 | if (epoll_fd == -1) return; |
| 369 | |
| 370 | if (events == -1) /* stop waiting on this fd completely */ |
| 371 | { |
| 372 | if (pollfd[user].fd == -1) return; /* already removed */ |
| 373 | ctl = EPOLL_CTL_DEL; |
| 374 | } |
| 375 | else if (pollfd[user].fd == -1) |
| 376 | { |
| 377 | if (pollfd[user].events) return; /* stopped waiting on it, don't restart */ |
| 378 | ctl = EPOLL_CTL_ADD; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | if (pollfd[user].events == events) return; /* nothing to do */ |
| 383 | ctl = EPOLL_CTL_MOD; |
| 384 | } |
| 385 | |
| 386 | ev.events = events; |
Eric Pouech | 2e0b533 | 2006-01-27 16:17:51 +0100 | [diff] [blame] | 387 | memset(&ev.data, 0, sizeof(ev.data)); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 388 | ev.data.u32 = user; |
| 389 | |
| 390 | if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1) |
| 391 | { |
| 392 | if (errno == ENOMEM) /* not enough memory, give up on epoll */ |
| 393 | { |
| 394 | close( epoll_fd ); |
| 395 | epoll_fd = -1; |
| 396 | } |
| 397 | else perror( "epoll_ctl" ); /* should not happen */ |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | #else /* USE_EPOLL */ |
| 402 | |
| 403 | static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) |
| 404 | { |
| 405 | } |
| 406 | |
| 407 | #endif /* USE_EPOLL */ |
| 408 | |
| 409 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 410 | /* add a user in the poll array and return its index, or -1 on failure */ |
| 411 | static int add_poll_user( struct fd *fd ) |
| 412 | { |
| 413 | int ret; |
| 414 | if (freelist) |
| 415 | { |
| 416 | ret = freelist - poll_users; |
| 417 | freelist = (struct fd **)poll_users[ret]; |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | if (nb_users == allocated_users) |
| 422 | { |
| 423 | struct fd **newusers; |
| 424 | struct pollfd *newpoll; |
| 425 | int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16; |
| 426 | if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1; |
| 427 | if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) ))) |
| 428 | { |
| 429 | if (allocated_users) |
| 430 | poll_users = newusers; |
| 431 | else |
| 432 | free( newusers ); |
| 433 | return -1; |
| 434 | } |
| 435 | poll_users = newusers; |
| 436 | pollfd = newpoll; |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 437 | #ifdef USE_EPOLL |
| 438 | if (!allocated_users) epoll_fd = epoll_create( new_count ); |
| 439 | if (epoll_fd != -1) |
| 440 | { |
| 441 | struct epoll_event *new_events; |
| 442 | if (!(new_events = realloc( epoll_events, new_count * sizeof(*epoll_events) ))) |
| 443 | return -1; |
| 444 | epoll_events = new_events; |
| 445 | } |
| 446 | #endif |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 447 | allocated_users = new_count; |
| 448 | } |
| 449 | ret = nb_users++; |
| 450 | } |
| 451 | pollfd[ret].fd = -1; |
| 452 | pollfd[ret].events = 0; |
| 453 | pollfd[ret].revents = 0; |
| 454 | poll_users[ret] = fd; |
| 455 | active_users++; |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | /* remove a user from the poll list */ |
| 460 | static void remove_poll_user( struct fd *fd, int user ) |
| 461 | { |
| 462 | assert( user >= 0 ); |
| 463 | assert( poll_users[user] == fd ); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 464 | |
| 465 | #ifdef USE_EPOLL |
| 466 | if (epoll_fd != -1 && pollfd[user].fd != -1) |
| 467 | { |
| 468 | struct epoll_event dummy; |
| 469 | epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy ); |
| 470 | } |
| 471 | #endif |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 472 | pollfd[user].fd = -1; |
| 473 | pollfd[user].events = 0; |
| 474 | pollfd[user].revents = 0; |
| 475 | poll_users[user] = (struct fd *)freelist; |
| 476 | freelist = &poll_users[user]; |
| 477 | active_users--; |
| 478 | } |
| 479 | |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 480 | /* process pending timeouts and return the time until the next timeout, in milliseconds */ |
| 481 | static int get_next_timeout(void) |
| 482 | { |
| 483 | if (!list_empty( &timeout_list )) |
| 484 | { |
| 485 | struct list expired_list, *ptr; |
| 486 | struct timeval now; |
| 487 | |
| 488 | gettimeofday( &now, NULL ); |
| 489 | |
| 490 | /* first remove all expired timers from the list */ |
| 491 | |
| 492 | list_init( &expired_list ); |
| 493 | while ((ptr = list_head( &timeout_list )) != NULL) |
| 494 | { |
| 495 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
| 496 | |
| 497 | if (!time_before( &now, &timeout->when )) |
| 498 | { |
| 499 | list_remove( &timeout->entry ); |
| 500 | list_add_tail( &expired_list, &timeout->entry ); |
| 501 | } |
| 502 | else break; |
| 503 | } |
| 504 | |
| 505 | /* now call the callback for all the removed timers */ |
| 506 | |
| 507 | while ((ptr = list_head( &expired_list )) != NULL) |
| 508 | { |
| 509 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
| 510 | list_remove( &timeout->entry ); |
| 511 | timeout->callback( timeout->private ); |
| 512 | free( timeout ); |
| 513 | } |
| 514 | |
| 515 | if ((ptr = list_head( &timeout_list )) != NULL) |
| 516 | { |
| 517 | struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry ); |
| 518 | int diff = (timeout->when.tv_sec - now.tv_sec) * 1000 |
| 519 | + (timeout->when.tv_usec - now.tv_usec) / 1000; |
| 520 | if (diff < 0) diff = 0; |
| 521 | return diff; |
| 522 | } |
| 523 | } |
| 524 | return -1; /* no pending timeouts */ |
| 525 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 526 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 527 | /* server main poll() loop */ |
| 528 | void main_loop(void) |
| 529 | { |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 530 | int i, ret, timeout; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 531 | |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 532 | #ifdef USE_EPOLL |
| 533 | assert( POLLIN == EPOLLIN ); |
| 534 | assert( POLLOUT == EPOLLOUT ); |
| 535 | assert( POLLERR == EPOLLERR ); |
| 536 | assert( POLLHUP == EPOLLHUP ); |
| 537 | |
| 538 | if (epoll_fd != -1) |
| 539 | { |
| 540 | while (active_users) |
| 541 | { |
| 542 | timeout = get_next_timeout(); |
| 543 | |
| 544 | if (!active_users) break; /* last user removed by a timeout */ |
| 545 | if (epoll_fd == -1) break; /* an error occurred with epoll */ |
| 546 | |
| 547 | ret = epoll_wait( epoll_fd, epoll_events, allocated_users, timeout ); |
| 548 | |
| 549 | /* put the events into the pollfd array first, like poll does */ |
| 550 | for (i = 0; i < ret; i++) |
| 551 | { |
| 552 | int user = epoll_events[i].data.u32; |
| 553 | pollfd[user].revents = epoll_events[i].events; |
| 554 | } |
| 555 | |
| 556 | /* read events from the pollfd array, as set_fd_events may modify them */ |
| 557 | for (i = 0; i < ret; i++) |
| 558 | { |
| 559 | int user = epoll_events[i].data.u32; |
| 560 | if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents ); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | /* fall through to normal poll loop */ |
| 565 | #endif /* USE_EPOLL */ |
| 566 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 567 | while (active_users) |
| 568 | { |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 569 | timeout = get_next_timeout(); |
Alexandre Julliard | 0a6af13 | 2004-09-07 23:28:14 +0000 | [diff] [blame] | 570 | |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 571 | if (!active_users) break; /* last user removed by a timeout */ |
Alexandre Julliard | 0a6af13 | 2004-09-07 23:28:14 +0000 | [diff] [blame] | 572 | |
Alexandre Julliard | 4949a71 | 2004-09-20 19:14:35 +0000 | [diff] [blame] | 573 | ret = poll( pollfd, nb_users, timeout ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 574 | if (ret > 0) |
| 575 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 576 | for (i = 0; i < nb_users; i++) |
| 577 | { |
| 578 | if (pollfd[i].revents) |
| 579 | { |
| 580 | fd_poll_event( poll_users[i], pollfd[i].revents ); |
| 581 | if (!--ret) break; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 588 | |
| 589 | /****************************************************************/ |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 590 | /* device functions */ |
| 591 | |
| 592 | static struct list device_hash[DEVICE_HASH_SIZE]; |
| 593 | |
| 594 | /* retrieve the device object for a given fd, creating it if needed */ |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 595 | static struct device *get_device( dev_t dev, int create ) |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 596 | { |
| 597 | struct device *device; |
| 598 | unsigned int i, hash = dev % DEVICE_HASH_SIZE; |
| 599 | |
| 600 | if (device_hash[hash].next) |
| 601 | { |
| 602 | LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry ) |
| 603 | if (device->dev == dev) return (struct device *)grab_object( device ); |
| 604 | } |
| 605 | else list_init( &device_hash[hash] ); |
| 606 | |
| 607 | /* not found, create it */ |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 608 | |
| 609 | if (!create) return NULL; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 610 | if ((device = alloc_object( &device_ops ))) |
| 611 | { |
| 612 | device->dev = dev; |
Alexandre Julliard | f62f6e8 | 2005-08-24 18:33:50 +0000 | [diff] [blame] | 613 | device->removable = -1; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 614 | for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] ); |
| 615 | list_add_head( &device_hash[hash], &device->entry ); |
| 616 | } |
| 617 | return device; |
| 618 | } |
| 619 | |
| 620 | static void device_dump( struct object *obj, int verbose ) |
| 621 | { |
| 622 | struct device *device = (struct device *)obj; |
| 623 | fprintf( stderr, "Device dev=" ); |
| 624 | DUMP_LONG_LONG( device->dev ); |
| 625 | fprintf( stderr, "\n" ); |
| 626 | } |
| 627 | |
| 628 | static void device_destroy( struct object *obj ) |
| 629 | { |
| 630 | struct device *device = (struct device *)obj; |
| 631 | unsigned int i; |
| 632 | |
| 633 | for (i = 0; i < INODE_HASH_SIZE; i++) |
| 634 | assert( list_empty(&device->inode_hash[i]) ); |
| 635 | |
| 636 | list_remove( &device->entry ); /* remove it from the hash table */ |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /****************************************************************/ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 641 | /* inode functions */ |
| 642 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 643 | /* close all pending file descriptors in the closed list */ |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 644 | static void inode_close_pending( struct inode *inode, int keep_unlinks ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 645 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 646 | struct list *ptr = list_head( &inode->closed ); |
| 647 | |
| 648 | while (ptr) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 649 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 650 | struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry ); |
| 651 | struct list *next = list_next( &inode->closed, ptr ); |
| 652 | |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 653 | if (fd->unix_fd != -1) |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 654 | { |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 655 | close( fd->unix_fd ); |
| 656 | fd->unix_fd = -1; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 657 | } |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 658 | if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 659 | { |
| 660 | list_remove( ptr ); |
| 661 | free( fd ); |
| 662 | } |
| 663 | ptr = next; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 667 | static void inode_dump( struct object *obj, int verbose ) |
| 668 | { |
| 669 | struct inode *inode = (struct inode *)obj; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 670 | fprintf( stderr, "Inode device=%p ino=", inode->device ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 671 | DUMP_LONG_LONG( inode->ino ); |
| 672 | fprintf( stderr, "\n" ); |
| 673 | } |
| 674 | |
| 675 | static void inode_destroy( struct object *obj ) |
| 676 | { |
| 677 | struct inode *inode = (struct inode *)obj; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 678 | struct list *ptr; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 679 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 680 | assert( list_empty(&inode->open) ); |
| 681 | assert( list_empty(&inode->locks) ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 682 | |
| 683 | list_remove( &inode->entry ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 684 | |
| 685 | while ((ptr = list_head( &inode->closed ))) |
| 686 | { |
| 687 | struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry ); |
| 688 | list_remove( ptr ); |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 689 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 690 | if (fd->unlink[0]) |
| 691 | { |
| 692 | /* make sure it is still the same file */ |
| 693 | struct stat st; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 694 | if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino) |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 695 | { |
| 696 | if (S_ISDIR(st.st_mode)) rmdir( fd->unlink ); |
| 697 | else unlink( fd->unlink ); |
| 698 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 699 | } |
| 700 | free( fd ); |
| 701 | } |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 702 | release_object( inode->device ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* retrieve the inode object for a given fd, creating it if needed */ |
| 706 | static struct inode *get_inode( dev_t dev, ino_t ino ) |
| 707 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 708 | struct device *device; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 709 | struct inode *inode; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 710 | unsigned int hash = ino % INODE_HASH_SIZE; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 711 | |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 712 | if (!(device = get_device( dev, 1 ))) return NULL; |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 713 | |
| 714 | LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry ) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 715 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 716 | if (inode->ino == ino) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 717 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 718 | release_object( device ); |
| 719 | return (struct inode *)grab_object( inode ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 722 | |
| 723 | /* not found, create it */ |
| 724 | if ((inode = alloc_object( &inode_ops ))) |
| 725 | { |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 726 | inode->device = device; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 727 | inode->ino = ino; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 728 | list_init( &inode->open ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 729 | list_init( &inode->locks ); |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 730 | list_init( &inode->closed ); |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 731 | list_add_head( &device->inode_hash[hash], &inode->entry ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 732 | } |
Alexandre Julliard | d432515 | 2005-07-30 19:09:43 +0000 | [diff] [blame] | 733 | else release_object( device ); |
| 734 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 735 | return inode; |
| 736 | } |
| 737 | |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 738 | /* add fd to the inode list of file descriptors to close */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 739 | static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd ) |
| 740 | { |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 741 | if (!list_empty( &inode->locks )) |
| 742 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 743 | list_add_head( &inode->closed, &fd->entry ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 744 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 745 | else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */ |
| 746 | { |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 747 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
| 748 | fd->unix_fd = -1; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 749 | list_add_head( &inode->closed, &fd->entry ); |
| 750 | } |
| 751 | else /* no locks on this inode and no unlink, get rid of the fd */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 752 | { |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 753 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 754 | free( fd ); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | |
| 759 | /****************************************************************/ |
| 760 | /* file lock functions */ |
| 761 | |
| 762 | static void file_lock_dump( struct object *obj, int verbose ) |
| 763 | { |
| 764 | struct file_lock *lock = (struct file_lock *)obj; |
| 765 | fprintf( stderr, "Lock %s fd=%p proc=%p start=", |
| 766 | lock->shared ? "shared" : "excl", lock->fd, lock->process ); |
| 767 | DUMP_LONG_LONG( lock->start ); |
| 768 | fprintf( stderr, " end=" ); |
| 769 | DUMP_LONG_LONG( lock->end ); |
| 770 | fprintf( stderr, "\n" ); |
| 771 | } |
| 772 | |
| 773 | static int file_lock_signaled( struct object *obj, struct thread *thread ) |
| 774 | { |
| 775 | struct file_lock *lock = (struct file_lock *)obj; |
| 776 | /* lock is signaled if it has lost its owner */ |
| 777 | return !lock->process; |
| 778 | } |
| 779 | |
| 780 | /* set (or remove) a Unix lock if possible for the given range */ |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 781 | static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 782 | { |
| 783 | struct flock fl; |
| 784 | |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 785 | if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */ |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 786 | for (;;) |
| 787 | { |
| 788 | if (start == end) return 1; /* can't set zero-byte lock */ |
| 789 | if (start > max_unix_offset) return 1; /* ignore it */ |
| 790 | fl.l_type = type; |
| 791 | fl.l_whence = SEEK_SET; |
| 792 | fl.l_start = start; |
| 793 | if (!end || end > max_unix_offset) fl.l_len = 0; |
| 794 | else fl.l_len = end - start; |
| 795 | if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1; |
| 796 | |
| 797 | switch(errno) |
| 798 | { |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 799 | case EACCES: |
| 800 | /* check whether locks work at all on this file system */ |
| 801 | if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1) |
| 802 | { |
| 803 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
| 804 | return 0; |
| 805 | } |
| 806 | /* fall through */ |
Alexandre Julliard | b932723 | 2003-05-11 02:45:33 +0000 | [diff] [blame] | 807 | case EIO: |
| 808 | case ENOLCK: |
| 809 | /* no locking on this fs, just ignore it */ |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 810 | fd->fs_locks = 0; |
Alexandre Julliard | b932723 | 2003-05-11 02:45:33 +0000 | [diff] [blame] | 811 | return 1; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 812 | case EAGAIN: |
| 813 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
| 814 | return 0; |
Alexandre Julliard | d71ba93 | 2003-03-21 21:31:35 +0000 | [diff] [blame] | 815 | case EBADF: |
| 816 | /* this can happen if we try to set a write lock on a read-only file */ |
| 817 | /* we just ignore that error */ |
| 818 | if (fl.l_type == F_WRLCK) return 1; |
| 819 | set_error( STATUS_ACCESS_DENIED ); |
| 820 | return 0; |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 821 | #ifdef EOVERFLOW |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 822 | case EOVERFLOW: |
Wim Lewis | 99a01c0 | 2004-01-02 20:11:35 +0000 | [diff] [blame] | 823 | #endif |
Alexandre Julliard | b932723 | 2003-05-11 02:45:33 +0000 | [diff] [blame] | 824 | case EINVAL: |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 825 | /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */ |
| 826 | /* in that case we shrink the limit and retry */ |
| 827 | if (max_unix_offset > INT_MAX) |
| 828 | { |
| 829 | max_unix_offset = INT_MAX; |
| 830 | break; /* retry */ |
| 831 | } |
| 832 | /* fall through */ |
| 833 | default: |
| 834 | file_set_error(); |
| 835 | return 0; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* check if interval [start;end) overlaps the lock */ |
| 841 | inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end ) |
| 842 | { |
| 843 | if (lock->end && start >= lock->end) return 0; |
| 844 | if (end && lock->start >= end) return 0; |
| 845 | return 1; |
| 846 | } |
| 847 | |
| 848 | /* remove Unix locks for all bytes in the specified area that are no longer locked */ |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 849 | static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end ) |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 850 | { |
| 851 | struct hole |
| 852 | { |
| 853 | struct hole *next; |
| 854 | struct hole *prev; |
| 855 | file_pos_t start; |
| 856 | file_pos_t end; |
| 857 | } *first, *cur, *next, *buffer; |
| 858 | |
| 859 | struct list *ptr; |
| 860 | int count = 0; |
| 861 | |
| 862 | if (!fd->inode) return; |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 863 | if (!fd->fs_locks) return; |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 864 | if (start == end || start > max_unix_offset) return; |
| 865 | if (!end || end > max_unix_offset) end = max_unix_offset + 1; |
| 866 | |
| 867 | /* count the number of locks overlapping the specified area */ |
| 868 | |
| 869 | LIST_FOR_EACH( ptr, &fd->inode->locks ) |
| 870 | { |
| 871 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry ); |
| 872 | if (lock->start == lock->end) continue; |
| 873 | if (lock_overlaps( lock, start, end )) count++; |
| 874 | } |
| 875 | |
| 876 | if (!count) /* no locks at all, we can unlock everything */ |
| 877 | { |
| 878 | set_unix_lock( fd, start, end, F_UNLCK ); |
| 879 | return; |
| 880 | } |
| 881 | |
| 882 | /* allocate space for the list of holes */ |
| 883 | /* max. number of holes is number of locks + 1 */ |
| 884 | |
| 885 | if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return; |
| 886 | first = buffer; |
| 887 | first->next = NULL; |
| 888 | first->prev = NULL; |
| 889 | first->start = start; |
| 890 | first->end = end; |
| 891 | next = first + 1; |
| 892 | |
| 893 | /* build a sorted list of unlocked holes in the specified area */ |
| 894 | |
| 895 | LIST_FOR_EACH( ptr, &fd->inode->locks ) |
| 896 | { |
| 897 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry ); |
| 898 | if (lock->start == lock->end) continue; |
| 899 | if (!lock_overlaps( lock, start, end )) continue; |
| 900 | |
| 901 | /* go through all the holes touched by this lock */ |
| 902 | for (cur = first; cur; cur = cur->next) |
| 903 | { |
| 904 | if (cur->end <= lock->start) continue; /* hole is before start of lock */ |
| 905 | if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */ |
| 906 | |
| 907 | /* now we know that lock is overlapping hole */ |
| 908 | |
| 909 | if (cur->start >= lock->start) /* lock starts before hole, shrink from start */ |
| 910 | { |
| 911 | cur->start = lock->end; |
| 912 | if (cur->start && cur->start < cur->end) break; /* done with this lock */ |
| 913 | /* now hole is empty, remove it */ |
| 914 | if (cur->next) cur->next->prev = cur->prev; |
| 915 | if (cur->prev) cur->prev->next = cur->next; |
| 916 | else if (!(first = cur->next)) goto done; /* no more holes at all */ |
| 917 | } |
| 918 | else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */ |
| 919 | { |
| 920 | cur->end = lock->start; |
| 921 | assert( cur->start < cur->end ); |
| 922 | } |
| 923 | else /* lock is in the middle of hole, split hole in two */ |
| 924 | { |
| 925 | next->prev = cur; |
| 926 | next->next = cur->next; |
| 927 | cur->next = next; |
| 928 | next->start = lock->end; |
| 929 | next->end = cur->end; |
| 930 | cur->end = lock->start; |
| 931 | assert( next->start < next->end ); |
| 932 | assert( cur->end < next->start ); |
| 933 | next++; |
| 934 | break; /* done with this lock */ |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* clear Unix locks for all the holes */ |
| 940 | |
| 941 | for (cur = first; cur; cur = cur->next) |
| 942 | set_unix_lock( fd, cur->start, cur->end, F_UNLCK ); |
| 943 | |
| 944 | done: |
| 945 | free( buffer ); |
| 946 | } |
| 947 | |
| 948 | /* create a new lock on a fd */ |
| 949 | static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end ) |
| 950 | { |
| 951 | struct file_lock *lock; |
| 952 | |
| 953 | if (!fd->inode) /* not a regular file */ |
| 954 | { |
| 955 | set_error( STATUS_INVALID_HANDLE ); |
| 956 | return NULL; |
| 957 | } |
| 958 | |
| 959 | if (!(lock = alloc_object( &file_lock_ops ))) return NULL; |
| 960 | lock->shared = shared; |
| 961 | lock->start = start; |
| 962 | lock->end = end; |
| 963 | lock->fd = fd; |
| 964 | lock->process = current->process; |
| 965 | |
| 966 | /* now try to set a Unix lock */ |
| 967 | if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK )) |
| 968 | { |
| 969 | release_object( lock ); |
| 970 | return NULL; |
| 971 | } |
| 972 | list_add_head( &fd->locks, &lock->fd_entry ); |
| 973 | list_add_head( &fd->inode->locks, &lock->inode_entry ); |
| 974 | list_add_head( &lock->process->locks, &lock->proc_entry ); |
| 975 | return lock; |
| 976 | } |
| 977 | |
| 978 | /* remove an existing lock */ |
| 979 | static void remove_lock( struct file_lock *lock, int remove_unix ) |
| 980 | { |
| 981 | struct inode *inode = lock->fd->inode; |
| 982 | |
| 983 | list_remove( &lock->fd_entry ); |
| 984 | list_remove( &lock->inode_entry ); |
| 985 | list_remove( &lock->proc_entry ); |
| 986 | if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 987 | if (list_empty( &inode->locks )) inode_close_pending( inode, 1 ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 988 | lock->process = NULL; |
| 989 | wake_up( &lock->obj, 0 ); |
| 990 | release_object( lock ); |
| 991 | } |
| 992 | |
| 993 | /* remove all locks owned by a given process */ |
| 994 | void remove_process_locks( struct process *process ) |
| 995 | { |
| 996 | struct list *ptr; |
| 997 | |
| 998 | while ((ptr = list_head( &process->locks ))) |
| 999 | { |
| 1000 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry ); |
| 1001 | remove_lock( lock, 1 ); /* this removes it from the list */ |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* remove all locks on a given fd */ |
| 1006 | static void remove_fd_locks( struct fd *fd ) |
| 1007 | { |
| 1008 | file_pos_t start = FILE_POS_T_MAX, end = 0; |
| 1009 | struct list *ptr; |
| 1010 | |
| 1011 | while ((ptr = list_head( &fd->locks ))) |
| 1012 | { |
| 1013 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry ); |
| 1014 | if (lock->start < start) start = lock->start; |
| 1015 | if (!lock->end || lock->end > end) end = lock->end - 1; |
| 1016 | remove_lock( lock, 0 ); |
| 1017 | } |
| 1018 | if (start < end) remove_unix_locks( fd, start, end + 1 ); |
| 1019 | } |
| 1020 | |
| 1021 | /* add a lock on an fd */ |
| 1022 | /* returns handle to wait on */ |
| 1023 | obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait ) |
| 1024 | { |
| 1025 | struct list *ptr; |
| 1026 | file_pos_t end = start + count; |
| 1027 | |
| 1028 | /* don't allow wrapping locks */ |
| 1029 | if (end && end < start) |
| 1030 | { |
| 1031 | set_error( STATUS_INVALID_PARAMETER ); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | /* check if another lock on that file overlaps the area */ |
| 1036 | LIST_FOR_EACH( ptr, &fd->inode->locks ) |
| 1037 | { |
| 1038 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry ); |
| 1039 | if (!lock_overlaps( lock, start, end )) continue; |
| 1040 | if (lock->shared && shared) continue; |
| 1041 | /* found one */ |
| 1042 | if (!wait) |
| 1043 | { |
| 1044 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | set_error( STATUS_PENDING ); |
| 1048 | return alloc_handle( current->process, lock, SYNCHRONIZE, 0 ); |
| 1049 | } |
| 1050 | |
| 1051 | /* not found, add it */ |
| 1052 | if (add_lock( fd, shared, start, end )) return 0; |
| 1053 | if (get_error() == STATUS_FILE_LOCK_CONFLICT) |
| 1054 | { |
| 1055 | /* Unix lock conflict -> tell client to wait and retry */ |
| 1056 | if (wait) set_error( STATUS_PENDING ); |
| 1057 | } |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | /* remove a lock on an fd */ |
| 1062 | void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count ) |
| 1063 | { |
| 1064 | struct list *ptr; |
| 1065 | file_pos_t end = start + count; |
| 1066 | |
| 1067 | /* find an existing lock with the exact same parameters */ |
| 1068 | LIST_FOR_EACH( ptr, &fd->locks ) |
| 1069 | { |
| 1070 | struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry ); |
| 1071 | if ((lock->start == start) && (lock->end == end)) |
| 1072 | { |
| 1073 | remove_lock( lock, 1 ); |
| 1074 | return; |
| 1075 | } |
| 1076 | } |
| 1077 | set_error( STATUS_FILE_LOCK_CONFLICT ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1081 | /****************************************************************/ |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1082 | /* asynchronous operations support */ |
| 1083 | |
| 1084 | struct async |
| 1085 | { |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1086 | struct thread *thread; |
| 1087 | void *apc; |
| 1088 | void *user; |
| 1089 | void *sb; |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1090 | struct timeout_user *timeout; |
Alexandre Julliard | dd81ac5 | 2005-02-24 17:06:31 +0000 | [diff] [blame] | 1091 | struct list entry; |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1092 | }; |
| 1093 | |
Alexandre Julliard | dd81ac5 | 2005-02-24 17:06:31 +0000 | [diff] [blame] | 1094 | /* notifies client thread of new status of its async request */ |
| 1095 | /* destroys the server side of it */ |
| 1096 | static void async_terminate( struct async *async, int status ) |
| 1097 | { |
| 1098 | thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO, |
| 1099 | 1, async->user, async->sb, (void *)status ); |
| 1100 | |
| 1101 | if (async->timeout) remove_timeout_user( async->timeout ); |
| 1102 | async->timeout = NULL; |
| 1103 | list_remove( &async->entry ); |
| 1104 | release_object( async->thread ); |
| 1105 | free( async ); |
| 1106 | } |
| 1107 | |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1108 | /* cb for timeout on an async request */ |
| 1109 | static void async_callback(void *private) |
| 1110 | { |
| 1111 | struct async *async = (struct async *)private; |
| 1112 | |
| 1113 | /* fprintf(stderr, "async timeout out %p\n", async); */ |
| 1114 | async->timeout = NULL; |
| 1115 | async_terminate( async, STATUS_TIMEOUT ); |
| 1116 | } |
| 1117 | |
| 1118 | /* create an async on a given queue of a fd */ |
Eric Pouech | 7001d6e | 2005-03-29 11:40:10 +0000 | [diff] [blame] | 1119 | struct async *create_async(struct thread *thread, int* timeout, struct list *queue, |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1120 | void *io_apc, void *io_user, void* io_sb) |
| 1121 | { |
| 1122 | struct async *async = mem_alloc( sizeof(struct async) ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (!async) return NULL; |
| 1125 | |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1126 | async->thread = (struct thread *)grab_object(thread); |
| 1127 | async->apc = io_apc; |
| 1128 | async->user = io_user; |
| 1129 | async->sb = io_sb; |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1130 | |
Alexandre Julliard | dd81ac5 | 2005-02-24 17:06:31 +0000 | [diff] [blame] | 1131 | list_add_tail( queue, &async->entry ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1132 | |
| 1133 | if (timeout) |
| 1134 | { |
Eric Pouech | 7001d6e | 2005-03-29 11:40:10 +0000 | [diff] [blame] | 1135 | struct timeval when; |
| 1136 | |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 1137 | gettimeofday( &when, NULL ); |
Eric Pouech | 7001d6e | 2005-03-29 11:40:10 +0000 | [diff] [blame] | 1138 | add_timeout( &when, *timeout ); |
| 1139 | async->timeout = add_timeout_user( &when, async_callback, async ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1140 | } |
| 1141 | else async->timeout = NULL; |
| 1142 | |
| 1143 | return async; |
| 1144 | } |
| 1145 | |
Alexandre Julliard | dd81ac5 | 2005-02-24 17:06:31 +0000 | [diff] [blame] | 1146 | /* terminate the async operation at the head of the queue */ |
| 1147 | void async_terminate_head( struct list *queue, int status ) |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1148 | { |
Alexandre Julliard | dd81ac5 | 2005-02-24 17:06:31 +0000 | [diff] [blame] | 1149 | struct list *ptr = list_head( queue ); |
| 1150 | if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /****************************************************************/ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1154 | /* file descriptor functions */ |
| 1155 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1156 | static void fd_dump( struct object *obj, int verbose ) |
| 1157 | { |
| 1158 | struct fd *fd = (struct fd *)obj; |
Alexandre Julliard | e432d54 | 2005-02-03 10:45:18 +0000 | [diff] [blame] | 1159 | fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user ); |
| 1160 | if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink ); |
| 1161 | fprintf( stderr, "\n" ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static void fd_destroy( struct object *obj ) |
| 1165 | { |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1166 | struct fd *fd = (struct fd *)obj; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1167 | |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1168 | async_terminate_queue( &fd->read_q, STATUS_CANCELLED ); |
| 1169 | async_terminate_queue( &fd->write_q, STATUS_CANCELLED ); |
| 1170 | |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1171 | remove_fd_locks( fd ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1172 | list_remove( &fd->inode_entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1173 | if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1174 | if (fd->inode) |
| 1175 | { |
| 1176 | inode_add_closed_fd( fd->inode, fd->closed ); |
| 1177 | release_object( fd->inode ); |
| 1178 | } |
| 1179 | else /* no inode, close it right away */ |
| 1180 | { |
| 1181 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
| 1182 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1185 | /* set the events that select waits for on this fd */ |
| 1186 | void set_fd_events( struct fd *fd, int events ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1187 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1188 | int user = fd->poll_index; |
| 1189 | assert( poll_users[user] == fd ); |
Alexandre Julliard | 969f57c | 2004-09-23 04:48:24 +0000 | [diff] [blame] | 1190 | |
| 1191 | set_fd_epoll_events( fd, user, events ); |
| 1192 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1193 | if (events == -1) /* stop waiting on this fd completely */ |
| 1194 | { |
| 1195 | pollfd[user].fd = -1; |
| 1196 | pollfd[user].events = POLLERR; |
| 1197 | pollfd[user].revents = 0; |
| 1198 | } |
| 1199 | else if (pollfd[user].fd != -1 || !pollfd[user].events) |
| 1200 | { |
| 1201 | pollfd[user].fd = fd->unix_fd; |
| 1202 | pollfd[user].events = events; |
| 1203 | } |
| 1204 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1205 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1206 | /* prepare an fd for unmounting its corresponding device */ |
| 1207 | static inline void unmount_fd( struct fd *fd ) |
| 1208 | { |
| 1209 | assert( fd->inode ); |
| 1210 | |
| 1211 | async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED ); |
| 1212 | async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED ); |
| 1213 | |
| 1214 | if (fd->poll_index != -1) set_fd_events( fd, -1 ); |
| 1215 | |
| 1216 | if (fd->unix_fd != -1) close( fd->unix_fd ); |
| 1217 | |
| 1218 | fd->unix_fd = -1; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1219 | fd->unmounted = 1; |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1220 | fd->closed->unix_fd = -1; |
| 1221 | fd->closed->unlink[0] = 0; |
| 1222 | |
| 1223 | /* stop using Unix locks on this fd (existing locks have been removed by close) */ |
| 1224 | fd->fs_locks = 0; |
| 1225 | } |
| 1226 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1227 | /* allocate an fd object, without setting the unix fd yet */ |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1228 | static struct fd *alloc_fd_object(void) |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1229 | { |
| 1230 | struct fd *fd = alloc_object( &fd_ops ); |
| 1231 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1232 | if (!fd) return NULL; |
| 1233 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1234 | fd->fd_ops = NULL; |
| 1235 | fd->user = NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1236 | fd->inode = NULL; |
| 1237 | fd->closed = NULL; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1238 | fd->access = 0; |
| 1239 | fd->sharing = 0; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1240 | fd->unix_fd = -1; |
Alexandre Julliard | b23aa94 | 2003-07-03 18:12:02 +0000 | [diff] [blame] | 1241 | fd->fs_locks = 1; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1242 | fd->unmounted = 0; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1243 | fd->poll_index = -1; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1244 | list_init( &fd->inode_entry ); |
Alexandre Julliard | ce61349 | 2003-03-18 05:04:33 +0000 | [diff] [blame] | 1245 | list_init( &fd->locks ); |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1246 | list_init( &fd->read_q ); |
| 1247 | list_init( &fd->write_q ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1248 | |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1249 | if ((fd->poll_index = add_poll_user( fd )) == -1) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1250 | { |
| 1251 | release_object( fd ); |
| 1252 | return NULL; |
| 1253 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1254 | return fd; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1257 | /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */ |
| 1258 | struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user ) |
| 1259 | { |
| 1260 | struct fd *fd = alloc_object( &fd_ops ); |
| 1261 | |
| 1262 | if (!fd) return NULL; |
| 1263 | |
| 1264 | fd->fd_ops = fd_user_ops; |
| 1265 | fd->user = user; |
| 1266 | fd->inode = NULL; |
| 1267 | fd->closed = NULL; |
| 1268 | fd->access = 0; |
| 1269 | fd->sharing = 0; |
| 1270 | fd->unix_fd = -1; |
| 1271 | fd->fs_locks = 0; |
| 1272 | fd->unmounted = 0; |
| 1273 | fd->poll_index = -1; |
| 1274 | list_init( &fd->inode_entry ); |
| 1275 | list_init( &fd->locks ); |
| 1276 | list_init( &fd->read_q ); |
| 1277 | list_init( &fd->write_q ); |
| 1278 | return fd; |
| 1279 | } |
| 1280 | |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1281 | /* check if the desired access is possible without violating */ |
| 1282 | /* the sharing mode of other opens of the same file */ |
| 1283 | static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing ) |
| 1284 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1285 | unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1286 | unsigned int existing_access = 0; |
| 1287 | struct list *ptr; |
| 1288 | |
| 1289 | /* if access mode is 0, sharing mode is ignored */ |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1290 | if (!access) sharing = existing_sharing; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1291 | fd->access = access; |
| 1292 | fd->sharing = sharing; |
| 1293 | |
| 1294 | LIST_FOR_EACH( ptr, &fd->inode->open ) |
| 1295 | { |
| 1296 | struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry ); |
| 1297 | if (fd_ptr != fd) |
| 1298 | { |
| 1299 | existing_sharing &= fd_ptr->sharing; |
| 1300 | existing_access |= fd_ptr->access; |
| 1301 | } |
| 1302 | } |
| 1303 | |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 1304 | if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0; |
| 1305 | if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0; |
Alexandre Julliard | 8b0feb2 | 2006-01-27 15:50:38 +0100 | [diff] [blame] | 1306 | if ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)) return 0; |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 1307 | if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0; |
| 1308 | if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0; |
Alexandre Julliard | 8b0feb2 | 2006-01-27 15:50:38 +0100 | [diff] [blame] | 1309 | if ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)) return 0; |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1310 | return 1; |
| 1311 | } |
| 1312 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1313 | /* sets the user of an fd that previously had no user */ |
| 1314 | void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user ) |
| 1315 | { |
| 1316 | assert( fd->fd_ops == NULL ); |
| 1317 | fd->fd_ops = user_ops; |
| 1318 | fd->user = user; |
| 1319 | } |
| 1320 | |
| 1321 | /* open() wrapper that returns a struct fd with no fd user set */ |
| 1322 | struct fd *open_fd( const char *name, int flags, mode_t *mode, unsigned int access, |
| 1323 | unsigned int sharing, unsigned int options ) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1324 | { |
| 1325 | struct stat st; |
| 1326 | struct closed_fd *closed_fd; |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1327 | struct fd *fd; |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1328 | const char *unlink_name = ""; |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1329 | int rw_mode; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1330 | |
Alexandre Julliard | 8b0feb2 | 2006-01-27 15:50:38 +0100 | [diff] [blame] | 1331 | if ((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) |
| 1332 | { |
| 1333 | set_error( STATUS_INVALID_PARAMETER ); |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1337 | if (!(fd = alloc_fd_object())) return NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1338 | |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1339 | if (options & FILE_DELETE_ON_CLOSE) unlink_name = name; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1340 | if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) ))) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1341 | { |
| 1342 | release_object( fd ); |
| 1343 | return NULL; |
| 1344 | } |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1345 | |
Alexandre Julliard | ad9b799 | 2004-04-27 02:27:09 +0000 | [diff] [blame] | 1346 | /* create the directory if needed */ |
| 1347 | if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT)) |
| 1348 | { |
| 1349 | if (mkdir( name, 0777 ) == -1) |
| 1350 | { |
| 1351 | if (errno != EEXIST || (flags & O_EXCL)) |
| 1352 | { |
| 1353 | file_set_error(); |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1354 | goto error; |
Alexandre Julliard | ad9b799 | 2004-04-27 02:27:09 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); |
| 1358 | } |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1359 | |
| 1360 | if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE)) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1361 | { |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1362 | if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR; |
| 1363 | else rw_mode = O_WRONLY; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1364 | } |
Alexandre Julliard | 471782a | 2006-01-25 15:06:48 +0100 | [diff] [blame] | 1365 | else rw_mode = O_RDONLY; |
| 1366 | |
| 1367 | if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1) |
| 1368 | { |
| 1369 | /* if we tried to open a directory for write access, retry read-only */ |
| 1370 | if (errno != EISDIR || |
| 1371 | !(access & FILE_UNIX_WRITE_ACCESS) || |
| 1372 | (fd->unix_fd = open( name, O_RDONLY | (flags & ~O_TRUNC), *mode )) == -1) |
| 1373 | { |
| 1374 | file_set_error(); |
| 1375 | goto error; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Alexandre Julliard | c132542 | 2005-07-28 10:48:57 +0000 | [diff] [blame] | 1379 | closed_fd->unix_fd = fd->unix_fd; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1380 | closed_fd->unlink[0] = 0; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1381 | fstat( fd->unix_fd, &st ); |
| 1382 | *mode = st.st_mode; |
| 1383 | |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1384 | /* only bother with an inode for normal files and directories */ |
| 1385 | if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1386 | { |
| 1387 | struct inode *inode = get_inode( st.st_dev, st.st_ino ); |
| 1388 | |
| 1389 | if (!inode) |
| 1390 | { |
| 1391 | /* we can close the fd because there are no others open on the same file, |
| 1392 | * otherwise we wouldn't have failed to allocate a new inode |
| 1393 | */ |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1394 | goto error; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1395 | } |
| 1396 | fd->inode = inode; |
| 1397 | fd->closed = closed_fd; |
| 1398 | list_add_head( &inode->open, &fd->inode_entry ); |
Alexandre Julliard | 17c775f | 2004-09-09 00:28:36 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* check directory options */ |
| 1401 | if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode)) |
| 1402 | { |
| 1403 | release_object( fd ); |
| 1404 | set_error( STATUS_NOT_A_DIRECTORY ); |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode)) |
| 1408 | { |
| 1409 | release_object( fd ); |
| 1410 | set_error( STATUS_FILE_IS_A_DIRECTORY ); |
| 1411 | return NULL; |
| 1412 | } |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1413 | if (!check_sharing( fd, access, sharing )) |
| 1414 | { |
| 1415 | release_object( fd ); |
| 1416 | set_error( STATUS_SHARING_VIOLATION ); |
| 1417 | return NULL; |
| 1418 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1419 | strcpy( closed_fd->unlink, unlink_name ); |
Alexandre Julliard | 60287d0 | 2004-05-22 03:15:04 +0000 | [diff] [blame] | 1420 | if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1421 | } |
Alexandre Julliard | 17c775f | 2004-09-09 00:28:36 +0000 | [diff] [blame] | 1422 | else /* special file */ |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1423 | { |
Alexandre Julliard | 17c775f | 2004-09-09 00:28:36 +0000 | [diff] [blame] | 1424 | if (options & FILE_DIRECTORY_FILE) |
| 1425 | { |
| 1426 | set_error( STATUS_NOT_A_DIRECTORY ); |
| 1427 | goto error; |
| 1428 | } |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1429 | if (unlink_name[0]) /* we can't unlink special files */ |
| 1430 | { |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1431 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1432 | goto error; |
Alexandre Julliard | 9ff7872 | 2004-04-02 19:50:49 +0000 | [diff] [blame] | 1433 | } |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1434 | free( closed_fd ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1435 | } |
| 1436 | return fd; |
Alexandre Julliard | 684b65c | 2004-04-16 04:31:35 +0000 | [diff] [blame] | 1437 | |
| 1438 | error: |
| 1439 | release_object( fd ); |
| 1440 | free( closed_fd ); |
| 1441 | return NULL; |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* create an fd for an anonymous file */ |
| 1445 | /* if the function fails the unix fd is closed */ |
| 1446 | struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user ) |
| 1447 | { |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1448 | struct fd *fd = alloc_fd_object(); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (fd) |
| 1451 | { |
Mike McCormack | 9a7124e | 2006-01-24 13:30:55 +0100 | [diff] [blame] | 1452 | set_fd_user( fd, fd_user_ops, user ); |
Alexandre Julliard | 580da24 | 2003-03-12 22:38:14 +0000 | [diff] [blame] | 1453 | fd->unix_fd = unix_fd; |
| 1454 | return fd; |
| 1455 | } |
| 1456 | close( unix_fd ); |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1460 | /* retrieve the object that is using an fd */ |
| 1461 | void *get_fd_user( struct fd *fd ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1462 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1463 | return fd->user; |
| 1464 | } |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1465 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1466 | /* retrieve the unix fd for an object */ |
| 1467 | int get_unix_fd( struct fd *fd ) |
| 1468 | { |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1469 | if (fd->unix_fd == -1) |
| 1470 | { |
| 1471 | if (fd->unmounted) set_error( STATUS_VOLUME_DISMOUNTED ); |
| 1472 | else set_error( STATUS_BAD_DEVICE_TYPE ); |
| 1473 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1474 | return fd->unix_fd; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Alexandre Julliard | 74bd1e4 | 2004-03-27 20:48:42 +0000 | [diff] [blame] | 1477 | /* check if two file descriptors point to the same file */ |
| 1478 | int is_same_file_fd( struct fd *fd1, struct fd *fd2 ) |
| 1479 | { |
| 1480 | return fd1->inode == fd2->inode; |
| 1481 | } |
| 1482 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1483 | /* callback for event happening in the main poll() loop */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1484 | void fd_poll_event( struct fd *fd, int event ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1485 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1486 | return fd->fd_ops->poll_event( fd, event ); |
| 1487 | } |
| 1488 | |
| 1489 | /* check if events are pending and if yes return which one(s) */ |
| 1490 | int check_fd_events( struct fd *fd, int events ) |
| 1491 | { |
| 1492 | struct pollfd pfd; |
| 1493 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1494 | if (fd->unix_fd == -1) return POLLERR; |
| 1495 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1496 | pfd.fd = fd->unix_fd; |
| 1497 | pfd.events = events; |
| 1498 | if (poll( &pfd, 1, 0 ) <= 0) return 0; |
| 1499 | return pfd.revents; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /* default add_queue() routine for objects that poll() on an fd */ |
| 1503 | int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry ) |
| 1504 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1505 | struct fd *fd = get_obj_fd( obj ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1506 | |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1507 | if (!fd) return 0; |
Alexandre Julliard | 770c4a1 | 2005-12-12 17:20:03 +0100 | [diff] [blame] | 1508 | if (!fd->inode && list_empty( &obj->wait_queue )) /* first on the queue */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1509 | set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1510 | add_queue( obj, entry ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1511 | release_object( fd ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1512 | return 1; |
| 1513 | } |
| 1514 | |
| 1515 | /* default remove_queue() routine for objects that poll() on an fd */ |
| 1516 | void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry ) |
| 1517 | { |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1518 | struct fd *fd = get_obj_fd( obj ); |
| 1519 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1520 | grab_object( obj ); |
| 1521 | remove_queue( obj, entry ); |
Alexandre Julliard | 770c4a1 | 2005-12-12 17:20:03 +0100 | [diff] [blame] | 1522 | if (!fd->inode && list_empty( &obj->wait_queue )) /* last on the queue is gone */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1523 | set_fd_events( fd, 0 ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1524 | release_object( obj ); |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1525 | release_object( fd ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /* default signaled() routine for objects that poll() on an fd */ |
| 1529 | int default_fd_signaled( struct object *obj, struct thread *thread ) |
| 1530 | { |
Alexandre Julliard | b1aad5d | 2005-02-25 14:02:20 +0000 | [diff] [blame] | 1531 | int events, ret; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1532 | struct fd *fd = get_obj_fd( obj ); |
Alexandre Julliard | b1aad5d | 2005-02-25 14:02:20 +0000 | [diff] [blame] | 1533 | |
Mike McCormack | 213e01e | 2006-01-17 13:13:14 +0100 | [diff] [blame] | 1534 | if (fd->inode) ret = 1; /* regular files are always signaled */ |
| 1535 | else |
| 1536 | { |
| 1537 | events = fd->fd_ops->get_poll_events( fd ); |
| 1538 | ret = check_fd_events( fd, events ) != 0; |
Alexandre Julliard | b1aad5d | 2005-02-25 14:02:20 +0000 | [diff] [blame] | 1539 | |
Mike McCormack | 213e01e | 2006-01-17 13:13:14 +0100 | [diff] [blame] | 1540 | if (ret) |
| 1541 | { |
| 1542 | /* stop waiting on select() if we are signaled */ |
| 1543 | set_fd_events( fd, 0 ); |
| 1544 | } |
| 1545 | else if (!list_empty( &obj->wait_queue )) |
| 1546 | { |
| 1547 | /* restart waiting on poll() if we are no longer signaled */ |
| 1548 | set_fd_events( fd, events ); |
| 1549 | } |
| 1550 | } |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1551 | release_object( fd ); |
| 1552 | return ret; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1555 | int default_fd_get_poll_events( struct fd *fd ) |
| 1556 | { |
| 1557 | int events = 0; |
| 1558 | |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 1559 | if (!list_empty( &fd->read_q )) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1560 | events |= POLLIN; |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 1561 | if (!list_empty( &fd->write_q )) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1562 | events |= POLLOUT; |
| 1563 | |
| 1564 | return events; |
| 1565 | } |
| 1566 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1567 | /* default handler for poll() events */ |
| 1568 | void default_poll_event( struct fd *fd, int event ) |
| 1569 | { |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1570 | if (!list_empty( &fd->read_q ) && (POLLIN & event) ) |
| 1571 | { |
| 1572 | async_terminate_head( &fd->read_q, STATUS_ALERTED ); |
| 1573 | return; |
| 1574 | } |
| 1575 | if (!list_empty( &fd->write_q ) && (POLLOUT & event) ) |
| 1576 | { |
| 1577 | async_terminate_head( &fd->write_q, STATUS_ALERTED ); |
| 1578 | return; |
| 1579 | } |
| 1580 | |
| 1581 | /* if an error occurred, stop polling this fd to avoid busy-looping */ |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 1582 | if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1583 | wake_up( fd->user, 0 ); |
| 1584 | } |
| 1585 | |
Robert Shearman | 37773dd | 2005-07-14 12:18:05 +0000 | [diff] [blame] | 1586 | void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count, int *timeout ) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1587 | { |
| 1588 | struct list *queue; |
| 1589 | int events; |
| 1590 | |
Robert Shearman | 37773dd | 2005-07-14 12:18:05 +0000 | [diff] [blame] | 1591 | if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT))) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1592 | { |
| 1593 | set_error( STATUS_INVALID_HANDLE ); |
| 1594 | return; |
| 1595 | } |
| 1596 | |
| 1597 | switch (type) |
| 1598 | { |
| 1599 | case ASYNC_TYPE_READ: |
| 1600 | queue = &fd->read_q; |
| 1601 | break; |
| 1602 | case ASYNC_TYPE_WRITE: |
| 1603 | queue = &fd->write_q; |
| 1604 | break; |
| 1605 | default: |
| 1606 | set_error( STATUS_INVALID_PARAMETER ); |
| 1607 | return; |
| 1608 | } |
| 1609 | |
Robert Shearman | 37773dd | 2005-07-14 12:18:05 +0000 | [diff] [blame] | 1610 | if (!create_async( current, timeout, queue, apc, user, io_sb )) |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1611 | return; |
| 1612 | |
| 1613 | /* Check if the new pending request can be served immediately */ |
| 1614 | events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) ); |
| 1615 | if (events) fd->fd_ops->poll_event( fd, events ); |
| 1616 | |
| 1617 | set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) ); |
| 1618 | } |
| 1619 | |
Robert Shearman | 37773dd | 2005-07-14 12:18:05 +0000 | [diff] [blame] | 1620 | void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count ) |
| 1621 | { |
| 1622 | fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL ); |
| 1623 | } |
| 1624 | |
Robert Shearman | d6a4e34 | 2005-06-07 20:09:01 +0000 | [diff] [blame] | 1625 | void default_fd_cancel_async( struct fd *fd ) |
| 1626 | { |
| 1627 | async_terminate_queue( &fd->read_q, STATUS_CANCELLED ); |
| 1628 | async_terminate_queue( &fd->write_q, STATUS_CANCELLED ); |
| 1629 | } |
| 1630 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1631 | /* default flush() routine */ |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1632 | int no_flush( struct fd *fd, struct event **event ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1633 | { |
| 1634 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1638 | /* default get_file_info() routine */ |
Alexandre Julliard | 6a27b48 | 2004-08-18 00:04:58 +0000 | [diff] [blame] | 1639 | int no_get_file_info( struct fd *fd ) |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1640 | { |
| 1641 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
Alexandre Julliard | 6a27b48 | 2004-08-18 00:04:58 +0000 | [diff] [blame] | 1642 | return 0; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1645 | /* default queue_async() routine */ |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1646 | void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb, |
| 1647 | int type, int count) |
| 1648 | { |
| 1649 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
| 1650 | } |
| 1651 | |
| 1652 | /* default cancel_async() routine */ |
| 1653 | void no_cancel_async( struct fd *fd ) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1654 | { |
| 1655 | set_error( STATUS_OBJECT_TYPE_MISMATCH ); |
| 1656 | } |
| 1657 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1658 | /* close all Unix file descriptors on a device to allow unmounting it */ |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1659 | static void unmount_device( struct fd *device_fd ) |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1660 | { |
| 1661 | unsigned int i; |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1662 | struct stat st; |
| 1663 | struct device *device; |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1664 | struct inode *inode; |
| 1665 | struct fd *fd; |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1666 | int unix_fd = get_unix_fd( device_fd ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1667 | |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1668 | if (unix_fd == -1) return; |
| 1669 | |
| 1670 | if (fstat( unix_fd, &st ) == -1 || !S_ISBLK( st.st_mode )) |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1671 | { |
| 1672 | set_error( STATUS_INVALID_PARAMETER ); |
| 1673 | return; |
| 1674 | } |
| 1675 | |
| 1676 | if (!(device = get_device( st.st_rdev, 0 ))) return; |
| 1677 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1678 | for (i = 0; i < INODE_HASH_SIZE; i++) |
| 1679 | { |
| 1680 | LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry ) |
| 1681 | { |
| 1682 | LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry ) |
| 1683 | { |
| 1684 | unmount_fd( fd ); |
| 1685 | } |
| 1686 | inode_close_pending( inode, 0 ); |
| 1687 | } |
| 1688 | } |
| 1689 | /* remove it from the hash table */ |
| 1690 | list_remove( &device->entry ); |
| 1691 | list_init( &device->entry ); |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1692 | release_object( device ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1695 | /* same as get_handle_obj but retrieve the struct fd associated to the object */ |
| 1696 | static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle, |
| 1697 | unsigned int access ) |
| 1698 | { |
| 1699 | struct fd *fd = NULL; |
| 1700 | struct object *obj; |
| 1701 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1702 | if ((obj = get_handle_obj( process, handle, access, NULL ))) |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1703 | { |
Bill Medland | 3f7c3ff | 2003-04-17 02:14:04 +0000 | [diff] [blame] | 1704 | fd = get_obj_fd( obj ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1705 | release_object( obj ); |
| 1706 | } |
| 1707 | return fd; |
| 1708 | } |
| 1709 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1710 | /* flush a file buffers */ |
| 1711 | DECL_HANDLER(flush_file) |
| 1712 | { |
| 1713 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1714 | struct event * event = NULL; |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1715 | |
| 1716 | if (fd) |
| 1717 | { |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1718 | fd->fd_ops->flush( fd, &event ); |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1719 | if ( event ) |
Mike McCormack | ef8b946 | 2003-05-15 04:22:45 +0000 | [diff] [blame] | 1720 | { |
| 1721 | reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 ); |
| 1722 | } |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1723 | release_object( fd ); |
| 1724 | } |
| 1725 | } |
| 1726 | |
Alexandre Julliard | 67505c0 | 2005-12-12 14:27:45 +0100 | [diff] [blame] | 1727 | /* open a file object */ |
| 1728 | DECL_HANDLER(open_file_object) |
| 1729 | { |
| 1730 | struct unicode_str name; |
| 1731 | struct directory *root = NULL; |
| 1732 | struct object *obj; |
| 1733 | |
| 1734 | get_req_unicode_str( &name ); |
| 1735 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 1736 | return; |
| 1737 | |
| 1738 | if ((obj = open_object_dir( root, &name, req->attributes, NULL ))) |
| 1739 | { |
| 1740 | /* make sure this is a valid file object */ |
| 1741 | struct fd *fd = get_obj_fd( obj ); |
| 1742 | if (fd) |
| 1743 | { |
| 1744 | reply->handle = alloc_handle( current->process, obj, req->access, req->attributes ); |
| 1745 | release_object( fd ); |
| 1746 | } |
| 1747 | release_object( obj ); |
| 1748 | } |
| 1749 | |
| 1750 | if (root) release_object( root ); |
| 1751 | } |
| 1752 | |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1753 | /* get a Unix fd to access a file */ |
| 1754 | DECL_HANDLER(get_handle_fd) |
| 1755 | { |
| 1756 | struct fd *fd; |
| 1757 | |
| 1758 | reply->fd = -1; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1759 | |
| 1760 | if ((fd = get_handle_fd_obj( current->process, req->handle, req->access ))) |
| 1761 | { |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1762 | int unix_fd = get_unix_fd( fd ); |
| 1763 | if (unix_fd != -1) |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1764 | { |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1765 | int cached_fd = get_handle_unix_fd( current->process, req->handle, req->access ); |
| 1766 | if (cached_fd != -1) reply->fd = cached_fd; |
| 1767 | else if (!get_error()) send_client_fd( current->process, unix_fd, req->handle ); |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1768 | } |
Alexandre Julliard | f62f6e8 | 2005-08-24 18:33:50 +0000 | [diff] [blame] | 1769 | if (fd->inode) reply->removable = fd->inode->device->removable; |
Alexandre Julliard | 6a27b48 | 2004-08-18 00:04:58 +0000 | [diff] [blame] | 1770 | reply->flags = fd->fd_ops->get_file_info( fd ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1771 | release_object( fd ); |
| 1772 | } |
| 1773 | } |
| 1774 | |
Alexandre Julliard | f62f6e8 | 2005-08-24 18:33:50 +0000 | [diff] [blame] | 1775 | /* set the cached file descriptor of a handle */ |
| 1776 | DECL_HANDLER(set_handle_fd) |
| 1777 | { |
| 1778 | struct fd *fd; |
| 1779 | |
| 1780 | reply->cur_fd = -1; |
| 1781 | if ((fd = get_handle_fd_obj( current->process, req->handle, 0 ))) |
| 1782 | { |
| 1783 | struct device *device = fd->inode ? fd->inode->device : NULL; |
| 1784 | |
| 1785 | if (device && device->removable == -1) device->removable = req->removable; |
| 1786 | |
| 1787 | /* only cache the fd on non-removable devices */ |
| 1788 | if (!device || !device->removable) |
| 1789 | reply->cur_fd = set_handle_unix_fd( current->process, req->handle, req->fd ); |
| 1790 | release_object( fd ); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1794 | /* get ready to unmount a Unix device */ |
| 1795 | DECL_HANDLER(unmount_device) |
| 1796 | { |
| 1797 | struct fd *fd; |
| 1798 | |
| 1799 | if ((fd = get_handle_fd_obj( current->process, req->handle, 0 ))) |
| 1800 | { |
Alexandre Julliard | f8037bd | 2005-10-27 11:20:50 +0000 | [diff] [blame] | 1801 | unmount_device( fd ); |
Alexandre Julliard | 964815b | 2005-08-08 15:11:03 +0000 | [diff] [blame] | 1802 | release_object( fd ); |
| 1803 | } |
| 1804 | } |
| 1805 | |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1806 | /* create / reschedule an async I/O */ |
| 1807 | DECL_HANDLER(register_async) |
| 1808 | { |
| 1809 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
| 1810 | |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1811 | /* |
| 1812 | * The queue_async method must do the following: |
| 1813 | * |
| 1814 | * 1. Get the async_queue for the request of given type. |
| 1815 | * 2. Create a new asynchronous request for the selected queue |
| 1816 | * 3. Carry out any operations necessary to adjust the object's poll events |
| 1817 | * Usually: set_elect_events (obj, obj->ops->get_poll_events()). |
| 1818 | * 4. When the async request is triggered, then send back (with a proper APC) |
| 1819 | * the trigger (STATUS_ALERTED) to the thread that posted the request. |
| 1820 | * async_destroy() is to be called: it will both notify the sender about |
| 1821 | * the trigger and destroy the request by itself |
| 1822 | * See also the implementations in file.c, serial.c, and sock.c. |
| 1823 | */ |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1824 | |
| 1825 | if (fd) |
| 1826 | { |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1827 | fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb, |
| 1828 | req->type, req->count ); |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 1829 | release_object( fd ); |
| 1830 | } |
| 1831 | } |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 1832 | |
| 1833 | /* cancels all async I/O */ |
| 1834 | DECL_HANDLER(cancel_async) |
| 1835 | { |
| 1836 | struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 ); |
| 1837 | if (fd) |
| 1838 | { |
| 1839 | /* Note: we don't kill the queued APC_ASYNC_IO on this thread because |
| 1840 | * NtCancelIoFile() will force the pending APC to be run. Since, |
| 1841 | * Windows only guarantees that the current thread will have no async |
| 1842 | * operation on the current fd when NtCancelIoFile returns, this shall |
| 1843 | * do the work. |
| 1844 | */ |
| 1845 | fd->fd_ops->cancel_async( fd ); |
| 1846 | release_object( fd ); |
| 1847 | } |
| 1848 | } |